-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
502 lines (489 loc) · 16.1 KB
/
server.c
File metadata and controls
502 lines (489 loc) · 16.1 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
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
#define FIFO1_NAME "server_to_client"
#define FIFO_NAME "client_to_server"
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <utmp.h>
bool logat = false;
char *int_to_char(int n)
{
char *aux;
aux = malloc(20);
int nr = 0;
if(n == 0)
return "0";
while(n)
{
aux[nr++] = (char)(n % 10 + '0');
n /= 10;
}
for(int i = 0; i < nr / 2; i++)
{
char a = aux[i];
aux[i] = aux[nr - i - 1];
aux[nr - i - 1] = a;
}
aux[nr] = '\0';
return aux;
}
void Login(char *s, int fd)
{
int nrc;
s[0] = ';';
strcat(s, ";");
FILE *users = fopen("users.txt", "r");
char sir[500];
fscanf(users, "%s", sir);
if(strstr(sir, s) != NULL)
{
char msg[] = "Utilizator gasit.\n";
nrc = strlen(msg);
if((write(fd, &nrc, sizeof(nrc))) == -1)
{
perror("Problema la scriere in SOCKET, linia 53.\n");
exit(2);
}
if((write(fd, msg, nrc)) == -1)
{
perror("Problema la scriere in SOCKET, linia 58.\n");
exit(2);
}
}
else
{
char msg[] = "Utilizator negasit.\n";
nrc = strlen(msg);
if((write(fd, &nrc, sizeof(nrc))) == -1)
{
perror("Problema la scriere in SOCKET, linia 68.\n");
exit(2);
}
if((write(fd, msg, nrc)) == -1)
{
perror("Problema la scriere in SOCKET, linia 73.\n");
exit(2);
}
}
}
void get_logged_users(int fd)
{
int len = 0;
char aux[500];
if(!logat)
{
strcpy(aux, "Utilizatorul nu este logat.\n");
len = strlen(aux);
if((write(fd, &len, sizeof(len))) == -1)
{
perror("Problema la scriere in PIPE, linia 89.\n");
exit(2);
}
if((write(fd, aux, len)) == -1)
{
perror("Problema la scriere in PIPE, linia 94.\n");
exit(2);
}
}
else
{
//username, hostname for remote login, time entry was made
strcpy(aux, "");
struct utmp* data;
data = getutent();
while(data != NULL)
{
strcat(aux, "Username: ");
strcat(aux, data->ut_user);
strcat(aux, ", hostname for remote login: ");
strcat(aux, data->ut_host);
strcat(aux, ", time entry was made: ");
strcat(aux, int_to_char(data->ut_tv.tv_sec));
strcat(aux, " s, ");
strcat(aux, int_to_char(data->ut_tv.tv_usec));
strcat(aux, " ms.\n");
len = len + 71 + strlen(data->ut_user) + strlen(data->ut_host) + strlen(int_to_char(data->ut_tv.tv_sec)) + strlen(int_to_char(data->ut_tv.tv_usec));
data = getutent();
}
if((write(fd, &len, sizeof(len))) == -1)
{
perror("Problema la scriere in PIPE, linia 120.\n");
exit(2);
}
if((write(fd, aux, len)) == -1)
{
perror("Problema la scriere in PIPE, linia 125.\n");
exit(2);
}
}
}
void get_proc_info(char *pid, int fdd)
{
char sir[500];
if(!logat)
{
strcpy(sir, "Utilizatorul nu este logat.\n");
int nr = strlen(sir);
if((write(fdd, &nr, sizeof(nr))) == -1)
{
perror("Problema la scriere in SOCKET, linia 140.\n");
exit(2);
}
if((write(fdd, sir, nr)) == -1)
{
perror("Problema la scriere in SOCKET, linia 145.\n");
exit(2);
}
}
else
{
//name, state, ppid, uid, vmsize
char *pid1, sir1[500];
int lungimea_finala = 0;
pid1 = malloc(strlen(pid));
strcpy(pid1, pid + 1);
strcpy(pid, pid1);
char path[50] = "/proc/";
strcat(path, pid);
strcat(path, "/status");
FILE *fd;
fd = fopen(path, "r");
int i = 1;
while(!feof(fd))
{
fgets(sir, 500, fd);
if(i == 1 || i == 3 || i == 7 || i == 9 || i == 18)
{
if(i == 1)
strcpy(sir1, sir);
else
strcat(sir1, sir);
}
i++;
}
lungimea_finala = strlen(sir1);
if((write(fdd, &lungimea_finala, sizeof(lungimea_finala))) == -1)
{
perror("Problema la scriere in SOCKET, linia 178.\n");
exit(2);
}
if((write(fdd, sir1, lungimea_finala)) == -1)
{
perror("Problema la scriere in SOCKET, linia 183.\n");
exit(2);
}
}
}
void Logout(int fd)
{
logat = false;
char msg[] = "Utilizator delogat.\n";
int nr = strlen(msg);
if((write(fd, &nr, sizeof(nr))) == -1)
{
perror("Problema la scriere in FIFO, linia 196.\n");
exit(2);
}
if((write(fd, msg, strlen(msg))) == -1)
{
perror("Problema la scriere in FIFO, linia 201.\n");
exit(2);
}
}
void quit(int pid, int fd)
{
kill(pid, SIGINT);
// raise(SIGINT);
}
void comanda_necunoscuta(int fd)
{
char msg[] = "Nu s-a gasit nicio comanda recunoscuta.\n";
int nrc = strlen(msg);
if((write(fd, &nrc, sizeof(nrc))) == -1)
{
perror("Problema la scriere in FIFO, linia 218.\n");
exit(2);
}
if((write(fd, msg, nrc)) == -1)
{
perror("Problema la scriere in FIFO, linia 223.\n");
exit(2);
}
}
void server()
{
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
while(1)
{
char s[1024], comanda[50];
int fd, pid, lungime_s;
fd = open(FIFO_NAME, O_RDONLY);
struct stat stat_record;
stat(FIFO_NAME, &stat_record);
while(stat_record.st_size == 0)
{
stat(FIFO_NAME, &stat_record);
sleep(1);
}
if(read(fd, &pid, sizeof(pid)) == -1)
{
perror("Eroare la read, linia 245.\n");
exit(1);
}
if(read(fd, &lungime_s, sizeof(lungime_s)) == -1)
{
perror("Eroare la read, linia 250.\n");
exit(1);
}
if(read(fd, s, lungime_s) == -1)
{
perror("Eroare la read, linia 255.\n");
exit(1);
}
s[lungime_s] = '\0';
close(fd);
fd = open(FIFO_NAME, O_WRONLY | O_TRUNC);
int fd1;
fd1 = open(FIFO1_NAME, O_CREAT | O_TRUNC | O_WRONLY);
strcpy(comanda, "");
if(s[3] == 'i')
strncpy(comanda, s, strlen(s) - strlen(strchr(s, ' ')));
else if(s[4] == 'p')
{
int cate_caractere_iau = strlen(s) - strlen(strchr(s, ' '));
unsigned int i;
for(i = 0; i < cate_caractere_iau; i++)
comanda[i] = s[i];
comanda[cate_caractere_iau] = '\0';
}
else
strcpy(comanda, s);
int len = strlen(comanda);
if(strstr(comanda, "login") && comanda[5] != '!')
comanda[5] = '\0';
if(!strcmp(comanda, "login") || !strcmp(comanda, "login:"))
{
logat = true;
int sockp[2], pidd;
if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockp) < 0)
{
perror("Eroare la socketpair, linia 285.\n");
exit(3);
}
if((pidd = fork()) == -1)
{
perror("Eroare la fork, linia 290.\n");
exit(4);
}
else
if(pidd) //parinte
{
close(sockp[0]);
int nrc;
char msg[500];
strcpy(msg, strrchr(s, ' '));
nrc = strlen(msg);
if(write(sockp[1], &nrc, sizeof(nrc)) < 0)
{
perror("Eroare la scrierea in socket, linia 303.\n");
exit(2);
}
if(write(sockp[1], msg, nrc) < 0)
{
perror("Eroare la scrierea in socket, linia 308.\n");
exit(2);
}
wait(NULL);
if(read(sockp[1], &nrc, sizeof(nrc)) < 0)
{
perror("Eroare la citirea din socket, linia 314.\n");
exit(1);
}
if(read(sockp[1], msg, nrc) < 0)
{
perror("Eroare la citirea din socket, linia 319.\n");
exit(1);
}
msg[nrc] = '\0';
if(write(fd1, &nrc, sizeof(nrc)) < 0)
{
perror("Eroare la scrierea in fifo, linia 325.\n");
exit(2);
}
if(write(fd1, msg, nrc) < 0)
{
perror("Eroare la scrierea in fifo, linia 330.\n");
exit(2);
}
close(sockp[1]);
}
else //copil
{
close(sockp[1]);
char msg[500];
int nrc;
if(read(sockp[0], &nrc, sizeof(nrc)) < 0)
{
perror("Eroare la citirea din socket, linia 342.\n");
exit(1);
}
if(read(sockp[0], msg, nrc) < 0)
{
perror("Eroare la citirea din socket, linia 347.\n");
exit(1);
}
msg[nrc] = '\0';
Login(msg, sockp[0]);
close(sockp[0]);
exit(7);
}
}
else
if(!strcmp(comanda, "get-logged-users"))
{
int pp[2], pidd;
if(pipe(pp) == -1)
{
perror("Eroare la pipe, linia 362.\n");
exit(5);
}
char buff[500];
if((pidd = fork()) == -1)
{
perror("Eroare la fork, linia 368.\n");
exit(4);
}
else
if(pidd) //parinte
{
close(pp[1]);
char msg[500];
wait(NULL);
int len;
if(read(pp[0], &len, sizeof(len)) < 0)
{
perror("Eroare la citirea din pipe, linia 380.\n");
exit(1);
}
if(read(pp[0], msg, len) < 0)
{
perror("Eroare la citirea din pipe, linia 385.\n");
exit(1);
}
if(write(fd1, &len, sizeof(len)) < 0)
{
perror("Eroare la scrierea in fifo, linia 390.\n");
exit(2);
}
if(write(fd1, msg, len) < 0)
{
perror("Eroare la scrierea in fifo, linia 395.\n");
exit(2);
}
close(pp[0]);
}
else //copil
{
close(pp[0]);
get_logged_users(pp[1]);
close(pp[1]);
exit(9);
}
}
else
if(!strcmp(comanda, "get-proc-info") || !strcmp(comanda, "get-proc-info:"))
{
int sockp[2], pidd;
if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockp) < 0)
{
perror("Eroare la socketpair, linia 414.\n");
exit(3);
}
if((pidd = fork()) == -1)
{
perror("Eroare la fork, linia 419.\n");
exit(4);
}
else
if(pidd) //parinte
{
close(sockp[0]);
char msg[500];
strcpy(msg, strrchr(s, ' '));
if(write(sockp[1], msg, sizeof(msg)) < 0)
{
perror("Eroare la scrierea in socket, linia 430.\n");
exit(2);
}
wait(NULL);
int lung;
if(read(sockp[1], &lung, sizeof(lung)) < 0)
{
perror("Eroare la citirea din socket, linia 437.\n");
exit(1);
}
if(read(sockp[1], msg, lung) < 0)
{
perror("Eroare la citirea din socket, linia 442.\n");
exit(1);
}
msg[lung] = '\0';
if(write(fd1, &lung, sizeof(lung)) < 0)
{
perror("Eroare la scrierea in fifo, linia 448.\n");
exit(2);
}
if(write(fd1, msg, lung) < 0)
{
perror("Eroare la scrierea in fifo, linia 453.\n");
exit(2);
}
close(sockp[1]);
}
else //copil
{
close(sockp[1]);
char msg[500];
if(read(sockp[0], msg, sizeof(msg)) < 0)
{
perror("Eroare la citirea din socket, linia 464.\n");
exit(1);
}
get_proc_info(msg, sockp[0]);
close(sockp[0]);
exit(11);
}
}
else
if(!strcmp(comanda, "logout"))
Logout(fd1);
else
if(!strcmp(comanda, "quit"))
quit(pid, fd1);
else
comanda_necunoscuta(fd1);
}
}
int main()
{
server();
return 0;
}
/*
Are mai multe procese-copil.
Trimite clientului raspunsuri sub forma de siruri de octeti prefixate de lungimea raspunsului.
Procesele copil nu comunica direct cu clientul, ci doar cu procesul parinte.
Protocolul minimal cuprinde comenzile:
- "login : username" - a carei existenta este validata prin utilizarea unui fisier de configurare, care contine toti utilizatorii care au acces la functionalitati. Executia comenzii va fi realizata intr-un proces copil din server;
- "get-logged-users" - afiseaza informatii (username, hostname for remote login, time entry was made) despre utilizatorii autentificati pe sistemul de operare (vezi "man 5 utmp" si "man 3 getutent"). Aceasta comanda nu va putea fi executata daca utilizatorul nu este autentificat in aplicatie. Executia comenzii va fi realizata intr-un proces copil din server;
- "get-proc-info : pid" - afiseaza informatii (name, state, ppid, uid, vmsize) despre procesul indicat (sursa informatii: fisierul /proc/<pid>/status). Aceasta comanda nu va putea fi executata daca utilizatorul nu este autentificat in aplicatie. Executia comenzii va fi realizata intr-un proces copil din server;
- "logout";
- "quit".
*/