diff --git a/apps/getty.c b/apps/getty.c index d63c1205..ebd80943 100644 --- a/apps/getty.c +++ b/apps/getty.c @@ -12,14 +12,24 @@ int main(int argc, char * argv[]) { int fd_master, fd_slave, fd_serial; char * file = "/dev/ttyS0"; + char * user = NULL; if (getuid() != 0) { fprintf(stderr, "%s: only root can do that\n", argv[0]); return 1; } - if (argc > 1) { - file = argv[1]; + int opt; + while ((opt = getopt(argc, argv, "a:")) != -1) { + switch (opt) { + case 'a': + user = optarg; + break; + } + } + + if (optind < argc) { + file = argv[optind]; } openpty(&fd_master, &fd_slave, NULL, NULL, NULL); @@ -34,7 +44,13 @@ int main(int argc, char * argv[]) { system("ttysize -q"); - char * tokens[] = {"/bin/login",NULL}; + char * tokens[] = {"/bin/login",NULL,NULL,NULL}; + + if (user) { + tokens[1] = "-f"; + tokens[2] = user; + } + execvp(tokens[0], tokens); exit(1); } else { diff --git a/apps/login.c b/apps/login.c index 1fc358b5..77502754 100644 --- a/apps/login.c +++ b/apps/login.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -43,7 +44,29 @@ void sig_segv(int sig) { int main(int argc, char ** argv) { + char * user = NULL; int uid; + pid_t pid, f; + + int opt; + while ((opt = getopt(argc, argv, "f:")) != -1) { + switch (opt) { + case 'f': + user = optarg; + break; + } + } + + if (user) { + struct passwd * pw = getpwnam(user); + if (pw) { + uid = pw->pw_uid; + goto do_fork; + } else { + fprintf(stderr, "%s: no such user\n", argv[0]); + return 1; + } + } printf("\n"); system("uname -a"); @@ -115,9 +138,9 @@ int main(int argc, char ** argv) { system("cat /etc/motd"); - pid_t pid = getpid(); - - pid_t f = fork(); +do_fork: + pid = getpid(); + f = fork(); if (getpid() != pid) { setuid(uid); toaru_auth_set_vars();