forked from 389ds/389-ds-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aclutil.c
1508 lines (1301 loc) · 43.3 KB
/
aclutil.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"
/**************************************************************************
* Defines and usefuls stuff
****************************************************************************/
/*************************************************************************
* Prototypes
*************************************************************************/
static void aclutil__typestr(int type, char str[]);
static void aclutil__Ruletypestr(int type, char str[]);
static char *__aclutil_extract_dn_component(char **e_dns, int position, char *attrName);
static int acl_find_comp_start(char *s, int pos);
static PRIntn acl_ht_free_entry_and_value(PLHashEntry *he, PRIntn i, void *arg);
static PLHashNumber acl_ht_hash(const void *key);
#ifdef FOR_DEBUGGING
static PRIntn acl_ht_display_entry(PLHashEntry *he, PRIntn i, void *arg);
#endif
int acl_match_substr_prefix(char *macro_prefix, const char *ndn, int *exact_match);
/***************************************************************************/
/* UTILITY FUNCTIONS */
/***************************************************************************/
int
aclutil_str_append(char **str1, const char *str2)
{
int new_len;
if (str1 == NULL || str2 == NULL)
return (0);
if (*str1 == NULL) {
new_len = strlen(str2) + 1;
*str1 = (char *)slapi_ch_malloc(new_len);
*str1[0] = 0;
} else {
new_len = strlen(*str1) + strlen(str2) + 1;
*str1 = (char *)slapi_ch_realloc(*str1, new_len);
}
if (*str1 == NULL)
return (-1);
strcat(*str1, str2);
return (0);
}
/*
* dlen: the length of the buffer *dest (not the string length in *dest)
*/
int
aclutil_str_append_ext(char **dest, size_t *dlen, const char *src, size_t slen)
{
char *ptr = NULL;
int rc = 0;
PR_ASSERT(NULL != dlen);
if (dest == NULL || src == NULL) {
return rc;
}
if (0 == slen) {
slen = strlen(src);
}
if (*dest && *dlen > 0) {
size_t dest_strlen = strlen(*dest);
size_t new_len = dest_strlen + slen + 1;
if (new_len > *dlen) {
*dest = (char *)slapi_ch_realloc(*dest, new_len);
*dlen = new_len;
ptr = *dest + dest_strlen;
} else {
ptr = *dest + dest_strlen;
}
} else {
*dlen = slen + 1;
*dest = (char *)slapi_ch_malloc(*dlen);
ptr = *dest;
}
memcpy(ptr, src, slen);
*(ptr + slen) = '\0';
return rc;
}
/***************************************************************************/
/* Print routines */
/***************************************************************************/
/* print eroror message returned from the ACL Library */
#define ACLUTIL_ACLLIB_MSGBUF_LEN 200
void
acl_print_acllib_err(NSErr_t *errp, char *str)
{
char msgbuf[ACLUTIL_ACLLIB_MSGBUF_LEN];
if ((NULL == errp) || !slapi_is_loglevel_set(SLAPI_LOG_ACL))
return;
aclErrorFmt(errp, msgbuf, ACLUTIL_ACLLIB_MSGBUF_LEN, 1);
msgbuf[ACLUTIL_ACLLIB_MSGBUF_LEN - 1] = '\0';
if (strlen(msgbuf) > 0) {
slapi_log_err(SLAPI_LOG_ACL, plugin_name, "acl_print_acllib_err - ACL LIB ERR:(%s)(%s)\n",
msgbuf, str ? str : "NULL");
} else {
slapi_log_err(SLAPI_LOG_ACL, plugin_name, "acl_print_acllib_err - ACL LIB ERR:(%s)\n",
str ? str : "NULL");
}
}
void
aclutil_print_aci(aci_t *aci_item, char *type __attribute__((unused)))
{
char str[BUFSIZ];
const char *dn;
if (!slapi_is_loglevel_set(SLAPI_LOG_ACL))
return;
if (!aci_item) {
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"acl__print_aci: Null item\n");
return;
}
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"***BEGIN ACL INFO[ Name:%s]***\n", aci_item->aclName);
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"ACL Index:%d ACL_ELEVEL:%d\n", aci_item->aci_index, aci_item->aci_elevel);
aclutil__access_str(aci_item->aci_access, str);
aclutil__typestr(aci_item->aci_type, &str[strlen(str)]);
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"ACI type:(%s)\n", str);
aclutil__Ruletypestr(aci_item->aci_ruleType, str);
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"ACI RULE type:(%s)\n", str);
dn = slapi_sdn_get_dn(aci_item->aci_sdn);
slapi_log_err(SLAPI_LOG_ACL, plugin_name, "Slapi_Entry DN:%s\n", dn);
slapi_log_err(SLAPI_LOG_ACL, plugin_name,
"***END ACL INFO*****************************\n");
}
void
aclutil_print_err(int rv, const Slapi_DN *sdn, const struct berval *val, char **errbuf)
{
char ebuf[BUFSIZ];
/*
* The maximum size of line is ebuf_size + the log message
* itself (less than 200 characters for all but potentially ACL_INVALID_TARGET)
*/
char line[BUFSIZ + 200];
char str[1024];
char *lineptr = line;
char *newline = NULL;
if (rv >= 0)
return;
if (val->bv_len > 0 && val->bv_val != NULL) {
PR_snprintf(str, sizeof(str), "%.1023s", val->bv_val);
} else {
str[0] = '\0';
}
switch (rv) {
case ACL_TARGET_FILTER_ERR:
sprintf(lineptr, "ACL Internal Error(%d): "
"Error in generating the target filter for the ACL(%s)\n",
rv, escape_string_with_punctuation(str, ebuf));
break;
case ACL_TARGETATTR_FILTER_ERR:
sprintf(lineptr, "ACL Internal Error(%d): "
"Error in generating the targetattr filter for the ACL(%s)\n",
rv, escape_string_with_punctuation(str, ebuf));
break;
case ACL_TARGETFILTER_ERR:
sprintf(lineptr, "ACL Internal Error(%d): "
"Error in generating the targetfilter filter for the ACL(%s)\n",
rv, escape_string_with_punctuation(str, ebuf));
break;
case ACL_SYNTAX_ERR:
sprintf(lineptr, "ACL Syntax Error(%d):%s\n",
rv, escape_string_with_punctuation(str, ebuf));
break;
case ACL_ONEACL_TEXT_ERR:
sprintf(lineptr, "ACL Syntax Error in the Bind Rules(%d):%s\n",
rv, escape_string_with_punctuation(str, ebuf));
break;
case ACL_ERR_CONCAT_HANDLES:
sprintf(lineptr, "ACL Internal Error(%d): "
"Error in Concatenating List handles\n",
rv);
break;
case ACL_INVALID_TARGET: {
size_t newsize;
const char *dn = slapi_sdn_get_dn(sdn);
if (dn) {
newsize = strlen(dn) + strlen(str) + 200;
} else {
newsize = strlen(str) + 208; /* for "NULL" */
}
if (newsize > sizeof(line)) {
/*
* if (str_length + dn_length + 200 char message) > (BUFSIZ + 200) line
* we have to make space for a bigger line...
*/
newline = slapi_ch_malloc(newsize);
lineptr = newline;
}
sprintf(lineptr, "ACL Invalid Target Error(%d): "
"Target is beyond the scope of the ACL(SCOPE:%s)",
rv, dn ? escape_string_with_punctuation(dn, ebuf) : "NULL");
sprintf(lineptr + strlen(lineptr), " %s\n", escape_string_with_punctuation(str, ebuf));
break;
}
case ACL_INVALID_AUTHMETHOD:
sprintf(lineptr, "ACL Multiple auth method Error(%d):"
"Multiple Authentication Metod in the ACL(%s)\n",
rv, escape_string_with_punctuation(str, ebuf));
break;
case ACL_INVALID_AUTHORIZATION:
sprintf(lineptr, "ACL Syntax Error(%d):"
"Invalid Authorization statement in the ACL(%s)\n",
rv, escape_string_with_punctuation(str, ebuf));
break;
case ACL_INCORRECT_ACI_VERSION:
sprintf(lineptr, "ACL Syntax Error(%d):"
"Incorrect version Number in the ACL(%s)\n",
rv, escape_string_with_punctuation(str, ebuf));
break;
default:
sprintf(lineptr, "ACL Internal Error(%d):"
"ACL generic error (%s)\n",
rv, escape_string_with_punctuation(str, ebuf));
break;
}
if (errbuf) {
/* If a buffer is provided, then copy the error */
aclutil_str_append(errbuf, lineptr);
}
slapi_log_err(SLAPI_LOG_ERR, plugin_name, "aclutil_print_err - %s", lineptr);
slapi_ch_free_string(&newline);
}
/***************************************************************************
* Convert access to str
***************************************************************************/
char *
aclutil__access_str(int type, char str[])
{
char *p;
str[0] = '\0';
p = str;
if (type & SLAPI_ACL_COMPARE) {
strcpy(p, "compare ");
p = strchr(p, '\0');
}
if (type & SLAPI_ACL_SEARCH) {
strcpy(p, "search ");
p = strchr(p, '\0');
}
if (type & SLAPI_ACL_READ) {
strcpy(p, "read ");
p = strchr(p, '\0');
}
if (type & SLAPI_ACL_WRITE) {
strcpy(p, "write ");
p = strchr(p, '\0');
}
if (type & SLAPI_ACL_DELETE) {
strcpy(p, "delete ");
p = strchr(p, '\0');
}
if (type & SLAPI_ACL_ADD) {
strcpy(p, "add ");
p = strchr(p, '\0');
}
if (type & SLAPI_ACL_SELF) {
strcpy(p, "self ");
p = strchr(p, '\0');
}
if (type & SLAPI_ACL_PROXY) {
strcpy(p, "proxy ");
}
return str;
}
/***************************************************************************
* Convert type to str
***************************************************************************/
static void
aclutil__typestr(int type, char str[])
{
char *p;
/* Start copying in at whatever location is passed in */
p = str;
if (type & ACI_TARGET_DN) {
strcpy(p, "target_DN ");
p = strchr(p, '\0');
}
if (type & ACI_TARGET_ATTR) {
strcpy(p, "target_attr ");
p = strchr(p, '\0');
}
if (type & ACI_TARGET_PATTERN) {
strcpy(p, "target_patt ");
p = strchr(p, '\0');
}
if ((type & ACI_TARGET_ATTR_ADD_FILTERS) | (type & ACI_TARGET_ATTR_DEL_FILTERS)) {
strcpy(p, "targetattrfilters ");
p = strchr(p, '\0');
}
if (type & ACI_TARGET_FILTER) {
strcpy(p, "target_filter ");
p = strchr(p, '\0');
}
if (type & ACI_ACLTXT) {
strcpy(p, "acltxt ");
p = strchr(p, '\0');
}
if (type & ACI_TARGET_NOT) {
strcpy(p, "target_not ");
p = strchr(p, '\0');
}
if (type & ACI_TARGET_ATTR_NOT) {
strcpy(p, "target_attr_not ");
p = strchr(p, '\0');
}
if (type & ACI_TARGET_FILTER_NOT) {
strcpy(p, "target_filter_not ");
p = strchr(p, '\0');
}
if (type & ACI_HAS_ALLOW_RULE) {
strcpy(p, "allow_rule ");
p = strchr(p, '\0');
}
if (type & ACI_HAS_DENY_RULE) {
strcpy(p, "deny_rule ");
}
}
static void
aclutil__Ruletypestr(int type, char str[])
{
char *p;
str[0] = '\0';
p = str;
if (type & ACI_USERDN_RULE) {
strcpy(p, "userdn ");
p = strchr(p, '\0');
}
if (type & ACI_USERDNATTR_RULE) {
strcpy(p, "userdnattr ");
p = strchr(p, '\0');
}
if (type & ACI_USERATTR_RULE) {
strcpy(p, "userattr ");
p = strchr(p, '\0');
}
if (type & ACI_GROUPDN_RULE) {
strcpy(p, "groupdn ");
p = strchr(p, '\0');
}
if (type & ACI_GROUPDNATTR_RULE) {
strcpy(p, "groupdnattr ");
p = strchr(p, '\0');
}
if (type & ACI_ROLEDN_RULE) {
strcpy(p, "roledn ");
p = strchr(p, '\0');
}
if (type & ACI_IP_RULE) {
strcpy(p, "ip ");
p = strchr(p, '\0');
}
if (type & ACI_DNS_RULE) {
strcpy(p, "dns ");
p = strchr(p, '\0');
}
if (type & ACI_TIMEOFDAY_RULE) {
strcpy(p, "timeofday ");
p = strchr(p, '\0');
}
if (type & ACI_DAYOFWEEK_RULE) {
strcpy(p, "dayofweek ");
p = strchr(p, '\0');
}
if (type & ACI_AUTHMETHOD_RULE) {
strcpy(p, "authmethod ");
p = strchr(p, '\0');
}
if (type & ACI_PARAM_DNRULE) {
strcpy(p, "paramdn ");
p = strchr(p, '\0');
}
if (type & ACI_PARAM_ATTRRULE) {
strcpy(p, "paramAttr ");
p = strchr(p, '\0');
}
if (type & ACI_SSF_RULE) {
strcpy(p, "ssf ");
}
}
/*
** acl_gen_err_msg
** This function is called by backend to generate the error message
** if access is denied.
*/
void
acl_gen_err_msg(int access, char *edn, char *attr, char **errbuf)
{
char *line = NULL;
if (access & SLAPI_ACL_WRITE) {
line = PR_smprintf(
"Insufficient 'write' privilege to the '%s' attribute of entry '%s'.\n",
attr ? attr : "NULL", edn);
} else if (access & SLAPI_ACL_ADD) {
line = PR_smprintf(
"Insufficient 'add' privilege to add the entry '%s'.\n", edn);
} else if (access & SLAPI_ACL_DELETE) {
line = PR_smprintf(
"Insufficient 'delete' privilege to delete the entry '%s'.\n", edn);
} else if (access & SLAPI_ACL_MODDN) {
line = PR_smprintf(
"Insufficient 'moddn' privilege to move an entry to '%s'.\n", edn);
}
aclutil_str_append(errbuf, line);
if (line) {
PR_smprintf_free(line);
line = NULL;
}
}
short
aclutil_gen_signature(short c_signature)
{
short o_signature = 0;
short randval = (short)slapi_rand();
o_signature = c_signature ^ (randval % 32768);
if (!o_signature) {
randval = (short)slapi_rand();
o_signature = c_signature ^ (randval % 32768);
}
return o_signature;
}
void
aclutil_print_resource(struct acl_pblock *aclpb, const char *right, char *attr, char *clientdn)
{
char str[BUFSIZ];
const char *dn;
if (aclpb == NULL)
return;
if (!slapi_is_loglevel_set(SLAPI_LOG_ACL))
return;
slapi_log_err(SLAPI_LOG_ACL, plugin_name, " ************ RESOURCE INFO STARTS *********\n");
slapi_log_err(SLAPI_LOG_ACL, plugin_name, " Client DN: %s\n",
clientdn ? clientdn : "NULL");
aclutil__access_str(aclpb->aclpb_access, str);
aclutil__typestr(aclpb->aclpb_res_type, &str[strlen(str)]);
slapi_log_err(SLAPI_LOG_ACL, plugin_name, " resource type:%d(%s)\n",
aclpb->aclpb_res_type, str);
dn = slapi_sdn_get_dn(aclpb->aclpb_curr_entry_sdn);
slapi_log_err(SLAPI_LOG_ACL, plugin_name, " Slapi_Entry DN: %s\n",
dn ? dn : "NULL");
slapi_log_err(SLAPI_LOG_ACL, plugin_name, " ATTR: %s\n", attr ? attr : "NULL");
slapi_log_err(SLAPI_LOG_ACL, plugin_name, " rights:%s\n", right ? right : "NULL");
slapi_log_err(SLAPI_LOG_ACL, plugin_name, " ************ RESOURCE INFO ENDS *********\n");
}
/*
* The input string contains a rule like
* "cn=helpdesk, ou=$attr.deptName, o=$dn.o, o=ISP"
*
* Where $attr -- means look into the attribute list for values
* $dn -- means look into the entry's dn
*
* We extract the values from the entry and returned a string
* with the values added.
* For "$attr" rule - if we find multiple values then it is
* the pattern is not expanded.
* For "$dn" rule, if we find multiple of them, we use the relative
* position.
* NOTE: The caller is responsible in freeing the memory.
*/
char *
aclutil_expand_paramString(char *str, Slapi_Entry *e)
{
char **e_dns;
char **a_dns;
char *attrName;
char *s, *p;
char *attrVal = NULL;
int i, len;
int ncomponents, type;
int rc = -1;
char *buf = NULL;
if ((NULL == slapi_entry_get_ndn(e)) || (NULL == str)) {
return NULL;
}
e_dns = slapi_ldap_explode_dn(slapi_entry_get_ndn(e), 0);
a_dns = slapi_ldap_explode_dn(str, 0);
if ((NULL == e_dns) || (NULL == a_dns)) {
goto cleanup;
}
i = 0;
ncomponents = 0;
while (a_dns[ncomponents])
ncomponents++;
for (i = 0; i < ncomponents; i++) {
/* Look for"$" char */
if ((s = strchr(a_dns[i], '$')) != NULL) {
p = s;
s++;
if (strncasecmp(s, "dn", 2) == 0)
type = 1;
else if (strncasecmp(s, "attr", 4) == 0)
type = 2;
else {
/* error */
goto cleanup;
}
*p = '\0';
aclutil_str_append(&buf, a_dns[i]);
if (type == 1) {
/* xyz = $dn.o */
s += 3;
attrName = s;
attrVal = __aclutil_extract_dn_component(e_dns,
ncomponents - i, attrName);
if (NULL == attrVal) /*error*/
goto cleanup;
} else {
Slapi_Attr *attr;
const struct berval *attrValue;
int kk;
Slapi_Value *sval, *t_sval;
/* The pattern is x=$attr.o" */
s += 5;
attrName = s;
slapi_entry_attr_find(e, attrName, &attr);
if (NULL == attr)
goto cleanup;
kk = slapi_attr_first_value(attr, &sval);
if (kk != -1) {
t_sval = sval;
kk = slapi_attr_next_value(attr, kk, &sval);
if (kk != -1) /* can't handle multiple --error */
goto cleanup;
attrValue = slapi_value_get_berval(t_sval);
attrVal = attrValue->bv_val;
}
}
} else {
attrVal = a_dns[i];
}
aclutil_str_append(&buf, attrVal);
aclutil_str_append(&buf, ",");
}
rc = 0; /* everything is okay*/
/* remove the last comma */
if (buf) {
len = strlen(buf);
buf[len - 1] = '\0';
}
cleanup:
slapi_ldap_value_free(a_dns);
slapi_ldap_value_free(e_dns);
if (0 != rc) /* error */ {
slapi_ch_free_string(&buf);
}
return buf;
}
static char *
__aclutil_extract_dn_component(char **e_dns, int position, char *attrName)
{
int i, matched, len;
char *s;
int matchedPosition = 0;
len = strlen(attrName);
/* First check if there thare are multiple of these */
i = matched = 0;
while (e_dns[i]) {
if (0 == strncasecmp(e_dns[i], attrName, len)) {
matched++;
matchedPosition = i;
}
i++;
}
if (!matched)
return NULL;
if (matched > 1) {
matchedPosition = i - position;
}
if (NULL == e_dns[matchedPosition])
return NULL;
s = strstr(e_dns[matchedPosition], "=");
if (NULL == s)
return NULL;
else
return s + 1;
}
/*
* Does the first component of ndn match the first component of match_this ?
*/
int
acl_dn_component_match(const char *ndn __attribute__((unused)), char *match_this __attribute__((unused)), int component_number __attribute__((unused)))
{
return (1);
}
/*
* Here, ndn is a resource dn and match_this is a dn, containing a macro, ($dn).
*
* eg. ndn is cn=fred,ou=groups,ou=people,ou=icnc,o=ISP and
* match_this is "ou=Groups,($dn),o=ISP" or
* "cn=*,ou=Groups,($dn),o=ISP".
*
* They match if:
* match_this is a suffix of ndn
*
* It returns NULL, if they do not match.
* Otherwise it returns a copy of the substring of ndn that matches the ($dn).
*
* eg. in the above example, "ou=people,ou=icnc"
*/
char *
acl_match_macro_in_target(const char *ndn, char *match_this, char *macro_ptr)
{
char *macro_prefix = NULL;
int macro_prefix_len = 0;
char *macro_suffix = NULL;
char *tmp_ptr = NULL;
char *matched_val = NULL;
char *ret_val = NULL;
int ndn_len = 0;
int macro_suffix_len = 0;
int ndn_prefix_len = 0;
int ndn_prefix_end = 0;
int matched_val_len = 0;
/*
* First, grab the macro_suffix--the bit after the ($dn)
*
*/
if (strlen(macro_ptr) == strlen(ACL_TARGET_MACRO_DN_KEY)) {
macro_suffix = NULL; /* just ($dn) */
} else {
if (macro_ptr[strlen(ACL_TARGET_MACRO_DN_KEY)] == ',') {
macro_suffix = ¯o_ptr[strlen(ACL_TARGET_MACRO_DN_KEY) + 1];
} else {
macro_suffix = ¯o_ptr[strlen(ACL_TARGET_MACRO_DN_KEY)];
}
}
/*
* First ensure that the suffix of match_this is
* a suffix of ndn.
*/
ndn_len = strlen(ndn);
if (macro_suffix != NULL) {
macro_suffix_len = strlen(macro_suffix);
if (macro_suffix_len >= ndn_len) {
/*
* eg ndn: o=icnc,o=sun.com
* match_this: ($dn),o=icnc,o=sun.com
*/
return (NULL); /* ($dn) must match something. */
} else {
/*
* eg ndn: ou=People,o=icnc,o=sun.com
* match_this: ($dn),o=icnc,o=sun.com
*
* we can do a direct strncmp() because we know that
* there can be no "*" after the ($dn)...by definition.
*/
if (strncasecmp(macro_suffix, &ndn[ndn_len - macro_suffix_len],
macro_suffix_len) != 0) {
return (NULL); /* suffix must match */
}
}
}
/* Here, macro_suffix is a suffix of ndn.
*
*
* Now, look at macro_prefix, if it is NULL, then ($dn) matches
* ndn[0..ndn_len-macro_suffix_len].
* (eg, ndn: cn=fred,ou=People,o=sun.com
* match_this: ($dn),o=sun.com.
*
*/
macro_prefix = slapi_ch_strdup(match_this);
/* we know it's got a $(dn) */
tmp_ptr = PL_strcasestr(macro_prefix, ACL_TARGET_MACRO_DN_KEY);
if (!tmp_ptr) {
slapi_log_err(SLAPI_LOG_ACL, plugin_name, "acl_match_macro_in_target - "
"Target macro DN key \"%s\" not found in \"%s\".\n",
ACL_TARGET_MACRO_DN_KEY, macro_prefix);
slapi_ch_free_string(¯o_prefix);
return ret_val;
}
*tmp_ptr = '\0';
/* There may be a NULL prefix eg. match_this: ($dn),o=sun.com */
macro_prefix_len = strlen(macro_prefix);
if (macro_prefix_len == 0) {
slapi_ch_free_string(¯o_prefix);
macro_prefix = NULL;
}
if (macro_prefix == NULL) {
/*
* ($dn) matches ndn[0..ndn_len-macro_suffix_len]
*/
int val_len = 0;
val_len = ndn_len - macro_suffix_len;
matched_val = (char *)slapi_ch_malloc(val_len + 1);
strncpy(matched_val, ndn, ndn_len - macro_suffix_len);
/*
* Null terminate matched_val, removing trailing "," if there is
* one.
*/
if (val_len > 1) {
if (matched_val[val_len - 1] == ',') {
matched_val[val_len - 1] = '\0';
} else {
matched_val[val_len] = '\0';
}
}
ret_val = matched_val;
} else {
/*
* If it is not NULL, then if macro_prefix contains a * then
* it needs to be an exact prefix of ndn (modulo the * component
* which matches anything) becuase that's the semantics
* of target patterns containing *'s, except that we just
* make it match one component.
* If it is such a prefix then ($dn) matches that portion of ndn
* from the end of the prefix, &ndn[ndn_prefix_end] to
* ndn_suffix_start.
* If ndn_prefix_len > ndn_len-macro_suffix_len then return(NULL),
* otherwise $(dn) matches ndn[ndn_prefix_len..ndn_len-macro_suffix_len].
*
*
* eg. ndn: cn=fred,ou=P,o=sun.com
* match_this: cn=*,($dn),o=sun.com
*/
if (strstr(macro_prefix, "=*") != NULL) {
int exact_match = 0;
if (macro_prefix[macro_prefix_len - 1] == ',') {
/* macro aligns with dn components */
ndn_prefix_len = acl_match_prefix(macro_prefix, ndn, &exact_match);
} else {
/* do a initial * final substring match */
ndn_prefix_len = acl_match_substr_prefix(macro_prefix, ndn, &exact_match);
}
if (ndn_prefix_len != -1) {
/*
* ndn[0..ndn_prefix_len] is the prefix in ndn.
* ndn[ndn_prefix_len..ndn_len-macro_suffix_len] is the
* matched string.
*/
if (ndn_prefix_len >= ndn_len - macro_suffix_len) {
/*
* eg ndn: cn=fred,ou=People,o=icnc,o=sun.com
* cn=*,ou=People,o=icnc,($dn),o=icnc,o=sun.com
*/
ret_val = NULL; /* matched string is empty */
} else {
/*
* eg ndn: cn=fred,ou=People,o=icnc,o=sun.com
* cn=*,ou=People,($dn),o=sun.com
*/
matched_val_len = ndn_len - macro_suffix_len - ndn_prefix_len;
matched_val = (char *)slapi_ch_malloc(matched_val_len + 1);
strncpy(matched_val, &ndn[ndn_prefix_len], matched_val_len);
if (matched_val_len > 1) {
if (matched_val[matched_val_len - 1] == ',') {
matched_val[matched_val_len - 1] = '\0';
} else {
matched_val[matched_val_len] = '\0';
}
}
matched_val[matched_val_len] = '\0';
ret_val = matched_val;
}
} else {
/* Was not a prefix so not a match */
ret_val = NULL;
}
} else {
/*
*
* If macro_prefix is not NULL and it does not
* contain a =* then
* we need to ensure that macro_prefix is a substring
* ndn.
* If it is and the position of the character after it's end in
* ndn is
* ndn_prefix_end then ($dn) matches
* ndn[ndn_prefix_end..ndn_len-macro_suffix_len].
*
*
* One important principal is that ($dn) matches a maximal
* chunk--this way it will serve to make the link
* between resources and users at each level of the structure.
*
* eg. ndn: ou=Groups,ou=Groups,ou=Groups,c=fr
* macro_prefix: ou=Groups,($dn),c=fr
*
* then ($dn) matches ou=Groups,ou=Groups.
*
*
*
* If it is not a substring, then there is no match.
* If it is a substring and
* ndn[ndn_prefix_end..ndn_len-macro_suffix_len] is empty then
* it's also not a match as we demand that ($dn) match a non-empty
* string.
*
*
*
* (eg. ndn: cn=fred,o=icnc,ou=People,o=sun.com
* match_this: o=icnc,($dn),o=sun.com.)
*
*
* (eg. ndn: cn=fred,o=menlo park,ou=People,o=icnc,o=sun.com
* match_this: o=menlo park,ou=People,($dn),o=sun.com
*
*/
ndn_prefix_end = acl_strstr((char *)ndn, macro_prefix);
if (ndn_prefix_end == -1) {
ret_val = NULL;
} else {
/* Is a substring */
ndn_prefix_end += macro_prefix_len;
/*
* make sure the matching part is non-empty:
*
* ndn[ndn_prefix_end..mndn_len-macro_suffix_len].
*/
if (ndn_prefix_end >= ndn_len - macro_suffix_len) {
ret_val = NULL;
} else {
/*
* ($dn) matches the non-empty string segment
* ndn[ndn_prefix_end..mndn_len-macro_suffix_len]
* the -1 is because macro_suffix_eln does not include
* the coma before the suffix.
*
* there are cases where the macro does end inside a dn component
*/
matched_val_len = ndn_len - macro_suffix_len -
ndn_prefix_end;
if (ndn[ndn_len - macro_suffix_len - 1] == ',')
matched_val_len -= 1;
matched_val = (char *)slapi_ch_malloc(matched_val_len + 1);
strncpy(matched_val, &ndn[ndn_prefix_end],
matched_val_len);
matched_val[matched_val_len] = '\0';
ret_val = matched_val;
}
}
} /* contains an =* */
slapi_ch_free_string(¯o_prefix);
} /* macro_prefix != NULL */
return (ret_val);
}
int
acl_match_substr_prefix(char *macro_prefix, const char *ndn, int *exact_match)
{
int ret_code = -1;
char *tmp_str = NULL;
int initial, any, final;
*exact_match = 0;
tmp_str = slapi_ch_strdup(macro_prefix);
any = acl_strstr(tmp_str, "*");
tmp_str[any] = '\0';
initial = acl_strstr((char *)ndn, tmp_str);
if (initial >= 0) {
final = acl_strstr((char *)&ndn[initial + strlen(tmp_str)], &tmp_str[any + 1]);
if (final > 0)
ret_code = initial + strlen(tmp_str) + final + strlen(&tmp_str[any + 1]);
}
slapi_ch_free_string(&tmp_str);
return (ret_code);
}
/*
* Checks to see if macro_prefix is an exact prefix of ndn.
* macro_prefix may contain a * component.
*
* The length of the matched prefix in ndn is returned.
* If it was not a match, a negative int is returned.
* Also, if the string matched exactly,
* exact_match is set to 1, other wise it was a proper prefix.
*
*/
int
acl_match_prefix(char *macro_prefix, const char *ndn, int *exact_match)
{
int ret_code = -1;
int macro_prefix_len = 0;
int ndn_len = 0;
int i = 0;
int j = 0;
int done = 0;
int t = 0;
char *tmp_str = NULL;
int k, l = 0;