[git commit master 1/1] *: s/"/bin/sh"/DEFAULT_SHELL, run_shell() API fix, remove unneeded strdup

Denys Vlasenko vda.linux at googlemail.com
Sun Jun 27 01:23:31 UTC 2010


commit: http://git.busybox.net/busybox/commit/?id=a73b87e9343df2a6f14e328a977e7b70eb3ed707
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

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 at seznam.cz>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 archival/libunarchive/data_extract_to_command.c |    4 +-
 include/libbb.h                                 |    7 +--
 libbb/run_shell.c                               |   18 ++++---
 libbb/setup_environment.c                       |    4 +-
 loginutils/login.c                              |   12 ++--
 loginutils/su.c                                 |   57 ++++++++++++----------
 loginutils/sulogin.c                            |    8 +--
 runit/svlogd.c                                  |    2 +-
 8 files changed, 58 insertions(+), 54 deletions(-)

diff --git a/archival/libunarchive/data_extract_to_command.c b/archival/libunarchive/data_extract_to_command.c
index 53a7217..eb09439 100644
--- a/archival/libunarchive/data_extract_to_command.c
+++ b/archival/libunarchive/data_extract_to_command.c
@@ -102,8 +102,8 @@ void FAST_FUNC data_extract_to_command(archive_handle_t *archive_handle)
 			close(p[1]);
 			xdup2(p[0], STDIN_FILENO);
 			signal(SIGPIPE, SIG_DFL);
-			execl("/bin/sh", "/bin/sh" + 5, "-c", archive_handle->tar__to_command, NULL);
-			bb_perror_msg_and_die("can't execute '%s'", "/bin/sh");
+			execl(DEFAULT_SHELL, DEFAULT_SHELL_SHORT_NAME, "-c", archive_handle->tar__to_command, NULL);
+			bb_perror_msg_and_die("can't execute '%s'", DEFAULT_SHELL);
 		}
 		close(p[0]);
 		/* Our caller is expected to do signal(SIGPIPE, SIG_IGN)
diff --git a/include/libbb.h b/include/libbb.h
index 58719a8..53b768d 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -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 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) FAST_FUNC;
 #if ENABLE_SELINUX
 extern void renew_current_security_context(void) 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.
  * If you change LIBBB_DEFAULT_LOGIN_SHELL,
  * 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[];
 /* "/bin/sh" */
-#define DEFAULT_SHELL     (bb_default_login_shell+1)
+#define DEFAULT_SHELL              (bb_default_login_shell+1)
 /* "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
 # define CURRENT_VC "/dev/vc/0"
diff --git a/libbb/run_shell.c b/libbb/run_shell.c
index 4608a24..4d92c3c 100644
--- a/libbb/run_shell.c
+++ b/libbb/run_shell.c
@@ -49,15 +49,14 @@ void FAST_FUNC set_current_security_context(security_context_t sid)
 
 #endif
 
-/* Run SHELL, or DEFAULT_SHELL if SHELL is empty.
-   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
-   arguments.  */
-
+/* Run SHELL, or DEFAULT_SHELL if SHELL is "" or NULL.
+ * 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
+ * arguments.  */
 void FAST_FUNC run_shell(const char *shell, int loginshell, const char *command, const char **additional_args)
 {
 	const char **args;
-	int argno = 1;
+	int argno;
 	int additional_args_cnt = 0;
 
 	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[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)
 		args[0] = xasprintf("-%s", args[0]);
-
+	argno = 1;
 	if (command) {
 		args[argno++] = "-c";
 		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] = NULL;
+
 #if ENABLE_SELINUX
 	if (current_sid)
 		setexeccon(current_sid);
diff --git a/libbb/setup_environment.c b/libbb/setup_environment.c
index 13e60d8..a95fbc5 100644
--- a/libbb/setup_environment.c
+++ b/libbb/setup_environment.c
@@ -43,7 +43,7 @@ void FAST_FUNC setup_environment(const char *shell, int flags, const struct pass
 		const char *term;
 
 		/* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH.
-		   Unset all other environment variables.  */
+		 * Unset all other environment variables.  */
 		term = getenv("TERM");
 		clearenv();
 		if (term)
@@ -57,7 +57,7 @@ void FAST_FUNC setup_environment(const char *shell, int flags, const struct pass
 		//xsetenv("SHELL",   shell);
 	} else if (flags & SETUP_ENV_CHANGEENV) {
 		/* Set HOME, SHELL, and if not becoming a super-user,
-		   USER and LOGNAME.  */
+		 * USER and LOGNAME.  */
 		if (pw->pw_uid) {
  shortcut:
 			xsetenv("USER",    pw->pw_name);
diff --git a/loginutils/login.c b/loginutils/login.c
index 078cd68..88ed0af 100644
--- a/loginutils/login.c
+++ b/loginutils/login.c
@@ -201,7 +201,7 @@ int login_main(int argc UNUSED_PARAM, char **argv)
 	};
 	char *fromhost;
 	char username[USERNAME_SIZE];
-	const char *tmp;
+	const char *shell;
 	int run_by_root;
 	unsigned opt;
 	int count = 0;
@@ -389,10 +389,10 @@ int login_main(int argc UNUSED_PARAM, char **argv)
 		run_login_script(pw, full_tty);
 
 	change_identity(pw);
-	tmp = pw->pw_shell;
-	if (!tmp || !*tmp)
-		tmp = DEFAULT_SHELL;
-	setup_environment(tmp,
+	shell = pw->pw_shell;
+	if (!shell || !shell[0])
+		shell = DEFAULT_SHELL;
+	setup_environment(shell,
 			(!(opt & LOGIN_OPT_p) * SETUP_ENV_CLEARENV) + SETUP_ENV_CHANGEENV,
 			pw);
 
@@ -427,7 +427,7 @@ int login_main(int argc UNUSED_PARAM, char **argv)
 	signal(SIGINT, SIG_DFL);
 
 	/* Exec login shell with no additional parameters */
-	run_shell(tmp, 1, NULL, NULL);
+	run_shell(shell, 1, NULL, NULL);
 
 	/* return EXIT_FAILURE; - not reached */
 }
diff --git a/loginutils/su.c b/loginutils/su.c
index af25655..9bae375 100644
--- a/loginutils/su.c
+++ b/loginutils/su.c
@@ -10,23 +10,27 @@
 
 #if ENABLE_FEATURE_SU_CHECKS_SHELLS
 /* 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)
 {
 	char *line;
+	int result = 1;
 
 	/*setusershell(); - getusershell does it itself*/
 	while ((line = getusershell()) != NULL) {
-		if (/* *line != '#' && */ strcmp(line, shell) == 0)
-			return 0;
+		if (/* *line != '#' && */ strcmp(line, shell) == 0) {
+			result = 0;
+			break;
+		}
 	}
-	endusershell();
-	return 1;
+	if (ENABLE_FEATURE_CLEAN_UP)
+		endusershell();
+	return result;
 }
 #endif
 
 #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 UNUSED_PARAM, char **argv)
@@ -38,7 +42,8 @@ int su_main(int argc UNUSED_PARAM, char **argv)
 	struct passwd *pw;
 	uid_t cur_uid = getuid();
 	const char *tty;
-	char *old_user;
+	char user_buf[64];
+	const char *old_user;
 
 	flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
 	//argc -= optind;
@@ -56,21 +61,18 @@ int su_main(int argc UNUSED_PARAM, char **argv)
 	}
 
 	if (ENABLE_FEATURE_SU_SYSLOG) {
-		/* The utmp entry (via getlogin) is probably the best way to identify
-		 * the user, especially if someone su's from a su-shell.
+		/* The utmp entry (via getlogin) is probably the best way to
+		 * identify the user, especially if someone su's from a su-shell.
 		 * But getlogin can fail -- usually due to lack of utmp entry.
 		 * in this case resort to getpwuid.  */
-		const char *user;
 #if ENABLE_FEATURE_UTMP
-		char user_buf[64];
-		user = user_buf;
+		old_user = user_buf;
 		if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
 #endif
 		{
 			pw = getpwuid(cur_uid);
-			user = pw ? pw->pw_name : "";
+			old_user = pw ? xstrdup(pw->pw_name) : "";
 		}
-		old_user = xstrdup(user);
 		tty = xmalloc_ttyname(2);
 		if (!tty) {
 			tty = "none";
@@ -80,13 +82,7 @@ int su_main(int argc UNUSED_PARAM, char **argv)
 
 	pw = xgetpwnam(opt_username);
 
-	/* Make sure pw->pw_shell is non-NULL.  It may be NULL when NEW_USER
-	   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 (cur_uid == 0 || correct_password(pw)) {
 		if (ENABLE_FEATURE_SU_SYSLOG)
 			syslog(LOG_NOTICE, "%c %s %s:%s",
 				'+', 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) {
 		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");
+	}
+
+	/* 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 (opt_shell && cur_uid != 0 && restricted_shell(pw->pw_shell)) {
 		/* The user being su'd to has a nonstandard shell, and so is
-		   probably a uucp account or has restricted access.  Don't
-		   compromise the account by allowing access with a standard
-		   shell.  */
+		 * probably a uucp account or has restricted access.  Don't
+		 * compromise the account by allowing access with a standard
+		 * shell.  */
 		bb_error_msg("using restricted shell");
 		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
 	if (!opt_shell)
 		opt_shell = pw->pw_shell;
diff --git a/loginutils/sulogin.c b/loginutils/sulogin.c
index 6e3d3b0..3516013 100644
--- a/loginutils/sulogin.c
+++ b/loginutils/sulogin.c
@@ -101,11 +101,9 @@ int sulogin_main(int argc UNUSED_PARAM, char **argv)
 	shell = getenv("SUSHELL");
 	if (!shell)
 		shell = getenv("sushell");
-	if (!shell) {
-		shell = "/bin/sh";
-		if (pwd->pw_shell[0])
-			shell = pwd->pw_shell;
-	}
+	if (!shell)
+		shell = pwd->pw_shell;
+
 	/* Exec login shell with no additional parameters. Never returns. */
 	run_shell(shell, 1, NULL, NULL);
 
diff --git a/runit/svlogd.c b/runit/svlogd.c
index fc8b4ab..9fe81b9 100644
--- a/runit/svlogd.c
+++ b/runit/svlogd.c
@@ -354,7 +354,7 @@ static void processorstart(struct logdir *ld)
 		xmove_fd(fd, 5);
 
 // 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);
 	}
 	ld->fnsave[26] = sv_ch; /* ...restore */
-- 
1.7.1



More information about the busybox-cvs mailing list