Make the new printf-surprise test more precise.
[coreutils/ericb.git] / src / setuidgid.c
blob7e0109abaf80a554b1a0fc0aa37901a04c59cf52
1 /* setuidgid - run a command with the UID and GID of a specified user
2 Copyright (C) 2003-2007 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 Jim Meyering */
19 #include <config.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <pwd.h>
24 #include <grp.h>
26 #include "system.h"
28 #include "error.h"
29 #include "long-options.h"
30 #include "mgetgroups.h"
31 #include "quote.h"
33 #define PROGRAM_NAME "setuidgid"
35 /* I wrote this program from scratch, based on the description of
36 D.J. Bernstein's program: http://cr.yp.to/daemontools/setuidgid.html. */
37 #define AUTHORS "Jim Meyering"
39 #define SETUIDGID_FAILURE 111
41 char *program_name;
43 void
44 usage (int status)
46 if (status != EXIT_SUCCESS)
47 fprintf (stderr, _("Try `%s --help' for more information.\n"),
48 program_name);
49 else
51 printf (_("\
52 Usage: %s USERNAME COMMAND [ARGUMENT]...\n\
53 or: %s OPTION\n\
54 "),
55 program_name, program_name);
57 fputs (_("\
58 Drop any supplemental groups, assume the user-ID and group-ID of\n\
59 the specified USERNAME, and run COMMAND with any specified ARGUMENTs.\n\
60 Exit with status 111 if unable to assume the required user and group ID.\n\
61 Otherwise, exit with the exit status of COMMAND.\n\
62 This program is useful only when run by root (user ID zero).\n\
63 \n\
64 "), stdout);
65 fputs (HELP_OPTION_DESCRIPTION, stdout);
66 fputs (VERSION_OPTION_DESCRIPTION, stdout);
67 emit_bug_reporting_address ();
69 exit (status);
72 int
73 main (int argc, char **argv)
75 char const *user_id;
76 struct passwd *pwd;
78 initialize_main (&argc, &argv);
79 program_name = argv[0];
80 setlocale (LC_ALL, "");
81 bindtextdomain (PACKAGE, LOCALEDIR);
82 textdomain (PACKAGE);
84 initialize_exit_failure (SETUIDGID_FAILURE);
85 atexit (close_stdout);
87 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
88 usage, AUTHORS, (char const *) NULL);
89 if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
90 usage (SETUIDGID_FAILURE);
92 if (argc <= optind + 1)
94 if (argc < optind + 1)
95 error (0, 0, _("missing operand"));
96 else
97 error (0, 0, _("missing operand after %s"), quote (argv[optind]));
98 usage (SETUIDGID_FAILURE);
101 user_id = argv[optind];
102 pwd = getpwnam (user_id);
103 if (pwd == NULL)
104 error (SETUIDGID_FAILURE, errno,
105 _("unknown user-ID: %s"), quote (user_id));
107 #if HAVE_SETGROUPS
109 GETGROUPS_T *groups;
110 int n_groups = mgetgroups (user_id, pwd->pw_gid, &groups);
111 if (n_groups < 0)
113 n_groups = 1;
114 groups = xmalloc (sizeof *groups);
115 *groups = pwd->pw_gid;
118 if (0 < n_groups && setgroups (n_groups, groups))
119 error (SETUIDGID_FAILURE, errno, _("cannot set supplemental group(s)"));
121 free (groups);
123 #endif
125 if (setgid (pwd->pw_gid))
126 error (SETUIDGID_FAILURE, errno,
127 _("cannot set group-ID to %lu"), (unsigned long int) pwd->pw_gid);
129 if (setuid (pwd->pw_uid))
130 error (SETUIDGID_FAILURE, errno,
131 _("cannot set user-ID to %lu"), (unsigned long int) pwd->pw_uid);
134 char **cmd = argv + optind + 1;
135 int exit_status;
136 execvp (*cmd, cmd);
137 exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
139 error (0, errno, _("cannot run command %s"), quote (*cmd));
140 exit (exit_status);