28 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
29 |
*/ |
*/ |
30 |
|
|
|
#include <stdio.h> |
|
|
#include <errno.h> |
|
|
#include <unistd.h> |
|
|
#include <string.h> |
|
|
#include <stdlib.h> |
|
|
#include <syslog.h> |
|
|
#include <ctype.h> |
|
31 |
#include "libbb.h" |
#include "libbb.h" |
32 |
|
|
33 |
|
void FAST_FUNC setup_environment(const char *shell, int clear_env, int change_env, const struct passwd *pw) |
|
|
|
|
#define DEFAULT_LOGIN_PATH "/bin:/usr/bin" |
|
|
#define DEFAULT_ROOT_LOGIN_PATH "/usr/sbin:/bin:/usr/bin:/sbin" |
|
|
|
|
|
void setup_environment(const char *shell, int loginshell, int changeenv, const struct passwd *pw) |
|
34 |
{ |
{ |
35 |
if (loginshell) { |
/* Change the current working directory to be the home directory |
36 |
const char *term; |
* of the user */ |
37 |
|
if (chdir(pw->pw_dir)) { |
38 |
|
xchdir("/"); |
39 |
|
bb_error_msg("can't chdir to home directory '%s'", pw->pw_dir); |
40 |
|
} |
41 |
|
|
42 |
/* Change the current working directory to be the home directory |
if (clear_env) { |
43 |
* of the user. It is a fatal error for this process to be unable |
const char *term; |
|
* to change to that directory. There is no "default" home |
|
|
* directory. |
|
|
* Some systems default to HOME=/ |
|
|
*/ |
|
|
if (chdir(pw->pw_dir)) { |
|
|
xchdir("/"); |
|
|
fputs("warning: cannot change to home directory\n", stderr); |
|
|
} |
|
44 |
|
|
45 |
/* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH. |
/* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH. |
46 |
Unset all other environment variables. */ |
Unset all other environment variables. */ |
47 |
term = getenv("TERM"); |
term = getenv("TERM"); |
48 |
clearenv(); |
clearenv(); |
49 |
if (term) |
if (term) |
50 |
xsetenv("TERM", term); |
xsetenv("TERM", term); |
51 |
xsetenv("HOME", pw->pw_dir); |
xsetenv("PATH", (pw->pw_uid ? bb_default_path : bb_default_root_path)); |
52 |
xsetenv("SHELL", shell); |
goto shortcut; |
53 |
xsetenv("USER", pw->pw_name); |
// No, gcc (4.2.1) is not clever enougn to do it itself. |
54 |
xsetenv("LOGNAME", pw->pw_name); |
//xsetenv("USER", pw->pw_name); |
55 |
xsetenv("PATH", (pw->pw_uid ? DEFAULT_LOGIN_PATH : DEFAULT_ROOT_LOGIN_PATH)); |
//xsetenv("LOGNAME", pw->pw_name); |
56 |
|
//xsetenv("HOME", pw->pw_dir); |
57 |
|
//xsetenv("SHELL", shell); |
58 |
} |
} |
59 |
else if (changeenv) { |
else if (change_env) { |
60 |
/* Set HOME, SHELL, and if not becoming a super-user, |
/* Set HOME, SHELL, and if not becoming a super-user, |
61 |
USER and LOGNAME. */ |
USER and LOGNAME. */ |
|
xsetenv("HOME", pw->pw_dir); |
|
|
xsetenv("SHELL", shell); |
|
62 |
if (pw->pw_uid) { |
if (pw->pw_uid) { |
63 |
|
shortcut: |
64 |
xsetenv("USER", pw->pw_name); |
xsetenv("USER", pw->pw_name); |
65 |
xsetenv("LOGNAME", pw->pw_name); |
xsetenv("LOGNAME", pw->pw_name); |
66 |
} |
} |
67 |
|
xsetenv("HOME", pw->pw_dir); |
68 |
|
xsetenv("SHELL", shell); |
69 |
} |
} |
70 |
} |
} |