* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / setuidgid.c
blob3a51225dc0d5573afd98d2e6115fdd6182c8a79a
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)
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Jim Meyering */
20 #include <config.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <pwd.h>
25 #include <grp.h>
27 #include "system.h"
29 #include "error.h"
30 #include "long-options.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 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
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, 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"));
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
108 if (setgroups (1, &pwd->pw_gid))
109 error (SETUIDGID_FAILURE, errno, _("cannot set supplemental group"));
110 #endif
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;
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);