2013-10-17 22:02:55 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <sys/types.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
2013-10-17 22:02:55 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <pwd.h>
|
2013-10-17 22:02:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-10-19 18:07:30 +00:00
|
|
|
#include "config.h"
|
2014-06-30 18:03:41 +00:00
|
|
|
#include "passwd.h"
|
2013-10-17 22:02:55 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-10-18 09:33:02 +00:00
|
|
|
extern char **environ;
|
|
|
|
|
2014-06-03 11:29:16 +00:00
|
|
|
static int dologin(struct passwd *);
|
2013-10-18 10:14:36 +00:00
|
|
|
|
2013-10-17 22:02:55 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-06-09 15:40:00 +00:00
|
|
|
eprintf("usage: %s [-lp] [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-18 13:26:14 +00:00
|
|
|
static int pflag = 0;
|
2013-10-18 09:33:02 +00:00
|
|
|
|
2013-10-17 22:02:55 +00:00
|
|
|
int
|
2014-04-18 10:49:10 +00:00
|
|
|
main(int argc, char *argv[])
|
2013-10-17 22:02:55 +00:00
|
|
|
{
|
2014-06-09 09:55:41 +00:00
|
|
|
char *usr = "root", *pass;
|
2014-06-05 11:12:18 +00:00
|
|
|
char *shell;
|
2013-10-17 22:02:55 +00:00
|
|
|
struct passwd *pw;
|
2014-06-05 10:28:08 +00:00
|
|
|
char *newargv[2];
|
2013-10-17 22:02:55 +00:00
|
|
|
uid_t uid;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
2013-10-18 09:33:02 +00:00
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
2013-10-18 13:26:14 +00:00
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
2013-10-17 22:02:55 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc < 1)
|
2014-02-14 13:49:34 +00:00
|
|
|
;
|
2013-10-17 22:02:55 +00:00
|
|
|
else if (argc == 1)
|
|
|
|
usr = argv[0];
|
|
|
|
else
|
|
|
|
usage();
|
|
|
|
|
2013-10-18 09:20:13 +00:00
|
|
|
errno = 0;
|
2014-06-03 11:09:25 +00:00
|
|
|
pw = getpwnam(usr);
|
2014-07-09 15:39:32 +00:00
|
|
|
if (!pw) {
|
2014-07-09 14:56:02 +00:00
|
|
|
if (errno)
|
|
|
|
eprintf("getpwnam: %s:", usr);
|
|
|
|
else
|
|
|
|
eprintf("who are you?\n");
|
|
|
|
}
|
2013-10-17 22:02:55 +00:00
|
|
|
|
2014-02-14 13:49:34 +00:00
|
|
|
uid = getuid();
|
2013-10-17 22:02:55 +00:00
|
|
|
if (uid) {
|
2014-06-09 09:55:41 +00:00
|
|
|
pass = getpass("Password: "); putchar('\n');
|
2013-10-17 22:02:55 +00:00
|
|
|
if (!pass)
|
|
|
|
eprintf("getpass:");
|
2014-06-09 11:58:40 +00:00
|
|
|
if (pw_check(pw, pass) <= 0)
|
|
|
|
exit(EXIT_FAILURE);
|
2014-06-03 11:09:25 +00:00
|
|
|
}
|
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 10:14:36 +00:00
|
|
|
if (lflag) {
|
2014-06-03 11:29:16 +00:00
|
|
|
return dologin(pw);
|
2013-10-18 10:14:36 +00:00
|
|
|
} else {
|
2014-06-05 11:12:18 +00:00
|
|
|
shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell;
|
|
|
|
newargv[0] = shell;
|
2014-06-05 10:28:08 +00:00
|
|
|
newargv[1] = NULL;
|
2013-10-18 13:26:14 +00:00
|
|
|
if (!pflag) {
|
|
|
|
setenv("HOME", pw->pw_dir, 1);
|
2014-06-05 11:12:18 +00:00
|
|
|
setenv("SHELL", shell, 1);
|
2013-10-18 13:26:14 +00:00
|
|
|
if (strcmp(pw->pw_name, "root") != 0) {
|
|
|
|
setenv("USER", pw->pw_name, 1);
|
|
|
|
setenv("LOGNAME", pw->pw_name, 1);
|
|
|
|
}
|
2013-10-18 10:20:59 +00:00
|
|
|
}
|
2013-10-19 18:07:30 +00:00
|
|
|
if (strcmp(pw->pw_name, "root") == 0)
|
|
|
|
setenv("PATH", ENV_SUPATH, 1);
|
|
|
|
else
|
|
|
|
setenv("PATH", ENV_PATH, 1);
|
2014-06-05 11:12:18 +00:00
|
|
|
execve(pflag ? getenv("SHELL") : shell,
|
2013-10-18 15:22:24 +00:00
|
|
|
newargv, environ);
|
2014-06-05 11:12:18 +00:00
|
|
|
weprintf("execve %s:", shell);
|
2014-06-03 11:29:16 +00:00
|
|
|
return (errno == ENOENT) ? 127 : 126;
|
2013-10-18 10:14:36 +00:00
|
|
|
}
|
2014-06-03 11:29:16 +00:00
|
|
|
return EXIT_SUCCESS;
|
2013-10-17 22:02:55 +00:00
|
|
|
}
|
2013-10-18 10:14:36 +00:00
|
|
|
|
2014-06-03 11:29:16 +00:00
|
|
|
static int
|
2013-10-18 10:14:36 +00:00
|
|
|
dologin(struct passwd *pw)
|
|
|
|
{
|
2014-06-05 11:12:18 +00:00
|
|
|
char *shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell;
|
2014-06-03 11:24:17 +00:00
|
|
|
char *term = getenv("TERM");
|
|
|
|
clearenv();
|
|
|
|
setenv("HOME", pw->pw_dir, 1);
|
2014-06-05 11:12:18 +00:00
|
|
|
setenv("SHELL", shell, 1);
|
2014-06-03 11:24:17 +00:00
|
|
|
setenv("USER", pw->pw_name, 1);
|
|
|
|
setenv("LOGNAME", pw->pw_name, 1);
|
2014-06-03 17:19:31 +00:00
|
|
|
setenv("TERM", term ? term : "linux", 1);
|
2014-06-03 11:24:17 +00:00
|
|
|
if (strcmp(pw->pw_name, "root") == 0)
|
|
|
|
setenv("PATH", ENV_SUPATH, 1);
|
|
|
|
else
|
|
|
|
setenv("PATH", ENV_PATH, 1);
|
2013-10-18 10:14:36 +00:00
|
|
|
if (chdir(pw->pw_dir) < 0)
|
|
|
|
eprintf("chdir %s:", pw->pw_dir);
|
2014-06-05 11:12:18 +00:00
|
|
|
execlp(shell, shell, "-l", NULL);
|
|
|
|
weprintf("execlp %s:", shell);
|
2014-06-03 11:29:16 +00:00
|
|
|
return (errno == ENOENT) ? 127 : 126;
|
2013-10-18 10:14:36 +00:00
|
|
|
}
|