1 /* acl.c - routines to parse and check acl's */
8 #include <sys/socket.h>
9 #include <netinet/in.h>
15 extern Attribute *attr_find();
16 extern struct acl *global_acl;
17 extern int global_default_access;
18 extern char *access2str();
19 extern char *dn_normalize_case();
21 int acl_access_allowed();
23 struct acl *acl_get_applicable();
25 static int regex_matches();
27 static string_expand(char *newbuf, int bufsiz, char *pattern,
28 char *match, regmatch_t *matches);
32 * access_allowed - check whether dn is allowed the requested access
33 * to entry e, attribute attr, value val. if val is null, access to
34 * the whole attribute is assumed (all values). this routine finds
35 * the applicable acl and calls acl_access_allowed() to make the
38 * returns 0 access NOT allowed
58 regmatch_t matches[MAXREMATCHES];
66 edn = dn_normalize_case( strdup( e->e_dn ) );
67 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: entry (%s) attr (%s)\n",
70 /* the lastmod attributes are ignored by ACL checking */
71 if ( strcasecmp( attr, "modifiersname" ) == 0 ||
72 strcasecmp( attr, "modifytimestamp" ) == 0 ||
73 strcasecmp( attr, "creatorsname" ) == 0 ||
74 strcasecmp( attr, "createtimestamp" ) == 0 )
76 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access allowed\n",
82 memset(matches, 0, sizeof(matches));
84 a = acl_get_applicable( be, op, e, attr, edn, MAXREMATCHES, matches );
87 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
88 Debug( LDAP_DEBUG_ARGS, "=> match[%d]: %d %d ",
89 i, matches[i].rm_so, matches[i].rm_eo );
91 if( matches[i].rm_so <= matches[0].rm_eo ) {
92 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
93 Debug( LDAP_DEBUG_ARGS, "%c", edn[n], 0, 0 );
96 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
100 rc = acl_access_allowed( a, be, conn, e, val, op, access, edn, matches );
103 Debug( LDAP_DEBUG_ACL, "\n=> access_allowed: exit (%s) attr (%s)\n",
110 * acl_get_applicable - return the acl applicable to entry e, attribute
111 * attr. the acl returned is suitable for use in subsequent calls to
112 * acl_access_allowed().
129 Debug( LDAP_DEBUG_ACL, "\n=> acl_get: entry (%s) attr (%s)\n",
132 if ( be_isroot( be, op->o_dn ) ) {
133 Debug( LDAP_DEBUG_ACL,
134 "<= acl_get: no acl applicable to database root\n", 0, 0,
139 Debug( LDAP_DEBUG_ARGS, "=> acl_get: edn %s\n", edn, 0, 0 );
141 /* check for a backend-specific acl that matches the entry */
142 for ( i = 1, a = be->be_acl; a != NULL; a = a->acl_next, i++ ) {
143 if (a->acl_dnpat != NULL) {
144 Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n",
145 i, a->acl_dnpat, a->acl_dnre.re_nsub);
147 if (regexec(&a->acl_dnre, edn, nmatch, matches, 0))
150 Debug( LDAP_DEBUG_TRACE, "=> acl_get:[%d] backend ACL match\n",
154 if ( a->acl_filter != NULL ) {
155 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
160 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr %s\n", i, attr, 0);
162 if ( attr == NULL || a->acl_attrs == NULL ||
163 charray_inlist( a->acl_attrs, attr ) )
165 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] backend acl %s attr: %s\n",
169 matches[0].rm_so = matches[0].rm_eo = -1;
172 /* check for a global acl that matches the entry */
173 for ( i = 1, a = global_acl; a != NULL; a = a->acl_next, i++ ) {
174 if (a->acl_dnpat != NULL) {
175 Debug( LDAP_DEBUG_TRACE, "=> dnpat: [%d] %s nsub: %d\n",
176 i, a->acl_dnpat, a->acl_dnre.re_nsub);
178 if (regexec(&a->acl_dnre, edn, nmatch, matches, 0)) {
181 Debug( LDAP_DEBUG_TRACE, "=> acl_get: [%d] global ACL match\n",
186 if ( a->acl_filter != NULL ) {
187 if ( test_filter( NULL, NULL, NULL, e, a->acl_filter ) != 0 ) {
192 Debug( LDAP_DEBUG_ARGS, "=> acl_get: [%d] check attr\n", i, 0, 0);
194 if ( attr == NULL || a->acl_attrs == NULL ||
195 charray_inlist( a->acl_attrs, attr ) )
197 Debug( LDAP_DEBUG_ACL, "<= acl_get: [%d] global acl %s attr: %s\n",
202 matches[0].rm_so = matches[0].rm_eo = -1;
205 Debug( LDAP_DEBUG_ACL, "<= acl_get: no match\n", 0, 0, 0 );
210 * acl_access_allowed - check whether the given acl allows dn the
211 * requested access to entry e, attribute attr, value val. if val
212 * is null, access to the whole attribute is assumed (all values).
214 * returns 0 access NOT allowed
238 Debug( LDAP_DEBUG_ACL,
239 "\n=> acl_access_allowed: %s access to entry \"%s\"\n",
240 access2str( access ), e->e_dn, 0 );
242 Debug( LDAP_DEBUG_ACL,
243 "\n=> acl_access_allowed: %s access to value \"%s\" by \"%s\"\n",
244 access2str( access ),
245 val ? val->bv_val : "any",
246 op->o_dn ? op->o_dn : "" );
248 if ( be_isroot( be, op->o_dn ) ) {
249 Debug( LDAP_DEBUG_ACL,
250 "<= acl_access_allowed: granted to database root\n",
255 default_access = be->be_dfltaccess ? be->be_dfltaccess : global_default_access;
258 Debug( LDAP_DEBUG_ACL,
259 "<= acl_access_allowed: %s by default (no matching to)\n",
260 default_access >= access ? "granted" : "denied", 0, 0 );
261 return( default_access >= access );
265 if ( op->o_dn != NULL ) {
266 odn = dn_normalize_case( strdup( op->o_dn ) );
268 bv.bv_len = strlen( odn );
270 for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
271 if ( b->a_dnpat != NULL ) {
272 Debug( LDAP_DEBUG_TRACE, "<= check a_dnpat: %s\n",
275 * if access applies to the entry itself, and the
276 * user is bound as somebody in the same namespace as
277 * the entry, OR the given dn matches the dn pattern
279 if ( strcasecmp( b->a_dnpat, "self" ) == 0 &&
280 op->o_dn != NULL && *(op->o_dn) && e->e_dn != NULL )
282 if ( strcasecmp( edn, op->o_dn ) == 0 ) {
283 Debug( LDAP_DEBUG_ACL,
284 "<= acl_access_allowed: matched by clause #%d access %s\n",
285 i, (b->a_access & ~ACL_SELF) >=
286 access ? "granted" : "denied", 0 );
288 if ( odn ) free( odn );
289 return( (b->a_access & ~ACL_SELF) >= access );
292 if ( regex_matches( b->a_dnpat, odn, edn, matches ) ) {
293 Debug( LDAP_DEBUG_ACL,
294 "<= acl_access_allowed: matched by clause #%d access %s\n",
295 i, (b->a_access & ~ACL_SELF) >= access ?
296 "granted" : "denied", 0 );
298 if ( odn ) free( odn );
299 return( (b->a_access & ~ACL_SELF) >= access );
303 if ( b->a_addrpat != NULL ) {
304 if ( regex_matches( b->a_addrpat, conn->c_addr, edn, matches ) ) {
305 Debug( LDAP_DEBUG_ACL,
306 "<= acl_access_allowed: matched by clause #%d access %s\n",
307 i, (b->a_access & ~ACL_SELF) >= access ?
308 "granted" : "denied", 0 );
310 if ( odn ) free( odn );
311 return( (b->a_access & ~ACL_SELF) >= access );
314 if ( b->a_domainpat != NULL ) {
315 Debug( LDAP_DEBUG_ARGS, "<= check a_domainpath: %s\n",
316 b->a_domainpat, 0, 0 );
317 if ( regex_matches( b->a_domainpat, conn->c_domain, edn, matches ) )
319 Debug( LDAP_DEBUG_ACL,
320 "<= acl_access_allowed: matched by clause #%d access %s\n",
321 i, (b->a_access & ~ACL_SELF) >= access ?
322 "granted" : "denied", 0 );
324 if ( odn ) free( odn );
325 return( (b->a_access & ~ACL_SELF) >= access );
328 if ( b->a_dnattr != NULL && op->o_dn != NULL ) {
329 Debug( LDAP_DEBUG_ARGS, "<= check a_dnattr: %s\n",
331 /* see if asker is listed in dnattr */
332 if ( (at = attr_find( e->e_attrs, b->a_dnattr )) != NULL &&
333 value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
335 if ( (b->a_access & ACL_SELF) &&
336 (val == NULL || value_cmp( &bv, val, at->a_syntax, 2 )) )
341 if ( odn ) free( odn );
342 Debug( LDAP_DEBUG_ACL,
343 "<= acl_acces_allowed: matched by clause #%d access %s\n",
344 i, (b->a_access & ~ACL_SELF) >= access ?
345 "granted" : "denied", 0 );
347 return( (b->a_access & ~ACL_SELF) >= access );
350 /* asker not listed in dnattr - check for self access */
351 if ( ! (b->a_access & ACL_SELF) || val == NULL ||
352 value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
357 if ( odn ) free( odn );
358 Debug( LDAP_DEBUG_ACL,
359 "<= acl_access_allowed: matched by clause #%d (self) access %s\n",
360 i, (b->a_access & ~ACL_SELF) >= access ? "granted"
363 return( (b->a_access & ~ACL_SELF) >= access );
366 if ( b->a_group != NULL && op->o_dn != NULL ) {
369 /* b->a_group is an unexpanded entry name, expanded it should be an
370 * entry with objectclass group* and we test to see if odn is one of
371 * the values in the attribute uniquegroup
373 Debug( LDAP_DEBUG_ARGS, "<= check a_group: %s\n",
375 Debug( LDAP_DEBUG_ARGS, "<= check a_group: odn: %s\n",
378 /* see if asker is listed in dnattr */
379 string_expand(buf, sizeof(buf), b->a_group, edn, matches);
381 if (be_group(be, buf, odn) == 0) {
382 Debug( LDAP_DEBUG_ACL,
383 "<= acl_access_allowed: matched by clause #%d (group) access granted\n",
385 if ( odn ) free( odn );
386 return( (b->a_access & ~ACL_SELF) >= access );
389 #endif /* ACLGROUP */
392 if ( odn ) free( odn );
393 Debug( LDAP_DEBUG_ACL,
394 "<= acl_access_allowed: %s by default (no matching by)\n",
395 default_access >= access ? "granted" : "denied", 0, 0 );
397 return( default_access >= access );
401 * acl_check_mods - check access control on the given entry to see if
402 * it allows the given modifications by the user associated with op.
403 * returns LDAP_SUCCESS mods allowed ok
404 * anything else mods not allowed - return is an error
405 * code indicating the problem
421 edn = dn_normalize_case( strdup( e->e_dn ) );
423 for ( ; mods != NULL; mods = mods->mod_next ) {
424 regmatch_t matches[MAXREMATCHES];
426 /* the lastmod attributes are ignored by ACL checking */
427 if ( strcasecmp( mods->mod_type, "modifiersname" ) == 0 ||
428 strcasecmp( mods->mod_type, "modifytimestamp" ) == 0 ||
429 strcasecmp( mods->mod_type, "creatorsname" ) == 0 ||
430 strcasecmp( mods->mod_type, "createtimestamp" ) == 0 )
432 Debug( LDAP_DEBUG_ACL, "LASTMOD attribute: %s access allowed\n",
433 mods->mod_type, 0, 0 );
437 a = acl_get_applicable( be, op, e, mods->mod_type, edn,
438 MAXREMATCHES, matches );
440 switch ( mods->mod_op & ~LDAP_MOD_BVALUES ) {
441 case LDAP_MOD_REPLACE:
443 if ( mods->mod_bvalues == NULL ) {
446 for ( i = 0; mods->mod_bvalues[i] != NULL; i++ ) {
447 if ( ! acl_access_allowed( a, be, conn, e, mods->mod_bvalues[i],
448 op, ACL_WRITE, edn, matches) )
451 return( LDAP_INSUFFICIENT_ACCESS );
456 case LDAP_MOD_DELETE:
457 if ( mods->mod_bvalues == NULL ) {
458 if ( ! acl_access_allowed( a, be, conn, e,
459 NULL, op, ACL_WRITE, edn, matches) )
462 return( LDAP_INSUFFICIENT_ACCESS );
466 for ( i = 0; mods->mod_bvalues[i] != NULL; i++ ) {
467 if ( ! acl_access_allowed( a, be, conn, e, mods->mod_bvalues[i],
468 op, ACL_WRITE, edn, matches) )
471 return( LDAP_INSUFFICIENT_ACCESS );
479 return( LDAP_SUCCESS );
482 static string_expand(
498 for ( dp = newbuf, sp = pat; size < 512 && *sp ; sp++) {
499 /* did we previously see a $ */
504 } else if (*sp >= '0' && *sp <= '9' ) {
512 i = matches[n].rm_so;
513 l = matches[n].rm_eo;
514 for ( ; size < 512 && i < l; size++, i++ ) {
532 Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern: %s\n", pat, 0, 0 );
533 Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
538 char *pat, /* pattern to expand and match against */
539 char *str, /* string to match against pattern */
540 char *buf, /* buffer with $N expansion variables */
541 regmatch_t *matches /* offsets in buffer for $N expansion variables */
548 string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
549 if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
551 regerror(rc, &re, error, sizeof(error));
553 Debug( LDAP_DEBUG_TRACE,
554 "compile( \"%s\", \"%s\") failed %s\n",
559 rc = regexec(&re, str, 0, NULL, 0);
562 Debug( LDAP_DEBUG_TRACE,
563 "=> regex_matches: string: %s\n", str, 0, 0 );
564 Debug( LDAP_DEBUG_TRACE,
565 "=> regex_matches: rc: %d %s\n",
566 rc, !rc ? "matches" : "no matches", 0 );