forked from 389ds/389-ds-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aclparse.c
2389 lines (2125 loc) · 76.9 KB
/
aclparse.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
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2021 Red Hat, Inc.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "acl.h"
/****************************************************************************/
/* prototypes */
/****************************************************************************/
static int __aclp__parse_aci(char *str, aci_t *aci_item, char **errbuf);
static int __aclp__sanity_check_acltxt(aci_t *aci_item, char *str);
static char *__aclp__normalize_acltxt(aci_t *aci_item, char *str);
static char *__aclp__getNextLASRule(aci_t *aci_item, char *str, char **endOfCurrRule);
static int __aclp__get_aci_right(char *str);
static int __aclp__init_targetattr(aci_t *aci, char *attr_val, char **errbuf);
static int __acl__init_targetattrfilters(aci_t *aci_item, char *str);
static int process_filter_list(Targetattrfilter ***attrfilterarray,
char *str);
static int __acl_init_targetattrfilter(Targetattrfilter *attrfilter, char *str);
static void __aclp_chk_paramRules(aci_t *aci_item, char *start, char *end);
static void __acl_strip_trailing_space(char *str);
static void __acl_strip_leading_space(char **str);
static char *__acl_trim_filterstr(char *str);
static int acl_verify_exactly_one_attribute(char *attr_name, Slapi_Filter *f);
static int type_compare(Slapi_Filter *f, void *arg);
static int acl_check_for_target_macro(aci_t *aci_item, char *value);
static int get_acl_rights_as_int(char *strValue);
/* Enforce strict aci syntax */
#define STRICT_SYNTAX_CHECK 0
/***************************************************************************
*
* acl_parse
*
* Parses the input string and copies the information into the
* correct place in the aci.
*
*
* Input:
* Slapi_PBlock *pb - Parameter block
* char *str - Input string which has the ACL
* This is a duped copy, so here we have
* the right to stich '\0' characters into str for
* processing purposes. If you want to keep
* a piece of str, you'll need to dup it
* as it gets freed outside the scope of acl_parse.
* aci_t *item - the aci item where the ACL info will be
* - stored.
*
* Returns:
* 0 -- Parsed okay
* < 0 -- error codes
*
* Error Handling:
* None.
*
**************************************************************************/
int
acl_parse(Slapi_PBlock *pb, char *str, aci_t *aci_item, char **errbuf)
{
int rv = 0;
char *next = NULL;
char *save = NULL;
while (*str) {
__acl_strip_leading_space(&str);
if (*str == '\0')
break;
if (*str == '(') {
if ((next = slapi_find_matching_paren(str)) == NULL) {
return (ACL_SYNTAX_ERR);
}
} else if (!next) {
/* the statement does not start with a parenthesis */
return (ACL_SYNTAX_ERR);
} else {
/* then we have done all the processing */
return 0;
}
LDAP_UTF8INC(str); /* skip the "(" */
save = next;
LDAP_UTF8INC(next);
*save = '\0';
/* Now we have a "str)" */
if (0 != (rv = __aclp__parse_aci(str, aci_item, errbuf))) {
return (rv);
}
/* Move to the next */
str = next;
}
/* check if have a ACLTXT or not */
if (!(aci_item->aci_type & ACI_ACLTXT))
return ACL_SYNTAX_ERR;
if (aci_item->target) {
Slapi_Filter *f;
/* Make sure that the target is a valid target.
** Example: ACL is located in
** "ou=engineering, o=ace industry, c=us
** but if the target is "o=ace industry, c=us",
** then it's an ERROR.
*/
f = aci_item->target;
if (aci_item->aci_type & ACI_TARGET_DN) {
char *avaType;
struct berval *avaValue;
Slapi_DN targdn;
slapi_filter_get_ava(f, &avaType, &avaValue);
slapi_sdn_init_dn_byref(&targdn, avaValue->bv_val);
if (!slapi_sdn_get_dn(&targdn)) {
/* not a valid DN */
slapi_sdn_done(&targdn);
return ACL_INVALID_TARGET;
}
if (!slapi_sdn_issuffix(&targdn, aci_item->aci_sdn)) {
slapi_sdn_done(&targdn);
return ACL_INVALID_TARGET;
}
if (slapi_sdn_compare(&targdn, aci_item->aci_sdn)) {
int target_check = 0;
if (pb) {
slapi_pblock_get(pb, SLAPI_ACI_TARGET_CHECK, &target_check);
}
if (target_check != 1) {
/* Make sure that the target exists */
int rc = 0;
Slapi_PBlock *temppb = slapi_pblock_new();
slapi_search_internal_set_pb_ext(temppb, &targdn,
LDAP_SCOPE_BASE, "(objectclass=*)", NULL, 1, NULL, NULL,
(void *)plugin_get_default_component_id(), 0);
slapi_search_internal_pb(temppb);
slapi_pblock_get(temppb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
if (rc != LDAP_SUCCESS) {
slapi_log_err(SLAPI_LOG_WARNING, plugin_name,
"acl_parse - The ACL target %s does not exist\n", slapi_sdn_get_dn(&targdn));
}
slapi_free_search_results_internal(temppb);
slapi_pblock_destroy(temppb);
if (pb) {
target_check = 1;
slapi_pblock_set(pb, SLAPI_ACI_TARGET_CHECK, &target_check);
}
}
}
slapi_sdn_done(&targdn);
}
}
/*
** We need to keep the taregetFilterStr for anyone ACL only.
** same for targetValueFilterStr.
** We need to keep it for macros too as it needs to be expnaded at eval time.
**
*/
if ((aci_item->aci_elevel != ACI_ELEVEL_USERDN_ANYONE) &&
!(aci_item->aci_type & ACI_TARGET_MACRO_DN)) {
slapi_ch_free((void **)&aci_item->targetFilterStr);
}
/*
* If we parsed the aci and there was a ($dn) on the user side
* but none in hte taget then that's an error as the user side
* value is derived from the target side value.
*/
if (!(aci_item->aci_type & ACI_TARGET_MACRO_DN) &&
(aci_item->aci_ruleType & ACI_PARAM_DNRULE)) {
slapi_log_err(SLAPI_LOG_ERR, plugin_name,
"acl_parse - A macro in a subject ($dn) must have a macro in the target.\n");
return (ACL_INVALID_TARGET);
}
return 0;
}
/***************************************************************************
*
* __aclp__parse_aci
*
* Parses Each individual subset of information/
*
* Input:
* char *str - Input string which has the ACL like "str)"
* aci_t *item - the aci item where the ACL info will be
* - stored.
*
* Returns:
* 0 -- Parsed okay
* < 0 -- error codes
*
* Error Handling:
* None.
*
**************************************************************************/
static int
__aclp__parse_aci(char *str, aci_t *aci_item, char **errbuf)
{
int len;
int rv;
int type;
char *tmpstr;
char *s = NULL;
char *value = NULL;
char *single_space = " ";
Slapi_Filter *f = NULL;
int targetattrlen = strlen(aci_targetattr);
int targetdnlen = strlen(aci_targetdn);
int tfilterlen = strlen(aci_targetfilter);
int targetattrfilterslen = strlen(aci_targetattrfilters);
int target_to_len = strlen(aci_target_to);
int target_from_len = strlen(aci_target_from);
PRBool is_target_to = PR_FALSE;
__acl_strip_leading_space(&str);
if (*str == '\0') {
return (ACL_SYNTAX_ERR);
}
/* The first letter should tell us something */
switch (*str) {
case 'v':
type = ACI_ACLTXT;
rv = __aclp__sanity_check_acltxt(aci_item, str);
if (rv) {
return rv;
}
break;
case 't':
if (strncmp(str, aci_targetattrfilters, targetattrfilterslen) == 0) {
type = ACI_TARGET_ATTR;
/*
* The targetattrfilters bit looks like this:
* (targetattrfilters="add= attr1:F1 && attr2:F2 ... && attrn:Fn,
* del= attr1:F1 && attr2:F2... && attrn:Fn")
*/
if (0 != (rv = __acl__init_targetattrfilters(aci_item, str))) {
return rv;
}
} else if (strncmp(str, aci_targetattr, targetattrlen) == 0) {
type = ACI_TARGET_ATTR;
if ((s = strstr(str, "!=")) != NULL) {
type |= ACI_TARGET_ATTR_NOT;
memcpy(s, single_space, 1);
}
/* Get individual components of the targetattr.
* (targetattr = "cn || u* || phone ||tel:add:(tel=1234)
* || sn:del:(gn=5678)")
* If it contains a value filter, the type will also be
* ACI_TARGET_VALUE_ATTR.
*/
if (0 != (rv = __aclp__init_targetattr(aci_item, str, errbuf))) {
return rv;
}
} else if (strncmp(str, aci_targetfilter, tfilterlen) == 0) {
if (aci_item->targetFilter)
return ACL_SYNTAX_ERR;
type = ACI_TARGET_FILTER;
/* we need to remove the targetfilter stuff*/
if (strstr(str, "!=") != NULL) {
type |= ACI_TARGET_FILTER_NOT;
}
/*
* If it's got a macro in the targetfilter then it must
* have a target and it must have a macro.
*/
if ((PL_strcasestr(str, ACL_RULE_MACRO_DN_KEY) != NULL) ||
(PL_strcasestr(str, ACL_RULE_MACRO_DN_LEVELS_KEY) != NULL)) {
/* Must have a targetmacro */
if (!(aci_item->aci_type & ACI_TARGET_MACRO_DN)) {
slapi_log_err(SLAPI_LOG_ERR, plugin_name,
"__aclp__parse_aci - A macro in a targetfilter ($dn) must have a macro in the target.\n");
return (ACL_SYNTAX_ERR);
}
type |= ACI_TARGET_FILTER_MACRO_DN;
}
tmpstr = strchr(str, '=');
if (NULL == tmpstr) {
return ACL_SYNTAX_ERR;
}
tmpstr++;
/* Consecutive equals are not allowed */
if (*tmpstr == '=') {
slapi_log_err(SLAPI_LOG_ERR, plugin_name,
"__aclp__parse_aci - target filter has an invalid syntax, "
"do not use more than one '=' between the targetfilter keyword and its value: (%s)\n",
str);
return ACL_SYNTAX_ERR;
}
__acl_strip_leading_space(&tmpstr);
/* The first character is expected to be a double quote */
if (STRICT_SYNTAX_CHECK && *tmpstr != '"') {
slapi_log_err(SLAPI_LOG_ERR, plugin_name,
"__aclp__parse_aci - target filter has an invalid value (%s)\n", str);
return ACL_SYNTAX_ERR;
}
/*
* Trim off enclosing quotes and enclosing
* superfluous brackets.
* The result has been duped so it can be kept.
*/
tmpstr = __acl_trim_filterstr(tmpstr);
f = slapi_str2filter(tmpstr);
/* save the filter string */
aci_item->targetFilterStr = tmpstr;
} else if ((strncmp(str, aci_target_to, target_to_len) == 0) || (strncmp(str, aci_target_from, target_from_len) == 0)) {
/*
* This is important to make this test before aci_targetdn
* because aci_targetdn also match aci_target_to/aci_target_from
*/
char *tstr = NULL;
size_t LDAP_URL_prefix_len = 0;
size_t tmplen = 0;
/* Keep a copy of the target attr */
type = ACI_TARGET_MODDN;
if (strncmp(str, aci_target_to, target_to_len) == 0) {
if (aci_item->target_to) {
return (ACL_SYNTAX_ERR);
}
is_target_to = PR_TRUE;
} else {
if (aci_item->target_from) {
return (ACL_SYNTAX_ERR);
}
is_target_to = PR_FALSE;
}
if ((s = strstr(str, "!=")) != NULL) {
type |= ACI_TARGET_NOT;
memcpy(s, single_space, 1);
}
if ((s = strchr(str, '=')) != NULL) {
s++;
if (*s == '=') {
/* Consecutive equals are not allowed */
slapi_log_err(SLAPI_LOG_ERR, plugin_name,
"__aclp__parse_aci - target to/from has an invalid syntax, "
"do not use more than one '=' between the target to/from keyword and its value: (%s)\n",
str);
return ACL_SYNTAX_ERR;
}
value = s;
__acl_strip_leading_space(&value);
__acl_strip_trailing_space(value);
/* The first character is expected to be a double quote */
if (STRICT_SYNTAX_CHECK && *value != '"') {
slapi_log_err(SLAPI_LOG_ERR, plugin_name,
"__aclp__parse_aci - target to/from has an invalid value (%s)\n", str);
return ACL_SYNTAX_ERR;
}
len = strlen(value);
/* strip double quotes */
if (*value == '"' && value[len - 1] == '"') {
value[len - 1] = '\0';
value++;
}
__acl_strip_leading_space(&value);
} else {
return (ACL_SYNTAX_ERR);
}
if (0 ==
strncasecmp(value, LDAP_URL_prefix, strlen(LDAP_URL_prefix))) {
LDAP_URL_prefix_len = strlen(LDAP_URL_prefix);
} else if (0 == strncasecmp(value, LDAPS_URL_prefix,
strlen(LDAPS_URL_prefix))) {
LDAP_URL_prefix_len = strlen(LDAPS_URL_prefix);
} else {
return (ACL_SYNTAX_ERR);
}
value += LDAP_URL_prefix_len;
rv = slapi_dn_normalize_case_ext(value, 0, &tmpstr, &tmplen);
if (rv < 0) {
return ACL_SYNTAX_ERR;
} else if (rv == 0) { /* value passed in; not null terminated */
*(tmpstr + tmplen) = '\0';
}
/* Now prepare the filter */
if (strncmp(str, aci_target_to, target_to_len) == 0) {
tstr = slapi_ch_smprintf("(%s=%s)", aci_target_to, tmpstr);
} else {
tstr = slapi_ch_smprintf("(%s=%s)", aci_target_from, tmpstr);
}
f = slapi_str2filter(tstr);
slapi_ch_free_string(&tstr);
} else if (strncmp(str, aci_targetdn, targetdnlen) == 0) {
char *tstr = NULL;
size_t LDAP_URL_prefix_len = 0;
size_t tmplen = 0;
type = ACI_TARGET_DN;
/* Keep a copy of the target attr */
if (aci_item->target) {
return (ACL_SYNTAX_ERR);
}
if ((s = strstr(str, "!=")) != NULL) {
type |= ACI_TARGET_NOT;
memcpy(s, single_space, 1);
}
if ((s = strchr(str, '=')) != NULL) {
s++;
if (*s == '=') {
/* Consecutive equals are not allowed */
slapi_log_err(SLAPI_LOG_ERR, plugin_name,
"__aclp__parse_aci - target has an invalid syntax, "
"do not use more than one '=' between the target keyword and its value: (%s)\n",
str);
return ACL_SYNTAX_ERR;
}
value = s;
__acl_strip_leading_space(&value);
__acl_strip_trailing_space(value);
/* The first character is expected to be a double quote */
if (STRICT_SYNTAX_CHECK && *value != '"') {
slapi_log_err(SLAPI_LOG_ERR, plugin_name,
"__aclp__parse_aci - target has an invalid value (%s)\n", str);
return ACL_SYNTAX_ERR;
}
len = strlen(value);
/* strip double quotes */
if (*value == '"' && value[len - 1] == '"') {
value[len - 1] = '\0';
value++;
}
__acl_strip_leading_space(&value);
} else {
return (ACL_SYNTAX_ERR);
}
if (0 ==
strncasecmp(value, LDAP_URL_prefix, strlen(LDAP_URL_prefix))) {
LDAP_URL_prefix_len = strlen(LDAP_URL_prefix);
} else if (0 == strncasecmp(value, LDAPS_URL_prefix,
strlen(LDAPS_URL_prefix))) {
LDAP_URL_prefix_len = strlen(LDAPS_URL_prefix);
} else {
return (ACL_SYNTAX_ERR);
}
value += LDAP_URL_prefix_len;
rv = slapi_dn_normalize_case_ext(value, 0, &tmpstr, &tmplen);
if (rv < 0) {
return ACL_SYNTAX_ERR;
} else if (rv == 0) { /* value passed in; not null terminated */
*(tmpstr + tmplen) = '\0';
}
tstr = slapi_ch_smprintf("(target=%s)", tmpstr);
if (rv > 0) {
slapi_ch_free_string(&tmpstr);
}
if ((rv = acl_check_for_target_macro(aci_item, value)) == -1) {
slapi_ch_free((void **)&tstr);
return (ACL_SYNTAX_ERR);
} else if (rv > 0) {
/* is present, so the type is now ACL_TARGET_MACRO_DN */
type = ACI_TARGET_MACRO_DN;
} else {
/* it's a normal target with no macros inside */
f = slapi_str2filter(tstr);
}
slapi_ch_free_string(&tstr);
} else {
/* did start with a 't' but was not a recognsied keyword */
return (ACL_SYNTAX_ERR);
}
/*
* Here, it was a recognised keyword that started with 't'.
* Check that the filter associated with ACI_TARGET_DN and
* ACI_TARGET_FILTER are OK.
*/
if (f == NULL) {
/* The following types require a filter to have been created */
if (type & (ACI_TARGET_DN | ACI_TARGET_MODDN))
return ACL_TARGET_FILTER_ERR;
else if (type & ACI_TARGET_FILTER)
return ACL_TARGETFILTER_ERR;
} else {
int filterChoice;
filterChoice = slapi_filter_get_choice(f);
if ((type & (ACI_TARGET_DN | ACI_TARGET_MODDN)) &&
(filterChoice == LDAP_FILTER_PRESENT)) {
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"__aclp__parse_aci - Unsupported filter type:%d\n", filterChoice);
return (ACL_SYNTAX_ERR);
} else if ((filterChoice == LDAP_FILTER_SUBSTRINGS) &&
(type & ACI_TARGET_DN)) {
type &= ~ACI_TARGET_DN;
type |= ACI_TARGET_PATTERN;
} else if ((filterChoice == LDAP_FILTER_SUBSTRINGS) &&
(type & ACI_TARGET_MODDN)) {
if (is_target_to) {
type |= ACI_TARGET_MODDN_TO_PATTERN;
} else {
type |= ACI_TARGET_MODDN_FROM_PATTERN;
}
}
}
if ((type & ACI_TARGET_DN) ||
(type & ACI_TARGET_PATTERN)) {
if (aci_item->target) {
/* There is something already. ERROR */
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"__aclp__parse_aci - Multiple targets in the ACL syntax\n");
slapi_filter_free(f, 1);
return (ACL_SYNTAX_ERR);
} else {
aci_item->target = f;
}
} else if (type & ACI_TARGET_FILTER) {
if (aci_item->targetFilter) {
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"__aclp__parse_aci - Multiple target Filters in the ACL Syntax\n");
slapi_filter_free(f, 1);
return (ACL_SYNTAX_ERR);
} else {
aci_item->targetFilter = f;
}
} else if (type & ACI_TARGET_MODDN) {
if (is_target_to) {
if (aci_item->target_to) {
/* There is something already. ERROR */
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"__aclp__parse_aci - Multiple targets (target_to) in the ACL syntax\n");
slapi_filter_free(f, 1);
return (ACL_SYNTAX_ERR);
} else {
aci_item->target_to = f;
}
} else {
if (aci_item->target_from) {
/* There is something already. ERROR */
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"__aclp__parse_aci - Multiple targets (target_from) in the ACL syntax\n");
slapi_filter_free(f, 1);
return (ACL_SYNTAX_ERR);
} else {
aci_item->target_from = f;
}
}
}
break; /* 't' */
default:
/* Here the keyword did not start with 'v' ot 't' so error */
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"__aclp__parse_aci - Unknown keyword at \"%s\"\n Expecting"
" \"target\", \"targetattr\", \"targetfilter\", \"targattrfilters\""
" or \"version\"\n",
str);
return (ACL_SYNTAX_ERR);
} /* switch() */
/* Store the type info */
aci_item->aci_type |= type;
return 0;
}
/***************************************************************************
* acl__sanity_check_acltxt
*
* Check the input ACL text. Reports any errors. Also forgivs if certain
* things are missing.
*
* Input:
* char *str - String containing the acl text
* int *err - error status
*
* Returns:
* 0 --- good status
* <0 --- error
*
* Error Handling:
* None.
*
**************************************************************************/
static int
__aclp__sanity_check_acltxt(aci_t *aci_item, char *str)
{
NSErr_t errp;
char *s;
ACLListHandle_t *handle = NULL;
char *newstr = NULL;
char *word;
char *next = NULL;
const char *brkstr = " ;";
int checkversion = 0;
memset(&errp, 0, sizeof(NSErr_t));
newstr = str;
while ((s = strstr(newstr, "authenticate")) != NULL) {
char *next_str;
next_str = s + 12;
s--;
while (s > str && ldap_utf8isspace(s))
LDAP_UTF8DEC(s);
if (s && *s == ';') {
/* We don't support authenticate stuff */
return ACL_INVALID_AUTHORIZATION;
} else {
newstr = next_str;
}
}
newstr = slapi_ch_strdup(str);
for (word = ldap_utf8strtok_r(newstr, brkstr, &next); word;
word = ldap_utf8strtok_r(NULL, brkstr, &next)) {
if (0 == strcasecmp(word, "version")) {
checkversion = 1;
} else if (checkversion) {
checkversion = 0;
if ('3' != *word) {
slapi_ch_free((void **)&newstr);
return ACL_INCORRECT_ACI_VERSION;
}
} else if ((s = strstr(word, "($")) || (s = strstr(word, "[$"))) {
int attr_macro = -1;
/* See if this is a valid macro keyword. */
if ((0 != strncasecmp(s, ACL_RULE_MACRO_DN_KEY,
sizeof(ACL_RULE_MACRO_DN_KEY) - 1)) &&
(0 != strncasecmp(s, ACL_RULE_MACRO_DN_LEVELS_KEY,
sizeof(ACL_RULE_MACRO_DN_LEVELS_KEY) - 1)) &&
(0 != (attr_macro = strncasecmp(s, ACL_RULE_MACRO_ATTR_KEY,
sizeof(ACL_RULE_MACRO_ATTR_KEY) - 1)))) {
slapi_ch_free((void **)&newstr);
return ACL_SYNTAX_ERR;
}
/* For the $attr macro, validate that the attribute name is
* legal per RFC 4512. */
if (attr_macro == 0) {
int start = 1;
char *p = NULL;
for (p = s + sizeof(ACL_RULE_MACRO_ATTR_KEY) - 1;
p && *p && *p != ')'; p++) {
if (start) {
if (!isalpha(*p)) {
slapi_ch_free((void **)&newstr);
return ACL_SYNTAX_ERR;
}
start = 0;
} else {
if (!(isalnum(*p) || (*p == '-'))) {
slapi_ch_free((void **)&newstr);
return ACL_SYNTAX_ERR;
}
}
}
}
}
}
slapi_ch_free((void **)&newstr);
/* We need to normalize the DNs in the userdn and group dn
** so that, it's only done once.
*/
if ((newstr = __aclp__normalize_acltxt(aci_item, str)) == NULL) {
return ACL_SYNTAX_ERR;
}
slapi_log_err(SLAPI_LOG_ACL, plugin_name, "__aclp__sanity_check_acltxt - Normalized String:%s\n", newstr);
/* check for acl syntax error */
if ((handle = (ACLListHandle_t *)ACL_ParseString(&errp, newstr)) == NULL) {
acl_print_acllib_err(&errp, str);
slapi_ch_free_string(&newstr);
return ACL_SYNTAX_ERR;
} else {
/* get the rights and the aci type */
aci_item->aci_handle = handle;
nserrDispose(&errp);
slapi_ch_free_string(&newstr);
return 0;
}
}
/*
* If the src includes "ldap(s):///<dn>", normalize <dn> and copy
* the string starting from start to *dest.
* If isstrict is non-zero, if ldap(s):/// is not included in the src
* string, it returns an error (-1).
* If isstrict is zero, the string is copied as is.
*
* return value: 0 or positive: success
* negative: failure
*/
int
__aclp__copy_normalized_str(char *src, char *endsrc, char *start, char **dest, size_t *destlen, int isstrict)
{
char *p = NULL;
int rc = -1;
const char *dn = NULL;
p = PL_strnstr(src, LDAP_URL_prefix, endsrc - src);
if (p) {
p += strlen(LDAP_URL_prefix);
} else {
p = PL_strnstr(src, LDAPS_URL_prefix, endsrc - src);
if (p) {
p += strlen(LDAPS_URL_prefix);
}
}
if (isstrict && ((NULL == p) || 0 == strlen(p))) {
return rc; /* error */
}
rc = 0;
if (p && strlen(p) > 0) {
size_t len = 0;
Slapi_DN sdn;
char bak;
/* strip the string starting from ? */
char *q = PL_strnchr(p, '?', endsrc - p);
if (q) {
len = q - p;
} else {
len = endsrc - p;
}
bak = *(p + len);
*(p + len) = '\0';
/* Normalize the value of userdn and append it to ret_str */
slapi_sdn_init_dn_byref(&sdn, p);
dn = slapi_sdn_get_dn(&sdn);
/* Normalization failed so return an error (-1) */
if (!dn) {
slapi_sdn_done(&sdn);
return -1;
}
/* append up to ldap(s):/// */
aclutil_str_append_ext(dest, destlen, start, p - start);
/* append the DN part */
aclutil_str_append_ext(dest, destlen, dn, strlen(dn));
slapi_sdn_done(&sdn);
*(p + len) = bak;
if (q) {
/* append the rest from '?' */
aclutil_str_append_ext(dest, destlen, q, endsrc - q);
}
} else {
aclutil_str_append_ext(dest, destlen, start, endsrc - start);
}
return rc;
}
/******************************************************************************
*
* acl__normalize_acltxt
*
*
* XXXrbyrne this routine should be re-written when someone eventually
* gets sick enough of it. Same for getNextLAS() below.
*
* Normalize the acltxt i.e normalize all the DNs specified in the
* Userdn and Groupdn rule so that we normalize once here and not
* over and over again at the runtime in the LASes. We have to normalize
* before we generate the handle otherwise it's of no use.
* Also convert deny to deny absolute
*
* The string that comes in is something like:
* version 3.0; acl "Dept domain administration"; allow (all)
* groupdn = "ldap:///cn=Domain Administrators, o=$dn.o, o=ISP"; )
*
* Returns NULL on error.
*
******************************************************************************/
static char *
__aclp__normalize_acltxt(aci_t *aci_item, char *str)
{
char *s, *p;
char *end;
char *aclstr, *s_aclstr;
char *prevend = NULL;
char *ret_str = NULL;
size_t retstr_len = 0;
int len;
char *aclName;
char *nextACE;
char *tmp_str = NULL;
char *acestr = NULL;
char *s_acestr = NULL;
int aci_rights_val = 0; /* bug 389975 */
int rc = 0;
/* make a copy first */
s_aclstr = aclstr = slapi_ch_strdup(str);
/* The rules are like this version 3.0; acl "xyz"; rule1; rule2; */
s = strchr(aclstr, ';');
if (NULL == s) {
goto error;
}
aclstr = ++s;
/* From DS 4.0, we support both aci (or aci) "name" -- we have to change to acl
** as libaccess will not like it
*/
s = aclstr;
while (s && ldap_utf8isspace(s))
LDAP_UTF8INC(s);
*(s + 2) = 'l';
aclName = s + 3;
s = strchr(aclstr, ';');
if (NULL == s) {
goto error;
}
aclstr = s;
LDAP_UTF8INC(aclstr);
*s = '\0';
/* Here aclName is the acl description string */
aci_item->aclName = slapi_ch_strdup(aclName);
retstr_len = strlen(str) * 3;
ret_str = (char *)slapi_ch_calloc(sizeof(char), retstr_len);
aclutil_str_append_ext(&ret_str, &retstr_len, s_aclstr, strlen(s_aclstr));
aclutil_str_append_ext(&ret_str, &retstr_len, ";", 1);
/* start with the string */
acestr = aclstr;
/*
* Here acestr is something like:
*
* " allow (all) groupdn = "ldap:///cn=Domain Administrators, o=$dn.o, o=ISP";)"
*/
normalize_nextACERule:
/* now we are in the rule part */
tmp_str = acestr;
s = strchr(tmp_str, ';');
if (s == NULL) {
goto error;
}
nextACE = s;
LDAP_UTF8INC(nextACE);
*s = '\0';
/* acestr now will hold copy of the ACE. Also add
** some more space in case we need to add "absolute"
** for deny rule. We will never need more 3 times
** the len (even if all the chars are escaped).
*/
__acl_strip_leading_space(&tmp_str);
len = strlen(tmp_str);
s_acestr = acestr = slapi_ch_calloc(1, 3 * len);
/*
* Now it's something like:
* allow (all) groupdn = "ldap:///cn=Domain Administrators, o=$dn.o, o=ISP";
*/
if (strncasecmp(tmp_str, "allow", 5) == 0) {
memcpy(acestr, tmp_str, len);
tmp_str += 5;
/* gather the rights */
aci_rights_val = __aclp__get_aci_right(tmp_str); /* bug 389975 */
aci_item->aci_type |= ACI_HAS_ALLOW_RULE;
s = strchr(acestr, ')');
if (NULL == s) {
/* wrong syntax */
goto error;
}
/* add "allow(rights...)" */
aclutil_str_append_ext(&ret_str, &retstr_len, acestr, s - acestr + 1);
prevend = s + 1;
} else if (strncasecmp(tmp_str, "deny", 4) == 0) {
char *d_rule = "deny absolute";
/* Then we have to add "absolute" to the deny rule
** What we are doing here is to tackle this situation.
**
** allow -- deny -- allow
** deny -- allow
**
** by using deny absolute we force the precedence rule
** i.e deny has a precedence over allow. Since there doesn't
** seem to be an easy to detect the mix, forcing this
** to all the deny rules will do the job.
*/
__acl_strip_leading_space(&tmp_str);
tmp_str += 4;
/* We might have an absolute there already */
if ((s = strstr(tmp_str, "absolute")) != NULL) {
tmp_str = s;
tmp_str += 8;
}
/* gather the rights */
aci_rights_val = __aclp__get_aci_right(tmp_str); /* bug 389975 */
aci_item->aci_type |= ACI_HAS_DENY_RULE;
len = strlen(d_rule);
memcpy(acestr, d_rule, len);
memcpy(acestr + len, tmp_str, strlen(tmp_str));
s = strchr(acestr, ')');
if (NULL == s) {
/* wrong syntax */
goto error;
}
/* add "deny(rights...)" */
aclutil_str_append_ext(&ret_str, &retstr_len, acestr, s - acestr + 1);
prevend = s + 1;
} else {
/* wrong syntax */
aci_rights_val = -1;
}
if (aci_rights_val == -1) {
/* wrong syntax */
goto error;
} else
aci_item->aci_access |= aci_rights_val;
/* Normalize all the DNs in the userdn, groupdn, roledn rules */
/*
*
* Here acestr starts like this:
* " allow (all) groupdn = "ldap:///cn=Domain Administrators,o=$dn.o,o=ISP"
*/
s = __aclp__getNextLASRule(aci_item, acestr, &end);
while (s && (s < end)) {
if ((0 == strncmp(s, DS_LAS_USERDNATTR, 10)) ||
(0 == strncmp(s, DS_LAS_USERATTR, 8))) {
/*
** For userdnattr/userattr rule, the resources changes and hence
** we cannot cache the result. See above for more comments.
*/
aci_item->aci_elevel = ACI_ELEVEL_USERDNATTR;
rc = __aclp__copy_normalized_str(s, end, prevend,
&ret_str, &retstr_len, 0);
if (rc < 0) {
goto error;
}
} else if (0 == strncmp(s, DS_LAS_USERDN, 6)) {
char *prefix;
p = PL_strnchr(s, '=', end - s);
if (NULL == p) {
goto error;
}
p--;
if (strncmp(p, "!=", 2) == 0) {
aci_item->aci_type |= ACI_CONTAIN_NOT_USERDN;
}
/* XXXrbyrne
* Here we need to scan for more ldap:/// within
* this userdn rule type:
* eg. userdn = "ldap:///cn=joe,o=sun.com || ldap:///self"