wm: teppich

Download patch

ref: 3943794e421f09cb4789173935dd066d615b676c
parent: ddde627e1dff1e967bf0e5f3eec2698366f65e16
author: mkf <mkf@cloud9p.org>
date: Thu Dec 7 13:02:11 EST 2023

user: make it functional

previously, users were in a messy state, their password could easily, etc
now it's a bit more better (while you still can poke memory addresses, you need to bruteforce it.)

add getadam, getcuser, setcuser functions,
add getuser, finduser, validatepass, validateuser
make users, cuser and adam static
bunch of other changes

--- a/include/user.h
+++ b/include/user.h
@@ -1,15 +1,17 @@
 #pragma once
 #define PASSWD_MAX 16
+#define USERNAME_MAX 8
 
 typedef struct
 {
-	uint8 id;
+	uint8 uid;
 	char *name;
 	long pass; /* adler32 hash */
 }user_t;
 
-extern ll_t *users;
-extern user_t cuser;
-extern user_t adam;
-
 void users_init(void);
+int getuser(char *username);
+char* getpass(char *prompt, char *user);
+int setcuser(uint8 uid, char *pass);
+user_t* getcuser(void);
+user_t* getadam(void);
--- a/pc/user.c
+++ b/pc/user.c
@@ -3,18 +3,19 @@
 #include <user.h>
 #include <mem.h>
 #include <err.h>
+#include <cons.h>
 
-user_t adam =
+static user_t *cuser;
+static ll_t *users;
+
+static user_t adam =
 {
-	.id = 1,
+	.uid = 1,
 	.name = "adam",
 };
 
-user_t cuser;
-ll_t *users;
-
 static int
-checkname(char *s)
+validatename(char *s)
 {
 	for(int i = 0 ; i < strlen(s) ; i++)
 	{
@@ -25,7 +26,7 @@
 }
 
 static int
-checkpass(char *s)
+validatepass(char *s)
 {
 	if(strlen(s) < PASSWD_MAX)
 		return INVALID_PASSWD;
@@ -33,10 +34,44 @@
 	return OK;
 }
 
+static user_t*
+finduser(int uid)
+{
+	ll_t *t;
+	user_t *u;
+
+	t = users;
+	/* search for user */
+	while(t != nil)
+	{
+		u = (user_t*)t->val;
+		if(u->uid == uid)
+			break;
+		t = (ll_t*)t->next;
+	}
+	/* if t is nil, then there was no users */
+	if(t == nil)
+		return nil;
+	else
+		return u;
+}
+
+static int
+checkpass(uint8 uid, char *pass)
+{
+	user_t *u;
+
+	u = finduser(uid);
+	if(u != nil && u->pass == adler32(pass))
+		return OK;
+
+	return INCORRECT_PASSWD;
+}
+
 void
 users_init(void)
 {
-	users = malloc(sizeof(ll_t*));
+	users = (ll_t*)malloc(sizeof(ll_t));
 	adam.pass = adler32("1234");
 	users->val = &adam;
 	users->next = nil;
@@ -48,10 +83,10 @@
 	ll_t *t;
 	user_t *u, *lastuser;
 
-	if(checkname(name) != OK)
+	if(validatename(name) != OK)
 		return nil;
 
-	if(checkpass(pass) != OK)
+	if(validatepass(pass) != OK)
 		return nil;
 
 	/* check if there are any duplicates */
@@ -66,7 +101,7 @@
 	lastuser = u; /* needed for uid */
 
 	u = (user_t*)malloc(sizeof(user_t));
-	u->id = lastuser->id + 1;
+	u->uid = lastuser->uid + 1;
 	u->name = name;
 	u->pass = adler32(pass);
 
@@ -94,4 +129,89 @@
 		return NO_SUCH_USER;
 
 	return lldel(t);
+}
+
+char*
+getpass(char *prompt, char *str)
+{
+	int i;
+	char c;
+
+	i = 0;
+	printf("%s", prompt);
+	while(1)
+	{
+		c = cons_read(defcons);
+		
+		if(c == '\r')
+		{
+			str[i] = '\0';
+			break;
+		}
+		else if(i >= PASSWD_MAX - 1) /* silently ignore */
+			continue;
+		else if(c == '\b')
+		{
+			if(i)
+				i--;
+			str[i] = '\0';
+			continue;
+		}
+
+		str[i] = c;
+		i++;
+	}
+	printf("\n");
+	return str;
+}
+
+int
+getuser(char *username)
+{
+	ll_t *t;
+	user_t *u;
+
+	t = users;
+	/* search for user */
+	while(t != nil)
+	{
+		u = (user_t*)t->val;
+		if(!strcmp(u->name, username))
+			break;
+		t = (ll_t*)t->next;
+	}
+	if(u == nil)
+		return NO_SUCH_USER;
+	else
+		return u->uid;
+}
+
+user_t*
+getcuser(void)
+{
+	user_t *u;
+
+	u = (user_t*)malloc(sizeof(user_t));
+	return memcpy(u, cuser, sizeof(user_t));
+}
+
+user_t*
+getadam(void)
+{
+	user_t *u;
+
+	u = (user_t*)malloc(sizeof(user_t));
+	return memcpy(u, &adam, sizeof(user_t));
+}
+
+int
+setcuser(uint8 uid, char *pass)
+{
+	if(checkpass(uid, pass) == OK)
+	{
+		/* finduser might return nil, but checkpass wont be OK before that happens */
+		cuser = finduser(uid);
+		return OK;
+	}
+	return INCORRECT_PASSWD;
 }