aboutsummaryrefslogtreecommitdiff
path: root/src/client.c
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2015-12-03 21:25:33 -0500
committerFranklin Wei <git@fwei.tk>2015-12-03 21:25:33 -0500
commitb7541dfc74d2d549210aa0ad73e0536ca4818909 (patch)
treefbc2083fe99dffe5085f5861c79a284d48a72171 /src/client.c
parent1c795424fde9839ab6ebfd49ce7a84ac946be3c0 (diff)
downloadnetcosm-b7541dfc74d2d549210aa0ad73e0536ca4818909.zip
netcosm-b7541dfc74d2d549210aa0ad73e0536ca4818909.tar.gz
netcosm-b7541dfc74d2d549210aa0ad73e0536ca4818909.tar.bz2
netcosm-b7541dfc74d2d549210aa0ad73e0536ca4818909.tar.xz
better auth
Diffstat (limited to 'src/client.c')
-rw-r--r--src/client.c63
1 files changed, 52 insertions, 11 deletions
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);
+ }
}