Browse Source

getty -a, login -f

K. Lange 4 years ago
parent
commit
70e4125032
2 changed files with 45 additions and 6 deletions
  1. 19 3
      apps/getty.c
  2. 26 3
      apps/login.c

+ 19 - 3
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 {

+ 26 - 3
apps/login.c

@@ -18,6 +18,7 @@
 #include <signal.h>
 #include <termios.h>
 #include <errno.h>
+#include <pwd.h>
 #include <sys/wait.h>
 #include <sys/utsname.h>
 
@@ -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();