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 if( *right == '\0' ) {
173 a->acl_dn_pat.bv_val = ch_strdup( "*" );
174 a->acl_dn_pat.bv_len = 1;
177 a->acl_dn_style = ACL_STYLE_SUBTREE;
178 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
181 } else if ( strcasecmp( style, "children" ) == 0 ) {
182 a->acl_dn_style = ACL_STYLE_CHILDREN;
183 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
185 } else if ( strcasecmp( style, "regex" ) == 0 ) {
186 a->acl_dn_style = ACL_STYLE_REGEX;
188 if ( *right == '\0' ) {
189 /* empty regex should match empty DN */
190 a->acl_dn_style = ACL_STYLE_BASE;
191 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
193 } else if ( strcmp(right, "*") == 0
194 || strcmp(right, ".*") == 0
195 || strcmp(right, ".*$") == 0
196 || strcmp(right, "^.*") == 0
197 || strcmp(right, "^.*$") == 0
198 || strcmp(right, ".*$$") == 0
199 || strcmp(right, "^.*$$") == 0 )
201 a->acl_dn_pat.bv_val = ch_strdup( "*" );
202 a->acl_dn_pat.bv_len = sizeof("*")-1;
205 acl_regex_normalized_dn( right, &a->acl_dn_pat );
209 fprintf( stderr, "%s: line %d: "
210 "unknown dn style \"%s\" in to clause\n",
211 fname, lineno, style );
218 if ( strcasecmp( left, "filter" ) == 0 ) {
219 if ( (a->acl_filter = str2filter( right )) == NULL ) {
221 "%s: line %d: bad filter \"%s\" in to clause\n",
222 fname, lineno, right );
226 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
227 a->acl_attrs = str2anlist( a->acl_attrs,
229 if ( a->acl_attrs == NULL ) {
231 "%s: line %d: unknown attr \"%s\" in to clause\n",
232 fname, lineno, right );
238 "%s: line %d: expecting <what> got \"%s\"\n",
239 fname, lineno, left );
244 if ( a->acl_dn_pat.bv_len != 0 &&
245 strcmp(a->acl_dn_pat.bv_val, "*") == 0 )
247 free( a->acl_dn_pat.bv_val );
248 a->acl_dn_pat.bv_val = NULL;
249 a->acl_dn_pat.bv_len = 0;
252 if( a->acl_dn_pat.bv_len != 0 ||
253 ( a->acl_dn_style != ACL_STYLE_REGEX ) )
255 if ( a->acl_dn_style != ACL_STYLE_REGEX ) {
257 rc = dnNormalize( 0, NULL, NULL, &a->acl_dn_pat, &bv, NULL);
258 if ( rc != LDAP_SUCCESS ) {
260 "%s: line %d: bad DN \"%s\" in to DN clause\n",
261 fname, lineno, a->acl_dn_pat.bv_val );
264 free( a->acl_dn_pat.bv_val );
267 int e = regcomp( &a->acl_dn_re, a->acl_dn_pat.bv_val,
268 REG_EXTENDED | REG_ICASE );
271 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
272 fprintf( stderr, "%s: line %d: "
273 "regular expression \"%s\" bad because of %s\n",
274 fname, lineno, right, buf );
280 /* by clause - select who has what access to entries */
281 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
283 fprintf( stderr, "%s: line %d: "
284 "to clause required before by clause in access line\n",
290 * by clause consists of <who> and <access>
293 b = (Access *) ch_calloc( 1, sizeof(Access) );
295 ACL_INVALIDATE( b->a_access_mask );
299 "%s: line %d: premature eol: expecting <who>\n",
305 for ( ; i < argc; i++ ) {
306 slap_style_t sty = ACL_STYLE_REGEX;
307 char *style_modifier = NULL;
310 split( argv[i], '=', &left, &right );
311 split( left, '.', &left, &style );
313 split( style, ',', &style, &style_modifier);
316 if ( style == NULL || *style == '\0' ||
317 strcasecmp( style, "exact" ) == 0 ||
318 strcasecmp( style, "base" ) == 0 )
320 sty = ACL_STYLE_BASE;
322 } else if ( strcasecmp( style, "one" ) == 0 ) {
325 } else if ( strcasecmp( style, "subtree" ) == 0 ||
326 strcasecmp( style, "sub" ) == 0 )
328 sty = ACL_STYLE_SUBTREE;
330 } else if ( strcasecmp( style, "children" ) == 0 ) {
331 sty = ACL_STYLE_CHILDREN;
333 } else if ( strcasecmp( style, "regex" ) == 0 ) {
334 sty = ACL_STYLE_REGEX;
338 "%s: line %d: unknown style \"%s\" in by clause\n",
339 fname, lineno, style );
343 if ( style_modifier &&
344 strcasecmp( style_modifier, "expand" ) == 0 )
349 if ( strcasecmp( argv[i], "*" ) == 0 ) {
350 bv.bv_val = ch_strdup( "*" );
352 sty = ACL_STYLE_REGEX;
354 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
355 ber_str2bv("anonymous", sizeof("anonymous")-1, 1, &bv);
356 sty = ACL_STYLE_REGEX;
358 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
359 ber_str2bv("self", sizeof("self")-1, 1, &bv);
360 sty = ACL_STYLE_REGEX;
362 } else if ( strcasecmp( argv[i], "users" ) == 0 ) {
363 ber_str2bv("users", sizeof("users")-1, 1, &bv);
364 sty = ACL_STYLE_REGEX;
366 } else if ( strcasecmp( left, "dn" ) == 0 ) {
367 if ( sty == ACL_STYLE_REGEX ) {
368 b->a_dn_style = ACL_STYLE_REGEX;
369 if( right == NULL ) {
374 } else if (*right == '\0' ) {
376 ber_str2bv("anonymous",
377 sizeof("anonymous")-1,
379 } else if ( strcmp( right, "*" ) == 0 ) {
381 /* any or users? users for now */
385 } else if ( strcmp( right, ".+" ) == 0
386 || strcmp( right, "^.+" ) == 0
387 || strcmp( right, ".+$" ) == 0
388 || strcmp( right, "^.+$" ) == 0
389 || strcmp( right, ".+$$" ) == 0
390 || strcmp( right, "^.+$$" ) == 0 )
395 } else if ( strcmp( right, ".*" ) == 0
396 || strcmp( right, "^.*" ) == 0
397 || strcmp( right, ".*$" ) == 0
398 || strcmp( right, "^.*$" ) == 0
399 || strcmp( right, ".*$$" ) == 0
400 || strcmp( right, "^.*$$" ) == 0 )
407 acl_regex_normalized_dn( right, &bv );
408 if ( !ber_bvccmp( &bv, '*' ) ) {
409 regtest(fname, lineno, bv.bv_val);
412 } else if ( right == NULL || *right == '\0' ) {
413 fprintf( stderr, "%s: line %d: "
414 "missing \"=\" in (or value after) \"%s\" "
416 fname, lineno, left );
420 ber_str2bv( right, 0, 1, &bv );
427 if( bv.bv_val != NULL ) {
428 if( b->a_dn_pat.bv_len != 0 ) {
430 "%s: line %d: dn pattern already specified.\n",
435 if ( sty != ACL_STYLE_REGEX && expand == 0 ) {
436 rc = dnNormalize(0, NULL, NULL,
437 &bv, &b->a_dn_pat, NULL);
438 if ( rc != LDAP_SUCCESS ) {
440 "%s: line %d: bad DN \"%s\" in by DN clause\n",
441 fname, lineno, bv.bv_val );
449 b->a_dn_expand = expand;
453 if ( strcasecmp( left, "dnattr" ) == 0 ) {
454 if ( right == NULL || right[ 0 ] == '\0' ) {
456 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
457 fname, lineno, left );
461 if( b->a_dn_at != NULL ) {
463 "%s: line %d: dnattr already specified.\n",
468 rc = slap_str2ad( right, &b->a_dn_at, &text );
470 if( rc != LDAP_SUCCESS ) {
472 "%s: line %d: dnattr \"%s\": %s\n",
473 fname, lineno, right, text );
478 if( !is_at_syntax( b->a_dn_at->ad_type,
480 !is_at_syntax( b->a_dn_at->ad_type,
481 SLAPD_NAMEUID_SYNTAX ))
484 "%s: line %d: dnattr \"%s\": "
485 "inappropriate syntax: %s\n",
486 fname, lineno, right,
487 b->a_dn_at->ad_type->sat_syntax_oid );
491 if( b->a_dn_at->ad_type->sat_equality == NULL ) {
493 "%s: line %d: dnattr \"%s\": "
494 "inappropriate matching (no EQUALITY)\n",
495 fname, lineno, right );
502 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
506 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
507 fprintf( stderr, "%s: line %d: "
508 "inappropriate style \"%s\" in by clause\n",
509 fname, lineno, style );
513 if ( right == NULL || right[ 0 ] == '\0' ) {
514 fprintf( stderr, "%s: line %d: "
515 "missing \"=\" in (or value after) \"%s\" "
517 fname, lineno, left );
521 if( b->a_group_pat.bv_len ) {
523 "%s: line %d: group pattern already specified.\n",
528 /* format of string is
529 "group/objectClassValue/groupAttrName" */
530 if ((value = strchr(left, '/')) != NULL) {
532 if (*value && (name = strchr(value, '/')) != NULL) {
537 b->a_group_style = sty;
538 if (sty == ACL_STYLE_REGEX) {
539 acl_regex_normalized_dn( right, &bv );
540 if ( !ber_bvccmp( &bv, '*' ) ) {
541 regtest(fname, lineno, bv.bv_val);
545 ber_str2bv( right, 0, 0, &bv );
546 rc = dnNormalize( 0, NULL, NULL, &bv,
547 &b->a_group_pat, NULL );
548 if ( rc != LDAP_SUCCESS ) {
550 "%s: line %d: bad DN \"%s\"\n",
551 fname, lineno, right );
556 if (value && *value) {
557 b->a_group_oc = oc_find( value );
560 if( b->a_group_oc == NULL ) {
562 "%s: line %d: group objectclass "
564 fname, lineno, value );
568 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
570 if( b->a_group_oc == NULL ) {
572 "%s: line %d: group default objectclass "
574 fname, lineno, SLAPD_GROUP_CLASS );
579 if( is_object_subclass( slap_schema.si_oc_referral,
583 "%s: line %d: group objectclass \"%s\" "
584 "is subclass of referral\n",
585 fname, lineno, value );
589 if( is_object_subclass( slap_schema.si_oc_alias,
593 "%s: line %d: group objectclass \"%s\" "
594 "is subclass of alias\n",
595 fname, lineno, value );
600 rc = slap_str2ad( name, &b->a_group_at, &text );
602 if( rc != LDAP_SUCCESS ) {
604 "%s: line %d: group \"%s\": %s\n",
605 fname, lineno, right, text );
610 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
612 if( rc != LDAP_SUCCESS ) {
614 "%s: line %d: group \"%s\": %s\n",
615 fname, lineno, SLAPD_GROUP_ATTR, text );
620 if( !is_at_syntax( b->a_group_at->ad_type,
622 !is_at_syntax( b->a_group_at->ad_type,
623 SLAPD_NAMEUID_SYNTAX ) )
626 "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
627 fname, lineno, right,
628 b->a_group_at->ad_type->sat_syntax_oid );
635 struct berval vals[2];
637 vals[0].bv_val = b->a_group_oc->soc_oid;
638 vals[0].bv_len = strlen(vals[0].bv_val);
639 vals[1].bv_val = NULL;
642 rc = oc_check_allowed( b->a_group_at->ad_type,
646 fprintf( stderr, "%s: line %d: "
647 "group: \"%s\" not allowed by \"%s\"\n",
649 b->a_group_at->ad_cname.bv_val,
650 b->a_group_oc->soc_oid );
657 if ( strcasecmp( left, "peername" ) == 0 ) {
658 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
659 fprintf( stderr, "%s: line %d: "
660 "inappropriate style \"%s\" in by clause\n",
661 fname, lineno, style );
665 if ( right == NULL || right[ 0 ] == '\0' ) {
666 fprintf( stderr, "%s: line %d: "
667 "missing \"=\" in (or value after) \"%s\" "
669 fname, lineno, left );
673 if( b->a_peername_pat.bv_len ) {
674 fprintf( stderr, "%s: line %d: "
675 "peername pattern already specified.\n",
680 b->a_peername_style = sty;
681 if (sty == ACL_STYLE_REGEX) {
682 acl_regex_normalized_dn( right, &bv );
683 if ( !ber_bvccmp( &bv, '*' ) ) {
684 regtest(fname, lineno, bv.bv_val);
686 b->a_peername_pat = bv;
688 ber_str2bv( right, 0, 1, &b->a_peername_pat );
693 if ( strcasecmp( left, "sockname" ) == 0 ) {
694 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
695 fprintf( stderr, "%s: line %d: "
696 "inappropriate style \"%s\" in by clause\n",
697 fname, lineno, style );
701 if ( right == NULL || right[ 0 ] == '\0' ) {
702 fprintf( stderr, "%s: line %d: "
703 "missing \"=\" in (or value after) \"%s\" "
705 fname, lineno, left );
709 if( b->a_sockname_pat.bv_len ) {
710 fprintf( stderr, "%s: line %d: "
711 "sockname pattern already specified.\n",
716 b->a_sockname_style = sty;
717 if (sty == ACL_STYLE_REGEX) {
718 acl_regex_normalized_dn( right, &bv );
719 if ( !ber_bvccmp( &bv, '*' ) ) {
720 regtest(fname, lineno, bv.bv_val);
722 b->a_sockname_pat = bv;
724 ber_str2bv( right, 0, 1, &b->a_sockname_pat );
729 if ( strcasecmp( left, "domain" ) == 0 ) {
731 case ACL_STYLE_REGEX:
733 case ACL_STYLE_SUBTREE:
738 "%s: line %d: inappropriate style \"%s\" in by clause\n",
739 fname, lineno, style );
743 if ( right == NULL || right[ 0 ] == '\0' ) {
745 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
746 fname, lineno, left );
750 if( b->a_domain_pat.bv_len ) {
752 "%s: line %d: domain pattern already specified.\n",
757 b->a_domain_style = sty;
758 b->a_domain_expand = expand;
759 if (sty == ACL_STYLE_REGEX) {
760 acl_regex_normalized_dn( right, &bv );
761 if ( !ber_bvccmp( &bv, '*' ) ) {
762 regtest(fname, lineno, bv.bv_val);
764 b->a_domain_pat = bv;
766 ber_str2bv( right, 0, 1, &b->a_domain_pat );
771 if ( strcasecmp( left, "sockurl" ) == 0 ) {
772 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
774 "%s: line %d: inappropriate style \"%s\" in by clause\n",
775 fname, lineno, style );
779 if ( right == NULL || right[ 0 ] == '\0' ) {
781 "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
782 fname, lineno, left );
786 if( b->a_sockurl_pat.bv_len ) {
788 "%s: line %d: sockurl pattern already specified.\n",
793 b->a_sockurl_style = sty;
794 if (sty == ACL_STYLE_REGEX) {
795 acl_regex_normalized_dn( right, &bv );
796 if ( !ber_bvccmp( &bv, '*' ) ) {
797 regtest(fname, lineno, bv.bv_val);
799 b->a_sockurl_pat = bv;
801 ber_str2bv( right, 0, 1, &b->a_sockurl_pat );
806 if ( strcasecmp( left, "set" ) == 0 ) {
807 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
809 "%s: line %d: inappropriate style \"%s\" in by clause\n",
810 fname, lineno, style );
814 if( b->a_set_pat.bv_len != 0 ) {
816 "%s: line %d: set attribute already specified.\n",
821 if ( right == NULL || *right == '\0' ) {
823 "%s: line %d: no set is defined\n",
828 b->a_set_style = sty;
829 ber_str2bv( right, 0, 1, &b->a_set_pat );
834 #ifdef SLAPD_ACI_ENABLED
835 if ( strcasecmp( left, "aci" ) == 0 ) {
836 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
838 "%s: line %d: inappropriate style \"%s\" in by clause\n",
839 fname, lineno, style );
843 if( b->a_aci_at != NULL ) {
845 "%s: line %d: aci attribute already specified.\n",
850 if ( right != NULL && *right != '\0' ) {
851 rc = slap_str2ad( right, &b->a_aci_at, &text );
853 if( rc != LDAP_SUCCESS ) {
855 "%s: line %d: aci \"%s\": %s\n",
856 fname, lineno, right, text );
861 b->a_aci_at = slap_schema.si_ad_aci;
864 if( !is_at_syntax( b->a_aci_at->ad_type,
868 "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
869 fname, lineno, right,
870 b->a_aci_at->ad_type->sat_syntax_oid );
876 #endif /* SLAPD_ACI_ENABLED */
878 if ( strcasecmp( left, "ssf" ) == 0 ) {
879 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
881 "%s: line %d: inappropriate style \"%s\" in by clause\n",
882 fname, lineno, style );
886 if( b->a_authz.sai_ssf ) {
888 "%s: line %d: ssf attribute already specified.\n",
893 if ( right == NULL || *right == '\0' ) {
895 "%s: line %d: no ssf is defined\n",
900 b->a_authz.sai_ssf = atoi( right );
902 if( !b->a_authz.sai_ssf ) {
904 "%s: line %d: invalid ssf value (%s)\n",
905 fname, lineno, right );
911 if ( strcasecmp( left, "transport_ssf" ) == 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_authz.sai_transport_ssf ) {
921 "%s: line %d: transport_ssf attribute already specified.\n",
926 if ( right == NULL || *right == '\0' ) {
928 "%s: line %d: no transport_ssf is defined\n",
933 b->a_authz.sai_transport_ssf = atoi( right );
935 if( !b->a_authz.sai_transport_ssf ) {
937 "%s: line %d: invalid transport_ssf value (%s)\n",
938 fname, lineno, right );
944 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
945 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
947 "%s: line %d: inappropriate style \"%s\" in by clause\n",
948 fname, lineno, style );
952 if( b->a_authz.sai_tls_ssf ) {
954 "%s: line %d: tls_ssf attribute already specified.\n",
959 if ( right == NULL || *right == '\0' ) {
961 "%s: line %d: no tls_ssf is defined\n",
966 b->a_authz.sai_tls_ssf = atoi( right );
968 if( !b->a_authz.sai_tls_ssf ) {
970 "%s: line %d: invalid tls_ssf value (%s)\n",
971 fname, lineno, right );
977 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
978 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
980 "%s: line %d: inappropriate style \"%s\" in by clause\n",
981 fname, lineno, style );
985 if( b->a_authz.sai_sasl_ssf ) {
987 "%s: line %d: sasl_ssf attribute already specified.\n",
992 if ( right == NULL || *right == '\0' ) {
994 "%s: line %d: no sasl_ssf is defined\n",
999 b->a_authz.sai_sasl_ssf = atoi( right );
1001 if( !b->a_authz.sai_sasl_ssf ) {
1003 "%s: line %d: invalid sasl_ssf value (%s)\n",
1004 fname, lineno, right );
1010 if( right != NULL ) {
1017 if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) {
1018 /* out of arguments or plain stop */
1020 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1021 b->a_type = ACL_STOP;
1023 access_append( &a->acl_access, b );
1027 if( strcasecmp( left, "continue" ) == 0 ) {
1028 /* plain continue */
1030 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1031 b->a_type = ACL_CONTINUE;
1033 access_append( &a->acl_access, b );
1037 if( strcasecmp( left, "break" ) == 0 ) {
1038 /* plain continue */
1040 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1041 b->a_type = ACL_BREAK;
1043 access_append( &a->acl_access, b );
1047 if ( strcasecmp( left, "by" ) == 0 ) {
1048 /* we've gone too far */
1050 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1051 b->a_type = ACL_STOP;
1053 access_append( &a->acl_access, b );
1058 if( strncasecmp( left, "self", 4 ) == 0 ) {
1060 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[4] ) );
1063 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
1066 if( ACL_IS_INVALID( b->a_access_mask ) ) {
1068 "%s: line %d: expecting <access> got \"%s\"\n",
1069 fname, lineno, left );
1073 b->a_type = ACL_STOP;
1076 /* out of arguments or plain stop */
1077 access_append( &a->acl_access, b );
1081 if( strcasecmp( argv[i], "continue" ) == 0 ) {
1082 /* plain continue */
1083 b->a_type = ACL_CONTINUE;
1085 } else if( strcasecmp( argv[i], "break" ) == 0 ) {
1086 /* plain continue */
1087 b->a_type = ACL_BREAK;
1089 } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
1094 access_append( &a->acl_access, b );
1098 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
1099 fname, lineno, argv[i] );
1104 /* if we have no real access clause, complain and do nothing */
1107 "%s: line %d: warning: no access clause(s) specified in access line\n",
1112 if (ldap_debug & LDAP_DEBUG_ACL)
1116 if ( a->acl_access == NULL ) {
1118 "%s: line %d: warning: no by clause(s) specified in access line\n",
1123 acl_append( &be->be_acl, a );
1125 acl_append( &global_acl, a );
1131 accessmask2str( slap_mask_t mask, char *buf )
1136 assert( buf != NULL );
1138 if ( ACL_IS_INVALID( mask ) ) {
1144 if ( ACL_IS_LEVEL( mask ) ) {
1145 if ( ACL_LVL_IS_NONE(mask) ) {
1146 ptr = lutil_strcopy( ptr, "none" );
1148 } else if ( ACL_LVL_IS_AUTH(mask) ) {
1149 ptr = lutil_strcopy( ptr, "auth" );
1151 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
1152 ptr = lutil_strcopy( ptr, "compare" );
1154 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
1155 ptr = lutil_strcopy( ptr, "search" );
1157 } else if ( ACL_LVL_IS_READ(mask) ) {
1158 ptr = lutil_strcopy( ptr, "read" );
1160 } else if ( ACL_LVL_IS_WRITE(mask) ) {
1161 ptr = lutil_strcopy( ptr, "write" );
1163 ptr = lutil_strcopy( ptr, "unknown" );
1169 if( ACL_IS_ADDITIVE( mask ) ) {
1172 } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
1179 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
1184 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
1189 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
1194 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
1199 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
1204 if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
1213 if ( ACL_IS_LEVEL( mask ) ) {
1223 str2accessmask( const char *str )
1227 if( !ASCII_ALPHA(str[0]) ) {
1230 if ( str[0] == '=' ) {
1233 } else if( str[0] == '+' ) {
1234 ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1236 } else if( str[0] == '-' ) {
1237 ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1240 ACL_INVALIDATE(mask);
1244 for( i=1; str[i] != '\0'; i++ ) {
1245 if( TOLOWER((unsigned char) str[i]) == 'w' ) {
1246 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1248 } else if( TOLOWER((unsigned char) str[i]) == 'r' ) {
1249 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1251 } else if( TOLOWER((unsigned char) str[i]) == 's' ) {
1252 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1254 } else if( TOLOWER((unsigned char) str[i]) == 'c' ) {
1255 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1257 } else if( TOLOWER((unsigned char) str[i]) == 'x' ) {
1258 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1260 } else if( str[i] != '0' ) {
1261 ACL_INVALIDATE(mask);
1269 if ( strcasecmp( str, "none" ) == 0 ) {
1270 ACL_LVL_ASSIGN_NONE(mask);
1272 } else if ( strcasecmp( str, "auth" ) == 0 ) {
1273 ACL_LVL_ASSIGN_AUTH(mask);
1275 } else if ( strcasecmp( str, "compare" ) == 0 ) {
1276 ACL_LVL_ASSIGN_COMPARE(mask);
1278 } else if ( strcasecmp( str, "search" ) == 0 ) {
1279 ACL_LVL_ASSIGN_SEARCH(mask);
1281 } else if ( strcasecmp( str, "read" ) == 0 ) {
1282 ACL_LVL_ASSIGN_READ(mask);
1284 } else if ( strcasecmp( str, "write" ) == 0 ) {
1285 ACL_LVL_ASSIGN_WRITE(mask);
1288 ACL_INVALIDATE( mask );
1297 fprintf( stderr, "%s%s\n",
1298 "<access clause> ::= access to <what> "
1299 "[ by <who> <access> [ <control> ] ]+ \n"
1300 "<what> ::= * | [dn[.<dnstyle>]=<DN>] [filter=<filter>] [attrs=<attrlist>]\n"
1301 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
1302 "<attr> ::= <attrname> | entry | children\n"
1303 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<DN> ]\n"
1304 "\t[dnattr=<attrname>]\n"
1305 "\t[group[/<objectclass>[/<attrname>]][.<style>]=<group>]\n"
1306 "\t[peername[.<style>]=<peer>] [sockname[.<style>]=<name>]\n",
1307 "\t[domain[.<style>]=<domain>] [sockurl[.<style>]=<url>]\n"
1308 #ifdef SLAPD_ACI_ENABLED
1309 "\t[aci=<attrname>]\n"
1311 "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
1312 "<dnstyle> ::= base | exact | one | subtree | children | regex\n"
1313 "<style> ::= regex | base | exact\n"
1314 "<access> ::= [self]{<level>|<priv>}\n"
1315 "<level> ::= none | auth | compare | search | read | write\n"
1316 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
1317 "<control> ::= [ stop | continue | break ]\n"
1319 exit( EXIT_FAILURE );
1323 * Set pattern to a "normalized" DN from src.
1324 * At present it simply eats the (optional) space after
1325 * a RDN separator (,)
1326 * Eventually will evolve in a more complete normalization
1329 acl_regex_normalized_dn(
1331 struct berval *pattern
1337 str = ch_strdup( src );
1338 len = strlen( src );
1340 for ( p = str; p && p[ 0 ]; p++ ) {
1342 if ( p[ 0 ] == '\\' && p[ 1 ] ) {
1344 * if escaping a hex pair we should
1345 * increment p twice; however, in that
1346 * case the second hex number does
1352 if ( p[ 0 ] == ',' ) {
1353 if ( p[ 1 ] == ' ' ) {
1357 * too much space should be
1358 * an error if we are pedantic
1360 for ( q = &p[ 2 ]; q[ 0 ] == ' '; q++ ) {
1363 AC_MEMCPY( p+1, q, len-(q-str)+1);
1367 pattern->bv_val = str;
1368 pattern->bv_len = p-str;
1382 if ( (*right = strchr( line, splitchar )) != NULL ) {
1383 *((*right)++) = '\0';
1388 access_append( Access **l, Access *a )
1390 for ( ; *l != NULL; l = &(*l)->a_next )
1397 acl_append( AccessControl **l, AccessControl *a )
1399 for ( ; *l != NULL; l = &(*l)->acl_next )
1406 access_free( Access *a )
1408 if ( a->a_dn_pat.bv_val )
1409 free ( a->a_dn_pat.bv_val );
1410 if ( a->a_peername_pat.bv_val )
1411 free ( a->a_peername_pat.bv_val );
1412 if ( a->a_sockname_pat.bv_val )
1413 free ( a->a_sockname_pat.bv_val );
1414 if ( a->a_domain_pat.bv_val )
1415 free ( a->a_domain_pat.bv_val );
1416 if ( a->a_sockurl_pat.bv_val )
1417 free ( a->a_sockurl_pat.bv_val );
1418 if ( a->a_set_pat.bv_len )
1419 free ( a->a_set_pat.bv_val );
1420 if ( a->a_group_pat.bv_len )
1421 free ( a->a_group_pat.bv_val );
1426 acl_free( AccessControl *a )
1431 if ( a->acl_filter )
1432 filter_free( a->acl_filter );
1433 if ( a->acl_dn_pat.bv_len )
1434 free ( a->acl_dn_pat.bv_val );
1435 if ( a->acl_attrs ) {
1436 for ( an = a->acl_attrs; an->an_name.bv_val; an++ ) {
1437 free( an->an_name.bv_val );
1439 free( a->acl_attrs );
1441 for (; a->acl_access; a->acl_access = n) {
1442 n = a->acl_access->a_next;
1443 access_free( a->acl_access );
1448 /* Because backend_startup uses acl_append to tack on the global_acl to
1449 * the end of each backend's acl, we cannot just take one argument and
1450 * merrily free our way to the end of the list. backend_destroy calls us
1451 * with the be_acl in arg1, and global_acl in arg2 to give us a stopping
1452 * point. config_destroy calls us with global_acl in arg1 and NULL in
1453 * arg2, so we then proceed to polish off the global_acl.
1456 acl_destroy( AccessControl *a, AccessControl *end )
1460 for (; a && a!= end; a=n) {
1467 access2str( slap_access_t access )
1469 if ( access == ACL_NONE ) {
1472 } else if ( access == ACL_AUTH ) {
1475 } else if ( access == ACL_COMPARE ) {
1478 } else if ( access == ACL_SEARCH ) {
1481 } else if ( access == ACL_READ ) {
1484 } else if ( access == ACL_WRITE ) {
1492 str2access( const char *str )
1494 if ( strcasecmp( str, "none" ) == 0 ) {
1497 } else if ( strcasecmp( str, "auth" ) == 0 ) {
1500 } else if ( strcasecmp( str, "compare" ) == 0 ) {
1503 } else if ( strcasecmp( str, "search" ) == 0 ) {
1506 } else if ( strcasecmp( str, "read" ) == 0 ) {
1509 } else if ( strcasecmp( str, "write" ) == 0 ) {
1513 return( ACL_INVALID_ACCESS );
1518 static char *style_strings[5] = { "regex",
1519 "base", "one", "subtree", "children" };
1522 print_access( Access *b )
1524 char maskbuf[ACCESSMASK_MAXLEN];
1526 fprintf( stderr, "\tby" );
1528 if ( b->a_dn_pat.bv_len != 0 ) {
1529 if( strcmp(b->a_dn_pat.bv_val, "*") == 0
1530 || strcmp(b->a_dn_pat.bv_val, "users") == 0
1531 || strcmp(b->a_dn_pat.bv_val, "anonymous") == 0
1532 || strcmp(b->a_dn_pat.bv_val, "self") == 0 )
1534 fprintf( stderr, " %s", b->a_dn_pat.bv_val );
1537 fprintf( stderr, " dn.%s=%s",
1538 style_strings[b->a_dn_style], b->a_dn_pat.bv_val );
1542 if ( b->a_dn_at != NULL ) {
1543 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname.bv_val );
1546 if ( b->a_group_pat.bv_len ) {
1547 fprintf( stderr, " group=%s", b->a_group_pat.bv_val );
1549 if ( b->a_group_oc ) {
1550 fprintf( stderr, " objectClass: %s",
1551 b->a_group_oc->soc_oclass.oc_oid );
1553 if ( b->a_group_at ) {
1554 fprintf( stderr, " attributeType: %s",
1555 b->a_group_at->ad_cname.bv_val );
1560 if ( b->a_peername_pat.bv_len != 0 ) {
1561 fprintf( stderr, " peername=%s", b->a_peername_pat.bv_val );
1564 if ( b->a_sockname_pat.bv_len != 0 ) {
1565 fprintf( stderr, " sockname=%s", b->a_sockname_pat.bv_val );
1568 if ( b->a_domain_pat.bv_len != 0 ) {
1569 fprintf( stderr, " domain=%s", b->a_domain_pat.bv_val );
1572 if ( b->a_sockurl_pat.bv_len != 0 ) {
1573 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat.bv_val );
1576 #ifdef SLAPD_ACI_ENABLED
1577 if ( b->a_aci_at != NULL ) {
1578 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname.bv_val );
1582 /* Security Strength Factors */
1583 if ( b->a_authz.sai_ssf ) {
1584 fprintf( stderr, " ssf=%u",
1585 b->a_authz.sai_ssf );
1587 if ( b->a_authz.sai_transport_ssf ) {
1588 fprintf( stderr, " transport_ssf=%u",
1589 b->a_authz.sai_transport_ssf );
1591 if ( b->a_authz.sai_tls_ssf ) {
1592 fprintf( stderr, " tls_ssf=%u",
1593 b->a_authz.sai_tls_ssf );
1595 if ( b->a_authz.sai_sasl_ssf ) {
1596 fprintf( stderr, " sasl_ssf=%u",
1597 b->a_authz.sai_sasl_ssf );
1600 fprintf( stderr, " %s%s",
1601 b->a_dn_self ? "self" : "",
1602 accessmask2str( b->a_access_mask, maskbuf ) );
1604 if( b->a_type == ACL_BREAK ) {
1605 fprintf( stderr, " break" );
1607 } else if( b->a_type == ACL_CONTINUE ) {
1608 fprintf( stderr, " continue" );
1610 } else if( b->a_type != ACL_STOP ) {
1611 fprintf( stderr, " unknown-control" );
1614 fprintf( stderr, "\n" );
1619 print_acl( Backend *be, AccessControl *a )
1624 fprintf( stderr, "%s ACL: access to",
1625 be == NULL ? "Global" : "Backend" );
1627 if ( a->acl_dn_pat.bv_len != 0 ) {
1629 fprintf( stderr, " dn.%s=%s\n",
1630 style_strings[a->acl_dn_style], a->acl_dn_pat.bv_val );
1633 if ( a->acl_filter != NULL ) {
1634 struct berval bv = { 0, NULL };
1636 filter2bv( a->acl_filter, &bv );
1637 fprintf( stderr, " filter=%s\n", bv.bv_val );
1638 ch_free( bv.bv_val );
1641 if ( a->acl_attrs != NULL ) {
1646 fprintf( stderr, " attrs=" );
1647 for ( an = a->acl_attrs; an && an->an_name.bv_val; an++ ) {
1649 fprintf( stderr, "," );
1651 fputs( an->an_name.bv_val, stderr );
1654 fprintf( stderr, "\n" );
1658 fprintf( stderr, " *\n" );
1661 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1665 fprintf( stderr, "\n" );
1668 #endif /* LDAP_DEBUG */