796fbbcddc47c915b6df60f1a18926a0ff67c3be
[captive.git] / src / client / sandbox-server / main.c
1 /* $Id$
2  * filesystem sandbox server stub for libcaptive
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <stdlib.h>
24 #include <glib/giochannel.h>
25 #include <glib/gerror.h>
26 #include <popt.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <locale.h>
30 #include "captive/options.h"
31 #include <glib-object.h>
32 #include "captive/macros.h"
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <dirent.h>
37 #include <errno.h>
38 #include <grp.h>
39 #include <pwd.h>
40 #include <fcntl.h>
41 #include <sys/file.h>
42 #include <sys/resource.h>
43 #include <linc/linc-protocol.h> /* for linc_set_tmpdir() */
44 #include <orbit/orb-core/corba-defs.h>
45
46
47 /* Do not: #include "../../libcaptive/sandbox/split.h"  * for captive_sandbox_fd_closeup(); FIXME *
48  * as it has libcaptive-dependent includes conditioned by ORBIT2.
49  * FIXME: Unify this declaration:
50  */
51 void captive_sandbox_fd_closeup(int fd_first_to_delete);
52 void captive_corba_sandbox_child(const gchar *chrooted_orbit_dir);
53
54
55 /* CONFIG: */
56
57 /* FIXME: We hit linc-1.0.1/src/linc-protocols.c/linc_protocol_get_sockaddr_unix()
58  * limit of socket pathname 64 characters.
59  * With CHROOT_PATH_HASHKEY_LENGTH 12 "linc-%x-%x-%x%x" still does not fit completely.
60  */
61 #define CHROOT_PATH_HASHKEY_LENGTH (12)
62
63
64 GQuark sandbox_server_main_error_quark(void)
65 {
66 GQuark r=0;
67
68         if (!r)
69                 r=g_quark_from_static_string("sandbox-server");
70
71         return r;
72 }
73
74
75 static gchar *optarg_setuid=CAPTIVE_SANDBOX_SETUID;
76 static gchar *optarg_setgid=CAPTIVE_SANDBOX_SETGID;
77 static gchar *optarg_chroot=CAPTIVE_SANDBOX_CHROOT;
78 static gint   optarg_no_rlimit=0;
79
80 static const struct poptOption popt_table[]={
81 #define SANDBOX_SERVER_POPT(longname,argInfoP,argP,descripP,argDescripP) \
82                 { \
83                         longName: (longname), \
84                         shortName: 0, \
85                         argInfo: (argInfoP), \
86                         arg: (void *)argP, \
87                         val: 0, \
88                         descrip: (descripP), \
89                         argDescrip: (argDescripP), \
90                 }
91
92                 SANDBOX_SERVER_POPT("setuid"   ,POPT_ARG_STRING,&optarg_setuid,
93                                 N_("Username or UID to become; \"-\" for disable"),N_("UID")),
94                 SANDBOX_SERVER_POPT("setgid"   ,POPT_ARG_STRING,&optarg_setgid,
95                                 N_("Groupname or GID to become; \"-\" for disable"),N_("GID")),
96                 SANDBOX_SERVER_POPT("chroot"   ,POPT_ARG_STRING,&optarg_chroot,
97                                 N_("Pathname to directory for chroot(2); \"-\" for disable"),N_("directory")),
98                 SANDBOX_SERVER_POPT("no-rlimit",POPT_ARG_NONE  ,&optarg_no_rlimit,
99                                 N_("Disable setrlimit(2) restrictions"),NULL),
100
101 #undef SANDBOX_SERVER_POPT
102                 POPT_AUTOHELP
103                 POPT_TABLEEND
104                 };
105
106
107 static gchar *fatal_argv0;
108
109 static void fatal(const char *fmt,...)
110 {
111 va_list ap;
112
113         fprintf(stderr,"%s: ",fatal_argv0);
114         va_start(ap,fmt);
115         vfprintf(stderr,fmt,ap);
116         va_end(ap);
117         fprintf(stderr,"!\nAborting!\n");
118         exit(EXIT_FAILURE);
119         /* NOTREACHED */
120         for (;;);
121 }
122
123 static void check_dir_safety(const gchar *dir)
124 {
125 gchar *local_dir;
126 const gchar *cs;
127 static gint depth=0;
128
129         if (++depth>=1000)
130                 fatal("Loop count >=%d during check_dir_safety(\"%s\")",depth,dir);
131
132         if (*dir!='/')
133                 fatal("chroot path \"%s\" not absolute",dir);
134         dir=captive_printf_alloca("%s/",dir);
135         local_dir=(gchar *)captive_strdup_alloca(dir);
136         for (cs=dir;cs;cs=strchr(cs+1,'/')) {
137 struct stat statbuf;
138
139                 g_assert(*cs=='/');
140                 /* Include the trailing '/' to resolve the root directory as "/". */
141                 memcpy(local_dir,dir,cs+1-dir);
142                 local_dir[cs+1-dir]=0;
143                 if (lstat(local_dir,&statbuf))
144                         fatal("lstat(\"%s\") of chroot path component: %m",local_dir);
145                 if (S_ISLNK(statbuf.st_mode)) {
146 char linkbuf[PATH_MAX];
147 int linkbuflen;
148
149                         if (0>(linkbuflen=readlink(local_dir,linkbuf,sizeof(linkbuf)-1)))
150                                 fatal("readlink(\"%s\") of chroot path component: %m",local_dir);
151                         linkbuf[linkbuflen]=0;
152                         check_dir_safety(linkbuf);
153                         if (stat(local_dir,&statbuf))   /* NOT lstat(2) */
154                                 fatal("stat(\"%s\") of chroot path component: %m",local_dir);
155                         }
156                 if (!S_ISDIR(statbuf.st_mode))
157                         fatal("lstat/stat(\"%s\") of chroot path component is !S_ISDIR",local_dir);
158                 if (statbuf.st_uid!=0)
159                         fatal("lstat/stat(\"%s\") of chroot path component has UID %d !=0",local_dir,(int)statbuf.st_uid);
160                 if (statbuf.st_gid!=0)
161                         fatal("lstat/stat(\"%s\") of chroot path component has GID %d !=0",local_dir,(int)statbuf.st_gid);
162                 if ((statbuf.st_mode&(S_IFDIR|S_ISVTX|0111)) != (S_IFDIR|0111))
163                         fatal("lstat/stat(\"%s\") of chroot path component has mode 0%o !=04[01]111",local_dir,(int)statbuf.st_mode);
164                 }
165
166         depth--;
167 }
168
169 static void chrooted_unlink_recursive(const gchar *pathname)
170 {
171 DIR *dir;
172 struct dirent *dirent;
173 static gint depth=0;
174 struct stat statbuf;
175
176         if (++depth>=1000)
177                 fatal("Loop count >=%d during chrooted_unlink_recursive(\"%s\")",depth,pathname);
178
179         /* Security: Do not allow anyone to escape the sandbox directory by symlinks. */
180         if (lstat(pathname,&statbuf))
181                 fatal("Cannot lstat(\"%s\") to delete leftover sandbox files: %m",pathname);
182         if (!S_ISDIR(statbuf.st_mode)) {
183                 if (unlink(pathname))
184                         fatal("Cannot unlink(\"%s\") to delete leftover sandbox files: %m",pathname);
185                 goto done;
186                 }
187         if (!(dir=opendir(pathname)))
188                 fatal("Cannot opendir(\"%s\") to delete leftover sandbox files: %m",pathname);
189         while (errno=0,(dirent=readdir(dir))) {
190 gchar *dirent_path;
191
192                 if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
193                         continue;
194                 dirent_path=g_strdup_printf("%s/%s",pathname,dirent->d_name);
195                 chrooted_unlink_recursive(dirent_path);
196                 g_free(dirent_path);
197                 }
198         if (errno)
199                 fatal("Cannot readdir(\"%s\") during delete of leftover sandbox files: %m",pathname);
200         if (closedir(dir))
201                 fatal("Cannot closedir(\"%s\") during delete of leftover sandbox files: %m",pathname);
202         if (rmdir(pathname))
203                 fatal("Cannot rmdir(\"%s\") during delete of leftover sandbox files: %m",pathname);
204
205 done:
206         depth--;
207 }
208
209 static void chrooted_cleanuplockeddirs(const gchar *pathname,const gchar *prefix)
210 {
211 DIR *dir;
212 struct dirent *dirent;
213
214         if (!(dir=opendir(pathname))) {
215                 if (errno!=ENOTDIR)
216                         fatal("Cannot opendir(\"%s\") to delete leftover sandbox files: %m",pathname);
217                 /* errno==ENOTDIR, a regular file */
218                 if (unlink(pathname))
219                         fatal("Cannot unlink(\"%s\") to delete leftover sandbox files: %m",pathname);
220                 return;
221                 }
222         while (errno=0,(dirent=readdir(dir))) {
223 gchar *dirent_path;
224 int direntfd;
225
226                 if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
227                         continue;
228                 if (strncmp(dirent->d_name,prefix,strlen(prefix)))
229                         continue;
230                 dirent_path=g_strdup_printf("%s/%s",pathname,dirent->d_name);
231                 if (-1==(direntfd=open(dirent_path,O_RDONLY))) {
232                         if (errno==ENOENT)      /* It could disappear in the meantime. */
233                                 goto next_dirent_free_dirent_path;
234                         fatal("Cannot open(\"%s\") as the child directory during delete of leftover sandbox files: %m",dirent_path);
235                         }
236                 if (flock(direntfd,LOCK_EX|LOCK_NB)) {
237                         if (errno==EWOULDBLOCK) /* Valid directory in use. */
238                                 goto next_dirent_close_direntfd;
239                         fatal("Cannot flock(\"%s\",LOCK_EX|LOCK_NB) child directory during delete of leftover sandbox files: %m",dirent_path);
240                         }
241                 chrooted_unlink_recursive(dirent_path);
242 next_dirent_close_direntfd:
243                 if (close(direntfd))
244                         fatal("Cannot close(\"%s\") child directory during delete of leftover sandbox files: %m",dirent_path);
245 next_dirent_free_dirent_path:
246                 g_free(dirent_path);
247                 }
248         if (errno)
249                 fatal("Cannot readdir(\"%s\") during delete of leftover sandbox files: %m",pathname);
250         if (closedir(dir))
251                 fatal("Cannot closedir(\"%s\") during delete of leftover sandbox files: %m",pathname);
252 }
253
254 static void chrooted_createdir(const gchar *dir,uid_t uid,gid_t gid,gboolean lock)
255 {
256 gint retries;
257
258         for (retries=0;retries<10;retries++) {
259 struct stat statbuf;
260 int dirfd;
261
262                 if (mkdir(dir,0711)) {
263                         if (errno!=EEXIST)
264                                 fatal("Failed to create chroot directory \"%s\": %m",dir);
265                         chrooted_unlink_recursive(dir);
266                         if (mkdir(dir,0711))
267                                 fatal("Failed to create chroot directory \"%s\" after attempted unlink: %m",dir);
268                         }
269                 if (!lock)
270                         break;
271                 dirfd=open(dir,O_RDONLY);
272                 if (dirfd==-1) {
273                         if (errno!=ENOENT)
274                                 fatal("Failed to open created chroot directory \"%s\" to lock it: %m",dir);
275                         continue;
276                         }
277                 /* Do not use 'LOCK_NB' here as the garbage collector should release it soon. */
278                 if (flock(dirfd,LOCK_EX))
279                         fatal("Failed to lock created chroot directory \"%s\": %m",dir);
280                 if (lstat(dir,&statbuf)) {
281                         if (errno!=ENOENT)
282                                 fatal("Failed to lstat(2) created chroot directory \"%s\": %m",dir);
283                         if (close(dirfd))
284                                 fatal("Failed to close created and locked chroot directory \"%s\": %m",dir);
285                         continue;
286                         }
287                 /* Leave 'dirfd' open to leave it LOCK_EX-ed. */
288                 break;
289                 }
290         if (chown(dir,uid,gid))
291                 fatal("Failed to chown(\"%s\",%d,%d): %m",dir,uid,gid);
292         if (chmod(dir,0711))    /* Just to be safe after chown(2); should be already done by mkdir(2). */
293                 fatal("Failed to chmod(\"%s\",0%o): %m",dir,0711);
294 }
295
296 static void sandbox_server_rlimit(int resource,const gchar *resource_string,rlim_t rlim_max)
297 {
298 struct rlimit rlim;
299
300         rlim.rlim_cur=rlim.rlim_max=rlim_max;
301         if (setrlimit(resource,&rlim))
302                 fatal("setrlimit(%s,%d): %m",resource_string,(int)rlim_max);
303         if (getrlimit(resource,&rlim))
304                 fatal("getrlimit(%s,%d): %m",resource_string,(int)rlim_max);
305         if (rlim.rlim_cur!=rlim_max || rlim.rlim_max!=rlim_max)
306                 fatal("Unsuccessful setrlimit(%s)",resource_string);
307 }
308
309 static gchar *chrooted_orbit_dir;
310
311 static void chroot_setup(gboolean fragile)
312 {
313 uid_t want_uid=0;
314 const gchar *want_uid_name=NULL;
315 gid_t want_gid=0;
316 char *endptr;
317 const gchar *chroot_pid_hashkey_dir=NULL;
318
319         if (fragile) {
320                 captive_sandbox_fd_closeup(2 /* STDERR */ +1);
321                 clearenv();
322                 }
323
324 #define CLEANEMPTY(var) G_STMT_START { \
325                 if ((var) && (!*(var) || *(var)=='-')) \
326                         (var)=NULL; \
327                 } G_STMT_END
328         CLEANEMPTY(optarg_setgid);
329         CLEANEMPTY(optarg_setuid);
330         CLEANEMPTY(optarg_chroot);
331 #undef CLEANEMPTY
332
333         if (optarg_setgid) {
334 long want_gidl;
335
336                 want_gidl=strtol(optarg_setgid,&endptr,10);
337                 if (!endptr || !*endptr) {
338                         want_gid=want_gidl;
339                         if (want_gidl<=0 || want_gid!=(gid_t)want_gidl)
340                                 fatal("Numeric setgid not parsable: %s",optarg_setgid);
341                         }
342                 else {
343 struct group *want_gid_group=NULL;
344                         if (!(want_gid_group=getgrnam(optarg_setgid)))
345                                 fatal("Unable to query setgid group name \"%s\"",optarg_setgid);
346                         want_gid=want_gid_group->gr_gid;
347                         }
348                 }
349
350         if (optarg_setuid) {
351 long want_uidl;
352 struct passwd *want_uid_passwd;
353                 want_uidl=strtol(optarg_setuid,&endptr,10);
354                 if (!endptr || !*endptr) {
355                         want_uid=want_uidl;
356                         if (want_uidl<=0 || want_uid!=(gid_t)want_uidl)
357                                 fatal("Numeric setuid not parsable: %s",optarg_setuid);
358                         }
359                 else {
360                         if (!(want_uid_passwd=getpwnam(optarg_setuid)))
361                                 fatal("Unable to query setuid user name \"%s\"",optarg_setuid);
362                         want_uid=want_uid_passwd->pw_uid;
363                         }
364                 if (!want_uid)
365                         fatal("Unable to detect setuid UID");
366                 if (!(want_uid_passwd=getpwuid(want_uid)))
367                         fatal("Unable to query name of UID %d",(int)want_uid);
368                 want_uid_name=captive_strdup_alloca(want_uid_passwd->pw_name);
369                 }
370
371         /* Prevent: GLib-WARNING **: getpwuid_r(): failed due to unknown user id (42)
372          * Try to invoke GLib g_get_any_init() before possible chroot(2) below.
373          */
374         g_get_user_name();
375         g_get_real_name();
376         g_get_home_dir();
377         g_get_tmp_dir();
378
379         if (fragile && !optarg_chroot)
380                 fatal("Fragile setuid/root environment but no --chroot set");
381         if (optarg_chroot) {
382 const gchar *chroot_pid_dir;
383 GRand *grand;
384 gchar chroot_hashkey[CHROOT_PATH_HASHKEY_LENGTH+1],*s;
385 gint gi;
386
387                 check_dir_safety(optarg_chroot);
388                 if (!(grand=g_rand_new()))      /* I hope g_rand_new() is security-safe. It looks so. */
389                         fatal("Cannot initialize random number generator g_rand_new()");
390                 for (s=chroot_hashkey;s<chroot_hashkey+CHROOT_PATH_HASHKEY_LENGTH;s++) {
391                         gi=g_rand_int_range(grand,0,10+26+26);
392                         /**/ if (gi>=0 && gi<10)
393                                 *s='0'+gi-(0);
394                         else if (gi>=10+0 && gi<10+26)
395                                 *s='a'+gi-(10);
396                         else if (gi>=10+26+0 && gi<10+26+26)
397                                 *s='A'+gi-(10+26);
398                         else g_assert_not_reached();
399                         }
400                 g_rand_free(grand);
401                 *s=0;
402                 if (geteuid()==0)       /* Not 'fragile' as we can be native 'root'. */
403                         chrooted_cleanuplockeddirs(optarg_chroot,"s-");
404                 chroot_pid_dir=captive_printf_alloca("%s/s-%d",optarg_chroot,(int)getpid());
405                 chrooted_createdir(chroot_pid_dir,(!optarg_setuid ? (uid_t)-1 : want_uid),(!optarg_setgid ? (gid_t)-1 : want_gid),
406                                 TRUE);  /* lock */
407                 chroot_pid_hashkey_dir=captive_printf_alloca("%s/%s",chroot_pid_dir,chroot_hashkey);
408                 chrooted_createdir(chroot_pid_hashkey_dir,(!optarg_setuid ? (uid_t)-1 : want_uid),(!optarg_setgid ? (gid_t)-1 : want_gid),
409                                 FALSE); /* lock */
410                 if (chroot(chroot_pid_hashkey_dir))
411                         fatal("Failed to chroot(\"%s\"): %m",chroot_pid_hashkey_dir);
412                 if (chdir("/"))
413                         fatal("Failed to chdir(\"%s\"): %m","/");
414                 /* Now it is safe to set umask(0000) as we are protected by 'chroot_hashkey'.
415                  * We need it to permit our spawning parent to hardlink its sockets to us.
416                  */
417                 umask(0000);
418                 if (umask(0000)!=0000)
419                         fatal("Failed to set umask(0%o): %m",0000);
420                 printf("chroot_pid_hashkey_dir=%s\n",chroot_pid_hashkey_dir);
421                 }
422
423         if (fragile && !optarg_setgid)
424                 fatal("Fragile setuid/root environment but no --setgid set");
425         if (optarg_setgid) {
426                 if (!want_gid || setgid(want_gid))
427                         fatal("Failed to setgid(%d)",(!want_gid ? -1 : (int)want_gid));
428                 if (setgroups(1 /* size */,&want_gid))
429                         fatal("Failed to setgroups(1,[%d])",(!want_gid ? -1 : (int)want_gid));
430                 }
431         if (fragile && !optarg_setuid)
432                 fatal("Fragile setuid/root environment but no --setuid set");
433         if (optarg_setuid) {
434                 if (!want_uid || setuid(want_uid))
435                         fatal("Failed to setuid(%d)",(!want_uid ? -1 : (int)want_uid));
436                 }
437
438         /* Prepare /t for /t/o-$PID directories for ORBit2
439          * and also for parent's hardlink to its /t/o-$pid directory. */
440         if (optarg_chroot) {
441 gchar *chrooted_orbit_dir_old,*gs,*gs2;
442
443                 if (mkdir("/t",S_ISVTX|0777)) {
444                         if (errno!=EEXIST)
445                                 fatal("Failed to mkdir(\"%s\"): %m","/t");
446                         }
447                 if (mkdir("/etc",0700))
448                         fatal("Failed to mkdir(\"%s\"): %m","/etc");
449                 if (want_uid_name && want_uid && want_gid) {
450 FILE *f;
451                         if (!(f=fopen("/etc/passwd","w")))
452                                 fatal("Failed to fopen(\"%s\",\"w\"): %m","/etc/passwd");
453                         if (0>fprintf(f,"%s:*:%d:%d:%s:%s:/bin/false\n",want_uid_name,(int)want_uid,(int)want_gid,want_uid_name,optarg_chroot))
454                                 fatal("Failed to fprintf(\"%s\"): %m","/etc/passwd");
455                         if (fclose(f))
456                                 fatal("Failed to fclose(\"%s\"): %m","/etc/passwd");
457                         }
458                 g_assert(chroot_pid_hashkey_dir!=NULL);
459                 chrooted_orbit_dir=g_strdup_printf("%s/t/o-%d",chroot_pid_hashkey_dir,getpid());
460                 /* Missing mkdir(2) of the last component path is intentional: */
461                 for (gs=chrooted_orbit_dir;(gs2=strchr(gs,'/'));gs=gs2) {
462                         *gs2='\0';
463                         if (*chrooted_orbit_dir && mkdir(chrooted_orbit_dir,S_ISVTX|0777)) {
464                                 if (errno!=EEXIST)
465                                         fatal("Failed to mkdir(\"%s\"): %m",chrooted_orbit_dir);
466                                 }
467                         *gs2++='/';
468                         }
469                 /* Prepare '/tmp' for the initial CORBA_ORB_init() default path: */
470                 if (mkdir("/tmp",S_ISVTX|0777)) {
471                         if (errno!=EEXIST)
472                                 fatal("Failed to mkdir(\"%s\"): %m","/tmp");
473                         }
474                 /* Set '0700' to prevent: Wrong permissions for ...
475                  * by linc-1.0.1-1/src/linc-protocols.c/make_local_tmpdir()
476                  */
477                 if (mkdir(chrooted_orbit_dir,0700)) {
478                         /* Do not: g_assert(errno==EEXIST);
479                          * as if 'optarg_chroot' the whole chroot(2)ed directory should be ours.
480                          */
481                         fatal("Cannot created chrooted_orbit_dir \"%s\": %m",chrooted_orbit_dir);
482                         }
483                 /* Init 'orb' to pass through its linc_set_tmpdir() to not to be overriden below. */
484                 {
485 CORBA_ORB orb;
486 CORBA_Environment ev;
487 int orb_argc=1;
488 gchar *orb_argv[]={
489                 (gchar *)captive_strdup_alloca("captive-sandbox-server"),
490                 NULL};
491
492                         CORBA_exception_init(&ev);
493                         orb=CORBA_ORB_init(&orb_argc,orb_argv,"orbit-local-orb",&ev);
494                         if (orb==CORBA_OBJECT_NIL)
495                                 fatal("Cannot initialize CORBA ORB (CORBA_OBJECT_NIL): %m");
496                         if (ev._major!=CORBA_NO_EXCEPTION)
497                                 fatal("Cannot initialize CORBA ORB (exception): %m");
498                         }
499                 chrooted_orbit_dir_old=linc_get_tmpdir();       /* returns g_strdup()ed string */
500                 g_assert(chrooted_orbit_dir_old!=NULL);
501                 linc_set_tmpdir(chrooted_orbit_dir);
502                 if (rmdir(chrooted_orbit_dir_old))
503                         fatal("Cannot remove old chrooted_orbit_dir \"%s\": %m",chrooted_orbit_dir_old);
504                 g_free(chrooted_orbit_dir_old);
505                 /* chmod(2) it to prevent mode limitation by
506                  * active ulimit(2) of being executed by mount(8).
507                  */
508                 /* Set '0777' as our parent does not have 'captive' user permissions. */
509                 if (chmod(chrooted_orbit_dir,S_ISVTX|0777))
510                         fatal("Cannot chmod 0%o chrooted_orbit_dir \"%s\": %m",S_ISVTX|0777,chrooted_orbit_dir);
511                 printf("chrooted_orbit_dir=%s\n",chrooted_orbit_dir);
512                 }
513
514         if (fragile || !optarg_no_rlimit) {
515 #define SANDBOX_SERVER_RLIMIT(what,how) sandbox_server_rlimit((what),G_STRINGIFY(what),(how))
516                 SANDBOX_SERVER_RLIMIT(RLIMIT_NPROC,0);
517                 SANDBOX_SERVER_RLIMIT(RLIMIT_MEMLOCK,0);
518                 SANDBOX_SERVER_RLIMIT(RLIMIT_CORE,0);
519                 SANDBOX_SERVER_RLIMIT(RLIMIT_FSIZE,0);
520                 SANDBOX_SERVER_RLIMIT(RLIMIT_NOFILE,6); /* >=6 */
521                 /* FIXME: Why flock(dirfd,...) in chrooted_createdir() succeeds?: */
522                 SANDBOX_SERVER_RLIMIT(RLIMIT_LOCKS,0);
523 #undef SANDBOX_SERVER_RLIMIT
524                 }
525
526         if (fragile) {
527 gid_t gid_list[2];
528 int gid_list_size,i;
529
530                 if (getuid()!=want_uid)
531                         fatal("getuid()=%d != want_uid=%d",(int)getuid(),(int)want_uid);
532                 if (geteuid()!=want_uid)
533                         fatal("geteuid()=%d != want_uid=%d",(int)geteuid(),(int)want_uid);
534                 if (getgid()!=want_gid)
535                         fatal("getgid()=%d != want_gid=%d",(int)getgid(),(int)want_gid);
536                 if (getegid()!=want_gid)
537                         fatal("getegid()=%d != want_gid=%d",(int)getegid(),(int)want_gid);
538                 gid_list_size=getgroups(G_N_ELEMENTS(gid_list),gid_list);
539                 for (i=0;i<gid_list_size;i++) {
540                         if (gid_list[i]!=want_gid)
541                                 fatal("getgroups() list member @%d %d != want_gid=%d",i,(int)gid_list[i],(int)want_gid);
542                         }
543                 }
544 }
545
546
547 int main(int argc,char **argv)
548 {
549 poptContext context;
550 int errint;
551 const char *cmd_arg;
552 struct captive_options options;
553 gboolean fragile;
554
555         g_log_set_always_fatal(~(0
556                         |G_LOG_LEVEL_MESSAGE
557                         |G_LOG_LEVEL_INFO
558                         |G_LOG_LEVEL_DEBUG
559                         ));
560
561         fatal_argv0=argv[0];
562         fragile=(getuid()!=geteuid() || getuid()==0 || geteuid()==0);
563
564 #ifndef MAINTAINER_MODE
565         if (fragile && (argc!=1 || argv[1]))
566                 fatal("Arguments invalid as running in fragile setuid/root environment");
567
568         if (fragile)
569                 chroot_setup(TRUE);
570 #endif /* MAINTAINER_MODE */
571
572         /* Initialize the i18n stuff */
573         setlocale(LC_ALL,"");
574         bindtextdomain(PACKAGE,LOCALEDIR);
575         textdomain(PACKAGE);
576
577         /* Initialize GObject subsystem of GLib. */
578         g_type_init();
579
580         captive_options_init(&options);
581         captive_options=&options;       /* for parsing by 'CAPTIVE_POPT_INCLUDE' */
582
583         context=poptGetContext(
584                         PACKAGE,        /* name */
585                         argc,(/*en-const*/const char **)argv,   /* argc,argv */
586                         popt_table,     /* options */
587                         POPT_CONTEXT_POSIXMEHARDER);    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
588         if (context==NULL) {
589                 g_assert_not_reached(); /* argument recognization args_error */
590                 return EXIT_FAILURE;
591                 }
592         errint=poptReadDefaultConfig(context,
593                         TRUE);  /* useEnv */
594         if (errint!=0) {
595                 g_assert_not_reached(); /* argument recognization args_error */
596                 return EXIT_FAILURE;
597                 }
598         errint=poptGetNextOpt(context);
599         if (errint!=-1) {
600                 g_assert_not_reached(); /* some non-callbacked argument reached */
601                 return EXIT_FAILURE;
602                 }
603         cmd_arg=poptPeekArg(context);
604         if (cmd_arg) {
605                 g_assert_not_reached(); /* some non-option argument reached */
606                 return EXIT_FAILURE;
607                 }
608         /* 'cmd_arg'-style args gets cleared by 'poptFreeContext(context);' below */
609         poptFreeContext(context);
610
611 #ifdef MAINTAINER_MODE
612         chroot_setup(FALSE);
613 #endif /* MAINTAINER_MODE */
614
615         captive_options=NULL;   /* already parsed by 'CAPTIVE_POPT_INCLUDE' */
616
617         captive_corba_sandbox_child(chrooted_orbit_dir);
618
619         g_assert_not_reached();
620         return EXIT_SUCCESS;
621 }