Fix pw_check() semantics and style - it is now similar to pw_copy()

This commit is contained in:
sin 2014-06-09 12:58:40 +01:00
parent 78192e87d9
commit d745889805
3 changed files with 28 additions and 15 deletions

View File

@ -59,8 +59,8 @@ main(int argc, char *argv[])
pass = getpass("Password: "); putchar('\n'); pass = getpass("Password: "); putchar('\n');
if (!pass) if (!pass)
eprintf("getpass:"); eprintf("getpass:");
if (pw_check(pw, pass) == 0) if (pw_check(pw, pass) <= 0)
eprintf("incorrect password\n"); exit(EXIT_FAILURE);
if (initgroups(argv[0], gid) < 0) if (initgroups(argv[0], gid) < 0)
eprintf("initgroups:"); eprintf("initgroups:");

4
su.c
View File

@ -63,8 +63,8 @@ main(int argc, char *argv[])
pass = getpass("Password: "); putchar('\n'); pass = getpass("Password: "); putchar('\n');
if (!pass) if (!pass)
eprintf("getpass:"); eprintf("getpass:");
if (pw_check(pw, pass) == 0) if (pw_check(pw, pass) <= 0)
eprintf("incorrect password\n"); exit(EXIT_FAILURE);
} }
if (initgroups(usr, pw->pw_gid) < 0) if (initgroups(usr, pw->pw_gid) < 0)

View File

@ -10,6 +10,8 @@
#include "../text.h" #include "../text.h"
#include "../util.h" #include "../util.h"
/* Returns -1 on error, 0 for incorrect password
* and 1 if all went OK */
int int
pw_check(struct passwd *pw, const char *pass) pw_check(struct passwd *pw, const char *pass)
{ {
@ -17,8 +19,10 @@ pw_check(struct passwd *pw, const char *pass)
struct spwd *spw; struct spwd *spw;
p = pw->pw_passwd; p = pw->pw_passwd;
if (p[0] == '!' || p[0] == '*') if (p[0] == '!' || p[0] == '*') {
eprintf("denied\n"); weprintf("denied\n");
return -1;
}
if (pw->pw_passwd[0] == '\0') if (pw->pw_passwd[0] == '\0')
return pass[0] == '\0' ? 1 : 0; return pass[0] == '\0' ? 1 : 0;
@ -26,20 +30,29 @@ pw_check(struct passwd *pw, const char *pass)
if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '\0') { if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '\0') {
errno = 0; errno = 0;
spw = getspnam(pw->pw_name); spw = getspnam(pw->pw_name);
if (errno) if (errno) {
eprintf("getspnam: %:", pw->pw_name); weprintf("getspnam: %s:", pw->pw_name);
else if (!spw) return -1;
eprintf("who are you?\n"); } else if (!spw) {
weprintf("who are you?\n");
return -1;
}
p = spw->sp_pwdp; p = spw->sp_pwdp;
if (p[0] == '!' || p[0] == '*') if (p[0] == '!' || p[0] == '*') {
eprintf("denied\n"); weprintf("denied\n");
return -1;
}
} }
cryptpass = crypt(pass, p); cryptpass = crypt(pass, p);
if (!cryptpass) if (!cryptpass) {
eprintf("crypt:"); weprintf("crypt:");
if (strcmp(cryptpass, p) != 0) return -1;
}
if (strcmp(cryptpass, p) != 0) {
weprintf("incorrect password\n");
return 0; return 0;
}
return 1; return 1;
} }