1 /* aclparse.c - routines to parse and check acl's */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
33 #include <ac/socket.h>
34 #include <ac/string.h>
35 #include <ac/unistd.h>
41 static void split(char *line, int splitchar, char **left, char **right);
42 static void access_append(Access **l, Access *a);
43 static void acl_usage(void) LDAP_GCCATTR((noreturn));
45 static void acl_regex_normalized_dn(const char *src, struct berval *pat);
48 static void print_acl(Backend *be, AccessControl *a);
49 static void print_access(Access *b);
53 regtest(const char *fname, int lineno, char *pat) {
69 for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
71 if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
88 if ( size >= (sizeof(buf)-1) ) {
90 "%s: line %d: regular expression \"%s\" too large\n",
95 if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
97 regerror(e, &re, error, sizeof(error));
99 "%s: line %d: regular expression \"%s\" bad because of %s\n",
100 fname, lineno, pat, error );
116 char *left, *right, *style;
124 for ( i = 1; i < argc; i++ ) {
125 /* to clause - select which entries are protected */
126 if ( strcasecmp( argv[i], "to" ) == 0 ) {
128 fprintf( stderr, "%s: line %d: "
129 "only one to clause allowed in access line\n",
133 a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
134 for ( ++i; i < argc; i++ ) {
135 if ( strcasecmp( argv[i], "by" ) == 0 ) {
140 if ( strcasecmp( argv[i], "*" ) == 0 ) {
141 if( a->acl_dn_pat.bv_len ||
142 ( a->acl_dn_style != ACL_STYLE_REGEX ) )
145 "%s: line %d: dn pattern"
146 " already specified in to clause.\n",
151 a->acl_dn_pat.bv_val = ch_strdup( "*" );
152 a->acl_dn_pat.bv_len = 1;
156 split( argv[i], '=', &left, &right );
157 split( left, '.', &left, &style );
159 if ( right == NULL ) {
160 fprintf( stderr, "%s: line %d: "
161 "missing \"=\" in \"%s\" in to clause\n",
162 fname, lineno, left );
166 if ( strcasecmp( left, "dn" ) == 0 ) {
167 if( a->acl_dn_pat.bv_len != 0 ||
168 ( a->acl_dn_style != ACL_STYLE_REGEX ) )
171 "%s: line %d: dn pattern"
172 " already specified in to clause.\n",
177 if ( style == NULL || *style == '\0' ||
178 ( strcasecmp( style, "base" ) == 0 ) ||
179 ( strcasecmp( style, "exact" ) == 0 ))
181 a->acl_dn_style = ACL_STYLE_BASE;
182 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
184 } else if ( strcasecmp( style, "one" ) == 0 ) {
185 a->acl_dn_style = ACL_STYLE_ONE;
186 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
188 } else if ( strcasecmp( style, "subtree" ) == 0
189 || strcasecmp( style, "sub" ) == 0 )
191 if( *right == '\0' ) {
192 a->acl_dn_pat.bv_val = ch_strdup( "*" );
193 a->acl_dn_pat.bv_len = 1;
196 a->acl_dn_style = ACL_STYLE_SUBTREE;
197 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
200 } else if ( strcasecmp( style, "children" ) == 0 ) {
201 a->acl_dn_style = ACL_STYLE_CHILDREN;
202 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
204 } else if ( strcasecmp( style, "regex" ) == 0 ) {
205 a->acl_dn_style = ACL_STYLE_REGEX;
207 if ( *right == '\0' ) {
208 /* empty regex should match empty DN */
209 a->acl_dn_style = ACL_STYLE_BASE;
210 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
212 } else if ( strcmp(right, "*") == 0
213 || strcmp(right, ".*") == 0
214 || strcmp(right, ".*$") == 0
215 || strcmp(right, "^.*") == 0
216 || strcmp(right, "^.*$") == 0
217 || strcmp(right, ".*$$") == 0
218 || strcmp(right, "^.*$$") == 0 )
220 a->acl_dn_pat.bv_val = ch_strdup( "*" );
221 a->acl_dn_pat.bv_len = sizeof("*")-1;
224 acl_regex_normalized_dn( right, &a->acl_dn_pat );
228 fprintf( stderr, "%s: line %d: "
229 "unknown dn style \"%s\" in to clause\n",
230 fname, lineno, style );
237 if ( strcasecmp( left, "filter" ) == 0 ) {
238 if ( (a->acl_filter = str2filter( right )) == NULL ) {
240 "%s: line %d: bad filter \"%s\" in to clause\n",
241 fname, lineno, right );
245 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
246 a->acl_attrs = str2anlist( a->acl_attrs,
248 if ( a->acl_attrs == NULL ) {
250 "%s: line %d: unknown attr \"%s\" in to clause\n",
251 fname, lineno, right );
255 } else if ( strncasecmp( left, "val", 3 ) == 0 ) {
256 if ( a->acl_attrval.bv_len ) {
258 "%s: line %d: attr val already specified in to clause.\n",
262 if ( a->acl_attrs == NULL || a->acl_attrs[1].an_name.bv_val ) {
264 "%s: line %d: attr val requires a single attribute.\n",
268 ber_str2bv( right, 0, 1, &a->acl_attrval );
269 if ( style && strcasecmp( style, "regex" ) == 0 ) {
270 int e = regcomp( &a->acl_attrval_re, a->acl_attrval.bv_val,
271 REG_EXTENDED | REG_ICASE | REG_NOSUB );
274 regerror( e, &a->acl_attrval_re, buf, sizeof(buf) );
275 fprintf( stderr, "%s: line %d: "
276 "regular expression \"%s\" bad because of %s\n",
277 fname, lineno, right, buf );
280 a->acl_attrval_style = ACL_STYLE_REGEX;
282 /* FIXME: if the attribute has DN syntax,
283 * we might allow subtree and children styles as well */
284 if ( !strcasecmp( style, "exact" ) ) {
285 a->acl_attrval_style = ACL_STYLE_BASE;
287 } else if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
288 if ( !strcasecmp( style, "base" ) ) {
289 a->acl_attrval_style = ACL_STYLE_BASE;
290 } else if ( !strcasecmp( style, "children" ) ) {
291 a->acl_attrval_style = ACL_STYLE_CHILDREN;
292 } else if ( !strcasecmp( style, "onelevel" ) || !strcasecmp( style, "one" ) ) {
293 a->acl_attrval_style = ACL_STYLE_ONE;
294 } else if ( !strcasecmp( style, "subtree" ) || !strcasecmp( style, "sub" ) ) {
295 a->acl_attrval_style = ACL_STYLE_SUBTREE;
298 "%s: line %d: unknown val.<style>, got \"%s\" (ignored)\n",
299 fname, lineno, style );
300 a->acl_attrval_style = ACL_STYLE_BASE;
305 "%s: line %d: unknown val.<style>, got \"%s\" (ignored)\n",
306 fname, lineno, style );
307 a->acl_attrval_style = ACL_STYLE_BASE;
313 "%s: line %d: expecting <what> got \"%s\"\n",
314 fname, lineno, left );
319 if ( a->acl_dn_pat.bv_len != 0 &&
320 strcmp(a->acl_dn_pat.bv_val, "*") == 0 )
322 free( a->acl_dn_pat.bv_val );
323 a->acl_dn_pat.bv_val = NULL;
324 a->acl_dn_pat.bv_len = 0;
327 if( a->acl_dn_pat.bv_len != 0 ||
328 ( a->acl_dn_style != ACL_STYLE_REGEX ) )
330 if ( a->acl_dn_style != ACL_STYLE_REGEX ) {
332 rc = dnNormalize( 0, NULL, NULL, &a->acl_dn_pat, &bv, NULL);
333 if ( rc != LDAP_SUCCESS ) {
335 "%s: line %d: bad DN \"%s\" in to DN clause\n",
336 fname, lineno, a->acl_dn_pat.bv_val );
339 free( a->acl_dn_pat.bv_val );
342 int e = regcomp( &a->acl_dn_re, a->acl_dn_pat.bv_val,
343 REG_EXTENDED | REG_ICASE );
346 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
347 fprintf( stderr, "%s: line %d: "
348 "regular expression \"%s\" bad because of %s\n",
349 fname, lineno, right, buf );
355 /* by clause - select who has what access to entries */
356 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
358 fprintf( stderr, "%s: line %d: "
359 "to clause required before by clause in access line\n",
365 * by clause consists of <who> and <access>
368 b = (Access *) ch_calloc( 1, sizeof(Access) );
370 ACL_INVALIDATE( b->a_access_mask );
374 "%s: line %d: premature eol: expecting <who>\n",
380 for ( ; i < argc; i++ ) {
381 slap_style_t sty = ACL_STYLE_REGEX;
382 char *style_modifier = NULL;
385 split( argv[i], '=', &left, &right );
386 split( left, '.', &left, &style );
388 split( style, ',', &style, &style_modifier);
391 if ( style == NULL || *style == '\0' ||
392 strcasecmp( style, "exact" ) == 0 ||
393 strcasecmp( style, "base" ) == 0 )
395 sty = ACL_STYLE_BASE;
397 } else if ( strcasecmp( style, "one" ) == 0 ) {
400 } else if ( strcasecmp( style, "subtree" ) == 0 ||
401 strcasecmp( style, "sub" ) == 0 )
403 sty = ACL_STYLE_SUBTREE;
405 } else if ( strcasecmp( style, "children" ) == 0 ) {
406 sty = ACL_STYLE_CHILDREN;
408 } else if ( strcasecmp( style, "regex" ) == 0 ) {
409 sty = ACL_STYLE_REGEX;
413 "%s: line %d: unknown style \"%s\" in by clause\n",
414 fname, lineno, style );
418 if ( style_modifier &&
419 strcasecmp( style_modifier, "expand" ) == 0 )
424 if ( strcasecmp( argv[i], "*" ) == 0 ) {
425 bv.bv_val = ch_strdup( "*" );
427 sty = ACL_STYLE_REGEX;
429 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
430 ber_str2bv("anonymous", sizeof("anonymous")-1, 1, &bv);
431 sty = ACL_STYLE_REGEX;
433 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
434 ber_str2bv("self", sizeof("self")-1, 1, &bv);
435 sty = ACL_STYLE_REGEX;
437 } else if ( strcasecmp( argv[i], "users" ) == 0 ) {
438 ber_str2bv("users", sizeof("users")-1, 1, &bv);
439 sty = ACL_STYLE_REGEX;
441 } else if ( strcasecmp( left, "dn" ) == 0 ) {
442 if ( sty == ACL_STYLE_REGEX ) {
443 b->a_dn_style = ACL_STYLE_REGEX;
444 if( right == NULL ) {
449 } else if (*right == '\0' ) {
451 ber_str2bv("anonymous",
452 sizeof("anonymous")-1,
454 } else if ( strcmp( right, "*" ) == 0 ) {
456 /* any or users? users for now */
460 } else if ( strcmp( right, ".+" ) == 0
461 || strcmp( right, "^.+" ) == 0
462 || strcmp( right, ".+$" ) == 0
463 || strcmp( right, "^.+$" ) == 0
464 || strcmp( right, ".+$$" ) == 0
465 || strcmp( right, "^.+$$" ) == 0 )
470 } else if ( strcmp( right, ".*" ) == 0
471 || strcmp( right, "^.*" ) == 0
472 || strcmp( right, ".*$" ) == 0
473 || strcmp( right, "^.*$" ) == 0
474 || strcmp( right, ".*$$" ) == 0
475 || strcmp( right, "^.*$$" ) == 0 )
482 acl_regex_normalized_dn( right, &bv );
483 if ( !ber_bvccmp( &bv, '*' ) ) {
484 regtest(fname, lineno, bv.bv_val);
487 } else if ( right == NULL || *right == '\0' ) {
488 fprintf( stderr, "%s: line %d: "
489 "missing \"=\" in (or value after) \"%s\" "
491 fname, lineno, left );
495 ber_str2bv( right, 0, 1, &bv );
502 if( bv.bv_val != NULL ) {
503 if( b->a_dn_pat.bv_len != 0 ) {
505 "%s: line %d: dn pattern already specified.\n",
510 if ( sty != ACL_STYLE_REGEX && expand == 0 ) {
511 rc = dnNormalize(0, NULL, NULL,
512 &bv, &b->a_dn_pat, NULL);
513 if ( rc != LDAP_SUCCESS ) {
515 "%s: line %d: bad DN \"%s\" in by DN clause\n",
516 fname, lineno, bv.bv_val );
524 b->a_dn_expand = expand;
528 if ( strcasecmp( left, "dnattr" ) == 0 ) {
529 if ( right == NULL || right[ 0 ] == '\0' ) {
531 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
532 fname, lineno, left );
536 if( b->a_dn_at != NULL ) {
538 "%s: line %d: dnattr already specified.\n",
543 rc = slap_str2ad( right, &b->a_dn_at, &text );
545 if( rc != LDAP_SUCCESS ) {
547 "%s: line %d: dnattr \"%s\": %s\n",
548 fname, lineno, right, text );
553 if( !is_at_syntax( b->a_dn_at->ad_type,
555 !is_at_syntax( b->a_dn_at->ad_type,
556 SLAPD_NAMEUID_SYNTAX ))
559 "%s: line %d: dnattr \"%s\": "
560 "inappropriate syntax: %s\n",
561 fname, lineno, right,
562 b->a_dn_at->ad_type->sat_syntax_oid );
566 if( b->a_dn_at->ad_type->sat_equality == NULL ) {
568 "%s: line %d: dnattr \"%s\": "
569 "inappropriate matching (no EQUALITY)\n",
570 fname, lineno, right );
577 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
581 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
582 fprintf( stderr, "%s: line %d: "
583 "inappropriate style \"%s\" in by clause\n",
584 fname, lineno, style );
588 if ( right == NULL || right[ 0 ] == '\0' ) {
589 fprintf( stderr, "%s: line %d: "
590 "missing \"=\" in (or value after) \"%s\" "
592 fname, lineno, left );
596 if( b->a_group_pat.bv_len ) {
598 "%s: line %d: group pattern already specified.\n",
603 /* format of string is
604 "group/objectClassValue/groupAttrName" */
605 if ((value = strchr(left, '/')) != NULL) {
607 if (*value && (name = strchr(value, '/')) != NULL) {
612 b->a_group_style = sty;
613 if (sty == ACL_STYLE_REGEX) {
614 acl_regex_normalized_dn( right, &bv );
615 if ( !ber_bvccmp( &bv, '*' ) ) {
616 regtest(fname, lineno, bv.bv_val);
620 ber_str2bv( right, 0, 0, &bv );
621 rc = dnNormalize( 0, NULL, NULL, &bv,
622 &b->a_group_pat, NULL );
623 if ( rc != LDAP_SUCCESS ) {
625 "%s: line %d: bad DN \"%s\"\n",
626 fname, lineno, right );
631 if (value && *value) {
632 b->a_group_oc = oc_find( value );
635 if( b->a_group_oc == NULL ) {
637 "%s: line %d: group objectclass "
639 fname, lineno, value );
643 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
645 if( b->a_group_oc == NULL ) {
647 "%s: line %d: group default objectclass "
649 fname, lineno, SLAPD_GROUP_CLASS );
654 if( is_object_subclass( slap_schema.si_oc_referral,
658 "%s: line %d: group objectclass \"%s\" "
659 "is subclass of referral\n",
660 fname, lineno, value );
664 if( is_object_subclass( slap_schema.si_oc_alias,
668 "%s: line %d: group objectclass \"%s\" "
669 "is subclass of alias\n",
670 fname, lineno, value );
675 rc = slap_str2ad( name, &b->a_group_at, &text );
677 if( rc != LDAP_SUCCESS ) {
679 "%s: line %d: group \"%s\": %s\n",
680 fname, lineno, right, text );
685 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
687 if( rc != LDAP_SUCCESS ) {
689 "%s: line %d: group \"%s\": %s\n",
690 fname, lineno, SLAPD_GROUP_ATTR, text );
695 if( !is_at_syntax( b->a_group_at->ad_type,
697 !is_at_syntax( b->a_group_at->ad_type,
698 SLAPD_NAMEUID_SYNTAX ) &&
699 !is_at_subtype( b->a_group_at->ad_type, slap_schema.si_ad_labeledURI->ad_type ))
702 "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
703 fname, lineno, right,
704 b->a_group_at->ad_type->sat_syntax_oid );
711 struct berval vals[2];
713 vals[0].bv_val = b->a_group_oc->soc_oid;
714 vals[0].bv_len = strlen(vals[0].bv_val);
715 vals[1].bv_val = NULL;
718 rc = oc_check_allowed( b->a_group_at->ad_type,
722 fprintf( stderr, "%s: line %d: "
723 "group: \"%s\" not allowed by \"%s\"\n",
725 b->a_group_at->ad_cname.bv_val,
726 b->a_group_oc->soc_oid );
733 if ( strcasecmp( left, "peername" ) == 0 ) {
734 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
735 fprintf( stderr, "%s: line %d: "
736 "inappropriate style \"%s\" in by clause\n",
737 fname, lineno, style );
741 if ( right == NULL || right[ 0 ] == '\0' ) {
742 fprintf( stderr, "%s: line %d: "
743 "missing \"=\" in (or value after) \"%s\" "
745 fname, lineno, left );
749 if( b->a_peername_pat.bv_len ) {
750 fprintf( stderr, "%s: line %d: "
751 "peername pattern already specified.\n",
756 b->a_peername_style = sty;
757 if (sty == ACL_STYLE_REGEX) {
758 acl_regex_normalized_dn( right, &bv );
759 if ( !ber_bvccmp( &bv, '*' ) ) {
760 regtest(fname, lineno, bv.bv_val);
762 b->a_peername_pat = bv;
764 ber_str2bv( right, 0, 1, &b->a_peername_pat );
769 if ( strcasecmp( left, "sockname" ) == 0 ) {
770 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
771 fprintf( stderr, "%s: line %d: "
772 "inappropriate style \"%s\" in by clause\n",
773 fname, lineno, style );
777 if ( right == NULL || right[ 0 ] == '\0' ) {
778 fprintf( stderr, "%s: line %d: "
779 "missing \"=\" in (or value after) \"%s\" "
781 fname, lineno, left );
785 if( b->a_sockname_pat.bv_len ) {
786 fprintf( stderr, "%s: line %d: "
787 "sockname pattern already specified.\n",
792 b->a_sockname_style = sty;
793 if (sty == ACL_STYLE_REGEX) {
794 acl_regex_normalized_dn( right, &bv );
795 if ( !ber_bvccmp( &bv, '*' ) ) {
796 regtest(fname, lineno, bv.bv_val);
798 b->a_sockname_pat = bv;
800 ber_str2bv( right, 0, 1, &b->a_sockname_pat );
805 if ( strcasecmp( left, "domain" ) == 0 ) {
807 case ACL_STYLE_REGEX:
809 case ACL_STYLE_SUBTREE:
814 "%s: line %d: inappropriate style \"%s\" in by clause\n",
815 fname, lineno, style );
819 if ( right == NULL || right[ 0 ] == '\0' ) {
821 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
822 fname, lineno, left );
826 if( b->a_domain_pat.bv_len ) {
828 "%s: line %d: domain pattern already specified.\n",
833 b->a_domain_style = sty;
834 b->a_domain_expand = expand;
835 if (sty == ACL_STYLE_REGEX) {
836 acl_regex_normalized_dn( right, &bv );
837 if ( !ber_bvccmp( &bv, '*' ) ) {
838 regtest(fname, lineno, bv.bv_val);
840 b->a_domain_pat = bv;
842 ber_str2bv( right, 0, 1, &b->a_domain_pat );
847 if ( strcasecmp( left, "sockurl" ) == 0 ) {
848 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
850 "%s: line %d: inappropriate style \"%s\" in by clause\n",
851 fname, lineno, style );
855 if ( right == NULL || right[ 0 ] == '\0' ) {
857 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
858 fname, lineno, left );
862 if( b->a_sockurl_pat.bv_len ) {
864 "%s: line %d: sockurl pattern already specified.\n",
869 b->a_sockurl_style = sty;
870 if (sty == ACL_STYLE_REGEX) {
871 acl_regex_normalized_dn( right, &bv );
872 if ( !ber_bvccmp( &bv, '*' ) ) {
873 regtest(fname, lineno, bv.bv_val);
875 b->a_sockurl_pat = bv;
877 ber_str2bv( right, 0, 1, &b->a_sockurl_pat );
882 if ( strcasecmp( left, "set" ) == 0 ) {
883 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
885 "%s: line %d: inappropriate style \"%s\" in by clause\n",
886 fname, lineno, style );
890 if( b->a_set_pat.bv_len != 0 ) {
892 "%s: line %d: set attribute already specified.\n",
897 if ( right == NULL || *right == '\0' ) {
899 "%s: line %d: no set is defined\n",
904 b->a_set_style = sty;
905 ber_str2bv( right, 0, 1, &b->a_set_pat );
910 #ifdef SLAPD_ACI_ENABLED
911 if ( strcasecmp( left, "aci" ) == 0 ) {
912 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
914 "%s: line %d: inappropriate style \"%s\" in by clause\n",
915 fname, lineno, style );
919 if( b->a_aci_at != NULL ) {
921 "%s: line %d: aci attribute already specified.\n",
926 if ( right != NULL && *right != '\0' ) {
927 rc = slap_str2ad( right, &b->a_aci_at, &text );
929 if( rc != LDAP_SUCCESS ) {
931 "%s: line %d: aci \"%s\": %s\n",
932 fname, lineno, right, text );
937 b->a_aci_at = slap_schema.si_ad_aci;
940 if( !is_at_syntax( b->a_aci_at->ad_type,
944 "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
945 fname, lineno, right,
946 b->a_aci_at->ad_type->sat_syntax_oid );
952 #endif /* SLAPD_ACI_ENABLED */
954 if ( strcasecmp( left, "ssf" ) == 0 ) {
955 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
957 "%s: line %d: inappropriate style \"%s\" in by clause\n",
958 fname, lineno, style );
962 if( b->a_authz.sai_ssf ) {
964 "%s: line %d: ssf attribute already specified.\n",
969 if ( right == NULL || *right == '\0' ) {
971 "%s: line %d: no ssf is defined\n",
976 b->a_authz.sai_ssf = atoi( right );
978 if( !b->a_authz.sai_ssf ) {
980 "%s: line %d: invalid ssf value (%s)\n",
981 fname, lineno, right );
987 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
988 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
990 "%s: line %d: inappropriate style \"%s\" in by clause\n",
991 fname, lineno, style );
995 if( b->a_authz.sai_transport_ssf ) {
997 "%s: line %d: transport_ssf attribute already specified.\n",
1002 if ( right == NULL || *right == '\0' ) {
1004 "%s: line %d: no transport_ssf is defined\n",
1009 b->a_authz.sai_transport_ssf = atoi( right );
1011 if( !b->a_authz.sai_transport_ssf ) {
1013 "%s: line %d: invalid transport_ssf value (%s)\n",
1014 fname, lineno, right );
1020 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
1021 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
1023 "%s: line %d: inappropriate style \"%s\" in by clause\n",
1024 fname, lineno, style );
1028 if( b->a_authz.sai_tls_ssf ) {
1030 "%s: line %d: tls_ssf attribute already specified.\n",
1035 if ( right == NULL || *right == '\0' ) {
1037 "%s: line %d: no tls_ssf is defined\n",
1042 b->a_authz.sai_tls_ssf = atoi( right );
1044 if( !b->a_authz.sai_tls_ssf ) {
1046 "%s: line %d: invalid tls_ssf value (%s)\n",
1047 fname, lineno, right );
1053 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
1054 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
1056 "%s: line %d: inappropriate style \"%s\" in by clause\n",
1057 fname, lineno, style );
1061 if( b->a_authz.sai_sasl_ssf ) {
1063 "%s: line %d: sasl_ssf attribute already specified.\n",
1068 if ( right == NULL || *right == '\0' ) {
1070 "%s: line %d: no sasl_ssf is defined\n",
1075 b->a_authz.sai_sasl_ssf = atoi( right );
1077 if( !b->a_authz.sai_sasl_ssf ) {
1079 "%s: line %d: invalid sasl_ssf value (%s)\n",
1080 fname, lineno, right );
1086 if( right != NULL ) {
1093 if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) {
1094 /* out of arguments or plain stop */
1096 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1097 b->a_type = ACL_STOP;
1099 access_append( &a->acl_access, b );
1103 if( strcasecmp( left, "continue" ) == 0 ) {
1104 /* plain continue */
1106 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1107 b->a_type = ACL_CONTINUE;
1109 access_append( &a->acl_access, b );
1113 if( strcasecmp( left, "break" ) == 0 ) {
1114 /* plain continue */
1116 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1117 b->a_type = ACL_BREAK;
1119 access_append( &a->acl_access, b );
1123 if ( strcasecmp( left, "by" ) == 0 ) {
1124 /* we've gone too far */
1126 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1127 b->a_type = ACL_STOP;
1129 access_append( &a->acl_access, b );
1134 if( strncasecmp( left, "self", 4 ) == 0 ) {
1136 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[4] ) );
1139 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
1142 if( ACL_IS_INVALID( b->a_access_mask ) ) {
1144 "%s: line %d: expecting <access> got \"%s\"\n",
1145 fname, lineno, left );
1149 b->a_type = ACL_STOP;
1152 /* out of arguments or plain stop */
1153 access_append( &a->acl_access, b );
1157 if( strcasecmp( argv[i], "continue" ) == 0 ) {
1158 /* plain continue */
1159 b->a_type = ACL_CONTINUE;
1161 } else if( strcasecmp( argv[i], "break" ) == 0 ) {
1162 /* plain continue */
1163 b->a_type = ACL_BREAK;
1165 } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
1170 access_append( &a->acl_access, b );
1174 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
1175 fname, lineno, argv[i] );
1180 /* if we have no real access clause, complain and do nothing */
1183 "%s: line %d: warning: no access clause(s) specified in access line\n",
1188 if (ldap_debug & LDAP_DEBUG_ACL)
1192 if ( a->acl_access == NULL ) {
1194 "%s: line %d: warning: no by clause(s) specified in access line\n",
1199 acl_append( &be->be_acl, a );
1201 acl_append( &global_acl, a );
1207 accessmask2str( slap_mask_t mask, char *buf )
1212 assert( buf != NULL );
1214 if ( ACL_IS_INVALID( mask ) ) {
1220 if ( ACL_IS_LEVEL( mask ) ) {
1221 if ( ACL_LVL_IS_NONE(mask) ) {
1222 ptr = lutil_strcopy( ptr, "none" );
1224 } else if ( ACL_LVL_IS_AUTH(mask) ) {
1225 ptr = lutil_strcopy( ptr, "auth" );
1227 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
1228 ptr = lutil_strcopy( ptr, "compare" );
1230 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
1231 ptr = lutil_strcopy( ptr, "search" );
1233 } else if ( ACL_LVL_IS_READ(mask) ) {
1234 ptr = lutil_strcopy( ptr, "read" );
1236 } else if ( ACL_LVL_IS_WRITE(mask) ) {
1237 ptr = lutil_strcopy( ptr, "write" );
1239 ptr = lutil_strcopy( ptr, "unknown" );
1245 if( ACL_IS_ADDITIVE( mask ) ) {
1248 } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
1255 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
1260 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
1265 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
1270 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
1275 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
1280 if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
1289 if ( ACL_IS_LEVEL( mask ) ) {
1299 str2accessmask( const char *str )
1303 if( !ASCII_ALPHA(str[0]) ) {
1306 if ( str[0] == '=' ) {
1309 } else if( str[0] == '+' ) {
1310 ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1312 } else if( str[0] == '-' ) {
1313 ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1316 ACL_INVALIDATE(mask);
1320 for( i=1; str[i] != '\0'; i++ ) {
1321 if( TOLOWER((unsigned char) str[i]) == 'w' ) {
1322 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1324 } else if( TOLOWER((unsigned char) str[i]) == 'r' ) {
1325 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1327 } else if( TOLOWER((unsigned char) str[i]) == 's' ) {
1328 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1330 } else if( TOLOWER((unsigned char) str[i]) == 'c' ) {
1331 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1333 } else if( TOLOWER((unsigned char) str[i]) == 'x' ) {
1334 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1336 } else if( str[i] != '0' ) {
1337 ACL_INVALIDATE(mask);
1345 if ( strcasecmp( str, "none" ) == 0 ) {
1346 ACL_LVL_ASSIGN_NONE(mask);
1348 } else if ( strcasecmp( str, "auth" ) == 0 ) {
1349 ACL_LVL_ASSIGN_AUTH(mask);
1351 } else if ( strcasecmp( str, "compare" ) == 0 ) {
1352 ACL_LVL_ASSIGN_COMPARE(mask);
1354 } else if ( strcasecmp( str, "search" ) == 0 ) {
1355 ACL_LVL_ASSIGN_SEARCH(mask);
1357 } else if ( strcasecmp( str, "read" ) == 0 ) {
1358 ACL_LVL_ASSIGN_READ(mask);
1360 } else if ( strcasecmp( str, "write" ) == 0 ) {
1361 ACL_LVL_ASSIGN_WRITE(mask);
1364 ACL_INVALIDATE( mask );
1373 fprintf( stderr, "%s%s\n",
1374 "<access clause> ::= access to <what> "
1375 "[ by <who> <access> [ <control> ] ]+ \n"
1376 "<what> ::= * | [dn[.<dnstyle>]=<DN>] [filter=<filter>] [attrs=<attrlist>]\n"
1377 "<attrlist> ::= <attr> [val[.<style>]=<value>] | <attr> , <attrlist>\n"
1378 "<attr> ::= <attrname> | entry | children\n"
1379 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<DN> ]\n"
1380 "\t[dnattr=<attrname>]\n"
1381 "\t[group[/<objectclass>[/<attrname>]][.<style>]=<group>]\n"
1382 "\t[peername[.<style>]=<peer>] [sockname[.<style>]=<name>]\n",
1383 "\t[domain[.<style>]=<domain>] [sockurl[.<style>]=<url>]\n"
1384 #ifdef SLAPD_ACI_ENABLED
1385 "\t[aci=<attrname>]\n"
1387 "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
1388 "<dnstyle> ::= base | exact | one | subtree | children | regex\n"
1389 "<style> ::= regex | base | exact\n"
1390 "<access> ::= [self]{<level>|<priv>}\n"
1391 "<level> ::= none | auth | compare | search | read | write\n"
1392 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
1393 "<control> ::= [ stop | continue | break ]\n"
1395 exit( EXIT_FAILURE );
1399 * Set pattern to a "normalized" DN from src.
1400 * At present it simply eats the (optional) space after
1401 * a RDN separator (,)
1402 * Eventually will evolve in a more complete normalization
1405 acl_regex_normalized_dn(
1407 struct berval *pattern
1413 str = ch_strdup( src );
1414 len = strlen( src );
1416 for ( p = str; p && p[ 0 ]; p++ ) {
1418 if ( p[ 0 ] == '\\' && p[ 1 ] ) {
1420 * if escaping a hex pair we should
1421 * increment p twice; however, in that
1422 * case the second hex number does
1428 if ( p[ 0 ] == ',' ) {
1429 if ( p[ 1 ] == ' ' ) {
1433 * too much space should be
1434 * an error if we are pedantic
1436 for ( q = &p[ 2 ]; q[ 0 ] == ' '; q++ ) {
1439 AC_MEMCPY( p+1, q, len-(q-str)+1);
1443 pattern->bv_val = str;
1444 pattern->bv_len = p-str;
1458 if ( (*right = strchr( line, splitchar )) != NULL ) {
1459 *((*right)++) = '\0';
1464 access_append( Access **l, Access *a )
1466 for ( ; *l != NULL; l = &(*l)->a_next )
1473 acl_append( AccessControl **l, AccessControl *a )
1475 for ( ; *l != NULL; l = &(*l)->acl_next )
1482 access_free( Access *a )
1484 if ( a->a_dn_pat.bv_val )
1485 free ( a->a_dn_pat.bv_val );
1486 if ( a->a_peername_pat.bv_val )
1487 free ( a->a_peername_pat.bv_val );
1488 if ( a->a_sockname_pat.bv_val )
1489 free ( a->a_sockname_pat.bv_val );
1490 if ( a->a_domain_pat.bv_val )
1491 free ( a->a_domain_pat.bv_val );
1492 if ( a->a_sockurl_pat.bv_val )
1493 free ( a->a_sockurl_pat.bv_val );
1494 if ( a->a_set_pat.bv_len )
1495 free ( a->a_set_pat.bv_val );
1496 if ( a->a_group_pat.bv_len )
1497 free ( a->a_group_pat.bv_val );
1502 acl_free( AccessControl *a )
1507 if ( a->acl_filter )
1508 filter_free( a->acl_filter );
1509 if ( a->acl_dn_pat.bv_len )
1510 free ( a->acl_dn_pat.bv_val );
1511 if ( a->acl_attrs ) {
1512 for ( an = a->acl_attrs; an->an_name.bv_val; an++ ) {
1513 free( an->an_name.bv_val );
1515 free( a->acl_attrs );
1517 for (; a->acl_access; a->acl_access = n) {
1518 n = a->acl_access->a_next;
1519 access_free( a->acl_access );
1524 /* Because backend_startup uses acl_append to tack on the global_acl to
1525 * the end of each backend's acl, we cannot just take one argument and
1526 * merrily free our way to the end of the list. backend_destroy calls us
1527 * with the be_acl in arg1, and global_acl in arg2 to give us a stopping
1528 * point. config_destroy calls us with global_acl in arg1 and NULL in
1529 * arg2, so we then proceed to polish off the global_acl.
1532 acl_destroy( AccessControl *a, AccessControl *end )
1536 for (; a && a!= end; a=n) {
1543 access2str( slap_access_t access )
1545 if ( access == ACL_NONE ) {
1548 } else if ( access == ACL_AUTH ) {
1551 } else if ( access == ACL_COMPARE ) {
1554 } else if ( access == ACL_SEARCH ) {
1557 } else if ( access == ACL_READ ) {
1560 } else if ( access == ACL_WRITE ) {
1568 str2access( const char *str )
1570 if ( strcasecmp( str, "none" ) == 0 ) {
1573 } else if ( strcasecmp( str, "auth" ) == 0 ) {
1576 } else if ( strcasecmp( str, "compare" ) == 0 ) {
1579 } else if ( strcasecmp( str, "search" ) == 0 ) {
1582 } else if ( strcasecmp( str, "read" ) == 0 ) {
1585 } else if ( strcasecmp( str, "write" ) == 0 ) {
1589 return( ACL_INVALID_ACCESS );
1594 static char *style_strings[5] = { "regex",
1595 "base", "one", "subtree", "children" };
1598 print_access( Access *b )
1600 char maskbuf[ACCESSMASK_MAXLEN];
1602 fprintf( stderr, "\tby" );
1604 if ( b->a_dn_pat.bv_len != 0 ) {
1605 if( strcmp(b->a_dn_pat.bv_val, "*") == 0
1606 || strcmp(b->a_dn_pat.bv_val, "users") == 0
1607 || strcmp(b->a_dn_pat.bv_val, "anonymous") == 0
1608 || strcmp(b->a_dn_pat.bv_val, "self") == 0 )
1610 fprintf( stderr, " %s", b->a_dn_pat.bv_val );
1613 fprintf( stderr, " dn.%s=\"%s\"",
1614 style_strings[b->a_dn_style], b->a_dn_pat.bv_val );
1618 if ( b->a_dn_at != NULL ) {
1619 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname.bv_val );
1622 if ( b->a_group_pat.bv_len ) {
1623 fprintf( stderr, " group/%s/%s.%s=\"%s\"",
1624 b->a_group_oc ? b->a_group_oc->soc_cname.bv_val : "groupOfNames",
1625 b->a_group_at ? b->a_group_at->ad_cname.bv_val : "member",
1626 style_strings[b->a_group_style],
1627 b->a_group_pat.bv_val );
1630 if ( b->a_peername_pat.bv_len != 0 ) {
1631 fprintf( stderr, " peername=\"%s\"", b->a_peername_pat.bv_val );
1634 if ( b->a_sockname_pat.bv_len != 0 ) {
1635 fprintf( stderr, " sockname=\"%s\"", b->a_sockname_pat.bv_val );
1638 if ( b->a_domain_pat.bv_len != 0 ) {
1639 fprintf( stderr, " domain=%s", b->a_domain_pat.bv_val );
1642 if ( b->a_sockurl_pat.bv_len != 0 ) {
1643 fprintf( stderr, " sockurl=\"%s\"", b->a_sockurl_pat.bv_val );
1646 #ifdef SLAPD_ACI_ENABLED
1647 if ( b->a_aci_at != NULL ) {
1648 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname.bv_val );
1652 /* Security Strength Factors */
1653 if ( b->a_authz.sai_ssf ) {
1654 fprintf( stderr, " ssf=%u",
1655 b->a_authz.sai_ssf );
1657 if ( b->a_authz.sai_transport_ssf ) {
1658 fprintf( stderr, " transport_ssf=%u",
1659 b->a_authz.sai_transport_ssf );
1661 if ( b->a_authz.sai_tls_ssf ) {
1662 fprintf( stderr, " tls_ssf=%u",
1663 b->a_authz.sai_tls_ssf );
1665 if ( b->a_authz.sai_sasl_ssf ) {
1666 fprintf( stderr, " sasl_ssf=%u",
1667 b->a_authz.sai_sasl_ssf );
1670 fprintf( stderr, " %s%s",
1671 b->a_dn_self ? "self" : "",
1672 accessmask2str( b->a_access_mask, maskbuf ) );
1674 if( b->a_type == ACL_BREAK ) {
1675 fprintf( stderr, " break" );
1677 } else if( b->a_type == ACL_CONTINUE ) {
1678 fprintf( stderr, " continue" );
1680 } else if( b->a_type != ACL_STOP ) {
1681 fprintf( stderr, " unknown-control" );
1684 fprintf( stderr, "\n" );
1689 print_acl( Backend *be, AccessControl *a )
1694 fprintf( stderr, "%s ACL: access to",
1695 be == NULL ? "Global" : "Backend" );
1697 if ( a->acl_dn_pat.bv_len != 0 ) {
1699 fprintf( stderr, " dn.%s=\"%s\"\n",
1700 style_strings[a->acl_dn_style], a->acl_dn_pat.bv_val );
1703 if ( a->acl_filter != NULL ) {
1704 struct berval bv = { 0, NULL };
1706 filter2bv( a->acl_filter, &bv );
1707 fprintf( stderr, " filter=%s\n", bv.bv_val );
1708 ch_free( bv.bv_val );
1711 if ( a->acl_attrs != NULL ) {
1716 fprintf( stderr, " attrs=" );
1717 for ( an = a->acl_attrs; an && an->an_name.bv_val; an++ ) {
1719 fprintf( stderr, "," );
1722 fputc( an->an_oc_exclude ? '!' : '@', stderr);
1724 fputs( an->an_name.bv_val, stderr );
1727 fprintf( stderr, "\n" );
1730 if ( a->acl_attrval.bv_len != 0 ) {
1732 fprintf( stderr, " val.%s=\"%s\"\n",
1733 style_strings[a->acl_attrval_style], a->acl_attrval.bv_val );
1738 fprintf( stderr, " *\n" );
1741 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1745 fprintf( stderr, "\n" );
1748 #endif /* LDAP_DEBUG */