-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLocked_Shell.c
More file actions
266 lines (257 loc) · 7.8 KB
/
Copy pathLocked_Shell.c
File metadata and controls
266 lines (257 loc) · 7.8 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
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
//LOCKED SHELL
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#define MAX 256
#define ATTEMPTS 3
typedef struct Node {
char name[MAX];
int wrongAttempts;
struct Node *next, *prev;
} Node;
int main(int argc, char* argv[])
{
int stdPasswords, rbytes, flag, count = 0, exitCode;
char ch, *newFolder, buffer[MAX], *token, *username, *password;
Node *head = NULL, *temp, *node;
pid_t pid, pid2;
//creating a new directory called "Courses" and "student"
if (mkdir("Courses", 0777) == -1 && errno != EEXIST) {
write(2, "Opening directories failure.\n", strlen("Opening directories failure.\n"));
return (-1);
}
if (mkdir("student", 0777) == -1 && errno != EEXIST) {
write(2, "Opening directories failure.\n", strlen("Opening directories failure.\n"));
return (-1);
}
//opening the std_pass text file for creating folders for each student
if ((stdPasswords = open("std_pass.txt", O_RDONLY)) == -1) {
write(2, "Opening students file failure.\n", strlen("Opening students file failure.\n"));
return (-1);
}
//first moving to the second line (the first line is the title of the table - not relevant)
while (read(stdPasswords, &ch, 1) == 1)
{
if (ch == '\n')
break;
}
//now reading each student
while (read(stdPasswords, &ch, 1) == 1)
{
if (ch == ' ') //moving from one column of the table in the text file - to the next column
{
count++;
while(ch == ' ')
{
if (read(stdPasswords, &ch, 1) != 1)
{
write(2, "Reading failure.\n", strlen("Reading failure.\n"));
return (-1);
}
}
lseek(stdPasswords, -1, 1);
}
if (count == 2) //column no.2 is the ID column >> getting the ID
{
if (read(stdPasswords, buffer, 9) != 9)
{
write(2, "Reading failure.\n", strlen("Reading failure.\n"));
return (-1);
}
buffer[9] = '\0';
if ((newFolder = (char*)malloc(strlen("./student/") + 10)) == NULL)
{
write(2, "memory allocation failure\n", strlen("memory allocation failure\n"));
return (-1);
}
strcpy(newFolder, "./student/");
strcat(newFolder, buffer);
newFolder[strlen("./student/") + 9] = '\0';
if (mkdir(newFolder, 0777) == -1 && errno != EEXIST) {
write(2, "Opening directories failure.\n", strlen("Opening directories failure.\n"));
return (-1);
}
}
if (ch == '\n') //a new line means it is the next student
count = 0;
}
close(stdPasswords);
//Now starting the shell's action >> getting inputs from the user
while (1) {
printf("LockShell> ");
memset(buffer, 0, MAX); //clearing the buffer
fgets(buffer, MAX, stdin);
if (strlen(buffer) > 0 && buffer[strlen(buffer) - 1] == '\n')
buffer[strlen(buffer) - 1] = '\0';
//checking the inputs
token = strtok(buffer, " ");
//OPTION NUMBER 1 : LOGIN STUDENT COMMAND
if (strcmp(token, "LoginStudent") == 0) {
username = strtok(NULL, " ");
password = strtok(NULL, " ");
//adding the name to the linked list.
//It is done to keep track on how many times the same student entered wrong passwords
if (head == NULL) { //if the list's empty
if ((head = (Node*)malloc(sizeof(Node))) == NULL) {
write(2, "memory allocation failure\n", strlen("memory allocation failure\n"));
return (-1);
}
strcpy(head->name, username);
head->wrongAttempts = 0;
head->next = NULL;
head->prev = NULL;
}
else { //checking if the student is already in the list. if not - adding him
temp = head;
flag = 0; //to check if the name exists
while (temp != NULL) {
if (strcmp(temp->name, username) == 0) { //exists
flag = 1;
break;
}
temp = temp->next;
}
if (flag == 0) { //does not exist, adding it to the head
if ((node = (Node*)malloc(sizeof(Node))) == NULL) {
write(2, "memory allocation failure\n", strlen("memory allocation failure\n"));
return (-1);
}
strcpy(node->name, username);
node->wrongAttempts = 0;
node->next = head;
node->prev = NULL;
head->prev = node;
head = node;
}
}
//now calling the process with the given arguments
pid = fork();
if (pid == -1) { //error
write(2, "Fork failure.\n", strlen("Fork failure.\n"));
return (-1);
}
else if (pid == 0) { //child process
execlp("./LoginStudent", "./LoginStudent", username, password, NULL);
write(2, "LoginStudent execution failure.\n", strlen("LoginStudent execution failure.\n"));
return (-1);
}
else { //parent process
waitpid(pid, &exitCode, 0);
//the child process will return:
//2 if missing parameters
//1 if the password is incorrect
//0 if correct or if the name does not exist in the students file.
if (WIFEXITED(exitCode) && WEXITSTATUS(exitCode) == 1) { //adding 1 to the wrong password attempts of the student
//searching the student
temp = head;
while (temp != NULL) {
if (strcmp(temp->name, username) == 0)
break;
temp = temp->next;
}
temp->wrongAttempts++;
if (temp->wrongAttempts >= ATTEMPTS) {
//BLOCKING THE STUDENT USING FREEZE PROGRAM
pid2 = fork();
if (pid2 == -1) { //error
write(2, "Fork failure.\n", strlen("Fork failure.\n"));
return (-1);
}
else if (pid2 == 0) { //child process
execlp("./Freeze", "./Freeze", username, NULL);
write(2, "Freeze execution failure.\n", strlen("Freeze execution failure.\n"));
return (-1);
}
else { //parent process
waitpid(pid2, &exitCode, 0);
if (WIFEXITED(exitCode) && WEXITSTATUS(exitCode) == 0) {
printf("%s account Blocked!\n", username);
continue;
}
else {
return (-1);
}
}
}
else {
printf("Incorrect Password\n");
}
}
else if (WIFEXITED(exitCode) && WEXITSTATUS(exitCode) == 0) { //otherwise, removing the name from the list
temp = head;
while (temp != NULL) {
if (strcmp(temp->name, username) == 0) {
break;
}
temp = temp->next;
}
//freeing the node
if (temp->prev != NULL)
temp->prev->next = temp->next;
else { //it is the head
head = head->next;
if (head != NULL) //list's empty?
head->prev = NULL;
}
if (temp->next != NULL)
temp->next->prev = temp->prev;
free(temp);
}
else if (WIFEXITED(exitCode) && WEXITSTATUS(exitCode) == 2) { //missing parameters
continue;
}
else { //finished with an error
return(-1);
}
}
}
//OPTION NUMBER 2 : LOGIN LECTURER COMMAND
else if (strcmp(token, "LoginLecturer") == 0) {
username = strtok(NULL, " ");
password = strtok(NULL, " ");
pid = fork();
if (pid == -1) { //error
write(2, "Fork failure.\n", strlen("Fork failure.\n"));
return (-1);
}
else if (pid == 0) { //child process
execlp("./LoginLecturer", "./LoginLecturer", username, password, NULL);
write(2, "LoginLecturer execution failure.\n", strlen("LoginLecturer execution failure.\n"));
return (-1);
}
else { //parent process
waitpid(pid, &exitCode, 0);
//0 >> okay, 1 >> incorrect password
if (WIFEXITED(exitCode) && (WEXITSTATUS(exitCode) == 0 || WEXITSTATUS(exitCode) == 1)) //ok or incorrect password
continue;
else if (WIFEXITED(exitCode) && WEXITSTATUS(exitCode) == 2) //missing paraameters
continue;
else
return (-1);
}
}
//OPTION NUMBER 3 : EXIT COMMAND
else if (strcmp(token, "exit") == 0) {
if (pid == -1) { //error
write(2, "Fork failure.\n", strlen("Fork failure.\n"));
return (-1);
}
else if (pid == 0) { //child process
execlp("./exit", "./exit", NULL);
write(2, "Exit execution failure.\n", strlen("Exit execution failure.\n"));
return (-1);
}
else { //parent process
waitpid(pid, &exitCode, 0);
return 0;
}
}
//OTHERWISE: NOT SUPPORTED
else {
printf("Not Supported\n");
}
}
}