From a2eb3f5291f32bcc9da2c4eddc1959233dd836ed Mon Sep 17 00:00:00 2001 From: Tulio A M Mendes Date: Wed, 10 Jun 2026 23:55:13 -0300 Subject: [PATCH] uid: add login command with password authentication Implement UID Infrastructure: login real (completed). Added user/cmds/login/login.c with: - Username prompt - Password prompt with echo disabled (termios) - Password verification using check_password() - UID/GID setting after successful authentication - Home directory change - Shell execution (from /etc/passwd or default /bin/sh) - Loop for retry on failed login Added user/cmds/login/Makefile. Updated Makefile to include login in USER_CMD_NAMES. Test: make test-battery PASS (157/157) --- Makefile | 2 +- user/cmds/login/Makefile | 12 +++++ user/cmds/login/login.c | 114 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 user/cmds/login/Makefile create mode 100644 user/cmds/login/login.c diff --git a/Makefile b/Makefile index cabf5453..ed90dad4 100644 --- a/Makefile +++ b/Makefile @@ -127,7 +127,7 @@ ifeq ($(ARCH),x86) grep id uname dmesg \ printenv tr dd pwd stat \ sed awk who top du find which \ - init + login init # ELF paths for dynamically-linked commands USER_CMD_ELFS := $(foreach cmd,$(USER_CMD_NAMES),$(USER_BUILD)/cmds/$(cmd)/$(cmd).elf) diff --git a/user/cmds/login/Makefile b/user/cmds/login/Makefile new file mode 100644 index 00000000..fc669af3 --- /dev/null +++ b/user/cmds/login/Makefile @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright (c) 2018, Tulio A M Mendes +# All rights reserved. +# See LICENSE for details. +# +# Source: https://github.com/tadryanom/AdrOS +# + +NAME := login +SRCS := login.c +include ../common.mk diff --git a/user/cmds/login/login.c b/user/cmds/login/login.c new file mode 100644 index 00000000..851247ce --- /dev/null +++ b/user/cmds/login/login.c @@ -0,0 +1,114 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2018, Tulio A M Mendes + * All rights reserved. + * See LICENSE for details. + * + * Source: https://github.com/tadryanom/AdrOS + */ + +#include +#include +#include +#include +#include +#include +#include + +/* Simple login command */ +int main(int argc, char** argv) { + (void)argc; + (void)argv; + + printf("AdrOS Login\n"); + + while (1) { + char username[64]; + char password[64]; + + /* Prompt for username */ + printf("login: "); + fflush(stdout); + + if (fgets(username, sizeof(username), stdin) == NULL) { + break; + } + + /* Remove trailing newline */ + size_t len = strlen(username); + if (len > 0 && username[len - 1] == '\n') { + username[len - 1] = '\0'; + } + + if (username[0] == '\0') { + continue; + } + + /* Prompt for password (no echo) */ + printf("Password: "); + fflush(stdout); + + /* Disable echo */ + struct termios oldt, newt; + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + + if (fgets(password, sizeof(password), stdin) == NULL) { + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + printf("\n"); + break; + } + + /* Restore echo */ + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + printf("\n"); + + /* Remove trailing newline */ + len = strlen(password); + if (len > 0 && password[len - 1] == '\n') { + password[len - 1] = '\0'; + } + + /* Verify password */ + if (check_password(username, password) == 0) { + /* Authentication successful */ + struct passwd* pw = getpwnam(username); + if (pw) { + printf("Welcome %s!\n", pw->pw_name); + + /* Set UID/GID */ + if (setgid(pw->pw_gid) < 0) { + perror("setgid"); + continue; + } + if (setuid(pw->pw_uid) < 0) { + perror("setuid"); + continue; + } + + /* Change to home directory */ + if (pw->pw_dir && pw->pw_dir[0] != '\0') { + chdir(pw->pw_dir); + } + + /* Execute shell */ + char* shell = pw->pw_shell; + if (!shell || shell[0] == '\0') { + shell = "/bin/sh"; + } + + execl(shell, shell, NULL); + perror("execl"); + exit(1); + } else { + printf("Login failed (user not found)\n"); + } + } else { + printf("Login incorrect\n"); + } + } + + return 1; +} -- 2.43.0