-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradm_parse.c
1203 lines (1028 loc) · 29.3 KB
/
gradm_parse.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
/*
* Copyright (C) 2002-2015 Bradley Spengler, Open Source Security, Inc.
* http://www.grsecurity.net spender@grsecurity.net
*
* This file is part of gradm.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "gradm.h"
extern FILE *gradmin;
extern int gradmparse(void);
void set_role_umask(struct role_acl *role, u_int16_t umask)
{
role->umask = umask;
}
char *strip_trailing_slash(char *filename)
{
unsigned int file_len = strlen(filename);
if (file_len > 1 && filename[file_len - 1] == '/')
filename[file_len - 1] = '\0';
if (file_len >= PATH_MAX) {
fprintf(stderr, "Filename too long on line %lu of file %s.\n",
lineno, current_acl_file);
exit(EXIT_FAILURE);
}
return filename;
}
static int get_id_from_role_name(const char *rolename, u_int16_t type, int *retid)
{
unsigned long the_id = 0;
struct passwd *pwd;
struct group *grp;
char *endptr;
if (type & GR_ROLE_USER) {
pwd = getpwnam(rolename);
if (!pwd) {
/* now try it as a uid */
the_id = strtoul(rolename, &endptr, 10);
if (*endptr == '\0')
pwd = getpwuid((int)the_id);
if (the_id > INT_MAX || *endptr != '\0') {
fprintf(stderr, "Warning: User %s on line %lu of %s "
"is invalid.\n", rolename,
lineno, current_acl_file);
return 1;
}
}
if (pwd)
the_id = pwd->pw_uid;
/* else, the_id obtained above via strtoul is valid */
} else if (type & GR_ROLE_GROUP) {
grp = getgrnam(rolename);
if (!grp) {
/* now try it as a gid */
the_id = strtoul(rolename, &endptr, 10);
if (*endptr == '\0')
grp = getgrgid((int)the_id);
if (the_id > INT_MAX || *endptr != '\0') {
fprintf(stderr, "Warning: Group %s on line %lu of %s "
"is invalid.\n", rolename,
lineno, current_acl_file);
return 1;
}
}
if (grp)
the_id = grp->gr_gid;
/* else, the_id obtained above via strtoul is valid */
}
*retid = (int)the_id;
return 0;
}
void
add_id_transition(struct proc_acl *subject, const char *idname, int usergroup, int allowdeny)
{
int i;
int id;
int ret;
if (usergroup == GR_ID_USER) {
if ((subject->user_trans_type | allowdeny) == (GR_ID_ALLOW | GR_ID_DENY)) {
fprintf(stderr, "Error on line %lu of %s. You cannot use "
"both user_transition_allow and user_transition_deny.\n"
"The RBAC system will not be allowed to be enabled until "
"this error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
subject->user_trans_type |= allowdeny;
ret = get_id_from_role_name(idname, GR_ROLE_USER, &id);
if (ret)
return;
/* dupecheck */
for (i = 0; i < subject->user_trans_num; i++)
if (subject->user_transitions[i] == id)
return;
/* increment pointer count upon allocation of user transition list */
if (subject->user_transitions == NULL)
num_pointers++;
subject->user_trans_num++;
subject->user_transitions = (uid_t *)gr_realloc(subject->user_transitions, subject->user_trans_num * sizeof(uid_t));
subject->user_transitions[subject->user_trans_num - 1] = id;
} else if (usergroup == GR_ID_GROUP) {
if ((subject->group_trans_type | allowdeny) == (GR_ID_ALLOW | GR_ID_DENY)) {
fprintf(stderr, "Error on line %lu of %s. You cannot use "
"both group_transition_allow and group_transition_deny.\n"
"The RBAC system will not be allowed to be enabled until "
"this error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
subject->group_trans_type |= allowdeny;
ret = get_id_from_role_name(idname, GR_ROLE_GROUP, &id);
if (ret)
return;
/* dupecheck */
for (i = 0; i < subject->group_trans_num; i++)
if (subject->group_transitions[i] == id)
return;
/* increment pointer count upon allocation of group transition list */
if (subject->group_transitions == NULL)
num_pointers++;
subject->group_trans_num++;
subject->group_transitions = (gid_t *)gr_realloc(subject->group_transitions, subject->group_trans_num * sizeof(gid_t));
subject->group_transitions[subject->group_trans_num - 1] = id;
}
return;
}
static int
is_role_dupe(struct role_acl *role, const char *rolename, const u_int16_t type)
{
struct role_acl *tmp;
int id = 0;
int ret;
int i;
if ((type & GR_ROLE_ISID) || ((type & (GR_ROLE_USER | GR_ROLE_GROUP)) && !(type & GR_ROLE_DOMAIN))) {
ret = get_id_from_role_name(rolename, type, &id);
if (ret)
return 0;
}
for_each_role(tmp, role) {
if ((tmp->roletype & (GR_ROLE_USER | GR_ROLE_GROUP | GR_ROLE_SPECIAL) & type) && !strcmp(tmp->rolename, rolename))
return 1;
if ((tmp->roletype & GR_ROLE_DOMAIN) && (type & (GR_ROLE_USER | GR_ROLE_GROUP))) {
for (i = 0; i < tmp->domain_child_num; i++) {
if (tmp->domain_children[i] == id)
return 1;
}
}
}
return 0;
}
void
add_domain_child(struct role_acl *role, const char *idname)
{
int ret, id;
if (!(role->roletype & (GR_ROLE_USER | GR_ROLE_GROUP))) {
// should never get here
fprintf(stderr, "Unhandled exception 1.\n");
exit(EXIT_FAILURE);
}
if (is_role_dupe(current_role, idname, role->roletype | GR_ROLE_ISID)) {
fprintf(stderr, "Duplicate role %s on line %lu of %s.\n"
"The RBAC system will not be allowed to be "
"enabled until this error is fixed.\n",
idname, lineno, current_acl_file);
exit(EXIT_FAILURE);
}
ret = get_id_from_role_name(idname, role->roletype, &id);
if (ret)
return;
/* reason for this is that in the kernel, the hash table which is keyed by UID/GID
has a size dependent on the number of roles. Since we want to fake a domain
as being a real role for each of those users/groups by providing a pointer
to the domain for each user/group, we need to count each of these against the
role count */
num_domain_children++;
/* increment pointer count upon allocation of domain list */
if (role->domain_children == NULL)
num_pointers++;
role->domain_child_num++;
role->domain_children = (gid_t *)gr_realloc(role->domain_children, role->domain_child_num * sizeof(gid_t));
*(role->domain_children + role->domain_child_num - 1) = id;
return;
}
void
add_role_transition(struct role_acl *role, const char *rolename)
{
struct role_transition **roletpp;
struct role_transition *roletp;
/* one for transition, one for name */
num_pointers += 2;
roletp = (struct role_transition *) gr_alloc(sizeof (struct role_transition));
roletpp = &(role->transitions);
if (*roletpp)
(*roletpp)->next = roletp;
roletp->prev = *roletpp;
roletp->rolename = rolename;
*roletpp = roletp;
return;
}
void add_symlink(struct proc_acl *subj, struct file_acl *obj)
{
struct symlink *sym = (struct symlink *)gr_alloc(sizeof (struct symlink));
sym->role = current_role;
sym->subj = subj;
sym->obj = obj;
sym->policy_file = current_acl_file;
sym->lineno = lineno;
sym->next = symlinks;
symlinks = sym;
return;
}
static struct deleted_file *
is_deleted_file_dupe(const char *filename)
{
struct deleted_file *tmp;
for (tmp = deleted_files; tmp; tmp = tmp->next) {
if (!strcmp(filename, tmp->filename))
return tmp;
}
return NULL;
}
static struct deleted_file *
add_deleted_file(const char *filename)
{
struct deleted_file *dfile;
struct deleted_file *retfile;
static u_int64_t ino = 0x10000000;
ino++;
retfile = is_deleted_file_dupe(filename);
if (retfile)
return retfile;
dfile = (struct deleted_file *)gr_alloc(sizeof (struct deleted_file));
dfile->filename = filename;
dfile->ino = ++ino;
dfile->next = deleted_files;
deleted_files = dfile;
return deleted_files;
}
static struct file_acl *
is_proc_object_dupe(struct proc_acl *subject, struct file_acl *object)
{
struct file_acl *tmp = NULL;
tmp = lookup_acl_object_by_name(subject, object->filename);
if (tmp == NULL)
tmp = lookup_acl_object(subject, object);
else {
/* found a match by filename, handle 'Z' flag here */
if (object->mode & GR_OBJ_REPLACE)
tmp->mode = object->mode &~ GR_OBJ_REPLACE;
}
return tmp;
}
static struct proc_acl *
is_proc_subject_dupe(struct role_acl *role, struct proc_acl *subject)
{
struct proc_acl *tmp = NULL;
tmp = lookup_acl_subject_by_name(role, subject->filename);
if (tmp == NULL)
tmp = lookup_acl_subject(role, subject);
else {
/* found a match by filename, handle 'Z' flag here */
if (subject->mode & GR_SUBJ_REPLACE) {
// FIXME: we leak allocations here
/* save off the old ->prev and restore it */
struct proc_acl *prev = tmp->prev;
memcpy(tmp, subject, sizeof(struct proc_acl));
tmp->prev = prev;
tmp->mode = subject->mode &~ GR_SUBJ_REPLACE;
tmp->hash = create_hash_table(GR_HASH_OBJECT);
current_subject = tmp;
}
}
return tmp;
}
int
add_role_acl(struct role_acl **role, const char *rolename, u_int16_t type, int ignore)
{
struct role_acl *rtmp;
int id, ret;
if (current_role && current_role->hash == NULL) {
fprintf(stderr, "Error on line %lu of %s: "
"Attempting to add the role \"%s\" when "
"no subjects have been specified for "
"the previous role \"%s\".\nThe RBAC "
"system will not be allowed to be "
"enabled until this error is fixed.\n",
lineno, current_acl_file, rolename, current_role->rolename);
exit(EXIT_FAILURE);
}
num_roles++;
/* one for role, one for name */
num_pointers += 2;
if (!rolename) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
rtmp = (struct role_acl *) gr_alloc(sizeof (struct role_acl));
rtmp->umask = 0;
rtmp->roletype = type;
rtmp->rolename = rolename;
if (strcmp(rolename, "default") && (type & GR_ROLE_DEFAULT)) {
fprintf(stderr, "No role type specified for %s on line %lu "
"of %s.\nThe RBAC system will not be allowed to be "
"enabled until this error is fixed.\n", rolename,
lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if (is_role_dupe(*role, rtmp->rolename, rtmp->roletype)) {
fprintf(stderr, "Duplicate role %s on line %lu of %s.\n"
"The RBAC system will not be allowed to be "
"enabled until this error is fixed.\n",
rtmp->rolename, lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if (ignore)
rtmp->uidgid = special_role_uid++;
else if (strcmp(rolename, "default") || !(type & GR_ROLE_DEFAULT)) {
if (type & (GR_ROLE_USER | GR_ROLE_GROUP)) {
ret = get_id_from_role_name(rolename, type, &id);
if (ret) {
/* ignore roles for nonexistent users/groups */
rtmp->roletype |= GR_ROLE_IGNORENOEXIST;
num_roles--;
num_pointers -= 2;
id = -1;
}
rtmp->uidgid = id;
} else if (type & GR_ROLE_SPECIAL) {
rtmp->uidgid = special_role_uid++;
}
}
if (*role)
(*role)->next = rtmp;
rtmp->prev = *role;
*role = rtmp;
if (type & GR_ROLE_SPECIAL)
add_role_transition(rtmp,rolename);
if (type & GR_ROLE_AUTH) {
add_gradm_acl(rtmp);
add_gradm_pam_acl(rtmp);
}
if (!(type & GR_ROLE_SPECIAL))
add_grlearn_acl(rtmp);
if (type & GR_ROLE_LEARN)
add_rolelearn_acl();
return 1;
}
int count_slashes(const char *str)
{
int i = 0;
while (*str) {
if (*str == '/')
i++;
str++;
}
return i;
}
static int
add_globbing_file(struct proc_acl *subject, const char *filename,
u_int32_t mode, int type)
{
struct glob_file *glob = (struct glob_file *)gr_alloc(sizeof (struct glob_file));
glob->role = current_role;
glob->subj = subject;
glob->filename = filename;
glob->mode = mode;
glob->type = type;
glob->policy_file = current_acl_file;
glob->lineno = lineno;
glob->next = NULL;
if (!glob_files_head) {
glob_files_head = glob;
} else {
glob_files_tail->next = glob;
}
glob_files_tail = glob;
return 1;
}
int
add_globbed_object_acl(struct proc_acl *subject, const char *filename,
u_int32_t mode, int type, const char *policy_file, unsigned long line)
{
char *basepoint;
char *p, *p2;
struct file_acl *anchor;
struct file_acl *glob, *glob2;
int lnum, onum;
/* one for the object itself, one for the filename */
num_pointers += 2;
basepoint = get_anchor(filename);
anchor = lookup_acl_object_by_name(subject, basepoint);
if (!anchor) {
fprintf(stderr, "Error on line %lu of %s:\n"
"Object %s needs to be specified in the same subject as globbed object %s.\n"
"The RBAC system will not be allowed to be enabled until this error is corrected.\n\n",
line, policy_file, basepoint, filename);
exit(EXIT_FAILURE);
}
free(basepoint);
if (anchor->globbed) {
glob = anchor->globbed;
glob2 = (struct file_acl *)gr_alloc(sizeof(struct file_acl));
onum = count_slashes(filename);
lnum = count_slashes(glob->filename);
if (onum > lnum) {
glob2->next = glob;
anchor->globbed = glob2;
glob2->filename = filename;
glob2->mode = mode;
glob->prev = glob2;
return 1;
}
while (glob->next) {
lnum = count_slashes(glob->next->filename);
if (onum > lnum) {
glob2->next = glob->next;
glob->next = glob2;
glob2->filename = filename;
glob2->mode = mode;
glob2->prev = glob;
glob->next->prev = glob2;
return 1;
}
glob = glob->next;
}
glob2->filename = filename;
glob2->mode = mode;
glob2->prev = glob;
glob->next = glob2;
} else {
glob2 = (struct file_acl *)gr_alloc(sizeof(struct file_acl));
glob2->filename = filename;
glob2->mode = mode;
anchor->globbed = glob2;
}
return 1;
}
static void
display_all_dupes(struct proc_acl *subject, struct file_acl *filp2)
{
struct file_acl *tmp;
struct stat64 fstat;
struct file_acl ftmp;
for_each_file_object(tmp, subject) {
if (get_canonical_inodev(tmp->filename, &ftmp.inode, &ftmp.dev, NULL)) {
if (ftmp.inode == filp2->inode && ftmp.dev == filp2->dev)
fprintf(stderr, "%s (due to symlinking/hardlinking)\n", tmp->filename);
} else if (!strcmp(tmp->filename, filp2->filename)) {
fprintf(stderr, "%s\n", tmp->filename);
}
}
return;
}
static char *
parse_homedir(const char *filename)
{
struct passwd *pwd;
unsigned int newlen;
char *newfilename;
if (!(current_role->roletype & GR_ROLE_USER) ||
(current_role->roletype & GR_ROLE_DOMAIN)) {
fprintf(stderr, "Error on line %lu of %s. $HOME "
"is supported only on user roles.\n",
lineno, current_acl_file);
exit(EXIT_FAILURE);
}
pwd = getpwuid(current_role->uidgid);
if (pwd == NULL) {
fprintf(stderr, "Error: Unable to use $HOME on line %lu of %s"
", as it can only be used in roles for users that exist"
" at the time RBAC policy is enabled.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
newlen = strlen(pwd->pw_dir) + strlen(filename) - 5 + 1;
newfilename = (char *)gr_alloc(newlen);
if (!newfilename) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
strcpy(newfilename, pwd->pw_dir);
strcat(newfilename, (filename + 5));
return newfilename;
}
int
add_proc_object_acl(struct proc_acl *subject, const char *filename,
u_int32_t mode, int type)
{
struct file_acl *p;
struct file_acl *p2;
struct deleted_file *dfile;
const char *str;
u_int64_t inode;
u_int32_t dev;
int is_symlink;
if (!subject) {
fprintf(stderr, "Error on line %lu of %s. Attempt to "
"add an object without a subject declaration.\n"
"The RBAC system will not load until this "
"error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if (!filename) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
if (!strncmp(filename, "$HOME", 5))
filename = parse_homedir(filename);
else if (!strncmp(filename, "/dev/pts/", 9)) {
fprintf(stderr, "Error on line %lu of %s. Grsecurity does "
"not support fine-grained policy on devpts mounts.\n"
"Please change your more fine-grained object to a /dev/pts "
"object. This will in addition produce a better policy that "
"will not break as unnecessarily.\n"
"The RBAC system will not load until this "
"error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
str = filename;
if (!strncmp(filename, "/SYSV", 5))
return add_globbing_file(subject, filename, mode, type);
while (*str) {
if (*str == '?' || *str == '*')
return add_globbing_file(subject, filename, mode, type);
if (*str == '[') {
const char *str2 = str;
while (*str2) {
if (*str2 == ']')
return add_globbing_file(subject, filename, mode, type);
str2++;
}
}
str++;
}
if (!get_canonical_inodev(filename, &inode, &dev, &is_symlink)) {
dfile = add_deleted_file(filename);
inode = dfile->ino;
dev = 0;
mode |= GR_DELETED;
}
num_objects++;
/* one for the object, one for the filename, one for the name entry struct, and one for the inodev_entry struct in the kernel*/
num_pointers += 4;
p = (struct file_acl *) gr_alloc(sizeof (struct file_acl));
p->filename = filename;
p->mode = mode;
p->inode = inode;
p->dev = dev;
if (type & GR_FLEARN) {
struct file_acl *tmp;
tmp = lookup_acl_object_by_name(subject, p->filename);
if (tmp) {
tmp->mode |= mode;
return 1;
}
tmp = lookup_acl_object(subject, p);
if (tmp) {
tmp->mode |= mode;
return 1;
}
} else if ((p2 = is_proc_object_dupe(subject, p))) {
if (p2->mode == mode)
return 1;
fprintf(stderr, "Duplicate object found for \"%s\""
" in role %s, subject %s, on line %lu of %s.\n"
"\"%s\" references the same object as the following object(s):\n",
p->filename, current_role->rolename,
subject->filename, lineno,
current_acl_file ? current_acl_file : "<builtin_fulllearn_policy>", p->filename);
display_all_dupes(subject, p);
fprintf(stderr, "specified on an earlier line.\n");
fprintf(stderr, "The RBAC system will not load until this error is fixed.\n");
exit(EXIT_FAILURE);
}
insert_acl_object(subject, p);
if (is_symlink)
add_symlink(subject, p);
return 1;
}
int
add_proc_subject_acl(struct role_acl *role, const char *filename, u_int32_t mode, int flag)
{
struct proc_acl *p;
struct proc_acl *p2;
struct deleted_file *dfile;
struct stat fstat;
num_subjects++;
/* one for the subject, one for the filename */
num_pointers += 2;
if (!role) {
fprintf(stderr, "Error on line %lu of %s. Attempt to "
"add a subject without a role declaration.\n"
"The RBAC system will not load until this "
"error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if (!filename) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
if (mode & GR_LEARN && mode & GR_INHERITLEARN) {
fprintf(stderr, "Error on line %lu of %s. Subject mode "
"may not include both learn and inherit-learn.\n"
"The RBAC system will not load until this "
"error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if (!strncmp(filename, "$HOME", 5))
filename = parse_homedir(filename);
p = (struct proc_acl *) gr_alloc(sizeof (struct proc_acl));
// FIXME: for subjects we currently follow symlinks
if (!get_canonical_inodev(filename, &p->inode, &p->dev, NULL)) {
dfile = add_deleted_file(filename);
p->inode = dfile->ino;
p->dev = 0;
mode |= GR_DELETED;
}
if (!strcmp(filename, "/") && !(flag & GR_FFAKE))
role->root_label = p;
p->filename = filename;
p->mode = mode;
if (!(flag & GR_FFAKE) && (p2 = is_proc_subject_dupe(role, p))) {
if (mode & GR_SUBJ_REPLACE)
return 1;
fprintf(stderr, "Duplicate subject found for \"%s\""
" in role %s, on line %lu of %s.\n"
"\"%s\" references the same object as \"%s\""
" specified on an earlier line.\n"
"The RBAC system will not load until this"
" error is fixed.\n", p->filename,
role->rolename, lineno,
current_acl_file, p->filename, p2->filename);
exit(EXIT_FAILURE);
}
/* don't insert nested subjects into main hash */
if (!(flag & GR_FFAKE))
insert_acl_subject(role, p);
else
insert_nested_acl_subject(p);
current_subject = p;
return 1;
}
u_int16_t
role_mode_conv(const char *mode)
{
int len = strlen(mode) - 1;
u_int16_t retmode = GR_ROLE_DEFAULT;
for (; len >= 0; len--) {
switch (mode[len]) {
case 'u':
retmode &= ~GR_ROLE_DEFAULT;
retmode |= GR_ROLE_USER;
break;
case 'g':
retmode &= ~GR_ROLE_DEFAULT;
retmode |= GR_ROLE_GROUP;
break;
case 's':
retmode &= ~GR_ROLE_DEFAULT;
retmode |= GR_ROLE_SPECIAL;
break;
case 'l':
retmode |= GR_ROLE_LEARN;
break;
case 'G':
retmode |= GR_ROLE_AUTH;
break;
case 'N':
retmode |= GR_ROLE_NOPW;
break;
case 'A':
retmode |= GR_ROLE_GOD;
break;
case 'R':
retmode |= GR_ROLE_PERSIST;
break;
case 'T':
retmode |= GR_ROLE_TPE;
break;
case 'P':
retmode |= GR_ROLE_PAM;
break;
default:
fprintf(stderr, "Invalid role mode "
"\'%c\' found on line %lu "
"of %s\n", mode[len], lineno, current_acl_file);
exit(EXIT_FAILURE);
}
}
if ((retmode & (GR_ROLE_SPECIAL | GR_ROLE_PERSIST)) == GR_ROLE_PERSIST) {
fprintf(stderr, "Error on line %lu of %s. Persistent "
"roles are only valid in the context of special roles.\n"
"The RBAC system will not load until this error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if ((retmode & (GR_ROLE_NOPW | GR_ROLE_PAM)) == (GR_ROLE_NOPW | GR_ROLE_PAM)) {
fprintf(stderr, "Error on line %lu of %s. The role mode must contain only one of the noauth and pamauth modes.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if ((retmode & GR_ROLE_SPECIAL) &&
(retmode & (GR_ROLE_USER | GR_ROLE_GROUP))) {
fprintf(stderr, "Error on line %lu of %s. The role mode must be either "
"special, or user/group, not both.\n"
"The RBAC system will not load until this"
" error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if ((retmode & (GR_ROLE_USER | GR_ROLE_GROUP)) && (retmode & GR_ROLE_NOPW)) {
fprintf(stderr, "Error on line %lu of %s. The role mode \"N\" can only "
"be used with a special role.\n"
"The RBAC system will not load until this"
" error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if ((retmode & (GR_ROLE_USER | GR_ROLE_GROUP)) ==
(GR_ROLE_USER | GR_ROLE_GROUP)) {
fprintf(stderr, "Error on line %lu of %s. The role mode cannot be both "
"user or group, you must choose one.\n"
"The RBAC system will not load until this"
" error is fixed.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
return retmode;
}
u_int32_t
proc_subject_mode_conv(const char *mode)
{
int i;
u_int32_t retmode = 0;
retmode |= GR_PROCFIND;
for (i = 0; i < strlen(mode); i++) {
switch (mode[i]) {
case 'T':
retmode |= GR_NOTROJAN;
break;
case 'K':
retmode |= GR_KILLPROC;
break;
case 'C':
retmode |= GR_KILLIPPROC;
break;
case 'A':
retmode |= GR_PROTSHM;
break;
case 'O':
retmode |= GR_IGNORE;
break;
case 'Z':
retmode |= GR_SUBJ_REPLACE;
break;
case 'o':
retmode |= GR_OVERRIDE;
break;
case 't':
retmode |= GR_POVERRIDE;
break;
case 'l':
retmode |= GR_LEARN;
break;
case 'h':
retmode &= ~GR_PROCFIND;
break;
case 'p':
retmode |= GR_PROTECTED;
break;
case 'k':
retmode |= GR_KILL;
break;
case 'v':
retmode |= GR_VIEW;
break;
case 'd':
retmode |= GR_PROTPROCFD;
break;
case 'b':
retmode |= GR_PROCACCT;
break;
case 'r':
retmode |= GR_RELAXPTRACE;
break;
case 'i':
retmode |= GR_INHERITLEARN;
break;
case 'a':
retmode |= GR_KERNELAUTH;
break;
case 's':
retmode |= GR_ATSECURE;
break;
case 'x':
retmode |= GR_SHMEXEC;
break;
default:
fprintf(stderr, "Invalid subject mode "
"\'%c\' found on line %lu "
"of %s\n", mode[i], lineno, current_acl_file);
exit(EXIT_FAILURE);
}
}
return retmode;
}
u_int32_t
proc_object_mode_conv(const char *mode)
{
int i;
u_int32_t retmode = 0;
retmode |= GR_FIND;
for (i = 0; i < strlen(mode); i++) {
switch (mode[i]) {
case 'r':
retmode |= GR_READ;
break;
case 'w':
retmode |= GR_WRITE;
retmode |= GR_APPEND;
break;
case 'c':
retmode |= GR_CREATE;
break;
case 'd':
retmode |= GR_DELETE;
break;
case 'x':
retmode |= GR_EXEC;
break;
case 'a':
retmode |= GR_APPEND;
break;
case 'h':
retmode &= ~GR_FIND;
break;
case 'i':
retmode |= GR_INHERIT;
break;
case 't':
retmode |= GR_PTRACERD;
break;
case 'l':
retmode |= GR_LINK;
break;
case 'Z':
retmode |= GR_OBJ_REPLACE;
break;
case 'F':
retmode |= GR_AUDIT_FIND;
break;
case 'R':