1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#include "netcosm.h"
int client_fd;
void __attribute__((format(printf,1,2))) out(const char *fmt, ...)
{
char buf[128];
memset(buf, 0, sizeof(buf));
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
write(client_fd, buf, sizeof(buf));
}
#define BUFSZ 128
char *client_read(void)
{
char *buf = malloc(BUFSZ);
memset(buf, 0, BUFSZ);
if(read(client_fd, buf, BUFSZ - 1) < 0)
error("lost connection");
buf[BUFSZ - 1] = '\0';
const unsigned char ctrlc[] = { 0xff, 0xf4, 0xff, 0xfd, 0x06 };
if(!memcmp(buf, ctrlc, sizeof(ctrlc)))
exit(0);
return buf;
}
void client_main(int fd, struct sockaddr_in *addr, int total)
{
client_fd = fd;
char *ip = inet_ntoa(addr->sin_addr);
printf("New client %s\n", ip);
printf("Total clients: %d\n", total);
out("NetCosm " NETCOSM_VERSION "\n");
if(total > 1)
out("%d clients connected.\n", total);
else
out("%d client connected.\n", total);
out("\nPlease authenticate to continue.\n\n");
int failures = 0;
int authlevel;
/* auth loop */
while(1)
{
out("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);
}
}
|