2023-06-11 21:01:02 +03:00
|
|
|
#include <BAN/String.h>
|
|
|
|
#include <BAN/Optional.h>
|
|
|
|
#include <BAN/Vector.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2023-07-13 12:01:41 +03:00
|
|
|
#include <pwd.h>
|
2023-06-11 21:01:02 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-01-10 14:46:29 +02:00
|
|
|
#include <sys/banan-os.h>
|
2023-11-30 22:39:53 +02:00
|
|
|
#include <termios.h>
|
2023-06-11 21:01:02 +03:00
|
|
|
|
2024-11-17 22:38:52 +02:00
|
|
|
int main(int argc, char** argv)
|
2023-06-11 21:01:02 +03:00
|
|
|
{
|
2024-11-17 22:38:52 +02:00
|
|
|
ASSERT(argc == 2);
|
2023-06-11 21:01:02 +03:00
|
|
|
|
2024-11-17 22:38:52 +02:00
|
|
|
const char* tty_name = argv[1];
|
|
|
|
|
|
|
|
if (open(tty_name, O_RDONLY | O_TTY_INIT) != 0) _exit(1);
|
|
|
|
if (open(tty_name, O_WRONLY) != 1) _exit(1);
|
|
|
|
if (open(tty_name, O_WRONLY) != 2) _exit(1);
|
|
|
|
if (open("/dev/debug", O_WRONLY) != 3) _exit(1);
|
2023-06-11 21:01:02 +03:00
|
|
|
|
2023-07-28 18:10:36 +03:00
|
|
|
if (signal(SIGINT, [](int) {}) == SIG_ERR)
|
|
|
|
perror("signal");
|
|
|
|
|
2024-03-19 12:30:57 +02:00
|
|
|
if (load_keymap("/usr/share/keymaps/us.keymap") == -1)
|
2024-01-10 14:46:29 +02:00
|
|
|
perror("load_keymap");
|
|
|
|
|
2024-02-03 02:41:06 +02:00
|
|
|
if (fork() == 0)
|
|
|
|
{
|
|
|
|
execl("/bin/dhcp-client", "dhcp-client", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2024-02-08 03:14:00 +02:00
|
|
|
if (fork() == 0)
|
|
|
|
{
|
|
|
|
execl("/bin/resolver", "resolver", NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-06-12 00:46:07 +03:00
|
|
|
bool first = true;
|
|
|
|
|
2023-11-30 22:39:53 +02:00
|
|
|
termios termios;
|
|
|
|
tcgetattr(STDIN_FILENO, &termios);
|
|
|
|
|
2023-06-11 21:01:02 +03:00
|
|
|
while (true)
|
|
|
|
{
|
2023-11-30 22:39:53 +02:00
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &termios);
|
|
|
|
|
2023-06-11 21:01:02 +03:00
|
|
|
char name_buffer[128];
|
2023-06-12 00:46:07 +03:00
|
|
|
|
2023-07-13 12:01:41 +03:00
|
|
|
while (!first)
|
2023-06-11 21:01:02 +03:00
|
|
|
{
|
|
|
|
printf("username: ");
|
|
|
|
fflush(stdout);
|
|
|
|
|
2023-09-07 15:47:59 +03:00
|
|
|
ssize_t nread = read(STDIN_FILENO, name_buffer, sizeof(name_buffer) - 1);
|
|
|
|
if (nread == -1)
|
2023-06-11 21:01:02 +03:00
|
|
|
{
|
2023-09-07 15:47:59 +03:00
|
|
|
perror("read");
|
|
|
|
return 1;
|
2023-06-11 21:01:02 +03:00
|
|
|
}
|
2023-07-13 12:01:41 +03:00
|
|
|
if (nread <= 1 || name_buffer[nread - 1] != '\n')
|
|
|
|
continue;
|
|
|
|
name_buffer[nread - 1] = '\0';
|
2023-06-11 21:01:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-13 12:01:41 +03:00
|
|
|
if (first)
|
2023-06-11 21:01:02 +03:00
|
|
|
{
|
2023-07-13 12:01:41 +03:00
|
|
|
strcpy(name_buffer, "user");
|
|
|
|
first = false;
|
|
|
|
}
|
2023-06-11 21:01:02 +03:00
|
|
|
|
2023-07-13 12:01:41 +03:00
|
|
|
auto* pwd = getpwnam(name_buffer);
|
|
|
|
if (pwd == nullptr)
|
|
|
|
continue;
|
2023-06-11 22:32:51 +03:00
|
|
|
|
2024-12-02 05:03:07 +02:00
|
|
|
if (chown(tty_name, pwd->pw_uid, 0) == -1)
|
|
|
|
perror("chown");
|
|
|
|
|
2023-07-13 12:01:41 +03:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
2023-08-22 11:37:04 +03:00
|
|
|
pid_t pgrp = setpgrp();
|
|
|
|
if (tcsetpgrp(0, pgrp) == -1)
|
|
|
|
{
|
|
|
|
perror("tcsetpgrp");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-07-13 12:01:41 +03:00
|
|
|
printf("Welcome back %s!\n", pwd->pw_name);
|
2023-06-12 01:26:10 +03:00
|
|
|
|
2023-07-13 12:01:41 +03:00
|
|
|
if (setgid(pwd->pw_gid) == -1)
|
|
|
|
perror("setgid");
|
|
|
|
if (setuid(pwd->pw_uid) == -1)
|
|
|
|
perror("setuid");
|
2023-06-11 21:01:02 +03:00
|
|
|
|
2023-09-23 02:43:02 +03:00
|
|
|
setenv("PATH", "/bin:/usr/bin", 0);
|
|
|
|
|
2023-07-13 12:01:41 +03:00
|
|
|
setenv("HOME", pwd->pw_dir, 1);
|
|
|
|
chdir(pwd->pw_dir);
|
|
|
|
|
2024-10-04 17:24:37 +03:00
|
|
|
setenv("SHELL", pwd->pw_shell, 1);
|
2024-09-11 22:11:05 +03:00
|
|
|
char shell_path[PATH_MAX];
|
|
|
|
strcpy(shell_path, pwd->pw_shell);
|
|
|
|
|
|
|
|
endpwent();
|
|
|
|
|
|
|
|
execl(shell_path, shell_path, nullptr);
|
2023-07-13 12:01:41 +03:00
|
|
|
perror("execl");
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2023-07-13 12:01:41 +03:00
|
|
|
endpwent();
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2023-07-13 12:01:41 +03:00
|
|
|
if (pid == -1)
|
|
|
|
{
|
|
|
|
perror("fork");
|
|
|
|
break;
|
2023-06-11 21:01:02 +03:00
|
|
|
}
|
2023-07-28 18:10:36 +03:00
|
|
|
|
2023-07-13 12:01:41 +03:00
|
|
|
int status;
|
|
|
|
waitpid(pid, &status, 0);
|
2023-07-28 18:10:36 +03:00
|
|
|
|
2023-08-22 11:37:04 +03:00
|
|
|
if (tcsetpgrp(0, getpgrp()) == -1)
|
2023-07-28 18:10:36 +03:00
|
|
|
perror("tcsetpgrp");
|
2024-12-02 05:03:07 +02:00
|
|
|
|
|
|
|
if (chown(tty_name, 0, 0) == -1)
|
|
|
|
perror("chown");
|
2023-06-11 21:01:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|