-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.c
1120 lines (1070 loc) · 31.9 KB
/
users.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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "users.h"
#include "simplefs_shell_apis.h"
#include "disk_driver.h"
//all the operations must be executed as root user
//opens or create the file which stores the last uid and gid created by the fs
//return NULL on error
FileHandle *load_ids(DirectoryHandle *dh, Wallet *wallet) {
int res;
//we move into the root directory
while (dh->directory != NULL) {
res = shell_changeDir(dh, "..", wallet);
if (res == FAILED) {
return NULL;
}
}
//we move into etc, if this directory does not exist we create it
res = shell_changeDir(dh, "etc", wallet);
if (res == FAILED) {
res = shell_mkDir(dh, "etc", wallet);
if (res != FAILED) {
res = shell_changeDir(dh, "etc", wallet);
}
}
if (res == FAILED) {
return NULL;
}
//now we load the uid file or create it if it does not exists
FileHandle *ids = shell_openFile(dh, "id", wallet);
if (ids == NULL) {
ids = shell_createFile(dh, "id", wallet);
}
return ids;
}
//given the handle to the id file we load its content on memory
//returns NULL on error
Ids *read_ids(FileHandle *id, Wallet *wallet) {
int res;
//we seek at the beginning of the file to be sure to override everything
res = shell_seekFile(id, 0, wallet);
if (res == FAILED || res == PERM_ERR) {
return NULL;
}
//now we allocate the Ids struct
Ids *ids = (Ids *) malloc(sizeof(Ids));
//we read the uid from file
res = shell_readFile(id, &(ids->last_uid), sizeof(int), wallet);
if (res == FAILED || res == PERM_ERR || res != sizeof(int)) {
free(ids);
return NULL;
}
//we read the gid from file
res = shell_readFile(id, &(ids->last_gid), sizeof(int), wallet);
if (res == FAILED || res == PERM_ERR || res != sizeof(int)) {
free(ids);
return NULL;
}
return ids;
}
//given the handle to the id file we save on it the ids in the wallet
int save_ids(FileHandle *id, Wallet *wallet) {
int res;
//we seek at the beginning of the file to be sure to override everything
res = shell_seekFile(id, 0, wallet);
if (res == FAILED || res == PERM_ERR) {
return res;
}
//we write the uid to file
res = shell_writeFile(id, &(wallet->ids->last_uid), sizeof(int), wallet);
if (res == FAILED || res == PERM_ERR) {
return res;
}
//we write the gid to file
res = shell_writeFile(id, &(wallet->ids->last_gid), sizeof(int), wallet);
if (res == FAILED || res == PERM_ERR) {
return res;
}
return res;
}
//given the fs root it opens the file containing users
//returns NULL on error
FileHandle *load_users(DirectoryHandle *dh, Wallet *wallet) {
int res;
//we check that the given directory handle is the one of the root directory
while (dh->directory != NULL) {
res = shell_changeDir(dh, "..", wallet);
if (res == FAILED) {
return NULL;
}
}
//we move into etc, if this directory does not exist we create it
res = shell_changeDir(dh, "etc", wallet);
if (res == FAILED) {
res = shell_mkDir(dh, "etc", wallet);
if (res != FAILED) {
res = shell_changeDir(dh, "etc", wallet);
}
}
if (res == FAILED) {
return NULL;
}
//now we load the passwd file or create it if it does not exists
FileHandle *usr = shell_openFile(dh, "passwd", wallet);
if (usr == NULL) {
usr = shell_createFile(dh, "passwd", wallet);
}
return usr;
}
//given a file handle creates a User list
ListHead *read_users(FileHandle *users, Wallet *wallet) {
int res;
//we seek at the beginning of the file to be sure to read everything
res = shell_seekFile(users, 0, wallet);
if (res == FAILED || res == PERM_ERR) {
return NULL;
}
//we create the ListHead
ListHead *usrlst = (ListHead *) malloc(sizeof(ListHead));
usrlst->first = NULL;
usrlst->last = NULL;
//we create the first list element
ListElement *lst = (ListElement *) malloc(sizeof(ListElement));
//we initialize the list element
lst->prev = NULL;
lst->next = NULL;
lst->item = NULL;
//we add it in the list head
usrlst->first = lst;
//we allocate the first user element
User *tmp = (User *) malloc(sizeof(User));
//this variable indicates if the last read operation was successful
int usr_read = SUCCESS;
//we read all the user in the file
while (usr_read == SUCCESS) {
//we read a user from the file and fill it into a User element
res = shell_readFile(users, tmp, sizeof(User), wallet);
if (res == sizeof(User)) {
//we fit the user into the list
lst->item = tmp;
//we create and initialize the new list element
lst->next = (ListElement *) malloc(sizeof(ListElement));
lst->next->prev = lst;
lst = lst->next;
lst->item = NULL;
tmp = (User *) malloc(sizeof(User));
lst->next = NULL;
usr_read = SUCCESS;
} else {
usr_read = FAILED;
}
}
//we deallocate the last list element because it's empty when we have finished reading from file
free(tmp);
if (lst->prev != NULL) {
lst = lst->prev;
free(lst->next);
lst->next = NULL;
//we save the last element in the ListHead
usrlst->last = lst;
} else {
//our list is empty, so we deallocate the list head
free(lst);
free(usrlst);
usrlst = NULL;
}
if (res != FAILED) {
return usrlst;
} else {
delete_list(usrlst);
}
return NULL;
}
//it saves the users list into the file
int save_users(FileHandle *users, ListHead *data, Wallet *wallet) {
if (data == NULL) {
return FAILED;
}
ListElement *lst = data->first;
int res;
//we seek at the beginning of the file to be sure to override everything
res = shell_seekFile(users, 0, wallet);
if (res == FAILED || res == PERM_ERR) {
return res;
}
User *usr;
while (lst != NULL && res != FAILED) {
usr = lst->item;
if (usr != NULL) {
res = shell_writeFile(users, usr, sizeof(User), wallet);
lst = lst->next;
}
}
if (res == FAILED || res == PERM_ERR) {
return res;
} else {
return SUCCESS;
}
}
// adds a new user
//new users are always added in the tail of the list
int useradd(char *username, DirectoryHandle *dh, Wallet *wallet) {
//check if the given string respects the memory limit
if (strlen(username) > NAME_LENGTH || username == NULL) {
return FAILED;
}
// we check if the user list is invalid
if (wallet->user_list == NULL) {
return FAILED;
}
int current_user = wallet->current_user->uid;
//we check if we have enough permissions
if (current_user != ROOT) {
return PERM_ERR;
}
//we check if the username it's already in use
ListElement *usr_lst = usrsrc(wallet, username, 0);
if (usr_lst != NULL) {
return FAILED;
}
//we get the last item in the user list
ListElement *lst = wallet->user_list->last;
//if the username it's not used we calculate a new uid and (the last uid+1)
//we create the user group
int res = groupadd(username, wallet);
if (res == FAILED || res == PERM_ERR) {
return res;
}
//we now create the new user
User *new_usr = (User *) malloc(sizeof(User));
memset(new_usr->account, '\0', NAME_LENGTH * sizeof(char));
memcpy(new_usr->account, username, strlen(username));
new_usr->uid = wallet->ids->last_uid + 1;
new_usr->gid = res;
//we create the new list Element
ListElement *new_lst = (ListElement *) malloc(sizeof(ListElement));
new_lst->prev = lst;
new_lst->next = NULL;
new_lst->item = new_usr;
lst->next = new_lst;
//we update the ListHead
wallet->user_list->last = new_lst;
//we save the user list into its file
res = save_users(wallet->user_file, wallet->user_list, wallet);
if (res == FAILED || res == PERM_ERR) {
lst->next = NULL;
free(new_usr);
return res;
}
//now we add the new user in its group
res = gpasswd(username, username, wallet, ADD);
if (res == FAILED || res == PERM_ERR) {
//we delete the created user and return the error
userdel(username, dh, wallet);
return res;
}
//now we update the Ids structure
wallet->ids->last_uid++;
res = save_ids(wallet->ids_file, wallet);
if (res == FAILED || res == PERM_ERR) {
userdel(username, dh, wallet);
return res;
}
//now we need to create the user folder
//we get a new directory handle to /
DirectoryHandle *dh2 = NULL;
//this variabile controla if we must deallocate the handle at the end of the function
int to_be_freed = 0;
if (dh->directory == NULL) {
//if we are in the root directory we use this handle
dh2 = dh;
} else {
//or we get tha handle to the root directory
dh2 = fs_init(dh->sfs, dh->sfs->disk, wallet->current_user->uid, wallet->current_user->gid);
to_be_freed = 1;
}
//we try to move into the directory "home"
res = shell_changeDir(dh2, "home", wallet);
if (res == FAILED || res == PERM_ERR) {
userdel(username, dh, wallet);
return res;
}
//we create the user directory
res = shell_mkDir(dh2, username, wallet);
if (res == FAILED || res == PERM_ERR) {
userdel(username, dh, wallet);
return res;
}
//we make it owned by the created user
res = shell_chown(dh2, username, username, wallet);
if (res == FAILED || res == PERM_ERR) {
userdel(username, dh, wallet);
shell_removeFile(dh2, username, wallet);
}
//we deallocate the obtained handle
//TODO: check these free
if (to_be_freed) {
free(dh2->current_block);
free(dh2->dcb);
free(dh2->directory);
free(dh2);
}
return res;
}
// deletes a user
int userdel(char *username, DirectoryHandle *dh, Wallet *wallet) {
//check if the given string respects the memory limit
if (strlen(username) > NAME_LENGTH || username == NULL) {
return FAILED;
}
//we check if we have enough permissions
int res;
int current_user = wallet->current_user->uid;
if (current_user != ROOT) {
return PERM_ERR;
}
//we check if the user list is valid
if (wallet->user_list == NULL) {
return FAILED;
}
//we now try to find the user to delete (if exists)
ListElement *usr_lst = usrsrc(wallet, username, 0);
User *usr = NULL;
if (usr_lst != NULL) {
usr = (User *) usr_lst->item;
}
//if we found the user we delete it and update the file
if (usr != NULL) {
//before deleting the user we check if the user primary group contains other users
ListElement *grp_lst = grpsrc(wallet, username, usr->gid);
Group *grp = (Group *) grp_lst->item;
//if the user primary group contains other users we can't delete it
if (grp->group_members[0] == usr->uid && grp->group_members[1] == MISSING) {
res = groupdel(username, wallet);
if (res == FAILED || res == PERM_ERR) {
return res;
}
}
//now we delete the user
ListElement *prev = NULL, *next = NULL;
if (usr_lst->prev == NULL) {
//we delete the first entry in the list
next = usr_lst->next;
if (next != NULL) {
next->prev = NULL;
}
wallet->user_list->first = next;
} else {
if (usr_lst->next == NULL) {
//we delete the last entry in the list
prev = usr_lst->prev;
if (prev != NULL) {
prev->next = NULL;
}
wallet->user_list->last = prev;
} else {
//we delete an entry in between the list
prev = usr_lst->prev;
next = usr_lst->next;
prev->next = next;
next->prev = prev;
}
}
//we deallocate the memory block
free(usr_lst->item);
free(usr_lst);
//we save the changes in the file
res = save_users(wallet->user_file, wallet->user_list, wallet);
if (res == FAILED || res == PERM_ERR) {
//we rollback the changes by reloading the list
delete_list(wallet->user_list);
wallet->user_list = read_users(wallet->user_file, wallet);
CHECK_ERR(wallet->user_list == NULL, "users subsystem compromised!")
}
//now we need to remove the user folder
//we get a new directory handle to /
DirectoryHandle *dh2 = NULL;
//this variabile controla if we must deallocate the handle at the end of the function
int to_be_freed = 0;
if (dh->directory == NULL) {
//if we are in the root directory we use this handle
dh2 = dh;
} else {
//or we get the handle to the root directory
dh2 = fs_init(dh->sfs, dh->sfs->disk, wallet->current_user->uid, wallet->current_user->gid);
to_be_freed = 1;
}
//we remove the user home directory
res = shell_removeFile(dh2, username, wallet);
if (res == PERM_ERR) {
return res;
}
//we deallocate the obtained handle
//TODO: check these free
if (to_be_freed) {
free(dh2->current_block);
free(dh2->dcb);
free(dh2->directory);
free(dh2);
}
return res;
} else {
//otherwise we return failed
return FAILED;
}
}
//given a username or uid it searches the corresponding User
ListElement *usrsrc(Wallet *wallet, char *name, int uid) {
//we check if we have valid data
if ((uid == MISSING && name == NULL) || wallet == NULL || wallet->user_list == NULL) {
return NULL;
}
if (name != NULL) {
if (strlen(name) > NAME_LENGTH) {
return NULL;
}
}
ListElement *lst = wallet->user_list->first;
int found = 0;
//we search the list by username
if (name != NULL) {
while (lst != NULL && !found) {
if (memcmp(name, ((User *) lst->item)->account, strlen(name)) == 0) {
found = 1;
} else {
lst = lst->next;
}
}
} else {
//we search ny uid
while (lst != NULL && !found) {
if (((User *) lst->item)->uid == uid) {
found = 1;
} else {
lst = lst->next;
}
}
}
//we return the result
if (found) {
return lst;
} else {
return NULL;
}
}
//given a username or gid it searches the corresponding Group
ListElement *grpsrc(Wallet *wallet, char *name, int gid) {
//we check if we have valid data
if ((gid == MISSING && name == NULL) || wallet == NULL || wallet->group_list == NULL) {
return NULL;
}
if (name != NULL) {
if (strlen(name) > NAME_LENGTH) {
return NULL;
}
}
ListElement *lst = wallet->group_list->first;
int found = 0;
Group *grp;
//we search the list by name of the group
if (name != NULL) {
while (lst != NULL && lst->item != NULL && !found) {
grp = lst->item;
if (memcmp(name, grp->group_name, strlen(name)) == 0) {
found = 1;
} else {
lst = lst->next;
}
}
} else {
//we search by gid
while (lst != NULL && lst->item != NULL && !found) {
grp = lst->item;
if (grp->gid == gid) {
found = 1;
} else {
lst = lst->next;
}
}
}
//we return the result
if (found) {
return lst;
} else {
return NULL;
}
}
//given the fs root it opens the file containing groups
// returns NULL on error
FileHandle *load_groups(DirectoryHandle *dh, Wallet *wallet) {
int res;
//we check that the given directory handle is the one of the root directory
while (dh->directory != NULL) {
res = shell_changeDir(dh, "..", wallet);
if (res == FAILED) {
return NULL;
}
}
//we move into etc, if this directory does not exist we create it
res = shell_changeDir(dh, "etc", wallet);
if (res == FAILED) {
res = shell_mkDir(dh, "etc", wallet);
if (res != FAILED) {
res = shell_changeDir(dh, "etc", wallet);
}
}
if (res == FAILED) {
return NULL;
}
//now we load the group file or create it if it does not exists
FileHandle *usr = shell_openFile(dh, "group", wallet);
if (usr == NULL) {
usr = shell_createFile(dh, "group", wallet);
}
return usr;
}
//given a file handle creates a group list
ListHead *read_groups(FileHandle *groups, Wallet *wallet) {
int res;
//we seek at the beginning of the file to be sure to override everything
res = shell_seekFile(groups, 0, wallet);
if (res == FAILED || res == PERM_ERR) {
return NULL;
}
//we create our list head
ListHead *grp_lst = (ListHead *) malloc(sizeof(ListHead));
//we allocate memory for the first list element containing the group
ListElement *lst = (ListElement *) malloc(sizeof(ListElement));
lst->item = (Group *) malloc(sizeof(Group));
lst->next = NULL;
lst->prev = NULL;
grp_lst->first = lst;
int grp_read = SUCCESS;
//we read all the user in the file
while (grp_read == SUCCESS) {
//we read a user from the file and fill it into a User element
res = shell_readFile(groups, lst->item, sizeof(Group), wallet);
if (res == sizeof(Group)) {
//we create a new list element to store the next group
lst->next = malloc(sizeof(ListElement));
lst->next->prev = lst;
lst = lst->next;
lst->item = malloc(sizeof(Group));
lst->next = NULL;
grp_read = SUCCESS;
} else {
grp_read = FAILED;
}
}
//we clear the unused list item
free(lst->item);
if (lst->prev != NULL) {
lst = lst->prev;
free(lst->next);
lst->next = NULL;
grp_lst->last = lst;
} else {
//we don't have items in the file, so we deallocate our ListHead
free(lst);
free(grp_lst);
grp_lst = NULL;
}
if (res != FAILED) {
return grp_lst;
} else {
//cleaning up in case of error
delete_list(grp_lst);
}
return NULL;
}
//it saves the users list into the file
int save_groups(FileHandle *groups, ListHead *data, Wallet *wallet) {
if (data == NULL) {
return FAILED;
}
ListElement *lst = data->first;
int res;
//we seek at the beginning of the file to be sure to override everything
res = shell_seekFile(groups, 0, wallet);
if (res == FAILED || res == PERM_ERR) {
return res;
}
while (lst != NULL && res != FAILED) {
res = shell_writeFile(groups, lst->item, sizeof(Group), wallet);
if (res == sizeof(Group)) {
lst = lst->next;
} else {
res = FAILED;
}
}
if (res == FAILED || res == PERM_ERR) {
return res;
} else {
return SUCCESS;
}
}
//check if a user is in a group
int usringrp(User *usr, char *grp_name, int gid, Wallet *wallet) {
//we check if we have valid parameters
if (((grp_name == NULL || strlen(grp_name) > NAME_LENGTH) && gid == MISSING) || wallet == NULL || usr == NULL) {
return FAILED;
}
//we find the group
ListElement *lst = grpsrc(wallet, grp_name, gid);
Group *grp = NULL;
if (lst != NULL) {
grp = lst->item;
}
if (grp == NULL) {
return FAILED;
}
int i = 0;
while (i < GROUP_SIZE) {
if (usr->uid == grp->group_members[i]) {
return SUCCESS;
} else {
if (grp->group_members[i] == MISSING) {
return FAILED;
} else {
i++;
}
}
}
return FAILED;
}
// creates a group, returns the group gid on success
int groupadd(char *name, Wallet *wallet) {
//check if we have valid parameters
if (name == NULL || strlen(name) > NAME_LENGTH || wallet == NULL || wallet->group_list == NULL ||
wallet->group_file == NULL) {
return FAILED;
}
//we search for a group with the same name
if (grpsrc(wallet, name, MISSING) != NULL) {
return FAILED;
}
//we check if we have enough permissions
if (wallet->current_user->uid != ROOT) {
return PERM_ERR;
}
//we need to generate a new gid, which must be unique, to do so we get the last generated gid+1
//we get the last created group
ListElement *lst = wallet->group_list->last;
//now we create a new group
Group *new_grp = (Group *) malloc(sizeof(Group));
new_grp->gid = wallet->ids->last_gid + 1;
//we set the new group as empty
memset(new_grp->group_members, MISSING, GROUP_SIZE * sizeof(int));
memset(new_grp->group_name, '\0', NAME_LENGTH * sizeof(char));
memcpy(new_grp->group_name, name, strlen(name));
//we create the group list element
ListElement *new_lst = (ListElement *) malloc(sizeof(ListElement));
new_lst->item = new_grp;
new_lst->next = NULL;
new_lst->prev = lst;
lst->next = new_lst;
//we update the list head
wallet->group_list->last = new_lst;
//now we save the group list
int res = save_groups(wallet->group_file, wallet->group_list, wallet);
if (res == FAILED || res == PERM_ERR) {
//we revert the changes
lst->next = NULL;
free(new_grp);
free(new_lst);
wallet->user_list->last = lst;
return res;
}
//we save our last used gid
wallet->ids->last_gid++;
res = save_ids(wallet->ids_file, wallet);
CHECK_ERR(res == FAILED || res == PERM_ERR, "can't update the ids file");
return new_grp->gid;
}
//deletes a group
int groupdel(char *name, Wallet *wallet) {
// check if we have the right parameters
if (name == NULL || strlen(name) > NAME_LENGTH || wallet == NULL || wallet->group_list == NULL ||
wallet->group_file == NULL) {
return FAILED;
}
//we check if we have the right permission
if (wallet->current_user->uid != ROOT) {
return PERM_ERR;
}
//we now search for the group to delete
ListElement *grp_lst = grpsrc(wallet, name, MISSING);
if (grp_lst == NULL) {
return FAILED;
}
//now we delete the user
ListElement *prev = NULL, *next = NULL;
if (grp_lst->prev == NULL) {
//we delete the first entry in the list
next = grp_lst->next;
if (next != NULL) {
next->prev = NULL;
}
//we update our list head
wallet->group_list->first = next;
} else {
if (grp_lst->next == NULL) {
//we delete the last entry in the list
prev = grp_lst->prev;
if (prev != NULL) {
prev->next = NULL;
}
//we update the list head
wallet->group_list->last = prev;
} else {
//we delete an entry in between the list
prev = grp_lst->prev;
next = grp_lst->next;
prev->next = next;
next->prev = prev;
}
}
//we deallocate the memory blocks
free(grp_lst->item);
free(grp_lst);
//we save the changes in the file
int res = save_groups(wallet->group_file, wallet->group_list, wallet);
if (res == FAILED || res == PERM_ERR) {
//we rollback the changes by reloading the list
delete_list(wallet->group_list);
wallet->group_list = read_groups(wallet->group_file, wallet);
CHECK_ERR(wallet->group_list == NULL, "users subsystem compromised!")
return res;
}
return res;
}
//adds or deletes a user from a group
int gpasswd(char *group, char *user, Wallet *wallet, int type) {
// we check if we are given correct parameters
if (group == NULL || strlen(group) > NAME_LENGTH || user == NULL || strlen(user) > NAME_LENGTH || wallet == NULL ||
(type != ADD && type != REMOVE)) {
return FAILED;
}
//check if we have the required permissions
if (wallet->current_user->uid != ROOT) {
return PERM_ERR;
}
//we search for the group in which perform the operation
ListElement *grp_lst = grpsrc(wallet, group, MISSING);
if (grp_lst == NULL) {
return FAILED;
}
Group *grp = grp_lst->item;
if (grp == NULL) {
return FAILED;
}
//we search for the user in which perform the operation
ListElement *usr_lst = usrsrc(wallet, user, MISSING);
if (usr_lst == NULL) {
return FAILED;
}
User *usr = usr_lst->item;
if (usr == NULL) {
return FAILED;
}
//we perform the operation
if (type == ADD) {
//we add a user into a given group
//we search for a empty slot in the group while making sure the user isn't already in the group
int i;
for (i = 0; grp->group_members[i] != MISSING && i < GROUP_SIZE; i++) {
//we check if the user is already in the group
if (grp->group_members[i] == usr->uid) {
return FAILED;
}
}
//we check if we can fit another user in the group
if (grp->group_members[i] != MISSING) {
return FAILED;
}
//we insert the user in the group
grp->group_members[i] = usr->uid;
}
if (type == REMOVE) {
//we remove a user from the given group
//we search fo the user in the group
int i, hole = MISSING;
for (i = 0; grp->group_members[i] != MISSING && i < GROUP_SIZE; i++) {
if (grp->group_members[i] == usr->uid) {
//if we have found the user we mark its location to be filled with the last user in the list
hole = i;
}
}
//we check if the user has been found
if (hole == MISSING) {
return FAILED;
}
//if the user to be removed isn't the last one in the list
if (hole < i) {
grp->group_members[hole] = grp->group_members[i];
grp->group_members[i] = MISSING;
} else {
grp->group_members[hole] = MISSING;
}
}
//now we save the changes into the file
int res = save_groups(wallet->group_file, wallet->group_list, wallet);
if (res == FAILED || res == PERM_ERR) {
//we rollback the changes by reloading the list
delete_list(wallet->group_list);
wallet->group_list = read_groups(wallet->group_file, wallet);
CHECK_ERR(wallet->group_list == NULL, "users subsystem compromised!")
}
return res;
}
//given a wallet deallocate it along with its content
void destroy_wallet(Wallet *wallet) {
if (wallet != NULL) {
//we close the handle to /etc/ids
if (wallet->ids_file != NULL) {
shell_closeFile(wallet->ids_file);
}
//we close the handle to /etc/group
if (wallet->group_file != NULL) {
shell_closeFile(wallet->group_file);
}
//we close the handle to /etc/passwd
if (wallet->user_file != NULL) {
shell_closeFile(wallet->user_file);
}
//we delete the group list
if (wallet->group_list != NULL) {
delete_list(wallet->group_list);
}
//we delete the file list
if (wallet->user_list != NULL) {
delete_list(wallet->user_list);
}
//we delete the last used ids
if (wallet->ids != NULL) {
free(wallet->ids);
}
//we delete the wallet
free(wallet);
}
}
//load the user subsytem and if there are no users in the system creates root user
//the directory handle given must be the root of the disk
Wallet *initialize_wallet(DirectoryHandle *dh) {
int res;
//create the root user
User *root = (User *) malloc(sizeof(User));
memset(root->account, '\0', NAME_LENGTH * sizeof(char));
strcpy(root->account, "root\0");
root->uid = ROOT;
root->gid = ROOT;
//initialize the wallet only with the temporary root user
Wallet *wallet = (Wallet *) malloc(sizeof(Wallet));
wallet->current_user = root;
wallet->group_file = NULL;
wallet->group_list = NULL;
wallet->user_file = NULL;
wallet->user_list = NULL;
// try to load the users from the file
wallet->user_file = load_users(dh, wallet);
if (wallet->user_file == NULL) {
destroy_wallet(wallet);
return NULL;
}
wallet->user_list = read_users(wallet->user_file, wallet);
//we check if the file was empty
if (wallet->user_list == NULL) {
//if the file was empty
wallet->user_list = malloc(sizeof(ListHead));
wallet->user_list->first = malloc(sizeof(User));
wallet->user_list->first->item = root;
wallet->user_list->first->next = NULL;
wallet->user_list->first->prev = NULL;
wallet->user_list->last = wallet->user_list->first;
//we save our user list on file
res = save_users(wallet->user_file, wallet->user_list, wallet);
if (res == FAILED || res == PERM_ERR) {
destroy_wallet(wallet);
return NULL;
}
} else {
//we load the root user on file
free(wallet->current_user);
ListElement *tmp_lst = usrsrc(wallet, NULL, ROOT);
User *tmp;
if (tmp_lst != NULL) {
tmp = tmp_lst->item;
wallet->current_user = tmp;
}
}
//now we load the groups from the group file
wallet->group_file = load_groups(dh, wallet);
if (wallet->group_file == NULL) {
destroy_wallet(wallet);
return NULL;
}
//we load the group list
wallet->group_list = read_groups(wallet->group_file, wallet);
if (wallet->group_list == NULL) {
//create the root group if the group file is empty
Group *root_grp = (Group *) malloc(sizeof(Group));
memset(root_grp->group_name, '\0', NAME_LENGTH * sizeof(char));
strcpy(root_grp->group_name, "root");
root_grp->gid = ROOT;
memset(root_grp->group_members, MISSING, GROUP_SIZE * sizeof(int));
root_grp->group_members[0] = ROOT;
wallet->group_list = (ListHead *) malloc(sizeof(ListHead));
wallet->group_list->first = (ListElement *) malloc(sizeof(ListElement));
wallet->group_list->first->item = root_grp;
wallet->group_list->first->next = NULL;
wallet->group_list->first->prev = NULL;
wallet->group_list->last = wallet->group_list->first;
//we save the group list on file
res = save_groups(wallet->group_file, wallet->group_list, wallet);
if (res == FAILED || res == PERM_ERR) {
destroy_wallet(wallet);
return NULL;
}
}
//we load the last used ids from the ids file
wallet->ids_file = load_ids(dh, wallet);
if (wallet->ids_file == NULL) {
destroy_wallet(wallet);
return NULL;
}
//now we try to read from the ids file
wallet->ids = read_ids(wallet->ids_file, wallet);
if (wallet->ids == NULL) {
//we initialize and save the last used uid and gid
wallet->ids = malloc(sizeof(Ids));
wallet->ids->last_uid = ROOT;
wallet->ids->last_gid = ROOT;
res = save_ids(wallet->ids_file, wallet);
if (res == FAILED || res == PERM_ERR) {
destroy_wallet(wallet);
return NULL;
}
}
res = SUCCESS;
//we go back to the root directory
while (dh->directory != NULL && res != FAILED && res != PERM_ERR) {
res = shell_changeDir(dh, "..", wallet);
}
//we check if the root folder is present
res = shell_changeDir(dh, "root", wallet);
if (res == PERM_ERR) {
destroy_wallet(wallet);
return NULL;
}
if (res == FAILED) {
//we create the root folder
res = shell_mkDir(dh, "root", wallet);
if (res == FAILED || res == PERM_ERR) {
destroy_wallet(wallet);
return NULL;
}
}
//we go back to /
res = shell_changeDir(dh, "..", wallet);
if (res == PERM_ERR || res == FAILED) {
destroy_wallet(wallet);
return NULL;