Simplify dologin() in su(1)

Exec the user's shell with -l to fake a login.
This commit is contained in:
sin 2014-06-03 12:24:17 +01:00
parent 2f10d16b9f
commit b8dbf05ce7

43
su.c
View File

@ -16,7 +16,6 @@
extern char **environ; extern char **environ;
static const char *randreply(void); static const char *randreply(void);
static char *msetenv(const char *, const char *);
static void dologin(struct passwd *); static void dologin(struct passwd *);
static void static void
@ -161,39 +160,21 @@ randreply(void)
return replies[rand() % LEN(replies)]; 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 static void
dologin(struct passwd *pw) dologin(struct passwd *pw)
{ {
char shbuf[strlen(pw->pw_shell) + 1]; char *term = getenv("TERM");
char * const *newargv; clearenv();
char * const *newenv; setenv("HOME", pw->pw_dir, 1);
setenv("SHELL", pw->pw_shell, 1);
strcpy(shbuf, pw->pw_shell); setenv("USER", pw->pw_name, 1);
newargv = (char *const[]){shbuf, NULL}; setenv("LOGNAME", pw->pw_name, 1);
newenv = (char *const[]){ setenv("TERM", term ? term : "vt100", 1);
msetenv("HOME", pw->pw_dir), if (strcmp(pw->pw_name, "root") == 0)
msetenv("SHELL", pw->pw_shell), setenv("PATH", ENV_SUPATH, 1);
msetenv("USER", pw->pw_name), else
msetenv("LOGNAME", pw->pw_name), setenv("PATH", ENV_PATH, 1);
msetenv("TERM", getenv("TERM")),
msetenv("PATH",
strcmp(pw->pw_name, "root") == 0 ?
ENV_SUPATH : ENV_PATH),
NULL
};
if (chdir(pw->pw_dir) < 0) if (chdir(pw->pw_dir) < 0)
eprintf("chdir %s:", pw->pw_dir); eprintf("chdir %s:", pw->pw_dir);
execve(pw->pw_shell, newargv, newenv); execlp(pw->pw_shell, pw->pw_shell, "-l", NULL);
} }