1 /* acl.c - routines to parse and check acl's */
10 #include <ac/string.h>
11 #include <ac/unistd.h>
15 extern Filter *str2filter();
16 extern struct acl *global_acl;
17 extern char **str2charray();
18 extern char *dn_upcase();
21 static void acl_append();
22 static void access_append();
23 static void acl_usage();
25 static void print_acl();
26 static void print_access();
30 regtest(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",
68 fname, lineno, pat, 0 );
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 );
95 char *e, *left, *right;
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 = (struct acl *) ch_calloc( 1, sizeof(struct acl) );
110 for ( ++i; i < argc; i++ ) {
111 if ( strcasecmp( argv[i], "by" ) == 0 ) {
116 if ( strcasecmp( argv[i], "*" ) == 0 ) {
118 if ((e = regcomp( &a->acl_dnre, ".*",
119 REG_EXTENDED|REG_ICASE)))
122 regerror(e, &a->acl_dnre, buf, sizeof(buf));
124 "%s: line %d: regular expression \"%s\" bad because of %s\n",
125 fname, lineno, right, buf );
128 a->acl_dnpat = strdup( ".*" );
132 split( argv[i], '=', &left, &right );
133 if ( right == NULL || *right == '\0' ) {
135 "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
136 fname, lineno, left );
140 if ( strcasecmp( left, "filter" ) == 0 ) {
141 if ( (a->acl_filter = str2filter(
144 "%s: line %d: bad filter \"%s\" in to clause\n",
145 fname, lineno, right );
148 } else if ( strcasecmp( left, "dn" ) == 0 ) {
150 if ((e = regcomp(&a->acl_dnre, right,
151 REG_EXTENDED|REG_ICASE))) {
153 regerror(e, &a->acl_dnre, buf, sizeof(buf));
155 "%s: line %d: regular expression \"%s\" bad because of %s\n",
156 fname, lineno, right, buf );
160 a->acl_dnpat = dn_upcase(strdup( right ));
162 } else if ( strncasecmp( left, "attr", 4 )
166 alist = str2charray( right, "," );
167 charray_merge( &a->acl_attrs, alist );
171 "%s: line %d: expecting <what> got \"%s\"\n",
172 fname, lineno, left );
177 /* by clause - select who has what access to entries */
178 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
181 "%s: line %d: to clause required before by clause in access line\n",
186 * by clause consists of <who> and <access>
189 b = (struct access *) ch_calloc( 1,
190 sizeof(struct access) );
194 "%s: line %d: premature eol: expecting <who>\n",
200 split( argv[i], '=', &left, &right );
201 if ( strcasecmp( argv[i], "*" ) == 0 ) {
202 b->a_dnpat = strdup( ".*" );
203 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
204 b->a_dnpat = strdup( "self" );
205 } else if ( strcasecmp( left, "dn" ) == 0 ) {
206 regtest(fname, lineno, right);
207 b->a_dnpat = dn_upcase( strdup( right ) );
208 } else if ( strcasecmp( left, "dnattr" ) == 0 ) {
209 b->a_dnattr = strdup( right );
211 #ifdef SLAPD_ACLGROUPS
212 } else if ( strcasecmp( left, "group" ) == 0 ) {
213 regtest(fname, lineno, right);
214 b->a_group = dn_upcase(strdup( right ));
215 #endif /* SLAPD_ACLGROUPS */
216 } else if ( strcasecmp( left, "domain" ) == 0 ) {
218 regtest(fname, lineno, right);
219 b->a_domainpat = strdup( right );
221 /* normalize the domain */
222 for ( s = b->a_domainpat; *s; s++ ) {
225 } else if ( strcasecmp( left, "addr" ) == 0 ) {
226 regtest(fname, lineno, right);
227 b->a_addrpat = strdup( right );
230 "%s: line %d: expecting <who> got \"%s\"\n",
231 fname, lineno, left );
237 "%s: line %d: premature eol: expecting <access>\n",
243 split( argv[i], '=', &left, &right );
244 if ( (b->a_access = str2access( left )) == -1 ) {
246 "%s: line %d: expecting <access> got \"%s\"\n",
247 fname, lineno, left );
250 access_append( &a->acl_access, b );
254 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
255 fname, lineno, argv[i] );
260 /* if we have no real access clause, complain and do nothing */
263 "%s: line %d: warning: no access clause(s) specified in access line\n",
268 if ( a->acl_access == NULL ) {
270 "%s: line %d: warning: no by clause(s) specified in access line\n",
275 acl_append( &be->be_acl, a );
277 acl_append( &global_acl, a );
283 access2str( int access )
287 if ( access & ACL_SELF ) {
288 strcpy( buf, "self" );
293 if ( access & ACL_NONE ) {
294 strcat( buf, "none" );
295 } else if ( access & ACL_COMPARE ) {
296 strcat( buf, "compare" );
297 } else if ( access & ACL_SEARCH ) {
298 strcat( buf, "search" );
299 } else if ( access & ACL_READ ) {
300 strcat( buf, "read" );
301 } else if ( access & ACL_WRITE ) {
302 strcat( buf, "write" );
304 strcat( buf, "unknown" );
311 str2access( char *str )
316 if ( strncasecmp( str, "self", 4 ) == 0 ) {
321 if ( strcasecmp( str, "none" ) == 0 ) {
323 } else if ( strcasecmp( str, "compare" ) == 0 ) {
324 access |= ACL_COMPARE;
325 } else if ( strcasecmp( str, "search" ) == 0 ) {
326 access |= ACL_SEARCH;
327 } else if ( strcasecmp( str, "read" ) == 0 ) {
329 } else if ( strcasecmp( str, "write" ) == 0 ) {
341 fprintf( stderr, "\n<access clause> ::= access to <what> [ by <who> <access> ]+ \n" );
342 fprintf( stderr, "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n" );
343 fprintf( stderr, "<attrlist> ::= <attr> | <attr> , <attrlist>\n" );
344 fprintf( stderr, "<attr> ::= <attrname> | entry | children\n" );
345 fprintf( stderr, "<who> ::= * | self | dn=<regex> | addr=<regex> |\n\tdomain=<regex> | dnattr=<dnattrname>\n" );
346 fprintf( stderr, "<access> ::= [self]{none | compare | search | read | write }\n" );
359 if ( (*right = strchr( line, splitchar )) != NULL ) {
360 *((*right)++) = '\0';
365 access_append( struct access **l, struct access *a )
367 for ( ; *l != NULL; l = &(*l)->a_next )
374 acl_append( struct acl **l, struct acl *a )
376 for ( ; *l != NULL; l = &(*l)->acl_next )
385 print_access( struct access *b )
388 if ( b->a_dnpat != NULL ) {
389 printf( " dn=%s", b->a_dnpat );
390 } else if ( b->a_addrpat != NULL ) {
391 printf( " addr=%s", b->a_addrpat );
392 } else if ( b->a_domainpat != NULL ) {
393 printf( " domain=%s", b->a_domainpat );
394 } else if ( b->a_dnattr != NULL ) {
395 printf( " dnattr=%s", b->a_dnattr );
397 printf( " %s\n", access2str( b->a_access ) );
401 print_acl( struct acl *a )
409 printf( "access to" );
410 if ( a->acl_filter != NULL ) {
411 printf( " filter=" );
412 filter_print( a->acl_filter );
414 if ( a->acl_dnpat != NULL ) {
416 printf( a->acl_dnpat );
418 if ( a->acl_attrs != NULL ) {
422 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
426 printf( a->acl_attrs[i] );
431 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
436 #endif /* LDAP_DEBUG */