-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
137 lines (92 loc) · 3.14 KB
/
client.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
/*******************************************************************************
* This file contains the 'client' program source code. This program directly
* interacts with the user.
* *****************************************************************************/
#include "headers.h"
#include "communication.h"
#include "utilities.h"
int msgid = -1;
/* This function allows a user to enter credentials and login into the system */
void login()
{
char username[MAX_USERNAME_LENGTH] = {'\0'};
char *password;
printf("Enter username:"); // input username
scanf("%s", username);
password = getpass("Enter password:"); // input password without echoing
// check the inputted credentials with the server
int status = check_credentials(msgid, username, password);
if(status == 1)
printf("Login Successful!\n");
else if(status == HONEYPOT_HIT)
raise_alarm(username, HONEYPOT_HIT);
else if(status == HONEYWORD_HIT)
raise_alarm(username, HONEYWORD_HIT);
else if(status < 0)
printf("username or password wrong\n");
return ;
}
void create_new_account()
{
char username[MAX_USERNAME_LENGTH] = {'\0'};
char password[N] = {'\0'};
char *p1;
printf("Enter a username:"); // input username
scanf("%s", username);
while(username_available(msgid, username) > 0) // check if username is already registered
{
printf("Username already taken. Try again.\n");
return ;
}
p1 = getpass("Enter a password (should be 8-12 characters):"); // input password
if(strlen(p1) < 8 || strlen(p1) > 12)
{
printf("Password must be of 8-12 characters.\n");
return ;
}
strcpy(password, p1); // put password in the buffer
p1 = getpass("Enter the password again:"); // ask to repeat the password
if(strcmp(password, p1) != 0) // if passwords do not match
{
printf("Passwords do not match. Try Again!\n");
return ;
}
int k = 6;
printf("Enter k [default is 6; max is 20]: "); // input k
scanf("%d", &k);
// communicate with server to register the user
int status = register_user(msgid, username, password, k);
if(status == 1)
printf("Registration successful!\n");
else
printf("Registration failed. Try again.\n");
return ;
}
int main()
{
printf("Welcome to the Authentication System.\n\n");
msgid = get_msgid(); // get message-queue key
int option = 0;
while(option != 3)
{
printf("Options:\n");
printf("\t1 - Login\n");
printf("\t2 - Create a new account\n");
printf("\t3 - Exit\n\n");
printf("Select an option: "); // input selected option
scanf("%d", &option);
switch(option)
{
case 1: login();
break;
case 2: create_new_account();
break;
case 3: break;
default: printf("Please select a valid option.\n");
break;
}
printf("\n");
}
printf("Thank you for using the system.\n");
return 0;
}