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 op->o_ndn 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
44 regmatch_t matches[MAXREMATCHES];
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",
68 memset(matches, 0, sizeof(matches));
70 a = acl_get_applicable( be, op, e, attr, MAXREMATCHES, matches );
73 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
74 Debug( LDAP_DEBUG_ARGS, "=> match[%d]: %d %d ", i,
75 (int)matches[i].rm_so, (int)matches[i].rm_eo );
77 if( matches[i].rm_so <= matches[0].rm_eo ) {
78 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
79 Debug( LDAP_DEBUG_ARGS, "%c", edn[n], 0, 0 );
82 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
86 rc = acl_access_allowed( a, be, conn, e, val, op, access, edn, matches );
88 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: exit (%s) attr (%s)\n",
95 * acl_get_applicable - return the acl applicable to entry e, attribute
96 * attr. the acl returned is suitable for use in subsequent calls to
97 * acl_access_allowed().
114 Debug( LDAP_DEBUG_ACL, "\n=> acl_get: entry (%s) attr (%s)\n",
117 if ( be_isroot( be, op->o_ndn ) ) {
118 Debug( LDAP_DEBUG_ACL,
119 "<= 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_ndn ? op->o_ndn : "" );
235 if ( be_isroot( be, op->o_ndn ) ) {
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 );
255 bv.bv_len = strlen( odn );
258 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
259 if ( b->a_dnpat != NULL ) {
260 Debug( LDAP_DEBUG_TRACE, "<= check a_dnpat: %s\n",
263 * if access applies to the entry itself, and the
264 * user is bound as somebody in the same namespace as
265 * the entry, OR the given dn matches the dn pattern
267 if ( strcasecmp( b->a_dnpat, "self" ) == 0 &&
268 op->o_ndn != NULL && *(op->o_ndn) && e->e_dn != NULL )
270 if ( strcmp( edn, op->o_ndn ) == 0 ) {
271 Debug( LDAP_DEBUG_ACL,
272 "<= acl_access_allowed: matched by clause #%d access %s\n",
273 i, (b->a_access & ~ACL_SELF) >=
274 access ? "granted" : "denied", 0 );
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 return( (b->a_access & ~ACL_SELF) >= access );
289 if ( b->a_addrpat != NULL ) {
290 if ( regex_matches( b->a_addrpat, conn->c_addr, 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 return( (b->a_access & ~ACL_SELF) >= access );
299 if ( b->a_domainpat != NULL ) {
300 Debug( LDAP_DEBUG_ARGS, "<= check a_domainpath: %s\n",
301 b->a_domainpat, 0, 0 );
302 if ( regex_matches( b->a_domainpat, conn->c_domain, edn, matches ) )
304 Debug( LDAP_DEBUG_ACL,
305 "<= acl_access_allowed: matched by clause #%d access %s\n",
306 i, (b->a_access & ~ACL_SELF) >= access ?
307 "granted" : "denied", 0 );
309 return( (b->a_access & ~ACL_SELF) >= access );
312 if ( b->a_dnattr != NULL && op->o_ndn != NULL ) {
313 Debug( LDAP_DEBUG_ARGS, "<= check a_dnattr: %s\n",
315 /* see if asker is listed in dnattr */
316 if ( (at = attr_find( e->e_attrs, b->a_dnattr )) != NULL &&
317 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
319 if ( (b->a_access & ACL_SELF) &&
320 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
325 Debug( LDAP_DEBUG_ACL,
326 "<= acl_acces_allowed: matched by clause #%d access %s\n",
327 i, (b->a_access & ~ACL_SELF) >= access ?
328 "granted" : "denied", 0 );
330 return( (b->a_access & ~ACL_SELF) >= access );
333 /* asker not listed in dnattr - check for self access */
334 if ( ! (b->a_access & ACL_SELF) || val == NULL ||
335 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
340 Debug( LDAP_DEBUG_ACL,
341 "<= acl_access_allowed: matched by clause #%d (self) access %s\n",
342 i, (b->a_access & ~ACL_SELF) >= access ? "granted"
345 return( (b->a_access & ~ACL_SELF) >= access );
347 #ifdef SLAPD_ACLGROUPS
348 if ( b->a_group != NULL && op->o_ndn != NULL ) {
351 /* b->a_group is an unexpanded entry name, expanded it should be an
352 * entry with objectclass group* and we test to see if odn is one of
353 * the values in the attribute group
355 /* see if asker is listed in dnattr */
356 string_expand(buf, sizeof(buf), b->a_group, edn, matches);
357 (void) dn_normalize_case(buf);
359 if (backend_group(be, e, buf, odn,
360 b->a_objectclassvalue, b->a_groupattrname) == 0)
362 Debug( LDAP_DEBUG_ACL,
363 "<= acl_access_allowed: matched by clause #%d (group) access granted\n",
365 return( (b->a_access & ~ACL_SELF) >= access );
368 #endif /* SLAPD_ACLGROUPS */
371 Debug( LDAP_DEBUG_ACL,
372 "<= acl_access_allowed: %s by default (no matching by)\n",
373 default_access >= access ? "granted" : "denied", 0, 0 );
375 return( default_access >= access );
379 * acl_check_modlist - check access control on the given entry to see if
380 * it allows the given modifications by the user associated with op.
381 * returns LDAP_SUCCESS mods allowed ok
382 * anything else mods not allowed - return is an error
383 * code indicating the problem
397 char *edn = e->e_ndn;
399 for ( ; mlist != NULL; mlist = mlist->ml_next ) {
400 regmatch_t matches[MAXREMATCHES];
402 /* the lastmod attributes are ignored by ACL checking */
403 if ( strcasecmp( mlist->ml_type, "modifiersname" ) == 0 ||
404 strcasecmp( mlist->ml_type, "modifytimestamp" ) == 0 ||
405 strcasecmp( mlist->ml_type, "creatorsname" ) == 0 ||
406 strcasecmp( mlist->ml_type, "createtimestamp" ) == 0 )
408 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access allowed\n",
409 mlist->ml_type, 0, 0 );
413 a = acl_get_applicable( be, op, e, mlist->ml_type,
414 MAXREMATCHES, matches );
416 switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
417 case LDAP_MOD_REPLACE:
419 if ( mlist->ml_bvalues == NULL ) {
422 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
423 if ( ! acl_access_allowed( a, be, conn, e, mlist->ml_bvalues[i],
424 op, ACL_WRITE, edn, matches) )
426 return( LDAP_INSUFFICIENT_ACCESS );
431 case LDAP_MOD_DELETE:
432 if ( mlist->ml_bvalues == NULL ) {
433 if ( ! acl_access_allowed( a, be, conn, e,
434 NULL, op, ACL_WRITE, edn, matches) )
436 return( LDAP_INSUFFICIENT_ACCESS );
440 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
441 if ( ! acl_access_allowed( a, be, conn, e, mlist->ml_bvalues[i],
442 op, ACL_WRITE, edn, matches) )
444 return( LDAP_INSUFFICIENT_ACCESS );
451 return( LDAP_SUCCESS );
471 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
472 /* did we previously see a $ */
477 } else if (*sp >= '0' && *sp <= '9' ) {
484 i = matches[n].rm_so;
485 l = matches[n].rm_eo;
486 for ( ; size < 512 && i < l; size++, i++ ) {
504 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
505 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
510 char *pat, /* pattern to expand and match against */
511 char *str, /* string to match against pattern */
512 char *buf, /* buffer with $N expansion variables */
513 regmatch_t *matches /* offsets in buffer for $N expansion variables */
520 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
521 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
523 regerror(rc, &re, error, sizeof(error));
525 Debug( LDAP_DEBUG_TRACE,
526 "compile( \"%s\", \"%s\") failed %s\n",
531 rc = regexec(&re, str, 0, NULL, 0);
534 Debug( LDAP_DEBUG_TRACE,
535 "=> regex_matches: string: %s\n", str, 0, 0 );
536 Debug( LDAP_DEBUG_TRACE,
537 "=> regex_matches: rc: %d %s\n",
538 rc, !rc ? "matches" : "no matches", 0 );