Simplify login

Remove some unnecessary local values, simplify the exec-call at the end
(we don't need the separate array) and print clearer and more consistent
error-messages.
This commit is contained in:
FRIGN 2014-06-02 18:04:12 +02:00 committed by sin
parent aaea1e9ff7
commit af65094dbe
1 changed files with 7 additions and 14 deletions

21
login.c
View File

@ -24,8 +24,6 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct passwd *pw; struct passwd *pw;
uid_t uid;
gid_t gid;
char *pass, *cryptpass; char *pass, *cryptpass;
int pflag = 0; int pflag = 0;
@ -50,15 +48,12 @@ main(int argc, char *argv[])
switch (pw->pw_passwd[0]) { switch (pw->pw_passwd[0]) {
case '!': case '!':
case '*': case '*':
eprintf("Denied\n"); eprintf("denied\n");
} }
if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '\0') if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '\0')
eprintf("no shadow support\n"); eprintf("no shadow support\n");
uid = pw->pw_uid;
gid = pw->pw_gid;
/* Empty password? Login now */ /* Empty password? Login now */
if (pw->pw_passwd[0] == '\0') if (pw->pw_passwd[0] == '\0')
goto login; goto login;
@ -75,14 +70,14 @@ main(int argc, char *argv[])
if (!cryptpass) if (!cryptpass)
eprintf("crypt:"); eprintf("crypt:");
if (strcmp(cryptpass, pw->pw_passwd) != 0) if (strcmp(cryptpass, pw->pw_passwd) != 0)
eprintf("oops\n"); eprintf("login failed\n");
login: login:
if (initgroups(argv[0], gid) < 0) if (initgroups(argv[0], pw->pw_gid) < 0)
eprintf("initgroups:"); eprintf("initgroups:");
if (setgid(gid) < 0) if (setgid(pw->pw_gid) < 0)
eprintf("setgid:"); eprintf("setgid:");
if (setuid(uid) < 0) if (setuid(pw->pw_uid) < 0)
eprintf("setuid:"); eprintf("setuid:");
return dologin(pw, pflag); return dologin(pw, pflag);
@ -91,8 +86,6 @@ login:
static int static int
dologin(struct passwd *pw, int preserve) dologin(struct passwd *pw, int preserve)
{ {
char *shell[] = { pw->pw_shell, pw->pw_shell, "-l", NULL };
if (preserve == 0) if (preserve == 0)
clearenv(); clearenv();
setenv("HOME", pw->pw_dir, 1); setenv("HOME", pw->pw_dir, 1);
@ -103,7 +96,7 @@ dologin(struct passwd *pw, int preserve)
ENV_SUPATH : ENV_PATH, 1); ENV_SUPATH : ENV_PATH, 1);
if (chdir(pw->pw_dir) < 0) if (chdir(pw->pw_dir) < 0)
eprintf("chdir %s:", pw->pw_dir); eprintf("chdir %s:", pw->pw_dir);
execvp(shell[0], shell + 1); execlp(pw->pw_shell, pw->pw_shell, "-l", NULL);
weprintf("execvp %s:", shell[0]); weprintf("execvp %s:", pw->pw_shell);
return (errno == ENOENT) ? 127 : 126; return (errno == ENOENT) ? 127 : 126;
} }