2023-06-11 21:01:02 +03:00
|
|
|
#include <BAN/String.h>
|
|
|
|
#include <BAN/Optional.h>
|
|
|
|
#include <BAN/Vector.h>
|
|
|
|
|
|
|
|
#include <ctype.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>
|
2023-11-30 22:39:53 +02:00
|
|
|
#include <termios.h>
|
2023-06-11 21:01:02 +03:00
|
|
|
|
|
|
|
void initialize_stdio()
|
|
|
|
{
|
2023-09-05 00:59:29 +03:00
|
|
|
const char* tty = "/dev/tty";
|
2023-08-22 11:37:04 +03:00
|
|
|
if (open(tty, O_RDONLY | O_TTY_INIT) != 0) _exit(1);
|
2023-06-11 21:01:02 +03:00
|
|
|
if (open(tty, O_WRONLY) != 1) _exit(1);
|
|
|
|
if (open(tty, O_WRONLY) != 2) _exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
initialize_stdio();
|
|
|
|
|
2023-07-28 18:10:36 +03:00
|
|
|
if (signal(SIGINT, [](int) {}) == SIG_ERR)
|
|
|
|
perror("signal");
|
|
|
|
|
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-01-02 22:19:38 +02:00
|
|
|
if (chown("/dev/tty", pwd->pw_uid, pwd->pw_gid) == -1)
|
|
|
|
{
|
|
|
|
perror("chown");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chmod("/dev/tty", 0600) == -1)
|
|
|
|
{
|
|
|
|
perror("chmod");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
execl(pwd->pw_shell, pwd->pw_shell, nullptr);
|
|
|
|
perror("execl");
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
endpwent();
|
|
|
|
|
|
|
|
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");
|
2023-06-11 21:01:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|