Browse Source

add logname, getlogin()

K. Lange 2 years ago
parent
commit
f3c800b791
7 changed files with 77 additions and 8 deletions
  1. 2 0
      apps/login.c
  2. 22 0
      apps/logname.c
  3. 1 0
      base/usr/include/pwd.h
  4. 2 1
      base/usr/include/sys/ioctl.h
  5. 1 0
      base/usr/include/unistd.h
  6. 15 7
      kernel/fs/tty.c
  7. 34 0
      libc/unistd/getlogin.c

+ 2 - 0
apps/login.c

@@ -21,6 +21,7 @@
 #include <pwd.h>
 #include <sys/wait.h>
 #include <sys/utsname.h>
+#include <sys/ioctl.h>
 
 #include <toaru/auth.h>
 
@@ -142,6 +143,7 @@ do_fork:
 	pid = getpid();
 	f = fork();
 	if (getpid() != pid) {
+		ioctl(STDIN_FILENO, IOCTLTTYLOGIN, &uid);
 		setuid(uid);
 		toaru_auth_set_vars();
 		char * args[] = {

+ 22 - 0
apps/logname.c

@@ -0,0 +1,22 @@
+/* vim: tabstop=4 shiftwidth=4 noexpandtab
+ * This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2018 K. Lange
+ *
+ * logname - Effectively the same as whoami, but for compliance
+ *           with POSIX, this uses getlogin().
+ */
+#include <unistd.h>
+#include <stdio.h>
+
+int main(int argc, char ** argv) {
+	char * name = getlogin();
+	if (!name) {
+		fprintf(stderr, "%s: failed to determine login name\n", argv[0]);
+		return 1;
+	}
+
+	fprintf(stdout, "%s\n", name);
+	return 0;
+}
+

+ 1 - 0
base/usr/include/pwd.h

@@ -1,5 +1,6 @@
 #pragma once
 
+#include <stdio.h>
 #include <sys/types.h>
 
 struct passwd {

+ 2 - 1
base/usr/include/sys/ioctl.h

@@ -8,7 +8,8 @@
 #define IOCTL_DTYPE_FILE     1
 #define IOCTL_DTYPE_TTY      2
 
-#define IOCTLTTYNAME 0x4F01
+#define IOCTLTTYNAME  0x4F01
+#define IOCTLTTYLOGIN 0x4F02
 
 #define IOCTL_PACKETFS_QUEUED 0x5050
 

+ 1 - 0
base/usr/include/unistd.h

@@ -67,6 +67,7 @@ extern char * ttyname(int fd);
 extern int utime(const char *filename, const struct utimbuf *times);
 extern int rmdir(const char *pathname); /* TODO  rm probably just works */
 extern int chown(const char * pathname, uid_t owner, gid_t group);
+extern char * getlogin(void);
 
 #define STDIN_FILENO 0
 #define STDOUT_FILENO 1

+ 15 - 7
kernel/fs/tty.c

@@ -198,42 +198,50 @@ int pty_ioctl(pty_t * pty, int request, void * argp) {
 			 */
 			return IOCTL_DTYPE_TTY;
 		case IOCTLTTYNAME:
-			if (!argp) return -1;
+			if (!argp) return -EINVAL;
 			validate(argp);
 			((char*)argp)[0] = '\0';
 			sprintf((char*)argp, "/dev/pts/%d", pty->name);
 			return 0;
+		case IOCTLTTYLOGIN:
+			/* Set the user id of the login user */
+			if (current_process->user != 0) return -EPERM;
+			if (!argp) return -EINVAL;
+			validate(argp);
+			pty->slave->uid = *(int*)argp;
+			pty->master->uid = *(int*)argp;
+			return 0;
 		case TIOCSWINSZ:
-			if (!argp) return -1;
+			if (!argp) return -EINVAL;
 			validate(argp);
 			memcpy(&pty->size, argp, sizeof(struct winsize));
 			/* TODO send sigwinch to fg_prog */
 			return 0;
 		case TIOCGWINSZ:
-			if (!argp) return -1;
+			if (!argp) return -EINVAL;
 			validate(argp);
 			memcpy(argp, &pty->size, sizeof(struct winsize));
 			return 0;
 		case TCGETS:
-			if (!argp) return -1;
+			if (!argp) return -EINVAL;
 			validate(argp);
 			memcpy(argp, &pty->tios, sizeof(struct termios));
 			return 0;
 		case TIOCSPGRP:
-			if (!argp) return -1;
+			if (!argp) return -EINVAL;
 			validate(argp);
 			pty->fg_proc = *(pid_t *)argp;
 			debug_print(NOTICE, "Setting PTY group to %d", pty->fg_proc);
 			return 0;
 		case TIOCGPGRP:
-			if (!argp) return -1;
+			if (!argp) return -EINVAL;
 			validate(argp);
 			*(pid_t *)argp = pty->fg_proc;
 			return 0;
 		case TCSETS:
 		case TCSETSW:
 		case TCSETSF:
-			if (!argp) return -1;
+			if (!argp) return -EINVAL;
 			validate(argp);
 			if (!(((struct termios *)argp)->c_lflag & ICANON) && (pty->tios.c_lflag & ICANON)) {
 				/* Switch out of canonical mode, the dump the input buffer */

+ 34 - 0
libc/unistd/getlogin.c

@@ -0,0 +1,34 @@
+#include <unistd.h>
+#include <pwd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+static char _name[64]; /* NAME_MAX ? */
+
+char * getlogin(void) {
+
+	int tty = STDIN_FILENO;
+	if (!isatty(tty)) {
+		tty = STDOUT_FILENO;
+		if (!isatty(tty)) {
+			tty = STDERR_FILENO;
+			if (!isatty(tty)) {
+				errno = ENOTTY;
+				return NULL;
+			}
+		}
+	}
+
+	/* Get the owner */
+	struct stat statbuf;
+	fstat(tty, &statbuf);
+
+	struct passwd * passwd = getpwuid(statbuf.st_uid);
+
+	if (!passwd) return NULL;
+	if (!passwd->pw_name) return NULL;
+
+	memcpy(_name, passwd->pw_name, strlen(passwd->pw_name));
+	return _name;
+}