include <string.h>
[util-vserver.git] / util-vserver / src / rpm-fake.c
index f53a9db..f6c9770 100644 (file)
 #endif
 
 #include "pathconfig.h"
-#include "sys_clone.h"
 #include "util.h"
 
-#include "lib/vserver.h"
-#include "lib/internal.h"
+#include <lib/vserver.h>
+#include <lib/internal.h>
+#include <lib_internal/sys_clone.h>
 
 #include <sys/socket.h>
 #include <dlfcn.h>
 #include <pwd.h>
 #include <grp.h>
 
+
+  // from selinux.h
+  // FIXME: add configure autodetection and include <selinux.h> directly
+int rpm_execcon(unsigned int verified,
+               const char *filename,
+               char *const argv[], char *const envp[]);
+
+
 #define ENSC_WRAPPERS_PREFIX   "rpm-fake.so: "
 #define ENSC_WRAPPERS_VSERVER  1
 #define ENSC_WRAPPERS_UNISTD   1
@@ -74,7 +82,8 @@
 #define DBG_VERBOSE1   (0x4000 | DBG_VERBOSE0)
 #define DBG_VERBOSE2   (0x2000 | DBG_VERBOSE1)
 
-static char const *    ctx_s = 0;
+int                    wrapper_exit_code = 255;
+
 static xid_t           ctx   = VC_NOCTX;
 static uint32_t                caps  = ~0;
 static int             flags = 0;
@@ -86,7 +95,8 @@ static unsigned int   debug_level = 0;
 
 static bool            is_initialized = false;
 
-DECLARE(execv);
+  //DECLARE(rpm_execcon);
+  //DECLARE(execv);
 DECLARE(getpwnam);
 DECLARE(getgrnam);
 DECLARE(endpwent);
@@ -169,31 +179,56 @@ getAndClearEnv(char const *key, int dflt)
   return res;
 }
 
-static void
-setupContext(xid_t xid)
+  /// \returns true iff we are in ctx after leaving this function
+static bool
+setupContext(xid_t xid, char const **xid_str)
 {
+  bool         res = false;
   
   if (vc_isSupported(vcFEATURE_MIGRATE)) {
-    xid_t      rc = vc_create_context(xid);
-    if (rc==VC_NOCTX && errno!=EEXIST) {
-      perror("rpm-fake.so: vc_create_context()");
+    xid_t      rc=VC_NOCTX;
+
+    if ((xid==VC_DYNAMIC_XID || !vc_is_dynamic_xid(xid)) &&
+       (rc=vc_ctx_create(xid))==VC_NOCTX &&
+       errno!=EEXIST) {
+      perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
       exit(255);
     }
 
     if (rc!=VC_NOCTX) {
-      char             buf[128];
-      size_t           l;
+      char                     buf[sizeof(xid_t)*3 + 128];
+      size_t                   l;
+      struct vc_ctx_caps       caps;
+      
       strcpy(buf, "rpm-fake.so #");
       l = utilvserver_fmt_uint(buf+sizeof("rpm-fake.so #")-1, getppid());
       Evc_set_vhi_name(rc, vcVHI_CONTEXT, buf, sizeof("rpm-fake.so #")+l-1);
-#warning set some sane capabilities
+
+      caps.ccaps =  0ull;
+      caps.cmask = ~0ull;
+      caps.bcaps = ~vc_get_insecurebcaps();
+      caps.bmask = ~0ull;
+      Evc_set_ccaps(rc, &caps);
+      
        // context will be activated later...
 
       xid = rc;
+      res = true;
     }
   }
 
+  if (xid==VC_DYNAMIC_XID)
+    *xid_str = 0;
+  else {
+    char               buf[sizeof(xid_t)*3 + 2];
+    size_t             l;
+    
+    l        = utilvserver_fmt_uint(buf, xid); buf[l] = '\0';
+    *xid_str = strdup(buf);
+  }
   Ewrite(3, &xid, sizeof xid);
+  return res;
 }
 
 #if 0
@@ -214,7 +249,7 @@ initPwSocket()
        connect(pw_sock, (struct sockaddr *)(&addr), sizeof addr)==-1 ||
        (flag=fcntl(pw_sock, F_GETFD))==-1 ||
        fcntl(pw_sock, F_SETFD, flag | FD_CLOEXEC)==-1) {
-      perror("rpm-fake.so: error while initializing pw-socket");
+      perror(ENSC_WRAPPERS_PREFIX "error while initializing pw-socket");
       exit(255);
     }
   }
