1 /* acl.c - routines to parse and check acl's */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
19 static void split(char *line, int splitchar, char **left, char **right);
20 static void acl_append(AccessControl **l, AccessControl *a);
21 static void access_append(Access **l, Access *a);
22 static void acl_usage(void) LDAP_GCCATTR((noreturn));
24 static void print_acl(AccessControl *a);
25 static void print_access(Access *b);
29 regtest(const char *fname, int lineno, char *pat) {
45 for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
47 if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
64 if ( size >= (sizeof(buf)-1) ) {
66 "%s: line %d: regular expression \"%s\" too large\n",
71 if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
73 regerror(e, &re, error, sizeof(error));
75 "%s: line %d: regular expression \"%s\" bad because of %s\n",
76 fname, lineno, pat, error );
99 for ( i = 1; i < argc; i++ ) {
100 /* to clause - select which entries are protected */
101 if ( strcasecmp( argv[i], "to" ) == 0 ) {
104 "%s: line %d: only one to clause allowed in access line\n",
108 a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
109 a->acl_filter = NULL;
110 a->acl_dn_pat = NULL;
112 a->acl_access = NULL;
114 for ( ++i; i < argc; i++ ) {
115 if ( strcasecmp( argv[i], "by" ) == 0 ) {
120 if ( strcasecmp( argv[i], "*" ) == 0 ) {
121 a->acl_dn_pat = ch_strdup( ".*" );
125 split( argv[i], '=', &left, &right );
126 if ( right == NULL || *right == '\0' ) {
128 "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
129 fname, lineno, left );
133 if ( strcasecmp( left, "filter" ) == 0 ) {
134 if ( (a->acl_filter = str2filter(
137 "%s: line %d: bad filter \"%s\" in to clause\n",
138 fname, lineno, right );
142 } else if ( strcasecmp( left, "dn" ) == 0 ) {
143 a->acl_dn_pat = ch_strdup( right );
145 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
148 alist = str2charray( right, "," );
149 charray_merge( &a->acl_attrs, alist );
150 charray_free( alist );
154 "%s: line %d: expecting <what> got \"%s\"\n",
155 fname, lineno, left );
160 if ( a->acl_dn_pat != NULL ) {
161 int e = regcomp( &a->acl_dn_re, a->acl_dn_pat,
162 REG_EXTENDED | REG_ICASE );
165 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
167 "%s: line %d: regular expression \"%s\" bad because of %s\n",
168 fname, lineno, right, buf );
173 /* by clause - select who has what access to entries */
174 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
177 "%s: line %d: to clause required before by clause in access line\n",
182 * by clause consists of <who> and <access>
185 b = (Access *) ch_calloc( 1, sizeof(Access) );
189 "%s: line %d: premature eol: expecting <who>\n",
195 for ( ; i < argc; i++ ) {
197 split( argv[i], '=', &left, &right );
199 if ( strcasecmp( argv[i], "*" ) == 0 ) {
200 pat = ch_strdup( ".*" );
201 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
202 pat = ch_strdup( "anonymous" );
203 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
204 pat = ch_strdup( "self" );
205 } else if ( strcasecmp( left, "dn" ) == 0 ) {
206 regtest(fname, lineno, right);
207 pat = ch_strdup( right );
213 if( b->a_dn_pat != NULL ) {
215 "%s: line %d: dn pattern already specified.\n",
224 if ( strcasecmp( left, "dnattr" ) == 0 ) {
225 if( b->a_dn_pat != NULL ) {
227 "%s: line %d: dnaddr already specified.\n",
232 b->a_dn_at = ch_strdup( right );
236 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
240 if( b->a_group_pat != NULL ) {
242 "%s: line %d: group pattern already specified.\n",
247 /* format of string is "group/objectClassValue/groupAttrName" */
248 if ((value = strchr(left, '/')) != NULL) {
251 && (name = strchr(value, '/')) != NULL)
257 regtest(fname, lineno, right);
258 b->a_group_pat = ch_strdup( right );
260 if (value && *value) {
261 b->a_group_oc = ch_strdup(value);
264 b->a_group_oc = ch_strdup("groupOfNames");
267 b->a_group_at = ch_strdup(name);
271 b->a_group_at = ch_strdup("member");
277 if ( strcasecmp( left, "peername" ) == 0 ) {
278 if( b->a_peername_pat != NULL ) {
280 "%s: line %d: peername pattern already specified.\n",
285 regtest(fname, lineno, right);
286 b->a_peername_pat = ch_strdup( right );
290 if ( strcasecmp( left, "sockname" ) == 0 ) {
291 if( b->a_sockname_pat != NULL ) {
293 "%s: line %d: sockname pattern already specified.\n",
298 regtest(fname, lineno, right);
299 b->a_sockname_pat = ch_strdup( right );
303 if ( strcasecmp( left, "domain" ) == 0 ) {
304 if( b->a_domain_pat != NULL ) {
306 "%s: line %d: domain pattern already specified.\n",
311 regtest(fname, lineno, right);
312 b->a_domain_pat = ch_strdup( right );
316 if ( strcasecmp( left, "sockurl" ) == 0 ) {
317 if( b->a_sockurl_pat != NULL ) {
319 "%s: line %d: sockurl pattern already specified.\n",
324 regtest(fname, lineno, right);
325 b->a_sockurl_pat = ch_strdup( right );
329 #ifdef SLAPD_ACI_ENABLED
330 if ( strcasecmp( left, "aci" ) == 0 ) {
331 if( b->a_aci_at != NULL ) {
333 "%s: line %d: aci attribute already specified.\n",
338 if ( right != NULL && *right != '\0' )
339 b->a_aci_at = ch_strdup( right );
341 b->a_aci_at = ch_strdup( SLAPD_ACI_DEFAULT_ATTR );
347 if ( ACL_IS_INVALID(ACL_SET(b->a_access, str2access( left ))) ) {
349 "%s: line %d: expecting <access> got \"%s\"\n",
350 fname, lineno, left );
353 access_append( &a->acl_access, b );
358 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
359 fname, lineno, argv[i] );
364 /* if we have no real access clause, complain and do nothing */
367 "%s: line %d: warning: no access clause(s) specified in access line\n",
373 if (ldap_debug & LDAP_DEBUG_ACL)
377 if ( a->acl_access == NULL ) {
379 "%s: line %d: warning: no by clause(s) specified in access line\n",
384 acl_append( &be->be_acl, a );
386 acl_append( &global_acl, a );
392 access2str( int access )
396 if ( ACL_IS_SELF( access ) ) {
397 strcpy( buf, "self" );
402 if ( ACL_IS_NONE(access) ) {
403 strcat( buf, "none" );
404 } else if ( ACL_IS_AUTH(access) ) {
405 strcat( buf, "auth" );
406 } else if ( ACL_IS_COMPARE(access) ) {
407 strcat( buf, "compare" );
408 } else if ( ACL_IS_SEARCH(access) ) {
409 strcat( buf, "search" );
410 } else if ( ACL_IS_READ(access) ) {
411 strcat( buf, "read" );
412 } else if ( ACL_IS_WRITE(access) ) {
413 strcat( buf, "write" );
416 strcat( buf, "unknown" );
423 str2access( char *str )
429 if ( strncasecmp( str, "self", 4 ) == 0 ) {
430 ACL_SET_SELF(access);
434 if ( strcasecmp( str, "none" ) == 0 ) {
435 ACL_SET_NONE(access);
436 } else if ( strcasecmp( str, "auth" ) == 0 ) {
437 ACL_SET_AUTH(access);
438 } else if ( strcasecmp( str, "compare" ) == 0 ) {
439 ACL_SET_COMPARE(access);
440 } else if ( strcasecmp( str, "search" ) == 0 ) {
441 ACL_SET_SEARCH(access);
442 } else if ( strcasecmp( str, "read" ) == 0 ) {
443 ACL_SET_READ(access);
444 } else if ( strcasecmp( str, "write" ) == 0 ) {
445 ACL_SET_WRITE(access);
447 ACL_SET_INVALID(access);
456 fprintf( stderr, "\n"
457 "<access clause> ::= access to <what> [ by <who> <access> ]+ \n"
458 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
459 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
460 "<attr> ::= <attrname> | entry | children\n"
461 "<who> ::= [ * | anonymous | self | dn=<regex> ]\n"
462 "\t[dnattr=<attrname>]\n"
463 "\t[group[/<objectclass>[/<attrname>]]=<regex>]\n"
464 "\t[peername=<regex>] [sockname=<regex>]\n"
465 "\t[domain=<regex>] [sockurl=<regex>]\n"
466 #ifdef SLAPD_ACI_ENABLED
467 "\t[aci=<attrname>]\n"
469 "<access> ::= [self]{none|auth|compare|search|read|write}\n"
471 exit( EXIT_FAILURE );
483 if ( (*right = strchr( line, splitchar )) != NULL ) {
484 *((*right)++) = '\0';
489 access_append( Access **l, Access *a )
491 for ( ; *l != NULL; l = &(*l)->a_next )
498 acl_append( AccessControl **l, AccessControl *a )
500 for ( ; *l != NULL; l = &(*l)->acl_next )
509 print_access( Access *b )
511 fprintf( stderr, "\tby" );
513 if ( b->a_dn_pat != NULL ) {
514 if( strcmp(b->a_dn_pat, "anonymous") == 0 ) {
515 fprintf( stderr, " anonymous" );
517 } else if( strcmp(b->a_dn_pat, "self") == 0 ) {
518 fprintf( stderr, " self" );
521 fprintf( stderr, " dn=%s", b->a_dn_pat );
525 if ( b->a_dn_at != NULL ) {
526 fprintf( stderr, " dnattr=%s", b->a_dn_at );
529 if ( b->a_group_pat != NULL ) {
530 fprintf( stderr, " group: %s", b->a_group_pat );
532 if ( b->a_group_oc ) {
533 fprintf( stderr, " objectClass: %s", b->a_group_oc );
535 if ( b->a_group_at ) {
536 fprintf( stderr, " attributeType: %s", b->a_group_at );
541 if ( b->a_peername_pat != NULL ) {
542 fprintf( stderr, " peername=%s", b->a_peername_pat );
544 if ( b->a_sockname_pat != NULL ) {
545 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
548 if ( b->a_domain_pat != NULL ) {
549 fprintf( stderr, " domain=%s", b->a_domain_pat );
552 if ( b->a_sockurl_pat != NULL ) {
553 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
556 #ifdef SLAPD_ACI_ENABLED
557 if ( b->a_aci_at != NULL ) {
558 fprintf( stderr, " aci=%s", b->a_aci_at );
562 fprintf( stderr, "\n" );
566 print_acl( AccessControl *a )
572 fprintf( stderr, "NULL\n" );
574 fprintf( stderr, "ACL: access to" );
575 if ( a->acl_filter != NULL ) {
576 fprintf( stderr," filter=" );
577 filter_print( a->acl_filter );
579 if ( a->acl_dn_pat != NULL ) {
580 fprintf( stderr, " dn=" );
581 fprintf( stderr, a->acl_dn_pat );
583 if ( a->acl_attrs != NULL ) {
586 fprintf( stderr, "\n attrs=" );
587 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
589 fprintf( stderr, "," );
591 fprintf( stderr, a->acl_attrs[i] );
595 fprintf( stderr, "\n" );
596 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
599 fprintf( stderr, "\n" );
602 #endif /* LDAP_DEBUG */