From b7541dfc74d2d549210aa0ad73e0536ca4818909 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Thu, 3 Dec 2015 21:25:33 -0500 Subject: better auth --- src/client.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 11 deletions(-) (limited to 'src/client.c') diff --git a/src/client.c b/src/client.c index e907172..a3cc191 100644 --- a/src/client.c +++ b/src/client.c @@ -13,15 +13,15 @@ void __attribute__((format(printf,1,2))) out(const char *fmt, ...) write(client_fd, buf, sizeof(buf)); } +#define BUFSZ 128 + char *client_read(void) { - static char buf[128]; - if(read(client_fd, buf, sizeof(buf) - 1) < 0) - { + char *buf = malloc(BUFSZ); + memset(buf, 0, BUFSZ); + if(read(client_fd, buf, BUFSZ - 1) < 0) error("lost connection"); - exit(EXIT_FAILURE); - } - buf[sizeof(buf) - 1] = '\0'; + buf[BUFSZ - 1] = '\0'; const unsigned char ctrlc[] = { 0xff, 0xf4, 0xff, 0xfd, 0x06 }; if(!memcmp(buf, ctrlc, sizeof(ctrlc))) @@ -34,14 +34,55 @@ void client_main(int fd, struct sockaddr_in *addr, int total) { client_fd = fd; - bool admin = false; - char *ip = inet_ntoa(addr->sin_addr); printf("New client %s\n", ip); printf("Total clients: %d\n", total); out("Hello %s.\n", ip); - out("Please authenticate to continue.\n"); - out("login: "); - char *input = client_read(); + out("Please authenticate to continue.\n\n"); + + int failures = 0; + + int authlevel; + + /* auth loop */ + while(1) + { + out("NetCosm login: "); + char *user = client_read(); + out("Password: "); + char *pass = client_read(); + printf("pass is %s\n", pass); + struct authinfo_t auth = auth_check(user, pass); + free(user); + free(pass); + authlevel = auth.authlevel; + if(auth.success) + { + out("Access Granted.\n\n"); + break; + } + else + { + out("Access Denied.\n\n"); + if(++failures >= MAX_FAILURES) + return; + } + } + + /* authenticated */ + while(1) + { + out(">> "); + char *cmd = client_read(); + char *tok = strtok(cmd, " \t\r\n"); + + if(!strcmp(tok, "USER")) + { + void change_user(const char *name2, const char *pass2, int level); + add_user("admin", "test", 0); + } + + free(cmd); + } } -- cgit v1.1