.
[coreutils.git] / src / setuidgid.c
blobfd5348a1353ce3cb1f9f3592962a55cc975a4cd2
1 /* setuidgid - run a command with the UID and GID of a specified user
2 Copyright (C) 2003, 2004 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)
7 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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Jim Meyering */
20 #include <config.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 "quote.h"
32 #define PROGRAM_NAME "setuidgid"
34 /* I wrote this program from scratch, based on the description of
35 D.J. Bernstein's program: http://cr.yp.to/daemontools/setuidgid.html. */
36 #define AUTHORS "Jim Meyering"
38 #define SETUIDGID_FAILURE 111
40 char *program_name;
42 void
43 usage (int status)
45 if (status != EXIT_SUCCESS)
46 fprintf (stderr, _("Try `%s --help' for more information.\n"),
47 program_name);
48 else
50 printf (_("\
51 Usage: %s USERNAME COMMAND [ARGUMENT]...\n\
52 or: %s OPTION\n\
53 "),
54 program_name, program_name);
56 fputs (_("\
57 Drop any supplemental groups, assume the user-ID and group-ID of\n\
58 the specified USERNAME, and run COMMAND with any specified ARGUMENTs.\n\
59 Exit with status 111 if unable to assume the required UID and GID.\n\
60 Otherwise, exit with the exit status of COMMAND.\n\
61 This program is useful only when run by root (UID=0).\n\
62 \n\
63 "), stdout);
64 fputs (HELP_OPTION_DESCRIPTION, stdout);
65 fputs (VERSION_OPTION_DESCRIPTION, stdout);
66 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
68 exit (status);
71 int
72 main (int argc, char **argv)
74 char const *user_id;
75 struct passwd *pwd;
77 initialize_main (&argc, &argv);
78 program_name = argv[0];
79 setlocale (LC_ALL, "");
80 bindtextdomain (PACKAGE, LOCALEDIR);
81 textdomain (PACKAGE);
83 initialize_exit_failure (SETUIDGID_FAILURE);
84 atexit (close_stdout);
86 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
87 usage, AUTHORS, (char const *) NULL);
89 /* The above handles --help and --version.
90 Since there is no other invocation of getopt, handle `--' here. */
91 if (argc > 1 && STREQ (argv[1], "--"))
93 --argc;
94 ++argv;
97 if (argc <= 2)
99 error (0, 0, _("too few arguments"));
100 usage (SETUIDGID_FAILURE);
103 user_id = argv[1];
104 pwd = getpwnam (user_id);
105 if (pwd == NULL)
106 error (SETUIDGID_FAILURE, errno,
107 _("unknown user-ID: %s"), quote (user_id));
109 if (setgroups (1, &pwd->pw_gid))
110 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 %ld"), (long int) pwd->pw_gid);
116 if (setuid (pwd->pw_uid))
117 error (SETUIDGID_FAILURE, errno,
118 _("cannot set user-ID to %ld"), (long int) pwd->pw_uid);
121 char **cmd = argv + 2;
122 int exit_status;
123 execvp (*cmd, cmd);
124 exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
126 error (0, errno, _("cannot run command %s"), quote (*cmd));
127 exit (exit_status);