1 /* acl.c - routines to parse and check acl's */
6 #include <sys/socket.h>
7 #include <netinet/in.h>
13 extern Attribute *attr_find();
14 extern struct acl *global_acl;
15 extern int global_default_access;
16 extern char *access2str();
17 extern char *dn_normalize_case();
19 int acl_access_allowed();
21 struct acl *acl_get_applicable();
23 static int regex_matches();
25 static string_expand(char *newbuf, int bufsiz, char *pattern,
26 char *match, regmatch_t *matches);
30 * access_allowed - check whether dn is allowed the requested access
31 * to entry e, attribute attr, value val. if val is null, access to
32 * the whole attribute is assumed (all values). this routine finds
33 * the applicable acl and calls acl_access_allowed() to make the
36 * returns 0 access NOT allowed
56 regmatch_t matches[MAXREMATCHES];
64 edn = dn_normalize_case( strdup( e->e_dn ) );
65 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: entry (%s) attr (%s)\n",
68 /* the lastmod attributes are ignored by ACL checking */
69 if ( strcasecmp( attr, "modifiersname" ) == 0 ||
70 strcasecmp( attr, "modifytimestamp" ) == 0 ||
71 strcasecmp( attr, "creatorsname" ) == 0 ||
72 strcasecmp( attr, "createtimestamp" ) == 0 )
74 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access allowed\n",
80 memset(matches, 0, sizeof(matches));
82 a = acl_get_applicable( be, op, e, attr, edn, MAXREMATCHES, matches );
85 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
86 Debug( LDAP_DEBUG_ARGS, "=> match[%d]: %d %d ",
87 i, matches[i].rm_so, matches[i].rm_eo );
89 if( matches[i].rm_so <= matches[0].rm_eo ) {
90 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
91 Debug( LDAP_DEBUG_ARGS, "%c", edn[n], 0, 0 );
94 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
98 rc = acl_access_allowed( a, be, conn, e, val, op, access, edn, matches );
101 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: exit (%s) attr (%s)\n",
108 * acl_get_applicable - return the acl applicable to entry e, attribute
109 * attr. the acl returned is suitable for use in subsequent calls to
110 * acl_access_allowed().
127 Debug( LDAP_DEBUG_ACL, "\n=> acl_get: entry (%s) attr (%s)\n",
130 if ( be_isroot( be, op->o_dn ) ) {
131 Debug( LDAP_DEBUG_ACL,
132 "<= acl_get: no acl applicable to database root\n", 0, 0,
137 Debug( LDAP_DEBUG_ARGS, "=> acl_get: edn %s\n", edn, 0, 0 );
139 /* check for a backend-specific acl that matches the entry */
140 for ( i = 1, a = be->be_acl; a != NULL; a = a->acl_next, i++ ) {
141 if (a->acl_dnpat != NULL) {
142 Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n",
143 i, a->acl_dnpat, a->acl_dnre.re_nsub);
145 if (regexec(&a->acl_dnre, edn, nmatch, matches, 0))
148 Debug( LDAP_DEBUG_TRACE, "=> acl_get:[%d] backend ACL match\n",
152 if ( a->acl_filter != NULL ) {
153 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
158 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr %s\n", i, attr, 0);
160 if ( attr == NULL || a->acl_attrs == NULL ||
161 charray_inlist( a->acl_attrs, attr ) )
163 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] backend acl %s attr: %s\n",
167 matches[0].rm_so = matches[0].rm_eo = -1;
170 /* check for a global acl that matches the entry */
171 for ( i = 1, a = global_acl; a != NULL; a = a->acl_next, i++ ) {
172 if (a->acl_dnpat != NULL) {
173 Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n",
174 i, a->acl_dnpat, a->acl_dnre.re_nsub);
176 if (regexec(&a->acl_dnre, edn, nmatch, matches, 0)) {
179 Debug( LDAP_DEBUG_TRACE, "=> acl_get: [%d] global ACL match\n",
184 if ( a->acl_filter != NULL ) {
185 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
190 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr\n", i, 0, 0);
192 if ( attr == NULL || a->acl_attrs == NULL ||
193 charray_inlist( a->acl_attrs, attr ) )
195 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] global acl %s attr: %s\n",
200 matches[0].rm_so = matches[0].rm_eo = -1;
203 Debug( LDAP_DEBUG_ACL, "<= acl_get: no match\n", 0, 0, 0 );
208 * acl_access_allowed - check whether the given acl allows dn the
209 * requested access to entry e, attribute attr, value val. if val
210 * is null, access to the whole attribute is assumed (all values).
212 * returns 0 access NOT allowed
236 Debug( LDAP_DEBUG_ACL,
237 "\n=> acl_access_allowed: %s access to entry \"%s\"\n",
238 access2str( access ), e->e_dn, 0 );
240 Debug( LDAP_DEBUG_ACL,
241 "\n=> acl_access_allowed: %s access to value \"%s\" by \"%s\"\n",
242 access2str( access ),
243 val ? val->bv_val : "any",
244 op->o_dn ? op->o_dn : "" );
246 if ( be_isroot( be, op->o_dn ) ) {
247 Debug( LDAP_DEBUG_ACL,
248 "<= acl_access_allowed: granted to database root\n",
253 default_access = be->be_dfltaccess ? be->be_dfltaccess : global_default_access;
256 Debug( LDAP_DEBUG_ACL,
257 "<= acl_access_allowed: %s by default (no matching to)\n",
258 default_access >= access ? "granted" : "denied", 0, 0 );
259 return( default_access >= access );
263 if ( op->o_dn != NULL ) {
264 odn = dn_normalize_case( strdup( op->o_dn ) );
266 bv.bv_len = strlen( odn );
268 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
269 if ( b->a_dnpat != NULL ) {
270 Debug( LDAP_DEBUG_TRACE, "<= check a_dnpat: %s\n",
273 * if access applies to the entry itself, and the
274 * user is bound as somebody in the same namespace as
275 * the entry, OR the given dn matches the dn pattern
277 if ( strcasecmp( b->a_dnpat, "self" ) == 0 &&
278 op->o_dn != NULL && *(op->o_dn) && e->e_dn != NULL )
280 if ( strcasecmp( edn, op->o_dn ) == 0 ) {
281 Debug( LDAP_DEBUG_ACL,
282 "<= acl_access_allowed: matched by clause #%d access %s\n",
283 i, (b->a_access & ~ACL_SELF) >=
284 access ? "granted" : "denied", 0 );
286 if ( odn ) free( odn );
287 return( (b->a_access & ~ACL_SELF) >= access );
290 if ( regex_matches( b->a_dnpat, odn, edn, matches ) ) {
291 Debug( LDAP_DEBUG_ACL,
292 "<= acl_access_allowed: matched by clause #%d access %s\n",
293 i, (b->a_access & ~ACL_SELF) >= access ?
294 "granted" : "denied", 0 );
296 if ( odn ) free( odn );
297 return( (b->a_access & ~ACL_SELF) >= access );
301 if ( b->a_addrpat != NULL ) {
302 if ( regex_matches( b->a_addrpat, conn->c_addr, edn, matches ) ) {
303 Debug( LDAP_DEBUG_ACL,
304 "<= acl_access_allowed: matched by clause #%d access %s\n",
305 i, (b->a_access & ~ACL_SELF) >= access ?
306 "granted" : "denied", 0 );
308 if ( odn ) free( odn );
309 return( (b->a_access & ~ACL_SELF) >= access );
312 if ( b->a_domainpat != NULL ) {
313 Debug( LDAP_DEBUG_ARGS, "<= check a_domainpath: %s\n",
314 b->a_domainpat, 0, 0 );
315 if ( regex_matches( b->a_domainpat, conn->c_domain, edn, matches ) )
317 Debug( LDAP_DEBUG_ACL,
318 "<= acl_access_allowed: matched by clause #%d access %s\n",
319 i, (b->a_access & ~ACL_SELF) >= access ?
320 "granted" : "denied", 0 );
322 if ( odn ) free( odn );
323 return( (b->a_access & ~ACL_SELF) >= access );
326 if ( b->a_dnattr != NULL && op->o_dn != NULL ) {
327 Debug( LDAP_DEBUG_ARGS, "<= check a_dnattr: %s\n",
329 /* see if asker is listed in dnattr */
330 if ( (at = attr_find( e->e_attrs, b->a_dnattr )) != NULL &&
331 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
333 if ( (b->a_access & ACL_SELF) &&
334 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
339 if ( odn ) free( odn );
340 Debug( LDAP_DEBUG_ACL,
341 "<= acl_acces_allowed: matched by clause #%d access %s\n",
342 i, (b->a_access & ~ACL_SELF) >= access ?
343 "granted" : "denied", 0 );
345 return( (b->a_access & ~ACL_SELF) >= access );
348 /* asker not listed in dnattr - check for self access */
349 if ( ! (b->a_access & ACL_SELF) || val == NULL ||
350 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
355 if ( odn ) free( odn );
356 Debug( LDAP_DEBUG_ACL,
357 "<= acl_access_allowed: matched by clause #%d (self) access %s\n",
358 i, (b->a_access & ~ACL_SELF) >= access ? "granted"
361 return( (b->a_access & ~ACL_SELF) >= access );
364 if ( b->a_group != NULL && op->o_dn != NULL ) {
367 /* b->a_group is an unexpanded entry name, expanded it should be an
368 * entry with objectclass group* and we test to see if odn is one of
369 * the values in the attribute uniquegroup
371 Debug( LDAP_DEBUG_ARGS, "<= check a_group: %s\n",
373 Debug( LDAP_DEBUG_ARGS, "<= check a_group: odn: %s\n",
376 /* see if asker is listed in dnattr */
377 string_expand(buf, sizeof(buf), b->a_group, edn, matches);
379 if (be_group(be, buf, odn) == 0) {
380 Debug( LDAP_DEBUG_ACL,
381 "<= acl_access_allowed: matched by clause #%d (group) access granted\n",
383 if ( odn ) free( odn );
384 return( (b->a_access & ~ACL_SELF) >= access );
387 #endif /* ACLGROUP */
390 if ( odn ) free( odn );
391 Debug( LDAP_DEBUG_ACL,
392 "<= acl_access_allowed: %s by default (no matching by)\n",
393 default_access >= access ? "granted" : "denied", 0, 0 );
395 return( default_access >= access );
399 * acl_check_mods - check access control on the given entry to see if
400 * it allows the given modifications by the user associated with op.
401 * returns LDAP_SUCCESS mods allowed ok
402 * anything else mods not allowed - return is an error
403 * code indicating the problem
419 edn = dn_normalize_case( strdup( e->e_dn ) );
421 for ( ; mods != NULL; mods = mods->mod_next ) {
422 regmatch_t matches[MAXREMATCHES];
424 /* the lastmod attributes are ignored by ACL checking */
425 if ( strcasecmp( mods->mod_type, "modifiersname" ) == 0 ||
426 strcasecmp( mods->mod_type, "modifytimestamp" ) == 0 ||
427 strcasecmp( mods->mod_type, "creatorsname" ) == 0 ||
428 strcasecmp( mods->mod_type, "createtimestamp" ) == 0 )
430 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access allowed\n",
431 mods->mod_type, 0, 0 );
435 a = acl_get_applicable( be, op, e, mods->mod_type, edn,
436 MAXREMATCHES, matches );
438 switch ( mods->mod_op & ~LDAP_MOD_BVALUES ) {
439 case LDAP_MOD_REPLACE:
441 if ( mods->mod_bvalues == NULL ) {
444 for ( i = 0; mods->mod_bvalues[i] != NULL; i++ ) {
445 if ( ! acl_access_allowed( a, be, conn, e, mods->mod_bvalues[i],
446 op, ACL_WRITE, edn, matches) )
449 return( LDAP_INSUFFICIENT_ACCESS );
454 case LDAP_MOD_DELETE:
455 if ( mods->mod_bvalues == NULL ) {
456 if ( ! acl_access_allowed( a, be, conn, e,
457 NULL, op, ACL_WRITE, edn, matches) )
460 return( LDAP_INSUFFICIENT_ACCESS );
464 for ( i = 0; mods->mod_bvalues[i] != NULL; i++ ) {
465 if ( ! acl_access_allowed( a, be, conn, e, mods->mod_bvalues[i],
466 op, ACL_WRITE, edn, matches) )
469 return( LDAP_INSUFFICIENT_ACCESS );
477 return( LDAP_SUCCESS );
480 static string_expand(
496 for ( dp = newbuf, sp = pat; size < 512 && *sp ; sp++) {
497 /* did we previously see a $ */
502 } else if (*sp >= '0' && *sp <= '9' ) {
510 i = matches[n].rm_so;
511 l = matches[n].rm_eo;
512 for ( ; size < 512 && i < l; size++, i++ ) {
530 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
531 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
536 char *pat, /* pattern to expand and match against */
537 char *str, /* string to match against pattern */
538 char *buf, /* buffer with $N expansion variables */
539 regmatch_t *matches /* offsets in buffer for $N expansion variables */
546 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
547 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
549 regerror(rc, &re, error, sizeof(error));
551 Debug( LDAP_DEBUG_TRACE,
552 "compile( \"%s\", \"%s\") failed %s\n",
557 rc = regexec(&re, str, 0, NULL, 0);
560 Debug( LDAP_DEBUG_TRACE,
561 "=> regex_matches: string: %s\n", str, 0, 0 );
562 Debug( LDAP_DEBUG_TRACE,
563 "=> regex_matches: rc: %d %s\n",
564 rc, !rc ? "matches" : "no matches", 0 );