[PATCH] Simplify login

From: FRIGN <dev_AT_frign.de>
Date: Mon, 2 Jun 2014 18:04:12 +0200

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
_AT_@ -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;
 
_AT_@ -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;
_AT_@ -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);
_AT_@ -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);
_AT_@ -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
--Multipart=_Mon__2_Jun_2014_18_08_12_+0200_t2eAcZnRiuY=tfHA--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon Jun 02 2014 - 18:24:03 CEST