mirror of git://git.suckless.org/ubase
Add /etc/passwd support to su(1)
This commit is contained in:
parent
e5d539a76f
commit
bd4b6f4e47
54
su.c
54
su.c
|
@ -57,6 +57,31 @@ main(int argc, char *argv[])
|
|||
|
||||
srand(time(NULL));
|
||||
|
||||
errno = 0;
|
||||
pw = getpwnam(usr);
|
||||
if (errno)
|
||||
eprintf("getpwnam: %s:", usr);
|
||||
else if (!pw)
|
||||
eprintf("who are you?\n");
|
||||
|
||||
switch (pw->pw_passwd[0]) {
|
||||
case '!':
|
||||
case '*':
|
||||
eprintf("denied\n");
|
||||
}
|
||||
|
||||
/* Empty password? Su now */
|
||||
if (pw->pw_passwd[0] == '\0')
|
||||
goto dosu;
|
||||
|
||||
uid = getuid();
|
||||
if (uid) {
|
||||
pass = getpass("Password: ");
|
||||
if (!pass)
|
||||
eprintf("getpass:");
|
||||
}
|
||||
|
||||
if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '\0') {
|
||||
errno = 0;
|
||||
spw = getspnam(usr);
|
||||
if (errno)
|
||||
|
@ -69,31 +94,30 @@ main(int argc, char *argv[])
|
|||
case '*':
|
||||
eprintf("denied\n");
|
||||
}
|
||||
|
||||
uid = getuid();
|
||||
if (uid) {
|
||||
pass = getpass("Password: ");
|
||||
if (!pass)
|
||||
eprintf("getpass:");
|
||||
|
||||
cryptpass = crypt(pass, spw->sp_pwdp);
|
||||
explicit_bzero(pass, strlen(pass));
|
||||
if (!cryptpass)
|
||||
eprintf("crypt:");
|
||||
|
||||
if (strcmp(cryptpass, spw->sp_pwdp) != 0)
|
||||
eprintf(randreply());
|
||||
explicit_bzero(cryptpass, strlen(cryptpass));
|
||||
}
|
||||
explicit_bzero(spw, sizeof *spw);
|
||||
} else {
|
||||
if (uid) {
|
||||
cryptpass = crypt(pass, pw->pw_passwd);
|
||||
if (!cryptpass)
|
||||
eprintf("crypt:");
|
||||
if (strcmp(cryptpass, pw->pw_passwd) != 0)
|
||||
eprintf("login failed\n");
|
||||
}
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
pw = getpwnam(usr);
|
||||
if (errno)
|
||||
eprintf("getpwnam: %s", usr);
|
||||
else if (!pw)
|
||||
eprintf("who are you?\n");
|
||||
if (uid) {
|
||||
explicit_bzero(pass, strlen(pass));
|
||||
explicit_bzero(cryptpass, strlen(cryptpass));
|
||||
}
|
||||
|
||||
dosu:
|
||||
if (initgroups(usr, pw->pw_gid) < 0)
|
||||
eprintf("initgroups:");
|
||||
if (setgid(pw->pw_gid) < 0)
|
||||
|
|
Loading…
Reference in New Issue