forked from aznashwan/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.c
More file actions
68 lines (56 loc) · 2 KB
/
auth.c
File metadata and controls
68 lines (56 loc) · 2 KB
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
#include <stdlib.h>
#include <string.h>
#include "auth.h"
#include "debug.h"
#include "socklib.h"
const struct creds_t CREDENTIALS[] = {
{ .uname = "aznashwan" , .upass = "sockmaster" },
{ .uname = "luciantoo" , .upass = "toolucian" },
{ .uname = "thevladeffect", .upass = "vladspass" },
{ .uname = NULL, .upass = NULL }
};
// auth_connection is a helper function which, given the file descriptor of
// a freshly-accepted connection, blocks until both a username and a password
// are provided. If all was well, a pointer to the username of the newly
// authenticated user will be returned, and must be properly freed, otherwise
// NULL will be returned. In case of an error, a negative value is returned.
char* auth_connection(int sockfd) {
char* uname = (char *) malloc(STDTRANS_SIZE);
char* upass = (char *) malloc(STDTRANS_SIZE);
if (read_str_from_sock(sockfd, uname, STDTRANS_SIZE - 1) < 0) {
debug("auth_connection: error on reading username from socket.");
free(uname);
free(upass);
return (char *) -1;
}
if (read_str_from_sock(sockfd, upass, STDTRANS_SIZE - 1) < 0) {
debug("auth_connection: error on reading password from socket.");
free(uname);
free(upass);
return (char *) -1;
}
if(!verify_creds(CREDENTIALS, uname, upass)) {
debug("auth_connection: invalid credentials provided.");
free(uname);
free(upass);
return NULL;
}
free(upass);
return uname;
}
// verify_creds is a helper function which returns 1 if the given username
// and password are found in the given NULL-terminated list of
// creds_t or 0 if no match was found.
int verify_creds(struct creds_t* creds, char* uname, char* upass) {
for (;;) {
if (creds -> uname == NULL) {
return 0;
}
if (strcmp(creds -> uname, uname) == 0 &&
strcmp(creds -> upass, upass) == 0) {
return 1;
}
creds = creds + 1;
}
return 0;
}