ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit b8dbf05ce7aee91e1cf646840f7cc7a8c7827ba9
parent 2f10d16b9fc907a05a3b40573a614a561a40d68f
Author: sin <sin@2f30.org>
Date:   Tue,  3 Jun 2014 12:24:17 +0100

Simplify dologin() in su(1)

Exec the user's shell with -l to fake a login.

Diffstat:
Msu.c | 43++++++++++++-------------------------------
1 file changed, 12 insertions(+), 31 deletions(-)

diff --git a/su.c b/su.c @@ -16,7 +16,6 @@ extern char **environ; static const char *randreply(void); -static char *msetenv(const char *, const char *); static void dologin(struct passwd *); static void @@ -161,39 +160,21 @@ randreply(void) return replies[rand() % LEN(replies)]; } -static char * -msetenv(const char *name, const char *value) -{ - char *buf; - size_t sz; - - sz = strlen(name) + strlen(value) + 2; - buf = emalloc(sz); - snprintf(buf, sz, "%s=%s", name, value); - return buf; -} - static void dologin(struct passwd *pw) { - char shbuf[strlen(pw->pw_shell) + 1]; - char * const *newargv; - char * const *newenv; - - strcpy(shbuf, pw->pw_shell); - newargv = (char *const[]){shbuf, NULL}; - newenv = (char *const[]){ - msetenv("HOME", pw->pw_dir), - msetenv("SHELL", pw->pw_shell), - msetenv("USER", pw->pw_name), - msetenv("LOGNAME", pw->pw_name), - msetenv("TERM", getenv("TERM")), - msetenv("PATH", - strcmp(pw->pw_name, "root") == 0 ? - ENV_SUPATH : ENV_PATH), - NULL - }; + char *term = getenv("TERM"); + clearenv(); + setenv("HOME", pw->pw_dir, 1); + setenv("SHELL", pw->pw_shell, 1); + setenv("USER", pw->pw_name, 1); + setenv("LOGNAME", pw->pw_name, 1); + setenv("TERM", term ? term : "vt100", 1); + if (strcmp(pw->pw_name, "root") == 0) + setenv("PATH", ENV_SUPATH, 1); + else + setenv("PATH", ENV_PATH, 1); if (chdir(pw->pw_dir) < 0) eprintf("chdir %s:", pw->pw_dir); - execve(pw->pw_shell, newargv, newenv); + execlp(pw->pw_shell, pw->pw_shell, "-l", NULL); }