]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
uid: add login command with password authentication
authorTulio A M Mendes <[email protected]>
Thu, 11 Jun 2026 02:55:13 +0000 (23:55 -0300)
committerTulio A M Mendes <[email protected]>
Thu, 11 Jun 2026 02:55:13 +0000 (23:55 -0300)
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
user/cmds/login/Makefile [new file with mode: 0644]
user/cmds/login/login.c [new file with mode: 0644]

index cabf5453b7b7b59e6d1fe8be195eaf20862c461c..ed90dad486718f65e8a2a4540379631fc0c3d9ce 100644 (file)
--- 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 (file)
index 0000000..fc669af
--- /dev/null
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Copyright (c) 2018, Tulio A M Mendes <[email protected]>
+# 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 (file)
index 0000000..851247c
--- /dev/null
@@ -0,0 +1,114 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2018, Tulio A M Mendes <[email protected]>
+ * All rights reserved.
+ * See LICENSE for details.
+ *
+ * Source: https://github.com/tadryanom/AdrOS
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <pwd.h>
+#include <termios.h>
+#include <sys/types.h>
+
+/* 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;
+}