From c13663104076a7780d1bbbaf77273416733cbb58 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Wed, 30 Sep 2009 22:31:56 +0800 Subject: [PATCH] Updated the CLI Shell to use the new path, adding login to git --- Usermode/Applications/CLIShell_src/main.c | 2 +- Usermode/Applications/login_src/Makefile | 33 +++++ .../Applications/login_src/database_tpl.c | 45 ++++++ Usermode/Applications/login_src/main.c | 128 ++++++++++++++++++ 4 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 Usermode/Applications/login_src/Makefile create mode 100644 Usermode/Applications/login_src/database_tpl.c create mode 100644 Usermode/Applications/login_src/main.c diff --git a/Usermode/Applications/CLIShell_src/main.c b/Usermode/Applications/CLIShell_src/main.c index 4fe8a4a2..dbe18c59 100644 --- a/Usermode/Applications/CLIShell_src/main.c +++ b/Usermode/Applications/CLIShell_src/main.c @@ -28,7 +28,7 @@ struct { {"colour", Command_Colour}, {"clear", Command_Clear}, {"cd", Command_Cd}, {"dir", Command_Dir} }; -static char *cDEFAULT_PATH[] = {"/Acess"}; +static char *cDEFAULT_PATH[] = {"/Acess/Bin"}; #define BUILTIN_COUNT (sizeof(cBUILTINS)/sizeof(cBUILTINS[0])) // ==== LOCAL VARIABLES ==== diff --git a/Usermode/Applications/login_src/Makefile b/Usermode/Applications/login_src/Makefile new file mode 100644 index 00000000..83d9d7a1 --- /dev/null +++ b/Usermode/Applications/login_src/Makefile @@ -0,0 +1,33 @@ +# +# +# + +DBTYPE = tpl + +CC = gcc +AS = nasm +LD = ld +RM = rm -f + +ASFLAGS = -felf +CPPFLAGS = -I../../include +CFLAGS = -fno-stack-protector -Wall -Werror -O3 $(CPPFLAGS) +LDFLAGS = -I/Acess/Libs/ld-acess.so -L../../Libraries ../../Libraries/crt0.o -lacess -lgcc -lc + +OBJ = main.o database_$(DBTYPE).o +BIN = ../login + +.PHONY: all clean + +all: $(BIN) + +clean: + $(RM) $(BIN) $(OBJ) + +$(BIN): $(OBJ) + $(LD) $(LDFLAGS) $(OBJ) -o $(BIN) + cp $(BIN) /mnt/AcessHDD/Acess2/SBin/ + objdump -d ../login > login.dsm + +%.o: %.c Makefile + $(CC) $(CFLAGS) -c $< -o $@ diff --git a/Usermode/Applications/login_src/database_tpl.c b/Usermode/Applications/login_src/database_tpl.c new file mode 100644 index 00000000..de903d59 --- /dev/null +++ b/Usermode/Applications/login_src/database_tpl.c @@ -0,0 +1,45 @@ +/* + * Acess 2 Login + */ +#include +#include +#include "header.h" + +// === CONSTANTS === + +// === PROTOTYPES === + int ValidateUser(char *Username, char *Password); +tUserInfo *GetUserInfo(int UID); + +// === GLOBALS === +tUserInfo gUserInfo; + +// === CODE === +/** + * \fn int ValidateUser(char *Username, char *Password) + * \brief Validates a users credentials + * \return UID on success, -1 on failure + */ +int ValidateUser(char *Username, char *Password) +{ + if(Username == NULL) return -1; + if(Password == NULL) return -1; + if(strcmp(Username, "root") == 0) return 0; + return -1; +} + +/** + * \fn void GetUserInfo(int UID) + * \brief Gets a users information + */ +tUserInfo *GetUserInfo(int UID) +{ + if(UID != 0) return NULL; + + gUserInfo.UID = 0; + gUserInfo.GID = 0; + gUserInfo.Shell = "/Acess/Bin/CLIShell"; + gUserInfo.Home = "/Acess/Root"; + return &gUserInfo; +} + diff --git a/Usermode/Applications/login_src/main.c b/Usermode/Applications/login_src/main.c new file mode 100644 index 00000000..54532837 --- /dev/null +++ b/Usermode/Applications/login_src/main.c @@ -0,0 +1,128 @@ +/* + * Acess 2 Login + */ +#include "header.h" + +// === CONSTANTS === +#define BUFLEN 1024 + +// === PROTOTYPES === +char *GetUsername(); +char *GetPassword(); + +// === CODE === +int main(int argc, char *argv[]) +{ + char *sUsername, *sPassword; + int pid, uid; + int status = 0; + tUserInfo *uinfo; + + putchar('\n'); + for(;;) + { + // Validate User + do { + sUsername = GetUsername(); + sPassword = GetPassword(); + } while( (uid = ValidateUser(sUsername, sPassword)) == -1 ); + putchar('\n'); + + // Get user information + uinfo = GetUserInfo(uid); + + // Create child process + pid = clone(CLONE_VM, 0); + // Error check + if(pid == -1) { + fprintf(stderr, "Unable to fork the login process!\n"); + return -1; + } + + // Spawn shell in a child process + if(pid == 0) + { + char *argv[2] = {uinfo->Shell, 0}; + char **envp = NULL; + setgid(uinfo->GID); + setuid(uid); + + execve(uinfo->Shell, argv, envp); + exit(-1); + } + + // Wait for child to terminate + waittid(pid, &status); + } + + return 0; +} + +/** + * \fn char *GetUsername() + */ +char *GetUsername() +{ + char ret[BUFLEN]; + int pos = 0; + char ch; + + // Prompt the user + printf("Username: "); + + // Read in text + while( (ch = fgetc(stdin)) != -1 && ch != '\n' ) + { + if(ch == '\b') { + pos --; + ret[pos] = '\0'; + } + else + ret[pos++] = ch; + + // Echo out to the screen + fputc(ch, stdout); + + if(pos == BUFLEN-1) break; + } + + // Finish String + ret[pos] = '\0'; + + printf("\n"); + return strdup(ret); +} + +/** + * \fn char *GetPassword() + */ +char *GetPassword() +{ + char ret[BUFLEN]; + int pos = 0; + char ch; + + // Prompt the user + printf("Password: "); + + // Read in text + while( (ch = fgetc(stdin)) != -1 && ch != '\n' ) + { + if(ch == '\b') { + pos --; + ret[pos] = '\0'; + } + else + ret[pos++] = ch; + + // Don't echo out to the screen + //fputc(stdout, ch); + + if(pos == BUFLEN-1) break; + } + + ret[pos] = '\0'; + + printf("\n"); + return strdup(ret); +} -- 2.20.1