Compare commits

...

3 Commits

Author SHA1 Message Date
88180b34bd
initrd: Show default credentials in the motd
All checks were successful
continuous-integration/drone/push Build is passing
2023-05-06 22:12:06 +02:00
b08ebdc3cb
libos: Show "Options:" label even if --help and --version are the only
options
2023-05-06 22:05:08 +02:00
cd86d1d6d0
apps+initrd: Add a login utility and make it run at startup 2023-05-06 22:03:50 +02:00
6 changed files with 58 additions and 7 deletions

View File

@ -22,3 +22,4 @@ luna_app(rm.cpp rm)
luna_app(stat.cpp stat)
luna_app(uname.cpp uname)
luna_app(base64.cpp base64)
luna_app(login.cpp login)

45
apps/login.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <luna/String.h>
#include <os/ArgumentParser.h>
#include <os/File.h>
#include <stdio.h>
#include <unistd.h>
Result<int> luna_main(int argc, char** argv)
{
StringView username;
os::ArgumentParser parser;
parser.add_description("Begin a new session on the system.");
parser.add_system_program_info("login"_sv);
parser.add_positional_argument(username, "user", false);
parser.parse(argc, argv);
if (geteuid() != 0)
{
os::eprintln("error: login must be run as root.");
return 1;
}
putchar('\n');
String name;
if (username.is_empty())
{
auto input = os::File::standard_input();
os::print("Username: ");
name = TRY(input->read_line());
name.trim("\n");
if (name.is_empty()) return 0;
username = name.view();
}
execl("/bin/su", "login", "-p", "--", username.chars(), nullptr);
perror("su");
return 1;
}

View File

@ -54,10 +54,11 @@ char* getpass()
Result<int> luna_main(int argc, char** argv)
{
StringView name;
bool prompt_password;
if (geteuid() != 0)
{
fprintf(stderr, "su must be setuid root!\n");
fprintf(stderr, "%s must be setuid root!\n", argv[0]);
return 1;
}
@ -65,23 +66,24 @@ Result<int> luna_main(int argc, char** argv)
parser.add_description("Switch to a different user (by default, root)."_sv);
parser.add_system_program_info("su"_sv);
parser.add_positional_argument(name, "name"_sv, "root"_sv);
parser.add_switch_argument(prompt_password, 'p', "prompt", "prompt for a password even if running as root");
parser.parse(argc, argv);
struct passwd* entry = getpwnam(name.chars());
if (!entry)
{
fprintf(stderr, "su: user %s not found!\n", name.chars());
fprintf(stderr, "%s: user %s not found!\n", argv[0], name.chars());
return 1;
}
if (getuid() != geteuid() && *entry->pw_passwd)
if ((prompt_password || getuid() != geteuid()) && *entry->pw_passwd)
{
char* pass = getpass();
if (!pass) return 1;
if (strcmp(pass, entry->pw_passwd))
{
fprintf(stderr, "Wrong password!\n");
fprintf(stderr, "%s: wrong password!\n", argv[0]);
return 1;
}

View File

@ -1,4 +1,4 @@
Name=shell
Command=/bin/sh
Name=login
Command=/bin/login
Restart=true
Environment=PATH=/bin:/sbin

View File

@ -1,2 +1,4 @@
Welcome to the Luna system!
Have a look around, you can run 'ls /bin' to list available commands.
You can use the username 'selene' with the password 'moon' to start a user session.

View File

@ -275,7 +275,8 @@ namespace os
if (!m_description.is_empty()) { puts(m_description.chars()); }
if (m_switch_args.size() || m_value_args.size())
if (m_switch_args.size() || m_value_args.size() || m_add_long_help_flag || m_add_long_version_flag ||
m_add_short_help_flag || m_add_short_version_flag)
{
putchar('\n');
puts("Options:");