2 * Connection of captive-vfs interface through CORBA/ORBit
3 * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
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
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.
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
22 #include "split.h" /* self */
24 #include "server-GLogFunc.h"
25 #include "captive/macros.h"
26 #include <glib/gmacros.h>
28 #include "captive/rtl-file.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"
37 #include "server-CaptiveIOChannel.h"
39 #include "../client/giochannel-blind.h" /* for captive_giochannel_blind_new() */
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)
49 gboolean validate_CORBA_Environment(CORBA_Environment *evp)
51 if (evp->_major==CORBA_NO_EXCEPTION)
54 g_error(_("CORBA Exception occured: id=\"%s\", value=%p"),
55 CORBA_exception_id(evp),CORBA_exception_value(evp));
56 CORBA_exception_free(evp);
60 CORBA_Environment captive_corba_ev;
61 CORBA_ORB captive_corba_orb;
62 PortableServer_POA captive_corba_poa;
64 static void corba_shutdown_atexit(void);
66 static gboolean corba_init(const char *pname,CORBA_Environment *evp,CORBA_ORB *orbp,PortableServer_POA *poap)
68 static gboolean done=FALSE;
71 (gchar *)captive_strdup_alloca(pname),
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);
82 CORBA_exception_init(evp);
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);
90 *poap=(PortableServer_POA)CORBA_ORB_resolve_initial_references(*orbp,"RootPOA",evp);
91 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
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);
102 g_atexit(corba_shutdown_atexit);
109 static CORBA_ORB heartbeat_source_callback_orb=CORBA_OBJECT_NIL;
111 gboolean corba_shutdown(CORBA_Environment *evp,CORBA_ORB *orbp,PortableServer_POA *poap)
113 PortableServer_POA poa;
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);
122 *poap=CORBA_OBJECT_NIL;
123 CORBA_Object_release((CORBA_Object)poa,evp);
124 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
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
135 CORBA_exception_free(evp);
138 CORBA_exception_free(evp);
143 static void corba_shutdown_atexit(void)
147 errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
148 g_assert(errbool==TRUE);
152 static GSource *captive_corba_sandbox_child_heartbeat_gsource;
154 #if 0 /* Currently unused - see server-Vfs.c:impl_Captive_Vfs_shutdown() */
155 void sandbox_child_prepare_shutdown(void)
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;
167 void sandbox_child_shutdown(void)
169 /* Do not fail by passing logging messages to the master. */
170 impl_Captive_Vfs_init_g_log_func_disable();
172 g_main_loop_quit(linc_main_get_loop());
176 static gboolean heartbeat_source_callback(gpointer data /* unused */)
178 g_return_val_if_fail(heartbeat_source_callback_orb!=CORBA_OBJECT_NIL,FALSE); /* the source should be removed */
180 sandbox_child_shutdown();
182 return FALSE; /* the source should be removed */
186 static gboolean heartbeat_source_prepare(GSource *source,gint *timeout)
193 static GPollFD heartbeat_source_check_gpollfd;
195 static gboolean heartbeat_source_check(GSource *source)
197 return !!(heartbeat_source_check_gpollfd.revents & HEARTBEAT_SOURCE_CHECK_REVENTS);
200 static gboolean heartbeat_source_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
202 g_assert(callback!=NULL);
203 return (*callback)(user_data);
206 static GSourceFuncs heartbeat_source_watch_funcs={
207 heartbeat_source_prepare,
208 heartbeat_source_check,
209 heartbeat_source_dispatch,
213 void captive_corba_sandbox_child(const gchar *chrooted_orbit_dir)
215 Captive_Vfs Vfs_object;
216 impl_POA_Captive_Vfs *Vfs_servant;
221 /* attach heartbeat_source_callback() to watch for any abnormalities
222 * on our open pipe 'parentheart_fds' and terminate the child if parent dies.
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 */
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);
236 errbool=corba_init("captive-sandbox-child",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
237 g_return_if_fail(errbool==TRUE);
239 heartbeat_source_callback_orb=captive_corba_orb;
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);
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));
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...
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);
265 /* Default mode is 0700, permit parent non-"captive" users to access our IOR socket. */
266 if (chrooted_orbit_dir) {
268 struct dirent *dirent;
269 const gchar *socketname=NULL,*socketpathname;
271 errint=chmod(chrooted_orbit_dir,0755);
274 dir=opendir(chrooted_orbit_dir);
276 while (errno=0,(dirent=readdir(dir))) {
277 if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
279 g_assert(socketname==NULL);
280 socketname=captive_strdup_alloca(dirent->d_name);
283 errint=closedir(dir);
285 g_assert(socketname!=NULL);
287 socketpathname=captive_printf_alloca("%s/%s",chrooted_orbit_dir,socketname);
288 errint=chmod(socketpathname,0666);
291 printf("socketname=%s\n",socketname);
294 /* Close the output to flush it to our spawning parent. */
295 errint=fflush(stdout);
297 errint=fclose(stdout);
300 /* CORBA_ORB_run() -> linc_main_loop_run() -> g_main_loop_run()
301 * and therefore we should be safe with glib events handling.
303 CORBA_ORB_run(captive_corba_orb,&captive_corba_ev);
304 g_assert(validate_CORBA_Environment(&captive_corba_ev));
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));
314 errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
315 g_assert(errbool==TRUE);
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)
324 g_return_if_fail(dest_options_module_corba!=NULL);
325 g_return_if_fail(src_options_module_captive!=NULL);
327 g_return_if_fail(src_options_module_captive->type==CAPTIVE_OPTIONS_MODULE_TYPE_PE32);
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;
337 static void unlink_nonrecursive(const gchar *dirname)
340 struct dirent *dirent;
343 dir=opendir(dirname);
346 while (errno=0,(dirent=readdir(dir))) {
349 if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
351 pathname=g_strdup_printf("%s/%s",dirname,dirent->d_name);
352 errint=unlink(pathname);
357 errint=closedir(dir);
359 errint=rmdir(dirname);
363 static const gchar *sandbox_parent_own_orbit_dir;
364 static const gchar *sandbox_parent_own_orbit_socket;
366 static void sandbox_parent_own_orbit_dir_cleanup_atexit(void)
368 static gboolean done=FALSE;
374 unlink_nonrecursive(sandbox_parent_own_orbit_dir);
377 static struct sandbox_parent_own_orbit_dir_cleanup_signal {
379 /* FIXME: Why we cannot use 'sighandler_t'? */ void (*sighandler_orig)(int signum);
380 } sandbox_parent_own_orbit_dir_cleanup_signals[]={
389 static void sandbox_parent_own_orbit_dir_cleanup_sighandler(int signum)
391 struct sandbox_parent_own_orbit_dir_cleanup_signal *sigstructp;
393 sandbox_parent_own_orbit_dir_cleanup_atexit();
396 sigstructp=sandbox_parent_own_orbit_dir_cleanup_signals;
397 sigstructp<sandbox_parent_own_orbit_dir_cleanup_signals+G_N_ELEMENTS(sandbox_parent_own_orbit_dir_cleanup_signals);
399 if (sigstructp->signum==signum)
402 g_assert(sigstructp<sandbox_parent_own_orbit_dir_cleanup_signals+G_N_ELEMENTS(sandbox_parent_own_orbit_dir_cleanup_signals));
403 signal(signum,sigstructp->sighandler_orig);
407 static void sandbox_parent_own_orbit_dir_cleanup_init(void)
409 struct sandbox_parent_own_orbit_dir_cleanup_signal *sigstructp;
411 g_atexit(sandbox_parent_own_orbit_dir_cleanup_atexit);
413 sigstructp=sandbox_parent_own_orbit_dir_cleanup_signals;
414 sigstructp<sandbox_parent_own_orbit_dir_cleanup_signals+G_N_ELEMENTS(sandbox_parent_own_orbit_dir_cleanup_signals);
416 sigstructp->sighandler_orig=signal(sigstructp->signum,sandbox_parent_own_orbit_dir_cleanup_sighandler);
417 if (sigstructp->sighandler_orig==SIG_IGN)
418 sigstructp->sighandler_orig=SIG_DFL;
422 gchar *sandbox_parent_read_ior(int Vfs_IOR_fd_read,gchar **child_chroot_pid_hashkey_dirp)
427 gchar *ior,*child_chroot_pid_hashkey_dir;
432 /* Initialize /tmp/orbit-$username directory for linking IOR socket. */
433 errbool=corba_init("captive-sandbox-parent",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
434 g_assert(errbool==TRUE);
436 /* FIXME: Security: Duplicate giop_tmpdir_init() here. */
437 if (!sandbox_parent_own_orbit_dir) {
438 sandbox_parent_own_orbit_dir=g_strdup_printf("/tmp/captive-orbit-%d",getpid());
439 if (mkdir(sandbox_parent_own_orbit_dir,0700)) {
440 g_assert(errno==EEXIST);
441 sandbox_parent_own_orbit_dir_cleanup_init();
442 unlink_nonrecursive(sandbox_parent_own_orbit_dir);
443 if (mkdir(sandbox_parent_own_orbit_dir,0700))
444 g_assert_not_reached();
446 sandbox_parent_own_orbit_dir_cleanup_init();
447 linc_set_tmpdir(sandbox_parent_own_orbit_dir);
450 data=captive_rtl_file_read(Vfs_IOR_fd_read,&data_size); /* data_fd_read */
451 errint=close(Vfs_IOR_fd_read);
453 g_assert(data!=NULL);
454 g_assert(data_size>=1);
455 g_assert(data[data_size-1]=='\n');
457 hash=g_hash_table_new(g_str_hash,g_str_equal);
459 for (s=data;s<data+data_size;s=se+1) {
466 g_hash_table_insert(hash,s,sd+1);
468 g_assert(s==data+data_size);
470 if ((child_chroot_pid_hashkey_dir=g_hash_table_lookup(hash,"chroot_pid_hashkey_dir"))) {
471 const gchar *socketname,*socketpathname_src,*socketpathname_dest;
472 const gchar *chrooted_orbit_dir;
474 /* IOR contains the full pathname with the setuid username encoded. */
475 chrooted_orbit_dir=g_hash_table_lookup(hash,"chrooted_orbit_dir");
476 g_assert(chrooted_orbit_dir!=NULL);
477 if (mkdir(chrooted_orbit_dir,S_ISVTX|0777)) {
478 g_assert(errno==EEXIST);
481 socketname=g_hash_table_lookup(hash,"socketname");
482 g_assert(socketname!=NULL);
484 socketpathname_src=captive_printf_alloca("%s/%s/%s",child_chroot_pid_hashkey_dir,chrooted_orbit_dir,socketname);
485 socketpathname_dest=captive_printf_alloca("%s/%s",chrooted_orbit_dir,socketname);
486 errint=link(socketpathname_src,socketpathname_dest);
489 if (child_chroot_pid_hashkey_dirp)
490 *child_chroot_pid_hashkey_dirp=g_strdup(child_chroot_pid_hashkey_dir);
492 ior=g_hash_table_lookup(hash,"ior");
497 g_hash_table_destroy(hash);
503 static xmlNode *options_module_captive_to_xml
504 (xmlNode *dest_xml_parent,const struct captive_options_module *src_options_module_captive)
506 g_return_val_if_fail(dest_xml_parent!=NULL,NULL);
507 g_return_val_if_fail(src_options_module_captive!=NULL,NULL);
509 { xmlNode *module=xmlNewTextChild(dest_xml_parent,NULL,"module",NULL);
510 const gchar *type_string,*basename,*cgs;
512 basename=src_options_module_captive->pathname_utf8;
513 if ((cgs=strrchr(basename,'/')))
515 xmlNewProp(module,"basename",basename);
516 switch (src_options_module_captive->type) {
517 case CAPTIVE_OPTIONS_MODULE_TYPE_PE32:
519 xmlNewProp(module,"length",captive_printf_alloca("%lu",(unsigned long)src_options_module_captive->u.pe32.length));
520 xmlNewProp(module,"md5" ,src_options_module_captive->u.pe32.md5);
522 case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
523 type_string="gmodule";
525 default: g_assert_not_reached();
527 xmlNewProp(module,"type",type_string); /* AFTER the 'switch' to set 'type_string'! */
533 static void sandbox_parent_bug_doc_make(CaptiveVfsObject *captive_vfs_object)
535 { xmlDoc *doc=xmlNewDoc("1.0");
536 captive_vfs_object->corba_bug_doc=doc;
537 { xmlNode *bug=xmlNewDocNode(captive_vfs_object->corba_bug_doc,NULL,"bug",NULL);
538 xmlDocSetRootElement(captive_vfs_object->corba_bug_doc,bug);
539 captive_vfs_object->corba_bug=bug;
540 { xmlNode *bug_captive=xmlNewTextChild(bug,NULL,"captive",NULL);
541 xmlNewProp(bug_captive,"version",VERSION);
543 { xmlNode *bug_filesystem=xmlNewTextChild(bug,NULL,"filesystem",NULL);
544 options_module_captive_to_xml(bug_filesystem,&captive_vfs_object->options.filesystem);
546 { xmlNode *bug_load_module=xmlNewTextChild(bug,NULL,"load_module",NULL);
548 struct captive_options_module *options_module;
549 GList *load_module_node;
551 for (load_moduleui=0,load_module_node=captive_vfs_object->options.load_module;
553 load_moduleui++,load_module_node=load_module_node->next) {
554 options_module=load_module_node->data;
555 options_module_captive_to_xml(bug_load_module,options_module);
558 { xmlNode *bug_action=xmlNewTextChild(bug,NULL,"action",NULL);
559 captive_vfs_object->corba_bug_action=bug_action;
561 { xmlNode *bug_log=xmlNewTextChild(bug,NULL,"log",NULL);
562 captive_vfs_object->corba_bug_log=bug_log;
569 static void sandbox_parent(const gchar *Vfs_IOR,const gchar *child_chroot_pid_hashkey_dir,CaptiveVfsObject *captive_vfs_object)
571 Captive_Vfs Vfs_object;
572 Captive_GLogFunc GLogFunc_object;
573 Captive_CaptiveIOChannel CaptiveIOChannel_object;
574 Captive_CaptiveOptions options_corba;
575 guint load_module_length,load_moduleui;
576 struct captive_options_module *options_module;
577 GList *load_module_node;
578 const gchar *child_chroot_parent_own_orbit_socket,*child_chroot_parent_own_orbit_dir;
581 g_return_if_fail(Vfs_IOR!=NULL);
582 /* 'child_chroot_pid_hashkey_dir' may be NULL */
583 g_return_if_fail(captive_vfs_object!=NULL);
585 Vfs_object=CORBA_ORB_string_to_object(captive_corba_orb,Vfs_IOR,&captive_corba_ev);
586 g_assert(validate_CORBA_Environment(&captive_corba_ev));
588 /* Init 'GLogFunc_object' */
589 GLogFunc_object=impl_Captive_GLogFunc__create(captive_corba_poa,captive_vfs_object,&captive_corba_ev);
590 g_assert(validate_CORBA_Environment(&captive_corba_ev));
592 /* Init 'CaptiveIOChannel_object' */
593 if (!captive_vfs_object->corba_parent_giochanel_blind_source)
594 switch (captive_vfs_object->options.rwmode) {
595 case CAPTIVE_OPTION_RWMODE_RO:
596 case CAPTIVE_OPTION_RWMODE_RW:
597 captive_vfs_object->corba_parent_giochanel_blind_source=captive_vfs_object->options.image_iochannel;
599 case CAPTIVE_OPTION_RWMODE_BLIND:
600 captive_vfs_object->corba_parent_giochanel_blind_source=(GIOChannel *)captive_giochannel_blind_new(
601 captive_vfs_object->options.image_iochannel, /* giochannel_orig */
602 TRUE); /* writeable */
604 default: g_assert_not_reached();
606 if (!captive_vfs_object->corba_parent_giochanel_blind)
607 captive_vfs_object->corba_parent_giochanel_blind=(GIOChannel *)captive_giochannel_blind_new(
608 captive_vfs_object->corba_parent_giochanel_blind_source, /* giochannel_orig */
609 (captive_vfs_object->options.rwmode!=CAPTIVE_OPTION_RWMODE_RO)); /* writeable */
610 CaptiveIOChannel_object=impl_Captive_CaptiveIOChannel__create(captive_corba_poa,
611 captive_vfs_object->corba_parent_giochanel_blind,&captive_corba_ev);
612 g_assert(validate_CORBA_Environment(&captive_corba_ev));
614 /* Create the socket needed for 'sandbox_parent_own_orbit_socket' below
615 * by CORBA_ORB_object_to_string()->ORBit_marshal_object()->IOP_generate_profiles()->ORBit_ORB_start_servers...
619 GLogFunc_IOR=CORBA_ORB_object_to_string(captive_corba_orb,GLogFunc_object,&captive_corba_ev);
620 g_assert(validate_CORBA_Environment(&captive_corba_ev));
621 g_assert(GLogFunc_IOR!=NULL);
622 CORBA_free(GLogFunc_IOR);
625 if (sandbox_parent_own_orbit_dir && !sandbox_parent_own_orbit_socket) {
627 struct dirent *dirent;
629 dir=opendir(sandbox_parent_own_orbit_dir);
632 while (errno=0,(dirent=readdir(dir))) {
633 if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
635 g_assert(sandbox_parent_own_orbit_socket==NULL);
636 sandbox_parent_own_orbit_socket=g_strdup_printf("%s/%s",sandbox_parent_own_orbit_dir,dirent->d_name);
639 errint=closedir(dir);
641 g_assert(sandbox_parent_own_orbit_socket!=NULL);
644 if (child_chroot_pid_hashkey_dir) {
645 child_chroot_parent_own_orbit_dir=captive_printf_alloca("%s/%s",child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_dir);
646 errint=mkdir(child_chroot_parent_own_orbit_dir,0777);
648 child_chroot_parent_own_orbit_socket=captive_printf_alloca("%s/%s",
649 child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_socket);
650 errint=link(sandbox_parent_own_orbit_socket,child_chroot_parent_own_orbit_socket);
652 /* chmod(2)s also our orig. one (!) but this one is protected by its 0777 directory. */
653 errint=chmod(child_chroot_parent_own_orbit_socket,0666);
657 options_corba.g_log_func=GLogFunc_object;
658 options_module_captive_to_options_module_corba(&options_corba.filesystem,&captive_vfs_object->options.filesystem);
659 /* Prevent secondary captive_giochannel_blind inside of our sandbox child
660 * as we already have one captive_giochannel_blind in the parent.
662 options_corba.rwmode =(captive_vfs_object->options.rwmode == CAPTIVE_OPTION_RWMODE_BLIND ? CAPTIVE_OPTION_RWMODE_RW
663 : captive_vfs_object->options.rwmode);
664 options_corba.media =captive_vfs_object->options.media;
665 options_corba.debug_messages=captive_vfs_object->options.debug_messages;
666 options_corba.image_iochannel=CaptiveIOChannel_object;
668 load_module_length=g_list_length(captive_vfs_object->options.load_module);
669 captive_newn(options_corba.load_module._buffer,load_module_length);
670 options_corba.load_module._maximum=load_module_length;
671 options_corba.load_module._length=load_module_length;
672 options_corba.load_module._release=TRUE;
673 for (load_moduleui=0,load_module_node=captive_vfs_object->options.load_module;
675 load_moduleui++,load_module_node=load_module_node->next) {
676 options_module=load_module_node->data;
677 options_module_captive_to_options_module_corba(options_corba.load_module._buffer+load_moduleui,options_module);
680 Captive_Vfs_init(Vfs_object,&options_corba,&captive_corba_ev);
681 g_assert(validate_CORBA_Environment(&captive_corba_ev));
683 /* FIXME: Free 'options_corba' - LEAK */
685 captive_vfs_object->corba_Vfs_object=Vfs_object;
686 captive_vfs_object->corba_GLogFunc_object=GLogFunc_object;
687 captive_vfs_object->corba_CaptiveIOChannel_object=CaptiveIOChannel_object;
689 if (captive_vfs_object->options.bug_pathname)
690 sandbox_parent_bug_doc_make(captive_vfs_object);
693 static void fd_shiftup(int *fdp)
695 g_return_if_fail(fdp!=NULL);
697 while (*fdp<=2 /* STDERR */) {
703 void captive_sandbox_fd_closeup(int fd_first_to_delete)
708 struct dirent *dirent;
710 dir=opendir("/proc/self/fd/");
711 g_return_if_fail(dir!=NULL);
713 g_return_if_fail(dir_fd!=-1);
715 while (errno=0,(dirent=readdir(dir))) {
720 || !strcmp(dirent->d_name,".")
721 || !strcmp(dirent->d_name,".."))
723 dirent_fd=strtol(dirent->d_name,&endptr,10 /* base */);
724 g_assert(dirent_fd>=0 && (!endptr || !*endptr));
725 if (dirent_fd<fd_first_to_delete || dirent_fd==dir_fd)
728 errint=close(dirent_fd);
731 errint=close(dirent_fd);
732 g_assert(errint==-1); g_assert(errno==EBADF);
734 g_return_if_fail(errno==0); /* check for EOF */
736 errint=closedir(dir);
737 g_return_if_fail(errint==0);
739 close(dir_fd); g_assert(errno==EBADF); /* just a bit of paranoia; it should be already closed by closedir() */
742 gboolean captive_sandbox_spawn(CaptiveVfsObject *captive_vfs_object)
744 /* Vfs_IOR_fds[0] for reading by sandbox_parent() - client,
745 * Vfs_IOR_fds[1] for writing by sandbox_child() - server
747 int Vfs_IOR_fds[2],parentheart_fds[2];
751 g_return_val_if_fail(captive_vfs_object!=NULL,FALSE);
753 errint=pipe(Vfs_IOR_fds);
754 g_return_val_if_fail(errint==0,FALSE);
755 errint=pipe(parentheart_fds);
756 g_return_val_if_fail(errint==0,FALSE);
758 /* Currently never called anywhere:
759 * errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
760 * g_assert(errbool==TRUE);
763 if (captive_vfs_object->options.sandbox_server_ior) {
764 g_assert(captive_vfs_object->options.sandbox_server_argv==NULL);
766 errbool=corba_init("captive-sandbox-parent",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
767 g_assert(errbool==TRUE);
769 captive_vfs_object->corba_parentheart_fds_1=-1;
770 captive_vfs_object->corba_child_pid=-1;
772 captive_vfs_object->options.sandbox_server_ior, /* Vfs_IOR */
773 NULL, /* child_chroot_pid_hashkey_dir */
774 captive_vfs_object); /* captive_vfs_object */
778 g_assert(captive_vfs_object->options.sandbox_server_argv!=NULL);
779 switch ((captive_vfs_object->corba_child_pid=fork())) {
781 g_return_val_if_reached(FALSE);
783 case 0: { /* child */
785 errint=close(Vfs_IOR_fds[0]); /* close Vfs_IOR_fd_read */
786 g_return_val_if_fail(errint==0,FALSE);
787 errint=close(parentheart_fds[1]); /* close parentheart_fd_write */
788 g_return_val_if_fail(errint==0,FALSE);
790 fd_shiftup(Vfs_IOR_fds+1); /* Vfs_IOR_fd_write */
791 fd_shiftup(parentheart_fds+0); /* parentheart_fd_read */
792 errint=dup2(Vfs_IOR_fds[1],1 /* STDOUT */); /* Vfs_IOR_fd_write */
793 g_return_val_if_fail(errint==1 /* STDOUT */,FALSE);
794 errint=dup2(parentheart_fds[0],0 /* STDIN */); /* parentheart_fd_read */
795 g_return_val_if_fail(errint==0 /* STDIN */,FALSE);
797 captive_sandbox_fd_closeup(2 /* STDERR */ +1);
799 execv(captive_vfs_object->options.sandbox_server_argv[0],
800 /* re-const */ (char * const *)captive_vfs_object->options.sandbox_server_argv);
801 g_return_val_if_reached(FALSE);
804 default: { /* parent */
806 gchar *child_chroot_pid_hashkey_dir;
808 errint=close(Vfs_IOR_fds[1]); /* close Vfs_IOR_fd_write */
809 g_return_val_if_fail(errint==0,FALSE);
810 errint=close(parentheart_fds[0]); /* close parentheart_fd_read */
811 g_return_val_if_fail(errint==0,FALSE);
812 errint=fcntl(parentheart_fds[1],F_SETFD,FD_CLOEXEC);
813 /* This fcntl(2) may not be enough - some fork(2) may duplicate this
814 * write descriptor and even if we do some process finish the child
815 * may remain alone connected with some unknown fork(2)er from us.
816 * Currently I am not aware such case would occur.
818 g_return_val_if_fail(errint==0,FALSE);
819 captive_vfs_object->corba_parentheart_fds_1=parentheart_fds[1];
821 Vfs_IOR=sandbox_parent_read_ior(
822 Vfs_IOR_fds[0], /* Vfs_IOR_fd_read */
823 &child_chroot_pid_hashkey_dir);
826 Vfs_IOR, /* Vfs_IOR */
827 child_chroot_pid_hashkey_dir, /* child_chroot_pid_hashkey_dir */
828 captive_vfs_object); /* captive_vfs_object */
831 g_free(child_chroot_pid_hashkey_dir);
833 /* 'parentheart_fds[1]' - parentheart_fd_write - is left open here */
838 g_return_val_if_reached(FALSE);
842 GnomeVFSResult captive_sandbox_parent_return_from_CORBA_Environment(CORBA_Environment *evp)
846 if (evp->_major==CORBA_NO_EXCEPTION)
849 if (evp->_major==CORBA_USER_EXCEPTION && !strcmp(ex_Captive_GnomeVFSResultException,CORBA_exception_id(evp)))
850 r=((Captive_GnomeVFSResultException *)CORBA_exception_value(evp))->gnome_vfs_result;
852 r=GNOME_VFS_ERROR_GENERIC;
853 g_warning(_("CORBA Exception occured: id=\"%s\", value=%p"),
854 CORBA_exception_id(evp),CORBA_exception_value(evp));
856 CORBA_exception_free(evp);
862 gboolean captive_sandbox_parent_query_vfs_retry(CORBA_Environment *evp,CaptiveVfsObject *captive_vfs_object)
864 GnomeVFSResult errvfsresult;
867 g_return_val_if_fail(evp!=NULL,FALSE);
868 g_return_val_if_fail(captive_vfs_object!=NULL,FALSE);
870 /* If !captive_vfs_object->options.sandbox_server_argv it is captive_vfs_object->options.sandbox_server_ior
871 * where we cannot do any restart anyway.
873 want_retry=(captive_vfs_object->options.sandbox_server_argv
874 && (evp->_major==CORBA_SYSTEM_EXCEPTION && !strcmp(ex_CORBA_COMM_FAILURE,CORBA_exception_id(evp))));
875 /* Never free 'evp' if returning as no-retry - 'evp' will be reevaluated by the caller! */
877 return FALSE; /* no retry */
878 CORBA_exception_free(evp);
880 captive_sandbox_parent_vfs_close(captive_vfs_object); /* errors ignored */
881 errvfsresult=captive_sandbox_parent_vfs_new(captive_vfs_object);
883 return errvfsresult==GNOME_VFS_OK; /* retry if restart succeeded */
887 void captive_sandbox_child_GnomeVFSResultException_throw(CORBA_Environment *evp,GnomeVFSResult errvfsresult)
889 Captive_GnomeVFSResultException *gnome_vfs_result_exception;
891 g_return_if_fail(evp!=NULL);
893 gnome_vfs_result_exception=Captive_GnomeVFSResultException__alloc();
894 gnome_vfs_result_exception->gnome_vfs_result=errvfsresult;
895 CORBA_exception_set(evp,CORBA_USER_EXCEPTION,ex_Captive_GnomeVFSResultException,gnome_vfs_result_exception);