From 1811d12f2961262cec31e72f5da24363c2f71526 Mon Sep 17 00:00:00 2001
From: Giuseppe Scrivano <gscrivano@gnu.org>
Date: Fri, 1 May 2009 23:50:11 +0200
Subject: [PATCH] * src/chroot.c (main): Added possibility to specify the user and group
 to setgid/setuid inside the chroot.

---
 src/chroot.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/src/chroot.c b/src/chroot.c
index 6d3fddf..e88735f 100644
--- a/src/chroot.c
+++ b/src/chroot.c
@@ -25,6 +25,7 @@
 #include "system.h"
 #include "error.h"
 #include "long-options.h"
+#include "userspec.h"
 #include "quote.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -32,6 +33,16 @@
 
 #define AUTHORS proper_name ("Roland McGrath")
 
+
+static struct option const long_opts[] =
+{
+  {"userspec", required_argument, NULL, 'u'},
+  {GETOPT_HELP_OPTION_DECL},
+  {GETOPT_VERSION_OPTION_DECL},
+  {NULL, 0, NULL, 0}
+};
+
+
 void
 usage (int status)
 {
@@ -41,13 +52,19 @@ usage (int status)
   else
     {
       printf (_("\
-Usage: %s NEWROOT [COMMAND [ARG]...]\n\
+Usage: %s [OPTION] NEWROOT [COMMAND [ARG]...]\n\
   or:  %s OPTION\n\
 "), program_name, program_name);
+
       fputs (_("\
 Run COMMAND with root directory set to NEWROOT.\n\
 \n\
 "), stdout);
+
+      fputs (_("\
+  -u, --userspec specify the userspec in the form USER:GROUP\n\
+"), stdout);
+
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
       fputs (_("\
@@ -62,6 +79,9 @@ If no command is given, run ``${SHELL} -i'' (default: /bin/sh).\n\
 int
 main (int argc, char **argv)
 {
+  int c;
+  const char *userspec = NULL;
+
   initialize_main (&argc, &argv);
   set_program_name (argv[0]);
   setlocale (LC_ALL, "");
@@ -73,22 +93,26 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
 		      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
-    usage (EXIT_FAILURE);
+
+  while ((c = getopt_long (argc, argv, "+u:", long_opts, NULL))	 != -1)
+    {
+      if (c == 'u')
+        userspec = optarg;
+    }
 
   if (argc <= optind)
     {
       error (0, 0, _("missing operand"));
       usage (EXIT_FAILURE);
     }
-
+  /*
   if (chroot (argv[optind]) != 0)
     error (EXIT_FAILURE, errno, _("cannot change root directory to %s"),
 	   argv[optind]);
 
   if (chdir ("/"))
     error (EXIT_FAILURE, errno, _("cannot chdir to root directory"));
-
+*/
   if (argc == optind + 1)
     {
       /* No command.  Run an interactive shell.  */
@@ -105,6 +129,33 @@ main (int argc, char **argv)
       argv += optind + 1;
     }
 
+  if (userspec)
+    {
+      uid_t uid;
+      gid_t gid;
+      char *user;
+      char *group;
+      const char *err = parse_user_spec (userspec, &uid, &gid, &user, &group);
+      if (err)
+        {
+          perror (err);
+          exit (1);
+        }
+
+      free (user);
+      free (group);
+
+      if (gid && setgid (gid))
+        {
+          error (0, errno, _("cannot setgid"));
+        }
+
+      if (uid && setuid (uid))
+        {
+          error (0, errno, _("cannot setgid"));
+        }
+    }
+
   /* Execute the given command.  */
   execvp (argv[0], argv);
 
-- 
1.6.2.1

