*: s/"/bin/sh"/DEFAULT_SHELL, run_shell() API fix, remove unneeded strdup

function                                             old     new   delta
run_shell                                            157     166      +9
su_main                                              477     470      -7
sulogin_main                                         515     503     -12

Signed-off-by: Ladislav Michl <Ladislav.Michl@seznam.cz>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Ladislav Michl 2010-06-27 03:23:31 +02:00 committed by Denys Vlasenko
parent 1b14cdb27c
commit a73b87e934
8 changed files with 58 additions and 54 deletions

View File

@ -102,8 +102,8 @@ void FAST_FUNC data_extract_to_command(archive_handle_t *archive_handle)
close(p[1]); close(p[1]);
xdup2(p[0], STDIN_FILENO); xdup2(p[0], STDIN_FILENO);
signal(SIGPIPE, SIG_DFL); signal(SIGPIPE, SIG_DFL);
execl("/bin/sh", "/bin/sh" + 5, "-c", archive_handle->tar__to_command, NULL); execl(DEFAULT_SHELL, DEFAULT_SHELL_SHORT_NAME, "-c", archive_handle->tar__to_command, NULL);
bb_perror_msg_and_die("can't execute '%s'", "/bin/sh"); bb_perror_msg_and_die("can't execute '%s'", DEFAULT_SHELL);
} }
close(p[0]); close(p[0]);
/* Our caller is expected to do signal(SIGPIPE, SIG_IGN) /* Our caller is expected to do signal(SIGPIPE, SIG_IGN)

View File

@ -1157,7 +1157,6 @@ char *bb_simplify_abs_path_inplace(char *path) FAST_FUNC;
extern void bb_do_delay(int seconds) FAST_FUNC; extern void bb_do_delay(int seconds) FAST_FUNC;
extern void change_identity(const struct passwd *pw) FAST_FUNC; extern void change_identity(const struct passwd *pw) FAST_FUNC;
extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) NORETURN FAST_FUNC; extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) NORETURN FAST_FUNC;
extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) FAST_FUNC;
#if ENABLE_SELINUX #if ENABLE_SELINUX
extern void renew_current_security_context(void) FAST_FUNC; extern void renew_current_security_context(void) FAST_FUNC;
extern void set_current_security_context(security_context_t sid) FAST_FUNC; extern void set_current_security_context(security_context_t sid) FAST_FUNC;
@ -1593,12 +1592,12 @@ extern struct globals *const ptr_to_globals;
* use bb_default_login_shell and following defines. * use bb_default_login_shell and following defines.
* If you change LIBBB_DEFAULT_LOGIN_SHELL, * If you change LIBBB_DEFAULT_LOGIN_SHELL,
* don't forget to change increment constant. */ * don't forget to change increment constant. */
#define LIBBB_DEFAULT_LOGIN_SHELL "-/bin/sh" #define LIBBB_DEFAULT_LOGIN_SHELL "-/bin/sh"
extern const char bb_default_login_shell[]; extern const char bb_default_login_shell[];
/* "/bin/sh" */ /* "/bin/sh" */
#define DEFAULT_SHELL (bb_default_login_shell+1) #define DEFAULT_SHELL (bb_default_login_shell+1)
/* "sh" */ /* "sh" */
#define DEFAULT_SHELL_SHORT_NAME (bb_default_login_shell+6) #define DEFAULT_SHELL_SHORT_NAME (bb_default_login_shell+6)
#if ENABLE_FEATURE_DEVFS #if ENABLE_FEATURE_DEVFS
# define CURRENT_VC "/dev/vc/0" # define CURRENT_VC "/dev/vc/0"

View File

