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);
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",
67 fname, lineno, pat, 0 );
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 for ( ++i; i < argc; i++ ) {
110 if ( strcasecmp( argv[i], "by" ) == 0 ) {
115 if ( strcasecmp( argv[i], "*" ) == 0 ) {
116 a->acl_dn_pat = ch_strdup( ".*" );
120 split( argv[i], '=', &left, &right );
121 if ( right == NULL || *right == '\0' ) {
123 "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
124 fname, lineno, left );
128 if ( strcasecmp( left, "filter" ) == 0 ) {
129 if ( (a->acl_filter = str2filter(
132 "%s: line %d: bad filter \"%s\" in to clause\n",
133 fname, lineno, right );
137 } else if ( strcasecmp( left, "dn" ) == 0 ) {
140 if ((e = regcomp(&a->acl_dn_re, right,
141 REG_EXTENDED|REG_ICASE))) {
143 regerror(e, &a->acl_dn_re, buf, sizeof(buf));
145 "%s: line %d: regular expression \"%s\" bad because of %s\n",
146 fname, lineno, right, buf );
150 a->acl_dn_pat = ch_strdup( right );
153 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
156 alist = str2charray( right, "," );
157 charray_merge( &a->acl_attrs, alist );
158 charray_free( alist );
162 "%s: line %d: expecting <what> got \"%s\"\n",
163 fname, lineno, left );
168 /* by clause - select who has what access to entries */
169 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
172 "%s: line %d: to clause required before by clause in access line\n",
177 * by clause consists of <who> and <access>
180 b = (Access *) ch_calloc( 1, sizeof(Access) );
184 "%s: line %d: premature eol: expecting <who>\n",
190 for ( ; i < argc; i++ ) {
192 split( argv[i], '=', &left, &right );
194 if ( strcasecmp( argv[i], "*" ) == 0 ) {
195 pat = ch_strdup( ".*" );
196 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
197 pat = ch_strdup( "anonymous" );
198 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
199 pat = ch_strdup( "self" );
200 } else if ( strcasecmp( left, "dn" ) == 0 ) {
201 regtest(fname, lineno, right);
202 pat = ch_strdup( right );
208 if( b->a_dn_pat != NULL ) {
210 "%s: line %d: dn pattern already specified.\n",
219 if ( strcasecmp( left, "dnattr" ) == 0 ) {
220 if( b->a_dn_pat != NULL ) {
222 "%s: line %d: dnaddr already specified.\n",
227 b->a_dn_at = ch_strdup( right );
231 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
235 if( b->a_group_pat != NULL ) {
237 "%s: line %d: group pattern already specified.\n",
242 /* format of string is "group/objectClassValue/groupAttrName" */
243 if ((value = strchr(left, '/')) != NULL) {
246 && (name = strchr(value, '/')) != NULL)
252 regtest(fname, lineno, right);
253 b->a_group_pat = ch_strdup( right );
255 if (value && *value) {
256 b->a_group_oc = ch_strdup(value);
259 b->a_group_oc = ch_strdup("groupOfNames");
262 b->a_group_at = ch_strdup(name);
266 b->a_group_at = ch_strdup("member");
272 if ( strcasecmp( left, "peername" ) == 0 ) {
273 if( b->a_peername_pat != NULL ) {
275 "%s: line %d: peername pattern already specified.\n",
280 regtest(fname, lineno, right);
281 b->a_peername_pat = ch_strdup( right );
285 if ( strcasecmp( left, "sockname" ) == 0 ) {
286 if( b->a_sockname_pat != NULL ) {
288 "%s: line %d: sockname pattern already specified.\n",
293 regtest(fname, lineno, right);
294 b->a_sockname_pat = ch_strdup( right );
298 if ( strcasecmp( left, "domain" ) == 0 ) {
299 if( b->a_domain_pat != NULL ) {
301 "%s: line %d: domain pattern already specified.\n",
306 regtest(fname, lineno, right);
307 b->a_domain_pat = ch_strdup( right );
311 if ( strcasecmp( left, "sockurl" ) == 0 ) {
312 if( b->a_sockurl_pat != NULL ) {
314 "%s: line %d: sockurl pattern already specified.\n",
319 regtest(fname, lineno, right);
320 b->a_sockurl_pat = ch_strdup( right );
325 if ( ACL_IS_INVALID(ACL_SET(b->a_access, str2access( left ))) ) {
327 "%s: line %d: expecting <access> got \"%s\"\n",
328 fname, lineno, left );
331 access_append( &a->acl_access, b );
336 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
337 fname, lineno, argv[i] );
342 /* if we have no real access clause, complain and do nothing */
345 "%s: line %d: warning: no access clause(s) specified in access line\n",
351 if (ldap_debug & LDAP_DEBUG_ACL)
355 if ( a->acl_access == NULL ) {
357 "%s: line %d: warning: no by clause(s) specified in access line\n",
362 acl_append( &be->be_acl, a );
364 acl_append( &global_acl, a );
370 access2str( int access )
374 if ( ACL_IS_SELF( access ) ) {
375 strcpy( buf, "self" );
380 if ( ACL_IS_NONE(access) ) {
381 strcat( buf, "none" );
382 } else if ( ACL_IS_AUTH(access) ) {
383 strcat( buf, "auth" );
384 } else if ( ACL_IS_COMPARE(access) ) {
385 strcat( buf, "compare" );
386 } else if ( ACL_IS_SEARCH(access) ) {
387 strcat( buf, "search" );
388 } else if ( ACL_IS_READ(access) ) {
389 strcat( buf, "read" );
390 } else if ( ACL_IS_WRITE(access) ) {
391 strcat( buf, "write" );
394 strcat( buf, "unknown" );
401 str2access( char *str )
407 if ( strncasecmp( str, "self", 4 ) == 0 ) {
408 ACL_SET_SELF(access);
412 if ( strcasecmp( str, "none" ) == 0 ) {
413 ACL_SET_NONE(access);
414 } else if ( strcasecmp( str, "auth" ) == 0 ) {
415 ACL_SET_AUTH(access);
416 } else if ( strcasecmp( str, "compare" ) == 0 ) {
417 ACL_SET_COMPARE(access);
418 } else if ( strcasecmp( str, "search" ) == 0 ) {
419 ACL_SET_SEARCH(access);
420 } else if ( strcasecmp( str, "read" ) == 0 ) {
421 ACL_SET_READ(access);
422 } else if ( strcasecmp( str, "write" ) == 0 ) {
423 ACL_SET_WRITE(access);
425 ACL_SET_INVALID(access);
434 fprintf( stderr, "\n"
435 "<access clause> ::= access to <what> [ by <who> <access> ]+ \n"
436 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
437 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
438 "<attr> ::= <attrname> | entry | children\n"
439 "<who> ::= [ * | anonymous | self | dn=<regex> ]\n"
440 "\t[dnattr=<attrname>]\n"
441 "\t[group[/<objectclass>[/<attrname>]]=<regex>]\n"
442 "\t[peername=<regex>] [sockname=<regex>]\n"
443 "\t[domain=<regex>] [sockurl=<regex>]\n"
444 "<access> ::= [self]{none|auth|compare|search|read|write}\n"
446 exit( EXIT_FAILURE );
458 if ( (*right = strchr( line, splitchar )) != NULL ) {
459 *((*right)++) = '\0';
464 access_append( Access **l, Access *a )
466 for ( ; *l != NULL; l = &(*l)->a_next )
473 acl_append( AccessControl **l, AccessControl *a )
475 for ( ; *l != NULL; l = &(*l)->acl_next )
484 print_access( Access *b )
486 fprintf( stderr, "\tby" );
488 if ( b->a_dn_pat != NULL ) {
489 if( strcmp(b->a_dn_pat, "anonymous") == 0 ) {
490 fprintf( stderr, " anonymous" );
492 } else if( strcmp(b->a_dn_pat, "self") == 0 ) {
493 fprintf( stderr, " self" );
496 fprintf( stderr, " dn=%s", b->a_dn_pat );
500 if ( b->a_dn_at != NULL ) {
501 fprintf( stderr, " dnattr=%s", b->a_dn_at );
504 if ( b->a_group_pat != NULL ) {
505 fprintf( stderr, " group: %s", b->a_group_pat );
507 if ( b->a_group_oc ) {
508 fprintf( stderr, " objectClass: %s", b->a_group_oc );
510 if ( b->a_group_at ) {
511 fprintf( stderr, " attributeType: %s", b->a_group_at );
516 if ( b->a_peername_pat != NULL ) {
517 fprintf( stderr, " peername=%s", b->a_peername_pat );
519 if ( b->a_sockname_pat != NULL ) {
520 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
523 if ( b->a_domain_pat != NULL ) {
524 fprintf( stderr, " domain=%s", b->a_domain_pat );
527 if ( b->a_sockurl_pat != NULL ) {
528 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
531 fprintf( stderr, "\n" );
535 print_acl( AccessControl *a )
541 fprintf( stderr, "NULL\n" );
543 fprintf( stderr, "ACL: access to" );
544 if ( a->acl_filter != NULL ) {
545 fprintf( stderr," filter=" );
546 filter_print( a->acl_filter );
548 if ( a->acl_dn_pat != NULL ) {
549 fprintf( stderr, " dn=" );
550 fprintf( stderr, a->acl_dn_pat );
552 if ( a->acl_attrs != NULL ) {
555 fprintf( stderr, "\n attrs=" );
556 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
558 fprintf( stderr, "," );
560 fprintf( stderr, a->acl_attrs[i] );
564 fprintf( stderr, "\n" );
565 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
568 fprintf( stderr, "\n" );
571 #endif /* LDAP_DEBUG */