+Sandbox directory cleanup handler debug dumps.
[captive.git] / src / libcaptive / sandbox / split.c
1 /* $Id$
2  * Connection of captive-vfs interface through CORBA/ORBit
3  * Copyright (C) 2002 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 "split.h"      /* self */
23 #include "sandbox.h"
24 #include "server-GLogFunc.h"
25 #include "captive/macros.h"
26 #include <glib/gmacros.h>
27 #include <stdlib.h>
28 #include "captive/rtl-file.h"
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <linc/linc.h>  /* for linc_main_get_loop() */
32 #include "server-Directory.h"
33 #include "server-Vfs.h"
34 #include "../client/vfs.h"
35 #include "captive/parent-Vfs.h"
36 #include <dirent.h>
37 #include "server-CaptiveIOChannel.h"
38 #include <errno.h>
39 #include "../client/giochannel-blind.h" /* for captive_giochannel_blind_new() */
40 #include <signal.h>
41
42
43 /* CONFIG: */
44
45 #define HEARTBEAT_SOURCE_CHECK_EVENTS (G_IO_IN|G_IO_PRI)
46 #define HEARTBEAT_SOURCE_CHECK_REVENTS (HEARTBEAT_SOURCE_CHECK_EVENTS|G_IO_ERR|G_IO_HUP|G_IO_NVAL)
47
48
49 gboolean validate_CORBA_Environment(CORBA_Environment *evp)
50 {
51         if (evp->_major==CORBA_NO_EXCEPTION)
52                 return TRUE;
53
54         g_error(_("CORBA Exception occured: id=\"%s\", value=%p"),
55                         CORBA_exception_id(evp),CORBA_exception_value(evp));
56         CORBA_exception_free(evp);
57         return FALSE;
58 }
59
60 CORBA_Environment captive_corba_ev;
61 CORBA_ORB captive_corba_orb;
62 PortableServer_POA captive_corba_poa;
63
64 static void corba_shutdown_atexit(void);
65
66 static gboolean corba_init(const char *pname,CORBA_Environment *evp,CORBA_ORB *orbp,PortableServer_POA *poap)
67 {
68 static gboolean done=FALSE;
69 int orb_argc=1;
70 gchar *orb_argv[]={
71                 (gchar *)captive_strdup_alloca(pname),
72                 NULL};
73
74         g_return_val_if_fail(evp!=NULL,FALSE);
75         g_return_val_if_fail(orbp!=NULL,FALSE);
76         g_return_val_if_fail(poap!=NULL,FALSE);
77
78         if (done)
79                 return TRUE;
80
81         /* Init 'ev' */
82         CORBA_exception_init(evp);
83
84         /* Init 'orb' */
85         *orbp=CORBA_ORB_init(&orb_argc,orb_argv,"orbit-local-orb",evp);
86         g_return_val_if_fail(*orbp!=CORBA_OBJECT_NIL,FALSE);
87         g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
88
89         /* Init 'poa' */
90         *poap=(PortableServer_POA)CORBA_ORB_resolve_initial_references(*orbp,"RootPOA",evp);
91         g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
92         {
93 PortableServer_POAManager poa_mgr;
94                 poa_mgr=PortableServer_POA__get_the_POAManager(*poap,evp);
95                 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
96                 PortableServer_POAManager_activate(poa_mgr,evp);
97                 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
98                 CORBA_Object_release((CORBA_Object)poa_mgr,evp);
99                 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
100                 }
101
102         g_atexit(corba_shutdown_atexit);
103
104         done=TRUE;
105         return TRUE;
106 }
107
108
109 static CORBA_ORB heartbeat_source_callback_orb=CORBA_OBJECT_NIL;
110
111 gboolean corba_shutdown(CORBA_Environment *evp,CORBA_ORB *orbp,PortableServer_POA *poap)
112 {
113 PortableServer_POA poa;
114 CORBA_ORB orb;
115
116         g_return_val_if_fail(evp!=NULL,FALSE);
117         g_return_val_if_fail(orbp!=NULL,FALSE);
118         g_return_val_if_fail(poap!=NULL,FALSE);
119
120         /* Shutdown 'poa' */
121         poa=*poap;
122         *poap=CORBA_OBJECT_NIL;
123         CORBA_Object_release((CORBA_Object)poa,evp);
124         g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
125
126         /* Shutdown 'orb' */
127         orb=*orbp;
128         *orbp=CORBA_OBJECT_NIL;
129         heartbeat_source_callback_orb=CORBA_OBJECT_NIL;
130         CORBA_ORB_destroy(orb,evp);
131         /* Do not: g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
132          * here as CORBA_ORB_destroy() sometimes reports:
133          *      WARNING **: ORB: a total of X refs to X ORB objects were leaked
134          */
135         CORBA_exception_free(evp);
136
137         /* Shutdown 'ev' */
138         CORBA_exception_free(evp);
139
140         return TRUE;
141 }
142
143 static void corba_shutdown_atexit(void)
144 {
145 gboolean errbool;
146
147         errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
148         g_assert(errbool==TRUE);
149 }
150
151
152 static GSource *captive_corba_sandbox_child_heartbeat_gsource;
153
154 #if 0   /* Currently unused - see server-Vfs.c:impl_Captive_Vfs_shutdown() */
155 void sandbox_child_prepare_shutdown(void)
156 {
157         /* Prevent during shutdown: Captive-WARNING **: CORBA Exception occured: id="IDL:omg.org/CORBA/COMM_FAILURE:1.0" */
158         if (captive_corba_sandbox_child_heartbeat_gsource) {
159                 g_source_destroy(captive_corba_sandbox_child_heartbeat_gsource);
160                 g_source_unref(captive_corba_sandbox_child_heartbeat_gsource);
161                 captive_corba_sandbox_child_heartbeat_gsource=NULL;
162                 }
163 }
164 #endif
165
166
167 void sandbox_child_shutdown(void)
168 {
169         /* Do not fail by passing logging messages to the master. */
170         impl_Captive_Vfs_init_g_log_func_disable();
171
172         g_main_loop_quit(linc_main_get_loop());
173 }
174
175
176 static gboolean heartbeat_source_callback(gpointer data /* unused */)
177 {
178         g_return_val_if_fail(heartbeat_source_callback_orb!=CORBA_OBJECT_NIL,FALSE);    /* the source should be removed */
179
180         sandbox_child_shutdown();
181
182         return FALSE;   /* the source should be removed */
183 }
184
185
186 static gboolean heartbeat_source_prepare(GSource *source,gint *timeout)
187 {
188         *timeout=-1;
189
190         return FALSE;
191 }
192
193 static GPollFD heartbeat_source_check_gpollfd;
194
195 static gboolean heartbeat_source_check(GSource *source)
196 {
197         return !!(heartbeat_source_check_gpollfd.revents & HEARTBEAT_SOURCE_CHECK_REVENTS);
198 }
199
200 static gboolean heartbeat_source_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
201 {
202         g_assert(callback!=NULL);
203         return (*callback)(user_data);
204 }
205
206 static GSourceFuncs heartbeat_source_watch_funcs={
207                 heartbeat_source_prepare,
208                 heartbeat_source_check,
209                 heartbeat_source_dispatch,
210                 NULL,   /* finalize */
211                 };
212
213 void captive_corba_sandbox_child(const gchar *chrooted_orbit_dir)
214 {
215 Captive_Vfs Vfs_object;
216 impl_POA_Captive_Vfs *Vfs_servant;
217 gboolean errbool;
218 guint errguint;
219 int errint;
220
221         /* attach heartbeat_source_callback() to watch for any abnormalities
222          * on our open pipe 'parentheart_fds' and terminate the child if parent dies.
223          */
224         captive_corba_sandbox_child_heartbeat_gsource=g_source_new(&heartbeat_source_watch_funcs,sizeof(GSource));
225         g_return_if_fail(captive_corba_sandbox_child_heartbeat_gsource!=NULL);
226         g_source_set_callback(
227                         captive_corba_sandbox_child_heartbeat_gsource,  /* source */
228                         heartbeat_source_callback,      /* func */
229                         NULL,   /* data */
230                         NULL);  /* notify */
231         heartbeat_source_check_gpollfd.fd=0 /* STDIN */;        /* parentheart_fd_read */
232         heartbeat_source_check_gpollfd.events=HEARTBEAT_SOURCE_CHECK_EVENTS;
233         heartbeat_source_check_gpollfd.revents=0;
234         g_source_add_poll(captive_corba_sandbox_child_heartbeat_gsource,&heartbeat_source_check_gpollfd);
235
236         errbool=corba_init("captive-sandbox-child",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
237         g_return_if_fail(errbool==TRUE);
238
239         heartbeat_source_callback_orb=captive_corba_orb;
240
241         /* linc_main_get_loop() makes sense only after corba_init() -> CORBA_ORB_init() */
242         errguint=g_source_attach(
243                         captive_corba_sandbox_child_heartbeat_gsource,  /* source */
244                         g_main_loop_get_context(linc_main_get_loop())); /* context; NULL means 'default context' */
245         g_assert(errguint!=0);
246
247         /* Init 'Vfs_object' */
248         Vfs_object=impl_Captive_Vfs__create(captive_corba_poa,&captive_corba_ev);
249         g_assert(validate_CORBA_Environment(&captive_corba_ev));
250
251         /* Pass IOR to our parent.
252          * It will also create the socket needed for 'chrooted_orbit_dir' below
253          * by CORBA_ORB_object_to_string()->ORBit_marshal_object()->IOP_generate_profiles()->ORBit_ORB_start_servers...
254          */
255         {
256 char *Vfs_IOR;
257                 Vfs_IOR=CORBA_ORB_object_to_string(captive_corba_orb,Vfs_object,&captive_corba_ev);
258                 g_assert(validate_CORBA_Environment(&captive_corba_ev));
259                 g_assert(Vfs_IOR!=NULL);
260                 errint=printf("ior=%s\n",Vfs_IOR);
261                 g_assert(errint>=0);
262                 CORBA_free(Vfs_IOR);
263                 }
264
265         /* Default mode is 0700, permit parent non-"captive" users to access our IOR socket. */
266         if (chrooted_orbit_dir) {
267 DIR *dir;
268 struct dirent *dirent;
269 const gchar *socketname=NULL,*socketpathname;
270
271                 errint=chmod(chrooted_orbit_dir,0755);
272                 g_assert(errint==0);
273
274                 dir=opendir(chrooted_orbit_dir);
275                 g_assert(dir!=NULL);
276                 while (errno=0,(dirent=readdir(dir))) {
277                         if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
278                                 continue;
279                         g_assert(socketname==NULL);
280                         socketname=captive_strdup_alloca(dirent->d_name);
281                         }
282                 g_assert(errno==0);
283                 errint=closedir(dir);
284                 g_assert(errint==0);
285                 g_assert(socketname!=NULL);
286
287                 socketpathname=captive_printf_alloca("%s/%s",chrooted_orbit_dir,socketname);
288                 errint=chmod(socketpathname,0666);
289                 g_assert(errint==0);
290
291                 printf("socketname=%s\n",socketname);
292                 }
293
294         /* Close the output to flush it to our spawning parent. */
295         errint=fflush(stdout);
296         g_assert(errint==0);
297         errint=fclose(stdout);
298         g_assert(errint==0);
299
300         /* CORBA_ORB_run() -> linc_main_loop_run() -> g_main_loop_run()
301          * and therefore we should be safe with glib events handling.
302          */
303         CORBA_ORB_run(captive_corba_orb,&captive_corba_ev);
304         g_assert(validate_CORBA_Environment(&captive_corba_ev));
305
306         /* Shutdown 'Vfs' servant */
307         Vfs_servant=PortableServer_POA_reference_to_servant(captive_corba_poa,Vfs_object,&captive_corba_ev);
308         g_assert(validate_CORBA_Environment(&captive_corba_ev));
309         CORBA_Object_release(Vfs_object,&captive_corba_ev);
310         g_assert(validate_CORBA_Environment(&captive_corba_ev));
311         impl_Captive_Vfs__destroy(Vfs_servant,&captive_corba_ev);
312         g_assert(validate_CORBA_Environment(&captive_corba_ev));
313
314         errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
315         g_assert(errbool==TRUE);
316
317         _exit(EXIT_SUCCESS);
318 }
319
320
321 static void options_module_captive_to_options_module_corba
322                 (Captive_CaptiveOptionsModule *dest_options_module_corba,const struct captive_options_module *src_options_module_captive)
323 {
324         g_return_if_fail(dest_options_module_corba!=NULL);
325         g_return_if_fail(src_options_module_captive!=NULL);
326
327         g_return_if_fail(src_options_module_captive->type==CAPTIVE_OPTIONS_MODULE_TYPE_PE32);
328
329         dest_options_module_corba->pathname_utf8=g_strdup(src_options_module_captive->pathname_utf8);
330         dest_options_module_corba->data._buffer=g_memdup(src_options_module_captive->u.pe32.base,
331                         src_options_module_captive->u.pe32.length);
332         dest_options_module_corba->data._maximum=src_options_module_captive->u.pe32.length;
333         dest_options_module_corba->data._length =src_options_module_captive->u.pe32.length;
334         dest_options_module_corba->data._release=TRUE;
335 }
336
337 static void unlink_nonrecursive(const gchar *dirname)
338 {
339 DIR *dir;
340 struct dirent *dirent;
341 int errint;
342
343         dir=opendir(dirname);
344         g_assert(dir!=NULL);
345
346         while (errno=0,(dirent=readdir(dir))) {
347 gchar *pathname;
348
349                 if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
350                         continue;
351                 pathname=g_strdup_printf("%s/%s",dirname,dirent->d_name);
352                 errint=unlink(pathname);
353                 g_assert(errint==0);
354                 g_free(pathname);
355                 }
356         g_assert(errno==0);
357         errint=closedir(dir);
358         g_assert(errint==0);
359         errint=rmdir(dirname);
360         g_assert(errint==0);
361 }
362
363 static const gchar *sandbox_parent_own_orbit_dir;
364 static const gchar *sandbox_parent_own_orbit_socket;
365
366 static void sandbox_parent_own_orbit_dir_cleanup_atexit(void)
367 {
368 static gboolean done=FALSE;
369
370         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s; done=%d",G_STRLOC,(int)done);
371         if (done)
372                 return;
373         done=TRUE;
374
375         unlink_nonrecursive(sandbox_parent_own_orbit_dir);
376 }
377
378 static struct sandbox_parent_own_orbit_dir_cleanup_signal {
379         int signum;
380         /* FIXME: Why we cannot use 'sighandler_t'? */ void (*sighandler_orig)(int signum);
381         } sandbox_parent_own_orbit_dir_cleanup_signals[]={
382                 { SIGINT  },
383                 { SIGQUIT },
384                 { SIGTERM },
385                 { SIGHUP  },
386                 { SIGABRT },
387                 { SIGFPE  },
388                 };
389
390 static void sandbox_parent_own_orbit_dir_cleanup_sighandler(int signum)
391 {
392 struct sandbox_parent_own_orbit_dir_cleanup_signal *sigstructp;
393
394         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: signum=%d",G_STRLOC,signum);
395
396         sandbox_parent_own_orbit_dir_cleanup_atexit();
397
398         for (
399                         sigstructp=sandbox_parent_own_orbit_dir_cleanup_signals;
400                         sigstructp<sandbox_parent_own_orbit_dir_cleanup_signals+G_N_ELEMENTS(sandbox_parent_own_orbit_dir_cleanup_signals);
401                         sigstructp++) {
402                 if (sigstructp->signum==signum)
403                         break;
404                 }
405         g_assert(sigstructp<sandbox_parent_own_orbit_dir_cleanup_signals+G_N_ELEMENTS(sandbox_parent_own_orbit_dir_cleanup_signals));
406         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: sighandler_orig=%p,re-raising...",G_STRLOC,sigstructp->sighandler_orig);
407         signal(signum,sigstructp->sighandler_orig);
408         raise(signum);
409 }
410
411 static void sandbox_parent_own_orbit_dir_cleanup_init(void)
412 {
413 struct sandbox_parent_own_orbit_dir_cleanup_signal *sigstructp;
414
415         g_atexit(sandbox_parent_own_orbit_dir_cleanup_atexit);
416         for (
417                         sigstructp=sandbox_parent_own_orbit_dir_cleanup_signals;
418                         sigstructp<sandbox_parent_own_orbit_dir_cleanup_signals+G_N_ELEMENTS(sandbox_parent_own_orbit_dir_cleanup_signals);
419                         sigstructp++) {
420                 sigstructp->sighandler_orig=signal(sigstructp->signum,sandbox_parent_own_orbit_dir_cleanup_sighandler);
421                 if (sigstructp->sighandler_orig==SIG_IGN)
422                         sigstructp->sighandler_orig=SIG_DFL;
423                 }
424 }
425
426 gchar *sandbox_parent_read_ior(int Vfs_IOR_fd_read,gchar **child_chroot_pid_hashkey_dirp)
427 {
428 gchar *data;
429 gsize data_size;
430 GHashTable *hash;
431 gchar *ior,*child_chroot_pid_hashkey_dir;
432 int errint;
433 gchar *s,*sd,*se;
434 gboolean errbool;
435
436         /* Initialize /tmp/orbit-$username directory for linking IOR socket. */
437         errbool=corba_init("captive-sandbox-parent",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
438         g_assert(errbool==TRUE);
439
440         /* FIXME: Security: Duplicate giop_tmpdir_init() here. */
441         if (!sandbox_parent_own_orbit_dir) {
442                 sandbox_parent_own_orbit_dir=g_strdup_printf("/tmp/captive-orbit-%d",getpid());
443                 if (mkdir(sandbox_parent_own_orbit_dir,0700)) {
444                         g_assert(errno==EEXIST);
445                         sandbox_parent_own_orbit_dir_cleanup_init();
446                         unlink_nonrecursive(sandbox_parent_own_orbit_dir);
447                         if (mkdir(sandbox_parent_own_orbit_dir,0700))
448                                 g_assert_not_reached();
449                         }
450                 sandbox_parent_own_orbit_dir_cleanup_init();
451                 linc_set_tmpdir(sandbox_parent_own_orbit_dir);
452                 }
453
454         data=captive_rtl_file_read(Vfs_IOR_fd_read,&data_size); /* data_fd_read */
455         errint=close(Vfs_IOR_fd_read);
456         g_assert(errint==0);
457         g_assert(data!=NULL);
458         g_assert(data_size>=1);
459         g_assert(data[data_size-1]=='\n');
460
461         hash=g_hash_table_new(g_str_hash,g_str_equal);
462
463         for (s=data;s<data+data_size;s=se+1) {
464                 se=strchr(s,'\n');
465                 g_assert(se!=NULL);
466                 *se=0;
467                 sd=strchr(s,'=');
468                 g_assert(sd!=NULL);
469                 *sd=0;
470                 g_hash_table_insert(hash,s,sd+1);
471                 }
472         g_assert(s==data+data_size);
473
474         if ((child_chroot_pid_hashkey_dir=g_hash_table_lookup(hash,"chroot_pid_hashkey_dir"))) {
475 const gchar *socketname,*socketpathname_src,*socketpathname_dest;
476 const gchar *chrooted_orbit_dir;
477
478                 /* IOR contains the full pathname with the setuid username encoded. */
479                 chrooted_orbit_dir=g_hash_table_lookup(hash,"chrooted_orbit_dir");
480                 g_assert(chrooted_orbit_dir!=NULL);
481                 if (mkdir(chrooted_orbit_dir,S_ISVTX|0777)) {
482                         g_assert(errno==EEXIST);
483                         }
484
485                 socketname=g_hash_table_lookup(hash,"socketname");
486                 g_assert(socketname!=NULL);
487
488                 socketpathname_src=captive_printf_alloca("%s/%s/%s",child_chroot_pid_hashkey_dir,chrooted_orbit_dir,socketname);
489                 socketpathname_dest=captive_printf_alloca("%s/%s",chrooted_orbit_dir,socketname);
490                 errint=link(socketpathname_src,socketpathname_dest);
491                 g_assert(errint==0);
492                 }
493         if (child_chroot_pid_hashkey_dirp)
494                 *child_chroot_pid_hashkey_dirp=g_strdup(child_chroot_pid_hashkey_dir);
495
496         ior=g_hash_table_lookup(hash,"ior");
497         g_assert(ior!=NULL);
498         ior=g_strdup(ior);
499         g_free(data);
500
501         g_hash_table_destroy(hash);
502
503         return ior;
504 }
505
506
507 static xmlNode *options_module_captive_to_xml
508                 (xmlNode *dest_xml_parent,const struct captive_options_module *src_options_module_captive)
509 {
510         g_return_val_if_fail(dest_xml_parent!=NULL,NULL);
511         g_return_val_if_fail(src_options_module_captive!=NULL,NULL);
512
513         { xmlNode *module=xmlNewTextChild(dest_xml_parent,NULL,"module",NULL);
514         const gchar *type_string,*basename,*cgs;
515
516                 basename=src_options_module_captive->pathname_utf8;
517                 if ((cgs=strrchr(basename,'/')))
518                         basename=cgs+1;
519                 xmlNewProp(module,"basename",basename);
520                 switch (src_options_module_captive->type) {
521                         case CAPTIVE_OPTIONS_MODULE_TYPE_PE32:
522                                 type_string="PE32";
523                                 xmlNewProp(module,"length",captive_printf_alloca("%lu",(unsigned long)src_options_module_captive->u.pe32.length));
524                                 xmlNewProp(module,"md5"   ,src_options_module_captive->u.pe32.md5);
525                                 break;
526                         case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
527                                 type_string="gmodule";
528                                 break;
529                         default: g_assert_not_reached();
530                         }
531                 xmlNewProp(module,"type",type_string);  /* AFTER the 'switch' to set 'type_string'! */
532                 return module;
533                 }
534 }
535
536
537 static void sandbox_parent_bug_doc_make(CaptiveVfsObject *captive_vfs_object)
538 {
539         { xmlDoc *doc=xmlNewDoc("1.0");
540                 captive_vfs_object->corba_bug_doc=doc;
541                 { xmlNode *bug=xmlNewDocNode(captive_vfs_object->corba_bug_doc,NULL,"bug",NULL);
542                         xmlDocSetRootElement(captive_vfs_object->corba_bug_doc,bug);
543                         captive_vfs_object->corba_bug=bug;
544                         { xmlNode *bug_captive=xmlNewTextChild(bug,NULL,"captive",NULL);
545                                 xmlNewProp(bug_captive,"version",VERSION);
546                                 }
547                         { xmlNode *bug_filesystem=xmlNewTextChild(bug,NULL,"filesystem",NULL);
548                                 options_module_captive_to_xml(bug_filesystem,&captive_vfs_object->options.filesystem);
549                                 }
550                         { xmlNode *bug_load_module=xmlNewTextChild(bug,NULL,"load_module",NULL);
551                         guint load_moduleui;
552                         struct captive_options_module *options_module;
553                         GList *load_module_node;
554
555                                 for (load_moduleui=0,load_module_node=captive_vfs_object->options.load_module;
556                                                 load_module_node;
557                                                 load_moduleui++,load_module_node=load_module_node->next) {
558                                         options_module=load_module_node->data;
559                                         options_module_captive_to_xml(bug_load_module,options_module);
560                                         }
561                                 }
562                         { xmlNode *bug_action=xmlNewTextChild(bug,NULL,"action",NULL);
563                                 captive_vfs_object->corba_bug_action=bug_action;
564                                 }
565                         { xmlNode *bug_log=xmlNewTextChild(bug,NULL,"log",NULL);
566                                 captive_vfs_object->corba_bug_log=bug_log;
567                                 }
568                         }
569                 }
570 }
571
572
573 static void sandbox_parent(const gchar *Vfs_IOR,const gchar *child_chroot_pid_hashkey_dir,CaptiveVfsObject *captive_vfs_object)
574 {
575 Captive_Vfs Vfs_object;
576 Captive_GLogFunc GLogFunc_object;
577 Captive_CaptiveIOChannel CaptiveIOChannel_object;
578 Captive_CaptiveOptions options_corba;
579 guint load_module_length,load_moduleui;
580 struct captive_options_module *options_module;
581 GList *load_module_node;
582 const gchar *child_chroot_parent_own_orbit_socket,*child_chroot_parent_own_orbit_dir;
583 int errint;
584
585         g_return_if_fail(Vfs_IOR!=NULL);
586         /* 'child_chroot_pid_hashkey_dir' may be NULL */
587         g_return_if_fail(captive_vfs_object!=NULL);
588
589         Vfs_object=CORBA_ORB_string_to_object(captive_corba_orb,Vfs_IOR,&captive_corba_ev);
590         g_assert(validate_CORBA_Environment(&captive_corba_ev));
591
592         /* Init 'GLogFunc_object' */
593         GLogFunc_object=impl_Captive_GLogFunc__create(captive_corba_poa,captive_vfs_object,&captive_corba_ev);
594         g_assert(validate_CORBA_Environment(&captive_corba_ev));
595
596         /* Init 'CaptiveIOChannel_object' */
597         if (!captive_vfs_object->corba_parent_giochanel_blind_source)
598                 switch (captive_vfs_object->options.rwmode) {
599                         case CAPTIVE_OPTION_RWMODE_RO:
600                         case CAPTIVE_OPTION_RWMODE_RW:
601                                 captive_vfs_object->corba_parent_giochanel_blind_source=captive_vfs_object->options.image_iochannel;
602                                 break;
603                         case CAPTIVE_OPTION_RWMODE_BLIND:
604                                 captive_vfs_object->corba_parent_giochanel_blind_source=(GIOChannel *)captive_giochannel_blind_new(
605                                                 captive_vfs_object->options.image_iochannel,    /* giochannel_orig */
606                                                 TRUE); /* writeable */
607                                 break;
608                         default: g_assert_not_reached();
609                         }
610         if (!captive_vfs_object->corba_parent_giochanel_blind)
611                 captive_vfs_object->corba_parent_giochanel_blind=(GIOChannel *)captive_giochannel_blind_new(
612                                 captive_vfs_object->corba_parent_giochanel_blind_source,  /* giochannel_orig */
613                                 (captive_vfs_object->options.rwmode!=CAPTIVE_OPTION_RWMODE_RO)); /* writeable */
614         CaptiveIOChannel_object=impl_Captive_CaptiveIOChannel__create(captive_corba_poa,
615                         captive_vfs_object->corba_parent_giochanel_blind,&captive_corba_ev);
616         g_assert(validate_CORBA_Environment(&captive_corba_ev));
617
618         /* Create the socket needed for 'sandbox_parent_own_orbit_socket' below
619          * by CORBA_ORB_object_to_string()->ORBit_marshal_object()->IOP_generate_profiles()->ORBit_ORB_start_servers...
620          */
621         {
622 char *GLogFunc_IOR;
623                 GLogFunc_IOR=CORBA_ORB_object_to_string(captive_corba_orb,GLogFunc_object,&captive_corba_ev);
624                 g_assert(validate_CORBA_Environment(&captive_corba_ev));
625                 g_assert(GLogFunc_IOR!=NULL);
626                 CORBA_free(GLogFunc_IOR);
627                 }
628
629         if (sandbox_parent_own_orbit_dir && !sandbox_parent_own_orbit_socket) {
630 DIR *dir;
631 struct dirent *dirent;
632
633                 dir=opendir(sandbox_parent_own_orbit_dir);
634                 g_assert(dir!=NULL);
635
636                 while (errno=0,(dirent=readdir(dir))) {
637                         if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
638                                 continue;
639                         g_assert(sandbox_parent_own_orbit_socket==NULL);
640                         sandbox_parent_own_orbit_socket=g_strdup_printf("%s/%s",sandbox_parent_own_orbit_dir,dirent->d_name);
641                         }
642                 g_assert(errno==0);
643                 errint=closedir(dir);
644                 g_assert(errint==0);
645                 g_assert(sandbox_parent_own_orbit_socket!=NULL);
646                 }
647
648         if (child_chroot_pid_hashkey_dir) {
649                 child_chroot_parent_own_orbit_dir=captive_printf_alloca("%s/%s",child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_dir);
650                 errint=mkdir(child_chroot_parent_own_orbit_dir,0777);
651                 g_assert(errint==0);
652                 child_chroot_parent_own_orbit_socket=captive_printf_alloca("%s/%s",
653                                 child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_socket);
654                 errint=link(sandbox_parent_own_orbit_socket,child_chroot_parent_own_orbit_socket);
655                 g_assert(errint==0);
656                 /* chmod(2)s also our orig. one (!) but this one is protected by its 0777 directory. */
657                 errint=chmod(child_chroot_parent_own_orbit_socket,0666);
658                 g_assert(errint==0);
659                 }
660
661         options_corba.g_log_func=GLogFunc_object;
662         options_module_captive_to_options_module_corba(&options_corba.filesystem,&captive_vfs_object->options.filesystem);
663         /* Prevent secondary captive_giochannel_blind inside of our sandbox child
664          * as we already have one captive_giochannel_blind in the parent.
665          */
666         options_corba.rwmode        =(captive_vfs_object->options.rwmode == CAPTIVE_OPTION_RWMODE_BLIND ? CAPTIVE_OPTION_RWMODE_RW
667                         : captive_vfs_object->options.rwmode);
668         options_corba.media         =captive_vfs_object->options.media;
669         options_corba.debug_messages=captive_vfs_object->options.debug_messages;
670         options_corba.image_iochannel=CaptiveIOChannel_object;
671
672         load_module_length=g_list_length(captive_vfs_object->options.load_module);
673         captive_newn(options_corba.load_module._buffer,load_module_length);
674         options_corba.load_module._maximum=load_module_length;
675         options_corba.load_module._length=load_module_length;
676         options_corba.load_module._release=TRUE;
677         for (load_moduleui=0,load_module_node=captive_vfs_object->options.load_module;
678                         load_module_node;
679                         load_moduleui++,load_module_node=load_module_node->next) {
680                 options_module=load_module_node->data;
681                 options_module_captive_to_options_module_corba(options_corba.load_module._buffer+load_moduleui,options_module);
682                 }
683
684         Captive_Vfs_init(Vfs_object,&options_corba,&captive_corba_ev);
685         g_assert(validate_CORBA_Environment(&captive_corba_ev));
686
687         /* FIXME: Free 'options_corba' - LEAK */
688
689         captive_vfs_object->corba_Vfs_object=Vfs_object;
690         captive_vfs_object->corba_GLogFunc_object=GLogFunc_object;
691         captive_vfs_object->corba_CaptiveIOChannel_object=CaptiveIOChannel_object;
692
693         if (captive_vfs_object->options.bug_pathname)
694                 sandbox_parent_bug_doc_make(captive_vfs_object);
695 }
696
697 static void fd_shiftup(int *fdp)
698 {
699         g_return_if_fail(fdp!=NULL);
700
701         while (*fdp<=2 /* STDERR */) {
702                 *fdp=dup(*fdp);
703                 g_assert(*fdp!=-1);
704                 }
705 }
706
707 void captive_sandbox_fd_closeup(int fd_first_to_delete)
708 {
709 DIR *dir;
710 int errint;
711 int dir_fd;
712 struct dirent *dirent;
713
714         dir=opendir("/proc/self/fd/");
715         g_return_if_fail(dir!=NULL);
716         dir_fd=dirfd(dir);
717         g_return_if_fail(dir_fd!=-1);
718
719         while (errno=0,(dirent=readdir(dir))) {
720 long dirent_fd;
721 char *endptr;
722
723                 if (0
724                                 || !strcmp(dirent->d_name,".")
725                                 || !strcmp(dirent->d_name,".."))
726                         continue;
727                 dirent_fd=strtol(dirent->d_name,&endptr,10 /* base */);
728                 g_assert(dirent_fd>=0 && (!endptr || !*endptr));
729                 if (dirent_fd<fd_first_to_delete || dirent_fd==dir_fd)
730                         continue;
731
732                 errint=close(dirent_fd);
733                 g_assert(errint==0);
734                 errno=0;
735                 errint=close(dirent_fd);
736                 g_assert(errint==-1); g_assert(errno==EBADF);
737                 }
738         g_return_if_fail(errno==0);     /* check for EOF */
739
740         errint=closedir(dir);
741         g_return_if_fail(errint==0);
742         errno=0;
743         close(dir_fd); g_assert(errno==EBADF);  /* just a bit of paranoia; it should be already closed by closedir() */
744 }
745
746 gboolean captive_sandbox_spawn(CaptiveVfsObject *captive_vfs_object)
747 {
748 /* Vfs_IOR_fds[0] for reading by sandbox_parent() - client,
749  * Vfs_IOR_fds[1] for writing by sandbox_child()  - server
750  */
751 int Vfs_IOR_fds[2],parentheart_fds[2];
752 int errint;
753 gboolean errbool;
754
755         g_return_val_if_fail(captive_vfs_object!=NULL,FALSE);
756
757         errint=pipe(Vfs_IOR_fds);
758         g_return_val_if_fail(errint==0,FALSE);
759         errint=pipe(parentheart_fds);
760         g_return_val_if_fail(errint==0,FALSE);
761
762         /* Currently never called anywhere:
763          *     errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
764          *     g_assert(errbool==TRUE);
765          */
766
767         if (captive_vfs_object->options.sandbox_server_ior) {
768                 g_assert(captive_vfs_object->options.sandbox_server_argv==NULL);
769
770                 errbool=corba_init("captive-sandbox-parent",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
771                 g_assert(errbool==TRUE);
772
773                 captive_vfs_object->corba_parentheart_fds_1=-1;
774                 captive_vfs_object->corba_child_pid=-1;
775                 sandbox_parent(
776                                 captive_vfs_object->options.sandbox_server_ior, /* Vfs_IOR */
777                                 NULL,   /* child_chroot_pid_hashkey_dir */
778                                 captive_vfs_object);    /* captive_vfs_object */
779                 return TRUE;
780                 }
781
782         g_assert(captive_vfs_object->options.sandbox_server_argv!=NULL);
783         switch ((captive_vfs_object->corba_child_pid=fork())) {
784                 case -1:        /* error */
785                         g_return_val_if_reached(FALSE);
786
787                 case 0: { /* child */
788
789                         errint=close(Vfs_IOR_fds[0]);   /* close Vfs_IOR_fd_read */
790                         g_return_val_if_fail(errint==0,FALSE);
791                         errint=close(parentheart_fds[1]);       /* close parentheart_fd_write */
792                         g_return_val_if_fail(errint==0,FALSE);
793
794                         fd_shiftup(Vfs_IOR_fds+1);      /* Vfs_IOR_fd_write */
795                         fd_shiftup(parentheart_fds+0);  /* parentheart_fd_read */
796                         errint=dup2(Vfs_IOR_fds[1],1 /* STDOUT */);     /* Vfs_IOR_fd_write */
797                         g_return_val_if_fail(errint==1 /* STDOUT */,FALSE);
798                         errint=dup2(parentheart_fds[0],0 /* STDIN */);  /* parentheart_fd_read */
799                         g_return_val_if_fail(errint==0 /* STDIN */,FALSE);
800
801                         captive_sandbox_fd_closeup(2 /* STDERR */ +1);
802
803                         execv(captive_vfs_object->options.sandbox_server_argv[0],
804                                         /* re-const */ (char * const *)captive_vfs_object->options.sandbox_server_argv);
805                         g_return_val_if_reached(FALSE);
806                         } /* NOTREACHED */
807
808                 default: {      /* parent */
809 gchar *Vfs_IOR;
810 gchar *child_chroot_pid_hashkey_dir;
811
812                         errint=close(Vfs_IOR_fds[1]);   /* close Vfs_IOR_fd_write */
813                         g_return_val_if_fail(errint==0,FALSE);
814                         errint=close(parentheart_fds[0]);       /* close parentheart_fd_read */
815                         g_return_val_if_fail(errint==0,FALSE);
816                         errint=fcntl(parentheart_fds[1],F_SETFD,FD_CLOEXEC);
817                         /* This fcntl(2) may not be enough - some fork(2) may duplicate this
818                          * write descriptor and even if we do some process finish the child
819                          * may remain alone connected with some unknown fork(2)er from us.
820                          * Currently I am not aware such case would occur.
821                          */
822                         g_return_val_if_fail(errint==0,FALSE);
823                         captive_vfs_object->corba_parentheart_fds_1=parentheart_fds[1];
824
825                         Vfs_IOR=sandbox_parent_read_ior(
826                                         Vfs_IOR_fds[0], /* Vfs_IOR_fd_read */
827                                         &child_chroot_pid_hashkey_dir);
828
829                         sandbox_parent(
830                                         Vfs_IOR,        /* Vfs_IOR */
831                                         child_chroot_pid_hashkey_dir,   /* child_chroot_pid_hashkey_dir */
832                                         captive_vfs_object);    /* captive_vfs_object */
833
834                         g_free(Vfs_IOR);
835                         g_free(child_chroot_pid_hashkey_dir);
836
837                         /* 'parentheart_fds[1]' - parentheart_fd_write - is left open here */
838                         return TRUE;
839                         }
840                 }
841         /* NOTREACHED */
842         g_return_val_if_reached(FALSE);
843 }
844
845
846 GnomeVFSResult captive_sandbox_parent_return_from_CORBA_Environment(CORBA_Environment *evp)
847 {
848 GnomeVFSResult r;
849
850         if (evp->_major==CORBA_NO_EXCEPTION)
851                 return GNOME_VFS_OK;
852
853         if (evp->_major==CORBA_USER_EXCEPTION && !strcmp(ex_Captive_GnomeVFSResultException,CORBA_exception_id(evp)))
854                 r=((Captive_GnomeVFSResultException *)CORBA_exception_value(evp))->gnome_vfs_result;
855         else {
856                 r=GNOME_VFS_ERROR_GENERIC;
857                 g_warning(_("CORBA Exception occured: id=\"%s\", value=%p"),
858                                 CORBA_exception_id(evp),CORBA_exception_value(evp));
859                 }
860         CORBA_exception_free(evp);
861
862         return r;
863 }
864
865
866 gboolean captive_sandbox_parent_query_vfs_retry(CORBA_Environment *evp,CaptiveVfsObject *captive_vfs_object)
867 {
868 GnomeVFSResult errvfsresult;
869 gboolean want_retry;
870
871         g_return_val_if_fail(evp!=NULL,FALSE);
872         g_return_val_if_fail(captive_vfs_object!=NULL,FALSE);
873
874         /* If !captive_vfs_object->options.sandbox_server_argv it is captive_vfs_object->options.sandbox_server_ior
875          * where we cannot do any restart anyway.
876          */
877         want_retry=(captive_vfs_object->options.sandbox_server_argv
878                         && (evp->_major==CORBA_SYSTEM_EXCEPTION && !strcmp(ex_CORBA_COMM_FAILURE,CORBA_exception_id(evp))));
879         /* Never free 'evp' if returning as no-retry - 'evp' will be reevaluated by the caller! */
880         if (!want_retry)
881                 return FALSE;   /* no retry */
882         CORBA_exception_free(evp);
883
884         captive_sandbox_parent_vfs_close(captive_vfs_object);   /* errors ignored */
885         errvfsresult=captive_sandbox_parent_vfs_new(captive_vfs_object);
886
887         return errvfsresult==GNOME_VFS_OK;      /* retry if restart succeeded */
888 }
889
890
891 void captive_sandbox_child_GnomeVFSResultException_throw(CORBA_Environment *evp,GnomeVFSResult errvfsresult)
892 {
893 Captive_GnomeVFSResultException *gnome_vfs_result_exception;
894
895         g_return_if_fail(evp!=NULL);
896
897         gnome_vfs_result_exception=Captive_GnomeVFSResultException__alloc();
898         gnome_vfs_result_exception->gnome_vfs_result=errvfsresult;
899         CORBA_exception_set(evp,CORBA_USER_EXCEPTION,ex_Captive_GnomeVFSResultException,gnome_vfs_result_exception);
900 }