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