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
1 changed files with 44 additions and 20 deletions

54
su.c
View File

@ -57,6 +57,31 @@ main(int argc, char *argv[])
srand(time(NULL)); 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; errno = 0;
spw = getspnam(usr); spw = getspnam(usr);
if (errno) if (errno)
@ -69,31 +94,30 @@ main(int argc, char *argv[])
case '*': case '*':
eprintf("denied\n"); eprintf("denied\n");
} }
uid = getuid();
if (uid) { if (uid) {
pass = getpass("Password: ");
if (!pass)
eprintf("getpass:");
cryptpass = crypt(pass, spw->sp_pwdp); cryptpass = crypt(pass, spw->sp_pwdp);
explicit_bzero(pass, strlen(pass));
if (!cryptpass) if (!cryptpass)
eprintf("crypt:"); eprintf("crypt:");
if (strcmp(cryptpass, spw->sp_pwdp) != 0) if (strcmp(cryptpass, spw->sp_pwdp) != 0)
eprintf(randreply()); eprintf(randreply());
explicit_bzero(cryptpass, strlen(cryptpass)); }
explicit_bzero(spw, sizeof *spw); 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; if (uid) {
pw = getpwnam(usr); explicit_bzero(pass, strlen(pass));
if (errno) explicit_bzero(cryptpass, strlen(cryptpass));
eprintf("getpwnam: %s", usr); }
else if (!pw)
eprintf("who are you?\n");
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)