ubase

suckless linux base utils
git clone git://git.2f30.org/ubase.git
Log | Files | Refs | README | LICENSE

commit d745889805558b4e0d03d360d66b38c54a143520
parent 78192e87d95baa19c1810062c1d3335a0ad3285c
Author: sin <sin@2f30.org>
Date:   Mon Jun  9 12:58:40 +0100

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

Diffstat:
login.c | 4++--
su.c | 4++--
util/passwd.c | 35++++++++++++++++++++++++-----------
3 files changed, 28 insertions(+), 15 deletions(-)
diff --git a/login.c b/login.c @@ -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:"); diff --git a/su.c b/su.c @@ -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) diff --git a/util/passwd.c b/util/passwd.c @@ -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; }