- Teach vcontext about usernames (if dietlibc is used).
authorDaniel Hokka Zakrisson <daniel@hozac.com>
Tue, 12 Sep 2006 17:09:08 +0000 (17:09 +0000)
committerDaniel Hokka Zakrisson <daniel@hozac.com>
Tue, 12 Sep 2006 17:09:08 +0000 (17:09 +0000)
- Teach capchroot about uid 0.
- Add Einitgroups.
- Specify uid 0 by default, so username lookups are avoided.

git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@2307 94cd875c-1c1d-0410-91d2-eb244daf1a30

ensc_wrappers/wrappers-unistd.hc
scripts/vserver
src/capchroot.c
src/vcontext.c

index 6b21fc3..d6dfc71 100644 (file)
@@ -156,6 +156,12 @@ Esetgroups(size_t size, const gid_t *list)
 {
   FatalErrnoError(setgroups(size, list)==-1, "setgroups()");
 }
+
+inline static void
+Einitgroups(const char *user, gid_t group)
+{
+  FatalErrnoError(initgroups(user, group)==-1, "initgroups()");
+}
 #endif
 
 inline static WRAPPER_DECL int
index 9eb7249..0a8457f 100755 (executable)
@@ -213,16 +213,16 @@ case "$2" in
        ;;
     (exec)
        shift 2
-       suexec root "$@"
+       suexec 0 "$@"
        ;;
     (chkconfig)
        shift 2
-       suexec root chkconfig "$@"
+       suexec 0 chkconfig "$@"
        ;;
     (enter)
        OPTS_VCONTEXT_ENTER=( "${OPTS_VCONTEXT_ENTER[@]}" --vlogin )
        getEnterShell "$VSERVER_DIR"
-       suexec root "${ENTER_SHELL[@]}"
+       suexec 0 "${ENTER_SHELL[@]}"
        ;;
     (running)
        isVserverRunning "$VSERVER_DIR"
index e2feed9..d739e6b 100644 (file)
@@ -101,7 +101,7 @@ static UNUSED void
 setUser(char const *user)
 {
   struct passwd                *p = 0;
-  if (user!=0 && strcmp(user, "root")!=0) {
+  if (user!=0 && strcmp(user, "root")!=0 && strcmp(user, "0")!=0) {
     errno = 0;
     p     = getpwnam(user);
     if (p==0) {
index 3cae6ea..cabcbc3 100644 (file)
@@ -1,6 +1,6 @@
 // $Id$    --*- c -*--
 
-// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+// Copyright (C) 2004-2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
 //  
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -33,6 +33,9 @@
 #include <sys/un.h>
 #include <assert.h>
 #include <signal.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <grp.h>
 
 #include <linux/personality.h>
 
@@ -107,7 +110,7 @@ struct Arguments {
     uint_least32_t     personality_type;
     int                        verbosity;
     bool               do_chroot;
-    uid_t              uid;
+    char const *       uid;
     xid_t              xid;
     char const *       sync_sock;
     char const *       sync_msg;
@@ -165,7 +168,7 @@ showVersion()
   WRITE_MSG(1,
            "vcontext " VERSION " -- manages the creation of security contexts\n"
            "This program is part of " PACKAGE_STRING "\n\n"
-           "Copyright (C) 2004 Enrico Scholz\n"
+           "Copyright (C) 2004-2006 Enrico Scholz\n"
            VERSION_COPYRIGHT_DISCLAIMER);
   exit(0);
 }
@@ -281,9 +284,35 @@ doit(struct Arguments const *args, int argc, char *argv[])
     if (args->do_migrate && !args->do_migrateself)
       Evc_ctx_migrate(xid);
 
-    if (args->uid!=(uid_t)(-1) && getuid()!=args->uid) {
-      Esetuid(args->uid);
-      if (getuid()!=args->uid) {
+    if (args->uid != NULL) {
+      uid_t uid = 0;
+      unsigned long tmp;
+
+      if (!isNumberUnsigned(args->uid, &tmp, false)) {
+#ifdef __dietlibc__
+       struct passwd *pw;
+       pw = getpwnam(args->uid);
+       if (pw == NULL) {
+         WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Username '");
+         WRITE_STR(2, args->uid);
+         WRITE_MSG(2, "' does not exist\n");
+         return wrapper_exit_code;
+       }
+       uid = pw->pw_uid;
+       Einitgroups(args->uid, pw->pw_gid);
+       Esetgid(pw->pw_gid);
+#else
+       WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Uid '");
+       WRITE_STR(2, args->uid);
+       WRITE_MSG(2, "' is not a number\n");
+       return wrapper_exit_code;
+#endif
+      }
+      else
+       uid = (uid_t) tmp;
+
+      Esetuid((uid_t) uid);
+      if (getuid()!=uid) {
        WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Something went wrong while changing the UID\n");
        exit(wrapper_exit_code);
       }
@@ -355,7 +384,7 @@ int main (int argc, char *argv[])
     .is_silentexist    = false,
     .set_namespace     = false,
     .verbosity         = 1,
-    .uid               = -1,
+    .uid               = NULL,
     .xid               = VC_DYNAMIC_XID,
     .personality_type  = VC_BAD_PERSONALITY,
     .personality_flags = 0,
@@ -380,7 +409,7 @@ int main (int argc, char *argv[])
       case CMD_SILENTEXIST     :  args.is_silentexist = true;   break;
       case CMD_SYNCSOCK                :  args.sync_sock      = optarg; break;
       case CMD_SYNCMSG         :  args.sync_msg       = optarg; break;
-      case CMD_UID             :  args.uid = atol(optarg);      break;
+      case CMD_UID             :  args.uid            = optarg; break;
       case CMD_XID             :  args.xid = Evc_xidopt2xid(optarg,true); break;
       case CMD_SILENT          :  --args.verbosity; break;
       case CMD_PERSTYPE                :