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');
if (!pass)
eprintf("getpass:");
if (pw_check(pw, pass) == 0)
eprintf("incorrect password\n");
if (pw_check(pw, pass) <= 0)
exit(EXIT_FAILURE);
if (initgroups(argv[0], gid) < 0)
eprintf("initgroups:");

4
su.c
View File

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

View File

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