Initial import to Gerrit.
[profile/ivi/festival.git] / packaging / festival-1.96-chroot.patch
1 --- src/main/festival_main.cc
2 +++ src/main/festival_main.cc
3 @@ -39,6 +39,10 @@
4  /*                                                                       */
5  /*=======================================================================*/
6  #include <cstdio>
7 +#include <sys/types.h>
8 +#include <unistd.h>
9 +#include <pwd.h>
10 +#include <grp.h>
11  
12  using namespace std;
13  
14 @@ -75,6 +79,9 @@
15      EST_StrList files;
16      int real_number_of_files = 0;
17      int heap_size = FESTIVAL_HEAP_SIZE;
18 +       unsigned int uid = -1;
19 +       unsigned int gid = -1;
20 +       struct passwd *pw;
21  
22      if (festival_check_script_mode(argc,argv) == TRUE)
23      {   // Need to check this directly as in script mode args are 
24 @@ -106,6 +113,9 @@
25         "              english, spanish and welsh are available\n"+
26         "--server      Run in server mode waiting for clients\n"+
27         "              of server_port (1314)\n"+
28 +       "--chroot <string> Run server in chroot\n"+
29 +       "--uid <string> Run server as given user\n"+
30 +       "--gid <int> Run server with this group\n"+
31         "--script <ifile>\n"+
32          "              Used in #! scripts, runs in batch mode on\n"+
33         "              file and passes all other args to Scheme\n"+
34 @@ -123,6 +133,77 @@
35         exit(0);
36      }
37  
38 +       if( al.present( "--uid" ) )
39 +       {
40 +               EST_String b = al.sval( "--uid" );
41 +
42 +               pw = getpwnam( b.str() );
43 +               if( pw != NULL )
44 +               {
45 +                       uid = pw->pw_uid;
46 +                       gid = pw->pw_gid;
47 +               }
48 +               else
49 +               {
50 +                       printf("unknow user\n");
51 +                       festival_error();
52 +               }
53 +       }
54 +
55 +       if( al.present( "--gid" ) )
56 +       {
57 +               gid = al.ival( "--gid" );
58 +               if( al.present( "--uid" ) )
59 +               {
60 +                       printf( "useless without --uid\n" );
61 +                       festival_error();
62 +               }
63 +       }
64 +
65 +       if( al.present( "--chroot" ) )
66 +       {
67 +               if( !al.present( "--uid" ) )
68 +               {
69 +                       printf( "chroot only makes sense in combination with uid switching\n" );
70 +                       festival_error();
71 +               }
72 +
73 +               EST_String a = al.sval( "--chroot" );
74 +               printf( "chroot to %s\n", a.str() );
75 +               if( chdir( a.str() ) )
76 +               {
77 +                       festival_error();
78 +               }
79 +               if( chroot( a.str() ) )
80 +               {
81 +               festival_error();
82 +               }
83 +               if( chdir( "/" ) )
84 +               {
85 +                       festival_error();
86 +               }
87 +       }
88 +
89 +       if( al.present( "--uid" ) )
90 +       {
91 +               if( setgroups( 1, &gid ) < 0 )
92 +               {
93 +                       festival_error();
94 +               }
95 +
96 +               if( setgid( gid ) != 0 )
97 +               {
98 +                       printf( "can't setgid\n" );
99 +                       festival_error();
100 +               }
101 +
102 +               if( setuid( uid ) != 0 )
103 +               {
104 +                       printf( "can't setuid\n" );
105 +                       festival_error();
106 +               }
107 +       }
108 +
109      if (al.present("--libdir"))
110         festival_libdir = wstrdup(al.val("--libdir"));
111      else if (getenv("FESTLIBDIR") != 0)