2023-04-09 09:24:34 +00:00
|
|
|
#include <bits/termios.h>
|
2023-04-08 11:50:18 +00:00
|
|
|
#include <os/ArgumentParser.h>
|
2023-04-08 14:31:33 +00:00
|
|
|
#include <pwd.h>
|
2023-04-08 11:50:18 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2023-04-09 09:24:34 +00:00
|
|
|
#include <sys/ioctl.h>
|
2023-04-08 11:50:18 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-04-09 09:24:34 +00:00
|
|
|
static struct termios orig;
|
|
|
|
|
|
|
|
void restore_terminal()
|
|
|
|
{
|
|
|
|
ioctl(fileno(stdin), TCSETS, &orig);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* getpass()
|
|
|
|
{
|
|
|
|
fputs("Password: ", stdout);
|
|
|
|
|
|
|
|
if (ioctl(fileno(stdin), TCGETS, &orig) < 0)
|
|
|
|
{
|
|
|
|
perror("ioctl(TCGETS)");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
atexit(restore_terminal);
|
|
|
|
|
|
|
|
struct termios tc = orig;
|
|
|
|
tc.c_lflag &= ~ECHO;
|
|
|
|
if (ioctl(fileno(stdin), TCSETS, &tc) < 0)
|
|
|
|
{
|
|
|
|
perror("ioctl(TCSETS)");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char buf[1024];
|
|
|
|
char* rc = fgets(buf, sizeof(buf), stdin);
|
|
|
|
|
|
|
|
restore_terminal();
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
perror("fgets");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* newline = strrchr(rc, '\n');
|
|
|
|
if (newline) *newline = 0;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2023-04-13 15:04:59 +00:00
|
|
|
Result<int> luna_main(int argc, char** argv)
|
2023-04-08 11:50:18 +00:00
|
|
|
{
|
2023-04-08 14:31:33 +00:00
|
|
|
StringView name;
|
2023-04-08 11:50:18 +00:00
|
|
|
|
|
|
|
if (geteuid() != 0)
|
|
|
|
{
|
2023-04-09 09:24:34 +00:00
|
|
|
fprintf(stderr, "su must be setuid root!\n");
|
2023-04-08 11:50:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
os::ArgumentParser parser;
|
2023-04-24 20:24:07 +00:00
|
|
|
parser.add_description("Switch to a different user (by default, root)."_sv);
|
2023-04-28 14:33:05 +00:00
|
|
|
parser.add_system_program_info("su"_sv);
|
2023-04-24 20:24:07 +00:00
|
|
|
parser.add_positional_argument(name, "name"_sv, "root"_sv);
|
2023-04-08 11:50:18 +00:00
|
|
|
parser.parse(argc, argv);
|
|
|
|
|
2023-04-08 14:31:33 +00:00
|
|
|
struct passwd* entry = getpwnam(name.chars());
|
|
|
|
if (!entry)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "su: user %s not found!\n", name.chars());
|
|
|
|
return 1;
|
|
|
|
}
|
2023-04-08 11:50:18 +00:00
|
|
|
|
2023-04-09 09:24:34 +00:00
|
|
|
if (getuid() != geteuid() && *entry->pw_passwd)
|
2023-04-08 11:50:18 +00:00
|
|
|
{
|
2023-04-09 09:24:34 +00:00
|
|
|
char* pass = getpass();
|
|
|
|
if (!pass) return 1;
|
|
|
|
|
|
|
|
if (strcmp(pass, entry->pw_passwd))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Wrong password!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(pass, 0, strlen(pass));
|
2023-04-08 11:50:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-08 14:31:33 +00:00
|
|
|
setgid(entry->pw_gid);
|
|
|
|
setuid(entry->pw_uid);
|
2023-04-08 11:50:18 +00:00
|
|
|
|
2023-04-11 20:15:46 +00:00
|
|
|
chdir(entry->pw_dir);
|
2023-04-30 12:46:34 +00:00
|
|
|
if (setenv("HOME", entry->pw_dir, 1) < 0)
|
|
|
|
{
|
|
|
|
perror("setenv");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-04-11 20:15:46 +00:00
|
|
|
|
2023-04-08 14:31:33 +00:00
|
|
|
execl(entry->pw_shell, entry->pw_shell, NULL);
|
2023-04-13 15:04:59 +00:00
|
|
|
|
2023-04-30 12:46:34 +00:00
|
|
|
perror("execl");
|
2023-04-13 15:04:59 +00:00
|
|
|
return 1;
|
2023-04-08 11:50:18 +00:00
|
|
|
}
|