]> 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
1454         assert( op->o_bd != NULL );
1455
1456         /* short circuit root database access */
1457         if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
1458 #ifdef NEW_LOGGING
1459                 LDAP_LOG( ACL, DETAIL1, 
1460                            "acl_check_modlist: conn %lu  access granted to root user\n",
1461                            op->o_connid, 0, 0 );
1462 #else
1463                 Debug( LDAP_DEBUG_ACL,
1464                         "<= acl_access_allowed: granted to database root\n",
1465                     0, 0, 0 );
1466 #endif
1467                 return 1;
1468         }
1469
1470         /* use backend default access if no backend acls */
1471         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1472 #ifdef NEW_LOGGING
1473                 LDAP_LOG( ACL, DETAIL1, 
1474                         "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1475                         access2str( ACL_WRITE ),
1476                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", 
1477                         op->o_dn.bv_val  );
1478 #else
1479                 Debug( LDAP_DEBUG_ACL,
1480                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1481                         access2str( ACL_WRITE ),
1482                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1483 #endif
1484                 return op->o_bd->be_dfltaccess >= ACL_WRITE;
1485
1486 #ifdef notdef
1487         /* op->o_bd is always non-NULL */
1488         /* use global default access if no global acls */
1489         } else if ( op->o_bd == NULL && global_acl == NULL ) {
1490 #ifdef NEW_LOGGING
1491                 LDAP_LOG( ACL, DETAIL1, 
1492                         "acl_check_modlist: global default %s access %s to \"%s\"\n",
1493                    access2str( ACL_WRITE ),
1494                    global_default_access >= ACL_WRITE ? "granted" : "denied", 
1495                    op->o_dn  );
1496 #else
1497                 Debug( LDAP_DEBUG_ACL,
1498                         "=> access_allowed: global default %s access %s to \"%s\"\n",
1499                         access2str( ACL_WRITE ),
1500                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1501 #endif
1502                 return global_default_access >= ACL_WRITE;
1503 #endif
1504         }
1505
1506         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1507                 /*
1508                  * no-user-modification operational attributes are ignored
1509                  * by ACL_WRITE checking as any found here are not provided
1510                  * by the user
1511                  */
1512                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1513 #ifdef NEW_LOGGING
1514                         LDAP_LOG( ACL, DETAIL1, 
1515                                    "acl_check_modlist: conn %lu  no-user-mod %s: modify access granted\n",
1516                                    op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1517 #else
1518                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1519                                 " modify access granted\n",
1520                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1521 #endif
1522                         continue;
1523                 }
1524
1525                 switch ( mlist->sml_op ) {
1526                 case LDAP_MOD_REPLACE:
1527                         /*
1528                          * We must check both permission to delete the whole
1529                          * attribute and permission to add the specific attributes.
1530                          * This prevents abuse from selfwriters.
1531                          */
1532                         if ( ! access_allowed( op, e,
1533                                 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1534                         {
1535                                 return( 0 );
1536                         }
1537
1538                         if ( mlist->sml_bvalues == NULL ) break;
1539
1540                         /* fall thru to check value to add */
1541
1542                 case LDAP_MOD_ADD:
1543                         assert( mlist->sml_bvalues != NULL );
1544
1545                         for ( bv = mlist->sml_nvalues
1546                                         ? mlist->sml_nvalues : mlist->sml_values;
1547                                 bv->bv_val != NULL; bv++ )
1548                         {
1549                                 if ( ! access_allowed( op, e,
1550                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1551                                 {
1552                                         return( 0 );
1553                                 }
1554                         }
1555                         break;
1556
1557                 case LDAP_MOD_DELETE:
1558                         if ( mlist->sml_bvalues == NULL ) {
1559                                 if ( ! access_allowed( op, e,
1560                                         mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1561                                 {
1562                                         return( 0 );
1563                                 }
1564                                 break;
1565                         }
1566                         for ( bv = mlist->sml_nvalues
1567                                         ? mlist->sml_nvalues : mlist->sml_values;
1568                                 bv->bv_val != NULL; bv++ )
1569                         {
1570                                 if ( ! access_allowed( op, e,
1571                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1572                                 {
1573                                         return( 0 );
1574                                 }
1575                         }
1576                         break;
1577
1578                 case SLAP_MOD_SOFTADD:
1579                         /* allow adding attribute via modrdn thru */
1580                         break;
1581
1582                 default:
1583                         assert( 0 );
1584                         return( 0 );
1585                 }
1586         }
1587
1588         return( 1 );
1589 }
1590
1591 static int
1592 aci_get_part(
1593         struct berval *list,
1594         int ix,
1595         char sep,
1596         struct berval *bv )
1597 {
1598         int len;
1599         char *p;
1600
1601         if (bv) {
1602                 bv->bv_len = 0;
1603                 bv->bv_val = NULL;
1604         }
1605         len = list->bv_len;
1606         p = list->bv_val;
1607         while (len >= 0 && --ix >= 0) {
1608                 while (--len >= 0 && *p++ != sep) ;
1609         }
1610         while (len >= 0 && *p == ' ') {
1611                 len--;
1612                 p++;
1613         }
1614         if (len < 0)
1615                 return(-1);
1616
1617         if (!bv)
1618                 return(0);
1619
1620         bv->bv_val = p;
1621         while (--len >= 0 && *p != sep) {
1622                 bv->bv_len++;
1623                 p++;
1624         }
1625         while (bv->bv_len > 0 && *--p == ' ')
1626                 bv->bv_len--;
1627         return(bv->bv_len);
1628 }
1629
1630 BerVarray
1631 aci_set_gather (SetCookie *cookie, struct berval *name, struct berval *attr)
1632 {
1633         AciSetCookie *cp = (AciSetCookie *)cookie;
1634         BerVarray bvals = NULL;
1635         struct berval ndn;
1636
1637         /* this routine needs to return the bervals instead of
1638          * plain strings, since syntax is not known.  It should
1639          * also return the syntax or some "comparison cookie".
1640          */
1641
1642         if (dnNormalize(0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx) == LDAP_SUCCESS) {
1643                 const char *text;
1644                 AttributeDescription *desc = NULL;
1645                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1646                         backend_attribute(cp->op,
1647                                 cp->e, &ndn, desc, &bvals);
1648                 }
1649                 sl_free(ndn.bv_val, cp->op->o_tmpmemctx);
1650         }
1651         return(bvals);
1652 }
1653
1654 static int
1655 aci_match_set (
1656         struct berval *subj,
1657         Operation *op,
1658         Entry *e,
1659         int setref
1660 )
1661 {
1662         struct berval set = { 0, NULL };
1663         int rc = 0;
1664         AciSetCookie cookie;
1665
1666         if (setref == 0) {
1667                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
1668         } else {
1669                 struct berval subjdn, ndn = { 0, NULL };
1670                 struct berval setat;
1671                 BerVarray bvals;
1672                 const char *text;
1673                 AttributeDescription *desc = NULL;
1674
1675                 /* format of string is "entry/setAttrName" */
1676                 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1677                         return(0);
1678                 }
1679
1680                 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1681                         setat.bv_val = SLAPD_ACI_SET_ATTR;
1682                         setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1683                 }
1684
1685                 if ( setat.bv_val != NULL ) {
1686                         /*
1687                          * NOTE: dnNormalize honors the ber_len field
1688                          * as the length of the dn to be normalized
1689                          */
1690                         if ( dnNormalize(0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS
1691                                 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1692                         {
1693                                 backend_attribute(op, e,
1694                                         &ndn, desc, &bvals);
1695                                 if ( bvals != NULL ) {
1696                                         if ( bvals[0].bv_val != NULL ) {
1697                                                 int i;
1698                                                 set = bvals[0];
1699                                                 bvals[0].bv_val = NULL;
1700                                                 for (i=1;bvals[i].bv_val;i++);
1701                                                 bvals[0].bv_val = bvals[i-1].bv_val;
1702                                                 bvals[i-1].bv_val = NULL;
1703                                         }
1704                                         ber_bvarray_free_x(bvals, op->o_tmpmemctx);
1705                                 }
1706                         }
1707                         if (ndn.bv_val)
1708                                 free(ndn.bv_val);
1709                 }
1710         }
1711
1712         if (set.bv_val != NULL) {
1713                 cookie.op = op;
1714                 cookie.e = e;
1715                 rc = (slap_set_filter(aci_set_gather, (SetCookie *)&cookie, &set,
1716                         &op->o_ndn, &e->e_nname, NULL) > 0);
1717                 sl_free(set.bv_val, op->o_tmpmemctx);
1718         }
1719         return(rc);
1720 }
1721
1722 #ifdef SLAPD_ACI_ENABLED
1723 static int
1724 aci_list_map_rights(
1725         struct berval *list )
1726 {
1727         struct berval bv;
1728         slap_access_t mask;
1729         int i;
1730
1731         ACL_INIT(mask);
1732         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1733                 if (bv.bv_len <= 0)
1734                         continue;
1735                 switch (*bv.bv_val) {
1736                 case 'c':
1737                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1738                         break;
1739                 case 's':
1740                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1741                          * the right 's' to mean "set", but in the examples states
1742                          * that the right 's' means "search".  The latter definition
1743                          * is used here.
1744                          */
1745                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1746                         break;
1747                 case 'r':
1748                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1749                         break;
1750                 case 'w':
1751                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1752                         break;
1753                 case 'x':
1754                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1755                          * define any equivalent to the AUTH right, so I've just used
1756                          * 'x' for now.
1757                          */
1758                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1759                         break;
1760                 default:
1761                         break;
1762                 }
1763
1764         }
1765         return(mask);
1766 }
1767
1768 static int
1769 aci_list_has_attr(
1770         struct berval *list,
1771         const struct berval *attr,
1772         struct berval *val )
1773 {
1774         struct berval bv, left, right;
1775         int i;
1776
1777         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1778                 if (aci_get_part(&bv, 0, '=', &left) < 0
1779                         || aci_get_part(&bv, 1, '=', &right) < 0)
1780                 {
1781                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1782                                 return(1);
1783                 } else if (val == NULL) {
1784                         if (ber_bvstrcasecmp(attr, &left) == 0)
1785                                 return(1);
1786                 } else {
1787                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1788                                 /* this is experimental code that implements a
1789                                  * simple (prefix) match of the attribute value.
1790                                  * the ACI draft does not provide for aci's that
1791                                  * apply to specific values, but it would be
1792                                  * nice to have.  If the <attr> part of an aci's
1793                                  * rights list is of the form <attr>=<value>,
1794                                  * that means the aci applies only to attrs with
1795                                  * the given value.  Furthermore, if the attr is
1796                                  * of the form <attr>=<value>*, then <value> is
1797                                  * treated as a prefix, and the aci applies to 
1798                                  * any value with that prefix.
1799                                  *
1800                                  * Ideally, this would allow r.e. matches.
1801                                  */
1802                                 if (aci_get_part(&right, 0, '*', &left) < 0
1803                                         || right.bv_len <= left.bv_len)
1804                                 {
1805                                         if (ber_bvstrcasecmp(val, &right) == 0)
1806                                                 return(1);
1807                                 } else if (val->bv_len >= left.bv_len) {
1808                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1809                                                 return(1);
1810                                 }
1811                         }
1812                 }
1813         }
1814         return(0);
1815 }
1816
1817 static slap_access_t
1818 aci_list_get_attr_rights(
1819         struct berval *list,
1820         const struct berval *attr,
1821         struct berval *val )
1822 {
1823     struct berval bv;
1824     slap_access_t mask;
1825     int i;
1826
1827         /* loop through each rights/attr pair, skip first part (action) */
1828         ACL_INIT(mask);
1829         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1830                 if (aci_list_has_attr(&bv, attr, val) == 0)
1831                         continue;
1832                 if (aci_get_part(list, i, ';', &bv) < 0)
1833                         continue;
1834                 mask |= aci_list_map_rights(&bv);
1835         }
1836         return(mask);
1837 }
1838
1839 static int
1840 aci_list_get_rights(
1841         struct berval *list,
1842         const struct berval *attr,
1843         struct berval *val,
1844         slap_access_t *grant,
1845         slap_access_t *deny )
1846 {
1847     struct berval perm, actn;
1848     slap_access_t *mask;
1849     int i, found;
1850
1851         if (attr == NULL || attr->bv_len == 0 
1852                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1853                 attr = &aci_bv_br_entry;
1854         }
1855
1856         found = 0;
1857         ACL_INIT(*grant);
1858         ACL_INIT(*deny);
1859         /* loop through each permissions clause */
1860         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1861                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1862                         continue;
1863                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1864                         mask = grant;
1865                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1866                         mask = deny;
1867                 } else {
1868                         continue;
1869                 }
1870
1871                 found = 1;
1872                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1873                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1874         }
1875         return(found);
1876 }
1877
1878 static int
1879 aci_group_member (
1880         struct berval *subj,
1881         struct berval *defgrpoc,
1882         struct berval *defgrpat,
1883         Operation               *op,
1884         Entry           *e,
1885         regmatch_t      *matches
1886 )
1887 {
1888         struct berval subjdn;
1889         struct berval grpoc;
1890         struct berval grpat;
1891         ObjectClass *grp_oc = NULL;
1892         AttributeDescription *grp_ad = NULL;
1893         const char *text;
1894         int rc;
1895
1896         /* format of string is "group/objectClassValue/groupAttrName" */
1897         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1898                 return(0);
1899         }
1900
1901         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1902                 grpoc = *defgrpoc;
1903         }
1904
1905         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1906                 grpat = *defgrpat;
1907         }
1908
1909         rc = slap_bv2ad( &grpat, &grp_ad, &text );
1910         if( rc != LDAP_SUCCESS ) {
1911                 rc = 0;
1912                 goto done;
1913         }
1914         rc = 0;
1915
1916         grp_oc = oc_bvfind( &grpoc );
1917
1918         if (grp_oc != NULL && grp_ad != NULL ) {
1919                 char buf[ACL_BUF_SIZE];
1920                 struct berval bv, ndn;
1921                 bv.bv_len = sizeof( buf ) - 1;
1922                 bv.bv_val = (char *)&buf;
1923                 string_expand(&bv, &subjdn, e->e_ndn, matches);
1924                 if ( dnNormalize(0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1925                         rc = (backend_group(op, e, &ndn, &op->o_ndn,
1926                                 grp_oc, grp_ad) == 0);
1927                         free( ndn.bv_val );
1928                 }
1929         }
1930
1931 done:
1932         return(rc);
1933 }
1934
1935 static int
1936 aci_mask(
1937     Operation           *op,
1938     Entry                       *e,
1939         AttributeDescription *desc,
1940     struct berval       *val,
1941     struct berval       *aci,
1942         regmatch_t              *matches,
1943         slap_access_t   *grant,
1944         slap_access_t   *deny,
1945         struct berval   *scope
1946 )
1947 {
1948     struct berval bv, perms, sdn;
1949         int rc;
1950                 
1951
1952         assert( desc->ad_cname.bv_val != NULL );
1953
1954         /* parse an aci of the form:
1955                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1956
1957            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1958            a full description of the format for this attribute.
1959            Differences: "this" in the draft is "self" here, and
1960            "self" and "public" is in the position of dnType.
1961
1962            For now, this routine only supports scope=entry.
1963          */
1964         /* check that the aci has all 5 components */
1965         if (aci_get_part(aci, 4, '#', NULL) < 0)
1966                 return(0);
1967
1968         /* check that the aci family is supported */
1969         if (aci_get_part(aci, 0, '#', &bv) < 0)
1970                 return(0);
1971
1972         /* check that the scope matches */
1973         if (aci_get_part(aci, 1, '#', &bv) < 0
1974                 || ber_bvstrcasecmp( scope, &bv ) != 0)
1975         {
1976                 return(0);
1977         }
1978
1979         /* get the list of permissions clauses, bail if empty */
1980         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1981                 return(0);
1982
1983         /* check if any permissions allow desired access */
1984         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1985                 return(0);
1986
1987         /* see if we have a DN match */
1988         if (aci_get_part(aci, 3, '#', &bv) < 0)
1989                 return(0);
1990
1991         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1992                 return(0);
1993
1994         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1995                 struct berval ndn;
1996                 rc = 0;
1997                 if ( dnNormalize(0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx) == LDAP_SUCCESS ) {
1998                         if (dn_match( &op->o_ndn, &ndn))
1999                                 rc = 1;
2000                         free(ndn.bv_val);
2001                 }
2002                 return (rc);
2003
2004         } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2005                 return(1);
2006
2007         } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2008                 if (dn_match(&op->o_ndn, &e->e_nname))
2009                         return(1);
2010
2011         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2012                 Attribute *at;
2013                 AttributeDescription *ad = NULL;
2014                 const char *text;
2015
2016                 rc = slap_bv2ad( &sdn, &ad, &text );
2017
2018                 if( rc != LDAP_SUCCESS ) {
2019                         return 0;
2020                 }
2021
2022                 rc = 0;
2023
2024                 bv = op->o_ndn;
2025
2026                 for(at = attrs_find( e->e_attrs, ad );
2027                         at != NULL;
2028                         at = attrs_find( at->a_next, ad ) )
2029                 {
2030                         if (value_find_ex( ad,
2031                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2032                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2033                                 at->a_nvals,
2034                                 &bv, op->o_tmpmemctx) == 0 )
2035                         {
2036                                 rc = 1;
2037                                 break;
2038                         }
2039                 }
2040
2041                 return rc;
2042
2043
2044         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2045                 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
2046                         return(1);
2047
2048         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2049                 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
2050                         return(1);
2051
2052         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2053                 if (aci_match_set(&sdn, op, e, 0))
2054                         return(1);
2055
2056         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2057                 if (aci_match_set(&sdn, op, e, 1))
2058                         return(1);
2059
2060         }
2061
2062         return(0);
2063 }
2064
2065 #endif  /* SLAPD_ACI_ENABLED */
2066
2067 static void
2068 string_expand(
2069         struct berval *bv,
2070         struct berval *pat,
2071         char *match,
2072         regmatch_t *matches)
2073 {
2074         ber_len_t       size;
2075         char   *sp;
2076         char   *dp;
2077         int     flag;
2078
2079         size = 0;
2080         bv->bv_val[0] = '\0';
2081         bv->bv_len--; /* leave space for lone $ */
2082
2083         flag = 0;
2084         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2085                 sp < pat->bv_val + pat->bv_len ; sp++ )
2086         {
2087                 /* did we previously see a $ */
2088                 if ( flag ) {
2089                         if ( flag == 1 && *sp == '$' ) {
2090                                 *dp++ = '$';
2091                                 size++;
2092                                 flag = 0;
2093
2094                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2095                                 flag = 2;
2096
2097                         } else if ( *sp >= '0' && *sp <= '9' ) {
2098                                 int     n;
2099                                 int     i;
2100                                 int     l;
2101
2102                                 n = *sp - '0';
2103
2104                                 if ( flag == 2 ) {
2105                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2106                                                 if ( *sp >= '0' && *sp <= '9' ) {
2107                                                         n = 10*n + ( *sp - '0' );
2108                                                 }
2109                                         }
2110
2111                                         if ( *sp != /*'{'*/ '}' ) {
2112                                                 /* error */
2113                                         }
2114                                 }
2115
2116                                 if ( n >= MAXREMATCHES ) {
2117                                 
2118                                 }
2119                                 
2120                                 *dp = '\0';
2121                                 i = matches[n].rm_so;
2122                                 l = matches[n].rm_eo; 
2123                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2124                                         *dp++ = match[i];
2125                                 }
2126                                 *dp = '\0';
2127
2128                                 flag = 0;
2129                         }
2130                 } else {
2131                         if (*sp == '$') {
2132                                 flag = 1;
2133                         } else {
2134                                 *dp++ = *sp;
2135                                 size++;
2136                         }
2137                 }
2138         }
2139
2140         if ( flag ) {
2141                 /* must have ended with a single $ */
2142                 *dp++ = '$';
2143                 size++;
2144         }
2145
2146         *dp = '\0';
2147         bv->bv_len = size;
2148
2149 #ifdef NEW_LOGGING
2150         LDAP_LOG( ACL, DETAIL1, 
2151            "string_expand:  pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2152         LDAP_LOG( ACL, DETAIL1, "string_expand:  expanded = %s\n", bv->bv_val, 0, 0 );
2153 #else
2154         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2155         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2156 #endif
2157 }
2158
2159 static int
2160 regex_matches(
2161         struct berval *pat,                     /* pattern to expand and match against */
2162         char *str,                              /* string to match against pattern */
2163         char *buf,                              /* buffer with $N expansion variables */
2164         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
2165 )
2166 {
2167         regex_t re;
2168         char newbuf[ACL_BUF_SIZE];
2169         struct berval bv;
2170         int     rc;
2171
2172         bv.bv_len = sizeof(newbuf) - 1;
2173         bv.bv_val = newbuf;
2174
2175         if(str == NULL) str = "";
2176
2177         string_expand(&bv, pat, buf, matches);
2178         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
2179                 char error[ACL_BUF_SIZE];
2180                 regerror(rc, &re, error, sizeof(error));
2181
2182 #ifdef NEW_LOGGING
2183                 LDAP_LOG( ACL, ERR, 
2184                            "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2185                            pat->bv_val, str, error  );
2186 #else
2187                 Debug( LDAP_DEBUG_TRACE,
2188                     "compile( \"%s\", \"%s\") failed %s\n",
2189                         pat->bv_val, str, error );
2190 #endif
2191                 return( 0 );
2192         }
2193
2194         rc = regexec(&re, str, 0, NULL, 0);
2195         regfree( &re );
2196
2197 #ifdef NEW_LOGGING
2198         LDAP_LOG( ACL, DETAIL2, "regex_matches: string:   %s\n", str, 0, 0 );
2199         LDAP_LOG( ACL, DETAIL2, "regex_matches: rc:     %d  %s\n",
2200                    rc, rc ? "matches" : "no matches", 0  );
2201 #else
2202         Debug( LDAP_DEBUG_TRACE,
2203             "=> regex_matches: string:   %s\n", str, 0, 0 );
2204         Debug( LDAP_DEBUG_TRACE,
2205             "=> regex_matches: rc: %d %s\n",
2206                 rc, !rc ? "matches" : "no matches", 0 );
2207 #endif
2208         return( !rc );
2209 }
2210