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 ( oc_check_no_usermod_attr( attr ) ) {
59 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
64 memset(matches, 0, sizeof(matches));
66 a = acl_get_applicable( be, op, e, attr, MAXREMATCHES, matches );
69 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
70 Debug( LDAP_DEBUG_ARGS, "=> match[%d]: %d %d ", i,
71 (int)matches[i].rm_so, (int)matches[i].rm_eo );
73 if( matches[i].rm_so <= matches[0].rm_eo ) {
74 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
75 Debug( LDAP_DEBUG_ARGS, "%c", edn[n], 0, 0 );
78 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
82 rc = acl_access_allowed( a, be, conn, e, val, op, access, edn, matches );
84 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: exit (%s) attr (%s)\n",
91 * acl_get_applicable - return the acl applicable to entry e, attribute
92 * attr. the acl returned is suitable for use in subsequent calls to
93 * acl_access_allowed().
110 Debug( LDAP_DEBUG_ACL, "\n=> acl_get: entry (%s) attr (%s)\n",
113 if ( be_isroot( be, op->o_ndn ) ) {
114 Debug( LDAP_DEBUG_ACL,
115 "<= acl_get: no acl applicable to database root\n", 0, 0,
122 Debug( LDAP_DEBUG_ARGS, "=> acl_get: edn %s\n", edn, 0, 0 );
124 /* check for a backend-specific acl that matches the entry */
125 for ( i = 1, a = be->be_acl; a != NULL; a = a->acl_next, i++ ) {
126 if (a->acl_dnpat != NULL) {
127 Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n",
128 i, a->acl_dnpat, (int) a->acl_dnre.re_nsub);
130 if (regexec(&a->acl_dnre, edn, nmatch, matches, 0))
133 Debug( LDAP_DEBUG_TRACE, "=> acl_get:[%d] backend ACL match\n",
137 if ( a->acl_filter != NULL ) {
138 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
143 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr %s\n", i, attr, 0);
145 if ( attr == NULL || a->acl_attrs == NULL ||
146 charray_inlist( a->acl_attrs, attr ) )
148 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] backend acl %s attr: %s\n",
152 matches[0].rm_so = matches[0].rm_eo = -1;
155 /* check for a global acl that matches the entry */
156 for ( i = 1, a = global_acl; a != NULL; a = a->acl_next, i++ ) {
157 if (a->acl_dnpat != NULL) {
158 Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n",
159 i, a->acl_dnpat, (int) a->acl_dnre.re_nsub);
161 if (regexec(&a->acl_dnre, edn, nmatch, matches, 0)) {
164 Debug( LDAP_DEBUG_TRACE, "=> acl_get: [%d] global ACL match\n",
169 if ( a->acl_filter != NULL ) {
170 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
175 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr\n", i, 0, 0);
177 if ( attr == NULL || a->acl_attrs == NULL ||
178 charray_inlist( a->acl_attrs, attr ) )
180 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] global acl %s attr: %s\n",
185 matches[0].rm_so = matches[0].rm_eo = -1;
188 Debug( LDAP_DEBUG_ACL, "<= acl_get: no match\n", 0, 0, 0 );
193 * acl_access_allowed - check whether the given acl allows dn the
194 * requested access to entry e, attribute attr, value val. if val
195 * is null, access to the whole attribute is assumed (all values).
197 * returns 0 access NOT allowed
220 Debug( LDAP_DEBUG_ACL,
221 "\n=> acl_access_allowed: %s access to entry \"%s\"\n",
222 access2str( access ), e->e_dn, 0 );
224 Debug( LDAP_DEBUG_ACL,
225 "\n=> acl_access_allowed: %s access to value \"%s\" by \"%s\"\n",
226 access2str( access ),
227 val ? val->bv_val : "any",
228 op->o_ndn ? op->o_ndn : "" );
230 if ( be_isroot( be, op->o_ndn ) ) {
231 Debug( LDAP_DEBUG_ACL,
232 "<= acl_access_allowed: granted to database root\n",
237 default_access = be->be_dfltaccess ? be->be_dfltaccess : global_default_access;
240 Debug( LDAP_DEBUG_ACL,
241 "<= acl_access_allowed: %s by default (no matching to)\n",
242 default_access >= access ? "granted" : "denied", 0, 0 );
243 return( default_access >= access );
246 if ( op->o_ndn != NULL ) {
247 bv.bv_val = op->o_ndn;
248 bv.bv_len = strlen( bv.bv_val );
251 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
252 if ( b->a_dnpat != NULL ) {
253 Debug( LDAP_DEBUG_TRACE, "<= check a_dnpat: %s\n",
256 * if access applies to the entry itself, and the
257 * user is bound as somebody in the same namespace as
258 * the entry, OR the given dn matches the dn pattern
260 if ( strcasecmp( b->a_dnpat, "anonymous" ) == 0 &&
261 (op->o_ndn == NULL || *(op->o_ndn) == '\0' ) )
263 Debug( LDAP_DEBUG_ACL,
264 "<= acl_access_allowed: matched by clause #%d access %s\n",
265 i, ACL_GRANT(b->a_access, access)
266 ? "granted" : "denied", 0 );
268 return ACL_GRANT(b->a_access, access );
270 } else if ( strcasecmp( b->a_dnpat, "self" ) == 0 &&
271 op->o_ndn != NULL && *(op->o_ndn) && e->e_dn != NULL )
273 if ( strcmp( edn, op->o_ndn ) == 0 ) {
274 Debug( LDAP_DEBUG_ACL,
275 "<= acl_access_allowed: matched by clause #%d access %s\n",
276 i, ACL_GRANT(b->a_access, access)
277 ? "granted" : "denied", 0 );
279 return ACL_GRANT(b->a_access, access );
282 if ( regex_matches( b->a_dnpat, op->o_ndn, edn, matches ) ) {
283 Debug( LDAP_DEBUG_ACL,
284 "<= acl_access_allowed: matched by clause #%d access %s\n",
285 i, ACL_GRANT(b->a_access, access)
286 ? "granted" : "denied", 0 );
288 return ACL_GRANT(b->a_access, access );
292 if ( b->a_addrpat != NULL ) {
293 if ( regex_matches( b->a_addrpat, conn->c_client_addr,
296 Debug( LDAP_DEBUG_ACL,
297 "<= acl_access_allowed: matched by clause #%d access %s\n",
298 i, ACL_GRANT(b->a_access, access)
299 ? "granted" : "denied", 0 );
301 return ACL_GRANT(b->a_access, access );
304 if ( b->a_domainpat != NULL ) {
305 Debug( LDAP_DEBUG_ARGS, "<= check a_domainpath: %s\n",
306 b->a_domainpat, 0, 0 );
307 if ( regex_matches( b->a_domainpat, conn->c_client_name,
310 Debug( LDAP_DEBUG_ACL,
311 "<= acl_access_allowed: matched by clause #%d access %s\n",
312 i, ACL_GRANT(b->a_access, access)
313 ? "granted" : "denied", 0 );
315 return ACL_GRANT(b->a_access, access );
318 if ( b->a_dnattr != NULL && op->o_ndn != NULL ) {
319 Debug( LDAP_DEBUG_ARGS, "<= check a_dnattr: %s\n",
321 /* see if asker is listed in dnattr */
322 if ( (at = attr_find( e->e_attrs, b->a_dnattr )) != NULL &&
323 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
325 if ( ACL_IS_SELF(b->a_access) &&
326 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
331 Debug( LDAP_DEBUG_ACL,
332 "<= acl_acces_allowed: matched by clause #%d access %s\n",
333 i, ACL_GRANT(b->a_access, access)
334 ? "granted" : "denied", 0 );
336 return ACL_GRANT(b->a_access, access );
339 /* asker not listed in dnattr - check for self access */
340 if ( ! ACL_IS_SELF(b->a_access) || val == NULL ||
341 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
346 Debug( LDAP_DEBUG_ACL,
347 "<= acl_access_allowed: matched by clause #%d (self) access %s\n",
348 i, ACL_GRANT(b->a_access, access)
349 ? "granted" : "denied", 0 );
351 return ACL_GRANT(b->a_access, access );
354 if ( b->a_group != NULL && op->o_ndn != NULL ) {
357 /* b->a_group is an unexpanded entry name, expanded it should be an
358 * entry with objectclass group* and we test to see if odn is one of
359 * the values in the attribute group
361 /* see if asker is listed in dnattr */
362 string_expand(buf, sizeof(buf), b->a_group, edn, matches);
363 (void) dn_normalize_case(buf);
365 if (backend_group(be, e, buf, op->o_ndn,
366 b->a_group_oc, b->a_group_at) == 0)
368 Debug( LDAP_DEBUG_ACL,
369 "<= acl_access_allowed: matched by clause #%d (group) access granted\n",
371 return ACL_GRANT(b->a_access, access );
376 Debug( LDAP_DEBUG_ACL,
377 "<= acl_access_allowed: %s by default (no matching by)\n",
378 default_access >= access ? "granted" : "denied", 0, 0 );
380 return( default_access >= access );
384 * acl_check_modlist - check access control on the given entry to see if
385 * it allows the given modifications by the user associated with op.
386 * returns LDAP_SUCCESS mods allowed ok
387 * anything else mods not allowed - return is an error
388 * code indicating the problem
402 char *edn = e->e_ndn;
404 for ( ; mlist != NULL; mlist = mlist->ml_next ) {
405 regmatch_t matches[MAXREMATCHES];
407 /* the lastmod attributes are ignored by ACL checking */
408 if ( oc_check_no_usermod_attr( mlist->ml_type ) ) {
409 Debug( LDAP_DEBUG_ACL, "Operational attribute: %s access allowed\n",
410 mlist->ml_type, 0, 0 );
414 a = acl_get_applicable( be, op, e, mlist->ml_type,
415 MAXREMATCHES, matches );
417 switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
418 case LDAP_MOD_REPLACE:
420 if ( mlist->ml_bvalues == NULL ) {
423 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
424 if ( ! acl_access_allowed( a, be, conn, e, mlist->ml_bvalues[i],
425 op, ACL_WRITE, edn, matches) )
427 return( LDAP_INSUFFICIENT_ACCESS );
432 case LDAP_MOD_DELETE:
433 if ( mlist->ml_bvalues == NULL ) {
434 if ( ! acl_access_allowed( a, be, conn, e,
435 NULL, op, ACL_WRITE, edn, matches) )
437 return( LDAP_INSUFFICIENT_ACCESS );
441 for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
442 if ( ! acl_access_allowed( a, be, conn, e, mlist->ml_bvalues[i],
443 op, ACL_WRITE, edn, matches) )
445 return( LDAP_INSUFFICIENT_ACCESS );
452 return( LDAP_SUCCESS );
472 for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
473 /* did we previously see a $ */
478 } else if (*sp >= '0' && *sp <= '9' ) {
485 i = matches[n].rm_so;
486 l = matches[n].rm_eo;
487 for ( ; size < 512 && i < l; size++, i++ ) {
505 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
506 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
511 char *pat, /* pattern to expand and match against */
512 char *str, /* string to match against pattern */
513 char *buf, /* buffer with $N expansion variables */
514 regmatch_t *matches /* offsets in buffer for $N expansion variables */
521 if(str == NULL) str = "";
523 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
524 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
526 regerror(rc, &re, error, sizeof(error));
528 Debug( LDAP_DEBUG_TRACE,
529 "compile( \"%s\", \"%s\") failed %s\n",
534 rc = regexec(&re, str, 0, NULL, 0);
537 Debug( LDAP_DEBUG_TRACE,
538 "=> regex_matches: string: %s\n", str, 0, 0 );
539 Debug( LDAP_DEBUG_TRACE,
540 "=> regex_matches: rc: %d %s\n",
541 rc, !rc ? "matches" : "no matches", 0 );