2013-10-17 22:02:55 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <shadow.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
2013-10-18 09:33:02 +00:00
|
|
|
extern char **environ;
|
|
|
|
|
2013-10-17 22:02:55 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-10-18 09:33:02 +00:00
|
|
|
eprintf("usage: %s [-l] [username]\n", argv0);
|
2013-10-17 22:02:55 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 09:33:02 +00:00
|
|
|
static int lflag = 0;
|
|
|
|
|
2013-10-17 22:02:55 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *usr, *pass, *cryptpass;
|
2013-10-18 09:08:10 +00:00
|
|
|
char * const *newargv;
|
2013-10-17 22:02:55 +00:00
|
|
|
struct spwd *spw;
|
|
|
|
struct passwd *pw;
|
|
|
|
uid_t uid;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
2013-10-18 09:33:02 +00:00
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
2013-10-17 22:02:55 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
usr = "root";
|
|
|
|
else if (argc == 1)
|
|
|
|
usr = argv[0];
|
|
|
|
else
|
|
|
|
usage();
|
|
|
|
|
|
|
|
uid = getuid();
|
|
|
|
|
2013-10-18 09:20:13 +00:00
|
|
|
errno = 0;
|
2013-10-17 22:02:55 +00:00
|
|
|
spw = getspnam(usr);
|
2013-10-18 09:20:13 +00:00
|
|
|
if (errno)
|
2013-10-17 22:02:55 +00:00
|
|
|
eprintf("getspnam: %s:", usr);
|
2013-10-18 09:20:13 +00:00
|
|
|
else if (!spw)
|
|
|
|
enprintf(EXIT_FAILURE, "who are you?\n");
|
2013-10-17 22:02:55 +00:00
|
|
|
|
|
|
|
switch (spw->sp_pwdp[0]) {
|
|
|
|
case '!':
|
|
|
|
case '*':
|
|
|
|
enprintf(EXIT_FAILURE, "Denied\n");
|
|
|
|
case '$':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
enprintf(EXIT_FAILURE, "Invalid shadow record\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uid) {
|
|
|
|
pass = getpass("Password: ");
|
|
|
|
if (!pass)
|
|
|
|
eprintf("getpass:");
|
|
|
|
|
2013-10-18 09:25:39 +00:00
|
|
|
cryptpass = crypt(pass, spw->sp_pwdp);
|
|
|
|
for (i = 0; pass[i]; i++)
|
|
|
|
pass[i] = '\0';
|
|
|
|
if (!cryptpass)
|
|
|
|
eprintf("crypt:");
|
2013-10-17 22:02:55 +00:00
|
|
|
|
2013-10-18 09:25:39 +00:00
|
|
|
if (strcmp(cryptpass, spw->sp_pwdp) != 0)
|
|
|
|
enprintf(EXIT_FAILURE, "Denied\n");
|
|
|
|
}
|
2013-10-17 22:02:55 +00:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
pw = getpwnam(usr);
|
|
|
|
if (errno)
|
|
|
|
eprintf("getpwnam: %s", usr);
|
|
|
|
else if (!pw)
|
2013-10-18 09:20:13 +00:00
|
|
|
enprintf(EXIT_FAILURE, "who are you?\n");
|
2013-10-17 22:02:55 +00:00
|
|
|
|
|
|
|
if (initgroups(usr, pw->pw_gid) < 0)
|
|
|
|
eprintf("initgroups:");
|
|
|
|
if (setgid(pw->pw_gid) < 0)
|
|
|
|
eprintf("setgid:");
|
|
|
|
if (setuid(pw->pw_uid) < 0)
|
|
|
|
eprintf("setuid:");
|
|
|
|
|
2013-10-18 09:08:10 +00:00
|
|
|
newargv = (char *const[]){pw->pw_shell, NULL};
|
2013-10-17 22:02:55 +00:00
|
|
|
setenv("HOME", pw->pw_dir, 1);
|
|
|
|
execve(pw->pw_shell, newargv, environ);
|
|
|
|
return (errno == ENOENT) ? 127 : 126;
|
|
|
|
}
|