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(struct acl **l, struct acl *a);
17 static void access_append(struct access **l, struct access *a);
18 static void acl_usage(void);
20 static void print_acl(struct acl *a);
21 static void print_access(struct 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 );
90 char *e, *left, *right;
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 = (struct acl *) ch_calloc( 1, sizeof(struct acl) );
105 for ( ++i; i < argc; i++ ) {
106 if ( strcasecmp( argv[i], "by" ) == 0 ) {
111 if ( strcasecmp( argv[i], "*" ) == 0 ) {
113 if ((e = regcomp( &a->acl_dnre, ".*",
114 REG_EXTENDED|REG_ICASE)))
117 regerror(e, &a->acl_dnre, buf, sizeof(buf));
119 "%s: line %d: regular expression \"%s\" bad because of %s\n",
120 fname, lineno, right, buf );
123 a->acl_dnpat = ch_strdup( ".*" );
127 split( argv[i], '=', &left, &right );
128 if ( right == NULL || *right == '\0' ) {
130 "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
131 fname, lineno, left );
135 if ( strcasecmp( left, "filter" ) == 0 ) {
136 if ( (a->acl_filter = str2filter(
139 "%s: line %d: bad filter \"%s\" in to clause\n",
140 fname, lineno, right );
143 } else if ( strcasecmp( left, "dn" ) == 0 ) {
145 if ((e = regcomp(&a->acl_dnre, right,
146 REG_EXTENDED|REG_ICASE))) {
148 regerror(e, &a->acl_dnre, buf, sizeof(buf));
150 "%s: line %d: regular expression \"%s\" bad because of %s\n",
151 fname, lineno, right, buf );
155 a->acl_dnpat = dn_upcase(ch_strdup( right ));
157 } else if ( strncasecmp( left, "attr", 4 )
161 alist = str2charray( right, "," );
162 charray_merge( &a->acl_attrs, alist );
163 charray_free( alist );
166 "%s: line %d: expecting <what> got \"%s\"\n",
167 fname, lineno, left );
172 /* by clause - select who has what access to entries */
173 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
176 "%s: line %d: to clause required before by clause in access line\n",
181 * by clause consists of <who> and <access>
184 b = (struct access *) ch_calloc( 1,
185 sizeof(struct access) );
189 "%s: line %d: premature eol: expecting <who>\n",
195 split( argv[i], '=', &left, &right );
196 if ( strcasecmp( argv[i], "*" ) == 0 ) {
197 b->a_dnpat = ch_strdup( ".*" );
198 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
199 b->a_dnpat = ch_strdup( "self" );
200 } else if ( strcasecmp( left, "dn" ) == 0 ) {
201 regtest(fname, lineno, right);
202 b->a_dnpat = dn_upcase( ch_strdup( right ) );
203 } else if ( strcasecmp( left, "dnattr" ) == 0 ) {
204 b->a_dnattr = ch_strdup( right );
206 #ifdef SLAPD_ACLGROUPS
207 } else if ( strcasecmp( left, "group" ) == 0 ) {
210 regtest(fname, lineno, right);
212 /* format of string is "group/objectClassValue/groupAttrName"
214 if ((value = strchr(right, '/')) != NULL) {
216 if (value && *value && (name = strchr(value, '/')) != NULL)
220 b->a_group = dn_upcase(ch_strdup( right ));
222 if (value && *value) {
223 b->a_objectclassvalue = ch_strdup(value);
227 b->a_objectclassvalue = ch_strdup("groupOfNames");
230 b->a_groupattrname = ch_strdup(name);
234 b->a_groupattrname = ch_strdup("member");
238 #endif /* SLAPD_ACLGROUPS */
239 } else if ( strcasecmp( left, "domain" ) == 0 ) {
241 regtest(fname, lineno, right);
242 b->a_domainpat = ch_strdup( right );
244 /* normalize the domain */
245 for ( s = b->a_domainpat; *s; s++ ) {
246 *s = TOLOWER( (unsigned char) *s );
248 } else if ( strcasecmp( left, "addr" ) == 0 ) {
249 regtest(fname, lineno, right);
250 b->a_addrpat = ch_strdup( right );
253 "%s: line %d: expecting <who> got \"%s\"\n",
254 fname, lineno, left );
260 "%s: line %d: premature eol: expecting <access>\n",
266 split( argv[i], '=', &left, &right );
267 if ( (b->a_access = str2access( left )) == -1 ) {
269 "%s: line %d: expecting <access> got \"%s\"\n",
270 fname, lineno, left );
273 access_append( &a->acl_access, b );
277 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
278 fname, lineno, argv[i] );
283 /* if we have no real access clause, complain and do nothing */
286 "%s: line %d: warning: no access clause(s) specified in access line\n",
292 if (ldap_debug & LDAP_DEBUG_ACL)
296 if ( a->acl_access == NULL ) {
298 "%s: line %d: warning: no by clause(s) specified in access line\n",
303 acl_append( &be->be_acl, a );
305 acl_append( &global_acl, a );
311 access2str( int access )
315 if ( access & ACL_SELF ) {
316 strcpy( buf, "self" );
321 if ( access & ACL_NONE ) {
322 strcat( buf, "none" );
323 } else if ( access & ACL_COMPARE ) {
324 strcat( buf, "compare" );
325 } else if ( access & ACL_SEARCH ) {
326 strcat( buf, "search" );
327 } else if ( access & ACL_READ ) {
328 strcat( buf, "read" );
329 } else if ( access & ACL_WRITE ) {
330 strcat( buf, "write" );
332 strcat( buf, "unknown" );
339 str2access( char *str )
344 if ( strncasecmp( str, "self", 4 ) == 0 ) {
349 if ( strcasecmp( str, "none" ) == 0 ) {
351 } else if ( strcasecmp( str, "compare" ) == 0 ) {
352 access |= ACL_COMPARE;
353 } else if ( strcasecmp( str, "search" ) == 0 ) {
354 access |= ACL_SEARCH;
355 } else if ( strcasecmp( str, "read" ) == 0 ) {
357 } else if ( strcasecmp( str, "write" ) == 0 ) {
369 fprintf( stderr, "\n<access clause> ::= access to <what> [ by <who> <access> ]+ \n" );
370 fprintf( stderr, "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n" );
371 fprintf( stderr, "<attrlist> ::= <attr> | <attr> , <attrlist>\n" );
372 fprintf( stderr, "<attr> ::= <attrname> | entry | children\n" );
373 fprintf( stderr, "<who> ::= * | self | dn=<regex> | addr=<regex> |\n\tdomain=<regex> | dnattr=<dnattrname>\n" );
374 fprintf( stderr, "<access> ::= [self]{none | compare | search | read | write }\n" );
387 if ( (*right = strchr( line, splitchar )) != NULL ) {
388 *((*right)++) = '\0';
393 access_append( struct access **l, struct access *a )
395 for ( ; *l != NULL; l = &(*l)->a_next )
402 acl_append( struct acl **l, struct acl *a )
404 for ( ; *l != NULL; l = &(*l)->acl_next )
413 print_access( struct access *b )
415 fprintf( stderr, "\tby" );
417 if ( b->a_dnpat != NULL ) {
418 fprintf( stderr, " dn=%s", b->a_dnpat );
419 } else if ( b->a_addrpat != NULL ) {
420 fprintf( stderr, " addr=%s", b->a_addrpat );
421 } else if ( b->a_domainpat != NULL ) {
422 fprintf( stderr, " domain=%s", b->a_domainpat );
423 } else if ( b->a_dnattr != NULL ) {
424 fprintf( stderr, " dnattr=%s", b->a_dnattr );
426 #ifdef SLAPD_ACLGROUPS
427 else if ( b->a_group != NULL ) {
428 fprintf( stderr, " group: %s", b->a_group );
429 if ( b->a_objectclassvalue )
430 fprintf( stderr, " objectClassValue: %s", b->a_objectclassvalue );
431 if ( b->a_groupattrname )
432 fprintf( stderr, " groupAttrName: %s", b->a_groupattrname );
435 fprintf( stderr, "\n" );
439 print_acl( struct acl *a )
445 fprintf( stderr, "NULL\n" );
447 fprintf( stderr, "ACL: access to" );
448 if ( a->acl_filter != NULL ) {
449 fprintf( stderr," filter=" );
450 filter_print( a->acl_filter );
452 if ( a->acl_dnpat != NULL ) {
453 fprintf( stderr, " dn=" );
454 fprintf( stderr, a->acl_dnpat );
456 if ( a->acl_attrs != NULL ) {
459 fprintf( stderr, "\n attrs=" );
460 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
462 fprintf( stderr, "," );
464 fprintf( stderr, a->acl_attrs[i] );
468 fprintf( stderr, "\n" );
469 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
472 fprintf( stderr, "\n" );
475 #endif /* LDAP_DEBUG */