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 if( a->acl_dn_pat != NULL ) {
124 "%s: line %d: dn pattern"
125 " already specified in to clause.\n",
130 a->acl_dn_pat = ch_strdup( "*" );
134 split( argv[i], '=', &left, &right );
136 if ( strcasecmp( left, "dn" ) == 0 ) {
137 if( a->acl_dn_pat != NULL ) {
139 "%s: line %d: dn pattern"
140 " already specified in to clause.\n",
145 if ( right == NULL ) {
147 "%s: line %d: missing \"=\" in \"%s\" in to clause\n",
148 fname, lineno, left );
152 if( *right == '\0' ) {
153 a->acl_dn_pat = ch_strdup("^$");
155 } else if ( strcmp(right, "*") == 0
156 || strcmp(right, ".*") == 0
157 || strcmp(right, ".*$") == 0
158 || strcmp(right, "^.*") == 0
159 || strcmp(right, "^.*$$") == 0
160 || strcmp(right, ".*$$") == 0
161 || strcmp(right, "^.*$$") == 0 )
163 a->acl_dn_pat = ch_strdup( "*" );
166 a->acl_dn_pat = ch_strdup( right );
172 if ( right == NULL || *right == '\0' ) {
174 "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
175 fname, lineno, left );
179 if ( strcasecmp( left, "filter" ) == 0 ) {
180 if ( (a->acl_filter = str2filter(
183 "%s: line %d: bad filter \"%s\" in to clause\n",
184 fname, lineno, right );
188 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
191 alist = str2charray( right, "," );
192 charray_merge( &a->acl_attrs, alist );
193 charray_free( alist );
197 "%s: line %d: expecting <what> got \"%s\"\n",
198 fname, lineno, left );
203 if ( a->acl_dn_pat != NULL && strcmp(a->acl_dn_pat, "*") == 0) {
204 free( a->acl_dn_pat );
205 a->acl_dn_pat = NULL;
208 if( a->acl_dn_pat != NULL ) {
209 int e = regcomp( &a->acl_dn_re, a->acl_dn_pat,
210 REG_EXTENDED | REG_ICASE );
213 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
215 "%s: line %d: regular expression \"%s\" bad because of %s\n",
216 fname, lineno, right, buf );
221 /* by clause - select who has what access to entries */
222 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
225 "%s: line %d: to clause required before by clause in access line\n",
231 * by clause consists of <who> and <access>
234 b = (Access *) ch_calloc( 1, sizeof(Access) );
236 ACL_INVALIDATE( b->a_mask );
240 "%s: line %d: premature eol: expecting <who>\n",
246 for ( ; i < argc; i++ ) {
248 split( argv[i], '=', &left, &right );
250 if ( strcasecmp( argv[i], "*" ) == 0 ) {
251 pat = ch_strdup( "*" );
253 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
254 pat = ch_strdup( "anonymous" );
256 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
257 pat = ch_strdup( "self" );
259 } else if ( strcasecmp( argv[i], "users" ) == 0 ) {
260 pat = ch_strdup( "users" );
262 } else if ( strcasecmp( left, "dn" ) == 0 ) {
263 if( right == NULL ) {
265 pat = ch_strdup( "users" );
267 } else if (*right == '\0' ) {
269 pat = ch_strdup( "anonymous" );
271 } else if ( strcmp( right, "*" ) == 0 ) {
273 /* any or users? any for now */
274 pat = ch_strdup( "users" );
276 } else if ( strcmp( right, ".+" ) == 0
277 || strcmp( right, "^.+" ) == 0
278 || strcmp( right, ".+$" ) == 0
279 || strcmp( right, "^.+$" ) == 0
280 || strcmp( right, ".+$$" ) == 0
281 || strcmp( right, "^.+$$" ) == 0 )
283 pat = ch_strdup( "users" );
285 } else if ( strcmp( right, ".*" ) == 0
286 || strcmp( right, "^.*" ) == 0
287 || strcmp( right, ".*$" ) == 0
288 || strcmp( right, "^.*$" ) == 0
289 || strcmp( right, ".*$$" ) == 0
290 || strcmp( right, "^.*$$" ) == 0 )
292 pat = ch_strdup( "*" );
295 regtest(fname, lineno, right);
296 pat = ch_strdup( right );
304 if( b->a_dn_pat != NULL ) {
306 "%s: line %d: dn pattern already specified.\n",
315 if ( strcasecmp( left, "dnattr" ) == 0 ) {
316 if( b->a_dn_pat != NULL ) {
318 "%s: line %d: dnaddr already specified.\n",
323 b->a_dn_at = ch_strdup( right );
327 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
331 if( b->a_group_pat != NULL ) {
333 "%s: line %d: group pattern already specified.\n",
338 /* format of string is "group/objectClassValue/groupAttrName" */
339 if ((value = strchr(left, '/')) != NULL) {
342 && (name = strchr(value, '/')) != NULL)
348 regtest(fname, lineno, right);
349 b->a_group_pat = ch_strdup( right );
351 if (value && *value) {
352 b->a_group_oc = ch_strdup(value);
355 b->a_group_oc = ch_strdup("groupOfNames");
358 b->a_group_at = ch_strdup(name);
362 b->a_group_at = ch_strdup("member");
368 if ( strcasecmp( left, "peername" ) == 0 ) {
369 if( b->a_peername_pat != NULL ) {
371 "%s: line %d: peername pattern already specified.\n",
376 regtest(fname, lineno, right);
377 b->a_peername_pat = ch_strdup( right );
381 if ( strcasecmp( left, "sockname" ) == 0 ) {
382 if( b->a_sockname_pat != NULL ) {
384 "%s: line %d: sockname pattern already specified.\n",
389 regtest(fname, lineno, right);
390 b->a_sockname_pat = ch_strdup( right );
394 if ( strcasecmp( left, "domain" ) == 0 ) {
395 if( b->a_domain_pat != NULL ) {
397 "%s: line %d: domain pattern already specified.\n",
402 regtest(fname, lineno, right);
403 b->a_domain_pat = ch_strdup( right );
407 if ( strcasecmp( left, "sockurl" ) == 0 ) {
408 if( b->a_sockurl_pat != NULL ) {
410 "%s: line %d: sockurl pattern already specified.\n",
415 regtest(fname, lineno, right);
416 b->a_sockurl_pat = ch_strdup( right );
420 #ifdef SLAPD_ACI_ENABLED
421 if ( strcasecmp( left, "aci" ) == 0 ) {
422 if( b->a_aci_at != NULL ) {
424 "%s: line %d: aci attribute already specified.\n",
429 if ( right != NULL && *right != '\0' )
430 b->a_aci_at = ch_strdup( right );
432 b->a_aci_at = ch_strdup( SLAPD_ACI_DEFAULT_ATTR );
437 if( right != NULL ) {
444 if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) {
445 /* out of arguments or plain stop */
447 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
448 b->a_type = ACL_STOP;
450 access_append( &a->acl_access, b );
454 if( strcasecmp( left, "continue" ) == 0 ) {
457 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
458 b->a_type = ACL_CONTINUE;
460 access_append( &a->acl_access, b );
464 if( strcasecmp( left, "break" ) == 0 ) {
467 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
468 b->a_type = ACL_BREAK;
470 access_append( &a->acl_access, b );
474 if ( strcasecmp( left, "by" ) == 0 ) {
475 /* we've gone too far */
477 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
478 b->a_type = ACL_STOP;
480 access_append( &a->acl_access, b );
485 if( strncasecmp( left, "self", 4 ) == 0 ) {
487 ACL_PRIV_ASSIGN( b->a_mask, str2accessmask( &left[4] ) );
490 ACL_PRIV_ASSIGN( b->a_mask, str2accessmask( left ) );
493 if( ACL_IS_INVALID( b->a_mask ) ) {
495 "%s: line %d: expecting <access> got \"%s\"\n",
496 fname, lineno, left );
500 b->a_type = ACL_STOP;
503 /* out of arguments or plain stop */
504 access_append( &a->acl_access, b );
508 if( strcasecmp( argv[i], "continue" ) == 0 ) {
510 b->a_type = ACL_CONTINUE;
512 } else if( strcasecmp( argv[i], "break" ) == 0 ) {
514 b->a_type = ACL_BREAK;
516 } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
521 access_append( &a->acl_access, b );
525 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
526 fname, lineno, argv[i] );
531 /* if we have no real access clause, complain and do nothing */
534 "%s: line %d: warning: no access clause(s) specified in access line\n",
539 if (ldap_debug & LDAP_DEBUG_ACL)
543 if ( a->acl_access == NULL ) {
545 "%s: line %d: warning: no by clause(s) specified in access line\n",
550 acl_append( &be->be_acl, a );
552 acl_append( &global_acl, a );
558 accessmask2str( slap_access_mask_t mask, char *buf )
562 assert( buf != NULL );
564 if ( ACL_IS_INVALID( mask ) ) {
570 if ( ACL_IS_LEVEL( mask ) ) {
571 if ( ACL_LVL_IS_NONE(mask) ) {
572 strcat( buf, "none" );
574 } else if ( ACL_LVL_IS_AUTH(mask) ) {
575 strcat( buf, "auth" );
577 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
578 strcat( buf, "compare" );
580 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
581 strcat( buf, "search" );
583 } else if ( ACL_LVL_IS_READ(mask) ) {
584 strcat( buf, "read" );
586 } else if ( ACL_LVL_IS_WRITE(mask) ) {
587 strcat( buf, "write" );
589 strcat( buf, "unknown" );
595 if( ACL_IS_ADDITIVE( mask ) ) {
598 } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
605 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
610 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
615 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
620 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
625 if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
630 if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
639 if ( ACL_IS_LEVEL( mask ) ) {
646 str2accessmask( const char *str )
648 slap_access_mask_t mask;
650 if( !isalpha(str[0]) ) {
653 if ( str[0] == '=' ) {
656 } else if( str[0] == '+' ) {
657 ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
659 } else if( str[0] == '-' ) {
660 ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
663 ACL_INVALIDATE(mask);
667 for( i=1; str[i] != '\0'; i++ ) {
668 if( TOLOWER(str[i]) == 'w' ) {
669 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
671 } else if( TOLOWER(str[i]) == 'r' ) {
672 ACL_PRIV_SET(mask, ACL_PRIV_READ);
674 } else if( TOLOWER(str[i]) == 's' ) {
675 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
677 } else if( TOLOWER(str[i]) == 'c' ) {
678 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
680 } else if( TOLOWER(str[i]) == 'x' ) {
681 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
683 } else if( str[i] != '0' ) {
684 ACL_INVALIDATE(mask);
692 if ( strcasecmp( str, "none" ) == 0 ) {
693 ACL_LVL_ASSIGN_NONE(mask);
695 } else if ( strcasecmp( str, "auth" ) == 0 ) {
696 ACL_LVL_ASSIGN_AUTH(mask);
698 } else if ( strcasecmp( str, "compare" ) == 0 ) {
699 ACL_LVL_ASSIGN_COMPARE(mask);
701 } else if ( strcasecmp( str, "search" ) == 0 ) {
702 ACL_LVL_ASSIGN_SEARCH(mask);
704 } else if ( strcasecmp( str, "read" ) == 0 ) {
705 ACL_LVL_ASSIGN_READ(mask);
707 } else if ( strcasecmp( str, "write" ) == 0 ) {
708 ACL_LVL_ASSIGN_WRITE(mask);
711 ACL_INVALIDATE( mask );
720 fprintf( stderr, "\n"
721 "<access clause> ::= access to <what> "
722 "[ by <who> <access> <control> ]+ \n"
723 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
724 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
725 "<attr> ::= <attrname> | entry | children\n"
726 "<who> ::= [ * | anonymous | users | self | dn=<regex> ]\n"
727 "\t[dnattr=<attrname>]\n"
728 "\t[group[/<objectclass>[/<attrname>]]=<regex>]\n"
729 "\t[peername=<regex>] [sockname=<regex>]\n"
730 "\t[domain=<regex>] [sockurl=<regex>]\n"
731 #ifdef SLAPD_ACI_ENABLED
732 "\t[aci=<attrname>]\n"
734 "<access> ::= [self]{<level>|<priv>}\n"
735 "<level> ::= none | auth | compare | search | read | write\n"
736 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
737 "<control> ::= [ stop | continue | break ]\n"
739 exit( EXIT_FAILURE );
751 if ( (*right = strchr( line, splitchar )) != NULL ) {
752 *((*right)++) = '\0';
757 access_append( Access **l, Access *a )
759 for ( ; *l != NULL; l = &(*l)->a_next )
766 acl_append( AccessControl **l, AccessControl *a )
768 for ( ; *l != NULL; l = &(*l)->acl_next )
775 access2str( slap_access_t access )
777 if ( access == ACL_NONE ) {
780 } else if ( access == ACL_AUTH ) {
783 } else if ( access == ACL_COMPARE ) {
786 } else if ( access == ACL_SEARCH ) {
789 } else if ( access == ACL_READ ) {
792 } else if ( access == ACL_WRITE ) {
800 str2access( const char *str )
802 if ( strcasecmp( str, "none" ) == 0 ) {
805 } else if ( strcasecmp( str, "auth" ) == 0 ) {
808 } else if ( strcasecmp( str, "compare" ) == 0 ) {
811 } else if ( strcasecmp( str, "search" ) == 0 ) {
814 } else if ( strcasecmp( str, "read" ) == 0 ) {
817 } else if ( strcasecmp( str, "write" ) == 0 ) {
821 return( ACL_INVALID_ACCESS );
827 print_access( Access *b )
829 char maskbuf[ACCESSMASK_MAXLEN];
831 fprintf( stderr, "\tby" );
833 if ( b->a_dn_pat != NULL ) {
834 if( strcmp(b->a_dn_pat, "*") == 0
835 || strcmp(b->a_dn_pat, "users") == 0
836 || strcmp(b->a_dn_pat, "anonymous") == 0
837 || strcmp(b->a_dn_pat, "self") == 0 )
839 fprintf( stderr, " %s", b->a_dn_pat );
842 fprintf( stderr, " dn=%s", b->a_dn_pat );
846 if ( b->a_dn_at != NULL ) {
847 fprintf( stderr, " dnattr=%s", b->a_dn_at );
850 if ( b->a_group_pat != NULL ) {
851 fprintf( stderr, " group: %s", b->a_group_pat );
853 if ( b->a_group_oc ) {
854 fprintf( stderr, " objectClass: %s", b->a_group_oc );
856 if ( b->a_group_at ) {
857 fprintf( stderr, " attributeType: %s", b->a_group_at );
862 if ( b->a_peername_pat != NULL ) {
863 fprintf( stderr, " peername=%s", b->a_peername_pat );
866 if ( b->a_sockname_pat != NULL ) {
867 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
870 if ( b->a_domain_pat != NULL ) {
871 fprintf( stderr, " domain=%s", b->a_domain_pat );
874 if ( b->a_sockurl_pat != NULL ) {
875 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
878 #ifdef SLAPD_ACI_ENABLED
879 if ( b->a_aci_at != NULL ) {
880 fprintf( stderr, " aci=%s", b->a_aci_at );
884 fprintf( stderr, " %s%s",
885 b->a_dn_self ? "self" : "",
886 accessmask2str( b->a_mask, maskbuf ) );
888 if( b->a_type == ACL_BREAK ) {
889 fprintf( stderr, " break" );
891 } else if( b->a_type == ACL_CONTINUE ) {
892 fprintf( stderr, " continue" );
894 } else if( b->a_type != ACL_STOP ) {
895 fprintf( stderr, " unknown-control" );
898 fprintf( stderr, "\n" );
903 print_acl( Backend *be, AccessControl *a )
908 fprintf( stderr, "%s ACL: access to",
909 be == NULL ? "Global" : "Backend" );
911 if ( a->acl_dn_pat != NULL ) {
913 fprintf( stderr, " dn=%s\n",
917 if ( a->acl_filter != NULL ) {
919 fprintf( stderr, " filter=" );
920 filter_print( a->acl_filter );
921 fprintf( stderr, "\n" );
924 if ( a->acl_attrs != NULL ) {
928 fprintf( stderr, " attrs=" );
929 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
931 fprintf( stderr, "," );
933 fprintf( stderr, a->acl_attrs[i] );
936 fprintf( stderr, "\n" );
940 fprintf( stderr, " *\n" );
943 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
947 fprintf( stderr, "\n" );
950 #endif /* LDAP_DEBUG */