Hello, I was very glad to see the initial implementation of login by sin, but found some stuff that simplifies the code even more. Check the patch for more details, it should speak for itself.
Cheers FRIGN -- FRIGN <[email protected]>
>From b39d86ebdaadfe4880fd1681bd021ea06ecadb75 Mon Sep 17 00:00:00 2001 From: FRIGN <[email protected]> Date: Mon, 2 Jun 2014 18:04:12 +0200 Subject: [PATCH] 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. --- login.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/login.c b/login.c index 3adf188..40140ab 100644 --- a/login.c +++ b/login.c @@ -24,8 +24,6 @@ int main(int argc, char *argv[]) { struct passwd *pw; - uid_t uid; - gid_t gid; char *pass, *cryptpass; int pflag = 0; @@ -50,15 +48,12 @@ main(int argc, char *argv[]) switch (pw->pw_passwd[0]) { case '!': case '*': - eprintf("Denied\n"); + eprintf("denied\n"); } if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '\0') eprintf("no shadow support\n"); - uid = pw->pw_uid; - gid = pw->pw_gid; - /* Empty password? Login now */ if (pw->pw_passwd[0] == '\0') goto login; @@ -75,14 +70,14 @@ main(int argc, char *argv[]) if (!cryptpass) eprintf("crypt:"); if (strcmp(cryptpass, pw->pw_passwd) != 0) - eprintf("oops\n"); + eprintf("login failed\n"); login: - if (initgroups(argv[0], gid) < 0) + if (initgroups(argv[0], pw->pw_gid) < 0) eprintf("initgroups:"); - if (setgid(gid) < 0) + if (setgid(pw->pw_gid) < 0) eprintf("setgid:"); - if (setuid(uid) < 0) + if (setuid(pw->pw_uid) < 0) eprintf("setuid:"); return dologin(pw, pflag); @@ -91,8 +86,6 @@ login: static int dologin(struct passwd *pw, int preserve) { - char *shell[] = { pw->pw_shell, pw->pw_shell, "-l", NULL }; - if (preserve == 0) clearenv(); setenv("HOME", pw->pw_dir, 1); @@ -103,7 +96,7 @@ dologin(struct passwd *pw, int preserve) ENV_SUPATH : ENV_PATH, 1); if (chdir(pw->pw_dir) < 0) eprintf("chdir %s:", pw->pw_dir); - execvp(shell[0], shell + 1); - weprintf("execvp %s:", shell[0]); + execlp(pw->pw_shell, pw->pw_shell, "-l", NULL); + weprintf("execvp %s:", pw->pw_shell); return (errno == ENOENT) ? 127 : 126; } -- 1.8.5.5
