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 a->acl_attrval_style = ACL_STYLE_BASE;
287 "%s: line %d: expecting <what> got \"%s\"\n",
288 fname, lineno, left );
293 if ( a->acl_dn_pat.bv_len != 0 &&
294 strcmp(a->acl_dn_pat.bv_val, "*") == 0 )
296 free( a->acl_dn_pat.bv_val );
297 a->acl_dn_pat.bv_val = NULL;
298 a->acl_dn_pat.bv_len = 0;
301 if( a->acl_dn_pat.bv_len != 0 ||
302 ( a->acl_dn_style != ACL_STYLE_REGEX ) )
304 if ( a->acl_dn_style != ACL_STYLE_REGEX ) {
306 rc = dnNormalize( 0, NULL, NULL, &a->acl_dn_pat, &bv, NULL);
307 if ( rc != LDAP_SUCCESS ) {
309 "%s: line %d: bad DN \"%s\" in to DN clause\n",
310 fname, lineno, a->acl_dn_pat.bv_val );
313 free( a->acl_dn_pat.bv_val );
316 int e = regcomp( &a->acl_dn_re, a->acl_dn_pat.bv_val,
317 REG_EXTENDED | REG_ICASE );
320 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
321 fprintf( stderr, "%s: line %d: "
322 "regular expression \"%s\" bad because of %s\n",
323 fname, lineno, right, buf );
329 /* by clause - select who has what access to entries */
330 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
332 fprintf( stderr, "%s: line %d: "
333 "to clause required before by clause in access line\n",
339 * by clause consists of <who> and <access>
342 b = (Access *) ch_calloc( 1, sizeof(Access) );
344 ACL_INVALIDATE( b->a_access_mask );
348 "%s: line %d: premature eol: expecting <who>\n",
354 for ( ; i < argc; i++ ) {
355 slap_style_t sty = ACL_STYLE_REGEX;
356 char *style_modifier = NULL;
359 split( argv[i], '=', &left, &right );
360 split( left, '.', &left, &style );
362 split( style, ',', &style, &style_modifier);
365 if ( style == NULL || *style == '\0' ||
366 strcasecmp( style, "exact" ) == 0 ||
367 strcasecmp( style, "base" ) == 0 )
369 sty = ACL_STYLE_BASE;
371 } else if ( strcasecmp( style, "one" ) == 0 ) {
374 } else if ( strcasecmp( style, "subtree" ) == 0 ||
375 strcasecmp( style, "sub" ) == 0 )
377 sty = ACL_STYLE_SUBTREE;
379 } else if ( strcasecmp( style, "children" ) == 0 ) {
380 sty = ACL_STYLE_CHILDREN;
382 } else if ( strcasecmp( style, "regex" ) == 0 ) {
383 sty = ACL_STYLE_REGEX;
387 "%s: line %d: unknown style \"%s\" in by clause\n",
388 fname, lineno, style );
392 if ( style_modifier &&
393 strcasecmp( style_modifier, "expand" ) == 0 )
398 if ( strcasecmp( argv[i], "*" ) == 0 ) {
399 bv.bv_val = ch_strdup( "*" );
401 sty = ACL_STYLE_REGEX;
403 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
404 ber_str2bv("anonymous", sizeof("anonymous")-1, 1, &bv);
405 sty = ACL_STYLE_REGEX;
407 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
408 ber_str2bv("self", sizeof("self")-1, 1, &bv);
409 sty = ACL_STYLE_REGEX;
411 } else if ( strcasecmp( argv[i], "users" ) == 0 ) {
412 ber_str2bv("users", sizeof("users")-1, 1, &bv);
413 sty = ACL_STYLE_REGEX;
415 } else if ( strcasecmp( left, "dn" ) == 0 ) {
416 if ( sty == ACL_STYLE_REGEX ) {
417 b->a_dn_style = ACL_STYLE_REGEX;
418 if( right == NULL ) {
423 } else if (*right == '\0' ) {
425 ber_str2bv("anonymous",
426 sizeof("anonymous")-1,
428 } else if ( strcmp( right, "*" ) == 0 ) {
430 /* any or users? users for now */
434 } else if ( strcmp( right, ".+" ) == 0
435 || strcmp( right, "^.+" ) == 0
436 || strcmp( right, ".+$" ) == 0
437 || strcmp( right, "^.+$" ) == 0
438 || strcmp( right, ".+$$" ) == 0
439 || strcmp( right, "^.+$$" ) == 0 )
444 } else if ( strcmp( right, ".*" ) == 0
445 || strcmp( right, "^.*" ) == 0
446 || strcmp( right, ".*$" ) == 0
447 || strcmp( right, "^.*$" ) == 0
448 || strcmp( right, ".*$$" ) == 0
449 || strcmp( right, "^.*$$" ) == 0 )
456 acl_regex_normalized_dn( right, &bv );
457 if ( !ber_bvccmp( &bv, '*' ) ) {
458 regtest(fname, lineno, bv.bv_val);
461 } else if ( right == NULL || *right == '\0' ) {
462 fprintf( stderr, "%s: line %d: "
463 "missing \"=\" in (or value after) \"%s\" "
465 fname, lineno, left );
469 ber_str2bv( right, 0, 1, &bv );
476 if( bv.bv_val != NULL ) {
477 if( b->a_dn_pat.bv_len != 0 ) {
479 "%s: line %d: dn pattern already specified.\n",
484 if ( sty != ACL_STYLE_REGEX && expand == 0 ) {
485 rc = dnNormalize(0, NULL, NULL,
486 &bv, &b->a_dn_pat, NULL);
487 if ( rc != LDAP_SUCCESS ) {
489 "%s: line %d: bad DN \"%s\" in by DN clause\n",
490 fname, lineno, bv.bv_val );
498 b->a_dn_expand = expand;
502 if ( strcasecmp( left, "dnattr" ) == 0 ) {
503 if ( right == NULL || right[ 0 ] == '\0' ) {
505 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
506 fname, lineno, left );
510 if( b->a_dn_at != NULL ) {
512 "%s: line %d: dnattr already specified.\n",
517 rc = slap_str2ad( right, &b->a_dn_at, &text );
519 if( rc != LDAP_SUCCESS ) {
521 "%s: line %d: dnattr \"%s\": %s\n",
522 fname, lineno, right, text );
527 if( !is_at_syntax( b->a_dn_at->ad_type,
529 !is_at_syntax( b->a_dn_at->ad_type,
530 SLAPD_NAMEUID_SYNTAX ))
533 "%s: line %d: dnattr \"%s\": "
534 "inappropriate syntax: %s\n",
535 fname, lineno, right,
536 b->a_dn_at->ad_type->sat_syntax_oid );
540 if( b->a_dn_at->ad_type->sat_equality == NULL ) {
542 "%s: line %d: dnattr \"%s\": "
543 "inappropriate matching (no EQUALITY)\n",
544 fname, lineno, right );
551 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
555 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
556 fprintf( stderr, "%s: line %d: "
557 "inappropriate style \"%s\" in by clause\n",
558 fname, lineno, style );
562 if ( right == NULL || right[ 0 ] == '\0' ) {
563 fprintf( stderr, "%s: line %d: "
564 "missing \"=\" in (or value after) \"%s\" "
566 fname, lineno, left );
570 if( b->a_group_pat.bv_len ) {
572 "%s: line %d: group pattern already specified.\n",
577 /* format of string is
578 "group/objectClassValue/groupAttrName" */
579 if ((value = strchr(left, '/')) != NULL) {
581 if (*value && (name = strchr(value, '/')) != NULL) {
586 b->a_group_style = sty;
587 if (sty == ACL_STYLE_REGEX) {
588 acl_regex_normalized_dn( right, &bv );
589 if ( !ber_bvccmp( &bv, '*' ) ) {
590 regtest(fname, lineno, bv.bv_val);
594 ber_str2bv( right, 0, 0, &bv );
595 rc = dnNormalize( 0, NULL, NULL, &bv,
596 &b->a_group_pat, NULL );
597 if ( rc != LDAP_SUCCESS ) {
599 "%s: line %d: bad DN \"%s\"\n",
600 fname, lineno, right );
605 if (value && *value) {
606 b->a_group_oc = oc_find( value );
609 if( b->a_group_oc == NULL ) {
611 "%s: line %d: group objectclass "
613 fname, lineno, value );
617 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
619 if( b->a_group_oc == NULL ) {
621 "%s: line %d: group default objectclass "
623 fname, lineno, SLAPD_GROUP_CLASS );
628 if( is_object_subclass( slap_schema.si_oc_referral,
632 "%s: line %d: group objectclass \"%s\" "
633 "is subclass of referral\n",
634 fname, lineno, value );
638 if( is_object_subclass( slap_schema.si_oc_alias,
642 "%s: line %d: group objectclass \"%s\" "
643 "is subclass of alias\n",
644 fname, lineno, value );
649 rc = slap_str2ad( name, &b->a_group_at, &text );
651 if( rc != LDAP_SUCCESS ) {
653 "%s: line %d: group \"%s\": %s\n",
654 fname, lineno, right, text );
659 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
661 if( rc != LDAP_SUCCESS ) {
663 "%s: line %d: group \"%s\": %s\n",
664 fname, lineno, SLAPD_GROUP_ATTR, text );
669 if( !is_at_syntax( b->a_group_at->ad_type,
671 !is_at_syntax( b->a_group_at->ad_type,
672 SLAPD_NAMEUID_SYNTAX ) &&
673 !is_at_subtype( b->a_group_at->ad_type, slap_schema.si_ad_labeledURI->ad_type ))
676 "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
677 fname, lineno, right,
678 b->a_group_at->ad_type->sat_syntax_oid );
685 struct berval vals[2];
687 vals[0].bv_val = b->a_group_oc->soc_oid;
688 vals[0].bv_len = strlen(vals[0].bv_val);
689 vals[1].bv_val = NULL;
692 rc = oc_check_allowed( b->a_group_at->ad_type,
696 fprintf( stderr, "%s: line %d: "
697 "group: \"%s\" not allowed by \"%s\"\n",
699 b->a_group_at->ad_cname.bv_val,
700 b->a_group_oc->soc_oid );
707 if ( strcasecmp( left, "peername" ) == 0 ) {
708 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
709 fprintf( stderr, "%s: line %d: "
710 "inappropriate style \"%s\" in by clause\n",
711 fname, lineno, style );
715 if ( right == NULL || right[ 0 ] == '\0' ) {
716 fprintf( stderr, "%s: line %d: "
717 "missing \"=\" in (or value after) \"%s\" "
719 fname, lineno, left );
723 if( b->a_peername_pat.bv_len ) {
724 fprintf( stderr, "%s: line %d: "
725 "peername pattern already specified.\n",
730 b->a_peername_style = sty;
731 if (sty == ACL_STYLE_REGEX) {
732 acl_regex_normalized_dn( right, &bv );
733 if ( !ber_bvccmp( &bv, '*' ) ) {
734 regtest(fname, lineno, bv.bv_val);
736 b->a_peername_pat = bv;
738 ber_str2bv( right, 0, 1, &b->a_peername_pat );
743 if ( strcasecmp( left, "sockname" ) == 0 ) {
744 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
745 fprintf( stderr, "%s: line %d: "
746 "inappropriate style \"%s\" in by clause\n",
747 fname, lineno, style );
751 if ( right == NULL || right[ 0 ] == '\0' ) {
752 fprintf( stderr, "%s: line %d: "
753 "missing \"=\" in (or value after) \"%s\" "
755 fname, lineno, left );
759 if( b->a_sockname_pat.bv_len ) {
760 fprintf( stderr, "%s: line %d: "
761 "sockname pattern already specified.\n",
766 b->a_sockname_style = sty;
767 if (sty == ACL_STYLE_REGEX) {
768 acl_regex_normalized_dn( right, &bv );
769 if ( !ber_bvccmp( &bv, '*' ) ) {
770 regtest(fname, lineno, bv.bv_val);
772 b->a_sockname_pat = bv;
774 ber_str2bv( right, 0, 1, &b->a_sockname_pat );
779 if ( strcasecmp( left, "domain" ) == 0 ) {
781 case ACL_STYLE_REGEX:
783 case ACL_STYLE_SUBTREE:
788 "%s: line %d: inappropriate style \"%s\" in by clause\n",
789 fname, lineno, style );
793 if ( right == NULL || right[ 0 ] == '\0' ) {
795 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
796 fname, lineno, left );
800 if( b->a_domain_pat.bv_len ) {
802 "%s: line %d: domain pattern already specified.\n",
807 b->a_domain_style = sty;
808 b->a_domain_expand = expand;
809 if (sty == ACL_STYLE_REGEX) {
810 acl_regex_normalized_dn( right, &bv );
811 if ( !ber_bvccmp( &bv, '*' ) ) {
812 regtest(fname, lineno, bv.bv_val);
814 b->a_domain_pat = bv;
816 ber_str2bv( right, 0, 1, &b->a_domain_pat );
821 if ( strcasecmp( left, "sockurl" ) == 0 ) {
822 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
824 "%s: line %d: inappropriate style \"%s\" in by clause\n",
825 fname, lineno, style );
829 if ( right == NULL || right[ 0 ] == '\0' ) {
831 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
832 fname, lineno, left );
836 if( b->a_sockurl_pat.bv_len ) {
838 "%s: line %d: sockurl pattern already specified.\n",
843 b->a_sockurl_style = sty;
844 if (sty == ACL_STYLE_REGEX) {
845 acl_regex_normalized_dn( right, &bv );
846 if ( !ber_bvccmp( &bv, '*' ) ) {
847 regtest(fname, lineno, bv.bv_val);
849 b->a_sockurl_pat = bv;
851 ber_str2bv( right, 0, 1, &b->a_sockurl_pat );
856 if ( strcasecmp( left, "set" ) == 0 ) {
857 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
859 "%s: line %d: inappropriate style \"%s\" in by clause\n",
860 fname, lineno, style );
864 if( b->a_set_pat.bv_len != 0 ) {
866 "%s: line %d: set attribute already specified.\n",
871 if ( right == NULL || *right == '\0' ) {
873 "%s: line %d: no set is defined\n",
878 b->a_set_style = sty;
879 ber_str2bv( right, 0, 1, &b->a_set_pat );
884 #ifdef SLAPD_ACI_ENABLED
885 if ( strcasecmp( left, "aci" ) == 0 ) {
886 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
888 "%s: line %d: inappropriate style \"%s\" in by clause\n",
889 fname, lineno, style );
893 if( b->a_aci_at != NULL ) {
895 "%s: line %d: aci attribute already specified.\n",
900 if ( right != NULL && *right != '\0' ) {
901 rc = slap_str2ad( right, &b->a_aci_at, &text );
903 if( rc != LDAP_SUCCESS ) {
905 "%s: line %d: aci \"%s\": %s\n",
906 fname, lineno, right, text );
911 b->a_aci_at = slap_schema.si_ad_aci;
914 if( !is_at_syntax( b->a_aci_at->ad_type,
918 "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
919 fname, lineno, right,
920 b->a_aci_at->ad_type->sat_syntax_oid );
926 #endif /* SLAPD_ACI_ENABLED */
928 if ( strcasecmp( left, "ssf" ) == 0 ) {
929 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
931 "%s: line %d: inappropriate style \"%s\" in by clause\n",
932 fname, lineno, style );
936 if( b->a_authz.sai_ssf ) {
938 "%s: line %d: ssf attribute already specified.\n",
943 if ( right == NULL || *right == '\0' ) {
945 "%s: line %d: no ssf is defined\n",
950 b->a_authz.sai_ssf = atoi( right );
952 if( !b->a_authz.sai_ssf ) {
954 "%s: line %d: invalid ssf value (%s)\n",
955 fname, lineno, right );
961 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
962 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
964 "%s: line %d: inappropriate style \"%s\" in by clause\n",
965 fname, lineno, style );
969 if( b->a_authz.sai_transport_ssf ) {
971 "%s: line %d: transport_ssf attribute already specified.\n",
976 if ( right == NULL || *right == '\0' ) {
978 "%s: line %d: no transport_ssf is defined\n",
983 b->a_authz.sai_transport_ssf = atoi( right );
985 if( !b->a_authz.sai_transport_ssf ) {
987 "%s: line %d: invalid transport_ssf value (%s)\n",
988 fname, lineno, right );
994 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
995 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
997 "%s: line %d: inappropriate style \"%s\" in by clause\n",
998 fname, lineno, style );
1002 if( b->a_authz.sai_tls_ssf ) {
1004 "%s: line %d: tls_ssf attribute already specified.\n",
1009 if ( right == NULL || *right == '\0' ) {
1011 "%s: line %d: no tls_ssf is defined\n",
1016 b->a_authz.sai_tls_ssf = atoi( right );
1018 if( !b->a_authz.sai_tls_ssf ) {
1020 "%s: line %d: invalid tls_ssf value (%s)\n",
1021 fname, lineno, right );
1027 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
1028 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
1030 "%s: line %d: inappropriate style \"%s\" in by clause\n",
1031 fname, lineno, style );
1035 if( b->a_authz.sai_sasl_ssf ) {
1037 "%s: line %d: sasl_ssf attribute already specified.\n",
1042 if ( right == NULL || *right == '\0' ) {
1044 "%s: line %d: no sasl_ssf is defined\n",
1049 b->a_authz.sai_sasl_ssf = atoi( right );
1051 if( !b->a_authz.sai_sasl_ssf ) {
1053 "%s: line %d: invalid sasl_ssf value (%s)\n",
1054 fname, lineno, right );
1060 if( right != NULL ) {
1067 if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) {
1068 /* out of arguments or plain stop */
1070 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1071 b->a_type = ACL_STOP;
1073 access_append( &a->acl_access, b );
1077 if( strcasecmp( left, "continue" ) == 0 ) {
1078 /* plain continue */
1080 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1081 b->a_type = ACL_CONTINUE;
1083 access_append( &a->acl_access, b );
1087 if( strcasecmp( left, "break" ) == 0 ) {
1088 /* plain continue */
1090 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1091 b->a_type = ACL_BREAK;
1093 access_append( &a->acl_access, b );
1097 if ( strcasecmp( left, "by" ) == 0 ) {
1098 /* we've gone too far */
1100 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1101 b->a_type = ACL_STOP;
1103 access_append( &a->acl_access, b );
1108 if( strncasecmp( left, "self", 4 ) == 0 ) {
1110 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[4] ) );
1113 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
1116 if( ACL_IS_INVALID( b->a_access_mask ) ) {
1118 "%s: line %d: expecting <access> got \"%s\"\n",
1119 fname, lineno, left );
1123 b->a_type = ACL_STOP;
1126 /* out of arguments or plain stop */
1127 access_append( &a->acl_access, b );
1131 if( strcasecmp( argv[i], "continue" ) == 0 ) {
1132 /* plain continue */
1133 b->a_type = ACL_CONTINUE;
1135 } else if( strcasecmp( argv[i], "break" ) == 0 ) {
1136 /* plain continue */
1137 b->a_type = ACL_BREAK;
1139 } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
1144 access_append( &a->acl_access, b );
1148 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
1149 fname, lineno, argv[i] );
1154 /* if we have no real access clause, complain and do nothing */
1157 "%s: line %d: warning: no access clause(s) specified in access line\n",
1162 if (ldap_debug & LDAP_DEBUG_ACL)
1166 if ( a->acl_access == NULL ) {
1168 "%s: line %d: warning: no by clause(s) specified in access line\n",
1173 acl_append( &be->be_acl, a );
1175 acl_append( &global_acl, a );
1181 accessmask2str( slap_mask_t mask, char *buf )
1186 assert( buf != NULL );
1188 if ( ACL_IS_INVALID( mask ) ) {
1194 if ( ACL_IS_LEVEL( mask ) ) {
1195 if ( ACL_LVL_IS_NONE(mask) ) {
1196 ptr = lutil_strcopy( ptr, "none" );
1198 } else if ( ACL_LVL_IS_AUTH(mask) ) {
1199 ptr = lutil_strcopy( ptr, "auth" );
1201 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
1202 ptr = lutil_strcopy( ptr, "compare" );
1204 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
1205 ptr = lutil_strcopy( ptr, "search" );
1207 } else if ( ACL_LVL_IS_READ(mask) ) {
1208 ptr = lutil_strcopy( ptr, "read" );
1210 } else if ( ACL_LVL_IS_WRITE(mask) ) {
1211 ptr = lutil_strcopy( ptr, "write" );
1213 ptr = lutil_strcopy( ptr, "unknown" );
1219 if( ACL_IS_ADDITIVE( mask ) ) {
1222 } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
1229 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
1234 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
1239 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
1244 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
1249 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
1254 if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
1263 if ( ACL_IS_LEVEL( mask ) ) {
1273 str2accessmask( const char *str )
1277 if( !ASCII_ALPHA(str[0]) ) {
1280 if ( str[0] == '=' ) {
1283 } else if( str[0] == '+' ) {
1284 ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1286 } else if( str[0] == '-' ) {
1287 ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1290 ACL_INVALIDATE(mask);
1294 for( i=1; str[i] != '\0'; i++ ) {
1295 if( TOLOWER((unsigned char) str[i]) == 'w' ) {
1296 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1298 } else if( TOLOWER((unsigned char) str[i]) == 'r' ) {
1299 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1301 } else if( TOLOWER((unsigned char) str[i]) == 's' ) {
1302 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1304 } else if( TOLOWER((unsigned char) str[i]) == 'c' ) {
1305 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1307 } else if( TOLOWER((unsigned char) str[i]) == 'x' ) {
1308 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1310 } else if( str[i] != '0' ) {
1311 ACL_INVALIDATE(mask);
1319 if ( strcasecmp( str, "none" ) == 0 ) {
1320 ACL_LVL_ASSIGN_NONE(mask);
1322 } else if ( strcasecmp( str, "auth" ) == 0 ) {
1323 ACL_LVL_ASSIGN_AUTH(mask);
1325 } else if ( strcasecmp( str, "compare" ) == 0 ) {
1326 ACL_LVL_ASSIGN_COMPARE(mask);
1328 } else if ( strcasecmp( str, "search" ) == 0 ) {
1329 ACL_LVL_ASSIGN_SEARCH(mask);
1331 } else if ( strcasecmp( str, "read" ) == 0 ) {
1332 ACL_LVL_ASSIGN_READ(mask);
1334 } else if ( strcasecmp( str, "write" ) == 0 ) {
1335 ACL_LVL_ASSIGN_WRITE(mask);
1338 ACL_INVALIDATE( mask );
1347 fprintf( stderr, "%s%s\n",
1348 "<access clause> ::= access to <what> "
1349 "[ by <who> <access> [ <control> ] ]+ \n"
1350 "<what> ::= * | [dn[.<dnstyle>]=<DN>] [filter=<filter>] [attrs=<attrlist>]\n"
1351 "<attrlist> ::= <attr> [val[.<style>]=<value>] | <attr> , <attrlist>\n"
1352 "<attr> ::= <attrname> | entry | children\n"
1353 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<DN> ]\n"
1354 "\t[dnattr=<attrname>]\n"
1355 "\t[group[/<objectclass>[/<attrname>]][.<style>]=<group>]\n"
1356 "\t[peername[.<style>]=<peer>] [sockname[.<style>]=<name>]\n",
1357 "\t[domain[.<style>]=<domain>] [sockurl[.<style>]=<url>]\n"
1358 #ifdef SLAPD_ACI_ENABLED
1359 "\t[aci=<attrname>]\n"
1361 "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
1362 "<dnstyle> ::= base | exact | one | subtree | children | regex\n"
1363 "<style> ::= regex | base | exact\n"
1364 "<access> ::= [self]{<level>|<priv>}\n"
1365 "<level> ::= none | auth | compare | search | read | write\n"
1366 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
1367 "<control> ::= [ stop | continue | break ]\n"
1369 exit( EXIT_FAILURE );
1373 * Set pattern to a "normalized" DN from src.
1374 * At present it simply eats the (optional) space after
1375 * a RDN separator (,)
1376 * Eventually will evolve in a more complete normalization
1379 acl_regex_normalized_dn(
1381 struct berval *pattern
1387 str = ch_strdup( src );
1388 len = strlen( src );
1390 for ( p = str; p && p[ 0 ]; p++ ) {
1392 if ( p[ 0 ] == '\\' && p[ 1 ] ) {
1394 * if escaping a hex pair we should
1395 * increment p twice; however, in that
1396 * case the second hex number does
1402 if ( p[ 0 ] == ',' ) {
1403 if ( p[ 1 ] == ' ' ) {
1407 * too much space should be
1408 * an error if we are pedantic
1410 for ( q = &p[ 2 ]; q[ 0 ] == ' '; q++ ) {
1413 AC_MEMCPY( p+1, q, len-(q-str)+1);
1417 pattern->bv_val = str;
1418 pattern->bv_len = p-str;
1432 if ( (*right = strchr( line, splitchar )) != NULL ) {
1433 *((*right)++) = '\0';
1438 access_append( Access **l, Access *a )
1440 for ( ; *l != NULL; l = &(*l)->a_next )
1447 acl_append( AccessControl **l, AccessControl *a )
1449 for ( ; *l != NULL; l = &(*l)->acl_next )
1456 access_free( Access *a )
1458 if ( a->a_dn_pat.bv_val )
1459 free ( a->a_dn_pat.bv_val );
1460 if ( a->a_peername_pat.bv_val )
1461 free ( a->a_peername_pat.bv_val );
1462 if ( a->a_sockname_pat.bv_val )
1463 free ( a->a_sockname_pat.bv_val );
1464 if ( a->a_domain_pat.bv_val )
1465 free ( a->a_domain_pat.bv_val );
1466 if ( a->a_sockurl_pat.bv_val )
1467 free ( a->a_sockurl_pat.bv_val );
1468 if ( a->a_set_pat.bv_len )
1469 free ( a->a_set_pat.bv_val );
1470 if ( a->a_group_pat.bv_len )
1471 free ( a->a_group_pat.bv_val );
1476 acl_free( AccessControl *a )
1481 if ( a->acl_filter )
1482 filter_free( a->acl_filter );
1483 if ( a->acl_dn_pat.bv_len )
1484 free ( a->acl_dn_pat.bv_val );
1485 if ( a->acl_attrs ) {
1486 for ( an = a->acl_attrs; an->an_name.bv_val; an++ ) {
1487 free( an->an_name.bv_val );
1489 free( a->acl_attrs );
1491 for (; a->acl_access; a->acl_access = n) {
1492 n = a->acl_access->a_next;
1493 access_free( a->acl_access );
1498 /* Because backend_startup uses acl_append to tack on the global_acl to
1499 * the end of each backend's acl, we cannot just take one argument and
1500 * merrily free our way to the end of the list. backend_destroy calls us
1501 * with the be_acl in arg1, and global_acl in arg2 to give us a stopping
1502 * point. config_destroy calls us with global_acl in arg1 and NULL in
1503 * arg2, so we then proceed to polish off the global_acl.
1506 acl_destroy( AccessControl *a, AccessControl *end )
1510 for (; a && a!= end; a=n) {
1517 access2str( slap_access_t access )
1519 if ( access == ACL_NONE ) {
1522 } else if ( access == ACL_AUTH ) {
1525 } else if ( access == ACL_COMPARE ) {
1528 } else if ( access == ACL_SEARCH ) {
1531 } else if ( access == ACL_READ ) {
1534 } else if ( access == ACL_WRITE ) {
1542 str2access( const char *str )
1544 if ( strcasecmp( str, "none" ) == 0 ) {
1547 } else if ( strcasecmp( str, "auth" ) == 0 ) {
1550 } else if ( strcasecmp( str, "compare" ) == 0 ) {
1553 } else if ( strcasecmp( str, "search" ) == 0 ) {
1556 } else if ( strcasecmp( str, "read" ) == 0 ) {
1559 } else if ( strcasecmp( str, "write" ) == 0 ) {
1563 return( ACL_INVALID_ACCESS );
1568 static char *style_strings[5] = { "regex",
1569 "base", "one", "subtree", "children" };
1572 print_access( Access *b )
1574 char maskbuf[ACCESSMASK_MAXLEN];
1576 fprintf( stderr, "\tby" );
1578 if ( b->a_dn_pat.bv_len != 0 ) {
1579 if( strcmp(b->a_dn_pat.bv_val, "*") == 0
1580 || strcmp(b->a_dn_pat.bv_val, "users") == 0
1581 || strcmp(b->a_dn_pat.bv_val, "anonymous") == 0
1582 || strcmp(b->a_dn_pat.bv_val, "self") == 0 )
1584 fprintf( stderr, " %s", b->a_dn_pat.bv_val );
1587 fprintf( stderr, " dn.%s=\"%s\"",
1588 style_strings[b->a_dn_style], b->a_dn_pat.bv_val );
1592 if ( b->a_dn_at != NULL ) {
1593 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname.bv_val );
1596 if ( b->a_group_pat.bv_len ) {
1597 fprintf( stderr, " group/%s/%s.%s=\"%s\"",
1598 b->a_group_oc ? b->a_group_oc->soc_cname.bv_val : "groupOfNames",
1599 b->a_group_at ? b->a_group_at->ad_cname.bv_val : "member",
1600 style_strings[b->a_group_style],
1601 b->a_group_pat.bv_val );
1604 if ( b->a_peername_pat.bv_len != 0 ) {
1605 fprintf( stderr, " peername=\"%s\"", b->a_peername_pat.bv_val );
1608 if ( b->a_sockname_pat.bv_len != 0 ) {
1609 fprintf( stderr, " sockname=\"%s\"", b->a_sockname_pat.bv_val );
1612 if ( b->a_domain_pat.bv_len != 0 ) {
1613 fprintf( stderr, " domain=%s", b->a_domain_pat.bv_val );
1616 if ( b->a_sockurl_pat.bv_len != 0 ) {
1617 fprintf( stderr, " sockurl=\"%s\"", b->a_sockurl_pat.bv_val );
1620 #ifdef SLAPD_ACI_ENABLED
1621 if ( b->a_aci_at != NULL ) {
1622 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname.bv_val );
1626 /* Security Strength Factors */
1627 if ( b->a_authz.sai_ssf ) {
1628 fprintf( stderr, " ssf=%u",
1629 b->a_authz.sai_ssf );
1631 if ( b->a_authz.sai_transport_ssf ) {
1632 fprintf( stderr, " transport_ssf=%u",
1633 b->a_authz.sai_transport_ssf );
1635 if ( b->a_authz.sai_tls_ssf ) {
1636 fprintf( stderr, " tls_ssf=%u",
1637 b->a_authz.sai_tls_ssf );
1639 if ( b->a_authz.sai_sasl_ssf ) {
1640 fprintf( stderr, " sasl_ssf=%u",
1641 b->a_authz.sai_sasl_ssf );
1644 fprintf( stderr, " %s%s",
1645 b->a_dn_self ? "self" : "",
1646 accessmask2str( b->a_access_mask, maskbuf ) );
1648 if( b->a_type == ACL_BREAK ) {
1649 fprintf( stderr, " break" );
1651 } else if( b->a_type == ACL_CONTINUE ) {
1652 fprintf( stderr, " continue" );
1654 } else if( b->a_type != ACL_STOP ) {
1655 fprintf( stderr, " unknown-control" );
1658 fprintf( stderr, "\n" );
1663 print_acl( Backend *be, AccessControl *a )
1668 fprintf( stderr, "%s ACL: access to",
1669 be == NULL ? "Global" : "Backend" );
1671 if ( a->acl_dn_pat.bv_len != 0 ) {
1673 fprintf( stderr, " dn.%s=\"%s\"\n",
1674 style_strings[a->acl_dn_style], a->acl_dn_pat.bv_val );
1677 if ( a->acl_filter != NULL ) {
1678 struct berval bv = { 0, NULL };
1680 filter2bv( a->acl_filter, &bv );
1681 fprintf( stderr, " filter=%s\n", bv.bv_val );
1682 ch_free( bv.bv_val );
1685 if ( a->acl_attrs != NULL ) {
1690 fprintf( stderr, " attrs=" );
1691 for ( an = a->acl_attrs; an && an->an_name.bv_val; an++ ) {
1693 fprintf( stderr, "," );
1695 fputs( an->an_name.bv_val, stderr );
1698 fprintf( stderr, "\n" );
1701 if ( a->acl_attrval.bv_len != 0 ) {
1703 fprintf( stderr, " val.%s=\"%s\"\n",
1704 style_strings[a->acl_attrval_style], a->acl_attrval.bv_val );
1709 fprintf( stderr, " *\n" );
1712 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1716 fprintf( stderr, "\n" );
1719 #endif /* LDAP_DEBUG */