-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.c
455 lines (333 loc) · 9.08 KB
/
auth.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "chatter.h"
user_account_t user_accounts[MAX_USERS];
size_t num_user_accounts = 0;
user_info_t *user_infos;
bool echo_mode;
bool echo_flag;
bool user_exists(char *username){
FILE *fp;
char *line, *cur_username, *saveptr;
size_t length = 0;
size_t bytes_read = 0;
fp = fopen(accounts_filename,"r");
if (fp == NULL){
return false;
}
while ((bytes_read = getline(&line, &length, fp)) != -1) {
strip_char(line,'\n');
cur_username = strtok_r(line,":",&saveptr);
if (cur_username == NULL){
error("user accounts file is malformed.");
exit(1);
}
if (!strcmp(username,cur_username)){
fclose(fp);
if (line)
free(line);
return true;
}
}
if (line)
free(line);
fclose(fp);
return false;
}
/*
passwords must meet the f ollowing criteria to be valid:
Must be at least 5 characters in leng th
Must contain at least 1 uppercase character
Must contain at least 1 symbol character
Must contain at least 1 number character
*/
bool check_password(char *password){
int length, upper_count, symbol_count, number_count;
char *p;
length = upper_count = symbol_count = number_count = 0;
for (p = password; *p != '\0'; p++){
if (*p >= 'A' && *p <= 'Z')
upper_count++;
if (*p >= '0' && *p <= '9')
number_count++;
if (*p == '!' || *p == '@' || *p == '#' || *p == '$')
symbol_count++;
//if character is outside of the acceptable range ([!@#$0-9a-zA-Z])
if (!((*p >= 48 && *p <= 57) || (*p >=64 && *p <= 90) || (*p >= 97 && *p <= 122) || *p == 33 || *p == 35 || *p == 36)){
return false;
}
length++;
}
if (!(upper_count && number_count && symbol_count))
return false;
return length >= 5;
}
int fill_user_slot(char *username){
FILE *fp;
char blank_digest[SHA_DIGEST_LENGTH*2];
fp = fopen(accounts_filename,"a");
if (fp == NULL){
return -100;
}
for (int i = 0; i < SHA_DIGEST_LENGTH; i++){
snprintf(blank_digest + i,2,"%02x",0);
}
fprintf(fp,"%s:%08x:%s\n",username,0,blank_digest);
fclose(fp);
return 0;
}
int release_user_slot(char *username){
user_account_t *cur_user;
size_t user_index = 0;
if (get_user_accounts() == NULL){
error("Couldn't get user accounts from file.");
return -100;
}
while (user_index < num_user_accounts){
cur_user = &user_accounts[user_index++];
if (!strcmp(cur_user->username,username)){
cur_user->username[0] = '\0';
}
}
return 0;
}
int authenticate_user(char *username, char *password){
user_account_t *cur_user;
size_t user_index = 0;
unsigned char salted_password[MAX_PASSWORD + (sizeof(unsigned int) * 2)];
unsigned char hash[SHA_DIGEST_LENGTH];
char hash_digest[SHA_DIGEST_LENGTH*2]; //2 hexadecimal characters to represent each byte
if (get_user_accounts() == NULL){
error("Couldn't get user accounts from file.");
return -100;
}
while (user_index < num_user_accounts){
cur_user = &user_accounts[user_index++];
//we found our user. Hash the password and salt and compare the hashes
if (!strcmp(cur_user->username,username)){
//get user's salted password
snprintf((char*)salted_password,sizeof(salted_password),"%s%s",password,cur_user->salt);
//hash the salted password
//infow("hashing salted_password: %s",salted_password);
SHA1(salted_password, strlen((char*)salted_password), hash);
//get the printable digest
for (int i = 0; i < SHA_DIGEST_LENGTH; i++){
sprintf(hash_digest + (i*2),"%02x",hash[i]);
}
//debugw("hash digest of salted password: --%s--", hash_digest);
//compare it to the database
if (!strcmp(hash_digest,cur_user->password_hash)){
return 0;
}
else {
return 61;
}
}
}
return 100;
}
user_account_t* get_user_accounts(){
FILE *fp;
size_t user_index = 0;
user_account_t *cur_user;
char *line, *cur_username, *cur_salt, *cur_hash,*saveptr;
size_t length = 0;
size_t bytes_read = 0;
fp = fopen(accounts_filename,"r");
if (fp == NULL){
return false;
}
while ((bytes_read = getline(&line, &length, fp)) != -1) {
strip_char(line,'\n');
cur_username = strtok_r(line,":",&saveptr);
if (cur_username == NULL){
error("user accounts file is malformed.");
fclose(fp);
return NULL;
}
cur_salt = strtok_r(NULL,":",&saveptr);
if (cur_salt == NULL){
error("user accounts file is malformed.");
fclose(fp);
return NULL;
}
cur_hash = strtok_r(NULL,":",&saveptr);
if (cur_hash == NULL){
fclose(fp);
error("user accounts file is malformed.");
return NULL;
}
cur_user = &user_accounts[user_index++];
strcpy(cur_user->username,cur_username);
strcpy(cur_user->salt,cur_salt);
strcpy(cur_user->password_hash,cur_hash);
}
num_user_accounts = user_index;
if (line)
free(line);
fclose(fp);
return user_accounts;
}
int write_user_accounts(){
FILE *fp;
user_account_t *cur_user;
size_t user_index = 0;
fp = fopen(accounts_filename,"w");
if (fp == NULL){
error("couldn't open accounts file.");
return 100;
}
while (user_index < num_user_accounts){
cur_user = &user_accounts[user_index++];
if (!strlen(cur_user->username)){
continue;
}
fprintf(fp,"%s:%s:%s\n",cur_user->username,cur_user->salt,cur_user->password_hash);
}
fclose(fp);
return 0;
}
int free_user_accounts(){
for (size_t user_index = 0; user_index < num_user_accounts; user_index++){
memset(&user_accounts[user_index],0,sizeof(user_account_t));
}
num_user_accounts = 0;
return 0;
}
//we created a temporary user slot earlier - read entire file and
//change that slot
//be sure to write newline after writing username and password
int create_user(char *username, char *password){
user_account_t *cur_user;
int salt;
size_t user_index = 0;
unsigned char salted_password[MAX_PASSWORD + (sizeof(unsigned int) * 2)];
unsigned char hash[SHA_DIGEST_LENGTH];
char hash_digest[SHA_DIGEST_LENGTH*2]; //2 hexadecimal characters to represent each byte
if (!check_password(password)){
assert(false);
return 61;
}
//generate salt for user
salt = randint();
snprintf((char*)salted_password,sizeof(salted_password),"%s%08x",password,salt);
infow("hashing salted_password: %s",salted_password);
//should be 6eebe39d0f399e8a1d04a55a926cff7c1108ea81 for Vasia2!ca976175
SHA1(salted_password, strlen((char*)salted_password), hash);
for (int i = 0; i < SHA_DIGEST_LENGTH; i++){
sprintf(hash_digest + (i*2),"%02x",hash[i]);
}
infow("(creation) hash digest of salted password: --%s--", hash_digest);
if (get_user_accounts() == NULL){
error("Couldn't get user accounts from file.");
return 100;
}
while (user_index < num_user_accounts){
cur_user = &user_accounts[user_index++];
//we found our user, copy in the appropriate data into the
//struct.
if (!strcmp(cur_user->username,username)){
snprintf(cur_user->salt,(sizeof(int)*2)+1,"%08x",salt);
snprintf(cur_user->password_hash,sizeof(hash_digest)+1,"%s",hash_digest);
break;
}
}
write_user_accounts();
free_user_accounts();
return 0;
}
user_info_t* login_user(char *username, int connfd){
user_info_t *new_user;
assert(user_logged_in(username) == NULL);
new_user = calloc(1,sizeof(user_info_t));
strcpy(new_user->username,username);
new_user->connfd = connfd;
new_user->next = user_infos;
user_infos = new_user;
return new_user;
}
int logout_user(char *username){
user_info_t *user, *prev;
user = user_infos;
prev = NULL;
while(user != NULL){
if (!strcmp(username,user->username)){
if (prev != NULL)
prev->next = user->next;
else{
user_infos = user->next;
}
//close the connection if its not already closed.
if (fcntl(user->connfd, F_GETFD) == EBADF){
debugw("User (%s) connection is already closed", username);
}
else {
//debugw("Closing (%s) connection socket.", username);
infow("Connection with \"%s\" closed.", username);
close(user->connfd);
}
free(user);
if (user_infos == NULL){
echo_flag = false;
}
return 0;
}
prev = user;
user = user->next;
}
return 1;
}
user_info_t* user_logged_in(char *username){
user_info_t *user;
user = user_infos;
while (user != NULL){
if (!strcmp(username,user->username)){
return user;
}
user = user->next;
}
return NULL;
}
user_info_t* get_user_byfd(int connfd){
user_info_t *user;
user = user_infos;
while (user != NULL){
if (user->connfd == connfd){
return user;
}
user = user->next;
}
return NULL;
}
user_info_t* get_user_byname(char *username){
user_info_t *user;
user = user_infos;
while (user != NULL){
if (!strcmp(username,user->username)){
return user;
}
user = user->next;
}
return NULL;
}
int get_num_users(){
user_info_t *user;
int count = 0;
user = user_infos;
while (user != NULL){
count++;
user = user->next;
}
return count;
}
int mark_user_ready(char *username){
user_info_t *user;
user = user_infos;
while (user != NULL){
if (!strcmp(username,user->username)){
user->ready = true;
return 0;
}
user = user->next;
}
error("mark_user_ready couldn't find user: %s", username);
return 1;
}