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 "server-Directory.h"
32 #include "server-Vfs.h"
33 #include "../client/vfs.h"
34 #include "parent-Vfs.h"
36 #include "server-CaptiveIOChannel.h"
38 #include "../client/giochannel-blind.h" /* for captive_giochannel_blind_new() */
41 #ifdef HAVE_ORBIT_LINK
42 void link_set_tmpdir(const char *dir);
44 #include <linc/linc.h> /* for linc_main_get_loop() */
50 #define HEARTBEAT_SOURCE_CHECK_EVENTS (G_IO_IN|G_IO_PRI)
51 #define HEARTBEAT_SOURCE_CHECK_REVENTS (HEARTBEAT_SOURCE_CHECK_EVENTS|G_IO_ERR|G_IO_HUP|G_IO_NVAL)
54 gboolean validate_CORBA_Environment(CORBA_Environment *evp)
56 if (evp->_major==CORBA_NO_EXCEPTION)
59 g_error(_("CORBA Exception occured: id=\"%s\", value=%p"),
60 CORBA_exception_id(evp),CORBA_exception_value(evp));
61 CORBA_exception_free(evp);
65 CORBA_Environment captive_corba_ev;
66 CORBA_ORB captive_corba_orb;
67 PortableServer_POA captive_corba_poa;
69 static void corba_shutdown_atexit(void);
71 static gboolean corba_init(const char *pname,CORBA_Environment *evp,CORBA_ORB *orbp,PortableServer_POA *poap)
73 static gboolean done=FALSE;
76 (gchar *)captive_strdup_alloca(pname),
79 g_return_val_if_fail(evp!=NULL,FALSE);
80 g_return_val_if_fail(orbp!=NULL,FALSE);
81 g_return_val_if_fail(poap!=NULL,FALSE);
84 return TRUE; /* FIXME: '*poap' is left invalid! */
87 CORBA_exception_init(evp);
90 /* libcaptive is single-threaded only, caller must lock it.
91 * If thread A spawned the sandbox while currently doing its own work
92 * and thread B calls the sandbox thread B waits on ORB_run()
93 * while the sandbox waits for the response of thread A ORB. Deadlock.
94 * "orbit-local-non-threaded-orb" requests thread unaware ORB.
96 *orbp=CORBA_ORB_init(&orb_argc,orb_argv,"orbit-local-non-threaded-orb",evp);
97 g_return_val_if_fail(*orbp!=CORBA_OBJECT_NIL,FALSE);
98 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
101 *poap=(PortableServer_POA)CORBA_ORB_resolve_initial_references(*orbp,"RootPOA",evp);
102 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
104 PortableServer_POAManager poa_mgr;
105 poa_mgr=PortableServer_POA__get_the_POAManager(*poap,evp);
106 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
107 PortableServer_POAManager_activate(poa_mgr,evp);
108 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
109 CORBA_Object_release((CORBA_Object)poa_mgr,evp);
110 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
113 g_atexit(corba_shutdown_atexit);
120 static CORBA_ORB heartbeat_source_callback_orb=CORBA_OBJECT_NIL;
122 gboolean corba_shutdown(CORBA_Environment *evp,CORBA_ORB *orbp,PortableServer_POA *poap)
124 PortableServer_POA poa;
127 g_return_val_if_fail(evp!=NULL,FALSE);
128 g_return_val_if_fail(orbp!=NULL,FALSE);
129 g_return_val_if_fail(poap!=NULL,FALSE);
133 *poap=CORBA_OBJECT_NIL;
134 CORBA_Object_release((CORBA_Object)poa,evp);
135 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
139 *orbp=CORBA_OBJECT_NIL;
140 heartbeat_source_callback_orb=CORBA_OBJECT_NIL;
141 CORBA_ORB_destroy(orb,evp);
142 /* Do not: g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
143 * here as CORBA_ORB_destroy() sometimes reports:
144 * WARNING **: ORB: a total of X refs to X ORB objects were leaked
146 CORBA_exception_free(evp);
149 CORBA_exception_free(evp);
154 static void sandbox_parent_own_orbit_dir_cleanup_atexit(void);
156 static void corba_shutdown_atexit(void)
160 errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
161 g_assert(errbool==TRUE);
163 /* Delete the CORBA ORB socket directory only after full CORBA ORB shutdown. */
164 sandbox_parent_own_orbit_dir_cleanup_atexit();
168 static GSource *captive_corba_sandbox_child_heartbeat_gsource;
170 #if 0 /* Currently unused - see server-Vfs.c:impl_Captive_Vfs_shutdown() */
171 void sandbox_child_prepare_shutdown(void)
173 /* Prevent during shutdown: Captive-WARNING **: CORBA Exception occured: id="IDL:omg.org/CORBA/COMM_FAILURE:1.0" */
174 if (captive_corba_sandbox_child_heartbeat_gsource) {
175 g_source_destroy(captive_corba_sandbox_child_heartbeat_gsource);
176 g_source_unref(captive_corba_sandbox_child_heartbeat_gsource);
177 captive_corba_sandbox_child_heartbeat_gsource=NULL;
182 GMainLoop *captive_corba_get_loop(void)
184 #ifdef HAVE_ORBIT_LINK
185 static GMainLoop *link_loop;
188 link_loop=g_main_loop_new(g_main_context_default(),TRUE);
191 return linc_main_get_loop();
195 GMainContext *captive_corba_get_context(void)
197 return g_main_loop_get_context(captive_corba_get_loop());
200 void sandbox_child_shutdown(void)
202 /* Do not fail by passing logging messages to the master. */
203 impl_Captive_Vfs_init_g_log_func_disable();
205 g_main_loop_quit(captive_corba_get_loop());
209 static gboolean heartbeat_source_callback(gpointer data /* unused */)
211 g_return_val_if_fail(heartbeat_source_callback_orb!=CORBA_OBJECT_NIL,FALSE); /* the source should be removed */
213 sandbox_child_shutdown();
215 return FALSE; /* the source should be removed */
219 static gboolean heartbeat_source_prepare(GSource *source,gint *timeout)
226 static GPollFD heartbeat_source_check_gpollfd;
228 static gboolean heartbeat_source_check(GSource *source)
230 return !!(heartbeat_source_check_gpollfd.revents & HEARTBEAT_SOURCE_CHECK_REVENTS);
233 static gboolean heartbeat_source_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
235 g_assert(callback!=NULL);
236 return (*callback)(user_data);
239 static GSourceFuncs heartbeat_source_watch_funcs={
240 heartbeat_source_prepare,
241 heartbeat_source_check,
242 heartbeat_source_dispatch,
246 void captive_corba_sandbox_child(const gchar *chrooted_orbit_dir)
248 Captive_Vfs Vfs_object;
249 impl_POA_Captive_Vfs *Vfs_servant;
254 /* attach heartbeat_source_callback() to watch for any abnormalities
255 * on our open pipe 'parentheart_fds' and terminate the child if parent dies.
257 captive_corba_sandbox_child_heartbeat_gsource=g_source_new(&heartbeat_source_watch_funcs,sizeof(GSource));
258 g_return_if_fail(captive_corba_sandbox_child_heartbeat_gsource!=NULL);
259 g_source_set_callback(
260 captive_corba_sandbox_child_heartbeat_gsource, /* source */
261 heartbeat_source_callback, /* func */
264 heartbeat_source_check_gpollfd.fd=0 /* STDIN */; /* parentheart_fd_read */
265 heartbeat_source_check_gpollfd.events=HEARTBEAT_SOURCE_CHECK_EVENTS;
266 heartbeat_source_check_gpollfd.revents=0;
267 g_source_add_poll(captive_corba_sandbox_child_heartbeat_gsource,&heartbeat_source_check_gpollfd);
269 errbool=corba_init("captive-sandbox-child",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
270 g_return_if_fail(errbool==TRUE);
272 heartbeat_source_callback_orb=captive_corba_orb;
274 /* captive_corba_get_context() makes sense only after corba_init() -> CORBA_ORB_init() */
275 errguint=g_source_attach(
276 captive_corba_sandbox_child_heartbeat_gsource, /* source */
277 captive_corba_get_context()); /* context; NULL means 'default context' */
278 g_assert(errguint!=0);
280 /* Init 'Vfs_object' */
281 Vfs_object=impl_Captive_Vfs__create(captive_corba_poa,&captive_corba_ev);
282 g_assert(validate_CORBA_Environment(&captive_corba_ev));
284 /* Pass IOR to our parent.
285 * It will also create the socket needed for 'chrooted_orbit_dir' below
286 * by CORBA_ORB_object_to_string()->ORBit_marshal_object()->IOP_generate_profiles()->ORBit_ORB_start_servers...
290 Vfs_IOR=CORBA_ORB_object_to_string(captive_corba_orb,Vfs_object,&captive_corba_ev);
291 g_assert(validate_CORBA_Environment(&captive_corba_ev));
292 g_assert(Vfs_IOR!=NULL);
293 errint=printf("ior=%s\n",Vfs_IOR);
298 /* Default mode is 0700, permit parent non-"captive" users to access our IOR socket. */
299 if (chrooted_orbit_dir) {
301 struct dirent *dirent;
302 const gchar *socketname=NULL,*socketpathname;
304 errint=chmod(chrooted_orbit_dir,0755);
307 dir=opendir(chrooted_orbit_dir);
309 while (errno=0,(dirent=readdir(dir))) {
310 if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
312 g_assert(socketname==NULL);
313 socketname=captive_strdup_alloca(dirent->d_name);
316 errint=closedir(dir);
318 g_assert(socketname!=NULL);
320 socketpathname=captive_printf_alloca("%s/%s",chrooted_orbit_dir,socketname);
321 errint=chmod(socketpathname,0666);
324 printf("socketname=%s\n",socketname);
327 /* Close the output to flush it to our spawning parent. */
328 errint=fflush(stdout);
330 errint=fclose(stdout);
333 /* CORBA_ORB_run() -> linc_main_loop_run() -> g_main_loop_run()
334 * and therefore we should be safe with glib events handling.
336 #ifdef HAVE_ORBIT_LINK
337 g_main_loop_run(captive_corba_get_loop());
339 CORBA_ORB_run(captive_corba_orb,&captive_corba_ev);
340 g_assert(validate_CORBA_Environment(&captive_corba_ev));
343 /* Shutdown 'Vfs' servant */
344 Vfs_servant=PortableServer_POA_reference_to_servant(captive_corba_poa,Vfs_object,&captive_corba_ev);
345 g_assert(validate_CORBA_Environment(&captive_corba_ev));
346 CORBA_Object_release(Vfs_object,&captive_corba_ev);
347 g_assert(validate_CORBA_Environment(&captive_corba_ev));
348 impl_Captive_Vfs__destroy(Vfs_servant,&captive_corba_ev);
349 g_assert(validate_CORBA_Environment(&captive_corba_ev));
351 errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
352 g_assert(errbool==TRUE);
358 static void options_module_captive_to_options_module_corba
359 (Captive_CaptiveOptionsModule *dest_options_module_corba,const struct captive_options_module *src_options_module_captive)
361 g_return_if_fail(dest_options_module_corba!=NULL);
362 g_return_if_fail(src_options_module_captive!=NULL);
364 g_return_if_fail(src_options_module_captive->type==CAPTIVE_OPTIONS_MODULE_TYPE_PE32);
366 dest_options_module_corba->pathname_utf8=CORBA_string_dup(src_options_module_captive->pathname_utf8);
367 dest_options_module_corba->data._maximum=src_options_module_captive->u.pe32.length;
368 dest_options_module_corba->data._length =src_options_module_captive->u.pe32.length;
369 dest_options_module_corba->data._buffer=Captive_Bytes_allocbuf(dest_options_module_corba->data._maximum);
370 memcpy(dest_options_module_corba->data._buffer,src_options_module_captive->u.pe32.base,
371 src_options_module_captive->u.pe32.length);
372 dest_options_module_corba->data._release=TRUE;
375 static void unlink_nonrecursive(const gchar *dirname)
378 struct dirent *dirent;
381 dir=opendir(dirname);
384 while (errno=0,(dirent=readdir(dir))) {
387 if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
389 pathname=g_strdup_printf("%s/%s",dirname,dirent->d_name);
390 errint=unlink(pathname);
395 errint=closedir(dir);
397 errint=rmdir(dirname);
401 static const gchar *sandbox_parent_own_orbit_dir;
402 static const gchar *sandbox_parent_own_orbit_socket;
404 static void sandbox_parent_own_orbit_dir_cleanup_atexit(void)
406 static gboolean done=FALSE;
408 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s; done=%d",G_STRLOC,(int)done);
414 if (sandbox_parent_own_orbit_dir)
415 unlink_nonrecursive(sandbox_parent_own_orbit_dir);
418 static struct sandbox_parent_own_orbit_dir_cleanup_signal {
420 /* FIXME: Why we cannot use 'sighandler_t'? */ void (*sighandler_orig)(int signum);
421 } sandbox_parent_own_orbit_dir_cleanup_signals[]={
430 static void sandbox_parent_own_orbit_dir_cleanup_sighandler(int signum)
432 struct sandbox_parent_own_orbit_dir_cleanup_signal *sigstructp;
435 sigstructp=sandbox_parent_own_orbit_dir_cleanup_signals;
436 sigstructp<sandbox_parent_own_orbit_dir_cleanup_signals+G_N_ELEMENTS(sandbox_parent_own_orbit_dir_cleanup_signals);
438 if (sigstructp->signum==signum)
441 g_assert(sigstructp<sandbox_parent_own_orbit_dir_cleanup_signals+G_N_ELEMENTS(sandbox_parent_own_orbit_dir_cleanup_signals));
442 signal(signum,sigstructp->sighandler_orig);
444 /* Prevent recursive fatal logging before signal restore: */
445 /* Do not: g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: signum=%d,sighandler_orig=%p",G_STRLOC,signum,sigstructp->sighandler_orig);
446 * as it is dangerous to g_log() from sighandler.
449 sandbox_parent_own_orbit_dir_cleanup_atexit();
451 /* Do not: g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: re-raising...",G_STRLOC);
452 * as it is dangerous to g_log() from sighandler.
457 static void sandbox_parent_own_orbit_dir_cleanup_init(void)
459 struct sandbox_parent_own_orbit_dir_cleanup_signal *sigstructp;
462 sigstructp=sandbox_parent_own_orbit_dir_cleanup_signals;
463 sigstructp<sandbox_parent_own_orbit_dir_cleanup_signals+G_N_ELEMENTS(sandbox_parent_own_orbit_dir_cleanup_signals);
465 sigstructp->sighandler_orig=signal(sigstructp->signum,sandbox_parent_own_orbit_dir_cleanup_sighandler);
466 if (sigstructp->sighandler_orig==SIG_IGN)
467 sigstructp->sighandler_orig=SIG_DFL;
471 static gchar *sandbox_parent_read_ior
472 (int Vfs_IOR_fd_read,gchar **child_chroot_pid_hashkey_dirp,CaptiveVfsParentObject *captive_vfs_parent_object)
477 gchar *ior,*child_chroot_pid_hashkey_dir;
482 /* Initialize /tmp/orbit-$username directory for linking IOR socket. */
483 errbool=corba_init("captive-sandbox-parent",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
484 g_assert(errbool==TRUE);
486 /* FIXME: Security: Duplicate giop_tmpdir_init() here. */
487 if (!sandbox_parent_own_orbit_dir) {
488 /* FIXME: Make 'CAPTIVE_SANDBOX_CHROOT' configurable. */
489 sandbox_parent_own_orbit_dir=g_strdup_printf("%s/tmp/captive-orbit-%d",CAPTIVE_SANDBOX_CHROOT,getpid());
490 if (mkdir(sandbox_parent_own_orbit_dir,0700)) {
491 g_assert(errno==EEXIST);
492 sandbox_parent_own_orbit_dir_cleanup_init();
493 unlink_nonrecursive(sandbox_parent_own_orbit_dir);
494 if (mkdir(sandbox_parent_own_orbit_dir,0700))
495 g_assert_not_reached();
497 sandbox_parent_own_orbit_dir_cleanup_init();
498 #ifdef HAVE_ORBIT_LINK
499 link_set_tmpdir(sandbox_parent_own_orbit_dir);
501 linc_set_tmpdir(sandbox_parent_own_orbit_dir);
505 data=captive_rtl_file_read(Vfs_IOR_fd_read,&data_size); /* data_fd_read */
506 errint=close(Vfs_IOR_fd_read);
508 g_assert(data!=NULL);
509 g_assert(data_size>=1);
510 g_assert(data[data_size-1]=='\n');
512 hash=g_hash_table_new(g_str_hash,g_str_equal);
514 for (s=data;s<data+data_size;s=se+1) {
521 g_hash_table_insert(hash,s,sd+1);
523 g_assert(s==data+data_size);
525 if ((child_chroot_pid_hashkey_dir=g_hash_table_lookup(hash,"chroot_pid_hashkey_dir"))) {
526 const gchar *socketname,*socketpathname_src,*socketpathname_dest;
527 const gchar *chrooted_orbit_dir;
529 /* IOR contains the full pathname with the setuid username encoded. */
530 chrooted_orbit_dir=g_hash_table_lookup(hash,"chrooted_orbit_dir");
531 g_assert(chrooted_orbit_dir!=NULL);
532 captive_vfs_parent_object->corba_chrooted_orbit_dir=g_strdup(chrooted_orbit_dir);
533 /* 0700 as this directory will not be reused
534 * to commuicate with any other sandbox child.
536 if (mkdir(chrooted_orbit_dir,0700)) {
537 g_assert(errno==EEXIST);
540 socketname=g_hash_table_lookup(hash,"socketname");
541 g_assert(socketname!=NULL);
542 captive_vfs_parent_object->corba_socketname=g_strdup(socketname);
544 socketpathname_src=captive_printf_alloca("%s/%s/%s",child_chroot_pid_hashkey_dir,chrooted_orbit_dir,socketname);
545 socketpathname_dest=g_strdup_printf("%s/%s",chrooted_orbit_dir,socketname);
546 errint=link(socketpathname_src,socketpathname_dest);
548 g_error("link(\"%s\",\"%s\")=%d:%m",socketpathname_src,socketpathname_dest,errint);
550 if (child_chroot_pid_hashkey_dirp)
551 *child_chroot_pid_hashkey_dirp=g_strdup(child_chroot_pid_hashkey_dir);
553 ior=g_hash_table_lookup(hash,"ior");
558 g_hash_table_destroy(hash);
564 static xmlNode *options_module_captive_to_xml
565 (xmlNode *dest_xml_parent,const struct captive_options_module *src_options_module_captive)
567 g_return_val_if_fail(dest_xml_parent!=NULL,NULL);
568 g_return_val_if_fail(src_options_module_captive!=NULL,NULL);
570 { xmlNode *module=xmlNewTextChild(dest_xml_parent,NULL,"module",NULL);
571 const gchar *type_string="???"; /* Prevent: ... might be used uninitialized in this function */
572 const gchar *basename,*cgs;
574 basename=src_options_module_captive->pathname_utf8;
575 if ((cgs=strrchr(basename,'/')))
577 xmlNewProp(module,"basename",basename);
578 switch (src_options_module_captive->type) {
579 case CAPTIVE_OPTIONS_MODULE_TYPE_PE32:
581 xmlNewProp(module,"length",captive_printf_alloca("%lu",(unsigned long)src_options_module_captive->u.pe32.length));
582 xmlNewProp(module,"md5" ,src_options_module_captive->u.pe32.md5);
584 case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
585 type_string="gmodule";
587 default: g_assert_not_reached();
589 xmlNewProp(module,"type",type_string); /* AFTER the 'switch' to set 'type_string'! */
595 static void sandbox_parent_bug_doc_make(CaptiveVfsParentObject *captive_vfs_parent_object)
597 { xmlDoc *doc=xmlNewDoc("1.0");
598 captive_vfs_parent_object->corba_bug_doc=doc;
599 { xmlNode *bug=xmlNewDocNode(captive_vfs_parent_object->corba_bug_doc,NULL,"bug",NULL);
600 xmlDocSetRootElement(captive_vfs_parent_object->corba_bug_doc,bug);
601 captive_vfs_parent_object->corba_bug=bug;
602 { xmlNode *bug_captive=xmlNewTextChild(bug,NULL,"captive",NULL);
603 xmlNewProp(bug_captive,"version",VERSION);
605 { xmlNode *bug_filesystem=xmlNewTextChild(bug,NULL,"filesystem",NULL);
606 options_module_captive_to_xml(bug_filesystem,&CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.filesystem);
608 { xmlNode *bug_load_module=xmlNewTextChild(bug,NULL,"load_module",NULL);
610 struct captive_options_module *options_module;
611 GList *load_module_node;
613 for (load_moduleui=0,load_module_node=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.load_module;
615 load_moduleui++,load_module_node=load_module_node->next) {
616 options_module=load_module_node->data;
617 options_module_captive_to_xml(bug_load_module,options_module);
620 { xmlNode *bug_action=xmlNewTextChild(bug,NULL,"action",NULL);
621 captive_vfs_parent_object->corba_bug_action=bug_action;
623 { xmlNode *bug_log=xmlNewTextChild(bug,NULL,"log",NULL);
624 captive_vfs_parent_object->corba_bug_log=bug_log;
631 static void sandbox_parent(const gchar *Vfs_IOR,const gchar *child_chroot_pid_hashkey_dir,CaptiveVfsParentObject *captive_vfs_parent_object)
633 Captive_Vfs Vfs_object;
634 Captive_GLogFunc GLogFunc_object;
635 Captive_CaptiveIOChannel CaptiveIOChannel_object;
636 Captive_CaptiveOptions options_corba;
637 guint load_module_length,load_moduleui;
638 struct captive_options_module *options_module;
639 GList *load_module_node;
640 const gchar *child_chroot_parent_own_orbit_socket,*child_chroot_parent_own_orbit_dir;
643 g_return_if_fail(Vfs_IOR!=NULL);
644 /* 'child_chroot_pid_hashkey_dir' may be NULL */
645 g_return_if_fail(CAPTIVE_VFS_PARENT_IS_OBJECT(captive_vfs_parent_object));
647 Vfs_object=CORBA_ORB_string_to_object(captive_corba_orb,Vfs_IOR,&captive_corba_ev);
648 g_assert(validate_CORBA_Environment(&captive_corba_ev));
650 /* Init 'GLogFunc_object' */
651 GLogFunc_object=impl_Captive_GLogFunc__create(captive_corba_poa,captive_vfs_parent_object,&captive_corba_ev);
652 g_assert(validate_CORBA_Environment(&captive_corba_ev));
654 /* Init 'CaptiveIOChannel_object' */
655 if (!captive_vfs_parent_object->corba_parent_giochanel_blind_source)
656 switch (CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.rwmode) {
657 case CAPTIVE_OPTION_RWMODE_RO:
658 case CAPTIVE_OPTION_RWMODE_RW:
659 captive_vfs_parent_object->corba_parent_giochanel_blind_source=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.image_iochannel;
661 case CAPTIVE_OPTION_RWMODE_BLIND:
662 captive_vfs_parent_object->corba_parent_giochanel_blind_source=(GIOChannel *)captive_giochannel_blind_new(
663 CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.image_iochannel, /* giochannel_orig */
664 TRUE); /* writeable */
666 default: g_assert_not_reached();
668 if (!captive_vfs_parent_object->corba_parent_giochanel_blind)
669 captive_vfs_parent_object->corba_parent_giochanel_blind=(GIOChannel *)captive_giochannel_blind_new(
670 captive_vfs_parent_object->corba_parent_giochanel_blind_source, /* giochannel_orig */
671 (CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.rwmode!=CAPTIVE_OPTION_RWMODE_RO)); /* writeable */
672 CaptiveIOChannel_object=impl_Captive_CaptiveIOChannel__create(captive_corba_poa,
673 captive_vfs_parent_object->corba_parent_giochanel_blind,&captive_corba_ev);
674 g_assert(validate_CORBA_Environment(&captive_corba_ev));
676 /* Create the socket needed for 'sandbox_parent_own_orbit_socket' below
677 * by CORBA_ORB_object_to_string()->ORBit_marshal_object()->IOP_generate_profiles()->ORBit_ORB_start_servers...
681 GLogFunc_IOR=CORBA_ORB_object_to_string(captive_corba_orb,GLogFunc_object,&captive_corba_ev);
682 g_assert(validate_CORBA_Environment(&captive_corba_ev));
683 g_assert(GLogFunc_IOR!=NULL);
684 CORBA_free(GLogFunc_IOR);
687 if (sandbox_parent_own_orbit_dir && !sandbox_parent_own_orbit_socket) {
689 struct dirent *dirent;
691 dir=opendir(sandbox_parent_own_orbit_dir);
694 while (errno=0,(dirent=readdir(dir))) {
695 if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
697 g_assert(sandbox_parent_own_orbit_socket==NULL);
698 sandbox_parent_own_orbit_socket=g_strdup_printf("%s/%s",sandbox_parent_own_orbit_dir,dirent->d_name);
701 errint=closedir(dir);
703 g_assert(sandbox_parent_own_orbit_socket!=NULL);
706 if (child_chroot_pid_hashkey_dir) {
709 child_chroot_parent_own_orbit_dir=captive_printf_alloca("%s/%s",child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_dir);
710 s=(/* de-const */ gchar *)child_chroot_parent_own_orbit_dir;
715 if (*child_chroot_parent_own_orbit_dir) {
716 errint=mkdir(child_chroot_parent_own_orbit_dir,0777);
718 g_assert(errno==EEXIST);
720 /* chmod(2) it to prevent mode limitation by
721 * active ulimit(2) of being executed by mount(8).
723 errint=chmod(child_chroot_parent_own_orbit_dir,0777);
730 child_chroot_parent_own_orbit_socket=captive_printf_alloca("%s/%s",
731 child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_socket);
732 errint=link(sandbox_parent_own_orbit_socket,child_chroot_parent_own_orbit_socket);
734 /* chmod(2)s also our orig. one (!) but this one is protected by its 0777 directory. */
735 errint=chmod(child_chroot_parent_own_orbit_socket,0666);
739 options_corba.g_log_func=CORBA_Object_duplicate(GLogFunc_object,&captive_corba_ev);
740 g_assert(validate_CORBA_Environment(&captive_corba_ev));
742 options_module_captive_to_options_module_corba(&options_corba.filesystem,&CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.filesystem);
743 /* Prevent secondary captive_giochannel_blind inside of our sandbox child
744 * as we already have one captive_giochannel_blind in the parent.
746 options_corba.rwmode =(CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.rwmode == CAPTIVE_OPTION_RWMODE_BLIND ? CAPTIVE_OPTION_RWMODE_RW
747 : CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.rwmode);
748 options_corba.media =CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.media;
749 options_corba.debug_messages=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.debug_messages;
751 options_corba.image_iochannel=CORBA_Object_duplicate(CaptiveIOChannel_object,&captive_corba_ev);
752 g_assert(validate_CORBA_Environment(&captive_corba_ev));
754 load_module_length=g_list_length(CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.load_module);
755 options_corba.load_module._maximum=load_module_length;
756 options_corba.load_module._length=load_module_length;
757 options_corba.load_module._buffer=Captive_CaptiveOptionsModuleList_allocbuf(options_corba.load_module._maximum);
758 options_corba.load_module._release=TRUE;
759 for (load_moduleui=0,load_module_node=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.load_module;
761 load_moduleui++,load_module_node=load_module_node->next) {
762 options_module=load_module_node->data;
763 options_module_captive_to_options_module_corba(options_corba.load_module._buffer+load_moduleui,options_module);
766 Captive_Vfs_init(Vfs_object,&options_corba,&captive_corba_ev);
767 g_assert(validate_CORBA_Environment(&captive_corba_ev));
769 Captive_CaptiveOptions__freekids(&options_corba,
770 NULL); /* d; unused */
772 captive_vfs_parent_object->corba_Vfs_object=Vfs_object;
773 captive_vfs_parent_object->corba_GLogFunc_object=GLogFunc_object;
774 captive_vfs_parent_object->corba_CaptiveIOChannel_object=CaptiveIOChannel_object;
776 if (CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.bug_pathname)
777 sandbox_parent_bug_doc_make(captive_vfs_parent_object);
780 static void fd_shiftup(int *fdp)
782 g_return_if_fail(fdp!=NULL);
784 while (*fdp<=2 /* STDERR */) {
790 void captive_sandbox_fd_closeup(int fd_first_to_delete)
795 struct dirent *dirent;
797 dir=opendir("/proc/self/fd/");
798 g_return_if_fail(dir!=NULL);
800 g_return_if_fail(dir_fd!=-1);
802 while (errno=0,(dirent=readdir(dir))) {
807 || !strcmp(dirent->d_name,".")
808 || !strcmp(dirent->d_name,".."))
810 dirent_fd=strtol(dirent->d_name,&endptr,10 /* base */);
811 g_assert(dirent_fd>=0 && (!endptr || !*endptr));
812 if (dirent_fd<fd_first_to_delete || dirent_fd==dir_fd)
815 errint=close(dirent_fd);
818 errint=close(dirent_fd);
819 g_assert(errint==-1); g_assert(errno==EBADF);
821 g_return_if_fail(errno==0); /* check for EOF */
823 errint=closedir(dir);
824 g_return_if_fail(errint==0);
826 close(dir_fd); g_assert(errno==EBADF); /* just a bit of paranoia; it should be already closed by closedir() */
829 gboolean captive_sandbox_spawn(CaptiveVfsParentObject *captive_vfs_parent_object)
831 /* Vfs_IOR_fds[0] for reading by sandbox_parent() - client,
832 * Vfs_IOR_fds[1] for writing by sandbox_child() - server
834 int Vfs_IOR_fds[2],parentheart_fds[2];
838 g_return_val_if_fail(CAPTIVE_VFS_PARENT_IS_OBJECT(captive_vfs_parent_object),FALSE);
840 errint=pipe(Vfs_IOR_fds);
841 g_return_val_if_fail(errint==0,FALSE);
842 errint=pipe(parentheart_fds);
843 g_return_val_if_fail(errint==0,FALSE);
845 /* Currently never called anywhere:
846 * errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
847 * g_assert(errbool==TRUE);
850 if (CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_ior) {
851 g_assert(CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_argv==NULL);
853 errbool=corba_init("captive-sandbox-parent",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
854 g_assert(errbool==TRUE);
856 captive_vfs_parent_object->corba_parentheart_fds_1=-1;
857 captive_vfs_parent_object->corba_child_pid=-1;
859 CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_ior, /* Vfs_IOR */
860 NULL, /* child_chroot_pid_hashkey_dir */
861 captive_vfs_parent_object); /* captive_vfs_parent_object */
865 g_assert(CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_argv!=NULL);
866 switch ((captive_vfs_parent_object->corba_child_pid=fork())) {
868 g_return_val_if_reached(FALSE);
870 case 0: { /* child */
872 errint=close(Vfs_IOR_fds[0]); /* close Vfs_IOR_fd_read */
873 g_return_val_if_fail(errint==0,FALSE);
874 errint=close(parentheart_fds[1]); /* close parentheart_fd_write */
875 g_return_val_if_fail(errint==0,FALSE);
877 fd_shiftup(Vfs_IOR_fds+1); /* Vfs_IOR_fd_write */
878 fd_shiftup(parentheart_fds+0); /* parentheart_fd_read */
879 errint=dup2(Vfs_IOR_fds[1],1 /* STDOUT */); /* Vfs_IOR_fd_write */
880 g_return_val_if_fail(errint==1 /* STDOUT */,FALSE);
881 errint=dup2(parentheart_fds[0],0 /* STDIN */); /* parentheart_fd_read */
882 g_return_val_if_fail(errint==0 /* STDIN */,FALSE);
884 captive_sandbox_fd_closeup(2 /* STDERR */ +1);
886 execv(CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_argv[0],
887 /* re-const */ (char * const *)CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_argv);
888 g_return_val_if_reached(FALSE);
891 default: { /* parent */
893 gchar *child_chroot_pid_hashkey_dir;
895 errint=close(Vfs_IOR_fds[1]); /* close Vfs_IOR_fd_write */
896 g_return_val_if_fail(errint==0,FALSE);
897 errint=close(parentheart_fds[0]); /* close parentheart_fd_read */
898 g_return_val_if_fail(errint==0,FALSE);
899 errint=fcntl(parentheart_fds[1],F_SETFD,FD_CLOEXEC);
900 /* This fcntl(2) may not be enough - some fork(2) may duplicate this
901 * write descriptor and even if we do some process finish the child
902 * may remain alone connected with some unknown fork(2)er from us.
903 * Currently I am not aware such case would occur.
905 g_return_val_if_fail(errint==0,FALSE);
906 captive_vfs_parent_object->corba_parentheart_fds_1=parentheart_fds[1];
908 Vfs_IOR=sandbox_parent_read_ior(
909 Vfs_IOR_fds[0], /* Vfs_IOR_fd_read */
910 &child_chroot_pid_hashkey_dir,
911 captive_vfs_parent_object);
914 Vfs_IOR, /* Vfs_IOR */
915 child_chroot_pid_hashkey_dir, /* child_chroot_pid_hashkey_dir */
916 captive_vfs_parent_object); /* captive_vfs_parent_object */
919 g_free(child_chroot_pid_hashkey_dir);
921 /* 'parentheart_fds[1]' - parentheart_fd_write - is left open here */
926 g_return_val_if_reached(FALSE);
930 GnomeVFSResult captive_sandbox_parent_return_from_CORBA_Environment
931 (CORBA_Environment *evp,CaptiveVfsParentObject *captive_vfs_parent_object)
935 if (evp->_major==CORBA_NO_EXCEPTION)
938 /* If !CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_argv it is CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_ior
939 * where we cannot do any restart anyway.
941 if (captive_vfs_parent_object && CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_argv
942 && (evp->_major==CORBA_SYSTEM_EXCEPTION && !strcmp(ex_CORBA_COMM_FAILURE,CORBA_exception_id(evp)))) {
943 r=GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE;
944 if (captive_vfs_parent_object->corba_Vfs_object!=CORBA_OBJECT_NIL)
945 captive_vfs_parent_object_aborted(captive_vfs_parent_object); /* errors ignored */
947 else if (evp->_major==CORBA_USER_EXCEPTION && !strcmp(ex_Captive_GnomeVFSResultException,CORBA_exception_id(evp))) {
948 r=((Captive_GnomeVFSResultException *)CORBA_exception_value(evp))->gnome_vfs_result;
949 g_assert(r!=GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE); /* code reserved for sandbox restarts */
952 r=GNOME_VFS_ERROR_GENERIC;
953 g_warning(_("CORBA Exception occured: id=\"%s\", value=%p"),
954 CORBA_exception_id(evp),CORBA_exception_value(evp));
956 CORBA_exception_free(evp);
962 void captive_sandbox_child_GnomeVFSResultException_throw(CORBA_Environment *evp,GnomeVFSResult errvfsresult)
964 Captive_GnomeVFSResultException *gnome_vfs_result_exception;
966 g_return_if_fail(evp!=NULL);
968 gnome_vfs_result_exception=Captive_GnomeVFSResultException__alloc();
969 gnome_vfs_result_exception->gnome_vfs_result=errvfsresult;
970 CORBA_exception_set(evp,CORBA_USER_EXCEPTION,ex_Captive_GnomeVFSResultException,gnome_vfs_result_exception);