@ -49,15 +49,14 @@ void FAST_FUNC set_current_security_context(security_context_t sid)
#endif #endif
/* Run SHELL, or DEFAULT_SHELL if SHELL is empty. /* Run SHELL, or DEFAULT_SHELL if SHELL is "" or NULL.
If COMMAND is nonzero, pass it to the shell with the -c option. * If COMMAND is nonzero, pass it to the shell with the -c option.
If ADDITIONAL_ARGS is nonzero, pass it to the shell as more * If ADDITIONAL_ARGS is nonzero, pass it to the shell as more
arguments. */ * arguments. */
void FAST_FUNC run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) void FAST_FUNC run_shell(const char *shell, int loginshell, const char *command, const char **additional_args)
{ {
const char **args; const char **args;
int argno = 1; int argno;
int additional_args_cnt = 0; int additional_args_cnt = 0;
for (args = additional_args; args && *args; args++) for (args = additional_args; args && *args; args++)
@ -65,11 +64,13 @@ void FAST_FUNC run_shell(const char *shell, int loginshell, const char *command,
args = xmalloc(sizeof(char*) * (4 + additional_args_cnt)); args = xmalloc(sizeof(char*) * (4 + additional_args_cnt));
args[0] = bb_get_last_path_component_nostrip(xstrdup(shell)); if (!shell || !shell[0])
shell = DEFAULT_SHELL;
args[0] = bb_get_last_path_component_nostrip(shell);
if (loginshell) if (loginshell)
args[0] = xasprintf("-%s", args[0]); args[0] = xasprintf("-%s", args[0]);
argno = 1;
if (command) { if (command) {
args[argno++] = "-c"; args[argno++] = "-c";
args[argno++] = command; args[argno++] = command;
@ -79,6 +80,7 @@ void FAST_FUNC run_shell(const char *shell, int loginshell, const char *command,
args[argno++] = *additional_args; args[argno++] = *additional_args;
} }
args[argno] = NULL; args[argno] = NULL;
#if ENABLE_SELINUX #if ENABLE_SELINUX
if (current_sid) if (current_sid)
setexeccon(current_sid); setexeccon(current_sid);

View File

@ -43,7 +43,7 @@ void FAST_FUNC setup_environment(const char *shell, int flags, const struct pass
const char *term; const char *term;
/* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH. /* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH.
Unset all other environment variables. */ * Unset all other environment variables. */
term = getenv("TERM"); term = getenv("TERM");
clearenv(); clearenv();
if (term) if (term)
@ -57,7 +57,7 @@ void FAST_FUNC setup_environment(const char *shell, int flags, const struct pass
//xsetenv("SHELL", shell); //xsetenv("SHELL", shell);
} else if (flags & SETUP_ENV_CHANGEENV) { } else if (flags & SETUP_ENV_CHANGEENV) {
/* Set HOME, SHELL, and if not becoming a super-user, /* Set HOME, SHELL, and if not becoming a super-user,
USER and LOGNAME. */ * USER and LOGNAME. */
if (pw->pw_uid) { if (pw->pw_uid) {
shortcut: shortcut:
xsetenv("USER", pw->pw_name); xsetenv("USER", pw->pw_name);

View File

@ -201,7 +201,7 @@ int login_main(int argc UNUSED_PARAM, char **argv)
}; };
char *fromhost; char *fromhost;
char username[USERNAME_SIZE]; char username[USERNAME_SIZE];
const char *tmp; const char *shell;
int run_by_root; int run_by_root;
unsigned opt; unsigned opt;
int count = 0; int count = 0;
@ -389,10 +389,10 @@ int login_main(int argc UNUSED_PARAM, char **argv)
run_login_script(pw, full_tty); run_login_script(pw, full_tty);
change_identity(pw); change_identity(pw);
tmp = pw->pw_shell; shell = pw->pw_shell;
if (!tmp || !*tmp) if (!shell || !shell[0])
tmp = DEFAULT_SHELL; shell = DEFAULT_SHELL;
setup_environment(tmp, setup_environment(shell,
(!(opt & LOGIN_OPT_p) * SETUP_ENV_CLEARENV) + SETUP_ENV_CHANGEENV, (!(opt & LOGIN_OPT_p) * SETUP_ENV_CLEARENV) + SETUP_ENV_CHANGEENV,
pw); pw);
@ -427,7 +427,7 @@ int login_main(int argc UNUSED_PARAM, char **argv)
signal(SIGINT, SIG_DFL); signal(SIGINT, SIG_DFL);
/* Exec login shell with no additional parameters */ /* Exec login shell with no additional parameters */
run_shell(tmp, 1, NULL, NULL); run_shell(shell, 1, NULL, NULL);
/* return EXIT_FAILURE; - not reached */ /* return EXIT_FAILURE; - not reached */
} }

View File

@ -10,23 +10,27 @@
#if ENABLE_FEATURE_SU_CHECKS_SHELLS #if ENABLE_FEATURE_SU_CHECKS_SHELLS
/* Return 1 if SHELL is a restricted shell (one not returned by /* Return 1 if SHELL is a restricted shell (one not returned by
getusershell), else 0, meaning it is a standard shell. */ * getusershell), else 0, meaning it is a standard shell. */
static int restricted_shell(const char *shell) static int restricted_shell(const char *shell)
{ {
char *line; char *line;
int result = 1;
/*setusershell(); - getusershell does it itself*/ /*setusershell(); - getusershell does it itself*/
while ((line = getusershell()) != NULL) { while ((line = getusershell()) != NULL) {
if (/* *line != '#' && */ strcmp(line, shell) == 0) if (/* *line != '#' && */ strcmp(line, shell) == 0) {
return 0; result = 0;
break;
}
} }
endusershell(); if (ENABLE_FEATURE_CLEAN_UP)
return 1; endusershell();
return result;
} }
#endif #endif
#define SU_OPT_mp (3) #define SU_OPT_mp (3)
#define SU_OPT_l (4) #define SU_OPT_l (4)
int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int su_main(int argc UNUSED_PARAM, char **argv) int su_main(int argc UNUSED_PARAM, char **argv)
@ -38,7 +42,8 @@ int su_main(int argc UNUSED_PARAM, char **argv)
struct passwd *pw; struct passwd *pw;
uid_t cur_uid = getuid(); uid_t cur_uid = getuid();
const char *tty; const char *tty;
char *old_user; char user_buf[64];
const char *old_user;
flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell); flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
//argc -= optind; //argc -= optind;
@ -56,21 +61,18 @@ int su_main(int argc UNUSED_PARAM, char **argv)
} }
if (ENABLE_FEATURE_SU_SYSLOG) { if (ENABLE_FEATURE_SU_SYSLOG) {
/* The utmp entry (via getlogin) is probably the best way to identify /* The utmp entry (via getlogin) is probably the best way to
* the user, especially if someone su's from a su-shell. * identify the user, especially if someone su's from a su-shell.
* But getlogin can fail -- usually due to lack of utmp entry. * But getlogin can fail -- usually due to lack of utmp entry.
* in this case resort to getpwuid. */ * in this case resort to getpwuid. */
const char *user;
#if ENABLE_FEATURE_UTMP #if ENABLE_FEATURE_UTMP
char user_buf[64]; old_user = user_buf;
user = user_buf;
if (getlogin_r(user_buf, sizeof(user_buf)) != 0) if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
#endif #endif
{ {
pw = getpwuid(cur_uid); pw = getpwuid(cur_uid);
user = pw ? pw->pw_name : ""; old_user = pw ? xstrdup(pw->pw_name) : "";
} }
old_user = xstrdup(user);
tty = xmalloc_ttyname(2); tty = xmalloc_ttyname(2);
if (!tty) { if (!tty) {
tty = "none"; tty = "none";
@ -80,13 +82,7 @@ int su_main(int argc UNUSED_PARAM, char **argv)
pw = xgetpwnam(opt_username); pw = xgetpwnam(opt_username);
/* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER if (cur_uid == 0 || correct_password(pw)) {
is a username that is retrieved via NIS (YP), but that doesn't have
a default shell listed. */
if (!pw->pw_shell || !pw->pw_shell[0])
pw->pw_shell = (char *)DEFAULT_SHELL;
if ((cur_uid == 0) || correct_password(pw)) {
if (ENABLE_FEATURE_SU_SYSLOG) if (ENABLE_FEATURE_SU_SYSLOG)
syslog(LOG_NOTICE, "%c %s %s:%s", syslog(LOG_NOTICE, "%c %s %s:%s",
'+', tty, old_user, opt_username); '+', tty, old_user, opt_username);
@ -99,21 +95,30 @@ int su_main(int argc UNUSED_PARAM, char **argv)
if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) { if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
closelog(); closelog();
free(old_user);
} }
if (!opt_shell && (flags & SU_OPT_mp)) if (!opt_shell && (flags & SU_OPT_mp)) {
/* -s SHELL is not given, but "preserve env" opt is */
opt_shell = getenv("SHELL"); opt_shell = getenv("SHELL");
}
/* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
* is a username that is retrieved via NIS (YP), that doesn't have
* a default shell listed. */
if (!pw->pw_shell || !pw->pw_shell[0])
pw->pw_shell = (char *)DEFAULT_SHELL;
#if ENABLE_FEATURE_SU_CHECKS_SHELLS #if ENABLE_FEATURE_SU_CHECKS_SHELLS
if (opt_shell && cur_uid != 0 && restricted_shell(pw->pw_shell)) { if (opt_shell && cur_uid != 0 && restricted_shell(pw->pw_shell)) {
/* The user being su'd to has a nonstandard shell, and so is /* The user being su'd to has a nonstandard shell, and so is
probably a uucp account or has restricted access. Don't * probably a uucp account or has restricted access. Don't
compromise the account by allowing access with a standard * compromise the account by allowing access with a standard
shell. */ * shell. */
bb_error_msg("using restricted shell"); bb_error_msg("using restricted shell");
opt_shell = NULL; opt_shell = NULL;
} }
/* else: user can run whatever he wants via "su -s PROG USER".
* This is safe since PROG is run under user's uid/gid. */
#endif #endif
if (!opt_shell) if (!opt_shell)
opt_shell = pw->pw_shell; opt_shell = pw->pw_shell;

View File

@ -101,11 +101,9 @@ int sulogin_main(int argc UNUSED_PARAM, char **argv)
shell = getenv("SUSHELL"); shell = getenv("SUSHELL");
if (!shell) if (!shell)
shell = getenv("sushell"); shell = getenv("sushell");
if (!shell) { if (!shell)
shell = "/bin/sh"; shell = pwd->pw_shell;
if (pwd->pw_shell[0])
shell = pwd->pw_shell;
}
/* Exec login shell with no additional parameters. Never returns. */ /* Exec login shell with no additional parameters. Never returns. */
run_shell(shell, 1, NULL, NULL); run_shell(shell, 1, NULL, NULL);

View File

@ -354,7 +354,7 @@ static void processorstart(struct logdir *ld)
xmove_fd(fd, 5); xmove_fd(fd, 5);
// getenv("SHELL")? // getenv("SHELL")?
execl("/bin/sh", "/bin/sh" + 5, "-c", ld->processor, (char*) NULL); execl(DEFAULT_SHELL, DEFAULT_SHELL_SHORT_NAME, "-c", ld->processor, (char*) NULL);
bb_perror_msg_and_die(FATAL"can't %s processor %s", "run", ld->name); bb_perror_msg_and_die(FATAL"can't %s processor %s", "run", ld->name);
} }
ld->fnsave[26] = sv_ch; /* ...restore */ ld->fnsave[26] = sv_ch; /* ...restore */