Skip to content

Commit 8793d00

Browse files
committed
su.c: implement --exec
It's now possible to run commands as other users without shell interpolation by using "--exec": Read /etc/shadow as root without specifying user: ``` su --exec /bin/cat -- /etc/shadow ``` Or specify user: ``` su --exec /bin/cat root -- /etc/shadow ```
1 parent 16036dd commit 8793d00

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/su.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ static bool do_interactive_shell = false;
9494
static bool fakelogin = false;
9595
static /*@observer@*/const char *shellstr;
9696
static /*@null@*/char *command = NULL;
97+
static /*@null@*/char *exec_command = NULL;
9798
static int optidx;
9899

99100

@@ -440,12 +441,14 @@ static void usage (int status)
440441
"\n"
441442
"Options:\n"
442443
" -c, --command COMMAND pass COMMAND to the invoked shell\n"
444+
" -e, --exec PATH run PATH without shell, follow -- with args\n"
443445
" -h, --help display this help message and exit\n"
444446
" -, -l, --login make the shell a login shell\n"
445447
" -m, -p,\n"
446448
" --preserve-environment do not reset environment variables, and\n"
447449
" keep the same shell\n"
448450
" -s, --shell SHELL use SHELL instead of the default in passwd\n"
451+
" -- pass all subsequent arguments on as-is\n"
449452
"\n"
450453
"If no username is given, assume root.\n"), (E_SUCCESS != status) ? stderr : stdout);
451454
exit (status);
@@ -820,6 +823,12 @@ static void process_flags (int argc, char **argv)
820823
}
821824

822825
command = argv[++optidx];
826+
} else if (flags_match (arg, "--exec", "-e", NULL)) {
827+
if (optidx == argc - 1) {
828+
flag_arg_required (arg);
829+
}
830+
831+
exec_command = argv[++optidx];
823832
} else if (flags_match (arg, "--help", "-h", NULL)) {
824833
usage (E_SUCCESS);
825834
} else if (flags_match (arg, "--login", "-l", "-")) {
@@ -843,6 +852,17 @@ static void process_flags (int argc, char **argv)
843852
}
844853
}
845854

855+
if (NULL != exec_command && NULL != command) {
856+
fprintf (stderr,
857+
_("%s: COMMAND and PATH are mutually exclusive\n"),
858+
argv[0]);
859+
usage (E_USAGE);
860+
}
861+
862+
if (NULL != exec_command) {
863+
command = exec_command;
864+
}
865+
846866
/* if next arg is not "--", treat as USER */
847867
if (optidx < argc && strcmp (argv[optidx], "--")) {
848868
STRFCPY (name, argv[optidx++]); /* use this login id */
@@ -1226,10 +1246,18 @@ int main (int argc, char **argv)
12261246
* with the rest of the command line included.
12271247
*/
12281248
argv[-1] = cp;
1229-
execve_shell (shellstr, &argv[-1], environ);
1230-
err = errno;
1231-
(void) fprintf (stderr,
1232-
_("Cannot execute %s\n"), shellstr);
1249+
1250+
if (NULL != exec_command) {
1251+
(void) execve (command, &argv[1], environ);
1252+
err = errno;
1253+
(void) fprintf (stderr,
1254+
_("Cannot execute \'%s\'\n"), command);
1255+
} else {
1256+
execve_shell (shellstr, &argv[-1], environ);
1257+
err = errno;
1258+
(void) fprintf (stderr,
1259+
_("Cannot execute \'%s\'\n"), shellstr);
1260+
}
12331261
errno = err;
12341262
} else {
12351263
(void) shell (shellstr, cp, environ);

0 commit comments

Comments
 (0)