-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
474 lines (368 loc) · 11.4 KB
/
main.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
// gcc main.c -o main -no-pie
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <zconf.h>
#include <fcntl.h>
const int MAX_LEN = 0xfffff;
const int INPUT_LEN = 50;
const int AGE_LEN = 25;
const int MAX_PASSENGERS_COUNT = 50;
int PASSENGERS_COUNT = 0;
int CREWMANS_COUNT = 0;
int PASSANGER_NUMBER = 0;
struct passenger {
int id;
unsigned char is_frozen;
unsigned char age;
unsigned char is_crewman;
unsigned char token[7];
unsigned char country[100];
unsigned char first_name[100];
unsigned char last_name [100];
void (*on_unfreeze_ptr)(struct passenger*);
void (*on_freeze_ptr)(struct passenger*);
void (*info_ptr)(struct passenger*);
};
struct passenger *ship[50];
struct passenger *applier;
char **used_tokens;
void on_unfreeze(struct passenger *pass) {
printf("\n[+] The passenger is unfrozen!\n");
printf("============================\n");
pass_info(pass);
printf("============================\n");
printf("\n");
}
void on_freeze(struct passenger *pass) {
printf("\n[+] The passenger is frozen!\n");
printf("============================\n");
pass_info(pass);
printf("============================\n");
printf("\n");
}
void info(struct passenger *pass) {
printf("[*] The passenger's information:\n");
pass_info(pass);
printf("\n");
}
void pass_info(struct passenger *pass) {
printf(" First name: %s\n", pass->first_name);
printf(" Last name: %s\n", pass->last_name);
printf(" Age: %d\n", pass->age);
}
struct passenger* create_passenger(char *first_name, char *last_name, char *country, long age) {
struct passenger *passng = malloc(sizeof(struct passenger));
passng->id = PASSENGERS_COUNT++;
passng->is_frozen = 0;
passng->on_freeze_ptr = on_freeze;
passng->on_unfreeze_ptr = on_unfreeze;
passng->info_ptr = info;
strcpy(passng->first_name, first_name);
strcpy(passng->last_name, last_name);
strcpy(passng->country, country);
memset(passng->token, 0, 7);
passng->is_crewman = 0;
long *_age = (long *) &passng->age;
*_age = age;
return passng;
}
struct passenger* set_crewman_privilege(struct passenger *pass, char *token) {
int len = strlen(token);
if (len >= 7)
token[6] = '\x00';
pass->is_crewman = 1;
strcpy(pass->token, token);
used_tokens[CREWMANS_COUNT] = malloc(strlen(pass->token));
strcpy(used_tokens[CREWMANS_COUNT++], pass->token);
return pass;
}
int add_passenger(struct passenger *pass) {
if (MAX_PASSENGERS_COUNT <= PASSANGER_NUMBER) {
return 1;
}
ship[PASSANGER_NUMBER] = pass;
PASSANGER_NUMBER++;
return 0;
}
void init_ship() {
used_tokens = malloc(MAX_PASSENGERS_COUNT * sizeof(char *));
add_passenger(set_crewman_privilege(create_passenger("John", "Smith", "ENG", 39), "rim96z"));
add_passenger(set_crewman_privilege(create_passenger("Maria", "Garcia", "ENG", 24), "tjqb2v"));
add_passenger(set_crewman_privilege(create_passenger("Konata", "Izumi", "JAP", 18), "99iap3"));
add_passenger(set_crewman_privilege(create_passenger("Dmitry", "Sokolov", "RUS", 32), "nv7btm"));
add_passenger(set_crewman_privilege(create_passenger("Lucas", "Aris", "FRA", 23), "th4a80"));
}
void input_string(char *str, int size) {
fgets(str, size, stdin);
str[strlen(str) - 1] = 0;
}
int input_choice() {
int choice;
char buf[100];
input_string(buf, sizeof(buf));
int sscanf_res = sscanf(buf, "%d", &choice);
if ((sscanf_res == 0) || (sscanf_res == EOF)) {
choice = -1;
}
return choice;
}
long parse_long(char *s) {
char *ptr;
return strtol(s, &ptr, 10);
}
void header() {
printf("============ WELCOME TO OUR SHiP ============\n");
}
int input_age(long *real_age) {
char age[MAX_LEN];
input_string(age, AGE_LEN);
*real_age = parse_long(age);
if (*real_age <= 18) {
printf("[-] Invalid age, try again!\n\n");
return 0;
} else return 1;
}
struct passenger* apply() {
long real_age = 0;
char firstname[MAX_LEN], lastname[MAX_LEN], country[MAX_LEN];
printf("\nThank you for applying!\nPlease, full the fields below:\n");
printf("============================\n");
printf(" First name [50]: ");
input_string(firstname, INPUT_LEN);
printf(" Last name [50]: ");
input_string(lastname, INPUT_LEN);
printf(" Country [50]: ");
input_string(country, INPUT_LEN);
printf(" Age: ");
if (!input_age(&real_age)) return NULL;
struct passenger *pass = create_passenger(firstname, lastname, country, real_age);
add_passenger(pass);
printf("[+] Done!\n");
return pass;
}
void show_list() {
print_file_content("./applicants");
}
int crewman_enter(struct passenger *pass) {
printf("\n[*] Entering as a member of the ship crew...\n");
printf("[*] Checking is running...\n");
sleep(2);
if (pass->is_crewman == 1) {
for (int i = 0; i < CREWMANS_COUNT; i++) {
if (ship[i]->is_crewman && !strcmp(used_tokens[i], pass->token))
printf("[+] Done. Welcome!\n");
return 1;
}
}
printf("[-] Invalid credentials.\n");
return 0;
}
void crew_list() {
char buf[MAX_LEN];
for (int i = 0; i < PASSANGER_NUMBER; i++) {
sprintf(buf, "%d", (int) ship[i]->age);
printf("============================\n");
printf(" * First Name: %s\n", ship[i]->first_name);
printf(" * Last Name: %s\n", ship[i]->last_name);
printf(" * Country: %s\n", ship[i]->country);
printf(" * Age: %s\n", buf);
printf(" * Token: %s\n", ship[i]->token);
}
printf("============================\n\n");
}
void ship_info() {
printf("[-] This function isn\'t implemented yet\n");
}
struct passenger* find_passenger(int id) {
for (int i = 0; i < PASSANGER_NUMBER; i++)
if (ship[i]->id == id) {
return ship[i];
}
return NULL;
}
void freeze(int object_id) {
struct passenger *pass_ptr = find_passenger(object_id);
if (pass_ptr == NULL) {
printf("[-] Invalid id for this operation.\n");
return;
}
if (pass_ptr->is_frozen) {
printf("[-] Passenger is already frozen.\n");
return;
}
pass_ptr->is_frozen = 1;
pass_ptr->on_freeze_ptr(pass_ptr);
}
void unfreeze(int object_id) {
struct passenger *pass_ptr = find_passenger(object_id);
if (pass_ptr == NULL) {
printf("[-] Invalid id for this operation.\n");
return;
}
if (!pass_ptr->is_frozen) {
printf("[-] Passenger isn\'t frozen.\n");
return;
}
pass_ptr->is_frozen = 0;
pass_ptr->on_unfreeze_ptr(pass_ptr);
}
void print_file_content(char *filename) {
char c;
FILE *fd = fopen(filename, "r");
if (fd) {
while ((c = getc(fd)) != EOF)
putchar(c);
printf("\n");
fclose(fd);
return;
}
printf("[-] Error while opening the file!\n");
return;
}
void print_flag2() {
print_file_content("./flag2");
}
void update_info() {
if (applier == NULL) {
printf("[-] You haven\'t sent your aplication yet.\n");
return;
}
int choice = -1;
long real_age;
char buf[MAX_LEN];
while (choice != 5) {
printf("\n[1] Change first name.\n");
printf("[2] Change last name.\n");
printf("[3] Change country.\n");
printf("[4] Change age.\n");
printf("[5] Back.\n");
printf(">> ");
choice = input_choice();
switch(choice) {
case 1:
printf("\nNew first name: ");
input_string(buf, 111);
strcpy(applier->first_name, buf);
break;
case 2:
printf("\nNew last name: ");
input_string(buf, 111);
strcpy(applier->last_name, buf);
break;
case 3:
printf("\nNew country: ");
input_string(buf, 111);
strcpy(applier->country, buf);
break;
case 4:
real_age = 0;
printf("\nNew age: ");
if (input_age(&real_age)) {
long *age = (long *) &applier->age;
*age = real_age;
}
break;
case 5:
break;
default:
printf("[-] Invalid option.\n");
}
}
}
void crewman_menu() {
int choice = -1, index;
printf("\n\n++++=== WELCOME TO THE CREWMAN TERMINAL ===++++\n");
while (choice != 6) {
printf("\n[1] List of members of ship\'s crew.\n");
printf("[2] Information about the ship.\n");
printf("[3] Freeze a crewman.\n");
printf("[4] Unfreeze a crewman.\n");
printf("[5] Show secret information.\n");
printf("[6] Back.\n");
printf(">> ");
choice = input_choice();
switch (choice) {
case 1:
crew_list();
break;
case 2:
ship_info();
break;
case 3:
printf("Passenger\'s index: ");
index = input_choice();
freeze(index);
break;
case 4:
printf("Passenger\'s index: ");
index = input_choice();
unfreeze(index);
break;
case 5:
print_file_content("./flag1");
break;
case 6:
printf("\n=== WELCOME TO THE APPLICANT TERMINAL ===\n");
break;
default:
printf("[-] Invalid option.\n");
}
}
}
void applicant_menu() {
int choice = -1, is_applied = 0;
printf("=== WELCOME TO THE APPLICANT TERMINAL ===\n");
while (choice != 5) {
printf("\n[1] Apply for a passenger.\n");
printf("[2] Show the list of applicants.\n");
printf("[3] Change information in your application.\n");
printf("[4] Show you application.\n");
printf("[5] Exit.\n");
printf(">> ");
choice = input_choice();
switch (choice) {
case 31337:
if (applier != NULL && crewman_enter(applier))
crewman_menu();
break;
case 1:
if (is_applied == 1) {
printf("[-] You're already applied!\n");
break;
}
applier = apply();
if (applier)
is_applied = 1;
break;
case 2:
show_list();
break;
case 3:
update_info();
break;
case 4:
if (applier) {
applier->info_ptr(applier);
} else {
printf("[-] Please, make an application first.\n");
}
break;
case 5:
printf("See you later!\n");
break;
default:
printf("[-] Invalid option.\n");
}
}
}
int main(int argc, char **argv) {
setbuf(stdin, 0);
setbuf(stdout, 0);
setbuf(stderr, 0);
init_ship();
header();
applicant_menu();
return 0;
}