1 /* acl.c - routines to parse and check acl's */
13 static int regex_matches(char *pat, char *str, char *buf, regmatch_t *matches);
14 static void string_expand(char *newbuf, int bufsiz, char *pattern,
15 char *match, regmatch_t *matches);
19 * access_allowed - check whether dn is allowed the requested access
20 * to entry e, attribute attr, value val. if val is null, access to
21 * the whole attribute is assumed (all values). this routine finds
22 * the applicable acl and calls acl_access_allowed() to make the
25 * returns 0 access NOT allowed
45 regmatch_t matches[MAXREMATCHES];
53 edn = dn_normalize_case( ch_strdup( e->e_dn ) );
54 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: entry (%s) attr (%s)\n",
57 /* the lastmod attributes are ignored by ACL checking */
58 if ( strcasecmp( attr, "modifiersname" ) == 0 ||
59 strcasecmp( attr, "modifytimestamp" ) == 0 ||
60 strcasecmp( attr, "creatorsname" ) == 0 ||
61 strcasecmp( attr, "createtimestamp" ) == 0 )
63 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access allowed\n",
69 memset(matches, 0, sizeof(matches));
71 a = acl_get_applicable( be, op, e, attr, edn, MAXREMATCHES, matches );
74 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
75 Debug( LDAP_DEBUG_ARGS, "=> match[%d]: %d %d ", i,
76 (int)matches[i].rm_so, (int)matches[i].rm_eo );
78 if( matches[i].rm_so <= matches[0].rm_eo ) {
79 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
80 Debug( LDAP_DEBUG_ARGS, "%c", edn[n], 0, 0 );
83 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
87 rc = acl_access_allowed( a, be, conn, e, val, op, access, edn, matches );
90 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: exit (%s) attr (%s)\n",
97 * acl_get_applicable - return the acl applicable to entry e, attribute
98 * attr. the acl returned is suitable for use in subsequent calls to
99 * acl_access_allowed().
116 Debug( LDAP_DEBUG_ACL, "\n=> acl_get: entry (%s) attr (%s)\n",
119 if ( be_isroot( be, op->o_dn ) ) {
120 Debug( LDAP_DEBUG_ACL,
121 "<= acl_get: no acl applicable to database root\n", 0, 0,
126 Debug( LDAP_DEBUG_ARGS, "=> acl_get: edn %s\n", edn, 0, 0 );
128 /* check for a backend-specific acl that matches the entry */
129 for ( i = 1, a = be->be_acl; a != NULL; a = a->acl_next, i++ ) {
130 if (a->acl_dnpat != NULL) {
131 Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n",
132 i, a->acl_dnpat, (int) a->acl_dnre.re_nsub);
134 if (regexec(&a->acl_dnre, edn, nmatch, matches, 0))
137 Debug( LDAP_DEBUG_TRACE, "=> acl_get:[%d] backend ACL match\n",
141 if ( a->acl_filter != NULL ) {
142 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
147 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr %s\n", i, attr, 0);
149 if ( attr == NULL || a->acl_attrs == NULL ||
150 charray_inlist( a->acl_attrs, attr ) )
152 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] backend acl %s attr: %s\n",
156 matches[0].rm_so = matches[0].rm_eo = -1;
159 /* check for a global acl that matches the entry */
160 for ( i = 1, a = global_acl; a != NULL; a = a->acl_next, i++ ) {
161 if (a->acl_dnpat != NULL) {
162 Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n",
163 i, a->acl_dnpat, (int) a->acl_dnre.re_nsub);
165 if (regexec(&a->acl_dnre, edn, nmatch, matches, 0)) {
168 Debug( LDAP_DEBUG_TRACE, "=> acl_get: [%d] global ACL match\n",
173 if ( a->acl_filter != NULL ) {
174 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
179 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr\n", i, 0, 0);
181 if ( attr == NULL || a->acl_attrs == NULL ||
182 charray_inlist( a->acl_attrs, attr ) )
184 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] global acl %s attr: %s\n",
189 matches[0].rm_so = matches[0].rm_eo = -1;
192 Debug( LDAP_DEBUG_ACL, "<= acl_get: no match\n", 0, 0, 0 );
197 * acl_access_allowed - check whether the given acl allows dn the
198 * requested access to entry e, attribute attr, value val. if val
199 * is null, access to the whole attribute is assumed (all values).
201 * returns 0 access NOT allowed
225 Debug( LDAP_DEBUG_ACL,
226 "\n=> acl_access_allowed: %s access to entry \"%s\"\n",
227 access2str( access ), e->e_dn, 0 );
229 Debug( LDAP_DEBUG_ACL,
230 "\n=> acl_access_allowed: %s access to value \"%s\" by \"%s\"\n",
231 access2str( access ),
232 val ? val->bv_val : "any",
233 op->o_dn ? op->o_dn : "" );
235 if ( be_isroot( be, op->o_dn ) ) {
236 Debug( LDAP_DEBUG_ACL,
237 "<= acl_access_allowed: granted to database root\n",
242 default_access = be->be_dfltaccess ? be->be_dfltaccess : global_default_access;
245 Debug( LDAP_DEBUG_ACL,
246 "<= acl_access_allowed: %s by default (no matching to)\n",
247 default_access >= access ? "granted" : "denied", 0, 0 );
248 return( default_access >= access );
252 if ( op->o_dn != NULL ) {
253 odn = dn_normalize_case( ch_strdup( op->o_dn ) );
255 bv.bv_len = strlen( odn );
257 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
258 if ( b->a_dnpat != NULL ) {
259 Debug( LDAP_DEBUG_TRACE, "<= check a_dnpat: %s\n",
262 * if access applies to the entry itself, and the
263 * user is bound as somebody in the same namespace as
264 * the entry, OR the given dn matches the dn pattern
266 if ( strcasecmp( b->a_dnpat, "self" ) == 0 &&
267 op->o_dn != NULL && *(op->o_dn) && e->e_dn != NULL )
269 if ( strcasecmp( edn, op->o_dn ) == 0 ) {
270 Debug( LDAP_DEBUG_ACL,
271 "<= acl_access_allowed: matched by clause #%d access %s\n",
272 i, (b->a_access & ~ACL_SELF) >=
273 access ? "granted" : "denied", 0 );
275 if ( odn ) free( odn );
276 return( (b->a_access & ~ACL_SELF) >= access );
279 if ( regex_matches( b->a_dnpat, odn, edn, matches ) ) {
280 Debug( LDAP_DEBUG_ACL,
281 "<= acl_access_allowed: matched by clause #%d access %s\n",
282 i, (b->a_access & ~ACL_SELF) >= access ?
283 "granted" : "denied", 0 );
285 if ( odn ) free( odn );
286 return( (b->a_access & ~ACL_SELF) >= access );
290 if ( b->a_addrpat != NULL ) {
291 if ( regex_matches( b->a_addrpat, conn->c_addr, edn, matches ) ) {
292 Debug( LDAP_DEBUG_ACL,
293 "<= acl_access_allowed: matched by clause #%d access %s\n",
294 i, (b->a_access & ~ACL_SELF) >= access ?
295 "granted" : "denied", 0 );
297 if ( odn ) free( odn );
298 return( (b->a_access & ~ACL_SELF) >= access );
301 if ( b->a_domainpat != NULL ) {
302 Debug( LDAP_DEBUG_ARGS, "<= check a_domainpath: %s\n",
303 b->a_domainpat, 0, 0 );
304 if ( regex_matches( b->a_domainpat, conn->c_domain, edn, matches ) )
306 Debug( LDAP_DEBUG_ACL,
307 "<= acl_access_allowed: matched by clause #%d access %s\n",
308 i, (b->a_access & ~ACL_SELF) >= access ?
309 "granted" : "denied", 0 );
311 if ( odn ) free( odn );
312 return( (b->a_access & ~ACL_SELF) >= access );
315 if ( b->a_dnattr != NULL && op->o_dn != NULL ) {
316 Debug( LDAP_DEBUG_ARGS, "<= check a_dnattr: %s\n",
318 /* see if asker is listed in dnattr */
319 if ( (at = attr_find( e->e_attrs, b->a_dnattr )) != NULL &&
320 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
322 if ( (b->a_access & ACL_SELF) &&
323 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
328 if ( odn ) free( odn );
329 Debug( LDAP_DEBUG_ACL,
330 "<= acl_acces_allowed: matched by clause #%d access %s\n",
331 i, (b->a_access & ~ACL_SELF) >= access ?
332 "granted" : "denied", 0 );
334 return( (b->a_access & ~ACL_SELF) >= access );
337 /* asker not listed in dnattr - check for self access */
338 if ( ! (b->a_access & ACL_SELF) || val == NULL ||
339 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
344 if ( odn ) free( odn );
345 Debug( LDAP_DEBUG_ACL,
346 "<= acl_access_allowed: matched by clause #%d (self) access %s\n",
347 i, (b->a_access & ~ACL_SELF) >= access ? "granted"
350 return( (b->a_access & ~ACL_SELF) >= access );
352 #ifdef SLAPD_ACLGROUPS
353 if ( b->a_group != NULL && op->o_dn != NULL ) {
356 /* b->a_group is an unexpanded entry name, expanded it should be an
357 * entry with objectclass group* and we test to see if odn is one of
358 * the values in the attribute group
360 /* see if asker is listed in dnattr */
361 string_expand(buf, sizeof(buf), b->a_group, edn, matches);
363 if (be_group(be, buf, odn, b->a_objectclassvalue, b->a_groupattrname) == 0) {
364 Debug( LDAP_DEBUG_ACL,
365 "<= acl_access_allowed: matched by clause #%d (group) access granted\n",
367 if ( odn ) free( odn );
368 return( (b->a_access & ~ACL_SELF) >= access );
371 #endif /* SLAPD_ACLGROUPS */
374 if ( odn ) free( odn );
375 Debug( LDAP_DEBUG_ACL,
376 "<= acl_access_allowed: %s by default (no matching by)\n",
377 default_access >= access ? "granted" : "denied", 0, 0 );
379 return( default_access >= access );
383 * acl_check_mods - check access control on the given entry to see if
384 * it allows the given modifications by the user associated with op.
385 * returns LDAP_SUCCESS mods allowed ok
386 * anything else mods not allowed - return is an error
387 * code indicating the problem
403 edn = dn_normalize_case( ch_strdup( e->e_dn ) );
405 for ( ; mods != NULL; mods = mods->mod_next ) {
406 regmatch_t matches[MAXREMATCHES];
408 /* the lastmod attributes are ignored by ACL checking */
409 if ( strcasecmp( mods->mod_type, "modifiersname" ) == 0 ||
410 strcasecmp( mods->mod_type, "modifytimestamp" ) == 0 ||
411 strcasecmp( mods->mod_type, "creatorsname" ) == 0 ||
412 strcasecmp( mods->mod_type, "createtimestamp" ) == 0 )
414 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access allowed\n",
415 mods->mod_type, 0, 0 );
419 a = acl_get_applicable( be, op, e, mods->mod_type, edn,
420 MAXREMATCHES, matches );
422 switch ( mods->mod_op & ~LDAP_MOD_BVALUES ) {
423 case LDAP_MOD_REPLACE:
425 if ( mods->mod_bvalues == NULL ) {
428 for ( i = 0; mods->mod_bvalues[i] != NULL; i++ ) {
429 if ( ! acl_access_allowed( a, be, conn, e, mods->mod_bvalues[i],
430 op, ACL_WRITE, edn, matches) )
433 return( LDAP_INSUFFICIENT_ACCESS );
438 case LDAP_MOD_DELETE:
439 if ( mods->mod_bvalues == NULL ) {
440 if ( ! acl_access_allowed( a, be, conn, e,
441 NULL, op, ACL_WRITE, edn, matches) )
444 return( LDAP_INSUFFICIENT_ACCESS );
448 for ( i = 0; mods->mod_bvalues[i] != NULL; i++ ) {
449 if ( ! acl_access_allowed( a, be, conn, e, mods->mod_bvalues[i],
450 op, ACL_WRITE, edn, matches) )
453 return( LDAP_INSUFFICIENT_ACCESS );
461 return( LDAP_SUCCESS );
481 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
482 /* did we previously see a $ */
487 } else if (*sp >= '0' && *sp <= '9' ) {
495 i = matches[n].rm_so;
496 l = matches[n].rm_eo;
497 for ( ; size < 512 && i < l; size++, i++ ) {
515 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
516 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
521 char *pat, /* pattern to expand and match against */
522 char *str, /* string to match against pattern */
523 char *buf, /* buffer with $N expansion variables */
524 regmatch_t *matches /* offsets in buffer for $N expansion variables */
531 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
532 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
534 regerror(rc, &re, error, sizeof(error));
536 Debug( LDAP_DEBUG_TRACE,
537 "compile( \"%s\", \"%s\") failed %s\n",
542 rc = regexec(&re, str, 0, NULL, 0);
545 Debug( LDAP_DEBUG_TRACE,
546 "=> regex_matches: string: %s\n", str, 0, 0 );
547 Debug( LDAP_DEBUG_TRACE,
548 "=> regex_matches: rc: %d %s\n",
549 rc, !rc ? "matches" : "no matches", 0 );