wm: teppich

Download patch

ref: 39d47a954c12c1194e2394c0362b44fd646d85dc
parent: 4695d500412add95b4ca42cbb224bf15bf21873e
author: mkf <mkf@cloud9p.org>
date: Thu Dec 7 13:05:30 EST 2023

login: simplify, ask for username if three are none, use new APIs

--- a/cmd/login.c
+++ b/cmd/login.c
@@ -6,7 +6,7 @@
 
 #include <mem.h>
 
-char answers[32][256] =
+char answers[20][256] =
 {
 	"Authtication failure\n",
 
@@ -20,7 +20,7 @@
 	"Yawn...\n",
 	"Did you expected me to accept this?\n",
 	"Segmention fault\n",
-	"Segmention fault (core dumped)\n"
+	"Segmention fault (core dumped)\n",
 	"Divison by zero\n",
 	"General protection fault\n",
 	"login 6817489: suicide: invalid address 0x0/0 in sys call pc=0x40cf\n",
@@ -38,86 +38,48 @@
 static int
 usage()
 {
-	printf("usage: login username\n");
+	printf("usage: login [username]\n");
 	return USAGE;
 }
 
-static int
-checkpass(char *pass, user_t *u)
-{
-	if(u != nil && u->pass == adler32(pass))
-		return OK;
-	return INCORRECT_PASSWD;
-}
-
 int
 login_main(int argc, char **argv)
 {
-	int i;
-	char c, *p;
+	int uid;
+	int status;
+	int a; /* answer */
+	char *username, *pass;
 	ll_t *t;
-	user_t *u;
 
 
-	if(argc != 2)
+	if(argc > 2)
 		return usage();
 
-	u = nil;
-	t = users;
-	/* search for user */
-	while(t != nil)
+	username = malloc(USERNAME_MAX);
+	if(argc == 1)
 	{
-		u = (user_t*)t->val;
-		if(!strcmp(u->name, argv[1]))
-			break;
-		t = (ll_t*)t->next;
+		printf("Username: ");
+		scanf("%s", &username);
 	}
-	printf("Password: ");
-	i = 0;
-	p = malloc(PASSWD_MAX);
+	else
+		strncpy(username, argv[1], USERNAME_MAX);
 	
-	while(1)
-	{
-		
-		c = ps2_getc();
-		
-		if(c == '\r')
-		{
-			p[i] = '\0';
-			break;
-		}
-		else if(i >= PASSWD_MAX - 1) /* silently ignore */
-			continue;
-		else if(c == '\b')
-		{
-			if(i)
-				i--;
-			p[i] = '\0';
-			printf("\b");
-			continue;
-		}
+	uid = getuser(username); 
+	pass = malloc(PASSWD_MAX);
+	getpass("Password: ", pass);
 
-		p[i] = c;
-		i++;
-	}
-	printf("\n");
-	
+	status = setcuser(uid, pass);
 
-	if(i > PASSWD_MAX - 1)
-	{
-		printf("\noverflow!\n");
-		return OVERFLOW;
-	}
+	/* never is too late to mock your users */
+	a = (strlen(username) * (strlen(pass) * pass[strlen(pass-1)]) % (sizeof(answers) / sizeof(*answers)));
+	free(pass);
+	free(username);
 
-	if(checkpass(p, u) == OK)
+	if(status != OK)
 	{
-		cuser = *u;
-		return OK;
-	}
-	else
-	{
-		printf("%s", answers[i % (sizeof(answers) / sizeof(char*))]);
+		printf("%s", answers[a]);
 		return AUTH_FAILED;
 	}
+	return OK;
 }