Add /etc/passwd support to su(1)

This commit is contained in:
sin 2014-06-03 12:09:25 +01:00
parent e5d539a76f
commit bd4b6f4e47

64
su.c
View File

@ -58,42 +58,66 @@ main(int argc, char *argv[])
srand(time(NULL)); srand(time(NULL));
errno = 0; errno = 0;
spw = getspnam(usr); pw = getpwnam(usr);
if (errno) if (errno)
eprintf("getspnam: %s:", usr); eprintf("getpwnam: %s:", usr);
else if (!spw) else if (!pw)
eprintf("who are you?\n"); eprintf("who are you?\n");
switch (spw->sp_pwdp[0]) { switch (pw->pw_passwd[0]) {
case '!': case '!':
case '*': case '*':
eprintf("denied\n"); eprintf("denied\n");
} }
/* Empty password? Su now */
if (pw->pw_passwd[0] == '\0')
goto dosu;
uid = getuid(); uid = getuid();
if (uid) { if (uid) {
pass = getpass("Password: "); pass = getpass("Password: ");
if (!pass) if (!pass)
eprintf("getpass:"); 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);
} }
errno = 0; if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '\0') {
pw = getpwnam(usr); errno = 0;
if (errno) spw = getspnam(usr);
eprintf("getpwnam: %s", usr); if (errno)
else if (!pw) eprintf("getspnam: %s:", usr);
eprintf("who are you?\n"); else if (!spw)
eprintf("who are you?\n");
switch (spw->sp_pwdp[0]) {
case '!':
case '*':
eprintf("denied\n");
}
if (uid) {
cryptpass = crypt(pass, spw->sp_pwdp);
if (!cryptpass)
eprintf("crypt:");
if (strcmp(cryptpass, spw->sp_pwdp) != 0)
eprintf(randreply());
}
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");
}
}
if (uid) {
explicit_bzero(pass, strlen(pass));
explicit_bzero(cryptpass, strlen(cryptpass));
}
dosu:
if (initgroups(usr, pw->pw_gid) < 0) if (initgroups(usr, pw->pw_gid) < 0)
eprintf("initgroups:"); eprintf("initgroups:");
if (setgid(pw->pw_gid) < 0) if (setgid(pw->pw_gid) < 0)