build: use getgr*_nomembers functions on Interix
[coreutils/ericb.git] / src / chroot.c
blob8bd39fcb253607fc3a2d5a4d41e57745952c9f47
1 /* chroot -- run command or shell with special root directory
2 Copyright (C) 1995-1997, 1999-2004, 2007-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Roland McGrath. */
19 #include <config.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <grp.h>
25 #include "system.h"
26 #include "error.h"
27 #include "quote.h"
28 #include "userspec.h"
29 #include "xstrtol.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "chroot"
34 #define AUTHORS proper_name ("Roland McGrath")
36 #ifndef MAXGID
37 # define MAXGID GID_T_MAX
38 #endif
40 enum
42 GROUPS = UCHAR_MAX + 1,
43 USERSPEC
46 static struct option const long_opts[] =
48 {"groups", required_argument, NULL, GROUPS},
49 {"userspec", required_argument, NULL, USERSPEC},
50 {GETOPT_HELP_OPTION_DECL},
51 {GETOPT_VERSION_OPTION_DECL},
52 {NULL, 0, NULL, 0}
55 #if ! HAVE_SETGROUPS
56 /* At least Interix lacks supplemental group support. Define an
57 always-successful replacement to avoid checking for setgroups
58 availability everywhere, just to support broken platforms. */
59 static int
60 setgroups (size_t size ATTRIBUTE_UNUSED, gid_t const *list ATTRIBUTE_UNUSED)
62 return 0;
64 #endif
66 /* Call setgroups to set the supplementary groups to those listed in GROUPS.
67 GROUPS is a comma separated list of supplementary groups (names or numbers).
68 Parse that list, converting any names to numbers, and call setgroups on the
69 resulting numbers. Upon any failure give a diagnostic and return nonzero.
70 Otherwise return zero. */
71 static int
72 set_additional_groups (char const *groups)
74 GETGROUPS_T *gids = NULL;
75 size_t n_gids_allocated = 0;
76 size_t n_gids = 0;
77 char *buffer = xstrdup (groups);
78 char const *tmp;
79 int ret = 0;
81 for (tmp = strtok (buffer, ","); tmp; tmp = strtok (NULL, ","))
83 struct group *g;
84 unsigned long int value;
86 if (xstrtoul (tmp, NULL, 10, &value, "") == LONGINT_OK && value <= MAXGID)
87 g = getgrgid (value);
88 else
90 g = getgrnam (tmp);
91 if (g != NULL)
92 value = g->gr_gid;
95 if (g == NULL)
97 error (0, errno, _("invalid group %s"), quote (tmp));
98 ret = -1;
99 continue;
102 if (n_gids == n_gids_allocated)
103 gids = X2NREALLOC (gids, &n_gids_allocated);
104 gids[n_gids++] = value;
107 if (ret == 0 && n_gids == 0)
109 error (0, 0, _("invalid group list %s"), quote (groups));
110 ret = -1;
113 if (ret == 0)
115 ret = setgroups (n_gids, gids);
116 if (ret)
117 error (0, errno, _("failed to set additional groups"));
120 free (buffer);
121 free (gids);
122 return ret;
125 void
126 usage (int status)
128 if (status != EXIT_SUCCESS)
129 fprintf (stderr, _("Try `%s --help' for more information.\n"),
130 program_name);
131 else
133 printf (_("\
134 Usage: %s [OPTION] NEWROOT [COMMAND [ARG]...]\n\
135 or: %s OPTION\n\
136 "), program_name, program_name);
138 fputs (_("\
139 Run COMMAND with root directory set to NEWROOT.\n\
141 "), stdout);
143 fputs (_("\
144 --userspec=USER:GROUP specify user and group (ID or name) to use\n\
145 --groups=G_LIST specify supplementary groups as g1,g2,..,gN\n\
146 "), stdout);
148 fputs (HELP_OPTION_DESCRIPTION, stdout);
149 fputs (VERSION_OPTION_DESCRIPTION, stdout);
150 fputs (_("\
152 If no command is given, run ``${SHELL} -i'' (default: /bin/sh).\n\
153 "), stdout);
154 emit_ancillary_info ();
156 exit (status);
160 main (int argc, char **argv)
162 int c;
163 char const *userspec = NULL;
164 char const *groups = NULL;
166 initialize_main (&argc, &argv);
167 set_program_name (argv[0]);
168 setlocale (LC_ALL, "");
169 bindtextdomain (PACKAGE, LOCALEDIR);
170 textdomain (PACKAGE);
172 initialize_exit_failure (EXIT_CANCELED);
173 atexit (close_stdout);
175 while ((c = getopt_long (argc, argv, "+", long_opts, NULL)) != -1)
177 switch (c)
179 case USERSPEC:
180 userspec = optarg;
181 break;
183 case GROUPS:
184 groups = optarg;
185 break;
187 case_GETOPT_HELP_CHAR;
189 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
191 default:
192 usage (EXIT_CANCELED);
196 if (argc <= optind)
198 error (0, 0, _("missing operand"));
199 usage (EXIT_CANCELED);
202 if (chroot (argv[optind]) != 0)
203 error (EXIT_CANCELED, errno, _("cannot change root directory to %s"),
204 argv[optind]);
206 if (chdir ("/"))
207 error (EXIT_CANCELED, errno, _("cannot chdir to root directory"));
209 if (argc == optind + 1)
211 /* No command. Run an interactive shell. */
212 char *shell = getenv ("SHELL");
213 if (shell == NULL)
214 shell = bad_cast ("/bin/sh");
215 argv[0] = shell;
216 argv[1] = bad_cast ("-i");
217 argv[2] = NULL;
219 else
221 /* The following arguments give the command. */
222 argv += optind + 1;
225 bool fail = false;
227 /* Attempt to set all three: supplementary groups, group ID, user ID.
228 Diagnose any failures. If any have failed, exit before execvp. */
229 if (userspec)
231 uid_t uid = -1;
232 gid_t gid = -1;
233 char *user;
234 char *group;
235 char const *err = parse_user_spec (userspec, &uid, &gid, &user, &group);
237 if (err)
238 error (EXIT_CANCELED, errno, "%s", err);
240 free (user);
241 free (group);
243 if (groups && set_additional_groups (groups))
244 fail = true;
246 if (gid != (gid_t) -1 && setgid (gid))
248 error (0, errno, _("failed to set group-ID"));
249 fail = true;
252 if (uid != (uid_t) -1 && setuid (uid))
254 error (0, errno, _("failed to set user-ID"));
255 fail = true;
258 else
260 /* Yes, this call is identical to the one above.
261 However, when --userspec and --groups groups are used together,
262 we don't want to call this function until after parsing USER:GROUP,
263 and it must be called before setuid. */
264 if (groups && set_additional_groups (groups))
265 fail = true;
268 if (fail)
269 exit (EXIT_CANCELED);
271 /* Execute the given command. */
272 execvp (argv[0], argv);
275 int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
276 error (0, errno, _("failed to run command %s"), quote (argv[0]));
277 exit (exit_status);