1 /* aclparse.c - routines to parse and check acl's */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
22 static void split(char *line, int splitchar, char **left, char **right);
23 static void access_append(Access **l, Access *a);
24 static void acl_usage(void) LDAP_GCCATTR((noreturn));
26 static void acl_regex_normalized_dn(const char *src, struct berval *pat);
29 static void print_acl(Backend *be, AccessControl *a);
30 static void print_access(Access *b);
34 regtest(const char *fname, int lineno, char *pat) {
50 for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
52 if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
69 if ( size >= (sizeof(buf)-1) ) {
71 "%s: line %d: regular expression \"%s\" too large\n",
76 if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
78 regerror(e, &re, error, sizeof(error));
80 "%s: line %d: regular expression \"%s\" bad because of %s\n",
81 fname, lineno, pat, error );
97 char *left, *right, *style;
105 for ( i = 1; i < argc; i++ ) {
106 /* to clause - select which entries are protected */
107 if ( strcasecmp( argv[i], "to" ) == 0 ) {
109 fprintf( stderr, "%s: line %d: "
110 "only one to clause allowed in access line\n",
114 a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
115 for ( ++i; i < argc; i++ ) {
116 if ( strcasecmp( argv[i], "by" ) == 0 ) {
121 if ( strcasecmp( argv[i], "*" ) == 0 ) {
122 if( a->acl_dn_pat.bv_len ||
123 ( a->acl_dn_style != ACL_STYLE_REGEX ) )
126 "%s: line %d: dn pattern"
127 " already specified in to clause.\n",
132 a->acl_dn_pat.bv_val = ch_strdup( "*" );
133 a->acl_dn_pat.bv_len = 1;
137 split( argv[i], '=', &left, &right );
138 split( left, '.', &left, &style );
140 if ( right == NULL ) {
141 fprintf( stderr, "%s: line %d: "
142 "missing \"=\" in \"%s\" in to clause\n",
143 fname, lineno, left );
147 if ( strcasecmp( left, "dn" ) == 0 ) {
148 if( a->acl_dn_pat.bv_len != 0 ||
149 ( a->acl_dn_style != ACL_STYLE_REGEX ) )
152 "%s: line %d: dn pattern"
153 " already specified in to clause.\n",
158 if ( style == NULL || *style == '\0' ||
159 ( strcasecmp( style, "base" ) == 0 ) ||
160 ( strcasecmp( style, "exact" ) == 0 ))
162 a->acl_dn_style = ACL_STYLE_BASE;
163 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
165 } else if ( strcasecmp( style, "one" ) == 0 ) {
166 a->acl_dn_style = ACL_STYLE_ONE;
167 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
169 } else if ( strcasecmp( style, "subtree" ) == 0
170 || strcasecmp( style, "sub" ) == 0 )
172 a->acl_dn_style = ACL_STYLE_SUBTREE;
173 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
175 } else if ( strcasecmp( style, "children" ) == 0 ) {
176 a->acl_dn_style = ACL_STYLE_CHILDREN;
177 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
179 } else if ( strcasecmp( style, "regex" ) == 0 ) {
180 a->acl_dn_style = ACL_STYLE_REGEX;
182 if ( *right == '\0' ) {
183 /* empty regex should match empty DN */
184 a->acl_dn_style = ACL_STYLE_BASE;
185 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
187 } else if ( strcmp(right, "*") == 0
188 || strcmp(right, ".*") == 0
189 || strcmp(right, ".*$") == 0
190 || strcmp(right, "^.*") == 0
191 || strcmp(right, "^.*$") == 0
192 || strcmp(right, ".*$$") == 0
193 || strcmp(right, "^.*$$") == 0 )
195 a->acl_dn_pat.bv_val = ch_strdup( "*" );
196 a->acl_dn_pat.bv_len = sizeof("*")-1;
199 acl_regex_normalized_dn( right, &a->acl_dn_pat );
203 fprintf( stderr, "%s: line %d: "
204 "unknown dn style \"%s\" in to clause\n",
205 fname, lineno, style );
212 if ( strcasecmp( left, "filter" ) == 0 ) {
213 if ( (a->acl_filter = str2filter( right )) == NULL ) {
215 "%s: line %d: bad filter \"%s\" in to clause\n",
216 fname, lineno, right );
220 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
221 a->acl_attrs = str2anlist( a->acl_attrs,
223 if ( a->acl_attrs == NULL ) {
225 "%s: line %d: unknown attr \"%s\" in to clause\n",
226 fname, lineno, right );
232 "%s: line %d: expecting <what> got \"%s\"\n",
233 fname, lineno, left );
238 if ( a->acl_dn_pat.bv_len != 0 &&
239 strcmp(a->acl_dn_pat.bv_val, "*") == 0 )
241 free( a->acl_dn_pat.bv_val );
242 a->acl_dn_pat.bv_val = NULL;
243 a->acl_dn_pat.bv_len = 0;
246 if( a->acl_dn_pat.bv_len != 0 ||
247 ( a->acl_dn_style != ACL_STYLE_REGEX ) )
249 if ( a->acl_dn_style != ACL_STYLE_REGEX ) {
251 rc = dnNormalize( 0, NULL, NULL, &a->acl_dn_pat, &bv, NULL);
252 if ( rc != LDAP_SUCCESS ) {
254 "%s: line %d: bad DN \"%s\" in to DN clause\n",
255 fname, lineno, a->acl_dn_pat.bv_val );
258 free( a->acl_dn_pat.bv_val );
261 int e = regcomp( &a->acl_dn_re, a->acl_dn_pat.bv_val,
262 REG_EXTENDED | REG_ICASE );
265 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
266 fprintf( stderr, "%s: line %d: "
267 "regular expression \"%s\" bad because of %s\n",
268 fname, lineno, right, buf );
274 /* by clause - select who has what access to entries */
275 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
277 fprintf( stderr, "%s: line %d: "
278 "to clause required before by clause in access line\n",
284 * by clause consists of <who> and <access>
287 b = (Access *) ch_calloc( 1, sizeof(Access) );
289 ACL_INVALIDATE( b->a_access_mask );
293 "%s: line %d: premature eol: expecting <who>\n",
299 for ( ; i < argc; i++ ) {
300 slap_style_t sty = ACL_STYLE_REGEX;
301 char *style_modifier = NULL;
304 split( argv[i], '=', &left, &right );
305 split( left, '.', &left, &style );
307 split( style, ',', &style, &style_modifier);
310 if ( style == NULL || *style == '\0' ||
311 strcasecmp( style, "exact" ) == 0 ||
312 strcasecmp( style, "base" ) == 0 )
314 sty = ACL_STYLE_BASE;
316 } else if ( strcasecmp( style, "one" ) == 0 ) {
319 } else if ( strcasecmp( style, "subtree" ) == 0 ||
320 strcasecmp( style, "sub" ) == 0 )
322 sty = ACL_STYLE_SUBTREE;
324 } else if ( strcasecmp( style, "children" ) == 0 ) {
325 sty = ACL_STYLE_CHILDREN;
327 } else if ( strcasecmp( style, "regex" ) == 0 ) {
328 sty = ACL_STYLE_REGEX;
332 "%s: line %d: unknown style \"%s\" in by clause\n",
333 fname, lineno, style );
337 if ( style_modifier &&
338 strcasecmp( style_modifier, "expand" ) == 0 )
343 if ( strcasecmp( argv[i], "*" ) == 0 ) {
344 bv.bv_val = ch_strdup( "*" );
346 sty = ACL_STYLE_REGEX;
348 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
349 ber_str2bv("anonymous", sizeof("anonymous")-1, 1, &bv);
350 sty = ACL_STYLE_REGEX;
352 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
353 ber_str2bv("self", sizeof("self")-1, 1, &bv);
354 sty = ACL_STYLE_REGEX;
356 } else if ( strcasecmp( argv[i], "users" ) == 0 ) {
357 ber_str2bv("users", sizeof("users")-1, 1, &bv);
358 sty = ACL_STYLE_REGEX;
360 } else if ( strcasecmp( left, "dn" ) == 0 ) {
361 if ( sty == ACL_STYLE_REGEX ) {
362 b->a_dn_style = ACL_STYLE_REGEX;
363 if( right == NULL ) {
368 } else if (*right == '\0' ) {
370 ber_str2bv("anonymous",
371 sizeof("anonymous")-1,
373 } else if ( strcmp( right, "*" ) == 0 ) {
375 /* any or users? users for now */
379 } else if ( strcmp( right, ".+" ) == 0
380 || strcmp( right, "^.+" ) == 0
381 || strcmp( right, ".+$" ) == 0
382 || strcmp( right, "^.+$" ) == 0
383 || strcmp( right, ".+$$" ) == 0
384 || strcmp( right, "^.+$$" ) == 0 )
389 } else if ( strcmp( right, ".*" ) == 0
390 || strcmp( right, "^.*" ) == 0
391 || strcmp( right, ".*$" ) == 0
392 || strcmp( right, "^.*$" ) == 0
393 || strcmp( right, ".*$$" ) == 0
394 || strcmp( right, "^.*$$" ) == 0 )
401 acl_regex_normalized_dn( right, &bv );
402 if ( !ber_bvccmp( &bv, '*' ) ) {
403 regtest(fname, lineno, bv.bv_val);
406 } else if ( right == NULL || *right == '\0' ) {
407 fprintf( stderr, "%s: line %d: "
408 "missing \"=\" in (or value after) \"%s\" "
410 fname, lineno, left );
414 ber_str2bv( right, 0, 1, &bv );
421 if( bv.bv_val != NULL ) {
422 if( b->a_dn_pat.bv_len != 0 ) {
424 "%s: line %d: dn pattern already specified.\n",
429 if ( sty != ACL_STYLE_REGEX && expand == 0 ) {
430 rc = dnNormalize(0, NULL, NULL,
431 &bv, &b->a_dn_pat, NULL);
432 if ( rc != LDAP_SUCCESS ) {
434 "%s: line %d: bad DN \"%s\" in by DN clause\n",
435 fname, lineno, bv.bv_val );
443 b->a_dn_expand = expand;
447 if ( strcasecmp( left, "dnattr" ) == 0 ) {
448 if ( right == NULL || right[ 0 ] == '\0' ) {
450 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
451 fname, lineno, left );
455 if( b->a_dn_at != NULL ) {
457 "%s: line %d: dnattr already specified.\n",
462 rc = slap_str2ad( right, &b->a_dn_at, &text );
464 if( rc != LDAP_SUCCESS ) {
466 "%s: line %d: dnattr \"%s\": %s\n",
467 fname, lineno, right, text );
472 if( !is_at_syntax( b->a_dn_at->ad_type,
474 !is_at_syntax( b->a_dn_at->ad_type,
475 SLAPD_NAMEUID_SYNTAX ))
478 "%s: line %d: dnattr \"%s\": "
479 "inappropriate syntax: %s\n",
480 fname, lineno, right,
481 b->a_dn_at->ad_type->sat_syntax_oid );
485 if( b->a_dn_at->ad_type->sat_equality == NULL ) {
487 "%s: line %d: dnattr \"%s\": "
488 "inappropriate matching (no EQUALITY)\n",
489 fname, lineno, right );
496 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
500 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
501 fprintf( stderr, "%s: line %d: "
502 "inappropriate style \"%s\" in by clause\n",
503 fname, lineno, style );
507 if ( right == NULL || right[ 0 ] == '\0' ) {
508 fprintf( stderr, "%s: line %d: "
509 "missing \"=\" in (or value after) \"%s\" "
511 fname, lineno, left );
515 if( b->a_group_pat.bv_len ) {
517 "%s: line %d: group pattern already specified.\n",
522 /* format of string is
523 "group/objectClassValue/groupAttrName" */
524 if ((value = strchr(left, '/')) != NULL) {
526 if (*value && (name = strchr(value, '/')) != NULL) {
531 b->a_group_style = sty;
532 if (sty == ACL_STYLE_REGEX) {
533 acl_regex_normalized_dn( right, &bv );
534 if ( !ber_bvccmp( &bv, '*' ) ) {
535 regtest(fname, lineno, bv.bv_val);
539 ber_str2bv( right, 0, 0, &bv );
540 rc = dnNormalize( 0, NULL, NULL, &bv,
541 &b->a_group_pat, NULL );
542 if ( rc != LDAP_SUCCESS ) {
544 "%s: line %d: bad DN \"%s\"\n",
545 fname, lineno, right );
550 if (value && *value) {
551 b->a_group_oc = oc_find( value );
554 if( b->a_group_oc == NULL ) {
556 "%s: line %d: group objectclass "
558 fname, lineno, value );
562 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
564 if( b->a_group_oc == NULL ) {
566 "%s: line %d: group default objectclass "
568 fname, lineno, SLAPD_GROUP_CLASS );
573 if( is_object_subclass( slap_schema.si_oc_referral,
577 "%s: line %d: group objectclass \"%s\" "
578 "is subclass of referral\n",
579 fname, lineno, value );
583 if( is_object_subclass( slap_schema.si_oc_alias,
587 "%s: line %d: group objectclass \"%s\" "
588 "is subclass of alias\n",
589 fname, lineno, value );
594 rc = slap_str2ad( name, &b->a_group_at, &text );
596 if( rc != LDAP_SUCCESS ) {
598 "%s: line %d: group \"%s\": %s\n",
599 fname, lineno, right, text );
604 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
606 if( rc != LDAP_SUCCESS ) {
608 "%s: line %d: group \"%s\": %s\n",
609 fname, lineno, SLAPD_GROUP_ATTR, text );
614 if( !is_at_syntax( b->a_group_at->ad_type,
616 !is_at_syntax( b->a_group_at->ad_type,
617 SLAPD_NAMEUID_SYNTAX ) )
620 "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
621 fname, lineno, right,
622 b->a_group_at->ad_type->sat_syntax_oid );
629 struct berval vals[2];
631 vals[0].bv_val = b->a_group_oc->soc_oid;
632 vals[0].bv_len = strlen(vals[0].bv_val);
633 vals[1].bv_val = NULL;
636 rc = oc_check_allowed( b->a_group_at->ad_type,
640 fprintf( stderr, "%s: line %d: "
641 "group: \"%s\" not allowed by \"%s\"\n",
643 b->a_group_at->ad_cname.bv_val,
644 b->a_group_oc->soc_oid );
651 if ( strcasecmp( left, "peername" ) == 0 ) {
652 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
653 fprintf( stderr, "%s: line %d: "
654 "inappropriate style \"%s\" in by clause\n",
655 fname, lineno, style );
659 if ( right == NULL || right[ 0 ] == '\0' ) {
660 fprintf( stderr, "%s: line %d: "
661 "missing \"=\" in (or value after) \"%s\" "
663 fname, lineno, left );
667 if( b->a_peername_pat.bv_len ) {
668 fprintf( stderr, "%s: line %d: "
669 "peername pattern already specified.\n",
674 b->a_peername_style = sty;
675 if (sty == ACL_STYLE_REGEX) {
676 acl_regex_normalized_dn( right, &bv );
677 if ( !ber_bvccmp( &bv, '*' ) ) {
678 regtest(fname, lineno, bv.bv_val);
680 b->a_peername_pat = bv;
682 ber_str2bv( right, 0, 1, &b->a_peername_pat );
687 if ( strcasecmp( left, "sockname" ) == 0 ) {
688 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
689 fprintf( stderr, "%s: line %d: "
690 "inappropriate style \"%s\" in by clause\n",
691 fname, lineno, style );
695 if ( right == NULL || right[ 0 ] == '\0' ) {
696 fprintf( stderr, "%s: line %d: "
697 "missing \"=\" in (or value after) \"%s\" "
699 fname, lineno, left );
703 if( b->a_sockname_pat.bv_len ) {
704 fprintf( stderr, "%s: line %d: "
705 "sockname pattern already specified.\n",
710 b->a_sockname_style = sty;
711 if (sty == ACL_STYLE_REGEX) {
712 acl_regex_normalized_dn( right, &bv );
713 if ( !ber_bvccmp( &bv, '*' ) ) {
714 regtest(fname, lineno, bv.bv_val);
716 b->a_sockname_pat = bv;
718 ber_str2bv( right, 0, 1, &b->a_sockname_pat );
723 if ( strcasecmp( left, "domain" ) == 0 ) {
725 case ACL_STYLE_REGEX:
727 case ACL_STYLE_SUBTREE:
732 "%s: line %d: inappropriate style \"%s\" in by clause\n",
733 fname, lineno, style );
737 if ( right == NULL || right[ 0 ] == '\0' ) {
739 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
740 fname, lineno, left );
744 if( b->a_domain_pat.bv_len ) {
746 "%s: line %d: domain pattern already specified.\n",
751 b->a_domain_style = sty;
752 b->a_domain_expand = expand;
753 if (sty == ACL_STYLE_REGEX) {
754 acl_regex_normalized_dn( right, &bv );
755 if ( !ber_bvccmp( &bv, '*' ) ) {
756 regtest(fname, lineno, bv.bv_val);
758 b->a_domain_pat = bv;
760 ber_str2bv( right, 0, 1, &b->a_domain_pat );
765 if ( strcasecmp( left, "sockurl" ) == 0 ) {
766 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
768 "%s: line %d: inappropriate style \"%s\" in by clause\n",
769 fname, lineno, style );
773 if ( right == NULL || right[ 0 ] == '\0' ) {
775 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
776 fname, lineno, left );
780 if( b->a_sockurl_pat.bv_len ) {
782 "%s: line %d: sockurl pattern already specified.\n",
787 b->a_sockurl_style = sty;
788 if (sty == ACL_STYLE_REGEX) {
789 acl_regex_normalized_dn( right, &bv );
790 if ( !ber_bvccmp( &bv, '*' ) ) {
791 regtest(fname, lineno, bv.bv_val);
793 b->a_sockurl_pat = bv;
795 ber_str2bv( right, 0, 1, &b->a_sockurl_pat );
800 if ( strcasecmp( left, "set" ) == 0 ) {
801 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
803 "%s: line %d: inappropriate style \"%s\" in by clause\n",
804 fname, lineno, style );
808 if( b->a_set_pat.bv_len != 0 ) {
810 "%s: line %d: set attribute already specified.\n",
815 if ( right == NULL || *right == '\0' ) {
817 "%s: line %d: no set is defined\n",
822 b->a_set_style = sty;
823 ber_str2bv( right, 0, 1, &b->a_set_pat );
828 #ifdef SLAPD_ACI_ENABLED
829 if ( strcasecmp( left, "aci" ) == 0 ) {
830 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
832 "%s: line %d: inappropriate style \"%s\" in by clause\n",
833 fname, lineno, style );
837 if( b->a_aci_at != NULL ) {
839 "%s: line %d: aci attribute already specified.\n",
844 if ( right != NULL && *right != '\0' ) {
845 rc = slap_str2ad( right, &b->a_aci_at, &text );
847 if( rc != LDAP_SUCCESS ) {
849 "%s: line %d: aci \"%s\": %s\n",
850 fname, lineno, right, text );
855 b->a_aci_at = slap_schema.si_ad_aci;
858 if( !is_at_syntax( b->a_aci_at->ad_type,
862 "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
863 fname, lineno, right,
864 b->a_aci_at->ad_type->sat_syntax_oid );
870 #endif /* SLAPD_ACI_ENABLED */
872 if ( strcasecmp( left, "ssf" ) == 0 ) {
873 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
875 "%s: line %d: inappropriate style \"%s\" in by clause\n",
876 fname, lineno, style );
880 if( b->a_authz.sai_ssf ) {
882 "%s: line %d: ssf attribute already specified.\n",
887 if ( right == NULL || *right == '\0' ) {
889 "%s: line %d: no ssf is defined\n",
894 b->a_authz.sai_ssf = atoi( right );
896 if( !b->a_authz.sai_ssf ) {
898 "%s: line %d: invalid ssf value (%s)\n",
899 fname, lineno, right );
905 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
906 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
908 "%s: line %d: inappropriate style \"%s\" in by clause\n",
909 fname, lineno, style );
913 if( b->a_authz.sai_transport_ssf ) {
915 "%s: line %d: transport_ssf attribute already specified.\n",
920 if ( right == NULL || *right == '\0' ) {
922 "%s: line %d: no transport_ssf is defined\n",
927 b->a_authz.sai_transport_ssf = atoi( right );
929 if( !b->a_authz.sai_transport_ssf ) {
931 "%s: line %d: invalid transport_ssf value (%s)\n",
932 fname, lineno, right );
938 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
939 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
941 "%s: line %d: inappropriate style \"%s\" in by clause\n",
942 fname, lineno, style );
946 if( b->a_authz.sai_tls_ssf ) {
948 "%s: line %d: tls_ssf attribute already specified.\n",
953 if ( right == NULL || *right == '\0' ) {
955 "%s: line %d: no tls_ssf is defined\n",
960 b->a_authz.sai_tls_ssf = atoi( right );
962 if( !b->a_authz.sai_tls_ssf ) {
964 "%s: line %d: invalid tls_ssf value (%s)\n",
965 fname, lineno, right );
971 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
972 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
974 "%s: line %d: inappropriate style \"%s\" in by clause\n",
975 fname, lineno, style );
979 if( b->a_authz.sai_sasl_ssf ) {
981 "%s: line %d: sasl_ssf attribute already specified.\n",
986 if ( right == NULL || *right == '\0' ) {
988 "%s: line %d: no sasl_ssf is defined\n",
993 b->a_authz.sai_sasl_ssf = atoi( right );
995 if( !b->a_authz.sai_sasl_ssf ) {
997 "%s: line %d: invalid sasl_ssf value (%s)\n",
998 fname, lineno, right );
1004 if( right != NULL ) {
1011 if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) {
1012 /* out of arguments or plain stop */
1014 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1015 b->a_type = ACL_STOP;
1017 access_append( &a->acl_access, b );
1021 if( strcasecmp( left, "continue" ) == 0 ) {
1022 /* plain continue */
1024 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1025 b->a_type = ACL_CONTINUE;
1027 access_append( &a->acl_access, b );
1031 if( strcasecmp( left, "break" ) == 0 ) {
1032 /* plain continue */
1034 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1035 b->a_type = ACL_BREAK;
1037 access_append( &a->acl_access, b );
1041 if ( strcasecmp( left, "by" ) == 0 ) {
1042 /* we've gone too far */
1044 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1045 b->a_type = ACL_STOP;
1047 access_append( &a->acl_access, b );
1052 if( strncasecmp( left, "self", 4 ) == 0 ) {
1054 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[4] ) );
1057 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
1060 if( ACL_IS_INVALID( b->a_access_mask ) ) {
1062 "%s: line %d: expecting <access> got \"%s\"\n",
1063 fname, lineno, left );
1067 b->a_type = ACL_STOP;
1070 /* out of arguments or plain stop */
1071 access_append( &a->acl_access, b );
1075 if( strcasecmp( argv[i], "continue" ) == 0 ) {
1076 /* plain continue */
1077 b->a_type = ACL_CONTINUE;
1079 } else if( strcasecmp( argv[i], "break" ) == 0 ) {
1080 /* plain continue */
1081 b->a_type = ACL_BREAK;
1083 } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
1088 access_append( &a->acl_access, b );
1092 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
1093 fname, lineno, argv[i] );
1098 /* if we have no real access clause, complain and do nothing */
1101 "%s: line %d: warning: no access clause(s) specified in access line\n",
1106 if (ldap_debug & LDAP_DEBUG_ACL)
1110 if ( a->acl_access == NULL ) {
1112 "%s: line %d: warning: no by clause(s) specified in access line\n",
1117 acl_append( &be->be_acl, a );
1119 acl_append( &global_acl, a );
1125 accessmask2str( slap_mask_t mask, char *buf )
1130 assert( buf != NULL );
1132 if ( ACL_IS_INVALID( mask ) ) {
1138 if ( ACL_IS_LEVEL( mask ) ) {
1139 if ( ACL_LVL_IS_NONE(mask) ) {
1140 ptr = lutil_strcopy( ptr, "none" );
1142 } else if ( ACL_LVL_IS_AUTH(mask) ) {
1143 ptr = lutil_strcopy( ptr, "auth" );
1145 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
1146 ptr = lutil_strcopy( ptr, "compare" );
1148 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
1149 ptr = lutil_strcopy( ptr, "search" );
1151 } else if ( ACL_LVL_IS_READ(mask) ) {
1152 ptr = lutil_strcopy( ptr, "read" );
1154 } else if ( ACL_LVL_IS_WRITE(mask) ) {
1155 ptr = lutil_strcopy( ptr, "write" );
1157 ptr = lutil_strcopy( ptr, "unknown" );
1163 if( ACL_IS_ADDITIVE( mask ) ) {
1166 } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
1173 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
1178 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
1183 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
1188 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
1193 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
1198 if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
1207 if ( ACL_IS_LEVEL( mask ) ) {
1217 str2accessmask( const char *str )
1221 if( !ASCII_ALPHA(str[0]) ) {
1224 if ( str[0] == '=' ) {
1227 } else if( str[0] == '+' ) {
1228 ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1230 } else if( str[0] == '-' ) {
1231 ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1234 ACL_INVALIDATE(mask);
1238 for( i=1; str[i] != '\0'; i++ ) {
1239 if( TOLOWER((unsigned char) str[i]) == 'w' ) {
1240 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1242 } else if( TOLOWER((unsigned char) str[i]) == 'r' ) {
1243 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1245 } else if( TOLOWER((unsigned char) str[i]) == 's' ) {
1246 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1248 } else if( TOLOWER((unsigned char) str[i]) == 'c' ) {
1249 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1251 } else if( TOLOWER((unsigned char) str[i]) == 'x' ) {
1252 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1254 } else if( str[i] != '0' ) {
1255 ACL_INVALIDATE(mask);
1263 if ( strcasecmp( str, "none" ) == 0 ) {
1264 ACL_LVL_ASSIGN_NONE(mask);
1266 } else if ( strcasecmp( str, "auth" ) == 0 ) {
1267 ACL_LVL_ASSIGN_AUTH(mask);
1269 } else if ( strcasecmp( str, "compare" ) == 0 ) {
1270 ACL_LVL_ASSIGN_COMPARE(mask);
1272 } else if ( strcasecmp( str, "search" ) == 0 ) {
1273 ACL_LVL_ASSIGN_SEARCH(mask);
1275 } else if ( strcasecmp( str, "read" ) == 0 ) {
1276 ACL_LVL_ASSIGN_READ(mask);
1278 } else if ( strcasecmp( str, "write" ) == 0 ) {
1279 ACL_LVL_ASSIGN_WRITE(mask);
1282 ACL_INVALIDATE( mask );
1291 fprintf( stderr, "%s%s\n",
1292 "<access clause> ::= access to <what> "
1293 "[ by <who> <access> [ <control> ] ]+ \n"
1294 "<what> ::= * | [dn[.<dnstyle>]=<DN>] [filter=<filter>] [attrs=<attrlist>]\n"
1295 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
1296 "<attr> ::= <attrname> | entry | children\n"
1297 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<DN> ]\n"
1298 "\t[dnattr=<attrname>]\n"
1299 "\t[group[/<objectclass>[/<attrname>]][.<style>]=<group>]\n"
1300 "\t[peername[.<style>]=<peer>] [sockname[.<style>]=<name>]\n",
1301 "\t[domain[.<style>]=<domain>] [sockurl[.<style>]=<url>]\n"
1302 #ifdef SLAPD_ACI_ENABLED
1303 "\t[aci=<attrname>]\n"
1305 "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
1306 "<dnstyle> ::= base | exact | one | subtree | children | regex\n"
1307 "<style> ::= regex | base | exact\n"
1308 "<access> ::= [self]{<level>|<priv>}\n"
1309 "<level> ::= none | auth | compare | search | read | write\n"
1310 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
1311 "<control> ::= [ stop | continue | break ]\n"
1313 exit( EXIT_FAILURE );
1317 * Set pattern to a "normalized" DN from src.
1318 * At present it simply eats the (optional) space after
1319 * a RDN separator (,)
1320 * Eventually will evolve in a more complete normalization
1323 acl_regex_normalized_dn(
1325 struct berval *pattern
1331 str = ch_strdup( src );
1332 len = strlen( src );
1334 for ( p = str; p && p[ 0 ]; p++ ) {
1336 if ( p[ 0 ] == '\\' && p[ 1 ] ) {
1338 * if escaping a hex pair we should
1339 * increment p twice; however, in that
1340 * case the second hex number does
1346 if ( p[ 0 ] == ',' ) {
1347 if ( p[ 1 ] == ' ' ) {
1351 * too much space should be
1352 * an error if we are pedantic
1354 for ( q = &p[ 2 ]; q[ 0 ] == ' '; q++ ) {
1357 AC_MEMCPY( p+1, q, len-(q-str)+1);
1361 pattern->bv_val = str;
1362 pattern->bv_len = p-str;
1376 if ( (*right = strchr( line, splitchar )) != NULL ) {
1377 *((*right)++) = '\0';
1382 access_append( Access **l, Access *a )
1384 for ( ; *l != NULL; l = &(*l)->a_next )
1391 acl_append( AccessControl **l, AccessControl *a )
1393 for ( ; *l != NULL; l = &(*l)->acl_next )
1400 access_free( Access *a )
1402 if ( a->a_dn_pat.bv_val )
1403 free ( a->a_dn_pat.bv_val );
1404 if ( a->a_peername_pat.bv_val )
1405 free ( a->a_peername_pat.bv_val );
1406 if ( a->a_sockname_pat.bv_val )
1407 free ( a->a_sockname_pat.bv_val );
1408 if ( a->a_domain_pat.bv_val )
1409 free ( a->a_domain_pat.bv_val );
1410 if ( a->a_sockurl_pat.bv_val )
1411 free ( a->a_sockurl_pat.bv_val );
1412 if ( a->a_set_pat.bv_len )
1413 free ( a->a_set_pat.bv_val );
1414 if ( a->a_group_pat.bv_len )
1415 free ( a->a_group_pat.bv_val );
1420 acl_free( AccessControl *a )
1425 if ( a->acl_filter )
1426 filter_free( a->acl_filter );
1427 if ( a->acl_dn_pat.bv_len )
1428 free ( a->acl_dn_pat.bv_val );
1429 if ( a->acl_attrs ) {
1430 for ( an = a->acl_attrs; an->an_name.bv_val; an++ ) {
1431 free( an->an_name.bv_val );
1433 free( a->acl_attrs );
1435 for (; a->acl_access; a->acl_access = n) {
1436 n = a->acl_access->a_next;
1437 access_free( a->acl_access );
1442 /* Because backend_startup uses acl_append to tack on the global_acl to
1443 * the end of each backend's acl, we cannot just take one argument and
1444 * merrily free our way to the end of the list. backend_destroy calls us
1445 * with the be_acl in arg1, and global_acl in arg2 to give us a stopping
1446 * point. config_destroy calls us with global_acl in arg1 and NULL in
1447 * arg2, so we then proceed to polish off the global_acl.
1450 acl_destroy( AccessControl *a, AccessControl *end )
1454 for (; a && a!= end; a=n) {
1461 access2str( slap_access_t access )
1463 if ( access == ACL_NONE ) {
1466 } else if ( access == ACL_AUTH ) {
1469 } else if ( access == ACL_COMPARE ) {
1472 } else if ( access == ACL_SEARCH ) {
1475 } else if ( access == ACL_READ ) {
1478 } else if ( access == ACL_WRITE ) {
1486 str2access( const char *str )
1488 if ( strcasecmp( str, "none" ) == 0 ) {
1491 } else if ( strcasecmp( str, "auth" ) == 0 ) {
1494 } else if ( strcasecmp( str, "compare" ) == 0 ) {
1497 } else if ( strcasecmp( str, "search" ) == 0 ) {
1500 } else if ( strcasecmp( str, "read" ) == 0 ) {
1503 } else if ( strcasecmp( str, "write" ) == 0 ) {
1507 return( ACL_INVALID_ACCESS );
1512 static char *style_strings[5] = { "regex",
1513 "base", "one", "subtree", "children" };
1516 print_access( Access *b )
1518 char maskbuf[ACCESSMASK_MAXLEN];
1520 fprintf( stderr, "\tby" );
1522 if ( b->a_dn_pat.bv_len != 0 ) {
1523 if( strcmp(b->a_dn_pat.bv_val, "*") == 0
1524 || strcmp(b->a_dn_pat.bv_val, "users") == 0
1525 || strcmp(b->a_dn_pat.bv_val, "anonymous") == 0
1526 || strcmp(b->a_dn_pat.bv_val, "self") == 0 )
1528 fprintf( stderr, " %s", b->a_dn_pat.bv_val );
1531 fprintf( stderr, " dn.%s=%s",
1532 style_strings[b->a_dn_style], b->a_dn_pat.bv_val );
1536 if ( b->a_dn_at != NULL ) {
1537 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname.bv_val );
1540 if ( b->a_group_pat.bv_len ) {
1541 fprintf( stderr, " group=%s", b->a_group_pat.bv_val );
1543 if ( b->a_group_oc ) {
1544 fprintf( stderr, " objectClass: %s",
1545 b->a_group_oc->soc_oclass.oc_oid );
1547 if ( b->a_group_at ) {
1548 fprintf( stderr, " attributeType: %s",
1549 b->a_group_at->ad_cname.bv_val );
1554 if ( b->a_peername_pat.bv_len != 0 ) {
1555 fprintf( stderr, " peername=%s", b->a_peername_pat.bv_val );
1558 if ( b->a_sockname_pat.bv_len != 0 ) {
1559 fprintf( stderr, " sockname=%s", b->a_sockname_pat.bv_val );
1562 if ( b->a_domain_pat.bv_len != 0 ) {
1563 fprintf( stderr, " domain=%s", b->a_domain_pat.bv_val );
1566 if ( b->a_sockurl_pat.bv_len != 0 ) {
1567 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat.bv_val );
1570 #ifdef SLAPD_ACI_ENABLED
1571 if ( b->a_aci_at != NULL ) {
1572 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname.bv_val );
1576 /* Security Strength Factors */
1577 if ( b->a_authz.sai_ssf ) {
1578 fprintf( stderr, " ssf=%u",
1579 b->a_authz.sai_ssf );
1581 if ( b->a_authz.sai_transport_ssf ) {
1582 fprintf( stderr, " transport_ssf=%u",
1583 b->a_authz.sai_transport_ssf );
1585 if ( b->a_authz.sai_tls_ssf ) {
1586 fprintf( stderr, " tls_ssf=%u",
1587 b->a_authz.sai_tls_ssf );
1589 if ( b->a_authz.sai_sasl_ssf ) {
1590 fprintf( stderr, " sasl_ssf=%u",
1591 b->a_authz.sai_sasl_ssf );
1594 fprintf( stderr, " %s%s",
1595 b->a_dn_self ? "self" : "",
1596 accessmask2str( b->a_access_mask, maskbuf ) );
1598 if( b->a_type == ACL_BREAK ) {
1599 fprintf( stderr, " break" );
1601 } else if( b->a_type == ACL_CONTINUE ) {
1602 fprintf( stderr, " continue" );
1604 } else if( b->a_type != ACL_STOP ) {
1605 fprintf( stderr, " unknown-control" );
1608 fprintf( stderr, "\n" );
1613 print_acl( Backend *be, AccessControl *a )
1618 fprintf( stderr, "%s ACL: access to",
1619 be == NULL ? "Global" : "Backend" );
1621 if ( a->acl_dn_pat.bv_len != 0 ) {
1623 fprintf( stderr, " dn.%s=%s\n",
1624 style_strings[a->acl_dn_style], a->acl_dn_pat.bv_val );
1627 if ( a->acl_filter != NULL ) {
1628 struct berval bv = { 0, NULL };
1630 filter2bv( a->acl_filter, &bv );
1631 fprintf( stderr, " filter=%s\n", bv.bv_val );
1632 ch_free( bv.bv_val );
1635 if ( a->acl_attrs != NULL ) {
1640 fprintf( stderr, " attrs=" );
1641 for ( an = a->acl_attrs; an && an->an_name.bv_val; an++ ) {
1643 fprintf( stderr, "," );
1645 fputs( an->an_name.bv_val, stderr );
1648 fprintf( stderr, "\n" );
1652 fprintf( stderr, " *\n" );
1655 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1659 fprintf( stderr, "\n" );
1662 #endif /* LDAP_DEBUG */