1 /* setuidgid - run a command with the UID and GID of a specified user
2 Copyright (C) 2003, 2004, 2005, 2006 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Jim Meyering */
23 #include <sys/types.h>
30 #include "long-options.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
46 if (status != EXIT_SUCCESS)
47 fprintf (stderr, _("Try `%s --help' for more information.\n"),
52 Usage: %s USERNAME COMMAND [ARGUMENT]...\n\
55 program_name, program_name);
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\
65 fputs (HELP_OPTION_DESCRIPTION, stdout);
66 fputs (VERSION_OPTION_DESCRIPTION, stdout);
67 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
73 main (int argc, char **argv)
78 initialize_main (&argc, &argv);
79 program_name = argv[0];
80 setlocale (LC_ALL, "");
81 bindtextdomain (PACKAGE, LOCALEDIR);
84 initialize_exit_failure (SETUIDGID_FAILURE);
85 atexit (close_stdout);
87 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, 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"));
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);
104 error (SETUIDGID_FAILURE, errno,
105 _("unknown user-ID: %s"), quote (user_id));
108 if (setgroups (1, &pwd->pw_gid))
109 error (SETUIDGID_FAILURE, errno, _("cannot set supplemental group"));
112 if (setgid (pwd->pw_gid))
113 error (SETUIDGID_FAILURE, errno,
114 _("cannot set group-ID to %lu"), (unsigned long int) pwd->pw_gid);
116 if (setuid (pwd->pw_uid))
117 error (SETUIDGID_FAILURE, errno,
118 _("cannot set user-ID to %lu"), (unsigned long int) pwd->pw_uid);
121 char **cmd = argv + optind + 1;
124 exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
126 error (0, errno, _("cannot run command %s"), quote (*cmd));