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 acl_append(AccessControl **l, AccessControl *a);
22 static void access_append(Access **l, Access *a);
23 static void acl_usage(void) LDAP_GCCATTR((noreturn));
25 static void print_acl(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",
183 * by clause consists of <who> and <access>
186 b = (Access *) ch_calloc( 1, sizeof(Access) );
190 "%s: line %d: premature eol: expecting <who>\n",
196 for ( ; i < argc; i++ ) {
198 split( argv[i], '=', &left, &right );
200 if ( strcasecmp( argv[i], "*" ) == 0 ) {
201 pat = ch_strdup( ".*" );
202 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
203 pat = ch_strdup( "anonymous" );
204 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
205 pat = ch_strdup( "self" );
206 } else if ( strcasecmp( left, "dn" ) == 0 ) {
207 regtest(fname, lineno, right);
208 pat = ch_strdup( right );
214 if( b->a_dn_pat != NULL ) {
216 "%s: line %d: dn pattern already specified.\n",
225 if ( strcasecmp( left, "dnattr" ) == 0 ) {
226 if( b->a_dn_pat != NULL ) {
228 "%s: line %d: dnaddr already specified.\n",
233 b->a_dn_at = ch_strdup( right );
237 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
241 if( b->a_group_pat != NULL ) {
243 "%s: line %d: group pattern already specified.\n",
248 /* format of string is "group/objectClassValue/groupAttrName" */
249 if ((value = strchr(left, '/')) != NULL) {
252 && (name = strchr(value, '/')) != NULL)
258 regtest(fname, lineno, right);
259 b->a_group_pat = ch_strdup( right );
261 if (value && *value) {
262 b->a_group_oc = ch_strdup(value);
265 b->a_group_oc = ch_strdup("groupOfNames");
268 b->a_group_at = ch_strdup(name);
272 b->a_group_at = ch_strdup("member");
278 if ( strcasecmp( left, "peername" ) == 0 ) {
279 if( b->a_peername_pat != NULL ) {
281 "%s: line %d: peername pattern already specified.\n",
286 regtest(fname, lineno, right);
287 b->a_peername_pat = ch_strdup( right );
291 if ( strcasecmp( left, "sockname" ) == 0 ) {
292 if( b->a_sockname_pat != NULL ) {
294 "%s: line %d: sockname pattern already specified.\n",
299 regtest(fname, lineno, right);
300 b->a_sockname_pat = ch_strdup( right );
304 if ( strcasecmp( left, "domain" ) == 0 ) {
305 if( b->a_domain_pat != NULL ) {
307 "%s: line %d: domain pattern already specified.\n",
312 regtest(fname, lineno, right);
313 b->a_domain_pat = ch_strdup( right );
317 if ( strcasecmp( left, "sockurl" ) == 0 ) {
318 if( b->a_sockurl_pat != NULL ) {
320 "%s: line %d: sockurl pattern already specified.\n",
325 regtest(fname, lineno, right);
326 b->a_sockurl_pat = ch_strdup( right );
330 #ifdef SLAPD_ACI_ENABLED
331 if ( strcasecmp( left, "aci" ) == 0 ) {
332 if( b->a_aci_at != NULL ) {
334 "%s: line %d: aci attribute already specified.\n",
339 if ( right != NULL && *right != '\0' )
340 b->a_aci_at = ch_strdup( right );
342 b->a_aci_at = ch_strdup( SLAPD_ACI_DEFAULT_ATTR );
348 if ( ACL_IS_INVALID(ACL_SET(b->a_access, str2access( left ))) ) {
350 "%s: line %d: expecting <access> got \"%s\"\n",
351 fname, lineno, left );
354 access_append( &a->acl_access, b );
359 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
360 fname, lineno, argv[i] );
365 /* if we have no real access clause, complain and do nothing */
368 "%s: line %d: warning: no access clause(s) specified in access line\n",
374 if (ldap_debug & LDAP_DEBUG_ACL)
378 if ( a->acl_access == NULL ) {
380 "%s: line %d: warning: no by clause(s) specified in access line\n",
385 acl_append( &be->be_acl, a );
387 acl_append( &global_acl, a );
393 access2str( int access )
397 if ( ACL_IS_SELF( access ) ) {
398 strcpy( buf, "self" );
403 if ( ACL_IS_NONE(access) ) {
404 strcat( buf, "none" );
405 } else if ( ACL_IS_AUTH(access) ) {
406 strcat( buf, "auth" );
407 } else if ( ACL_IS_COMPARE(access) ) {
408 strcat( buf, "compare" );
409 } else if ( ACL_IS_SEARCH(access) ) {
410 strcat( buf, "search" );
411 } else if ( ACL_IS_READ(access) ) {
412 strcat( buf, "read" );
413 } else if ( ACL_IS_WRITE(access) ) {
414 strcat( buf, "write" );
417 strcat( buf, "unknown" );
424 str2access( char *str )
430 if ( strncasecmp( str, "self", 4 ) == 0 ) {
431 ACL_SET_SELF(access);
435 if ( strcasecmp( str, "none" ) == 0 ) {
436 ACL_SET_NONE(access);
437 } else if ( strcasecmp( str, "auth" ) == 0 ) {
438 ACL_SET_AUTH(access);
439 } else if ( strcasecmp( str, "compare" ) == 0 ) {
440 ACL_SET_COMPARE(access);
441 } else if ( strcasecmp( str, "search" ) == 0 ) {
442 ACL_SET_SEARCH(access);
443 } else if ( strcasecmp( str, "read" ) == 0 ) {
444 ACL_SET_READ(access);
445 } else if ( strcasecmp( str, "write" ) == 0 ) {
446 ACL_SET_WRITE(access);
448 ACL_SET_INVALID(access);
457 fprintf( stderr, "\n"
458 "<access clause> ::= access to <what> [ by <who> <access> ]+ \n"
459 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
460 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
461 "<attr> ::= <attrname> | entry | children\n"
462 "<who> ::= [ * | anonymous | self | dn=<regex> ]\n"
463 "\t[dnattr=<attrname>]\n"
464 "\t[group[/<objectclass>[/<attrname>]]=<regex>]\n"
465 "\t[peername=<regex>] [sockname=<regex>]\n"
466 "\t[domain=<regex>] [sockurl=<regex>]\n"
467 #ifdef SLAPD_ACI_ENABLED
468 "\t[aci=<attrname>]\n"
470 "<access> ::= [self]{none|auth|compare|search|read|write}\n"
472 exit( EXIT_FAILURE );
484 if ( (*right = strchr( line, splitchar )) != NULL ) {
485 *((*right)++) = '\0';
490 access_append( Access **l, Access *a )
492 for ( ; *l != NULL; l = &(*l)->a_next )
499 acl_append( AccessControl **l, AccessControl *a )
501 for ( ; *l != NULL; l = &(*l)->acl_next )
510 print_access( Access *b )
512 fprintf( stderr, "\tby" );
514 if ( b->a_dn_pat != NULL ) {
515 if( strcmp(b->a_dn_pat, "anonymous") == 0 ) {
516 fprintf( stderr, " anonymous" );
518 } else if( strcmp(b->a_dn_pat, "self") == 0 ) {
519 fprintf( stderr, " self" );
522 fprintf( stderr, " dn=%s", b->a_dn_pat );
526 if ( b->a_dn_at != NULL ) {
527 fprintf( stderr, " dnattr=%s", b->a_dn_at );
530 if ( b->a_group_pat != NULL ) {
531 fprintf( stderr, " group: %s", b->a_group_pat );
533 if ( b->a_group_oc ) {
534 fprintf( stderr, " objectClass: %s", b->a_group_oc );
536 if ( b->a_group_at ) {
537 fprintf( stderr, " attributeType: %s", b->a_group_at );
542 if ( b->a_peername_pat != NULL ) {
543 fprintf( stderr, " peername=%s", b->a_peername_pat );
545 if ( b->a_sockname_pat != NULL ) {
546 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
549 if ( b->a_domain_pat != NULL ) {
550 fprintf( stderr, " domain=%s", b->a_domain_pat );
553 if ( b->a_sockurl_pat != NULL ) {
554 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
557 #ifdef SLAPD_ACI_ENABLED
558 if ( b->a_aci_at != NULL ) {
559 fprintf( stderr, " aci=%s", b->a_aci_at );
563 fprintf( stderr, "\n" );
567 print_acl( AccessControl *a )
573 fprintf( stderr, "NULL\n" );
575 fprintf( stderr, "ACL: access to" );
576 if ( a->acl_filter != NULL ) {
577 fprintf( stderr," filter=" );
578 filter_print( a->acl_filter );
580 if ( a->acl_dn_pat != NULL ) {
581 fprintf( stderr, " dn=" );
582 fprintf( stderr, a->acl_dn_pat );
584 if ( a->acl_attrs != NULL ) {
587 fprintf( stderr, "\n attrs=" );
588 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
590 fprintf( stderr, "," );
592 fprintf( stderr, a->acl_attrs[i] );
596 fprintf( stderr, "\n" );
597 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
600 fprintf( stderr, "\n" );
603 #endif /* LDAP_DEBUG */