1 /* acl.c - routines to parse and check acl's */
10 #include <ac/string.h>
11 #include <ac/unistd.h>
15 static void split(char *line, int splitchar, char **left, char **right);
16 static void acl_append(AccessControl **l, AccessControl *a);
17 static void access_append(Access **l, Access *a);
18 static void acl_usage(void);
20 static void print_acl(AccessControl *a);
21 static void print_access(Access *b);
25 regtest(char *fname, int lineno, char *pat) {
41 for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
43 if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
60 if ( size >= (sizeof(buf)-1) ) {
62 "%s: line %d: regular expression \"%s\" too large\n",
63 fname, lineno, pat, 0 );
67 if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
69 regerror(e, &re, error, sizeof(error));
71 "%s: line %d: regular expression \"%s\" bad because of %s\n",
72 fname, lineno, pat, error );
95 for ( i = 1; i < argc; i++ ) {
96 /* to clause - select which entries are protected */
97 if ( strcasecmp( argv[i], "to" ) == 0 ) {
100 "%s: line %d: only one to clause allowed in access line\n",
104 a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
105 for ( ++i; i < argc; i++ ) {
106 if ( strcasecmp( argv[i], "by" ) == 0 ) {
111 if ( strcasecmp( argv[i], "*" ) == 0 ) {
112 a->acl_dn_pat = ch_strdup( ".*" );
116 split( argv[i], '=', &left, &right );
117 if ( right == NULL || *right == '\0' ) {
119 "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
120 fname, lineno, left );
124 if ( strcasecmp( left, "filter" ) == 0 ) {
125 if ( (a->acl_filter = str2filter(
128 "%s: line %d: bad filter \"%s\" in to clause\n",
129 fname, lineno, right );
133 } else if ( strcasecmp( left, "dn" ) == 0 ) {
136 if ((e = regcomp(&a->acl_dn_re, right,
137 REG_EXTENDED|REG_ICASE))) {
139 regerror(e, &a->acl_dn_re, buf, sizeof(buf));
141 "%s: line %d: regular expression \"%s\" bad because of %s\n",
142 fname, lineno, right, buf );
146 a->acl_dn_pat = ch_strdup( right );
149 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
152 alist = str2charray( right, "," );
153 charray_merge( &a->acl_attrs, alist );
154 charray_free( alist );
158 "%s: line %d: expecting <what> got \"%s\"\n",
159 fname, lineno, left );
164 /* by clause - select who has what access to entries */
165 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
168 "%s: line %d: to clause required before by clause in access line\n",
173 * by clause consists of <who> and <access>
176 b = (Access *) ch_calloc( 1, sizeof(Access) );
180 "%s: line %d: premature eol: expecting <who>\n",
186 for ( ; i < argc; i++ ) {
188 split( argv[i], '=', &left, &right );
190 if ( strcasecmp( argv[i], "*" ) == 0 ) {
191 pat = ch_strdup( ".*" );
192 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
193 pat = ch_strdup( "anonymous" );
194 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
195 pat = ch_strdup( "self" );
196 } else if ( strcasecmp( left, "dn" ) == 0 ) {
197 regtest(fname, lineno, right);
198 pat = ch_strdup( right );
204 if( b->a_dn_pat != NULL ) {
206 "%s: line %d: dn pattern already specified.\n",
215 if ( strcasecmp( left, "dnattr" ) == 0 ) {
216 if( b->a_dn_pat != NULL ) {
218 "%s: line %d: dnaddr already specified.\n",
223 b->a_dn_at = ch_strdup( right );
227 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
231 if( b->a_group_pat != NULL ) {
233 "%s: line %d: group pattern already specified.\n",
238 /* format of string is "group/objectClassValue/groupAttrName" */
239 if ((value = strchr(left, '/')) != NULL) {
242 && (name = strchr(value, '/')) != NULL)
248 regtest(fname, lineno, right);
249 b->a_group_pat = ch_strdup( right );
251 if (value && *value) {
252 b->a_group_oc = ch_strdup(value);
255 b->a_group_oc = ch_strdup("groupOfNames");
258 b->a_group_at = ch_strdup(name);
262 b->a_group_at = ch_strdup("member");
268 if ( strcasecmp( left, "peername" ) == 0 ) {
269 if( b->a_peername_pat != NULL ) {
271 "%s: line %d: peername pattern already specified.\n",
276 regtest(fname, lineno, right);
277 b->a_peername_pat = ch_strdup( right );
281 if ( strcasecmp( left, "sockname" ) == 0 ) {
282 if( b->a_sockname_pat != NULL ) {
284 "%s: line %d: sockname pattern already specified.\n",
289 regtest(fname, lineno, right);
290 b->a_sockname_pat = ch_strdup( right );
294 if ( strcasecmp( left, "domain" ) == 0 ) {
295 if( b->a_domain_pat != NULL ) {
297 "%s: line %d: domain pattern already specified.\n",
302 regtest(fname, lineno, right);
303 b->a_domain_pat = ch_strdup( right );
307 if ( strcasecmp( left, "sockurl" ) == 0 ) {
308 if( b->a_sockurl_pat != NULL ) {
310 "%s: line %d: sockurl pattern already specified.\n",
315 regtest(fname, lineno, right);
316 b->a_sockurl_pat = ch_strdup( right );
321 if ( ACL_IS_INVALID(ACL_SET(b->a_access, str2access( left ))) ) {
323 "%s: line %d: expecting <access> got \"%s\"\n",
324 fname, lineno, left );
327 access_append( &a->acl_access, b );
332 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
333 fname, lineno, argv[i] );
338 /* if we have no real access clause, complain and do nothing */
341 "%s: line %d: warning: no access clause(s) specified in access line\n",
347 if (ldap_debug & LDAP_DEBUG_ACL)
351 if ( a->acl_access == NULL ) {
353 "%s: line %d: warning: no by clause(s) specified in access line\n",
358 acl_append( &be->be_acl, a );
360 acl_append( &global_acl, a );
366 access2str( int access )
370 if ( ACL_IS_SELF( access ) ) {
371 strcpy( buf, "self" );
376 if ( ACL_IS_NONE(access) ) {
377 strcat( buf, "none" );
378 } else if ( ACL_IS_AUTH(access) ) {
379 strcat( buf, "auth" );
380 } else if ( ACL_IS_COMPARE(access) ) {
381 strcat( buf, "compare" );
382 } else if ( ACL_IS_SEARCH(access) ) {
383 strcat( buf, "search" );
384 } else if ( ACL_IS_READ(access) ) {
385 strcat( buf, "read" );
386 } else if ( ACL_IS_WRITE(access) ) {
387 strcat( buf, "write" );
390 strcat( buf, "unknown" );
397 str2access( char *str )
403 if ( strncasecmp( str, "self", 4 ) == 0 ) {
404 ACL_SET_SELF(access);
408 if ( strcasecmp( str, "none" ) == 0 ) {
409 ACL_SET_NONE(access);
410 } else if ( strcasecmp( str, "auth" ) == 0 ) {
411 ACL_SET_AUTH(access);
412 } else if ( strcasecmp( str, "compare" ) == 0 ) {
413 ACL_SET_COMPARE(access);
414 } else if ( strcasecmp( str, "search" ) == 0 ) {
415 ACL_SET_SEARCH(access);
416 } else if ( strcasecmp( str, "read" ) == 0 ) {
417 ACL_SET_READ(access);
418 } else if ( strcasecmp( str, "write" ) == 0 ) {
419 ACL_SET_WRITE(access);
421 ACL_SET_INVALID(access);
430 fprintf( stderr, "\n"
431 "<access clause> ::= access to <what> [ by <who> <access> ]+ \n"
432 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
433 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
434 "<attr> ::= <attrname> | entry | children\n"
435 "<who> ::= [ * | anonymous | self | dn=<regex> ]\n"
436 "\t[dnattr=<attrname>]\n"
437 "\t[group[/<objectclass>[/<attrname>]]=<regex>]\n"
438 "\t[peername=<regex>] [sockname=<regex>]\n"
439 "\t[domain=<regex>] [sockurl=<regex>]\n"
440 "<access> ::= [self]{none|auth|compare|search|read|write}\n"
454 if ( (*right = strchr( line, splitchar )) != NULL ) {
455 *((*right)++) = '\0';
460 access_append( Access **l, Access *a )
462 for ( ; *l != NULL; l = &(*l)->a_next )
469 acl_append( AccessControl **l, AccessControl *a )
471 for ( ; *l != NULL; l = &(*l)->acl_next )
480 print_access( Access *b )
482 fprintf( stderr, "\tby" );
484 if ( b->a_dn_pat != NULL ) {
485 if( strcmp(b->a_dn_pat, "anonymous") == 0 ) {
486 fprintf( stderr, " anonymous" );
488 } else if( strcmp(b->a_dn_pat, "self") == 0 ) {
489 fprintf( stderr, " self" );
492 fprintf( stderr, " dn=%s", b->a_dn_pat );
496 if ( b->a_dn_at != NULL ) {
497 fprintf( stderr, " dnattr=%s", b->a_dn_at );
500 if ( b->a_group_pat != NULL ) {
501 fprintf( stderr, " group: %s", b->a_group_pat );
503 if ( b->a_group_oc ) {
504 fprintf( stderr, " objectClass: %s", b->a_group_oc );
506 if ( b->a_group_at ) {
507 fprintf( stderr, " attributeType: %s", b->a_group_at );
512 if ( b->a_peername_pat != NULL ) {
513 fprintf( stderr, " peername=%s", b->a_peername_pat );
515 if ( b->a_sockname_pat != NULL ) {
516 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
519 if ( b->a_domain_pat != NULL ) {
520 fprintf( stderr, " domain=%s", b->a_domain_pat );
523 if ( b->a_sockurl_pat != NULL ) {
524 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
527 fprintf( stderr, "\n" );
531 print_acl( AccessControl *a )
537 fprintf( stderr, "NULL\n" );
539 fprintf( stderr, "ACL: access to" );
540 if ( a->acl_filter != NULL ) {
541 fprintf( stderr," filter=" );
542 filter_print( a->acl_filter );
544 if ( a->acl_dn_pat != NULL ) {
545 fprintf( stderr, " dn=" );
546 fprintf( stderr, a->acl_dn_pat );
548 if ( a->acl_attrs != NULL ) {
551 fprintf( stderr, "\n attrs=" );
552 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
554 fprintf( stderr, "," );
556 fprintf( stderr, a->acl_attrs[i] );
560 fprintf( stderr, "\n" );
561 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
564 fprintf( stderr, "\n" );
567 #endif /* LDAP_DEBUG */