]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
c655bd1e28d87f48d18b889c2d23aa17d032de6b
[openldap] / servers / slapd / acl.c
1 /* acl.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/regex.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
34
35 #include "slap.h"
36 #include "sets.h"
37 #include "lber_pvt.h"
38
39 #ifdef LDAP_SLAPI
40 #include "slapi/slapi.h"
41 #endif /* LDAPI_SLAPI */
42
43 #define ACL_BUF_SIZE    1024    /* use most appropriate size */
44
45 /*
46  * speed up compares
47  */
48 static struct berval 
49         aci_bv_entry            = BER_BVC("entry"),
50         aci_bv_children         = BER_BVC("children"),
51         aci_bv_br_entry         = BER_BVC("[entry]"),
52         aci_bv_br_all           = BER_BVC("[all]"),
53         aci_bv_access_id        = BER_BVC("access-id"),
54         aci_bv_anonymous        = BER_BVC("anonymous"),
55         aci_bv_public           = BER_BVC("public"),
56         aci_bv_users            = BER_BVC("users"),
57         aci_bv_self             = BER_BVC("self"),
58         aci_bv_dnattr           = BER_BVC("dnattr"),
59         aci_bv_group            = BER_BVC("group"),
60         aci_bv_role             = BER_BVC("role"),
61         aci_bv_set              = BER_BVC("set"),
62         aci_bv_set_ref          = BER_BVC("set-ref"),
63         aci_bv_grant            = BER_BVC("grant"),
64         aci_bv_deny             = BER_BVC("deny"),
65
66         aci_bv_ip_eq            = BER_BVC("IP="),
67 #ifdef LDAP_PF_LOCAL
68         aci_bv_path_eq          = BER_BVC("PATH="),
69         aci_bv_dirsep           = BER_BVC(LDAP_DIRSEP),
70 #endif /* LDAP_PF_LOCAL */
71         
72         aci_bv_group_class      = BER_BVC(SLAPD_GROUP_CLASS),
73         aci_bv_group_attr       = BER_BVC(SLAPD_GROUP_ATTR),
74         aci_bv_role_class       = BER_BVC(SLAPD_ROLE_CLASS),
75         aci_bv_role_attr        = BER_BVC(SLAPD_ROLE_ATTR),
76         aci_bv_set_attr         = BER_BVC(SLAPD_ACI_SET_ATTR);
77
78
79 static AccessControl * acl_get(
80         AccessControl *ac, int *count,
81         Operation *op, Entry *e,
82         AttributeDescription *desc,
83         struct berval *val,
84         int nmatches, regmatch_t *matches,
85         AccessControlState *state );
86
87 static slap_control_t acl_mask(
88         AccessControl *ac, slap_mask_t *mask,
89         Operation *op, Entry *e,
90         AttributeDescription *desc,
91         struct berval *val,
92         regmatch_t *matches,
93         int count,
94         AccessControlState *state );
95
96 #ifdef SLAPD_ACI_ENABLED
97 static int aci_mask(
98         Operation *op, Entry *e,
99         AttributeDescription *desc,
100         struct berval *val,
101         struct berval *aci,
102         regmatch_t *matches,
103         slap_access_t *grant,
104         slap_access_t *deny,
105         struct berval *scope);
106 #endif
107
108 static int      regex_matches(
109         struct berval *pat, char *str, char *buf, regmatch_t *matches);
110 static int      string_expand(
111         struct berval *newbuf, struct berval *pattern,
112         char *match, regmatch_t *matches);
113
114 typedef struct AciSetCookie {
115         Operation *op;
116         Entry *e;
117 } AciSetCookie;
118
119 SLAP_SET_GATHER aci_set_gather;
120 static int aci_match_set ( struct berval *subj, Operation *op,
121     Entry *e, int setref );
122
123 /*
124  * access_allowed - check whether op->o_ndn is allowed the requested access
125  * to entry e, attribute attr, value val.  if val is null, access to
126  * the whole attribute is assumed (all values).
127  *
128  * This routine loops through all access controls and calls
129  * acl_mask() on each applicable access control.
130  * The loop exits when a definitive answer is reached or
131  * or no more controls remain.
132  *
133  * returns:
134  *              0       access denied
135  *              1       access granted
136  *
137  * Notes:
138  * - can be legally called with op == NULL
139  * - can be legally called with op->o_bd == NULL
140  */
141
142 int
143 access_allowed_mask(
144         Operation               *op,
145         Entry           *e,
146         AttributeDescription    *desc,
147         struct berval   *val,
148         slap_access_t   access,
149         AccessControlState *state,
150         slap_mask_t *maskp )
151 {
152         int                             ret = 1;
153         int                             count;
154         AccessControl                   *a = NULL;
155         Backend *be;
156         int     be_null = 0;
157
158 #ifdef LDAP_DEBUG
159         char accessmaskbuf[ACCESSMASK_MAXLEN];
160 #endif
161         slap_mask_t mask;
162         slap_control_t control;
163         const char *attr;
164         regmatch_t matches[MAXREMATCHES];
165         int        st_same_attr = 0;
166         static AccessControlState state_init = ACL_STATE_INIT;
167
168         assert( e != NULL );
169         assert( desc != NULL );
170         assert( access > ACL_NONE );
171         if ( maskp ) ACL_INVALIDATE( *maskp );
172
173         attr = desc->ad_cname.bv_val;
174
175         assert( attr != NULL );
176
177         if( op && op->o_is_auth_check &&
178                 ( access == ACL_SEARCH || access == ACL_READ ))
179         {
180                 access = ACL_AUTH;
181         }
182
183         if( state ) {
184                 if ( state->as_vd_ad==desc) {
185                         if ( state->as_recorded ) {
186                                 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
187                                         val == NULL )
188                                 {
189                                         return state->as_result;
190                                 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
191                                         val != NULL && state->as_vd_acl == NULL )
192                                 {
193                                         return state->as_result;
194                                 }
195                         }
196                         st_same_attr = 1;
197                 } else {
198                         *state = state_init;
199                 }
200
201                 state->as_vd_ad=desc;
202         }
203
204         Debug( LDAP_DEBUG_ACL,
205                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
206             access2str( access ), e->e_dn, attr );
207
208         if ( op == NULL ) {
209                 /* no-op call */
210                 goto done;
211         }
212
213         be = op->o_bd;
214         if ( be == NULL ) {
215                 be = &backends[0];
216                 be_null = 1;
217 #ifdef LDAP_DEVEL
218                 /*
219                  * FIXME: experimental; use first backend rules
220                  * iff there is no global_acl (ITS#3100) */
221                 if ( frontendDB->be_acl == NULL ) 
222 #endif
223                 {
224                         op->o_bd = be;
225                 }
226         }
227         assert( be != NULL );
228
229 #ifdef LDAP_SLAPI
230         if ( op->o_pb != NULL ) {
231                 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
232                 if ( ret == 0 ) {
233                         /* ACL plugin denied access */
234                         goto done;
235                 }
236         }
237 #endif /* LDAP_SLAPI */
238
239         /* grant database root access */
240         if ( be != NULL && be_isroot( op ) ) {
241                 Debug( LDAP_DEBUG_ACL,
242                     "<= root access granted\n",
243                         0, 0, 0 );
244                 if ( maskp ) {
245                         mask = ACL_LVL_WRITE;
246                 }
247
248                 goto done;
249         }
250
251         /*
252          * no-user-modification operational attributes are ignored
253          * by ACL_WRITE checking as any found here are not provided
254          * by the user
255          */
256         if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
257                 && desc != slap_schema.si_ad_entry
258                 && desc != slap_schema.si_ad_children )
259         {
260                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
261                         " %s access granted\n",
262                         attr, 0, 0 );
263                 goto done;
264         }
265
266         /* use backend default access if no backend acls */
267         if( be != NULL && be->be_acl == NULL ) {
268                 Debug( LDAP_DEBUG_ACL,
269                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
270                         access2str( access ),
271                         be->be_dfltaccess >= access ? "granted" : "denied",
272                         op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
273                 ret = be->be_dfltaccess >= access;
274
275                 if ( maskp ) {
276                         int     i;
277
278                         mask = ACL_PRIV_LEVEL;
279                         for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
280                                 mask |= ACL_ACCESS2PRIV( i );
281                         }
282                 }
283
284                 goto done;
285
286 #ifdef notdef
287         /* be is always non-NULL */
288         /* use global default access if no global acls */
289         } else if ( be == NULL && frontendDB->be_acl == NULL ) {
290                 Debug( LDAP_DEBUG_ACL,
291                         "=> access_allowed: global default %s access %s to \"%s\"\n",
292                         access2str( access ),
293                         frontendDB->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
294                 ret = frontendDB->be_dfltaccess >= access;
295
296                 if ( maskp ) {
297                         int     i;
298
299                         mask = ACL_PRIV_LEVEL;
300                         for ( i = ACL_NONE; i <= global_default_access; i++ ) {
301                                 mask |= ACL_ACCESS2PRIV( i );
302                         }
303                 }
304
305                 goto done;
306 #endif
307         }
308
309         ret = 0;
310         control = ACL_BREAK;
311
312         if( st_same_attr ) {
313                 assert( state->as_vd_acl != NULL );
314
315                 a = state->as_vd_acl;
316                 count = state->as_vd_acl_count;
317                 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
318                         mask = state->as_vd_acl_mask;
319                         AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
320                         goto vd_access;
321                 }
322
323         } else {
324                 if ( state ) state->as_vi_acl = NULL;
325                 a = NULL;
326                 ACL_INIT(mask);
327                 count = 0;
328                 memset(matches, '\0', sizeof(matches));
329         }
330
331         while((a = acl_get( a, &count, op, e, desc, val,
332                 MAXREMATCHES, matches, state )) != NULL)
333         {
334                 int i;
335
336                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
337                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
338                             (int)matches[i].rm_so, (int)matches[i].rm_eo );
339                         if( matches[i].rm_so <= matches[0].rm_eo ) {
340                                 int n;
341                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
342                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
343                                 }
344                         }
345                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
346                 }
347
348                 if (state) {
349                         if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
350                                 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
351                                 ret = state->as_result;
352                                 goto done;
353                         } else {
354                                 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
355                         }
356                 }
357
358 vd_access:
359                 control = acl_mask( a, &mask, op,
360                         e, desc, val, matches, count, state );
361
362                 if ( control != ACL_BREAK ) {
363                         break;
364                 }
365
366                 memset(matches, '\0', sizeof(matches));
367         }
368
369         if ( ACL_IS_INVALID( mask ) ) {
370                 Debug( LDAP_DEBUG_ACL,
371                         "=> access_allowed: \"%s\" (%s) invalid!\n",
372                         e->e_dn, attr, 0 );
373                 ACL_INIT(mask);
374
375         } else if ( control == ACL_BREAK ) {
376                 Debug( LDAP_DEBUG_ACL,
377                         "=> access_allowed: no more rules\n", 0, 0, 0);
378
379                 goto done;
380         }
381
382         Debug( LDAP_DEBUG_ACL,
383                 "=> access_allowed: %s access %s by %s\n",
384                 access2str( access ),
385                 ACL_GRANT(mask, access) ? "granted" : "denied",
386                 accessmask2str( mask, accessmaskbuf ) );
387
388         ret = ACL_GRANT(mask, access);
389
390 done:
391         if( state != NULL ) {
392                 /* If not value-dependent, save ACL in case of more attrs */
393                 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
394                         state->as_vi_acl = a;
395                         state->as_result = ret;
396                 }
397                 state->as_recorded |= ACL_STATE_RECORDED;
398         }
399         if (be_null) op->o_bd = NULL;
400         if ( maskp ) *maskp = mask;
401         return ret;
402 }
403
404
405 /*
406  * acl_get - return the acl applicable to entry e, attribute
407  * attr.  the acl returned is suitable for use in subsequent calls to
408  * acl_access_allowed().
409  */
410
411 static AccessControl *
412 acl_get(
413         AccessControl *a,
414         int                     *count,
415         Operation       *op,
416         Entry           *e,
417         AttributeDescription *desc,
418         struct berval   *val,
419         int                     nmatch,
420         regmatch_t      *matches,
421         AccessControlState *state )
422 {
423         const char *attr;
424         int dnlen, patlen;
425         AccessControl *prev;
426
427         assert( e != NULL );
428         assert( count != NULL );
429         assert( desc != NULL );
430
431         attr = desc->ad_cname.bv_val;
432
433         assert( attr != NULL );
434
435         if( a == NULL ) {
436                 if( op->o_bd == NULL ) {
437                         a = frontendDB->be_acl;
438                 } else {
439                         a = op->o_bd->be_acl;
440                 }
441                 prev = NULL;
442
443                 assert( a != NULL );
444
445         } else {
446                 prev = a;
447                 a = a->acl_next;
448         }
449
450         dnlen = e->e_nname.bv_len;
451
452         for ( ; a != NULL; a = a->acl_next ) {
453                 (*count) ++;
454
455                 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
456                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
457                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
458                                         *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
459                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
460                                         continue;
461
462                         } else {
463                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
464                                         *count, a->acl_dn_pat.bv_val, 0 );
465                                 patlen = a->acl_dn_pat.bv_len;
466                                 if ( dnlen < patlen )
467                                         continue;
468
469                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
470                                         /* base dn -- entire object DN must match */
471                                         if ( dnlen != patlen )
472                                                 continue;
473
474                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
475                                         int     rdnlen = -1, sep = 0;
476
477                                         if ( dnlen <= patlen )
478                                                 continue;
479
480                                         if ( patlen > 0 ) {
481                                                 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
482                                                         continue;
483                                                 sep = 1;
484                                         }
485
486                                         rdnlen = dn_rdnlen( NULL, &e->e_nname );
487                                         if ( rdnlen != dnlen - patlen - sep )
488                                                 continue;
489
490                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
491                                         if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
492                                                 continue;
493
494                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
495                                         if ( dnlen <= patlen )
496                                                 continue;
497                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
498                                                 continue;
499                                 }
500
501                                 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
502                                         continue;
503                         }
504
505                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
506                                 *count, 0, 0 );
507                 }
508
509                 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
510                         matches[0].rm_so = matches[0].rm_eo = -1;
511                         continue;
512                 }
513
514                 /* Is this ACL only for a specific value? */
515                 if ( a->acl_attrval.bv_len ) {
516                         if ( val == NULL ) {
517                                 continue;
518                         }
519
520                         if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
521                                 state->as_recorded |= ACL_STATE_RECORDED_VD;
522                                 state->as_vd_acl = prev;
523                                 state->as_vd_acl_count = *count;
524                                 state->as_vd_access = a->acl_access;
525                                 state->as_vd_access_count = 1;
526                                 ACL_INVALIDATE( state->as_vd_acl_mask );
527                         }
528
529                         if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
530                                 Debug( LDAP_DEBUG_ACL,
531                                         "acl_get: valpat %s\n",
532                                         a->acl_attrval.bv_val, 0, 0 );
533                                 if (regexec(&a->acl_attrval_re, val->bv_val, 0, NULL, 0))
534                                         continue;
535                         } else {
536                                 int match = 0;
537                                 const char *text;
538                                 Debug( LDAP_DEBUG_ACL,
539                                         "acl_get: val %s\n",
540                                         a->acl_attrval.bv_val, 0, 0 );
541         
542                                 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
543                                         if (value_match( &match, desc,
544                                                 desc->ad_type->sat_equality, 0,
545                                                 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
546                                                         match )
547                                                 continue;
548                                         
549                                 } else {
550                                         int             patlen, vdnlen;
551         
552                                         patlen = a->acl_attrval.bv_len;
553                                         vdnlen = val->bv_len;
554         
555                                         if ( vdnlen < patlen )
556                                                 continue;
557         
558                                         if ( a->acl_dn_style == ACL_STYLE_BASE ) {
559                                                 if ( vdnlen > patlen )
560                                                         continue;
561         
562                                         } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
563                                                 int rdnlen = -1;
564         
565                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
566                                                         continue;
567         
568                                                 rdnlen = dn_rdnlen( NULL, val );
569                                                 if ( rdnlen != vdnlen - patlen - 1 )
570                                                         continue;
571         
572                                         } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
573                                                 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
574                                                         continue;
575         
576                                         } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
577                                                 if ( vdnlen <= patlen )
578                                                         continue;
579         
580                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
581                                                         continue;
582                                         }
583         
584                                         if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
585                                                 continue;
586                                 }
587                         }
588                 }
589
590                 if ( a->acl_filter != NULL ) {
591                         ber_int_t rc = test_filter( NULL, e, a->acl_filter );
592                         if ( rc != LDAP_COMPARE_TRUE ) {
593                                 continue;
594                         }
595                 }
596
597                 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
598                        *count, attr, 0);
599                 return a;
600         }
601
602         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
603         return( NULL );
604 }
605
606 /*
607  * Record value-dependent access control state
608  */
609 #define ACL_RECORD_VALUE_STATE do { \
610                 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
611                         state->as_recorded |= ACL_STATE_RECORDED_VD; \
612                         state->as_vd_acl = a; \
613                         AC_MEMCPY( state->as_vd_acl_matches, matches, \
614                                 sizeof( state->as_vd_acl_matches )) ; \
615                         state->as_vd_acl_count = count; \
616                         state->as_vd_access = b; \
617                         state->as_vd_access_count = i; \
618                 } \
619         } while( 0 )
620
621 /*
622  * acl_mask - modifies mask based upon the given acl and the
623  * requested access to entry e, attribute attr, value val.  if val
624  * is null, access to the whole attribute is assumed (all values).
625  *
626  * returns      0       access NOT allowed
627  *              1       access allowed
628  */
629
630 static slap_control_t
631 acl_mask(
632         AccessControl   *a,
633         slap_mask_t *mask,
634         Operation       *op,
635         Entry           *e,
636         AttributeDescription *desc,
637         struct berval   *val,
638         regmatch_t      *matches,
639         int     count,
640         AccessControlState *state )
641 {
642         int             i, odnlen, patlen;
643         Access  *b;
644 #ifdef LDAP_DEBUG
645         char accessmaskbuf[ACCESSMASK_MAXLEN];
646         char accessmaskbuf1[ACCESSMASK_MAXLEN];
647 #endif
648         const char *attr;
649
650         assert( a != NULL );
651         assert( mask != NULL );
652         assert( desc != NULL );
653
654         attr = desc->ad_cname.bv_val;
655
656         assert( attr != NULL );
657
658         Debug( LDAP_DEBUG_ACL,
659                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
660                 e->e_dn, attr, 0 );
661
662         Debug( LDAP_DEBUG_ACL,
663                 "=> acl_mask: to %s by \"%s\", (%s) \n",
664                 val ? "value" : "all values",
665                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
666                 accessmask2str( *mask, accessmaskbuf ) );
667
668
669         if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
670                 && state->as_vd_acl == a )
671         {
672                 b = state->as_vd_access;
673                 i = state->as_vd_access_count;
674
675         } else {
676                 b = a->acl_access;
677                 i = 1;
678         }
679
680         for ( ; b != NULL; b = b->a_next, i++ ) {
681                 slap_mask_t oldmask, modmask;
682
683                 ACL_INVALIDATE( modmask );
684
685                 /* AND <who> clauses */
686                 if ( b->a_dn_pat.bv_len != 0 ) {
687                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
688                                 b->a_dn_pat.bv_val, 0, 0);
689                         /*
690                          * if access applies to the entry itself, and the
691                          * user is bound as somebody in the same namespace as
692                          * the entry, OR the given dn matches the dn pattern
693                          */
694                         if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
695                                 if ( op->o_ndn.bv_len != 0 ) {
696                                         continue;
697                                 }
698
699                         } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
700                                 if ( op->o_ndn.bv_len == 0 ) {
701                                         continue;
702                                 }
703
704                         } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
705                                 if ( op->o_ndn.bv_len == 0 ) {
706                                         continue;
707                                 }
708                                 
709                                 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
710                                         continue;
711                                 }
712
713                         } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
714                                 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
715                                         int ret = regex_matches( &b->a_dn_pat,
716                                                 op->o_ndn.bv_val, e->e_ndn, matches );
717
718                                         if( ret == 0 ) {
719                                                 continue;
720                                         }
721                                 }
722
723                         } else {
724                                 struct berval pat;
725                                 int got_match = 0;
726
727                                 if ( e->e_dn == NULL )
728                                         continue;
729
730                                 if ( b->a_dn_expand ) {
731                                         struct berval bv;
732                                         char buf[ACL_BUF_SIZE];
733
734                                         bv.bv_len = sizeof( buf ) - 1;
735                                         bv.bv_val = buf;
736
737                                         if ( string_expand(&bv, &b->a_dn_pat, 
738                                                         e->e_ndn, matches) )
739                                         {
740                                                 continue;
741                                         }
742                                         if ( dnNormalize(0, NULL, NULL, &bv,
743                                                         &pat, op->o_tmpmemctx )
744                                                         != LDAP_SUCCESS )
745                                         {
746                                                 /* did not expand to a valid dn */
747                                                 continue;
748                                         }
749                                 } else {
750                                         pat = b->a_dn_pat;
751                                 }
752
753                                 patlen = pat.bv_len;
754                                 odnlen = op->o_ndn.bv_len;
755                                 if ( odnlen < patlen ) {
756                                         goto dn_match_cleanup;
757
758                                 }
759
760                                 if ( b->a_dn_style == ACL_STYLE_BASE ) {
761                                         /* base dn -- entire object DN must match */
762                                         if ( odnlen != patlen ) {
763                                                 goto dn_match_cleanup;
764                                         }
765
766                                 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
767                                         int rdnlen = -1;
768
769                                         if ( odnlen <= patlen ) {
770                                                 goto dn_match_cleanup;
771                                         }
772
773                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
774                                                 goto dn_match_cleanup;
775                                         }
776
777                                         rdnlen = dn_rdnlen( NULL, &op->o_ndn );
778                                         if ( rdnlen != odnlen - patlen - 1 ) {
779                                                 goto dn_match_cleanup;
780                                         }
781
782                                 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
783                                         if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
784                                                 goto dn_match_cleanup;
785                                         }
786
787                                 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
788                                         if ( odnlen <= patlen ) {
789                                                 goto dn_match_cleanup;
790                                         }
791
792                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
793                                                 goto dn_match_cleanup;
794                                         }
795                                 }
796
797                                 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
798
799 dn_match_cleanup:;
800                                 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
801                                         free( pat.bv_val );
802                                 }
803
804                                 if ( !got_match ) {
805                                         continue;
806                                 }
807                         }
808                 }
809
810                 if ( b->a_sockurl_pat.bv_len ) {
811                         if ( ! op->o_conn->c_listener ) {
812                                 continue;
813                         }
814                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
815                                 b->a_sockurl_pat.bv_val, 0, 0 );
816
817                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
818                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
819                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
820                                                         e->e_ndn, matches ) ) 
821                                         {
822                                                 continue;
823                                         }
824
825                                 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
826                                         struct berval   bv;
827                                         char buf[ACL_BUF_SIZE];
828
829                                         bv.bv_len = sizeof( buf ) - 1;
830                                         bv.bv_val = buf;
831                                         if ( string_expand( &bv, &b->a_sockurl_pat,
832                                                         e->e_ndn, matches ) )
833                                         {
834                                                 continue;
835                                         }
836
837                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
838                                         {
839                                                 continue;
840                                         }
841
842                                 } else {
843                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
844                                         {
845                                                 continue;
846                                         }
847                                 }
848                         }
849                 }
850
851                 if ( b->a_domain_pat.bv_len ) {
852                         if ( !op->o_conn->c_peer_domain.bv_val ) {
853                                 continue;
854                         }
855                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
856                                 b->a_domain_pat.bv_val, 0, 0 );
857                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
858                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
859                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
860                                                         e->e_ndn, matches ) ) 
861                                         {
862                                                 continue;
863                                         }
864                                 } else {
865                                         char buf[ACL_BUF_SIZE];
866
867                                         struct berval   cmp = op->o_conn->c_peer_domain;
868                                         struct berval   pat = b->a_domain_pat;
869
870                                         if ( b->a_domain_expand ) {
871                                                 struct berval bv;
872
873                                                 bv.bv_len = sizeof(buf) - 1;
874                                                 bv.bv_val = buf;
875
876                                                 if ( string_expand(&bv, &b->a_domain_pat,
877                                                                 e->e_ndn, matches) )
878                                                 {
879                                                         continue;
880                                                 }
881                                                 pat = bv;
882                                         }
883
884                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
885                                                 int offset = cmp.bv_len - pat.bv_len;
886                                                 if ( offset < 0 ) {
887                                                         continue;
888                                                 }
889
890                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
891                                                         continue;
892                                                 }
893
894                                                 /* trim the domain */
895                                                 cmp.bv_val = &cmp.bv_val[ offset ];
896                                                 cmp.bv_len -= offset;
897                                         }
898                                         
899                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
900                                                 continue;
901                                         }
902                                 }
903                         }
904                 }
905
906                 if ( b->a_peername_pat.bv_len ) {
907                         if ( !op->o_conn->c_peer_name.bv_val ) {
908                                 continue;
909                         }
910                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
911                                 b->a_peername_pat.bv_val, 0, 0 );
912                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
913                                 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
914                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
915                                                         e->e_ndn, matches ) ) 
916                                         {
917                                                 continue;
918                                         }
919
920                                 } else {
921                                         /* try exact match */
922                                         if ( b->a_peername_style == ACL_STYLE_BASE ) {
923                                                 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
924                                                         continue;
925                                                 }
926
927                                         } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
928                                                 struct berval   bv;
929                                                 char buf[ACL_BUF_SIZE];
930
931                                                 bv.bv_len = sizeof( buf ) - 1;
932                                                 bv.bv_val = buf;
933                                                 if ( string_expand( &bv, &b->a_peername_pat,
934                                                                 e->e_ndn, matches ) )
935                                                 {
936                                                         continue;
937                                                 }
938
939                                                 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
940                                                         continue;
941                                                 }
942
943                                         /* extract IP and try exact match */
944                                         } else if ( b->a_peername_style == ACL_STYLE_IP ) {
945                                                 char            *port;
946                                                 char            buf[] = "255.255.255.255";
947                                                 struct berval   ip;
948                                                 unsigned long   addr;
949                                                 int             port_number = -1;
950                                                 
951                                                 if ( strncasecmp( op->o_conn->c_peer_name.bv_val, 
952                                                                         aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 ) 
953                                                         continue;
954
955                                                 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
956                                                 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
957
958                                                 port = strrchr( ip.bv_val, ':' );
959                                                 if ( port ) {
960                                                         char    *next;
961                                                         
962                                                         ip.bv_len = port - ip.bv_val;
963                                                         ++port;
964                                                         port_number = strtol( port, &next, 10 );
965                                                         if ( next[0] != '\0' )
966                                                                 continue;
967                                                 }
968                                                 
969                                                 /* the port check can be anticipated here */
970                                                 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
971                                                         continue;
972                                                 
973                                                 /* address longer than expected? */
974                                                 if ( ip.bv_len >= sizeof(buf) )
975                                                         continue;
976
977                                                 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
978                                                 buf[ ip.bv_len ] = '\0';
979
980                                                 addr = inet_addr( buf );
981
982                                                 /* unable to convert? */
983                                                 if ( addr == (unsigned long)(-1) )
984                                                         continue;
985
986                                                 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
987                                                         continue;
988
989 #ifdef LDAP_PF_LOCAL
990                                         /* extract path and try exact match */
991                                         } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
992                                                 struct berval path;
993                                                 
994                                                 if ( strncmp( op->o_conn->c_peer_name.bv_val,
995                                                                         aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
996                                                         continue;
997
998                                                 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
999                                                 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1000
1001                                                 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1002                                                         continue;
1003
1004 #endif /* LDAP_PF_LOCAL */
1005
1006                                         /* exact match (very unlikely...) */
1007                                         } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1008                                                         continue;
1009                                         }
1010                                 }
1011                         }
1012                 }
1013
1014                 if ( b->a_sockname_pat.bv_len ) {
1015                         if ( !op->o_conn->c_sock_name.bv_val ) {
1016                                 continue;
1017                         }
1018                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1019                                 b->a_sockname_pat.bv_val, 0, 0 );
1020                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1021                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1022                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1023                                                         e->e_ndn, matches ) ) 
1024                                         {
1025                                                 continue;
1026                                         }
1027
1028                                 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1029                                         struct berval   bv;
1030                                         char buf[ACL_BUF_SIZE];
1031
1032                                         bv.bv_len = sizeof( buf ) - 1;
1033                                         bv.bv_val = buf;
1034                                         if ( string_expand( &bv, &b->a_sockname_pat,
1035                                                         e->e_ndn, matches ) )
1036                                         {
1037                                                 continue;
1038                                         }
1039
1040                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1041                                                 continue;
1042                                         }
1043
1044                                 } else {
1045                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1046                                                 continue;
1047                                         }
1048                                 }
1049                         }
1050                 }
1051
1052                 if ( b->a_dn_at != NULL ) {
1053                         Attribute       *at;
1054                         struct berval   bv;
1055                         int rc, match = 0;
1056                         const char *text;
1057                         const char *attr = b->a_dn_at->ad_cname.bv_val;
1058
1059                         assert( attr != NULL );
1060
1061                         if ( op->o_ndn.bv_len == 0 ) {
1062                                 continue;
1063                         }
1064
1065                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1066                                 attr, 0, 0);
1067                         bv = op->o_ndn;
1068
1069                         /* see if asker is listed in dnattr */
1070                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
1071                                 at != NULL;
1072                                 at = attrs_find( at->a_next, b->a_dn_at ) )
1073                         {
1074                                 if( value_find_ex( b->a_dn_at,
1075                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1076                                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1077                                         at->a_nvals,
1078                                         &bv, op->o_tmpmemctx ) == 0 )
1079                                 {
1080                                         /* found it */
1081                                         match = 1;
1082                                         break;
1083                                 }
1084                         }
1085
1086                         if( match ) {
1087                                 /* have a dnattr match. if this is a self clause then
1088                                  * the target must also match the op dn.
1089                                  */
1090                                 if ( b->a_dn_self ) {
1091                                         /* check if the target is an attribute. */
1092                                         if ( val == NULL ) continue;
1093
1094                                         /* target is attribute, check if the attribute value
1095                                          * is the op dn.
1096                                          */
1097                                         rc = value_match( &match, b->a_dn_at,
1098                                                 b->a_dn_at->ad_type->sat_equality, 0,
1099                                                 val, &bv, &text );
1100                                         /* on match error or no match, fail the ACL clause */
1101                                         if (rc != LDAP_SUCCESS || match != 0 )
1102                                                 continue;
1103                                 }
1104                         } else {
1105                                 /* no dnattr match, check if this is a self clause */
1106                                 if ( ! b->a_dn_self )
1107                                         continue;
1108
1109                                 ACL_RECORD_VALUE_STATE;
1110                                 
1111                                 /* this is a self clause, check if the target is an
1112                                  * attribute.
1113                                  */
1114                                 if ( val == NULL )
1115                                         continue;
1116
1117                                 /* target is attribute, check if the attribute value
1118                                  * is the op dn.
1119                                  */
1120                                 rc = value_match( &match, b->a_dn_at,
1121                                         b->a_dn_at->ad_type->sat_equality, 0,
1122                                         val, &bv, &text );
1123
1124                                 /* on match error or no match, fail the ACL clause */
1125                                 if (rc != LDAP_SUCCESS || match != 0 )
1126                                         continue;
1127                         }
1128                 }
1129
1130                 if ( b->a_group_pat.bv_len ) {
1131                         struct berval bv;
1132                         struct berval ndn = BER_BVNULL;
1133                         int rc;
1134
1135                         if ( op->o_ndn.bv_len == 0 ) {
1136                                 continue;
1137                         }
1138
1139                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1140                          * entry with objectclass group* and we test to see if odn is one of
1141                          * the values in the attribute group
1142                          */
1143                         /* see if asker is listed in dnattr */
1144                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1145                                 char buf[ACL_BUF_SIZE];
1146                                 bv.bv_len = sizeof(buf) - 1;
1147                                 bv.bv_val = buf; 
1148
1149                                 if ( string_expand( &bv, &b->a_group_pat,
1150                                                 e->e_ndn, matches ) )
1151                                 {
1152                                         continue;
1153                                 }
1154                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) != LDAP_SUCCESS ) {
1155                                         /* did not expand to a valid dn */
1156                                         continue;
1157                                 }
1158
1159                                 bv = ndn;
1160
1161                         } else {
1162                                 bv = b->a_group_pat;
1163                         }
1164
1165                         rc = backend_group( op, e, &bv, &op->o_ndn,
1166                                 b->a_group_oc, b->a_group_at );
1167
1168                         if ( ndn.bv_val ) free( ndn.bv_val );
1169
1170                         if ( rc != 0 ) {
1171                                 continue;
1172                         }
1173                 }
1174
1175                 if ( b->a_set_pat.bv_len != 0 ) {
1176                         struct berval bv;
1177                         char buf[ACL_BUF_SIZE];
1178                         if( b->a_set_style == ACL_STYLE_REGEX ){
1179                                 bv.bv_len = sizeof(buf) - 1;
1180                                 bv.bv_val = buf;
1181                                 if ( string_expand( &bv, &b->a_set_pat,
1182                                                 e->e_ndn, matches ) )
1183                                 {
1184                                         continue;
1185                                 }
1186                         }else{
1187                                 bv = b->a_set_pat;
1188                         }
1189                         if (aci_match_set( &bv, op, e, 0 ) == 0) {
1190                                 continue;
1191                         }
1192                 }
1193
1194                 if ( b->a_authz.sai_ssf ) {
1195                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1196                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1197                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1198                                 continue;
1199                         }
1200                 }
1201
1202                 if ( b->a_authz.sai_transport_ssf ) {
1203                         Debug( LDAP_DEBUG_ACL,
1204                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1205                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1206                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1207                                 continue;
1208                         }
1209                 }
1210
1211                 if ( b->a_authz.sai_tls_ssf ) {
1212                         Debug( LDAP_DEBUG_ACL,
1213                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1214                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1215                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1216                                 continue;
1217                         }
1218                 }
1219
1220                 if ( b->a_authz.sai_sasl_ssf ) {
1221                         Debug( LDAP_DEBUG_ACL,
1222                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1223                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1224                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1225                                 continue;
1226                         }
1227                 }
1228
1229 #ifdef SLAPD_ACI_ENABLED
1230                 if ( b->a_aci_at != NULL ) {
1231                         Attribute       *at;
1232                         slap_access_t grant, deny, tgrant, tdeny;
1233                         struct berval parent_ndn, old_parent_ndn;
1234                         BerVarray bvals = NULL;
1235                         int ret,stop;
1236
1237                         /* this case works different from the others above.
1238                          * since aci's themselves give permissions, we need
1239                          * to first check b->a_access_mask, the ACL's access level.
1240                          */
1241
1242                         if ( e->e_nname.bv_len == 0 ) {
1243                                 /* no ACIs in the root DSE */
1244                                 continue;
1245                         }
1246
1247                         /* first check if the right being requested
1248                          * is allowed by the ACL clause.
1249                          */
1250                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1251                                 continue;
1252                         }
1253                         /* start out with nothing granted, nothing denied */
1254                         ACL_INIT(tgrant);
1255                         ACL_INIT(tdeny);
1256
1257                         /* get the aci attribute */
1258                         at = attr_find( e->e_attrs, b->a_aci_at );
1259                         if ( at != NULL ) {
1260 #if 0
1261                                 /* FIXME: this breaks acl caching;
1262                                  * see also ACL_RECORD_VALUE_STATE below */
1263                                 ACL_RECORD_VALUE_STATE;
1264 #endif
1265                                 /* the aci is an multi-valued attribute.  The
1266                                 * rights are determined by OR'ing the individual
1267                                 * rights given by the acis.
1268                                 */
1269                                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1270                                         if (aci_mask( op,
1271                                                 e, desc, val,
1272                                                 &at->a_nvals[i],
1273                                                 matches, &grant, &deny,  &aci_bv_entry ) != 0)
1274                                         {
1275                                                 tgrant |= grant;
1276                                                 tdeny |= deny;
1277                                         }
1278                                 }
1279                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1280                                           accessmask2str(tgrant,accessmaskbuf), 
1281                                           accessmask2str(tdeny, accessmaskbuf1), 0);
1282
1283                         }
1284                         /* If the entry level aci didn't contain anything valid for the 
1285                          * current operation, climb up the tree and evaluate the
1286                          * acis with scope set to subtree
1287                          */
1288                         if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1289                                 dnParent(&(e->e_nname), &parent_ndn);
1290                                 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1291                                         old_parent_ndn = parent_ndn;
1292                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1293                                         ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1294                                         switch(ret){
1295                                         case LDAP_SUCCESS :
1296                                                 stop = 0;
1297                                                 if (!bvals){
1298                                                         break;
1299                                                 }
1300
1301                                                 for( i = 0; bvals[i].bv_val != NULL; i++){
1302 #if 0
1303                                                         /* FIXME: this breaks acl caching;
1304                                                          * see also ACL_RECORD_VALUE_STATE above */
1305                                                         ACL_RECORD_VALUE_STATE;
1306 #endif
1307                                                         if (aci_mask(op, e, desc, val, &bvals[i], matches,
1308                                                                         &grant, &deny, &aci_bv_children) != 0) {
1309                                                                 tgrant |= grant;
1310                                                                 tdeny |= deny;
1311                                                                 /* evaluation stops as soon as either a "deny" or a 
1312                                                                  * "grant" directive matches.
1313                                                                  */
1314                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1315                                                                         stop = 1;
1316                                                                 }
1317                                                         }
1318                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
1319                                                                 accessmask2str(tgrant,accessmaskbuf),
1320                                                                 accessmask2str(tdeny, accessmaskbuf1), 0);
1321                                                 }
1322                                                 break;
1323
1324                                         case LDAP_NO_SUCH_ATTRIBUTE:
1325                                                 /* just go on if the aci-Attribute is not present in
1326                                                  * the current entry 
1327                                                  */
1328                                                 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1329                                                 stop = 0;
1330                                                 break;
1331
1332                                         case LDAP_NO_SUCH_OBJECT:
1333                                                 /* We have reached the base object */
1334                                                 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1335                                                 stop = 1;
1336                                                 break;
1337
1338                                         default:
1339                                                 stop = 1;
1340                                                 break;
1341                                         }
1342                                         if (stop){
1343                                                 break;
1344                                         }
1345                                         dnParent(&old_parent_ndn, &parent_ndn);
1346                                 }
1347                         }
1348
1349
1350                         /* remove anything that the ACL clause does not allow */
1351                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1352                         tdeny &= ACL_PRIV_MASK;
1353
1354                         /* see if we have anything to contribute */
1355                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1356                                 continue;
1357                         }
1358
1359                         /* this could be improved by changing acl_mask so that it can deal with
1360                          * by clauses that return grant/deny pairs.  Right now, it does either
1361                          * additive or subtractive rights, but not both at the same time.  So,
1362                          * we need to combine the grant/deny pair into a single rights mask in
1363                          * a smart way:  if either grant or deny is "empty", then we use the
1364                          * opposite as is, otherwise we remove any denied rights from the grant
1365                          * rights mask and construct an additive mask.
1366                          */
1367                         if (ACL_IS_INVALID(tdeny)) {
1368                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1369
1370                         } else if (ACL_IS_INVALID(tgrant)) {
1371                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1372
1373                         } else {
1374                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1375                         }
1376
1377                 } else
1378 #endif
1379                 {
1380                         modmask = b->a_access_mask;
1381                 }
1382
1383                 Debug( LDAP_DEBUG_ACL,
1384                         "<= acl_mask: [%d] applying %s (%s)\n",
1385                         i, accessmask2str( modmask, accessmaskbuf ), 
1386                         b->a_type == ACL_CONTINUE
1387                                 ? "continue"
1388                                 : b->a_type == ACL_BREAK
1389                                         ? "break"
1390                                         : "stop" );
1391                 /* save old mask */
1392                 oldmask = *mask;
1393
1394                 if( ACL_IS_ADDITIVE(modmask) ) {
1395                         /* add privs */
1396                         ACL_PRIV_SET( *mask, modmask );
1397
1398                         /* cleanup */
1399                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1400
1401                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1402                         /* substract privs */
1403                         ACL_PRIV_CLR( *mask, modmask );
1404
1405                         /* cleanup */
1406                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1407
1408                 } else {
1409                         /* assign privs */
1410                         *mask = modmask;
1411                 }
1412
1413                 Debug( LDAP_DEBUG_ACL,
1414                         "<= acl_mask: [%d] mask: %s\n",
1415                         i, accessmask2str(*mask, accessmaskbuf), 0 );
1416
1417                 if( b->a_type == ACL_CONTINUE ) {
1418                         continue;
1419
1420                 } else if ( b->a_type == ACL_BREAK ) {
1421                         return ACL_BREAK;
1422
1423                 } else {
1424                         return ACL_STOP;
1425                 }
1426         }
1427
1428         /* implicit "by * none" clause */
1429         ACL_INIT(*mask);
1430
1431         Debug( LDAP_DEBUG_ACL,
1432                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1433                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1434         return ACL_STOP;
1435 }
1436
1437 /*
1438  * acl_check_modlist - check access control on the given entry to see if
1439  * it allows the given modifications by the user associated with op.
1440  * returns      1       if mods allowed ok
1441  *              0       mods not allowed
1442  */
1443
1444 int
1445 acl_check_modlist(
1446         Operation       *op,
1447         Entry   *e,
1448         Modifications   *mlist
1449 )
1450 {
1451         struct berval *bv;
1452         AccessControlState state = ACL_STATE_INIT;
1453         Backend *be;
1454         int be_null = 0;
1455         int ret = 1; /* default is access allowed */
1456
1457         be = op->o_bd;
1458         if ( be == NULL ) {
1459                 be = &backends[0];
1460                 be_null = 1;
1461                 op->o_bd = be;
1462         }
1463         assert( be != NULL );
1464
1465         /* short circuit root database access */
1466         if ( be_isroot( op ) ) {
1467                 Debug( LDAP_DEBUG_ACL,
1468                         "<= acl_access_allowed: granted to database root\n",
1469                     0, 0, 0 );
1470                 goto done;
1471         }
1472
1473         /* use backend default access if no backend acls */
1474         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1475                 Debug( LDAP_DEBUG_ACL,
1476                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1477                         access2str( ACL_WRITE ),
1478                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1479                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1480                 goto done;
1481         }
1482
1483         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1484                 /*
1485                  * no-user-modification operational attributes are ignored
1486                  * by ACL_WRITE checking as any found here are not provided
1487                  * by the user
1488                  */
1489                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1490                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1491                                 " modify access granted\n",
1492                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1493                         continue;
1494                 }
1495
1496                 switch ( mlist->sml_op ) {
1497                 case LDAP_MOD_REPLACE:
1498                         /*
1499                          * We must check both permission to delete the whole
1500                          * attribute and permission to add the specific attributes.
1501                          * This prevents abuse from selfwriters.
1502                          */
1503                         if ( ! access_allowed( op, e,
1504                                 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1505                         {
1506                                 ret = 0;
1507                                 goto done;
1508                         }
1509
1510                         if ( mlist->sml_values == NULL ) break;
1511
1512                         /* fall thru to check value to add */
1513
1514                 case LDAP_MOD_ADD:
1515                         assert( mlist->sml_values != NULL );
1516
1517                         for ( bv = mlist->sml_nvalues
1518                                         ? mlist->sml_nvalues : mlist->sml_values;
1519                                 bv->bv_val != NULL; bv++ )
1520                         {
1521                                 if ( ! access_allowed( op, e,
1522                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1523                                 {
1524                                         ret = 0;
1525                                         goto done;
1526                                 }
1527                         }
1528                         break;
1529
1530                 case LDAP_MOD_DELETE:
1531                         if ( mlist->sml_values == NULL ) {
1532                                 if ( ! access_allowed( op, e,
1533                                         mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1534                                 {
1535                                         ret = 0;
1536                                         goto done;
1537                                 }
1538                                 break;
1539                         }
1540                         for ( bv = mlist->sml_nvalues
1541                                         ? mlist->sml_nvalues : mlist->sml_values;
1542                                 bv->bv_val != NULL; bv++ )
1543                         {
1544                                 if ( ! access_allowed( op, e,
1545                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1546                                 {
1547                                         ret = 0;
1548                                         goto done;
1549                                 }
1550                         }
1551                         break;
1552
1553                 case SLAP_MOD_SOFTADD:
1554                         /* allow adding attribute via modrdn thru */
1555                         break;
1556
1557                 default:
1558                         assert( 0 );
1559                         /* not reached */
1560                         ret = 0;
1561                         break;
1562                 }
1563         }
1564
1565 done:
1566         if (be_null) op->o_bd = NULL;
1567         return( ret );
1568 }
1569
1570 static int
1571 aci_get_part(
1572         struct berval *list,
1573         int ix,
1574         char sep,
1575         struct berval *bv )
1576 {
1577         int len;
1578         char *p;
1579
1580         if (bv) {
1581                 BER_BVZERO( bv );
1582         }
1583         len = list->bv_len;
1584         p = list->bv_val;
1585         while (len >= 0 && --ix >= 0) {
1586                 while (--len >= 0 && *p++ != sep) ;
1587         }
1588         while (len >= 0 && *p == ' ') {
1589                 len--;
1590                 p++;
1591         }
1592         if (len < 0)
1593                 return(-1);
1594
1595         if (!bv)
1596                 return(0);
1597
1598         bv->bv_val = p;
1599         while (--len >= 0 && *p != sep) {
1600                 bv->bv_len++;
1601                 p++;
1602         }
1603         while (bv->bv_len > 0 && *--p == ' ')
1604                 bv->bv_len--;
1605         return(bv->bv_len);
1606 }
1607
1608 BerVarray
1609 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1610 {
1611         AciSetCookie *cp = (AciSetCookie *)cookie;
1612         BerVarray bvals = NULL;
1613         struct berval ndn;
1614
1615         /* this routine needs to return the bervals instead of
1616          * plain strings, since syntax is not known.  It should
1617          * also return the syntax or some "comparison cookie".
1618          */
1619
1620         if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1621                 const char *text;
1622                 AttributeDescription *desc = NULL;
1623                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1624                         backend_attribute(cp->op,
1625                                 cp->e, &ndn, desc, &bvals, ACL_NONE);
1626                 }
1627                 slap_sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1628         }
1629         return(bvals);
1630 }
1631
1632 static int
1633 aci_match_set (
1634         struct berval *subj,
1635         Operation *op,
1636         Entry *e,
1637         int setref
1638 )
1639 {
1640         struct berval   set = BER_BVNULL;
1641         int             rc = 0;
1642         AciSetCookie    cookie;
1643
1644         if (setref == 0) {
1645                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1646         } else {
1647                 struct berval           subjdn, ndn = BER_BVNULL;
1648                 struct berval           setat;
1649                 BerVarray               bvals;
1650                 const char              *text;
1651                 AttributeDescription    *desc = NULL;
1652
1653                 /* format of string is "entry/setAttrName" */
1654                 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
1655                         return(0);
1656                 }
1657
1658                 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
1659                         setat = aci_bv_set_attr;
1660                 }
1661
1662                 /*
1663                  * NOTE: dnNormalize honors the ber_len field
1664                  * as the length of the dn to be normalized
1665                  */
1666                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
1667                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
1668                         {
1669                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
1670                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
1671                                         int     i;
1672
1673                                         set = bvals[0];
1674                                         BER_BVZERO( &bvals[0] );
1675                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
1676                                                 /* count */ ;
1677                                         bvals[0].bv_val = bvals[i-1].bv_val;
1678                                         BER_BVZERO( &bvals[i-1] );
1679                                 }
1680                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
1681                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1682                         }
1683                 }
1684         }
1685
1686         if ( !BER_BVISNULL( &set ) ) {
1687                 cookie.op = op;
1688                 cookie.e = e;
1689                 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
1690                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
1691                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
1692         }
1693
1694         return(rc);
1695 }
1696
1697 #ifdef SLAPD_ACI_ENABLED
1698 static int
1699 aci_list_map_rights(
1700         struct berval *list )
1701 {
1702         struct berval bv;
1703         slap_access_t mask;
1704         int i;
1705
1706         ACL_INIT(mask);
1707         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1708                 if (bv.bv_len <= 0)
1709                         continue;
1710                 switch (*bv.bv_val) {
1711                 case 'c':
1712                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1713                         break;
1714                 case 's':
1715                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1716                          * the right 's' to mean "set", but in the examples states
1717                          * that the right 's' means "search".  The latter definition
1718                          * is used here.
1719                          */
1720                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1721                         break;
1722                 case 'r':
1723                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1724                         break;
1725                 case 'w':
1726                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1727                         break;
1728                 case 'x':
1729                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1730                          * define any equivalent to the AUTH right, so I've just used
1731                          * 'x' for now.
1732                          */
1733                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1734                         break;
1735                 default:
1736                         break;
1737                 }
1738
1739         }
1740         return(mask);
1741 }
1742
1743 static int
1744 aci_list_has_attr(
1745         struct berval *list,
1746         const struct berval *attr,
1747         struct berval *val )
1748 {
1749         struct berval bv, left, right;
1750         int i;
1751
1752         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1753                 if (aci_get_part(&bv, 0, '=', &left) < 0
1754                         || aci_get_part(&bv, 1, '=', &right) < 0)
1755                 {
1756                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1757                                 return(1);
1758                 } else if (val == NULL) {
1759                         if (ber_bvstrcasecmp(attr, &left) == 0)
1760                                 return(1);
1761                 } else {
1762                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1763                                 /* this is experimental code that implements a
1764                                  * simple (prefix) match of the attribute value.
1765                                  * the ACI draft does not provide for aci's that
1766                                  * apply to specific values, but it would be
1767                                  * nice to have.  If the <attr> part of an aci's
1768                                  * rights list is of the form <attr>=<value>,
1769                                  * that means the aci applies only to attrs with
1770                                  * the given value.  Furthermore, if the attr is
1771                                  * of the form <attr>=<value>*, then <value> is
1772                                  * treated as a prefix, and the aci applies to 
1773                                  * any value with that prefix.
1774                                  *
1775                                  * Ideally, this would allow r.e. matches.
1776                                  */
1777                                 if (aci_get_part(&right, 0, '*', &left) < 0
1778                                         || right.bv_len <= left.bv_len)
1779                                 {
1780                                         if (ber_bvstrcasecmp(val, &right) == 0)
1781                                                 return(1);
1782                                 } else if (val->bv_len >= left.bv_len) {
1783                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1784                                                 return(1);
1785                                 }
1786                         }
1787                 }
1788         }
1789         return(0);
1790 }
1791
1792 static slap_access_t
1793 aci_list_get_attr_rights(
1794         struct berval *list,
1795         const struct berval *attr,
1796         struct berval *val )
1797 {
1798     struct berval bv;
1799     slap_access_t mask;
1800     int i;
1801
1802         /* loop through each rights/attr pair, skip first part (action) */
1803         ACL_INIT(mask);
1804         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1805                 if (aci_list_has_attr(&bv, attr, val) == 0)
1806                         continue;
1807                 if (aci_get_part(list, i, ';', &bv) < 0)
1808                         continue;
1809                 mask |= aci_list_map_rights(&bv);
1810         }
1811         return(mask);
1812 }
1813
1814 static int
1815 aci_list_get_rights(
1816         struct berval *list,
1817         const struct berval *attr,
1818         struct berval *val,
1819         slap_access_t *grant,
1820         slap_access_t *deny )
1821 {
1822     struct berval perm, actn;
1823     slap_access_t *mask;
1824     int i, found;
1825
1826         if (attr == NULL || attr->bv_len == 0 
1827                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1828                 attr = &aci_bv_br_entry;
1829         }
1830
1831         found = 0;
1832         ACL_INIT(*grant);
1833         ACL_INIT(*deny);
1834         /* loop through each permissions clause */
1835         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1836                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1837                         continue;
1838                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1839                         mask = grant;
1840                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1841                         mask = deny;
1842                 } else {
1843                         continue;
1844                 }
1845
1846                 found = 1;
1847                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1848                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1849         }
1850         return(found);
1851 }
1852
1853 static int
1854 aci_group_member (
1855         struct berval *subj,
1856         struct berval *defgrpoc,
1857         struct berval *defgrpat,
1858         Operation               *op,
1859         Entry           *e,
1860         regmatch_t      *matches
1861 )
1862 {
1863         struct berval subjdn;
1864         struct berval grpoc;
1865         struct berval grpat;
1866         ObjectClass *grp_oc = NULL;
1867         AttributeDescription *grp_ad = NULL;
1868         const char *text;
1869         int rc;
1870
1871         /* format of string is "group/objectClassValue/groupAttrName" */
1872         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1873                 return(0);
1874         }
1875
1876         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1877                 grpoc = *defgrpoc;
1878         }
1879
1880         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1881                 grpat = *defgrpat;
1882         }
1883
1884         rc = slap_bv2ad( &grpat, &grp_ad, &text );
1885         if( rc != LDAP_SUCCESS ) {
1886                 rc = 0;
1887                 goto done;
1888         }
1889         rc = 0;
1890
1891         grp_oc = oc_bvfind( &grpoc );
1892
1893         if (grp_oc != NULL && grp_ad != NULL ) {
1894                 char buf[ACL_BUF_SIZE];
1895                 struct berval bv, ndn;
1896                 bv.bv_len = sizeof( buf ) - 1;
1897                 bv.bv_val = (char *)&buf;
1898                 if ( string_expand(&bv, &subjdn,
1899                                 e->e_ndn, matches) )
1900                 {
1901                         rc = LDAP_OTHER;
1902                         goto done;
1903                 }
1904                 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1905                         rc = (backend_group(op, e, &ndn, &op->o_ndn,
1906                                 grp_oc, grp_ad) == 0);
1907                         free( ndn.bv_val );
1908                 }
1909         }
1910
1911 done:
1912         return(rc);
1913 }
1914
1915 static int
1916 aci_mask(
1917     Operation           *op,
1918     Entry                       *e,
1919         AttributeDescription *desc,
1920     struct berval       *val,
1921     struct berval       *aci,
1922         regmatch_t              *matches,
1923         slap_access_t   *grant,
1924         slap_access_t   *deny,
1925         struct berval   *scope
1926 )
1927 {
1928     struct berval bv, perms, sdn;
1929         int rc;
1930                 
1931
1932         assert( desc->ad_cname.bv_val != NULL );
1933
1934         /* parse an aci of the form:
1935                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1936
1937            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1938            a full description of the format for this attribute.
1939            Differences: "this" in the draft is "self" here, and
1940            "self" and "public" is in the position of dnType.
1941
1942            For now, this routine only supports scope=entry.
1943          */
1944         /* check that the aci has all 5 components */
1945         if (aci_get_part(aci, 4, '#', NULL) < 0)
1946                 return(0);
1947
1948         /* check that the aci family is supported */
1949         if (aci_get_part(aci, 0, '#', &bv) < 0)
1950                 return(0);
1951
1952         /* check that the scope matches */
1953         if (aci_get_part(aci, 1, '#', &bv) < 0
1954                 || ber_bvstrcasecmp( scope, &bv ) != 0)
1955         {
1956                 return(0);
1957         }
1958
1959         /* get the list of permissions clauses, bail if empty */
1960         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1961                 return(0);
1962
1963         /* check if any permissions allow desired access */
1964         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1965                 return(0);
1966
1967         /* see if we have a DN match */
1968         if (aci_get_part(aci, 3, '#', &bv) < 0)
1969                 return(0);
1970
1971         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1972                 return(0);
1973
1974         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1975                 struct berval ndn;
1976                 rc = 0;
1977                 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1978                         if (dn_match( &op->o_ndn, &ndn))
1979                                 rc = 1;
1980                         free(ndn.bv_val);
1981                 }
1982                 return (rc);
1983
1984         } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
1985                 return(1);
1986
1987         } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1988                 if (dn_match(&op->o_ndn, &e->e_nname))
1989                         return(1);
1990
1991         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1992                 Attribute *at;
1993                 AttributeDescription *ad = NULL;
1994                 const char *text;
1995
1996                 rc = slap_bv2ad( &sdn, &ad, &text );
1997
1998                 if( rc != LDAP_SUCCESS ) {
1999                         return 0;
2000                 }
2001
2002                 rc = 0;
2003
2004                 bv = op->o_ndn;
2005
2006                 for(at = attrs_find( e->e_attrs, ad );
2007                         at != NULL;
2008                         at = attrs_find( at->a_next, ad ) )
2009                 {
2010                         if (value_find_ex( ad,
2011                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2012                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2013                                 at->a_nvals,
2014                                 &bv, op->o_tmpmemctx) == 0 )
2015                         {
2016                                 rc = 1;
2017                                 break;
2018                         }
2019                 }
2020
2021                 return rc;
2022
2023
2024         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2025                 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2026                         return(1);
2027
2028         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2029                 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2030                         return(1);
2031
2032         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2033                 if (aci_match_set(&sdn, op, e, 0))
2034                         return(1);
2035
2036         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2037                 if (aci_match_set(&sdn, op, e, 1))
2038                         return(1);
2039
2040         }
2041
2042         return(0);
2043 }
2044
2045 #endif  /* SLAPD_ACI_ENABLED */
2046
2047 static int
2048 string_expand(
2049         struct berval   *bv,
2050         struct berval   *pat,
2051         char            *match,
2052         regmatch_t      *matches)
2053 {
2054         ber_len_t       size;
2055         char   *sp;
2056         char   *dp;
2057         int     flag;
2058
2059         size = 0;
2060         bv->bv_val[0] = '\0';
2061         bv->bv_len--; /* leave space for lone $ */
2062
2063         flag = 0;
2064         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2065                 sp < pat->bv_val + pat->bv_len ; sp++ )
2066         {
2067                 /* did we previously see a $ */
2068                 if ( flag ) {
2069                         if ( flag == 1 && *sp == '$' ) {
2070                                 *dp++ = '$';
2071                                 size++;
2072                                 flag = 0;
2073
2074                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2075                                 flag = 2;
2076
2077                         } else if ( *sp >= '0' && *sp <= '9' ) {
2078                                 int     n;
2079                                 int     i;
2080                                 int     l;
2081
2082                                 n = *sp - '0';
2083
2084                                 if ( flag == 2 ) {
2085                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2086                                                 if ( *sp >= '0' && *sp <= '9' ) {
2087                                                         n = 10*n + ( *sp - '0' );
2088                                                 }
2089                                         }
2090
2091                                         if ( *sp != /*'{'*/ '}' ) {
2092                                                 /* FIXME: error */
2093                                                 return 1;
2094                                         }
2095                                 }
2096
2097                                 if ( n >= MAXREMATCHES ) {
2098                                         /* FIXME: error */
2099                                         return 1;
2100                                 }
2101                                 
2102                                 *dp = '\0';
2103                                 i = matches[n].rm_so;
2104                                 l = matches[n].rm_eo; 
2105                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2106                                         *dp++ = match[i];
2107                                 }
2108                                 *dp = '\0';
2109
2110                                 flag = 0;
2111                         }
2112                 } else {
2113                         if (*sp == '$') {
2114                                 flag = 1;
2115                         } else {
2116                                 *dp++ = *sp;
2117                                 size++;
2118                         }
2119                 }
2120         }
2121
2122         if ( flag ) {
2123                 /* must have ended with a single $ */
2124                 *dp++ = '$';
2125                 size++;
2126         }
2127
2128         *dp = '\0';
2129         bv->bv_len = size;
2130
2131         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2132         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2133
2134         return 0;
2135 }
2136
2137 static int
2138 regex_matches(
2139         struct berval *pat,                     /* pattern to expand and match against */
2140         char *str,                              /* string to match against pattern */
2141         char *buf,                              /* buffer with $N expansion variables */
2142         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
2143 )
2144 {
2145         regex_t re;
2146         char newbuf[ACL_BUF_SIZE];
2147         struct berval bv;
2148         int     rc;
2149
2150         bv.bv_len = sizeof(newbuf) - 1;
2151         bv.bv_val = newbuf;
2152
2153         if(str == NULL) str = "";
2154
2155         string_expand(&bv, pat, buf, matches);
2156         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2157                 char error[ACL_BUF_SIZE];
2158                 regerror(rc, &re, error, sizeof(error));
2159
2160                 Debug( LDAP_DEBUG_TRACE,
2161                     "compile( \"%s\", \"%s\") failed %s\n",
2162                         pat->bv_val, str, error );
2163                 return( 0 );
2164         }
2165
2166         rc = regexec(&re, str, 0, NULL, 0);
2167         regfree( &re );
2168
2169         Debug( LDAP_DEBUG_TRACE,
2170             "=> regex_matches: string:   %s\n", str, 0, 0 );
2171         Debug( LDAP_DEBUG_TRACE,
2172             "=> regex_matches: rc: %d %s\n",
2173                 rc, !rc ? "matches" : "no matches", 0 );
2174         return( !rc );
2175 }
2176