forked from jcnelson/vdev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacl.cpp
More file actions
945 lines (674 loc) · 22.4 KB
/
acl.cpp
File metadata and controls
945 lines (674 loc) · 22.4 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
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
/*
vdev: a virtual device manager for *nix
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#include "acl.h"
#define INI_MAX_LINE 4096
#define INI_STOP_ON_FIRST_ERROR 1
#include "ini.h"
#include "vdev.h"
#include "match.h"
// parse the ACL mode
static int vdev_acl_parse_mode( mode_t* mode, char const* mode_str ) {
char* tmp = NULL;
*mode = ((mode_t)strtol( mode_str, &tmp, 8 )) & 0777;
if( *tmp != '\0' ) {
return -EINVAL;
}
else {
return 0;
}
}
// convert a printable sha256 to a binary sha256 of length SHA256_DIGEST_LENGTH
// return 0 on success and fill in *sha256_bin to point to an allocated buffer
// return negative on error
static int vdev_sha256_to_bin( char const* sha256_printable, unsigned char** sha256_bin ) {
unsigned char* ret = NULL;
// sanity check
if( strlen(sha256_printable) != 2 * SHA256_DIGEST_LENGTH ) {
return -EINVAL;
}
// sanity check
for( int i = 0; i < 2 * SHA256_DIGEST_LENGTH; i++ ) {
int c = sha256_printable[i];
// must be a hex number
if( ! ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) ) {
return -EINVAL;
}
}
ret = VDEV_CALLOC( unsigned char, SHA256_DIGEST_LENGTH );
if( ret == NULL ) {
return -ENOMEM;
}
for( int i = 0; i < SHA256_DIGEST_LENGTH; i++ ) {
char buf[2];
memcpy( buf, sha256_printable + (2 * i), 2 );
ret[i] = (unsigned char) strtol( buf, NULL, 16 );
}
*sha256_bin = ret;
return 0;
}
// callback from inih to parse an acl
// return 1 on success
// return 0 on failure
static int vdev_acl_ini_parser( void* userdata, char const* section, char const* name, char const* value ) {
struct vdev_acl* acl = (struct vdev_acl*)userdata;
int rc = 0;
// verify this is an acl section
if( strcmp(section, VDEV_ACL_NAME) != 0 ) {
fprintf(stderr, "Invalid section '%s'\n", section);
return 0;
}
if( strcmp(name, VDEV_ACL_NAME_UID ) == 0 ) {
// parse user or UID
uid_t uid = 0;
int rc = vdev_parse_uid( value, &uid );
if( rc != 0 ) {
vdev_error("vdev_parse_uid(%s) rc = %d\n", value, rc );
fprintf(stderr, "Invalid user/UID '%s'\n", value );
return 0;
}
acl->has_uid = true;
acl->uid = uid;
return 1;
}
if( strcmp(name, VDEV_ACL_NAME_SETUID ) == 0 ) {
// parse user or UID
uid_t uid = 0;
int rc = vdev_parse_uid( value, &uid );
if( rc != 0 ) {
vdev_error("vdev_parse_uid(%s) rc = %d\n", value, rc );
fprintf(stderr, "Invalid user/UID '%s'\n", value );
return 0;
}
acl->has_setuid = true;
acl->setuid = uid;
return 1;
}
if( strcmp(name, VDEV_ACL_NAME_GID ) == 0 ) {
// parse group or GID
gid_t gid = 0;
int rc = vdev_parse_gid( value, &gid );
if( rc != 0 ) {
vdev_error("vdev_parse_gid(%s) rc = %d\n", value, rc );
fprintf(stderr, "Invalid group/GID '%s'\n", value );
return 0;
}
acl->has_gid = true;
acl->gid = gid;
return 1;
}
if( strcmp(name, VDEV_ACL_NAME_SETGID ) == 0 ) {
// parse group or GID
gid_t gid = 0;
int rc = vdev_parse_gid( value, &gid );
if( rc != 0 ) {
vdev_error("vdev_parse_gid(%s) rc = %d\n", value, rc );
fprintf(stderr, "Invalid group/GID '%s'\n", value );
return 0;
}
acl->has_setgid = true;
acl->setgid = gid;
return 1;
}
if( strcmp(name, VDEV_ACL_NAME_SETMODE ) == 0 ) {
// parse mode
mode_t mode = 0;
int rc = vdev_acl_parse_mode( &mode, value );
if( rc != 0 ) {
vdev_error("vdev_acl_parse_mode(%s) rc = %d\n", value, rc );
fprintf(stderr, "Invalid mode '%s'\n", value );
return 0;
}
acl->has_setmode = true;
acl->setmode = mode;
return 1;
}
if( strcmp(name, VDEV_ACL_DEVICE_REGEX ) == 0 ) {
// parse and preserve this value
int rc = vdev_match_regex_append( &acl->paths, &acl->regexes, &acl->num_paths, value );
if( rc != 0 ) {
vdev_error("vdev_match_regex_append(%s) rc = %d\n", value, rc );
fprintf(stderr, "Invalid regex '%s'\n", value );
return 0;
}
return 1;
}
if( strcmp(name, VDEV_ACL_NAME_PROC_PATH ) == 0 ) {
// preserve this value
if( acl->proc_path != NULL ) {
fprintf(stderr, "Duplicate process path '%s'\n", value );
return 0;
}
acl->has_proc = true;
acl->proc_path = vdev_strdup_or_null(value);
return 1;
}
if( strcmp(name, VDEV_ACL_NAME_PROC_SHA256 ) == 0 ) {
// preserve this value
if( acl->proc_sha256 != NULL ) {
fprintf(stderr, "Duplicate process SHA256 '%s'\n", value );
return 0;
}
rc = vdev_sha256_to_bin( value, &acl->proc_sha256 );
if( rc != 0 ) {
fprintf(stderr, "Failed to parse '%s' as a SHA256 hash\n", value );
return 0;
}
acl->has_proc = true;
return 1;
}
if( strcmp(name, VDEV_ACL_NAME_PROC_PIDLIST ) == 0 ) {
// preserve this value
if( acl->proc_pidlist_cmd != NULL ) {
fprintf(stderr, "Duplicate process PID list command '%s'\n", value );
return 0;
}
acl->has_proc = true;
acl->proc_pidlist_cmd = vdev_strdup_or_null(value);
return 1;
}
if( strcmp(name, VDEV_ACL_NAME_PROC_INODE ) == 0 ) {
// preserve this value
ino_t inode = 0;
bool success = false;
inode = vdev_parse_uint64( value, &success );
if( !success ) {
fprintf(stderr, "Failed to parse inode '%s'\n", value );
return 0;
}
acl->has_proc = true;
acl->proc_inode = inode;
return 1;
}
fprintf(stderr, "Unrecognized field '%s'\n", name);
return 0;
}
// acl sanity check
int vdev_acl_sanity_check( struct vdev_acl* acl ) {
int rc = 0;
if( acl->has_gid ) {
rc = vdev_validate_gid( acl->gid );
if( rc != 0 ) {
fprintf(stderr, "Invalid GID %d\n", acl->gid );
return rc;
}
}
if( acl->has_setgid ) {
rc = vdev_validate_gid( acl->gid );
if( rc != 0 ) {
fprintf(stderr, "Invalid set-GID %d\n", acl->setgid );
return rc;
}
}
if( acl->has_uid ) {
rc = vdev_validate_uid( acl->uid );
if( rc != 0 ) {
fprintf(stderr, "Invalid UID %d\n", acl->uid );
return rc;
}
}
if( acl->has_setuid ) {
rc = vdev_validate_uid( acl->uid );
if( rc != 0 ) {
fprintf(stderr, "Invalid set-UID %d\n", acl->setuid );
return rc;
}
}
return rc;
}
// initialize an acl
int vdev_acl_init( struct vdev_acl* acl ) {
memset( acl, 0, sizeof(struct vdev_acl) );
return 0;
}
// free an acl
int vdev_acl_free( struct vdev_acl* acl ) {
if( acl->num_paths > 0 ) {
vdev_match_regexes_free( acl->paths, acl->regexes, acl->num_paths );
}
if( acl->proc_path != NULL ) {
free( acl->proc_path );
acl->proc_path = NULL;
}
if( acl->proc_sha256 != NULL ) {
free( acl->proc_sha256 );
acl->proc_sha256 = NULL;
}
return 0;
}
// load an ACL from a file handle
int vdev_acl_load_file( FILE* file, struct vdev_acl* acl ) {
int rc = 0;
vdev_acl_init( acl );
rc = ini_parse_file( file, vdev_acl_ini_parser, acl );
if( rc != 0 ) {
vdev_error("ini_parse_file(ACL) rc = %d\n", rc );
return rc;
}
// sanity check
rc = vdev_acl_sanity_check( acl );
if( rc != 0 ) {
vdev_error("vdev_acl_sanity_check rc = %d\n", rc );
vdev_acl_free( acl );
memset( acl, 0, sizeof(struct vdev_acl) );
return rc;
}
return rc;
}
// load an ACL from a file, given the path
int vdev_acl_load( char const* path, struct vdev_acl* acl ) {
int rc = 0;
FILE* f = NULL;
f = fopen( path, "r" );
if( f == NULL ) {
rc = -errno;
vdev_error("fopen(%s) errno = %d\n", rc );
return rc;
}
rc = vdev_acl_load_file( f, acl );
fclose( f );
if( rc != 0 ) {
vdev_error("vdev_acl_load_file(%s) rc = %d\n", path, rc );
}
return rc;
}
// free a vector of acls
static void vdev_acl_free_vector( vector<struct vdev_acl>* acls ) {
for( unsigned int i = 0; i < acls->size(); i++ ) {
vdev_acl_free( &acls->at(i) );
}
acls->clear();
}
// free a C-style list of acls (including the list itself)
int vdev_acl_free_all( struct vdev_acl* acl_list, size_t num_acls ) {
int rc = 0;
for( unsigned int i = 0; i < num_acls; i++ ) {
rc = vdev_acl_free( &acl_list[i] );
if( rc != 0 ) {
return rc;
}
}
free( acl_list );
return rc;
}
// loader for an acl
int vdev_acl_loader( char const* fp, void* cls ) {
struct vdev_acl acl;
int rc = 0;
struct stat sb;
// skip if not a regular file
rc = stat( fp, &sb );
if( rc != 0 ) {
rc = -errno;
vdev_error("stat(%s) rc = %d\n", fp, rc );
return rc;
}
if( !S_ISREG( sb.st_mode ) ) {
return 0;
}
vdev_debug("Load ACL %s\n", fp );
vector<struct vdev_acl>* acls = (vector<struct vdev_acl>*)cls;
memset( &acl, 0, sizeof(struct vdev_acl) );
rc = vdev_acl_load( fp, &acl );
if( rc != 0 ) {
vdev_error("vdev_acl_load(%s) rc = %d\n", fp, rc );
return rc;
}
// save this acl
try {
acls->push_back( acl );
}
catch( bad_alloc& ba ) {
return -ENOMEM;
}
return 0;
}
// load all ACLs from a directory, in lexographic order
// return 0 on success, negative on error
int vdev_acl_load_all( char const* dir_path, struct vdev_acl** ret_acls, size_t* ret_num_acls ) {
int rc = 0;
vector<struct vdev_acl> acls;
rc = vdev_load_all( dir_path, vdev_acl_loader, &acls );
if( rc != 0 ) {
vdev_acl_free_vector( &acls );
return rc;
}
else {
if( acls.size() == 0 ) {
// nothing
*ret_acls = NULL;
*ret_num_acls = 0;
}
else {
// extract values
struct vdev_acl* acl_list = VDEV_CALLOC( struct vdev_acl, acls.size() );
if( acl_list == NULL ) {
vdev_acl_free_vector( &acls );
return -ENOMEM;
}
// NOTE: vectors are contiguous in memory
memcpy( acl_list, &acls[0], sizeof(struct vdev_acl) * acls.size() );
*ret_acls = acl_list;
*ret_num_acls = acls.size();
}
}
return 0;
}
// modify a stat buffer to apply a user access control list, if the acl says so
int vdev_acl_do_set_user( struct vdev_acl* acl, uid_t caller_uid, struct stat* sb ) {
if( acl->has_setuid && acl->has_uid ) {
// change the UID of this path
if( acl->uid == caller_uid ) {
sb->st_uid = acl->setuid;
}
}
return 0;
}
// modify a stat buffer to apply a group access control list
int vdev_acl_do_set_group( struct vdev_acl* acl, gid_t caller_gid, struct stat* sb ) {
if( acl->has_setgid && acl->has_gid ) {
// change the GID of this path
if( acl->gid == caller_gid ) {
sb->st_gid = acl->setgid;
}
}
return 0;
}
// modify a stat buffer to apply the mode
int vdev_acl_do_set_mode( struct vdev_acl* acl, struct stat* sb ) {
if( acl->has_setmode ) {
// clear permission bits
sb->st_mode &= ~(0777);
// set permission bits
sb->st_mode |= acl->setmode;
}
return 0;
}
// parse the list of PIDs
// return 0 on success, and allocate pids and set the number of pids in num_pids
// return negative on error
int vdev_parse_pids( char const* pid_list_str, size_t pid_list_len, pid_t** pids, int* num_pids ) {
char* tok_tmp = NULL;
char* strtoll_tmp = NULL;
char* pid_list_str_dup = NULL;
char* pid_list_str_in = NULL;
char* tok = NULL;
vector<pid_t> parsed_pids;
int rc = 0;
// make a copy of the pid list, so we can destroy it
pid_list_str_dup = VDEV_CALLOC( char, pid_list_len );
if( pid_list_str_dup == NULL ) {
return -ENOMEM;
}
memcpy( pid_list_str_dup, pid_list_str, pid_list_len );
pid_list_str_in = pid_list_str_dup;
while( true ) {
tok = strtok_r( pid_list_str_in, " \n\r\t", &tok_tmp );
pid_list_str_in = NULL;
if( tok == NULL ) {
// out of tokens
break;
}
// is this a pid?
pid_t next_pid = (pid_t)strtoll( tok, &strtoll_tmp, 10 );
if( *strtoll_tmp != '\0' ) {
// not valid
vdev_error("Invalid PID '%s'\n", tok );
rc = -EINVAL;
break;
}
parsed_pids.push_back( next_pid );
}
free( pid_list_str_dup );
if( rc == 0 ) {
*pids = VDEV_CALLOC( pid_t, parsed_pids.size() );
if( *pids == NULL ) {
return -ENOMEM;
}
// NOTE: vectors are guaranteed to be laid out contiguously in memory...
memcpy( *pids, &parsed_pids[0], sizeof(pid_t) * parsed_pids.size() );
}
return 0;
}
// run a 'pidlist' command, with the appropriate environment variables set.
// populate *pids with the list of PIDs returned, on success
// return 0 on success
// return negative on error
int vdev_acl_do_pidlist( struct vdev_acl* acl, struct pstat* ps, uid_t caller_uid, gid_t caller_gid, pid_t** ret_pids, int* ret_num_pids ) {
// get the list of PIDs from the shell command
char* pid_list_str = NULL;
size_t pid_list_maxlen = VDEV_ACL_PROC_BUFLEN;
pid_t* pids = NULL;
int num_pids = 0;
int exit_status = 0;
char* cmd_buf = NULL;
char env_buf[3][100];
char* pidlist_env[4];
int rc = 0;
// build the command with the apporpriate environment variables:
// * VDEV_GID: the gid of the calling process
// * VDEV_UID: the uid of the calling process
// * VDEV_PID: the pid of the calling process
sprintf(env_buf[0], "VDEV_UID=%u", caller_uid );
sprintf(env_buf[1], "VDEV_GID=%u", caller_gid );
sprintf(env_buf[2], "VDEV_PID=%u", ps->pid );
pidlist_env[0] = env_buf[0];
pidlist_env[1] = env_buf[1];
pidlist_env[2] = env_buf[2];
pidlist_env[3] = NULL;
rc = vdev_subprocess( cmd_buf, pidlist_env, &pid_list_str, pid_list_maxlen, &exit_status );
if( rc != 0 ) {
vdev_error("vdev_subprocess('%s') rc = %d\n", cmd_buf, rc );
free( cmd_buf );
return rc;
}
if( exit_status != 0 ) {
vdev_error("vdev_subprocess('%s') exit status %d\n", cmd_buf, exit_status );
free( cmd_buf );
return -EPERM;
}
free( cmd_buf );
// lex the pids
rc = vdev_parse_pids( pid_list_str, pid_list_maxlen, &pids, &num_pids );
free( pid_list_str );
if( rc != 0 ) {
vdev_error("vdev_parse_pids rc = %d\n", rc );
return rc;
}
*ret_pids = pids;
*ret_num_pids = num_pids;
return 0;
}
// check that the caller PID is matched by the given ACL.
// every process match criterion must be satisfied.
// return 1 if all ACL criteria match
// return 0 if at least one ACL criterion does not match
// return negative on error
int vdev_acl_match_process( struct vdev_acl* acl, struct pstat* ps, uid_t caller_uid, gid_t caller_gid ) {
int rc = 0;
if( !acl->has_proc ) {
// applies to anyone
return 1;
}
if( acl->proc_path != NULL || acl->proc_sha256 != NULL || acl->has_proc_inode ) {
if( acl->proc_path != NULL ) {
if( strcmp( acl->proc_path, ps->path ) != 0 ) {
// doesn't match
return 0;
}
}
if( acl->proc_sha256 != NULL ) {
// do we need to get the SHA256?
unsigned char blank_sha256[SHA256_DIGEST_LENGTH];
memset( blank_sha256, 0, SHA256_DIGEST_LENGTH );
if( memcmp( blank_sha256, ps->sha256, SHA256_DIGEST_LENGTH ) == 0 ) {
// we don't have the hash. Go get it
rc = pstat_sha256( ps );
if( rc != 0 ) {
vdev_error("Failed to SHA256 the process binary for %d, rc = %d\n", ps->pid, rc );
return rc;
}
}
if( memcmp( acl->proc_sha256, ps->sha256, SHA256_DIGEST_LENGTH ) != 0 ) {
// doesn't match
return 0;
}
}
if( acl->has_proc_inode ) {
if( acl->proc_inode != ps->bin_stat.st_ino ) {
// doesn't match
return 0;
}
}
}
// filter on a given PID list generator?
if( acl->proc_pidlist_cmd != NULL ) {
// get the list of PIDs from the shell command
pid_t* pids = NULL;
int num_pids = 0;
bool found = false;
rc = vdev_acl_do_pidlist( acl, ps, caller_uid, caller_gid, &pids, &num_pids );
if( rc != 0 ) {
vdev_error("vdev_acl_do_pidlist('%s') rc = %d\n", acl->proc_pidlist_cmd, rc );
return rc;
}
// is the caller in the list?
for( int i = 0; i < num_pids; i++ ) {
if( ps->pid == pids[i] ) {
found = true;
break;
}
}
free( pids );
if( !found ) {
return 0;
}
}
// all checks match
return 1;
}
// given a list of access control lists, find the index of the first one that applies to the given caller and path.
// return >= 0 with the index
// return num_acls if not found
// return negative on error
int vdev_acl_find_next( char const* path, struct pstat* caller_proc, uid_t caller_uid, gid_t caller_gid, struct vdev_acl* acls, size_t num_acls ) {
int rc = 0;
bool found = false;
int idx = 0;
for( unsigned int i = 0; i < num_acls; i++ ) {
rc = 0;
// match UID?
if( acls[i].has_uid && acls[i].uid != caller_uid ) {
// nope
continue;
}
// match GID?
if( acls[i].has_gid && acls[i].gid != caller_gid ) {
// nope
continue;
}
// match path?
if( acls[i].num_paths > 0 ) {
rc = vdev_match_first_regex( path, acls[i].regexes, acls[i].num_paths );
if( rc >= (signed)acls[i].num_paths ) {
// no match
continue;
}
}
if( rc < 0 ) {
vdev_error("vdev_match_first_regex(%s) rc = %d\n", path, rc );
break;
}
// match process? Do this last, since it can be expensive
rc = vdev_acl_match_process( &acls[i], caller_proc, caller_uid, caller_gid );
if( rc == 0 ) {
// no match
continue;
}
if( rc < 0 ) {
// error...
vdev_error("vdev_acl_match_process(%d) rc = %d\n", caller_proc->pid, rc );
break;
}
// success!
found = true;
idx = i;
rc = 0;
break;
}
if( found ) {
return idx;
}
else {
if( rc >= 0 ) {
return num_acls;
}
else {
return rc;
}
}
}
// apply the acl to the stat buf, filtering on caller uid, gid, and process information
int vdev_acl_apply( struct vdev_acl* acl, uid_t caller_uid, gid_t caller_gid, struct stat* sb ) {
// set user, group, mode (if given)
vdev_acl_do_set_user( acl, caller_uid, sb );
vdev_acl_do_set_group( acl, caller_gid, sb );
vdev_acl_do_set_mode( acl, sb );
return 0;
}
// go through the list of acls and apply any modifications to the given stat buffer
// return 1 if at least one ACL matches
// return 0 if there are no matches (i.e. this device node should be hidden)
// return negative on error
int vdev_acl_apply_all( struct vdev_acl* acls, size_t num_acls, char const* path, struct pstat* caller_proc, uid_t caller_uid, gid_t caller_gid, struct stat* sb ) {
int rc = 0;
int acl_offset = 0;
int i = 0;
bool found = false;
while( acl_offset < (signed)num_acls ) {
// find the next acl
rc = vdev_acl_find_next( path, caller_proc, caller_uid, caller_gid, acls + acl_offset, num_acls - acl_offset );
if( rc == (signed)(num_acls - acl_offset) ) {
// not found
rc = 0;
break;
}
else if( rc < 0 ) {
vdev_error("vdev_acl_find_next(%s, offset = %d) rc = %d\n", path, acl_offset, rc );
break;
}
else {
// matched! advance offset to next acl
i = acl_offset + rc;
acl_offset += rc + 1;
found = true;
// apply the ACL
rc = vdev_acl_apply( &acls[i], caller_uid, caller_gid, sb );
if( rc != 0 ) {
vdev_error("vdev_acl_apply(%s, offset = %d) rc = %d\n", path, acl_offset, rc );
break;
}
}
}
if( rc == 0 ) {
// no error reported
if( found ) {
rc = 1;
}
}
return rc;
}