Skip to contentSkip to conversation

username support for jail_uml

uml-devel Goetz Bock 1 participant 1 message May 30, 2004 User-mode Linux (UML)

lore
Share & tools
lore

Goetz Bock

author
#0 lore raw

Hi all,

I've written included patch to add support for usernames/groups and gids to jail_uml. The default group/gid is hardcoded to 99 (=nobody on my system).

Cu,
Goetz.

jail/jail_uml.c +36 -4
diff -ur tools/jail/jail_uml.c tools-bn/jail/jail_uml.c
@@ -1,18 +1,32 @@
+/* jail a uml into a directory.
+ 
+*/
+
 #include <stdio.h>
 #include <unistd.h>
 #include <errno.h>
+#include <pwd.h>
+#include <grp.h>
+#include <sys/types.h>
 
 static void Usage(void)
 {
-  fprintf(stderr, "Usage : jail_uml jail-directory uid "
+  fprintf(stderr, "Usage : jail_uml jail-directory user "
 	  "uml-command-line ...\n");
+  fprintf(stderr, "    or: jail_uml jail-directory uid "
+	  "uml-command-line ...\n\n");
+  fprintf(stderr, "If the user is not found, it's assumed to be a uid.\n");
   exit(1);
 }
 
 int main(int argc, char **argv)
 {
   char *dir, *end;
-  int uid;
+  char *user;
+  struct passwd *pw;
+  int uid, gid=99;
+  gid_t gidset[1];
+  gidset[0]=gid;
 
   if(geteuid() != 0){
     fprintf(stderr, "jail_uml must be run as root\n");
@@ -21,8 +35,22 @@
 
   if(argc < 3) Usage();
   dir = argv[1];
-  uid = strtoul(argv[2], &end, 0);
-  if(*end != '\0') Usage();
+  user = argv[2];
+  
+  // get users password information
+  pw = getpwnam (user);
+  if (pw == 0){
+    uid = strtoul(argv[2], &end, 0);
+    if(*end != '\0') Usage();
+    setgroups(1, gidset);
+  } else {
+    // try to init groups
+    initgroups (pw->pw_name, pw->pw_gid); 
+    uid = pw->pw_uid;
+    gid = pw->pw_gid;
+  }
+
+  // if(*end != '\0') Usage();
   argc -= 3;
   argv += 3;
 
@@ -36,6 +64,10 @@
     exit(1);
   }
 
+  if(setgid(gid)){
+    perror("setgid");
+    exit(1);
+  }
   if(setuid(uid)){
     perror("setuid");
     exit(1);

-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
_______________________________________________
User-mode-linux-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel