su: do not change to home dir unless -l
authorPascal Bellard <pascal.bellard@ads-lu.com>
Tue, 12 Jun 2012 11:21:02 +0000 (13:21 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 12 Jun 2012 11:21:02 +0000 (13:21 +0200)
Signed-off-by: Pascal Bellard <pascal.bellard@ads-lu.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
include/libbb.h
libbb/setup_environment.c
libbb/xfuncs_printf.c
loginutils/su.c
networking/httpd.c

index 5e5c8c7e89bfd9a13991fa47cb938929cecd1b03..322a28cab0376ad4f9bc6a237a3d173101dec48e 100644 (file)
@@ -1286,6 +1286,7 @@ int sd_listen_fds(void);
 #define SETUP_ENV_CHANGEENV (1 << 0)
 #define SETUP_ENV_CLEARENV  (1 << 1)
 #define SETUP_ENV_TO_TMP    (1 << 2)
+#define SETUP_ENV_NO_CHDIR  (1 << 4)
 extern void setup_environment(const char *shell, int flags, const struct passwd *pw) FAST_FUNC;
 extern int correct_password(const struct passwd *pw) FAST_FUNC;
 /* Returns a malloced string */
index 73229ca6ceb410790c1843469aefeddd038704d5..4258656fea2212bc61dc32d57747b9c48d41d40e 100644 (file)
@@ -37,9 +37,11 @@ void FAST_FUNC setup_environment(const char *shell, int flags, const struct pass
 
        /* Change the current working directory to be the home directory
         * of the user */
-       if (chdir(pw->pw_dir)) {
-               xchdir((flags & SETUP_ENV_TO_TMP) ? "/tmp" : "/");
-               bb_error_msg("can't chdir to home directory '%s'", pw->pw_dir);
+       if (!(flags & SETUP_ENV_NO_CHDIR)) {
+               if (chdir(pw->pw_dir) != 0) {
+                       bb_error_msg("can't change directory to '%s'", pw->pw_dir);
+                       xchdir((flags & SETUP_ENV_TO_TMP) ? "/tmp" : "/");
+               }
        }
 
        if (flags & SETUP_ENV_CLEARENV) {
index d8a42ba0bccb00a49fd250606965402cd7830c21..05aa07ce80151077676aa072073f81fffc3dd4f9 100644 (file)
@@ -355,13 +355,13 @@ void FAST_FUNC xsetuid(uid_t uid)
 void FAST_FUNC xchdir(const char *path)
 {
        if (chdir(path))
-               bb_perror_msg_and_die("chdir(%s)", path);
+               bb_perror_msg_and_die("can't change directory to '%s'", path);
 }
 
 void FAST_FUNC xchroot(const char *path)
 {
        if (chroot(path))
-               bb_perror_msg_and_die("can't change root directory to %s", path);
+               bb_perror_msg_and_die("can't change root directory to '%s'", path);
        xchdir("/");
 }
 
index 57ea738f4cb5acad0d15213d16e56f1482df5842..2ec05e12552e04588cc92be1f45d9ef337fd1fb0 100644 (file)
@@ -131,7 +131,8 @@ int su_main(int argc UNUSED_PARAM, char **argv)
        change_identity(pw);
        setup_environment(opt_shell,
                        ((flags & SU_OPT_l) / SU_OPT_l * SETUP_ENV_CLEARENV)
-                       + (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV),
+                       + (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV)
+                       + (!(flags & SU_OPT_l) * SETUP_ENV_NO_CHDIR),
                        pw);
        IF_SELINUX(set_current_security_context(NULL);)
 
index 12218a0a32246bfe9c11547b8e859657d8ac4b6c..a942794f574648bac827d454ed3bb2c8b59a0312 100644 (file)
@@ -1414,7 +1414,7 @@ static void send_cgi_and_exit(
                if (script != url) { /* paranoia */
                        *script = '\0';
                        if (chdir(url + 1) != 0) {
-                               bb_perror_msg("chdir(%s)", url + 1);
+                               bb_perror_msg("can't change directory to '%s'", url + 1);
                                goto error_execing_cgi;
                        }
                        // not needed: *script = '/';