1 /* acl.c - routines to parse and check acl's */
4 * Copyright 1998-1999 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>
20 static void split(char *line, int splitchar, char **left, char **right);
21 static void access_append(Access **l, Access *a);
22 static void acl_usage(void) LDAP_GCCATTR((noreturn));
25 static void print_acl(Backend *be, AccessControl *a);
26 static void print_access(Access *b);
30 regtest(const char *fname, int lineno, char *pat) {
46 for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
48 if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
65 if ( size >= (sizeof(buf)-1) ) {
67 "%s: line %d: regular expression \"%s\" too large\n",
72 if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
74 regerror(e, &re, error, sizeof(error));
76 "%s: line %d: regular expression \"%s\" bad because of %s\n",
77 fname, lineno, pat, error );
100 for ( i = 1; i < argc; i++ ) {
101 /* to clause - select which entries are protected */
102 if ( strcasecmp( argv[i], "to" ) == 0 ) {
105 "%s: line %d: only one to clause allowed in access line\n",
109 a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
110 a->acl_filter = NULL;
111 a->acl_dn_pat = NULL;
113 a->acl_access = NULL;
115 for ( ++i; i < argc; i++ ) {
116 if ( strcasecmp( argv[i], "by" ) == 0 ) {
121 if ( strcasecmp( argv[i], "*" ) == 0 ) {
122 a->acl_dn_pat = ch_strdup( ".*" );
126 split( argv[i], '=', &left, &right );
127 if ( right == NULL || *right == '\0' ) {
129 "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
130 fname, lineno, left );
134 if ( strcasecmp( left, "filter" ) == 0 ) {
135 if ( (a->acl_filter = str2filter(
138 "%s: line %d: bad filter \"%s\" in to clause\n",
139 fname, lineno, right );
143 } else if ( strcasecmp( left, "dn" ) == 0 ) {
144 a->acl_dn_pat = ch_strdup( right );
146 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
149 alist = str2charray( right, "," );
150 charray_merge( &a->acl_attrs, alist );
151 charray_free( alist );
155 "%s: line %d: expecting <what> got \"%s\"\n",
156 fname, lineno, left );
161 if ( a->acl_dn_pat != NULL ) {
162 int e = regcomp( &a->acl_dn_re, a->acl_dn_pat,
163 REG_EXTENDED | REG_ICASE );
166 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
168 "%s: line %d: regular expression \"%s\" bad because of %s\n",
169 fname, lineno, right, buf );
174 /* by clause - select who has what access to entries */
175 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
178 "%s: line %d: to clause required before by clause in access line\n",
184 * by clause consists of <who> and <access>
187 b = (Access *) ch_calloc( 1, sizeof(Access) );
189 ACL_INVALIDATE( b->a_mask );
193 "%s: line %d: premature eol: expecting <who>\n",
199 for ( ; i < argc; i++ ) {
201 split( argv[i], '=', &left, &right );
203 if ( strcasecmp( argv[i], "*" ) == 0 ) {
204 pat = ch_strdup( ".*" );
205 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
206 pat = ch_strdup( "anonymous" );
207 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
208 pat = ch_strdup( "self" );
209 } else if ( strcasecmp( left, "dn" ) == 0 ) {
210 regtest(fname, lineno, right);
211 pat = ch_strdup( right );
217 if( b->a_dn_pat != NULL ) {
219 "%s: line %d: dn pattern already specified.\n",
228 if ( strcasecmp( left, "dnattr" ) == 0 ) {
229 if( b->a_dn_pat != NULL ) {
231 "%s: line %d: dnaddr already specified.\n",
236 b->a_dn_at = ch_strdup( right );
240 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
244 if( b->a_group_pat != NULL ) {
246 "%s: line %d: group pattern already specified.\n",
251 /* format of string is "group/objectClassValue/groupAttrName" */
252 if ((value = strchr(left, '/')) != NULL) {
255 && (name = strchr(value, '/')) != NULL)
261 regtest(fname, lineno, right);
262 b->a_group_pat = ch_strdup( right );
264 if (value && *value) {
265 b->a_group_oc = ch_strdup(value);
268 b->a_group_oc = ch_strdup("groupOfNames");
271 b->a_group_at = ch_strdup(name);
275 b->a_group_at = ch_strdup("member");
281 if ( strcasecmp( left, "peername" ) == 0 ) {
282 if( b->a_peername_pat != NULL ) {
284 "%s: line %d: peername pattern already specified.\n",
289 regtest(fname, lineno, right);
290 b->a_peername_pat = ch_strdup( right );
294 if ( strcasecmp( left, "sockname" ) == 0 ) {
295 if( b->a_sockname_pat != NULL ) {
297 "%s: line %d: sockname pattern already specified.\n",
302 regtest(fname, lineno, right);
303 b->a_sockname_pat = ch_strdup( right );
307 if ( strcasecmp( left, "domain" ) == 0 ) {
308 if( b->a_domain_pat != NULL ) {
310 "%s: line %d: domain pattern already specified.\n",
315 regtest(fname, lineno, right);
316 b->a_domain_pat = ch_strdup( right );
320 if ( strcasecmp( left, "sockurl" ) == 0 ) {
321 if( b->a_sockurl_pat != NULL ) {
323 "%s: line %d: sockurl pattern already specified.\n",
328 regtest(fname, lineno, right);
329 b->a_sockurl_pat = ch_strdup( right );
333 #ifdef SLAPD_ACI_ENABLED
334 if ( strcasecmp( left, "aci" ) == 0 ) {
335 if( b->a_aci_at != NULL ) {
337 "%s: line %d: aci attribute already specified.\n",
342 if ( right != NULL && *right != '\0' )
343 b->a_aci_at = ch_strdup( right );
345 b->a_aci_at = ch_strdup( SLAPD_ACI_DEFAULT_ATTR );
350 if( right != NULL ) {
357 if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) {
358 /* out of arguments or plain stop */
360 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
361 b->a_type = ACL_STOP;
363 access_append( &a->acl_access, b );
367 if( strcasecmp( left, "continue" ) == 0 ) {
370 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
371 b->a_type = ACL_CONTINUE;
373 access_append( &a->acl_access, b );
377 if( strcasecmp( left, "break" ) == 0 ) {
380 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
381 b->a_type = ACL_BREAK;
383 access_append( &a->acl_access, b );
387 if ( strcasecmp( left, "by" ) == 0 ) {
388 /* we've gone too far */
390 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
391 b->a_type = ACL_STOP;
393 access_append( &a->acl_access, b );
398 if( strncasecmp( left, "self", 4 ) == 0 ) {
400 ACL_PRIV_ASSIGN( b->a_mask, str2accessmask( &left[4] ) );
403 ACL_PRIV_ASSIGN( b->a_mask, str2accessmask( left ) );
406 if( ACL_IS_INVALID( b->a_mask ) ) {
408 "%s: line %d: expecting <access> got \"%s\"\n",
409 fname, lineno, left );
413 b->a_type = ACL_STOP;
416 /* out of arguments or plain stop */
417 access_append( &a->acl_access, b );
421 if( strcasecmp( argv[i], "continue" ) == 0 ) {
423 b->a_type = ACL_CONTINUE;
425 } else if( strcasecmp( argv[i], "break" ) == 0 ) {
427 b->a_type = ACL_BREAK;
429 } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
434 access_append( &a->acl_access, b );
438 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
439 fname, lineno, argv[i] );
444 /* if we have no real access clause, complain and do nothing */
447 "%s: line %d: warning: no access clause(s) specified in access line\n",
453 if (ldap_debug & LDAP_DEBUG_ACL)
457 if ( a->acl_access == NULL ) {
459 "%s: line %d: warning: no by clause(s) specified in access line\n",
464 acl_append( &be->be_acl, a );
466 acl_append( &global_acl, a );
472 accessmask2str( slap_access_mask_t mask )
474 static char buf[sizeof("unknown (+wrscan)")];
477 if ( ACL_IS_INVALID( mask ) ) {
483 if ( ACL_IS_LEVEL( mask ) ) {
484 if ( ACL_LVL_IS_NONE(mask) ) {
485 strcat( buf, "none" );
487 } else if ( ACL_LVL_IS_AUTH(mask) ) {
488 strcat( buf, "auth" );
490 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
491 strcat( buf, "compare" );
493 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
494 strcat( buf, "search" );
496 } else if ( ACL_LVL_IS_READ(mask) ) {
497 strcat( buf, "read" );
499 } else if ( ACL_LVL_IS_WRITE(mask) ) {
500 strcat( buf, "write" );
502 strcat( buf, "unknown" );
508 if( ACL_IS_ADDITIVE( mask ) ) {
511 } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
518 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
523 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
528 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
533 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
538 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
543 if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
552 if ( ACL_IS_LEVEL( mask ) ) {
559 str2accessmask( const char *str )
561 slap_access_mask_t mask;
563 if( !isalpha(str[0]) ) {
566 if ( str[0] == '=' ) {
569 } else if( str[0] == '+' ) {
570 ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
572 } else if( str[0] == '-' ) {
573 ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
576 ACL_INVALIDATE(mask);
580 for( i=1; str[i] != '\0'; i++ ) {
581 if( TOLOWER(str[i]) == 'w' ) {
582 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
584 } else if( TOLOWER(str[i]) == 'r' ) {
585 ACL_PRIV_SET(mask, ACL_PRIV_READ);
587 } else if( TOLOWER(str[i]) == 's' ) {
588 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
590 } else if( TOLOWER(str[i]) == 'c' ) {
591 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
593 } else if( TOLOWER(str[i]) == 'x' ) {
594 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
596 } else if( str[i] != '0' ) {
597 ACL_INVALIDATE(mask);
605 if ( strcasecmp( str, "none" ) == 0 ) {
606 ACL_LVL_ASSIGN_NONE(mask);
608 } else if ( strcasecmp( str, "auth" ) == 0 ) {
609 ACL_LVL_ASSIGN_AUTH(mask);
611 } else if ( strcasecmp( str, "compare" ) == 0 ) {
612 ACL_LVL_ASSIGN_COMPARE(mask);
614 } else if ( strcasecmp( str, "search" ) == 0 ) {
615 ACL_LVL_ASSIGN_SEARCH(mask);
617 } else if ( strcasecmp( str, "read" ) == 0 ) {
618 ACL_LVL_ASSIGN_READ(mask);
620 } else if ( strcasecmp( str, "write" ) == 0 ) {
621 ACL_LVL_ASSIGN_WRITE(mask);
624 ACL_INVALIDATE( mask );
633 fprintf( stderr, "\n"
634 "<access clause> ::= access to <what> "
635 "[ by <who> <access> <control> ]+ \n"
636 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
637 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
638 "<attr> ::= <attrname> | entry | children\n"
639 "<who> ::= [ * | anonymous | self | dn=<regex> ]\n"
640 "\t[dnattr=<attrname>]\n"
641 "\t[group[/<objectclass>[/<attrname>]]=<regex>]\n"
642 "\t[peername=<regex>] [sockname=<regex>]\n"
643 "\t[domain=<regex>] [sockurl=<regex>]\n"
644 #ifdef SLAPD_ACI_ENABLED
645 "\t[aci=<attrname>]\n"
647 "<access> ::= [self]{<level>|<priv>}\n"
648 "<level> ::= none | auth | compare | search | read | write\n"
649 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
650 "<control> ::= [ stop | continue | break ]\n"
652 exit( EXIT_FAILURE );
664 if ( (*right = strchr( line, splitchar )) != NULL ) {
665 *((*right)++) = '\0';
670 access_append( Access **l, Access *a )
672 for ( ; *l != NULL; l = &(*l)->a_next )
679 acl_append( AccessControl **l, AccessControl *a )
681 for ( ; *l != NULL; l = &(*l)->acl_next )
690 print_access( Access *b )
692 fprintf( stderr, "\tby" );
694 if ( b->a_dn_pat != NULL ) {
695 if( strcmp(b->a_dn_pat, "anonymous") == 0 ) {
696 fprintf( stderr, " anonymous" );
698 } else if( strcmp(b->a_dn_pat, "self") == 0 ) {
699 fprintf( stderr, " self" );
702 fprintf( stderr, " dn=%s", b->a_dn_pat );
706 if ( b->a_dn_at != NULL ) {
707 fprintf( stderr, " dnattr=%s", b->a_dn_at );
710 if ( b->a_group_pat != NULL ) {
711 fprintf( stderr, " group: %s", b->a_group_pat );
713 if ( b->a_group_oc ) {
714 fprintf( stderr, " objectClass: %s", b->a_group_oc );
716 if ( b->a_group_at ) {
717 fprintf( stderr, " attributeType: %s", b->a_group_at );
722 if ( b->a_peername_pat != NULL ) {
723 fprintf( stderr, " peername=%s", b->a_peername_pat );
726 if ( b->a_sockname_pat != NULL ) {
727 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
730 if ( b->a_domain_pat != NULL ) {
731 fprintf( stderr, " domain=%s", b->a_domain_pat );
734 if ( b->a_sockurl_pat != NULL ) {
735 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
738 #ifdef SLAPD_ACI_ENABLED
739 if ( b->a_aci_at != NULL ) {
740 fprintf( stderr, " aci=%s", b->a_aci_at );
744 fprintf( stderr, " %s%s",
745 b->a_dn_self ? "self" : "",
746 accessmask2str( b->a_mask ) );
748 if( b->a_type == ACL_BREAK ) {
749 fprintf( stderr, " break" );
751 } else if( b->a_type == ACL_CONTINUE ) {
752 fprintf( stderr, " continue" );
754 } else if( b->a_type != ACL_STOP ) {
755 fprintf( stderr, " unknown-control" );
758 fprintf( stderr, "\n" );
762 access2str( slap_access_t access )
764 if ( access == ACL_NONE ) {
767 } else if ( access == ACL_AUTH ) {
770 } else if ( access == ACL_COMPARE ) {
773 } else if ( access == ACL_SEARCH ) {
776 } else if ( access == ACL_READ ) {
779 } else if ( access == ACL_WRITE ) {
787 str2access( const char *str )
789 if ( strcasecmp( str, "none" ) == 0 ) {
792 } else if ( strcasecmp( str, "auth" ) == 0 ) {
795 } else if ( strcasecmp( str, "compare" ) == 0 ) {
798 } else if ( strcasecmp( str, "search" ) == 0 ) {
801 } else if ( strcasecmp( str, "read" ) == 0 ) {
804 } else if ( strcasecmp( str, "write" ) == 0 ) {
808 return( ACL_INVALID_ACCESS );
813 print_acl( Backend *be, AccessControl *a )
818 fprintf( stderr, "%s ACL: access to",
819 be == NULL ? "Global" : "Backend" );
821 if ( a->acl_dn_pat != NULL ) {
823 fprintf( stderr, " dn=%s\n",
827 if ( a->acl_filter != NULL ) {
829 fprintf( stderr, " filter=" );
830 filter_print( a->acl_filter );
831 fprintf( stderr, "\n" );
834 if ( a->acl_attrs != NULL ) {
838 fprintf( stderr, " attrs=" );
839 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
841 fprintf( stderr, "," );
843 fprintf( stderr, a->acl_attrs[i] );
846 fprintf( stderr, "\n" );
850 fprintf( stderr, " *\n" );
853 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
857 fprintf( stderr, "\n" );
860 #endif /* LDAP_DEBUG */