Better error message during failed spawn of captive-sandbox-server(1).
[captive.git] / src / libcaptive / sandbox / split.c
index f9da17a..10ce69f 100644 (file)
@@ -37,6 +37,8 @@
 #include <errno.h>
 #include "../client/giochannel-blind.h"        /* for captive_giochannel_blind_new() */
 #include <signal.h>
+#include "../storage/relastblock.h"    /* for captive_storage_relastblock() */
+#include <sys/file.h>  /* flock() */
 
 #ifdef HAVE_ORBIT_LINK
 void link_set_tmpdir(const char *dir);
@@ -468,6 +470,42 @@ struct sandbox_parent_own_orbit_dir_cleanup_signal *sigstructp;
                }
 }
 
+static void createlockdir(const gchar *dir)
+{
+gint retries;
+
+       for (retries=0;retries<10;retries++) {
+struct stat statbuf;
+int dirfd;
+
+               if (mkdir(dir,0700)) {
+                       if (errno!=EEXIST)
+                               g_error(_("Failed to create chroot directory \"%s\": %m"),dir);
+                       unlink_nonrecursive(dir);
+                       if (mkdir(dir,0700))
+                               g_error(_("Failed to create chroot directory \"%s\" after attempted unlink: %m"),dir);
+                       }
+               dirfd=open(dir,O_RDONLY);
+               if (dirfd==-1) {
+                       if (errno!=ENOENT)
+                               g_error(_("Failed to open created chroot directory \"%s\" to lock it: %m"),dir);
+                       continue;
+                       }
+               /* Do not use 'LOCK_NB' here as the garbage collector should release it soon. */
+               if (flock(dirfd,LOCK_EX))
+                       g_error(_("Failed to lock created chroot directory \"%s\": %m"),dir);
+               if (lstat(dir,&statbuf)) {
+                       if (errno!=ENOENT)
+                               g_error(_("Failed to lstat(2) created chroot directory \"%s\": %m"),dir);
+                       if (close(dirfd))
+                               g_error(_("Failed to close created and locked chroot directory \"%s\": %m"),dir);
+                       continue;
+                       }
+               /* Leave 'dirfd' open to leave it LOCK_EX-ed. */
+               break;
+               }
+}
+
 static gchar *sandbox_parent_read_ior
                (int Vfs_IOR_fd_read,gchar **child_chroot_pid_hashkey_dirp,CaptiveVfsParentObject *captive_vfs_parent_object)
 {
@@ -478,6 +516,7 @@ gchar *ior,*child_chroot_pid_hashkey_dir;
 int errint;
 gchar *s,*sd,*se;
 gboolean errbool;
+const gchar *ior_read_error;
 
        /* Initialize /tmp/orbit-$username directory for linking IOR socket. */
        errbool=corba_init("captive-sandbox-parent",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
@@ -486,15 +525,12 @@ gboolean errbool;
        /* FIXME: Security: Duplicate giop_tmpdir_init() here. */
        if (!sandbox_parent_own_orbit_dir) {
                /* 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();
-                       unlink_nonrecursive(sandbox_parent_own_orbit_dir);
-                       if (mkdir(sandbox_parent_own_orbit_dir,0700))
-                               g_assert_not_reached();
-                       }
+               /* Use also geteuid() marker here as could not remove the directory
+                * of some other user otherwise.
+                */
+               sandbox_parent_own_orbit_dir=g_strdup_printf("%s/tmp/captive-orbit-%d-%d",CAPTIVE_SANDBOX_CHROOT,getpid(),geteuid());
                sandbox_parent_own_orbit_dir_cleanup_init();
+               createlockdir(sandbox_parent_own_orbit_dir);
 #ifdef HAVE_ORBIT_LINK
                link_set_tmpdir(sandbox_parent_own_orbit_dir);
 #else
@@ -504,10 +540,19 @@ gboolean errbool;
 
        data=captive_rtl_file_read(Vfs_IOR_fd_read,&data_size); /* data_fd_read */
        errint=close(Vfs_IOR_fd_read);
-       g_assert(errint==0);
-       g_assert(data!=NULL);
-       g_assert(data_size>=1);
-       g_assert(data[data_size-1]=='\n');
+       ior_read_error=NULL;
+#define IOR_READ_ERROR(cond) G_STMT_START { \
+               if (!ior_read_error && !(cond)) \
+                       ior_read_error=G_STRINGIFY(cond); \
+               } G_STMT_END
+       IOR_READ_ERROR(errint==0);
+       IOR_READ_ERROR(data!=NULL);
+       IOR_READ_ERROR(data_size>=1);
+       IOR_READ_ERROR(data[data_size-1]=='\n');
+       if (ior_read_error)
+               g_error(_("Error connecting to the sandbox server (failed \"%s\"). Try running by hand: %s"),
+                               ior_read_error,CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_argv[0]);
+#undef IOR_READ_ERROR
 
        hash=g_hash_table_new(g_str_hash,g_str_equal);
 
@@ -652,19 +697,32 @@ int errint;
        g_assert(validate_CORBA_Environment(&captive_corba_ev));
 
        /* Init 'CaptiveIOChannel_object' */
-       if (!captive_vfs_parent_object->corba_parent_giochanel_blind_source)
+       if (!captive_vfs_parent_object->corba_parent_giochanel_blind_source) {
+GIOChannel *giochannel;
+
+               giochannel=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.image_iochannel;
+               g_io_channel_ref(giochannel);
+
+               giochannel=captive_storage_relastblock(giochannel);
+
                switch (CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.rwmode) {
                        case CAPTIVE_OPTION_RWMODE_RO:
                        case CAPTIVE_OPTION_RWMODE_RW:
-                               captive_vfs_parent_object->corba_parent_giochanel_blind_source=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.image_iochannel;
                                break;
-                       case CAPTIVE_OPTION_RWMODE_BLIND:
-                               captive_vfs_parent_object->corba_parent_giochanel_blind_source=(GIOChannel *)captive_giochannel_blind_new(
-                                               CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.image_iochannel, /* giochannel_orig */
+                       case CAPTIVE_OPTION_RWMODE_BLIND: {
+GIOChannel *giochannel_orig;
+
+                               giochannel_orig=giochannel;
+                               giochannel=(GIOChannel *)captive_giochannel_blind_new(
+                                               giochannel,     /* giochannel_orig */
                                                TRUE); /* writeable */
-                               break;
+                               g_io_channel_unref(giochannel_orig);    /* reffed by captive_giochannel_blind_new() */
+                               } break;
                        default: g_assert_not_reached();
                        }
+               captive_vfs_parent_object->corba_parent_giochanel_blind_source=giochannel;
+               }
+
        if (!captive_vfs_parent_object->corba_parent_giochanel_blind)
                captive_vfs_parent_object->corba_parent_giochanel_blind=(GIOChannel *)captive_giochannel_blind_new(
                                captive_vfs_parent_object->corba_parent_giochanel_blind_source,  /* giochannel_orig */
@@ -868,6 +926,8 @@ gboolean errbool;
                        g_return_val_if_reached(FALSE);
 
                case 0: { /* child */
+const char *sandbox_argv0;
+int execv_rc;
 
                        errint=close(Vfs_IOR_fds[0]);   /* close Vfs_IOR_fd_read */
                        g_return_val_if_fail(errint==0,FALSE);
@@ -883,8 +943,10 @@ gboolean errbool;
 
                        captive_sandbox_fd_closeup(2 /* STDERR */ +1);
 
-                       execv(CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_argv[0],
+                       sandbox_argv0=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_argv[0];
+                       execv_rc=execv(sandbox_argv0,
                                        /* re-const */ (char * const *)CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.sandbox_server_argv);
+                       g_warning(_("Failed to spawn sandbox server - execv(\"%s\",...)=%d: %m"),sandbox_argv0,execv_rc);
                        g_return_val_if_reached(FALSE);
                        } /* NOTREACHED */