@@ -241,20 +276,23 @@ initPwSocket()
        pipe(sync_pipe)==-1 ||
        fcntl(res_sock[0],  F_SETFD, FD_CLOEXEC)==-1 ||
        fcntl(sync_pipe[0], F_SETFD, FD_CLOEXEC)==-1) {
-      perror("rpm-fake.so: failed to create/initialize resolver-socket or pipe");
+      perror(ENSC_WRAPPERS_PREFIX "failed to create/initialize resolver-socket or pipe");
       exit(255);
     }
 
     pid = fork();
     if (pid==-1) {
-      perror("rpm-fake.so: fork()");
+      perror(ENSC_WRAPPERS_PREFIX "fork()");
       exit(255);
     }
 
     if (pid==0) {
-      char const       *args[10];
+      char const       *args[20];
       char const       **ptr  = args;
       char const       *env[] = { "HOME=/", "PATH=/bin:/usr/bin", 0 };
+      char const       *xid_str;
+      char             flag_str[ sizeof(flags)*3 + 2];
+      char             caps_str[ sizeof(caps)*3  + 2];
       
       setsid();
       dup2(res_sock[1],  0);
@@ -267,16 +305,22 @@ initPwSocket()
       close(res_sock[1]);
        /* ... *socket[0] are marked as close-on-exec ...*/
 
+      flag_str[utilvserver_fmt_uint(flag_str, flags)] = '\0';
+      caps_str[utilvserver_fmt_uint(caps_str, caps)]  = '\0';
+
       *ptr++ = resolver;
+      *ptr++ = "-F"; *ptr++ = flag_str;
+      *ptr++ = "-C"; *ptr++ = caps_str;
       if (root)  { *ptr++ = "-r"; *ptr++ = root;  }
       if (uid)   { *ptr++ = "-u"; *ptr++ = uid;   }
       if (gid)   { *ptr++ = "-g"; *ptr++ = gid;   }
-      if (ctx_s) { *ptr++ = "-c"; *ptr++ = ctx_s; }
-      *ptr++ = 0;
 
-      setupContext(ctx);
+      if (setupContext(ctx, &xid_str)) { *ptr++ = "-s"; }
+      else if (xid_str)                { *ptr++ = "-c"; *ptr++ = xid_str; }
+      
+      *ptr++ = 0;
       execve(resolver, (char **)args, (char **)env);
-      perror("rpm-fake.so: failed to exec resolver");
+      perror(ENSC_WRAPPERS_PREFIX "failed to exec resolver");
       exit(255);
     }
     else {
@@ -292,17 +336,16 @@ initPwSocket()
          write(pw_sock, ".", 1)!=1 ||
          read(pw_sock, &c,   1)!=1 ||
          c!='.') {
-       WRITE_MSG(2, "rpm-fake.so: failed to initialize communication with resolver\n");
+       WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "failed to initialize communication with resolver\n");
        exit(255);
       }
 
       if (wait4(pid, 0, WNOHANG,0)==-1) {
-       WRITE_MSG(2, "rpm-fake.so: unexpected initialization-error of resolver\n");
+       WRITE_MSG(2, ENSC_WRAPPERS_PREFIX" unexpected initialization-error of resolver\n");
        exit(255);
       }
     }
 
-    free((char *)ctx_s);
     unsetenv("RPM_FAKE_RESOLVER_GID");
     unsetenv("RPM_FAKE_RESOLVER_UID");
     unsetenv("RPM_FAKE_RESOLVER");    
@@ -330,10 +373,6 @@ initEnvironment()
   }
 #endif
 
-  ctx_s = getenv("RPM_FAKE_CTX");
-  if (ctx_s && *ctx_s) ctx_s = strdup(ctx_s);
-  else                 ctx_s = 0;
-
   ctx       = getAndClearEnv("RPM_FAKE_CTX",  VC_DYNAMIC_XID);
   caps      = getAndClearEnv("RPM_FAKE_CAP",  ~0x3404040f);
   flags     = getAndClearEnv("RPM_FAKE_FLAGS", 0);
@@ -352,7 +391,8 @@ initEnvironment()
 static void
 initSymbols()
 {
-  INIT(RTLD_NEXT, execv);
+    //INIT(RTLD_NEXT, rpm_execcon);
+    //INIT(RTLD_NEXT, execv);
   INIT(RTLD_NEXT, getgrnam);
   INIT(RTLD_NEXT, getpwnam);
   INIT(RTLD_NEXT, endpwent);
@@ -502,17 +542,21 @@ endpwent()
 
 
 static int
-execvWorker(char const *path, char * const argv[])
+execvWorker(char const *path, char * const argv[], char * const envp[])
 {
-  int          res;
+  int          res = -1;
 
   if (vc_isSupported(vcFEATURE_MIGRATE))
-    res = vc_migrate_context(ctx);
+    res = vc_ctx_migrate(ctx);
   else
+#ifdef VC_ENABLE_API_COMPAT  
     res = vc_new_s_context(ctx,caps,flags);
-  
+#else
+    WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not change context: migrate kernel feature missing and 'compat' API disabled\n");
+#endif
+    
   if (res!=-1)
-    res=execv_func(path, argv);
+    res=execve(path, argv, envp);
 
   return res;
 }
@@ -521,6 +565,7 @@ struct ExecvParams
 {
     char const *       path;
     char * const *     argv;
+    char * const *     envp;
     char const *       mnts;
 };
 
@@ -543,13 +588,14 @@ removeNamespaceMountsChild(struct ExecvParams const *params)
     ptr = strtok(0, ":");
   }
 
-  return execvWorker(params->path, params->argv);
+  return execvWorker(params->path, params->argv, params->envp);
 }
 
 static int
-removeNamespaceMounts(char const *path, char * const argv[])
+removeNamespaceMounts(char const *path,
+                     char * const argv[], char * const envp[])
 {
-  if (mnts==0) return execvWorker(path, argv);
+  if (mnts==0) return execvWorker(path, argv, envp);
 
   {
     int                                status;
@@ -558,6 +604,7 @@ removeNamespaceMounts(char const *path, char * const argv[])
 
     params.path = path;
     params.argv = argv;
+    params.envp = envp;
     params.mnts = mnts;
 
       // the rpmlib signal-handler is still active; use the default one to
@@ -590,7 +637,17 @@ removeNamespaceMounts(char const *path, char * const argv[])
 
 
 int
-execv(char const *path, char * const argv[]) __THROW
+execv(char const *path, char * const argv[])
+{
+  extern char **environ;
+
+  return removeNamespaceMounts(path, argv, environ);
+}
+
+int
+rpm_execcon(unsigned int UNUSED verified,
+           const char *filename,
+           char *const argv[], char *const envp[])
 {
-  return removeNamespaceMounts(path, argv);
+  return removeNamespaceMounts(filename, argv, envp);
 }