+Debug logging of sandbox_parent_own_orbit_dir_cleanup_atexit() execution.
[captive.git] / src / libcaptive / sandbox / split.c
index c2fdd07..3a42c02 100644 (file)
@@ -76,7 +76,7 @@ gchar *orb_argv[]={
        g_return_val_if_fail(poap!=NULL,FALSE);
 
        if (done)
-               return TRUE;
+               return TRUE;    /* FIXME: '*poap' is left invalid! */
 
        /* Init 'ev' */
        CORBA_exception_init(evp);
@@ -373,6 +373,7 @@ static void sandbox_parent_own_orbit_dir_cleanup_atexit(void)
 static gboolean done=FALSE;
 
        g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s; done=%d",G_STRLOC,(int)done);
+
        if (done)
                return;
        done=TRUE;
@@ -408,11 +409,15 @@ struct sandbox_parent_own_orbit_dir_cleanup_signal *sigstructp;
        signal(signum,sigstructp->sighandler_orig);
 
        /* Prevent recursive fatal logging before signal restore: */
-       g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: signum=%d,sighandler_orig=%p",G_STRLOC,signum,sigstructp->sighandler_orig);
+       /* Do not: g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: signum=%d,sighandler_orig=%p",G_STRLOC,signum,sigstructp->sighandler_orig);
+        * as it is dangerous to g_log() from sighandler.
+        */
 
        sandbox_parent_own_orbit_dir_cleanup_atexit();
 
-       g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: re-raising...",G_STRLOC);
+       /* Do not: g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: re-raising...",G_STRLOC);
+        * as it is dangerous to g_log() from sighandler.
+        */
        raise(signum);
 }
 
@@ -430,7 +435,8 @@ struct sandbox_parent_own_orbit_dir_cleanup_signal *sigstructp;
                }
 }
 
-gchar *sandbox_parent_read_ior(int Vfs_IOR_fd_read,gchar **child_chroot_pid_hashkey_dirp)
+static gchar *sandbox_parent_read_ior
+               (int Vfs_IOR_fd_read,gchar **child_chroot_pid_hashkey_dirp,CaptiveVfsParentObject *captive_vfs_parent_object)
 {
 gchar *data;
 gsize data_size;
@@ -446,7 +452,8 @@ gboolean errbool;
 
        /* FIXME: Security: Duplicate giop_tmpdir_init() here. */
        if (!sandbox_parent_own_orbit_dir) {
-               sandbox_parent_own_orbit_dir=g_strdup_printf("/tmp/captive-orbit-%d",getpid());
+               /* FIXME: Make 'CAPTIVE_SANDBOX_CHROOT' configurable. */
+               sandbox_parent_own_orbit_dir=g_strdup_printf("%s/tmp/captive-orbit-%d",CAPTIVE_SANDBOX_CHROOT,getpid());
                if (mkdir(sandbox_parent_own_orbit_dir,0700)) {
                        g_assert(errno==EEXIST);
                        sandbox_parent_own_orbit_dir_cleanup_init();
@@ -485,17 +492,23 @@ const gchar *chrooted_orbit_dir;
                /* IOR contains the full pathname with the setuid username encoded. */
                chrooted_orbit_dir=g_hash_table_lookup(hash,"chrooted_orbit_dir");
                g_assert(chrooted_orbit_dir!=NULL);
-               if (mkdir(chrooted_orbit_dir,S_ISVTX|0777)) {
+               captive_vfs_parent_object->corba_chrooted_orbit_dir=g_strdup(chrooted_orbit_dir);
+               /* 0700 as this directory will not be reused
+                * to commuicate with any other sandbox child.
+                */
+               if (mkdir(chrooted_orbit_dir,0700)) {
                        g_assert(errno==EEXIST);
                        }
 
                socketname=g_hash_table_lookup(hash,"socketname");
                g_assert(socketname!=NULL);
+               captive_vfs_parent_object->corba_socketname=g_strdup(socketname);
 
                socketpathname_src=captive_printf_alloca("%s/%s/%s",child_chroot_pid_hashkey_dir,chrooted_orbit_dir,socketname);
-               socketpathname_dest=captive_printf_alloca("%s/%s",chrooted_orbit_dir,socketname);
+               socketpathname_dest=g_strdup_printf("%s/%s",chrooted_orbit_dir,socketname);
                errint=link(socketpathname_src,socketpathname_dest);
-               g_assert(errint==0);
+               if (errint)
+                       g_error("link(\"%s\",\"%s\")=%d:%m",socketpathname_src,socketpathname_dest,errint);
                }
        if (child_chroot_pid_hashkey_dirp)
                *child_chroot_pid_hashkey_dirp=g_strdup(child_chroot_pid_hashkey_dir);
@@ -654,14 +667,29 @@ struct dirent *dirent;
                }
 
        if (child_chroot_pid_hashkey_dir) {
+gchar *s;
+
                child_chroot_parent_own_orbit_dir=captive_printf_alloca("%s/%s",child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_dir);
-               errint=mkdir(child_chroot_parent_own_orbit_dir,0777);
-               g_assert(errint==0);
-               /* chmod(2) it to prevent mode limitation by
-                * active ulimit(2) of being executed by mount(8).
-                */
-               errint=chmod(child_chroot_parent_own_orbit_dir,0777);
-               g_assert(errint==0);
+               s=(/* de-const */ gchar *)child_chroot_parent_own_orbit_dir;
+               do {
+                       s=strchr(s,'/');
+                       if (s)
+                               *s=0;
+                       if (*child_chroot_parent_own_orbit_dir) {
+                               errint=mkdir(child_chroot_parent_own_orbit_dir,0777);
+                               if (errint)
+                                       g_assert(errno==EEXIST);
+                               else {
+                                       /* chmod(2) it to prevent mode limitation by
+                                        * active ulimit(2) of being executed by mount(8).
+                                        */
+                                       errint=chmod(child_chroot_parent_own_orbit_dir,0777);
+                                       g_assert(errint==0);
+                                       }
+                               }
+                       if (s)
+                               *s++='/';
+                       } while (s);
                child_chroot_parent_own_orbit_socket=captive_printf_alloca("%s/%s",
                                child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_socket);
                errint=link(sandbox_parent_own_orbit_socket,child_chroot_parent_own_orbit_socket);
@@ -837,7 +865,8 @@ gchar *child_chroot_pid_hashkey_dir;
 
                        Vfs_IOR=sandbox_parent_read_ior(
                                        Vfs_IOR_fds[0], /* Vfs_IOR_fd_read */
-                                       &child_chroot_pid_hashkey_dir);
+                                       &child_chroot_pid_hashkey_dir,
+                                       captive_vfs_parent_object);
 
                        sandbox_parent(
                                        Vfs_IOR,        /* Vfs_IOR */