getty -a, login -f

This commit is contained in:
K. Lange 2018-08-13 13:30:12 +09:00
parent 3d584cb55b
commit 70e4125032
2 changed files with 45 additions and 6 deletions

View File

@ -12,14 +12,24 @@
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
int fd_master, fd_slave, fd_serial; int fd_master, fd_slave, fd_serial;
char * file = "/dev/ttyS0"; char * file = "/dev/ttyS0";
char * user = NULL;
if (getuid() != 0) { if (getuid() != 0) {
fprintf(stderr, "%s: only root can do that\n", argv[0]); fprintf(stderr, "%s: only root can do that\n", argv[0]);
return 1; return 1;
} }
if (argc > 1) { int opt;
file = argv[1]; 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); openpty(&fd_master, &fd_slave, NULL, NULL, NULL);
@ -34,7 +44,13 @@ int main(int argc, char * argv[]) {
system("ttysize -q"); 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); execvp(tokens[0], tokens);
exit(1); exit(1);
} else { } else {

View File

@ -18,6 +18,7 @@
#include <signal.h> #include <signal.h>
#include <termios.h> #include <termios.h>
#include <errno.h> #include <errno.h>
#include <pwd.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/utsname.h> #include <sys/utsname.h>
@ -43,7 +44,29 @@ void sig_segv(int sig) {
int main(int argc, char ** argv) { int main(int argc, char ** argv) {
char * user = NULL;
int uid; 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"); printf("\n");
system("uname -a"); system("uname -a");
@ -115,9 +138,9 @@ int main(int argc, char ** argv) {
system("cat /etc/motd"); system("cat /etc/motd");
pid_t pid = getpid(); do_fork:
pid = getpid();
pid_t f = fork(); f = fork();
if (getpid() != pid) { if (getpid() != pid) {
setuid(uid); setuid(uid);
toaru_auth_set_vars(); toaru_auth_set_vars();