-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
963 lines (830 loc) · 22.1 KB
/
server.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define USER 0
#define ADMIN 2
#define UNAUTH_USER -1
#define RESPONSE_BYTES 512
#define REQUEST_BYTES 512
#define linesInMS 5
#define EXIT -1
typedef struct login{
char username[100];
char password[100];
char type;
long long int accno;
char active;
}user;
typedef struct seq{
long long int count;
}acc_num;
void talkToClient(int client_fd);
void closeclient(int client_fd,char *str);
void userReqAdmin(char *username,char *password,int client_fd);
// Send msg to client
void sendMsgtoClient(int clientFD, char *str)
{
int numPacketsToSend = (strlen(str)-1)/RESPONSE_BYTES + 1;
int n = write(clientFD, &numPacketsToSend, sizeof(int));
char *msgToSend = (char*)malloc(numPacketsToSend*RESPONSE_BYTES);
strcpy(msgToSend, str);
int i;
for(i = 0; i < numPacketsToSend; ++i)
{
int n = write(clientFD, msgToSend, RESPONSE_BYTES);
msgToSend += RESPONSE_BYTES;
}
}
// Recieve msg from client
char* recieveMsgFromClient(int clientFD)
{
int numPacketsToReceive = 0;
int n = read(clientFD, &numPacketsToReceive, sizeof(int));
if(n <= 0)
{
shutdown(clientFD, SHUT_WR);
return NULL;
}
char *str = (char*)malloc(numPacketsToReceive*REQUEST_BYTES);
memset(str, 0, numPacketsToReceive*REQUEST_BYTES);
char *str_p = str;
int i;
for(i = 0; i < numPacketsToReceive; ++i)
{
int n = read(clientFD, str, REQUEST_BYTES);
str = str+REQUEST_BYTES;
}
return str_p;
}
// convert long long int to string
char* strfromlonglong(long long int value)
{
char buf[32], *p;
long long int v;
v = (value < 0) ? -value: value;
p = buf + 31;
do // storing digits in p.....digit by digit
{
*p -- = '0' + (v%10);
v /= 10;
} while(v);
if(value < 0) *p -- = '-';
p ++;
int len = 32 - (p - buf);
char *s = (char*)malloc(sizeof(char) * (len + 1));
memcpy(s, p, len);
s[len] = '\0';
return s;
}
// get account number from username
char* accountFromName(char *username)
{
user usr;
int fd = open("login.dat",O_RDONLY);
while((read(fd,&usr,sizeof(usr)))>0)
{
if(strcmp(usr.username,username)==0)
{
char *buff = NULL;
buff = strfromlonglong(usr.accno);
return buff;
}
}
}
// print mini statement
char *printMiniStatement(char *db,int client_fd)
{
int fp = open(db, O_RDONLY);
char *current_balance = (char *)malloc(20*sizeof(char));
lseek(fp, 20, SEEK_CUR);
char *miniStatement = (char *)malloc(1000*sizeof(char));
read(fp, miniStatement, 1000);
return miniStatement;
}
// get current balance value from DB
char *printBalance(char *db)
{
int fd = open(db, O_RDONLY);
char *current_balance = (char *)malloc(20*sizeof(char));
read(fd, current_balance, 20);
return current_balance;
}
// Update DB
void updateTrans(char *db,char c,double balance,double amount)
{
int fp = open(db, O_WRONLY | O_APPEND);
int fpb = open(db, O_WRONLY);
char *buff = (char *)malloc(20*sizeof(char));
write(fpb, buff, 20); //
int length = sprintf(buff, "%20f",balance);
lseek(fpb, 0, 0);
write(fpb, buff, length);
close(fpb);
char buffer[100];
time_t ltime;
ltime=time(NULL); // current calendar time
length = sprintf(buffer,"%.*s %f %c %f\n",(int)strlen(asctime(localtime(<ime)))-1 , asctime(localtime(<ime)), balance, c,amount);
write(fp, buffer, length);
close(fp);
free(buff);
}
// Debit account
int Debit(char *username, int client_fd){
char usrupdate[] = "User updated successfully.\n--------------------\n\nDo you want to continue?";
char usrupdateu[] = "Update UnsucessFull Low balance\n--------------------\n\nWrite exit for quitting.";
char *db = accountFromName(username);
int fd = open(db, O_RDWR);
struct flock fl;
memset(&fl, 0, sizeof(fl));
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 20;
if(fcntl(fd, F_SETLK, &fl) == -1) // blocking mode F_SETLK
{
sendMsgtoClient(client_fd, "\n\nAnother Transaction is Being Processed either Type back or exit to quit\n");
close(fd);
return 0;
printf("cannot access write lock\n");
exit(1);
}
double balance=strtod(printBalance(db),NULL);
double amount=0.0;
sendMsgtoClient(client_fd, "Enter Amount");
while(1){
char *buff=recieveMsgFromClient(client_fd);
amount=strtod(buff,NULL);
if(amount<=0)
sendMsgtoClient(client_fd,"Enter valid amount");
else
break;
}
if(balance >= amount){
balance = balance - amount;
updateTrans(db,'D',balance,amount);
sendMsgtoClient(client_fd,usrupdate); // sending update successfull msg
}
else{
sendMsgtoClient(client_fd,usrupdateu); // send update unsuccessfull msg
}
fl.l_type = F_UNLCK;
if(fcntl(fd, F_SETLK, &fl) == -1){
printf("unlocked fail\n");
exit(1);
}
close(fd);
printf("User DataBase updated\n");
}
// Credit account
int Credit(char *username, char *password, int client_fd)
{
char usrupdate[] = "User updated successfully.\n--------------------\n\nDo you want to continue?";
char *db = accountFromName(username);
int fd = open(db, O_RDWR);
struct flock fl;
memset(&fl, 0, sizeof(fl));
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 20;
if(fcntl(fd, F_SETLK, &fl) == -1) // blocking mode F_SETLK
{
printf("trying to open %s",db);
sendMsgtoClient(client_fd, "\n\nAnother Transaction is Being Processed either Type back or exit to quit\n");
close(fd);
return 0;
printf("cannot access write lock\n");
exit(1);
}
double balance=strtod(printBalance(db),NULL);
double amount=0.0;
sendMsgtoClient(client_fd, "Enter Amount");
while(1)
{
char *buff=recieveMsgFromClient(client_fd);
amount=strtod(buff,NULL);
if(amount<=0)
sendMsgtoClient(client_fd,"Enter valid amount");
else
break;
}
balance +=amount;
updateTrans(db,'C',balance,amount);
sendMsgtoClient(client_fd,usrupdate);
fl.l_type = F_UNLCK;
if(fcntl(fd, F_SETLK, &fl) == -1)
{
printf("unlocked fail\n");
exit(1);
}
close(fd);
printf("User DB updated\n");
}
// Get a new account number
long long int getAccNo()
{
int fd = open("acc_num",O_RDWR);
acc_num num;
read(fd,&num,sizeof(num));
long long int x = num.count+1;
num.count = x;
lseek(fd,0,SEEK_SET);
write(fd,&num,sizeof(num));
close(fd);
return x;
}
// check if username already exists
int checkUser(char *username)
{
user usr;
int fd = open("login.dat", O_RDONLY);
while((read(fd, &usr, sizeof(usr))) > 0)
{
if(strcmp(usr.username,username)==0)
{
close(fd);
return 1;
}
}
close(fd);
return 0;
}
// Create a new Single account
void AddUser(int client_fd)
{
user usr;
sendMsgtoClient(client_fd,"Enter username::");
char *name = NULL;
NAME:
name = NULL;
name = recieveMsgFromClient(client_fd);
if(checkUser(name))
{
sendMsgtoClient(client_fd, "UserName already exists\nEnter username::");
goto NAME;
}
sendMsgtoClient(client_fd, "Enter Password::");
char *pass = NULL, *cpass = NULL;
PASS:
pass = recieveMsgFromClient(client_fd);
sendMsgtoClient(client_fd, "ReEnter New Password::");
cpass = recieveMsgFromClient(client_fd);
if(strcmp(pass,cpass) == 0) // passwords match, proceed with creating account
{
printf("pass verified");
strcpy(usr.username,name);
strcpy(usr.password,pass);
usr.accno = getAccNo();
usr.type = 'C';
usr.active = 'y';
printf("login.dat before");
int fd = open("login.dat", O_WRONLY | O_APPEND); // create entry in login.dat
write(fd, &usr, sizeof(usr));
close(fd);
printf("login.dat done");
char *filename = NULL;
filename = strfromlonglong(usr.accno);
creat(filename, 0766);
fd = open(filename, O_WRONLY | O_APPEND, 0766);
double balance = 1000.000000000000000;
char buff[20] = {0};
sprintf(buff, "%f", balance);
write(fd, buff, sizeof(buff));
close(fd);
printf("%s done",filename);
}
else
{
sendMsgtoClient(client_fd, "Passwords do not match\nEnter New Password::");
goto PASS;
}
}
// Create a joint account
void jointAcc(int client_fd)
{
char *name1, *pass1, *name2, *pass2, *cpass;
sendMsgtoClient(client_fd,"Enter 1st username::");
name1 = NULL;
NAME:
name1 = recieveMsgFromClient(client_fd);
if(checkUser(name1))
{
sendMsgtoClient(client_fd, "UserName already exists\nEnter username::");
goto NAME;
}
sendMsgtoClient(client_fd, "Enter Password::");
PASS:
pass1 = recieveMsgFromClient(client_fd);
sendMsgtoClient(client_fd, "ReEnter New Password::");
cpass = recieveMsgFromClient(client_fd);
if(strcmp(pass1,cpass) == 0) // 1st user's credentials verified
{
name2 = NULL;
cpass = NULL;
sendMsgtoClient(client_fd,"Enter 2nd username::");
NAME2:
name2 = recieveMsgFromClient(client_fd);
if(checkUser(name2))
{
sendMsgtoClient(client_fd, "UserName already exists\nEnter username::");
goto NAME2;
}
sendMsgtoClient(client_fd, "Enter Password::");
PASS2:
pass2 = recieveMsgFromClient(client_fd);
sendMsgtoClient(client_fd, "ReEnter New Password::");
cpass = recieveMsgFromClient(client_fd);
if(strcmp(pass2,cpass)==0) // user 2 password verified
{
// added both the users in login.dat
user usr1, usr2;
long long int no = getAccNo();
strcpy(usr1.username, name1);
strcpy(usr1.password, pass1);
usr1.type = 'J';
usr1.active = 'y';
usr1.accno = no;
strcpy(usr2.username, name2);
strcpy(usr2.password, pass2);
usr2.type = 'J';
usr2.active = 'y';
usr2.accno = no;
int fd = open("login.dat", O_WRONLY | O_APPEND);
write(fd, &usr1, sizeof(usr1));
write(fd, &usr2, sizeof(usr2));
close(fd);
// add mapping for usr1 and usr2
// account name will be user1's name. when user2 will login, it'll access the respective user1's file.
char *filename = NULL;
filename = strfromlonglong(no);
creat(filename, 0766);
fd = open(filename, O_WRONLY | O_APPEND, 0766);
double balance = 1000.000000000000000;
char buff[20] = {0};
sprintf(buff, "%f", balance);
write(fd, buff, sizeof(buff));
close(fd);
}
else // 2nd user's password's don't match
{
sendMsgtoClient(client_fd, "Passwords do not match\nEnter New Password::");
goto PASS2;
}
}
else // 1st user's passwords don't match
{
sendMsgtoClient(client_fd, "Passwords do not match\nEnter New Password::");
goto PASS;
}
}
// change password
int Passwordchng(char *username, int client_fd)
{
char usrupdate[] = "Password updated successfully.\n--------------------\n\nDo you want to continue?";
int fd = open("login.dat", O_RDWR);
user usr;
char *pass = NULL;
char *cpass = NULL;
while(read(fd, &usr, sizeof(usr)) > 0)
{
if(strcmp(usr.username, username) == 0)
{
sendMsgtoClient(client_fd, "Enter New Password::");
ReEntry:
pass = recieveMsgFromClient(client_fd);
sendMsgtoClient(client_fd, "ReEnter New Password::");
cpass = recieveMsgFromClient(client_fd);
if(strcmp(pass,cpass) == 0)
{
lseek(fd, -1*sizeof(user), SEEK_CUR);
strcpy(usr.password, pass);
write(fd, &usr, sizeof(usr));
sendMsgtoClient(client_fd, usrupdate); // sending user msg about successfull update
return 1;
}
else
{
sendMsgtoClient(client_fd, "Passwords do not match\nEnter New Password::");
goto ReEntry;
}
}
}
return 0;
}
// delete user account
void deleteUser(int client_fd,char* name)
{
printf("Message received");
int fd = open("login.dat",O_RDWR);
user usr;
while(read(fd,&usr,sizeof(usr))>0){
if(strcmp(name,usr.username) == 0)
{
printf("\ndeactivating");
usr.active = 'n';
lseek(fd, -1*sizeof(usr), SEEK_CUR);
write(fd, &usr, sizeof(usr));
break;
}
}
close(fd);
}
void userDetails(int client_fd, char *u)
{
if(checkUser(u))
{
printf("user check");
int fd = open("login.dat",O_RDONLY);
user usr;
char *str = "name::\n", *type="type::\n", *num="Account num::\n", *bal="Balance::\n";
while((read(fd,&usr,sizeof(usr))>0))
{
if(strcmp(usr.username,u)==0)
{
printf("Innnn");
printf("%s",strfromlonglong(usr.accno));
strcat(str,u);
strcat(num,strfromlonglong(usr.accno));
strcat(bal,printBalance(strfromlonglong(usr.accno)));
strcat(str,num);
strcat(str,bal);
sendMsgtoClient(client_fd, str);
}
}
}
else sendMsgtoClient(client_fd,"Wrong Username. Please enter a valid user or exit to quit");
}
// To manage User Requests
void userRequests(char *username,char *password,int client_fd)
{
int flag=1;
char option[] = "\n------------------\n\nEnter your choice\n1. Available Balance\n2. Mini Statement\n3. Deposit\n4. WithDraw\n5. Password Change\n6. User Details \nWrite exit for quitting.";
sendMsgtoClient(client_fd,option);
char *buff=NULL;
while(flag)
{
if(flag == 2)
{
flag = 1;
sendMsgtoClient(client_fd,option);
}
if(buff!=NULL)
buff=NULL;
buff=recieveMsgFromClient(client_fd);
int choice;
if(strcmp(buff,"exit")==0)
choice=7;
else choice=atoi(buff);
char *bal,*str;
bal=(char *)malloc(1000*sizeof(char));
str=(char *)malloc(100000*sizeof(char));
strcpy(bal,"------------------\nAvailable Balance: ");
strcpy(str,"------------------\nMini Statement: \n");
char *db = accountFromName(username);
char *acc_detail;
acc_detail=(char *)malloc(1000*sizeof(char));
char uname[50];
strcpy(acc_detail,"\n------------------\nUsername::");
strcat(acc_detail, username);
strcat(acc_detail,"\nAccount Number:");
strcat(acc_detail,db);
switch(choice)
{
case 1:
strcat(bal,printBalance(db));
sendMsgtoClient(client_fd,strcat(bal,option));
free(bal);
break;
case 2:
strcat(str, printMiniStatement(db,client_fd));
sendMsgtoClient(client_fd,strcat(str,option));
free(str);
break;
case 3:
Credit(username, password, client_fd);
buff=recieveMsgFromClient(client_fd);
if(strcmp(buff,"back")==0)
sendMsgtoClient(client_fd,option);
else if(strcmp(buff,"no")==0)
flag = 0;
else
flag = 2;
break;
case 4:
Debit(username, client_fd);
buff=recieveMsgFromClient(client_fd);
if(strcmp(buff,"back")==0)
sendMsgtoClient(client_fd,option);
else if(strcmp(buff,"no")==0)
flag = 0;
else
flag = 2;
break;
case 5:
Passwordchng(username, client_fd);
buff=recieveMsgFromClient(client_fd);
if(strcmp(buff,"no")==0)
flag = 0;
else
flag = 2;
break;
case 6:
sendMsgtoClient(client_fd,strcat(acc_detail,option));
break;
case 7:
flag=0;
break;
default:
sendMsgtoClient(client_fd, "Unknown Query, Please Choose from the given Options");
break;
}
}
}
// To manage Admin Requests
void adminRequests(char *username, int client_fd)
{
char options[] = "\n1.Create Individual Account\n2.Create Joint Account\n3.Get User Details\n4.Delete User\n5.Change Password\nWrite exit to quit";
sendMsgtoClient(client_fd, options);
char *dat = (char *)malloc(1000*sizeof(char));
char *name = NULL;
while(1)
{
char *buff=NULL;
buff=recieveMsgFromClient(client_fd);
if(strcmp(buff,"exit")==0)
break;
else
{
int choice=atoi(buff);
switch(choice)
{
// individual account
case 1:
AddUser(client_fd);
strcpy(dat, "USER ADDED SUCCESSFULLY----------------\n");
sendMsgtoClient(client_fd, strcat(dat,options));
break;
// joint account
case 2:
jointAcc(client_fd);
strcpy(dat,"Account created\n");
sendMsgtoClient(client_fd,strcat(dat,options));
break;
// get user details
case 3:
sendMsgtoClient(client_fd,"Enter username::");
name = NULL;
name = recieveMsgFromClient(client_fd);
userReqAdmin(name,NULL,client_fd);
break;
// delete user
case 4:
sendMsgtoClient(client_fd,"Enter the Username::");
char *buff=NULL;
buff=recieveMsgFromClient(client_fd);
deleteUser(client_fd,buff);
strcpy(dat,"Account deleted\n");
sendMsgtoClient(client_fd,strcat(dat,options));
break;
// change password
case 5:
Passwordchng(username, client_fd);
break;
default:
strcpy(dat, "Invalid Choice");
sendMsgtoClient(client_fd, strcat(dat,options));
break;
}
}
}
}
// To get User details for Admin
void userReqAdmin(char *username,char *password,int client_fd)
{
int flag=1;
char option[] = "\n------------------\n\nEnter your choice\n1. Available Balance\n2. Mini Statement\n3. Password Change\n4. User Details \n5.Go Back to Main menu\nWrite exit for quitting.";
sendMsgtoClient(client_fd,option);
char *buff=NULL;
while(flag)
{
if(flag == 2){
flag = 1;
sendMsgtoClient(client_fd,option);
}
if(buff!=NULL)
buff=NULL;
buff=recieveMsgFromClient(client_fd);
int choice;
if(strcmp(buff,"exit")==0)
choice=8;
else choice=atoi(buff);
char *bal,*str;
bal=(char *)malloc(1000*sizeof(char));
str=(char *)malloc(100000*sizeof(char));
strcpy(bal,"------------------\nAvailable Balance: ");
strcpy(str,"------------------\nMini Statement: \n");
char *db = accountFromName(username);
char *acc_detail;
acc_detail=(char *)malloc(1000*sizeof(char));
char uname[50];
strcpy(acc_detail,"\n------------------\nUsername::");
strcat(acc_detail, username);
strcat(acc_detail,"\nAccount Number:");
strcat(acc_detail,db);
switch(choice)
{
case 1:
strcat(bal,printBalance(db));
sendMsgtoClient(client_fd,strcat(bal,option));
free(bal);
break;
case 2:
strcat(str, printMiniStatement(db,client_fd));
sendMsgtoClient(client_fd,strcat(str,option));
free(str);
break;
// case 3:
// Credit(username, password, client_fd);
// buff=recieveMsgFromClient(client_fd);
// if(strcmp(buff,"back")==0)
// sendMsgtoClient(client_fd,option);
// else if(strcmp(buff,"no")==0)
// flag = 0;
// else
// flag = 2;
// break;
// case 4:
// Debit(username, client_fd);
// buff=recieveMsgFromClient(client_fd);
// if(strcmp(buff,"back")==0)
// sendMsgtoClient(client_fd,option);
// else if(strcmp(buff,"no")==0)
// flag = 0;
// else
// flag = 2;
// break;
case 3:
Passwordchng(username, client_fd);
buff=recieveMsgFromClient(client_fd);
if(strcmp(buff,"no")==0)
flag = 0;
else
flag = 2;
break;
case 4:
sendMsgtoClient(client_fd,strcat(acc_detail,option));
break;
case 5:
adminRequests(NULL,client_fd);
break;
case 6:
flag=0;
break;
default:
sendMsgtoClient(client_fd, "Unknown Query");
break;
}
}
}
// Get username and password from client
void getupcli(char *username,char *password,int client_fd)
{
char *ruser,*rpass;
sendMsgtoClient(client_fd,"Enter Username: ");
ruser=recieveMsgFromClient(client_fd);
sendMsgtoClient(client_fd,"Enter Password: ");
rpass=recieveMsgFromClient(client_fd);
int i=0;
while(ruser[i]!='\0' && ruser[i]!='\n')
{
username[i]=ruser[i];
i++;
}
username[i]='\0';
i=0;
while(rpass[i]!='\0' && rpass[i]!='\n')
{
password[i]=rpass[i];
i++;
}
password[i]='\0';
}
// authorize client details
int authorize(char* username,char *password)
{
printf("Authorizing\n");
ssize_t readc;
user usr;
int fd = open("login.dat", O_RDONLY);
while((readc = read(fd, &usr, sizeof(usr))) > 0){
if(strcmp(usr.username,username)==0){
if(strcmp(usr.password,password)==0){
if(usr.type=='C' || usr.type == 'J'){
// checking for active status of account
if(usr.active=='y'){
printf("\nAccepting\n");
close(fd);
return USER;
}
else
{
printf("Rejecting");
close(fd);
return UNAUTH_USER;
}
}
else if(usr.type =='A'){
close(fd);
return ADMIN;
}
}
}
}
close(fd);
return UNAUTH_USER;
}
void talkToClient(int client_fd)
{
char *username,*password;
username=(char *)malloc(100);
password=(char *)malloc(100);
int utype;
getupcli(username,password,client_fd); // get username and password from the user
utype=authorize(username,password); // validate and authorize
char *str=(char *)malloc(sizeof(char)*60);
strcpy(str,"Thanks ");
switch(utype)
{
case USER:
printf("User is IN \n");
userRequests(username,password,client_fd);
closeclient(client_fd,strcat(str,username));
break;
case ADMIN:
printf("ADMIN is IN \n");
adminRequests(username, client_fd);
closeclient(client_fd,strcat(str,username));
break;
case UNAUTH_USER:
closeclient(client_fd,"unauthorised access, Wrong Credentials");
break;
default:
closeclient(client_fd,"unauthorised access, Wrong Credentials");
break;
}
}
void closeclient(int client_fd,char *str)
{
sendMsgtoClient(client_fd, str);
shutdown(client_fd, SHUT_RDWR);
}
// main(), the home
int main(int argc,char **argv)
{
int sock_fd,client_fd,port_no;
struct sockaddr_in serv_addr, cli_addr;
memset((void*)&serv_addr, 0, sizeof(serv_addr));
port_no=atoi(argv[1]);
sock_fd=socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_port = htons(port_no); //set the port number
serv_addr.sin_family = AF_INET; //setting DOMAIN as IPV4
serv_addr.sin_addr.s_addr = INADDR_ANY; //permits any incoming IP
if(bind(sock_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
printf("Error on binding....Port Number may not be available.\n");
exit(EXIT_FAILURE);
}
int reuse=1;
setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int));
listen(sock_fd, 5);
int clisize=sizeof(cli_addr);
while(1) {
//blocking call
memset(&cli_addr, 0, sizeof(cli_addr));
if((client_fd = accept(sock_fd, (struct sockaddr*)&cli_addr, &clisize)) < 0) {
printf("Error on accept.\n");
exit(EXIT_FAILURE);
}
switch(fork()) {
case -1:
printf("Error in fork.\n");
break;
case 0: {
close(sock_fd);
talkToClient(client_fd);
exit(EXIT_SUCCESS);
break;
}
default:
close(client_fd);
break;
}
}
}