]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
honor disclose
[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-2005 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 #include "lutil.h"
39
40 #ifdef LDAP_SLAPI
41 #include "slapi/slapi.h"
42 #endif /* LDAPI_SLAPI */
43
44 #define ACL_BUF_SIZE    1024    /* use most appropriate size */
45
46 /*
47  * speed up compares
48  */
49 static struct berval 
50         aci_bv_entry            = BER_BVC("entry"),
51         aci_bv_children         = BER_BVC("children"),
52         aci_bv_onelevel         = BER_BVC("onelevel"),
53         aci_bv_subtree          = BER_BVC("subtree"),
54         aci_bv_br_entry         = BER_BVC("[entry]"),
55         aci_bv_br_all           = BER_BVC("[all]"),
56         aci_bv_access_id        = BER_BVC("access-id"),
57         aci_bv_anonymous        = BER_BVC("anonymous"),
58         aci_bv_public           = BER_BVC("public"),
59         aci_bv_users            = BER_BVC("users"),
60         aci_bv_self             = BER_BVC("self"),
61         aci_bv_dnattr           = BER_BVC("dnattr"),
62         aci_bv_group            = BER_BVC("group"),
63         aci_bv_role             = BER_BVC("role"),
64         aci_bv_set              = BER_BVC("set"),
65         aci_bv_set_ref          = BER_BVC("set-ref"),
66         aci_bv_grant            = BER_BVC("grant"),
67         aci_bv_deny             = BER_BVC("deny"),
68
69         aci_bv_ip_eq            = BER_BVC("IP="),
70 #ifdef LDAP_PF_LOCAL
71         aci_bv_path_eq          = BER_BVC("PATH="),
72         aci_bv_dirsep           = BER_BVC(LDAP_DIRSEP),
73 #endif /* LDAP_PF_LOCAL */
74         
75         aci_bv_group_class      = BER_BVC(SLAPD_GROUP_CLASS),
76         aci_bv_group_attr       = BER_BVC(SLAPD_GROUP_ATTR),
77         aci_bv_role_class       = BER_BVC(SLAPD_ROLE_CLASS),
78         aci_bv_role_attr        = BER_BVC(SLAPD_ROLE_ATTR),
79         aci_bv_set_attr         = BER_BVC(SLAPD_ACI_SET_ATTR);
80
81 typedef enum slap_aci_scope_t {
82         SLAP_ACI_SCOPE_ENTRY            = 0x1,
83         SLAP_ACI_SCOPE_CHILDREN         = 0x2,
84         SLAP_ACI_SCOPE_SUBTREE          = ( SLAP_ACI_SCOPE_ENTRY | SLAP_ACI_SCOPE_CHILDREN )
85 } slap_aci_scope_t;
86
87 static AccessControl * acl_get(
88         AccessControl *ac, int *count,
89         Operation *op, Entry *e,
90         AttributeDescription *desc,
91         struct berval *val,
92         int nmatch, regmatch_t *matches,
93         AccessControlState *state );
94
95 static slap_control_t acl_mask(
96         AccessControl *ac, slap_mask_t *mask,
97         Operation *op, Entry *e,
98         AttributeDescription *desc,
99         struct berval *val,
100         int nmatch,
101         regmatch_t *matches,
102         int count,
103         AccessControlState *state );
104
105 #ifdef SLAPD_ACI_ENABLED
106 static int aci_mask(
107         Operation *op, Entry *e,
108         AttributeDescription *desc,
109         struct berval *val,
110         struct berval *aci,
111         int nmatch,
112         regmatch_t *matches,
113         slap_access_t *grant,
114         slap_access_t *deny,
115         slap_aci_scope_t scope);
116 #endif /* SLAPD_ACI_ENABLED */
117
118 static int      regex_matches(
119         struct berval *pat, char *str, char *buf,
120         int nmatch, regmatch_t *matches);
121 static int      string_expand(
122         struct berval *newbuf, struct berval *pattern,
123         char *match, int nmatch, regmatch_t *matches);
124
125 typedef struct AciSetCookie {
126         Operation *op;
127         Entry *e;
128 } AciSetCookie;
129
130 SLAP_SET_GATHER aci_set_gather;
131 SLAP_SET_GATHER aci_set_gather2;
132 static int aci_match_set ( struct berval *subj, Operation *op,
133     Entry *e, int setref );
134
135 /*
136  * access_allowed - check whether op->o_ndn is allowed the requested access
137  * to entry e, attribute attr, value val.  if val is null, access to
138  * the whole attribute is assumed (all values).
139  *
140  * This routine loops through all access controls and calls
141  * acl_mask() on each applicable access control.
142  * The loop exits when a definitive answer is reached or
143  * or no more controls remain.
144  *
145  * returns:
146  *              0       access denied
147  *              1       access granted
148  *
149  * Notes:
150  * - can be legally called with op == NULL
151  * - can be legally called with op->o_bd == NULL
152  */
153
154 int
155 access_allowed_mask(
156         Operation               *op,
157         Entry                   *e,
158         AttributeDescription    *desc,
159         struct berval           *val,
160         slap_access_t           access,
161         AccessControlState      *state,
162         slap_mask_t             *maskp )
163 {
164         int                             ret = 1;
165         int                             count;
166         AccessControl                   *a = NULL;
167         Backend                         *be;
168         int                             be_null = 0;
169
170 #ifdef LDAP_DEBUG
171         char                            accessmaskbuf[ACCESSMASK_MAXLEN];
172 #endif
173         slap_mask_t                     mask;
174         slap_control_t                  control;
175         slap_access_t                   access_level;
176         const char                      *attr;
177         regmatch_t                      matches[MAXREMATCHES];
178         int                             st_same_attr = 0;
179         static AccessControlState       state_init = ACL_STATE_INIT;
180
181         assert( e != NULL );
182         assert( desc != NULL );
183
184         access_level = ACL_LEVEL( access );
185
186         assert( access_level > ACL_NONE );
187         if ( maskp ) ACL_INVALIDATE( *maskp );
188
189         attr = desc->ad_cname.bv_val;
190
191         assert( attr != NULL );
192
193         if( op && op->o_is_auth_check &&
194                 ( access_level == ACL_SEARCH || access_level == ACL_READ ))
195         {
196                 access = ACL_AUTH;
197         }
198
199         if( state ) {
200                 if ( state->as_vd_ad==desc) {
201                         if ( state->as_recorded ) {
202                                 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
203                                         val == NULL )
204                                 {
205                                         return state->as_result;
206                                 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
207                                         val != NULL && state->as_vd_acl == NULL )
208                                 {
209                                         return state->as_result;
210                                 }
211                         }
212                         st_same_attr = 1;
213                 } else {
214                         *state = state_init;
215                 }
216
217                 state->as_vd_ad=desc;
218         }
219
220         Debug( LDAP_DEBUG_ACL,
221                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
222             access2str( access ), e->e_dn, attr );
223
224         if ( op == NULL ) {
225                 /* no-op call */
226                 goto done;
227         }
228
229         be = op->o_bd;
230         if ( be == NULL ) {
231                 be = LDAP_STAILQ_FIRST(&backendDB);
232                 be_null = 1;
233 #ifdef LDAP_DEVEL
234                 /*
235                  * FIXME: experimental; use first backend rules
236                  * iff there is no global_acl (ITS#3100) */
237                 if ( frontendDB->be_acl == NULL ) 
238 #endif
239                 {
240                         op->o_bd = be;
241                 }
242         }
243         assert( be != NULL );
244
245 #ifdef LDAP_SLAPI
246         if ( op->o_pb != NULL ) {
247                 ret = slapi_int_access_allowed( op, e, desc, val, access, state );
248                 if ( ret == 0 ) {
249                         /* ACL plugin denied access */
250                         goto done;
251                 }
252         }
253 #endif /* LDAP_SLAPI */
254
255         /* grant database root access */
256         if ( /* be != NULL && */ be_isroot( op ) ) {
257                 Debug( LDAP_DEBUG_ACL,
258                     "<= root access granted\n",
259                         0, 0, 0 );
260                 if ( maskp ) {
261                         mask = ACL_LVL_MANAGE;
262                 }
263
264                 goto done;
265         }
266
267         /*
268          * no-user-modification operational attributes are ignored
269          * by ACL_WRITE checking as any found here are not provided
270          * by the user
271          */
272         if ( access_level >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
273                 && desc != slap_schema.si_ad_entry
274                 && desc != slap_schema.si_ad_children )
275         {
276                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
277                         " %s access granted\n",
278                         attr, 0, 0 );
279                 goto done;
280         }
281
282         /* use backend default access if no backend acls */
283         if( be != NULL && be->be_acl == NULL ) {
284                 Debug( LDAP_DEBUG_ACL,
285                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
286                         access2str( access ),
287                         be->be_dfltaccess >= access_level ? "granted" : "denied",
288                         op->o_dn.bv_val ? op->o_dn.bv_val : "(anonymous)" );
289                 ret = be->be_dfltaccess >= access_level;
290
291                 if ( maskp ) {
292                         int     i;
293
294                         mask = ACL_PRIV_LEVEL;
295                         for ( i = ACL_NONE; i <= be->be_dfltaccess; i++ ) {
296                                 mask |= ACL_ACCESS2PRIV( i );
297                         }
298                 }
299
300                 goto done;
301
302 #ifdef notdef
303         /* be is always non-NULL */
304         /* use global default access if no global acls */
305         } else if ( be == NULL && frontendDB->be_acl == NULL ) {
306                 Debug( LDAP_DEBUG_ACL,
307                         "=> access_allowed: global default %s access %s to \"%s\"\n",
308                         access2str( access ),
309                         frontendDB->be_dfltaccess >= access_level ? "granted" : "denied", op->o_dn.bv_val );
310                 ret = frontendDB->be_dfltaccess >= access_level;
311
312                 if ( maskp ) {
313                         int     i;
314
315                         mask = ACL_PRIV_LEVEL;
316                         for ( i = ACL_NONE; i <= global_default_access; i++ ) {
317                                 mask |= ACL_ACCESS2PRIV( i );
318                         }
319                 }
320
321                 goto done;
322 #endif
323         }
324
325         ret = 0;
326         control = ACL_BREAK;
327
328         if( st_same_attr ) {
329                 assert( state->as_vd_acl != NULL );
330
331                 a = state->as_vd_acl;
332                 count = state->as_vd_acl_count;
333                 if ( !ACL_IS_INVALID( state->as_vd_acl_mask )) {
334                         mask = state->as_vd_acl_mask;
335                         AC_MEMCPY( matches, state->as_vd_acl_matches, sizeof(matches) );
336                         goto vd_access;
337                 }
338
339         } else {
340                 if ( state ) state->as_vi_acl = NULL;
341                 a = NULL;
342                 ACL_INIT(mask);
343                 count = 0;
344                 memset(matches, '\0', sizeof(matches));
345         }
346
347         while((a = acl_get( a, &count, op, e, desc, val,
348                 MAXREMATCHES, matches, state )) != NULL)
349         {
350                 int i;
351
352                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
353                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
354                             (int)matches[i].rm_so, (int)matches[i].rm_eo );
355                         if( matches[i].rm_so <= matches[0].rm_eo ) {
356                                 int n;
357                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
358                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
359                                 }
360                         }
361                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
362                 }
363
364                 if (state) {
365                         if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
366                                 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
367                                 ret = state->as_result;
368                                 goto done;
369                         } else {
370                                 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
371                         }
372                 }
373
374 vd_access:
375                 control = acl_mask( a, &mask, op,
376                         e, desc, val, MAXREMATCHES, matches, count, state );
377
378                 if ( control != ACL_BREAK ) {
379                         break;
380                 }
381
382                 memset(matches, '\0', sizeof(matches));
383         }
384
385         if ( ACL_IS_INVALID( mask ) ) {
386                 Debug( LDAP_DEBUG_ACL,
387                         "=> access_allowed: \"%s\" (%s) invalid!\n",
388                         e->e_dn, attr, 0 );
389                 ACL_INIT(mask);
390
391         } else if ( control == ACL_BREAK ) {
392                 Debug( LDAP_DEBUG_ACL,
393                         "=> access_allowed: no more rules\n", 0, 0, 0);
394
395                 goto done;
396         }
397
398         Debug( LDAP_DEBUG_ACL,
399                 "=> access_allowed: %s access %s by %s\n",
400                 access2str( access ),
401                 ACL_GRANT(mask, access) ? "granted" : "denied",
402                 accessmask2str( mask, accessmaskbuf, 1 ) );
403
404         ret = ACL_GRANT(mask, access);
405
406 done:
407         if( state != NULL ) {
408                 /* If not value-dependent, save ACL in case of more attrs */
409                 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) ) {
410                         state->as_vi_acl = a;
411                         state->as_result = ret;
412                 }
413                 state->as_recorded |= ACL_STATE_RECORDED;
414         }
415         if (be_null) op->o_bd = NULL;
416         if ( maskp ) *maskp = mask;
417         return ret;
418 }
419
420
421 /*
422  * acl_get - return the acl applicable to entry e, attribute
423  * attr.  the acl returned is suitable for use in subsequent calls to
424  * acl_access_allowed().
425  */
426
427 static AccessControl *
428 acl_get(
429         AccessControl *a,
430         int                     *count,
431         Operation       *op,
432         Entry           *e,
433         AttributeDescription *desc,
434         struct berval   *val,
435         int                     nmatch,
436         regmatch_t      *matches,
437         AccessControlState *state )
438 {
439         const char *attr;
440         int dnlen, patlen;
441         AccessControl *prev;
442
443         assert( e != NULL );
444         assert( count != NULL );
445         assert( desc != NULL );
446
447         attr = desc->ad_cname.bv_val;
448
449         assert( attr != NULL );
450
451         if( a == NULL ) {
452                 if( op->o_bd == NULL ) {
453                         a = frontendDB->be_acl;
454                 } else {
455                         a = op->o_bd->be_acl;
456                 }
457                 prev = NULL;
458
459                 assert( a != NULL );
460
461         } else {
462                 prev = a;
463                 a = a->acl_next;
464         }
465
466         dnlen = e->e_nname.bv_len;
467
468         for ( ; a != NULL; a = a->acl_next ) {
469                 (*count) ++;
470
471                 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
472                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
473                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
474                                         *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
475                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
476                                         continue;
477
478                         } else {
479                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
480                                         *count, a->acl_dn_pat.bv_val, 0 );
481                                 patlen = a->acl_dn_pat.bv_len;
482                                 if ( dnlen < patlen )
483                                         continue;
484
485                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
486                                         /* base dn -- entire object DN must match */
487                                         if ( dnlen != patlen )
488                                                 continue;
489
490                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
491                                         int     rdnlen = -1, sep = 0;
492
493                                         if ( dnlen <= patlen )
494                                                 continue;
495
496                                         if ( patlen > 0 ) {
497                                                 if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
498                                                         continue;
499                                                 sep = 1;
500                                         }
501
502                                         rdnlen = dn_rdnlen( NULL, &e->e_nname );
503                                         if ( rdnlen != dnlen - patlen - sep )
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                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
522                                 *count, 0, 0 );
523                 }
524
525                 if ( a->acl_attrs && !ad_inlist( desc, a->acl_attrs ) ) {
526                         matches[0].rm_so = matches[0].rm_eo = -1;
527                         continue;
528                 }
529
530                 /* Is this ACL only for a specific value? */
531                 if ( a->acl_attrval.bv_len ) {
532                         if ( val == NULL ) {
533                                 continue;
534                         }
535
536                         if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) {
537                                 state->as_recorded |= ACL_STATE_RECORDED_VD;
538                                 state->as_vd_acl = prev;
539                                 state->as_vd_acl_count = *count;
540                                 state->as_vd_access = a->acl_access;
541                                 state->as_vd_access_count = 1;
542                                 ACL_INVALIDATE( state->as_vd_acl_mask );
543                         }
544
545                         if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
546                                 Debug( LDAP_DEBUG_ACL,
547                                         "acl_get: valpat %s\n",
548                                         a->acl_attrval.bv_val, 0, 0 );
549                                 if ( regexec( &a->acl_attrval_re, val->bv_val, 0, NULL, 0 ) )
550                                 {
551                                         continue;
552                                 }
553
554                         } else {
555                                 int match = 0;
556                                 const char *text;
557                                 Debug( LDAP_DEBUG_ACL,
558                                         "acl_get: val %s\n",
559                                         a->acl_attrval.bv_val, 0, 0 );
560         
561                                 if ( a->acl_attrs[0].an_desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
562                                         if (value_match( &match, desc,
563                                                 desc->ad_type->sat_equality, 0,
564                                                 val, &a->acl_attrval, &text ) != LDAP_SUCCESS ||
565                                                         match )
566                                                 continue;
567                                         
568                                 } else {
569                                         int             patlen, vdnlen;
570         
571                                         patlen = a->acl_attrval.bv_len;
572                                         vdnlen = val->bv_len;
573         
574                                         if ( vdnlen < patlen )
575                                                 continue;
576         
577                                         if ( a->acl_dn_style == ACL_STYLE_BASE ) {
578                                                 if ( vdnlen > patlen )
579                                                         continue;
580         
581                                         } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
582                                                 int rdnlen = -1;
583         
584                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
585                                                         continue;
586         
587                                                 rdnlen = dn_rdnlen( NULL, val );
588                                                 if ( rdnlen != vdnlen - patlen - 1 )
589                                                         continue;
590         
591                                         } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
592                                                 if ( vdnlen > patlen && !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
593                                                         continue;
594         
595                                         } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
596                                                 if ( vdnlen <= patlen )
597                                                         continue;
598         
599                                                 if ( !DN_SEPARATOR( val->bv_val[vdnlen - patlen - 1] ) )
600                                                         continue;
601                                         }
602         
603                                         if ( strcmp( a->acl_attrval.bv_val, val->bv_val + vdnlen - patlen ))
604                                                 continue;
605                                 }
606                         }
607                 }
608
609                 if ( a->acl_filter != NULL ) {
610                         ber_int_t rc = test_filter( NULL, e, a->acl_filter );
611                         if ( rc != LDAP_COMPARE_TRUE ) {
612                                 continue;
613                         }
614                 }
615
616                 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] attr %s\n",
617                        *count, attr, 0);
618                 return a;
619         }
620
621         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
622         return( NULL );
623 }
624
625 static int
626 acl_mask_dn(
627         Operation               *op,
628         Entry                   *e,
629         AccessControl           *a,
630         int                     nmatch,
631         regmatch_t              *matches,
632         slap_dn_access          *b,
633         struct berval           *opndn )
634 {
635         /*
636          * if access applies to the entry itself, and the
637          * user is bound as somebody in the same namespace as
638          * the entry, OR the given dn matches the dn pattern
639          */
640         /*
641          * NOTE: styles "anonymous", "users" and "self" 
642          * have been moved to enum slap_style_t, whose 
643          * value is set in a_dn_style; however, the string
644          * is maintaned in a_dn_pat.
645          */
646         if ( b->a_style == ACL_STYLE_ANONYMOUS ) {
647                 if ( !BER_BVISEMPTY( opndn ) ) {
648                         return 1;
649                 }
650
651         } else if ( b->a_style == ACL_STYLE_USERS ) {
652                 if ( BER_BVISEMPTY( opndn ) ) {
653                         return 1;
654                 }
655
656         } else if ( b->a_style == ACL_STYLE_SELF ) {
657                 struct berval   ndn, selfndn;
658                 int             level;
659
660                 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
661                         return 1;
662                 }
663
664                 level = b->a_self_level;
665                 if ( level < 0 ) {
666                         selfndn = *opndn;
667                         ndn = e->e_nname;
668                         level = -level;
669
670                 } else {
671                         ndn = *opndn;
672                         selfndn = e->e_nname;
673                 }
674
675                 for ( ; level > 0; level-- ) {
676                         if ( BER_BVISEMPTY( &ndn ) ) {
677                                 break;
678                         }
679                         dnParent( &ndn, &ndn );
680                 }
681                         
682                 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
683                 {
684                         return 1;
685                 }
686
687         } else if ( b->a_style == ACL_STYLE_REGEX ) {
688                 if ( !ber_bvccmp( &b->a_pat, '*' ) ) {
689                         int             tmp_nmatch;
690                         regmatch_t      tmp_matches[2],
691                                         *tmp_matchesp = tmp_matches;
692
693                         int             rc = 0;
694
695                         switch ( a->acl_dn_style ) {
696                         case ACL_STYLE_REGEX:
697                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
698                                         tmp_matchesp = matches;
699                                         tmp_nmatch = nmatch;
700                                         break;
701                                 }
702                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
703
704                         case ACL_STYLE_BASE:
705                                 tmp_matches[0].rm_so = 0;
706                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
707                                 tmp_nmatch = 1;
708                                 break;
709
710                         case ACL_STYLE_ONE:
711                         case ACL_STYLE_SUBTREE:
712                         case ACL_STYLE_CHILDREN:
713                                 tmp_matches[0].rm_so = 0;
714                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
715                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
716                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
717                                 tmp_nmatch = 2;
718                                 break;
719
720                         default:
721                                 /* error */
722                                 rc = 1;
723                                 break;
724                         }
725
726                         if ( rc ) {
727                                 return 1;
728                         }
729
730                         if ( !regex_matches( &b->a_pat, opndn->bv_val,
731                                 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
732                         {
733                                 return 1;
734                         }
735                 }
736
737         } else {
738                 struct berval   pat;
739                 ber_len_t       patlen, odnlen;
740                 int             got_match = 0;
741
742                 if ( e->e_dn == NULL )
743                         return 1;
744
745                 if ( b->a_expand ) {
746                         struct berval   bv;
747                         char            buf[ACL_BUF_SIZE];
748                         
749                         int             tmp_nmatch;
750                         regmatch_t      tmp_matches[2],
751                                         *tmp_matchesp = tmp_matches;
752
753                         int             rc = 0;
754
755                         bv.bv_len = sizeof( buf ) - 1;
756                         bv.bv_val = buf;
757
758                         switch ( a->acl_dn_style ) {
759                         case ACL_STYLE_REGEX:
760                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
761                                         tmp_matchesp = matches;
762                                         tmp_nmatch = nmatch;
763                                         break;
764                                 }
765                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
766
767                         case ACL_STYLE_BASE:
768                                 tmp_matches[0].rm_so = 0;
769                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
770                                 tmp_nmatch = 1;
771                                 break;
772
773                         case ACL_STYLE_ONE:
774                         case ACL_STYLE_SUBTREE:
775                         case ACL_STYLE_CHILDREN:
776                                 tmp_matches[0].rm_so = 0;
777                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
778                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
779                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
780                                 tmp_nmatch = 2;
781                                 break;
782
783                         default:
784                                 /* error */
785                                 rc = 1;
786                                 break;
787                         }
788
789                         if ( rc ) {
790                                 return 1;
791                         }
792
793                         if ( string_expand( &bv, &b->a_pat, 
794                                         e->e_nname.bv_val,
795                                         tmp_nmatch, tmp_matchesp ) )
796                         {
797                                 return 1;
798                         }
799                         
800                         if ( dnNormalize(0, NULL, NULL, &bv,
801                                         &pat, op->o_tmpmemctx )
802                                         != LDAP_SUCCESS )
803                         {
804                                 /* did not expand to a valid dn */
805                                 return 1;
806                         }
807
808                 } else {
809                         pat = b->a_pat;
810                 }
811
812                 patlen = pat.bv_len;
813                 odnlen = opndn->bv_len;
814                 if ( odnlen < patlen ) {
815                         goto dn_match_cleanup;
816
817                 }
818
819                 if ( b->a_style == ACL_STYLE_BASE ) {
820                         /* base dn -- entire object DN must match */
821                         if ( odnlen != patlen ) {
822                                 goto dn_match_cleanup;
823                         }
824
825                 } else if ( b->a_style == ACL_STYLE_ONE ) {
826                         int rdnlen = -1;
827
828                         if ( odnlen <= patlen ) {
829                                 goto dn_match_cleanup;
830                         }
831
832                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
833                                 goto dn_match_cleanup;
834                         }
835
836                         rdnlen = dn_rdnlen( NULL, opndn );
837                         if ( rdnlen != odnlen - patlen - 1 ) {
838                                 goto dn_match_cleanup;
839                         }
840
841                 } else if ( b->a_style == ACL_STYLE_SUBTREE ) {
842                         if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
843                                 goto dn_match_cleanup;
844                         }
845
846                 } else if ( b->a_style == ACL_STYLE_CHILDREN ) {
847                         if ( odnlen <= patlen ) {
848                                 goto dn_match_cleanup;
849                         }
850
851                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
852                                 goto dn_match_cleanup;
853                         }
854
855                 } else if ( b->a_style == ACL_STYLE_LEVEL ) {
856                         int level;
857                         struct berval ndn;
858
859                         if ( odnlen <= patlen ) {
860                                 goto dn_match_cleanup;
861                         }
862
863                         if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
864                         {
865                                 goto dn_match_cleanup;
866                         }
867                         
868                         level = b->a_level;
869                         ndn = *opndn;
870                         for ( ; level > 0; level-- ) {
871                                 if ( BER_BVISEMPTY( &ndn ) ) {
872                                         goto dn_match_cleanup;
873                                 }
874                                 dnParent( &ndn, &ndn );
875                                 if ( ndn.bv_len < patlen ) {
876                                         goto dn_match_cleanup;
877                                 }
878                         }
879                         
880                         if ( ndn.bv_len != patlen ) {
881                                 goto dn_match_cleanup;
882                         }
883                 }
884
885                 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
886
887 dn_match_cleanup:;
888                 if ( pat.bv_val != b->a_pat.bv_val ) {
889                         slap_sl_free( pat.bv_val, op->o_tmpmemctx );
890                 }
891
892                 if ( !got_match ) {
893                         return 1;
894                 }
895         }
896
897         return 0;
898 }
899
900 /*
901  * Record value-dependent access control state
902  */
903 #define ACL_RECORD_VALUE_STATE do { \
904                 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
905                         state->as_recorded |= ACL_STATE_RECORDED_VD; \
906                         state->as_vd_acl = a; \
907                         AC_MEMCPY( state->as_vd_acl_matches, matches, \
908                                 sizeof( state->as_vd_acl_matches )) ; \
909                         state->as_vd_acl_count = count; \
910                         state->as_vd_access = b; \
911                         state->as_vd_access_count = i; \
912                 } \
913         } while( 0 )
914
915 static int
916 acl_mask_dnattr(
917         Operation               *op,
918         Entry                   *e,
919         struct berval           *val,
920         AccessControl           *a,
921         Access                  *b,
922         int                     i,
923         regmatch_t              *matches,
924         int                     count,
925         AccessControlState      *state,
926         slap_dn_access          *bdn,
927         struct berval           *opndn )
928 {
929         Attribute       *at;
930         struct berval   bv;
931         int             rc, match = 0;
932         const char      *text;
933         const char      *attr = bdn->a_at->ad_cname.bv_val;
934
935         assert( attr != NULL );
936
937         if ( BER_BVISEMPTY( opndn ) ) {
938                 return 1;
939         }
940
941         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
942         bv = *opndn;
943
944         /* see if asker is listed in dnattr */
945         for ( at = attrs_find( e->e_attrs, bdn->a_at );
946                 at != NULL;
947                 at = attrs_find( at->a_next, bdn->a_at ) )
948         {
949                 if ( value_find_ex( bdn->a_at,
950                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
951                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
952                         at->a_nvals,
953                         &bv, op->o_tmpmemctx ) == 0 )
954                 {
955                         /* found it */
956                         match = 1;
957                         break;
958                 }
959         }
960
961         if ( match ) {
962                 /* have a dnattr match. if this is a self clause then
963                  * the target must also match the op dn.
964                  */
965                 if ( bdn->a_self ) {
966                         /* check if the target is an attribute. */
967                         if ( val == NULL ) return 1;
968
969                         /* target is attribute, check if the attribute value
970                          * is the op dn.
971                          */
972                         rc = value_match( &match, bdn->a_at,
973                                 bdn->a_at->ad_type->sat_equality, 0,
974                                 val, &bv, &text );
975                         /* on match error or no match, fail the ACL clause */
976                         if ( rc != LDAP_SUCCESS || match != 0 )
977                                 return 1;
978                 }
979
980         } else {
981                 /* no dnattr match, check if this is a self clause */
982                 if ( ! bdn->a_self )
983                         return 1;
984
985                 ACL_RECORD_VALUE_STATE;
986                 
987                 /* this is a self clause, check if the target is an
988                  * attribute.
989                  */
990                 if ( val == NULL )
991                         return 1;
992
993                 /* target is attribute, check if the attribute value
994                  * is the op dn.
995                  */
996                 rc = value_match( &match, bdn->a_at,
997                         bdn->a_at->ad_type->sat_equality, 0,
998                         val, &bv, &text );
999
1000                 /* on match error or no match, fail the ACL clause */
1001                 if ( rc != LDAP_SUCCESS || match != 0 )
1002                         return 1;
1003         }
1004
1005         return 0;
1006 }
1007
1008
1009 /*
1010  * acl_mask - modifies mask based upon the given acl and the
1011  * requested access to entry e, attribute attr, value val.  if val
1012  * is null, access to the whole attribute is assumed (all values).
1013  *
1014  * returns      0       access NOT allowed
1015  *              1       access allowed
1016  */
1017
1018 static slap_control_t
1019 acl_mask(
1020         AccessControl           *a,
1021         slap_mask_t             *mask,
1022         Operation               *op,
1023         Entry                   *e,
1024         AttributeDescription    *desc,
1025         struct berval           *val,
1026         int                     nmatch,
1027         regmatch_t              *matches,
1028         int                     count,
1029         AccessControlState      *state )
1030 {
1031         int             i;
1032         Access  *b;
1033 #ifdef LDAP_DEBUG
1034         char accessmaskbuf[ACCESSMASK_MAXLEN];
1035 #if !defined( SLAP_DYNACL ) && defined( SLAPD_ACI_ENABLED )
1036         char accessmaskbuf1[ACCESSMASK_MAXLEN];
1037 #endif /* !SLAP_DYNACL && SLAPD_ACI_ENABLED */
1038 #endif /* DEBUG */
1039         const char *attr;
1040
1041         assert( a != NULL );
1042         assert( mask != NULL );
1043         assert( desc != NULL );
1044
1045         attr = desc->ad_cname.bv_val;
1046
1047         assert( attr != NULL );
1048
1049         Debug( LDAP_DEBUG_ACL,
1050                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1051                 e->e_dn, attr, 0 );
1052
1053         Debug( LDAP_DEBUG_ACL,
1054                 "=> acl_mask: to %s by \"%s\", (%s) \n",
1055                 val ? "value" : "all values",
1056                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
1057                 accessmask2str( *mask, accessmaskbuf, 1) );
1058
1059
1060         if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
1061                 && state->as_vd_acl == a )
1062         {
1063                 b = state->as_vd_access;
1064                 i = state->as_vd_access_count;
1065
1066         } else {
1067                 b = a->acl_access;
1068                 i = 1;
1069         }
1070
1071         for ( ; b != NULL; b = b->a_next, i++ ) {
1072                 slap_mask_t oldmask, modmask;
1073
1074                 ACL_INVALIDATE( modmask );
1075
1076                 /* AND <who> clauses */
1077                 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1078                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1079                                 b->a_dn_pat.bv_val, 0, 0);
1080                         /*
1081                          * if access applies to the entry itself, and the
1082                          * user is bound as somebody in the same namespace as
1083                          * the entry, OR the given dn matches the dn pattern
1084                          */
1085                         /*
1086                          * NOTE: styles "anonymous", "users" and "self" 
1087                          * have been moved to enum slap_style_t, whose 
1088                          * value is set in a_dn_style; however, the string
1089                          * is maintaned in a_dn_pat.
1090                          */
1091
1092                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1093                                 &b->a_dn, &op->o_ndn ) )
1094                         {
1095                                 continue;
1096                         }
1097                 }
1098
1099                 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1100                         struct berval   ndn;
1101
1102                         Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1103                                 b->a_realdn_pat.bv_val, 0, 0);
1104                         /*
1105                          * if access applies to the entry itself, and the
1106                          * user is bound as somebody in the same namespace as
1107                          * the entry, OR the given dn matches the dn pattern
1108                          */
1109                         /*
1110                          * NOTE: styles "anonymous", "users" and "self" 
1111                          * have been moved to enum slap_style_t, whose 
1112                          * value is set in a_dn_style; however, the string
1113                          * is maintaned in a_dn_pat.
1114                          */
1115
1116                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1117                         {
1118                                 ndn = op->o_conn->c_ndn;
1119                         } else {
1120                                 ndn = op->o_ndn;
1121                         }
1122
1123                         if ( acl_mask_dn( op, e, a, nmatch, matches,
1124                                 &b->a_realdn, &ndn ) )
1125                         {
1126                                 continue;
1127                         }
1128                 }
1129
1130                 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1131                         if ( ! op->o_conn->c_listener ) {
1132                                 continue;
1133                         }
1134                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1135                                 b->a_sockurl_pat.bv_val, 0, 0 );
1136
1137                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1138                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1139                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1140                                                         e->e_ndn, nmatch, matches ) ) 
1141                                         {
1142                                                 continue;
1143                                         }
1144
1145                                 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1146                                         struct berval   bv;
1147                                         char buf[ACL_BUF_SIZE];
1148
1149                                         bv.bv_len = sizeof( buf ) - 1;
1150                                         bv.bv_val = buf;
1151                                         if ( string_expand( &bv, &b->a_sockurl_pat,
1152                                                         e->e_ndn, nmatch, matches ) )
1153                                         {
1154                                                 continue;
1155                                         }
1156
1157                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1158                                         {
1159                                                 continue;
1160                                         }
1161
1162                                 } else {
1163                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1164                                         {
1165                                                 continue;
1166                                         }
1167                                 }
1168                         }
1169                 }
1170
1171                 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1172                         if ( !op->o_conn->c_peer_domain.bv_val ) {
1173                                 continue;
1174                         }
1175                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1176                                 b->a_domain_pat.bv_val, 0, 0 );
1177                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1178                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1179                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1180                                                         e->e_ndn, nmatch, matches ) ) 
1181                                         {
1182                                                 continue;
1183                                         }
1184                                 } else {
1185                                         char buf[ACL_BUF_SIZE];
1186
1187                                         struct berval   cmp = op->o_conn->c_peer_domain;
1188                                         struct berval   pat = b->a_domain_pat;
1189
1190                                         if ( b->a_domain_expand ) {
1191                                                 struct berval bv;
1192
1193                                                 bv.bv_len = sizeof(buf) - 1;
1194                                                 bv.bv_val = buf;
1195
1196                                                 if ( string_expand(&bv, &b->a_domain_pat,
1197                                                                 e->e_ndn, nmatch, matches) )
1198                                                 {
1199                                                         continue;
1200                                                 }
1201                                                 pat = bv;
1202                                         }
1203
1204                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1205                                                 int offset = cmp.bv_len - pat.bv_len;
1206                                                 if ( offset < 0 ) {
1207                                                         continue;
1208                                                 }
1209
1210                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1211                                                         continue;
1212                                                 }
1213
1214                                                 /* trim the domain */
1215                                                 cmp.bv_val = &cmp.bv_val[ offset ];
1216                                                 cmp.bv_len -= offset;
1217                                         }
1218                                         
1219                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1220                                                 continue;
1221                                         }
1222                                 }
1223                         }
1224                 }
1225
1226                 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1227                         if ( !op->o_conn->c_peer_name.bv_val ) {
1228                                 continue;
1229                         }
1230                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1231                                 b->a_peername_pat.bv_val, 0, 0 );
1232                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1233                                 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1234                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1235                                                         e->e_ndn, nmatch, matches ) ) 
1236                                         {
1237                                                 continue;
1238                                         }
1239
1240                                 } else {
1241                                         /* try exact match */
1242                                         if ( b->a_peername_style == ACL_STYLE_BASE ) {
1243                                                 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1244                                                         continue;
1245                                                 }
1246
1247                                         } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1248                                                 struct berval   bv;
1249                                                 char buf[ACL_BUF_SIZE];
1250
1251                                                 bv.bv_len = sizeof( buf ) - 1;
1252                                                 bv.bv_val = buf;
1253                                                 if ( string_expand( &bv, &b->a_peername_pat,
1254                                                                 e->e_ndn, nmatch, matches ) )
1255                                                 {
1256                                                         continue;
1257                                                 }
1258
1259                                                 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1260                                                         continue;
1261                                                 }
1262
1263                                         /* extract IP and try exact match */
1264                                         } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1265                                                 char            *port;
1266                                                 char            buf[] = "255.255.255.255";
1267                                                 struct berval   ip;
1268                                                 unsigned long   addr;
1269                                                 int             port_number = -1;
1270                                                 
1271                                                 if ( strncasecmp( op->o_conn->c_peer_name.bv_val, 
1272                                                                         aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 ) 
1273                                                         continue;
1274
1275                                                 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1276                                                 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1277
1278                                                 port = strrchr( ip.bv_val, ':' );
1279                                                 if ( port ) {
1280                                                         char    *next;
1281                                                         
1282                                                         ip.bv_len = port - ip.bv_val;
1283                                                         ++port;
1284                                                         port_number = strtol( port, &next, 10 );
1285                                                         if ( next[0] != '\0' )
1286                                                                 continue;
1287                                                 }
1288                                                 
1289                                                 /* the port check can be anticipated here */
1290                                                 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1291                                                         continue;
1292                                                 
1293                                                 /* address longer than expected? */
1294                                                 if ( ip.bv_len >= sizeof(buf) )
1295                                                         continue;
1296
1297                                                 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1298                                                 buf[ ip.bv_len ] = '\0';
1299
1300                                                 addr = inet_addr( buf );
1301
1302                                                 /* unable to convert? */
1303                                                 if ( addr == (unsigned long)(-1) )
1304                                                         continue;
1305
1306                                                 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1307                                                         continue;
1308
1309 #ifdef LDAP_PF_LOCAL
1310                                         /* extract path and try exact match */
1311                                         } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1312                                                 struct berval path;
1313                                                 
1314                                                 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1315                                                                         aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1316                                                         continue;
1317
1318                                                 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1319                                                 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1320
1321                                                 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1322                                                         continue;
1323
1324 #endif /* LDAP_PF_LOCAL */
1325
1326                                         /* exact match (very unlikely...) */
1327                                         } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1328                                                         continue;
1329                                         }
1330                                 }
1331                         }
1332                 }
1333
1334                 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1335                         if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1336                                 continue;
1337                         }
1338                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1339                                 b->a_sockname_pat.bv_val, 0, 0 );
1340                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1341                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1342                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1343                                                         e->e_ndn, nmatch, matches ) ) 
1344                                         {
1345                                                 continue;
1346                                         }
1347
1348                                 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1349                                         struct berval   bv;
1350                                         char buf[ACL_BUF_SIZE];
1351
1352                                         bv.bv_len = sizeof( buf ) - 1;
1353                                         bv.bv_val = buf;
1354                                         if ( string_expand( &bv, &b->a_sockname_pat,
1355                                                         e->e_ndn, nmatch, matches ) )
1356                                         {
1357                                                 continue;
1358                                         }
1359
1360                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1361                                                 continue;
1362                                         }
1363
1364                                 } else {
1365                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1366                                                 continue;
1367                                         }
1368                                 }
1369                         }
1370                 }
1371
1372                 if ( b->a_dn_at != NULL ) {
1373                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1374                                         matches, count, state,
1375                                         &b->a_dn, &op->o_ndn ) )
1376                         {
1377                                 continue;
1378                         }
1379                 }
1380
1381                 if ( b->a_realdn_at != NULL ) {
1382                         struct berval   ndn;
1383
1384                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1385                         {
1386                                 ndn = op->o_conn->c_ndn;
1387                         } else {
1388                                 ndn = op->o_ndn;
1389                         }
1390
1391                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1392                                         matches, count, state,
1393                                         &b->a_realdn, &ndn ) )
1394                         {
1395                                 continue;
1396                         }
1397                 }
1398
1399                 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1400                         struct berval bv;
1401                         struct berval ndn = BER_BVNULL;
1402                         int rc;
1403
1404                         if ( op->o_ndn.bv_len == 0 ) {
1405                                 continue;
1406                         }
1407
1408                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1409                          * entry with objectclass group* and we test to see if odn is one of
1410                          * the values in the attribute group
1411                          */
1412                         /* see if asker is listed in dnattr */
1413                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1414                                 char            buf[ACL_BUF_SIZE];
1415                                 int             tmp_nmatch;
1416                                 regmatch_t      tmp_matches[2],
1417                                                 *tmp_matchesp = tmp_matches;
1418
1419                                 bv.bv_len = sizeof(buf) - 1;
1420                                 bv.bv_val = buf;
1421
1422                                 rc = 0;
1423
1424                                 switch ( a->acl_dn_style ) {
1425                                 case ACL_STYLE_REGEX:
1426                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1427                                                 tmp_matchesp = matches;
1428                                                 tmp_nmatch = nmatch;
1429                                                 break;
1430                                         }
1431
1432                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1433                                 case ACL_STYLE_BASE:
1434                                         tmp_matches[0].rm_so = 0;
1435                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1436                                         tmp_nmatch = 1;
1437                                         break;
1438
1439                                 case ACL_STYLE_ONE:
1440                                 case ACL_STYLE_SUBTREE:
1441                                 case ACL_STYLE_CHILDREN:
1442                                         tmp_matches[0].rm_so = 0;
1443                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1444                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1445                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1446                                         tmp_nmatch = 2;
1447                                         break;
1448
1449                                 default:
1450                                         /* error */
1451                                         rc = 1;
1452                                         break;
1453                                 }
1454
1455                                 if ( rc ) {
1456                                         continue;
1457                                 }
1458                                 
1459                                 if ( string_expand( &bv, &b->a_group_pat,
1460                                                 e->e_nname.bv_val,
1461                                                 tmp_nmatch, tmp_matchesp ) )
1462                                 {
1463                                         continue;
1464                                 }
1465
1466                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1467                                                 op->o_tmpmemctx ) != LDAP_SUCCESS )
1468                                 {
1469                                         /* did not expand to a valid dn */
1470                                         continue;
1471                                 }
1472
1473                                 bv = ndn;
1474
1475                         } else {
1476                                 bv = b->a_group_pat;
1477                         }
1478
1479                         rc = backend_group( op, e, &bv, &op->o_ndn,
1480                                 b->a_group_oc, b->a_group_at );
1481
1482                         if ( ndn.bv_val ) {
1483                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1484                         }
1485
1486                         if ( rc != 0 ) {
1487                                 continue;
1488                         }
1489                 }
1490
1491                 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1492                         struct berval   bv;
1493                         char            buf[ACL_BUF_SIZE];
1494
1495                         if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1496                                 int             tmp_nmatch;
1497                                 regmatch_t      tmp_matches[2],
1498                                                 *tmp_matchesp = tmp_matches;
1499                                 int             rc = 0;
1500
1501                                 bv.bv_len = sizeof( buf ) - 1;
1502                                 bv.bv_val = buf;
1503
1504                                 rc = 0;
1505
1506                                 switch ( a->acl_dn_style ) {
1507                                 case ACL_STYLE_REGEX:
1508                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1509                                                 tmp_matchesp = matches;
1510                                                 tmp_nmatch = nmatch;
1511                                                 break;
1512                                         }
1513
1514                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1515                                 case ACL_STYLE_BASE:
1516                                         tmp_matches[0].rm_so = 0;
1517                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1518                                         tmp_nmatch = 1;
1519                                         break;
1520
1521                                 case ACL_STYLE_ONE:
1522                                 case ACL_STYLE_SUBTREE:
1523                                 case ACL_STYLE_CHILDREN:
1524                                         tmp_matches[0].rm_so = 0;
1525                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1526                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1527                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1528                                         tmp_nmatch = 2;
1529                                         break;
1530
1531                                 default:
1532                                         /* error */
1533                                         rc = 1;
1534                                         break;
1535                                 }
1536
1537                                 if ( rc ) {
1538                                         continue;
1539                                 }
1540                                 
1541                                 if ( string_expand( &bv, &b->a_set_pat,
1542                                                 e->e_nname.bv_val,
1543                                                 tmp_nmatch, tmp_matchesp ) )
1544                                 {
1545                                         continue;
1546                                 }
1547
1548                         } else {
1549                                 bv = b->a_set_pat;
1550                         }
1551                         
1552                         if ( aci_match_set( &bv, op, e, 0 ) == 0 ) {
1553                                 continue;
1554                         }
1555                 }
1556
1557                 if ( b->a_authz.sai_ssf ) {
1558                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1559                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1560                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1561                                 continue;
1562                         }
1563                 }
1564
1565                 if ( b->a_authz.sai_transport_ssf ) {
1566                         Debug( LDAP_DEBUG_ACL,
1567                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1568                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1569                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1570                                 continue;
1571                         }
1572                 }
1573
1574                 if ( b->a_authz.sai_tls_ssf ) {
1575                         Debug( LDAP_DEBUG_ACL,
1576                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1577                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1578                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1579                                 continue;
1580                         }
1581                 }
1582
1583                 if ( b->a_authz.sai_sasl_ssf ) {
1584                         Debug( LDAP_DEBUG_ACL,
1585                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1586                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1587                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1588                                 continue;
1589                         }
1590                 }
1591
1592 #ifdef SLAP_DYNACL
1593                 if ( b->a_dynacl ) {
1594                         slap_dynacl_t   *da;
1595                         slap_access_t   tgrant, tdeny;
1596
1597                         /* this case works different from the others above.
1598                          * since aci's themselves give permissions, we need
1599                          * to first check b->a_access_mask, the ACL's access level.
1600                          */
1601                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
1602                                 /* no ACIs in the root DSE */
1603                                 continue;
1604                         }
1605
1606                         /* first check if the right being requested
1607                          * is allowed by the ACL clause.
1608                          */
1609                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1610                                 continue;
1611                         }
1612
1613                         /* start out with nothing granted, nothing denied */
1614                         ACL_INIT(tgrant);
1615                         ACL_INIT(tdeny);
1616
1617                         for ( da = b->a_dynacl; da; da = da->da_next ) {
1618                                 slap_access_t   grant, deny;
1619
1620                                 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1621
1622                                 tgrant |= grant;
1623                                 tdeny |= deny;
1624                         }
1625
1626                         /* remove anything that the ACL clause does not allow */
1627                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1628                         tdeny &= ACL_PRIV_MASK;
1629
1630                         /* see if we have anything to contribute */
1631                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1632                                 continue;
1633                         }
1634
1635                         /* this could be improved by changing acl_mask so that it can deal with
1636                          * by clauses that return grant/deny pairs.  Right now, it does either
1637                          * additive or subtractive rights, but not both at the same time.  So,
1638                          * we need to combine the grant/deny pair into a single rights mask in
1639                          * a smart way:  if either grant or deny is "empty", then we use the
1640                          * opposite as is, otherwise we remove any denied rights from the grant
1641                          * rights mask and construct an additive mask.
1642                          */
1643                         if (ACL_IS_INVALID(tdeny)) {
1644                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1645
1646                         } else if (ACL_IS_INVALID(tgrant)) {
1647                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1648
1649                         } else {
1650                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1651                         }
1652
1653                 } else
1654 #else /* !SLAP_DYNACL */
1655
1656 #ifdef SLAPD_ACI_ENABLED
1657                 if ( b->a_aci_at != NULL ) {
1658                         Attribute       *at;
1659                         slap_access_t   grant, deny, tgrant, tdeny;
1660                         struct berval   parent_ndn;
1661                         BerVarray       bvals = NULL;
1662                         int             ret, stop;
1663
1664                         /* this case works different from the others above.
1665                          * since aci's themselves give permissions, we need
1666                          * to first check b->a_access_mask, the ACL's access level.
1667                          */
1668
1669                         if ( BER_BVISEMPTY( &e->e_nname ) ) {
1670                                 /* no ACIs in the root DSE */
1671                                 continue;
1672                         }
1673
1674                         /* first check if the right being requested
1675                          * is allowed by the ACL clause.
1676                          */
1677                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1678                                 continue;
1679                         }
1680                         /* start out with nothing granted, nothing denied */
1681                         ACL_INIT(tgrant);
1682                         ACL_INIT(tdeny);
1683
1684                         /* get the aci attribute */
1685                         at = attr_find( e->e_attrs, b->a_aci_at );
1686                         if ( at != NULL ) {
1687 #if 0
1688                                 /* FIXME: this breaks acl caching;
1689                                  * see also ACL_RECORD_VALUE_STATE below */
1690                                 ACL_RECORD_VALUE_STATE;
1691 #endif
1692                                 /* the aci is an multi-valued attribute.  The
1693                                 * rights are determined by OR'ing the individual
1694                                 * rights given by the acis.
1695                                 */
1696                                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1697                                         if (aci_mask( op,
1698                                                 e, desc, val,
1699                                                 &at->a_nvals[i],
1700                                                 nmatch, matches,
1701                                                 &grant, &deny, SLAP_ACI_SCOPE_ENTRY ) != 0)
1702                                         {
1703                                                 tgrant |= grant;
1704                                                 tdeny |= deny;
1705                                         }
1706                                 }
1707                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1708                                           accessmask2str(tgrant, accessmaskbuf, 1), 
1709                                           accessmask2str(tdeny, accessmaskbuf1, 1), 0);
1710
1711                         }
1712                         /* If the entry level aci didn't contain anything valid for the 
1713                          * current operation, climb up the tree and evaluate the
1714                          * acis with scope set to subtree
1715                          */
1716                         if ( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ) {
1717                                 dnParent( &e->e_nname, &parent_ndn );
1718                                 while ( !BER_BVISEMPTY( &parent_ndn ) ) {
1719                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1720                                         ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1721                                         switch(ret){
1722                                         case LDAP_SUCCESS :
1723                                                 stop = 0;
1724                                                 if (!bvals){
1725                                                         break;
1726                                                 }
1727
1728                                                 for( i = 0; bvals[i].bv_val != NULL; i++){
1729 #if 0
1730                                                         /* FIXME: this breaks acl caching;
1731                                                          * see also ACL_RECORD_VALUE_STATE above */
1732                                                         ACL_RECORD_VALUE_STATE;
1733 #endif
1734                                                         if (aci_mask(op, e, desc, val, &bvals[i],
1735                                                                         nmatch, matches,
1736                                                                         &grant, &deny, SLAP_ACI_SCOPE_CHILDREN ) != 0 )
1737                                                         {
1738                                                                 tgrant |= grant;
1739                                                                 tdeny |= deny;
1740                                                                 /* evaluation stops as soon as either a "deny" or a 
1741                                                                  * "grant" directive matches.
1742                                                                  */
1743                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1744                                                                         stop = 1;
1745                                                                 }
1746                                                         }
1747                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
1748                                                                 accessmask2str(tgrant, accessmaskbuf, 1),
1749                                                                 accessmask2str(tdeny, accessmaskbuf1, 1), 0);
1750                                                 }
1751                                                 break;
1752
1753                                         case LDAP_NO_SUCH_ATTRIBUTE:
1754                                                 /* just go on if the aci-Attribute is not present in
1755                                                  * the current entry 
1756                                                  */
1757                                                 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1758                                                 stop = 0;
1759                                                 break;
1760
1761                                         case LDAP_NO_SUCH_OBJECT:
1762                                                 /* We have reached the base object */
1763                                                 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1764                                                 stop = 1;
1765                                                 break;
1766
1767                                         default:
1768                                                 stop = 1;
1769                                                 break;
1770                                         }
1771                                         if (stop){
1772                                                 break;
1773                                         }
1774                                         dnParent( &parent_ndn, &parent_ndn );
1775                                 }
1776                         }
1777
1778
1779                         /* remove anything that the ACL clause does not allow */
1780                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1781                         tdeny &= ACL_PRIV_MASK;
1782
1783                         /* see if we have anything to contribute */
1784                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1785                                 continue;
1786                         }
1787
1788                         /* this could be improved by changing acl_mask so that it can deal with
1789                          * by clauses that return grant/deny pairs.  Right now, it does either
1790                          * additive or subtractive rights, but not both at the same time.  So,
1791                          * we need to combine the grant/deny pair into a single rights mask in
1792                          * a smart way:  if either grant or deny is "empty", then we use the
1793                          * opposite as is, otherwise we remove any denied rights from the grant
1794                          * rights mask and construct an additive mask.
1795                          */
1796                         if (ACL_IS_INVALID(tdeny)) {
1797                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1798
1799                         } else if (ACL_IS_INVALID(tgrant)) {
1800                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1801
1802                         } else {
1803                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1804                         }
1805
1806                 } else
1807 #endif /* SLAPD_ACI_ENABLED */
1808 #endif /* !SLAP_DYNACL */
1809                 {
1810                         modmask = b->a_access_mask;
1811                 }
1812
1813                 Debug( LDAP_DEBUG_ACL,
1814                         "<= acl_mask: [%d] applying %s (%s)\n",
1815                         i, accessmask2str( modmask, accessmaskbuf, 1 ), 
1816                         b->a_type == ACL_CONTINUE
1817                                 ? "continue"
1818                                 : b->a_type == ACL_BREAK
1819                                         ? "break"
1820                                         : "stop" );
1821                 /* save old mask */
1822                 oldmask = *mask;
1823
1824                 if( ACL_IS_ADDITIVE(modmask) ) {
1825                         /* add privs */
1826                         ACL_PRIV_SET( *mask, modmask );
1827
1828                         /* cleanup */
1829                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1830
1831                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1832                         /* substract privs */
1833                         ACL_PRIV_CLR( *mask, modmask );
1834
1835                         /* cleanup */
1836                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1837
1838                 } else {
1839                         /* assign privs */
1840                         *mask = modmask;
1841                 }
1842
1843                 Debug( LDAP_DEBUG_ACL,
1844                         "<= acl_mask: [%d] mask: %s\n",
1845                         i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1846
1847                 if( b->a_type == ACL_CONTINUE ) {
1848                         continue;
1849
1850                 } else if ( b->a_type == ACL_BREAK ) {
1851                         return ACL_BREAK;
1852
1853                 } else {
1854                         return ACL_STOP;
1855                 }
1856         }
1857
1858         /* implicit "by * none" clause */
1859         ACL_INIT(*mask);
1860
1861         Debug( LDAP_DEBUG_ACL,
1862                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1863                 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1864         return ACL_STOP;
1865 }
1866
1867 /*
1868  * acl_check_modlist - check access control on the given entry to see if
1869  * it allows the given modifications by the user associated with op.
1870  * returns      1       if mods allowed ok
1871  *              0       mods not allowed
1872  */
1873
1874 int
1875 acl_check_modlist(
1876         Operation       *op,
1877         Entry   *e,
1878         Modifications   *mlist
1879 )
1880 {
1881         struct berval *bv;
1882         AccessControlState state = ACL_STATE_INIT;
1883         Backend *be;
1884         int be_null = 0;
1885         int ret = 1; /* default is access allowed */
1886
1887         be = op->o_bd;
1888         if ( be == NULL ) {
1889                 be = LDAP_STAILQ_FIRST(&backendDB);
1890                 be_null = 1;
1891                 op->o_bd = be;
1892         }
1893         assert( be != NULL );
1894
1895         /* short circuit root database access */
1896         if ( be_isroot( op ) ) {
1897                 Debug( LDAP_DEBUG_ACL,
1898                         "<= acl_access_allowed: granted to database root\n",
1899                     0, 0, 0 );
1900                 goto done;
1901         }
1902
1903         /* use backend default access if no backend acls */
1904         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1905                 Debug( LDAP_DEBUG_ACL,
1906                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1907                         access2str( ACL_WRITE ),
1908                         op->o_bd->be_dfltaccess >= ACL_WRITE
1909                                 ? "granted" : "denied",
1910                         op->o_dn.bv_val );
1911                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1912                 goto done;
1913         }
1914
1915         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1916                 /*
1917                  * no-user-modification operational attributes are ignored
1918                  * by ACL_WRITE checking as any found here are not provided
1919                  * by the user
1920                  */
1921                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1922                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1923                                 " modify access granted\n",
1924                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1925                         continue;
1926                 }
1927
1928                 switch ( mlist->sml_op ) {
1929                 case LDAP_MOD_REPLACE:
1930                         /*
1931                          * We must check both permission to delete the whole
1932                          * attribute and permission to add the specific attributes.
1933                          * This prevents abuse from selfwriters.
1934                          */
1935                         if ( ! access_allowed( op, e,
1936                                 mlist->sml_desc, NULL, ACL_WDEL, &state ) )
1937                         {
1938                                 ret = 0;
1939                                 goto done;
1940                         }
1941
1942                         if ( mlist->sml_values == NULL ) break;
1943
1944                         /* fall thru to check value to add */
1945
1946                 case LDAP_MOD_ADD:
1947                         assert( mlist->sml_values != NULL );
1948
1949                         for ( bv = mlist->sml_nvalues
1950                                         ? mlist->sml_nvalues : mlist->sml_values;
1951                                 bv->bv_val != NULL; bv++ )
1952                         {
1953                                 if ( ! access_allowed( op, e,
1954                                         mlist->sml_desc, bv, ACL_WADD, &state ) )
1955                                 {
1956                                         ret = 0;
1957                                         goto done;
1958                                 }
1959                         }
1960                         break;
1961
1962                 case LDAP_MOD_DELETE:
1963                         if ( mlist->sml_values == NULL ) {
1964                                 if ( ! access_allowed( op, e,
1965                                         mlist->sml_desc, NULL, ACL_WDEL, NULL ) )
1966                                 {
1967                                         ret = 0;
1968                                         goto done;
1969                                 }
1970                                 break;
1971                         }
1972                         for ( bv = mlist->sml_nvalues
1973                                         ? mlist->sml_nvalues : mlist->sml_values;
1974                                 bv->bv_val != NULL; bv++ )
1975                         {
1976                                 if ( ! access_allowed( op, e,
1977                                         mlist->sml_desc, bv, ACL_WDEL, &state ) )
1978                                 {
1979                                         ret = 0;
1980                                         goto done;
1981                                 }
1982                         }
1983                         break;
1984
1985                 case SLAP_MOD_SOFTADD:
1986                         /* allow adding attribute via modrdn thru */
1987                         break;
1988
1989                 default:
1990                         assert( 0 );
1991                         /* not reached */
1992                         ret = 0;
1993                         break;
1994                 }
1995         }
1996
1997 done:
1998         if (be_null) op->o_bd = NULL;
1999         return( ret );
2000 }
2001
2002 static int
2003 aci_get_part(
2004         struct berval   *list,
2005         int             ix,
2006         char            sep,
2007         struct berval   *bv )
2008 {
2009         int     len;
2010         char    *p;
2011
2012         if ( bv ) {
2013                 BER_BVZERO( bv );
2014         }
2015         len = list->bv_len;
2016         p = list->bv_val;
2017         while ( len >= 0 && --ix >= 0 ) {
2018                 while ( --len >= 0 && *p++ != sep )
2019                         ;
2020         }
2021         while ( len >= 0 && *p == ' ' ) {
2022                 len--;
2023                 p++;
2024         }
2025         if ( len < 0 ) {
2026                 return -1;
2027         }
2028
2029         if ( !bv ) {
2030                 return 0;
2031         }
2032
2033         bv->bv_val = p;
2034         while ( --len >= 0 && *p != sep ) {
2035                 bv->bv_len++;
2036                 p++;
2037         }
2038         while ( bv->bv_len > 0 && *--p == ' ' ) {
2039                 bv->bv_len--;
2040         }
2041         
2042         return bv->bv_len;
2043 }
2044
2045 typedef struct aci_set_gather_t {
2046         SetCookie               *cookie;
2047         BerVarray               bvals;
2048 } aci_set_gather_t;
2049
2050 static int
2051 aci_set_cb_gather( Operation *op, SlapReply *rs )
2052 {
2053         aci_set_gather_t        *p = (aci_set_gather_t *)op->o_callback->sc_private;
2054         
2055         if ( rs->sr_type == REP_SEARCH ) {
2056                 BerValue        bvals[ 2 ];
2057                 BerVarray       bvalsp = NULL;
2058                 int             j;
2059
2060                 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
2061                         AttributeDescription    *desc = rs->sr_attrs[ j ].an_desc;
2062                         
2063                         if ( desc == slap_schema.si_ad_entryDN ) {
2064                                 bvalsp = bvals;
2065                                 bvals[ 0 ] = rs->sr_entry->e_nname;
2066                                 BER_BVZERO( &bvals[ 1 ] );
2067
2068                         } else {
2069                                 Attribute       *a;
2070
2071                                 a = attr_find( rs->sr_entry->e_attrs, desc );
2072                                 if ( a != NULL ) {
2073                                         int     i;
2074
2075                                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2076                                                 ;
2077
2078                                         bvalsp = a->a_nvals;
2079                                 }
2080                         }
2081                 }
2082
2083                 if ( bvals ) {
2084                         p->bvals = slap_set_join( p->cookie, p->bvals,
2085                                         ( '|' | SLAP_SET_RREF ), bvalsp );
2086                 }
2087
2088         } else {
2089                 assert( rs->sr_type == REP_RESULT );
2090         }
2091
2092         return 0;
2093 }
2094
2095 BerVarray
2096 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2097 {
2098         AciSetCookie            *cp = (AciSetCookie *)cookie;
2099         int                     rc = 0;
2100         LDAPURLDesc             *ludp = NULL;
2101         Operation               op2 = { 0 };
2102         SlapReply               rs = {REP_RESULT};
2103         AttributeName           anlist[ 2 ], *anlistp = NULL;
2104         int                     nattrs = 0;
2105         slap_callback           cb = { NULL, aci_set_cb_gather, NULL, NULL };
2106         aci_set_gather_t        p = { 0 };
2107         const char              *text = NULL;
2108         static struct berval    defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2109
2110         /* this routine needs to return the bervals instead of
2111          * plain strings, since syntax is not known.  It should
2112          * also return the syntax or some "comparison cookie".
2113          */
2114         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2115                 return aci_set_gather2( cookie, name, desc );
2116         }
2117
2118         rc = ldap_url_parse( name->bv_val, &ludp );
2119         if ( rc != LDAP_URL_SUCCESS ) {
2120                 rc = LDAP_PROTOCOL_ERROR;
2121                 goto url_done;
2122         }
2123         
2124         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2125         {
2126                 /* host part must be empty */
2127                 /* extensions parts must be empty */
2128                 rc = LDAP_PROTOCOL_ERROR;
2129                 goto url_done;
2130         }
2131
2132         /* Grab the searchbase and see if an appropriate database can be found */
2133         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2134         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2135                         &op2.o_req_ndn, cp->op->o_tmpmemctx );
2136         BER_BVZERO( &op2.o_req_dn );
2137         if ( rc != LDAP_SUCCESS ) {
2138                 goto url_done;
2139         }
2140
2141         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2142         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2143                 rc = LDAP_NO_SUCH_OBJECT;
2144                 goto url_done;
2145         }
2146
2147         /* Grab the filter */
2148         if ( ludp->lud_filter ) {
2149                 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2150                                 cp->op->o_tmpmemctx );
2151                 
2152         } else {
2153                 op2.ors_filterstr = defaultFilter_bv;
2154         }
2155
2156         op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
2157         if ( op2.ors_filter == NULL ) {
2158                 rc = LDAP_PROTOCOL_ERROR;
2159                 goto url_done;
2160         }
2161
2162         /* Grab the scope */
2163         op2.ors_scope = ludp->lud_scope;
2164
2165         /* Grap the attributes */
2166         if ( ludp->lud_attrs ) {
2167                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2168                         ;
2169
2170                 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2171                                 cp->op->o_tmpmemctx );
2172
2173                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2174                         ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2175                         anlistp[ nattrs ].an_desc = NULL;
2176                         rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2177                                         &anlistp[ nattrs ].an_desc, &text );
2178                         if ( rc != LDAP_SUCCESS ) {
2179                                 goto url_done;
2180                         }
2181                 }
2182
2183         } else {
2184                 anlistp = anlist;
2185         }
2186
2187         anlistp[ nattrs ].an_name = desc->ad_cname;
2188         anlistp[ nattrs ].an_desc = desc;
2189
2190         BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2191         
2192         p.cookie = cookie;
2193         
2194         op2.o_hdr = cp->op->o_hdr;
2195         op2.o_tag = LDAP_REQ_SEARCH;
2196         op2.o_ndn = op2.o_bd->be_rootndn;
2197         op2.o_callback = &cb;
2198         op2.o_time = slap_get_time();
2199         op2.o_do_not_cache = 1;
2200         op2.o_is_auth_check = 0;
2201         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2202         op2.ors_slimit = SLAP_NO_LIMIT;
2203         op2.ors_tlimit = SLAP_NO_LIMIT;
2204         op2.ors_attrs = anlistp;
2205         op2.ors_attrsonly = 0;
2206         op2.o_private = cp->op->o_private;
2207
2208         cb.sc_private = &p;
2209
2210         rc = op2.o_bd->be_search( &op2, &rs );
2211         if ( rc != 0 ) {
2212                 goto url_done;
2213         }
2214
2215 url_done:;
2216         if ( op2.ors_filter ) {
2217                 filter_free_x( cp->op, op2.ors_filter );
2218         }
2219         if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2220                 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2221         }
2222         if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2223                 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2224         }
2225         if ( ludp ) {
2226                 ldap_free_urldesc( ludp );
2227         }
2228         if ( anlistp && anlistp != anlist ) {
2229                 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2230         }
2231
2232         return p.bvals;
2233 }
2234
2235 BerVarray
2236 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2237 {
2238         AciSetCookie    *cp = (AciSetCookie *)cookie;
2239         BerVarray       bvals = NULL;
2240         struct berval   ndn;
2241         int             rc = 0;
2242
2243         /* this routine needs to return the bervals instead of
2244          * plain strings, since syntax is not known.  It should
2245          * also return the syntax or some "comparison cookie".
2246          */
2247         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2248         if ( rc == LDAP_SUCCESS ) {
2249                 if ( desc == slap_schema.si_ad_entryDN ) {
2250                         bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2251                                         cp->op->o_tmpmemctx );
2252                         bvals[ 0 ] = ndn;
2253                         BER_BVZERO( &bvals[ 1 ] );
2254                         BER_BVZERO( &ndn );
2255
2256                 } else {
2257                         backend_attribute( cp->op,
2258                                 cp->e, &ndn, desc, &bvals, ACL_NONE );
2259                 }
2260
2261                 if ( !BER_BVISNULL( &ndn ) ) {
2262                         slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2263                 }
2264         }
2265
2266         return bvals;
2267 }
2268
2269 static int
2270 aci_match_set (
2271         struct berval *subj,
2272         Operation *op,
2273         Entry *e,
2274         int setref
2275 )
2276 {
2277         struct berval   set = BER_BVNULL;
2278         int             rc = 0;
2279         AciSetCookie    cookie;
2280
2281         if ( setref == 0 ) {
2282                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2283
2284         } else {
2285                 struct berval           subjdn, ndn = BER_BVNULL;
2286                 struct berval           setat;
2287                 BerVarray               bvals;
2288                 const char              *text;
2289                 AttributeDescription    *desc = NULL;
2290
2291                 /* format of string is "entry/setAttrName" */
2292                 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2293                         return 0;
2294                 }
2295
2296                 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2297                         setat = aci_bv_set_attr;
2298                 }
2299
2300                 /*
2301                  * NOTE: dnNormalize honors the ber_len field
2302                  * as the length of the dn to be normalized
2303                  */
2304                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2305                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2306                         {
2307                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2308                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2309                                         int     i;
2310
2311                                         set = bvals[0];
2312                                         BER_BVZERO( &bvals[0] );
2313                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2314                                                 /* count */ ;
2315                                         bvals[0].bv_val = bvals[i-1].bv_val;
2316                                         BER_BVZERO( &bvals[i-1] );
2317                                 }
2318                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2319                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2320                         }
2321                 }
2322         }
2323
2324         if ( !BER_BVISNULL( &set ) ) {
2325                 cookie.op = op;
2326                 cookie.e = e;
2327                 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2328                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2329                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2330         }
2331
2332         return(rc);
2333 }
2334
2335 #ifdef SLAPD_ACI_ENABLED
2336 static int
2337 aci_list_map_rights(
2338         struct berval *list )
2339 {
2340         struct berval bv;
2341         slap_access_t mask;
2342         int i;
2343
2344         ACL_INIT(mask);
2345         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2346                 if (bv.bv_len <= 0)
2347                         continue;
2348                 switch (*bv.bv_val) {
2349                 case 'c':
2350                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2351                         break;
2352                 case 's':
2353                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2354                          * the right 's' to mean "set", but in the examples states
2355                          * that the right 's' means "search".  The latter definition
2356                          * is used here.
2357                          */
2358                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2359                         break;
2360                 case 'r':
2361                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
2362                         break;
2363                 case 'w':
2364                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2365                         break;
2366                 case 'x':
2367                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
2368                          * define any equivalent to the AUTH right, so I've just used
2369                          * 'x' for now.
2370                          */
2371                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2372                         break;
2373                 default:
2374                         break;
2375                 }
2376
2377         }
2378         return(mask);
2379 }
2380
2381 static int
2382 aci_list_has_attr(
2383         struct berval *list,
2384         const struct berval *attr,
2385         struct berval *val )
2386 {
2387         struct berval bv, left, right;
2388         int i;
2389
2390         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2391                 if (aci_get_part(&bv, 0, '=', &left) < 0
2392                         || aci_get_part(&bv, 1, '=', &right) < 0)
2393                 {
2394                         if (ber_bvstrcasecmp(attr, &bv) == 0)
2395                                 return(1);
2396                 } else if (val == NULL) {
2397                         if (ber_bvstrcasecmp(attr, &left) == 0)
2398                                 return(1);
2399                 } else {
2400                         if (ber_bvstrcasecmp(attr, &left) == 0) {
2401                                 /* this is experimental code that implements a
2402                                  * simple (prefix) match of the attribute value.
2403                                  * the ACI draft does not provide for aci's that
2404                                  * apply to specific values, but it would be
2405                                  * nice to have.  If the <attr> part of an aci's
2406                                  * rights list is of the form <attr>=<value>,
2407                                  * that means the aci applies only to attrs with
2408                                  * the given value.  Furthermore, if the attr is
2409                                  * of the form <attr>=<value>*, then <value> is
2410                                  * treated as a prefix, and the aci applies to 
2411                                  * any value with that prefix.
2412                                  *
2413                                  * Ideally, this would allow r.e. matches.
2414                                  */
2415                                 if (aci_get_part(&right, 0, '*', &left) < 0
2416                                         || right.bv_len <= left.bv_len)
2417                                 {
2418                                         if (ber_bvstrcasecmp(val, &right) == 0)
2419                                                 return(1);
2420                                 } else if (val->bv_len >= left.bv_len) {
2421                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2422                                                 return(1);
2423                                 }
2424                         }
2425                 }
2426         }
2427         return(0);
2428 }
2429
2430 static slap_access_t
2431 aci_list_get_attr_rights(
2432         struct berval *list,
2433         const struct berval *attr,
2434         struct berval *val )
2435 {
2436     struct berval bv;
2437     slap_access_t mask;
2438     int i;
2439
2440         /* loop through each rights/attr pair, skip first part (action) */
2441         ACL_INIT(mask);
2442         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2443                 if (aci_list_has_attr(&bv, attr, val) == 0)
2444                         continue;
2445                 if (aci_get_part(list, i, ';', &bv) < 0)
2446                         continue;
2447                 mask |= aci_list_map_rights(&bv);
2448         }
2449         return(mask);
2450 }
2451
2452 static int
2453 aci_list_get_rights(
2454         struct berval *list,
2455         const struct berval *attr,
2456         struct berval *val,
2457         slap_access_t *grant,
2458         slap_access_t *deny )
2459 {
2460     struct berval perm, actn;
2461     slap_access_t *mask;
2462     int i, found;
2463
2464         if (attr == NULL || attr->bv_len == 0 
2465                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2466                 attr = &aci_bv_br_entry;
2467         }
2468
2469         found = 0;
2470         ACL_INIT(*grant);
2471         ACL_INIT(*deny);
2472         /* loop through each permissions clause */
2473         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2474                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2475                         continue;
2476                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2477                         mask = grant;
2478                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2479                         mask = deny;
2480                 } else {
2481                         continue;
2482                 }
2483
2484                 found = 1;
2485                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2486                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2487         }
2488         return(found);
2489 }
2490
2491 static int
2492 aci_group_member (
2493         struct berval   *subj,
2494         struct berval   *defgrpoc,
2495         struct berval   *defgrpat,
2496         Operation       *op,
2497         Entry           *e,
2498         int             nmatch,
2499         regmatch_t      *matches
2500 )
2501 {
2502         struct berval subjdn;
2503         struct berval grpoc;
2504         struct berval grpat;
2505         ObjectClass *grp_oc = NULL;
2506         AttributeDescription *grp_ad = NULL;
2507         const char *text;
2508         int rc;
2509
2510         /* format of string is "group/objectClassValue/groupAttrName" */
2511         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2512                 return(0);
2513         }
2514
2515         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2516                 grpoc = *defgrpoc;
2517         }
2518
2519         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2520                 grpat = *defgrpat;
2521         }
2522
2523         rc = slap_bv2ad( &grpat, &grp_ad, &text );
2524         if( rc != LDAP_SUCCESS ) {
2525                 rc = 0;
2526                 goto done;
2527         }
2528         rc = 0;
2529
2530         grp_oc = oc_bvfind( &grpoc );
2531
2532         if (grp_oc != NULL && grp_ad != NULL ) {
2533                 char buf[ACL_BUF_SIZE];
2534                 struct berval bv, ndn;
2535                 bv.bv_len = sizeof( buf ) - 1;
2536                 bv.bv_val = (char *)&buf;
2537                 if ( string_expand(&bv, &subjdn,
2538                                 e->e_ndn, nmatch, matches) )
2539                 {
2540                         rc = LDAP_OTHER;
2541                         goto done;
2542                 }
2543                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2544                         rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2545                                 grp_oc, grp_ad ) == 0 );
2546                         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2547                 }
2548         }
2549
2550 done:
2551         return(rc);
2552 }
2553
2554 static int
2555 aci_mask(
2556         Operation               *op,
2557         Entry                   *e,
2558         AttributeDescription    *desc,
2559         struct berval           *val,
2560         struct berval           *aci,
2561         int                     nmatch,
2562         regmatch_t              *matches,
2563         slap_access_t           *grant,
2564         slap_access_t           *deny,
2565         slap_aci_scope_t        asserted_scope
2566 )
2567 {
2568         struct berval           bv, scope, perms, type, sdn;
2569         int                     rc;
2570                 
2571
2572         assert( !BER_BVISNULL( &desc->ad_cname ) );
2573
2574         /* parse an aci of the form:
2575                 oid # scope # action;rights;attr;rights;attr 
2576                         $ action;rights;attr;rights;attr # type # subject
2577
2578            [NOTE: the following comment is very outdated,
2579            as the draft version it refers to (Ando, 2004-11-20)].
2580
2581            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2582            a full description of the format for this attribute.
2583            Differences: "this" in the draft is "self" here, and
2584            "self" and "public" is in the position of type.
2585
2586            <scope> = {entry|children|subtree}
2587            <type> = {public|users|access-id|subtree|onelevel|children|
2588                      self|dnattr|group|role|set|set-ref}
2589
2590            This routine now supports scope={ENTRY,CHILDREN}
2591            with the semantics:
2592              - ENTRY applies to "entry" and "subtree";
2593              - CHILDREN aplies to "children" and "subtree"
2594          */
2595
2596         /* check that the aci has all 5 components */
2597         if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2598                 return 0;
2599         }
2600
2601         /* check that the aci family is supported */
2602         if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2603                 return 0;
2604         }
2605
2606         /* check that the scope matches */
2607         if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2608                 return 0;
2609         }
2610
2611         /* note: scope can be either ENTRY or CHILDREN;
2612          * they respectively match "entry" and "children" in bv
2613          * both match "subtree" */
2614         switch ( asserted_scope ) {
2615         case SLAP_ACI_SCOPE_ENTRY:
2616                 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2617                                 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2618                 {
2619                         return 0;
2620                 }
2621                 break;
2622
2623         case SLAP_ACI_SCOPE_CHILDREN:
2624                 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2625                                 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2626                 {
2627                         return 0;
2628                 }
2629                 break;
2630
2631         default:
2632                 return 0;
2633         }
2634
2635         /* get the list of permissions clauses, bail if empty */
2636         if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2637                 return 0;
2638         }
2639
2640         /* check if any permissions allow desired access */
2641         if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2642                 return 0;
2643         }
2644
2645         /* see if we have a DN match */
2646         if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
2647                 return 0;
2648         }
2649
2650         /* see if we have a public (i.e. anonymous) access */
2651         if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
2652                 return 1;
2653         }
2654         
2655         /* otherwise require an identity */
2656         if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
2657                 return 0;
2658         }
2659
2660         /* see if we have a users access */
2661         if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
2662                 return 1;
2663         }
2664         
2665         /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
2666          * just grab all the berval up to its end (ITS#3303).
2667          * NOTE: the problem could be solved by providing the DN with
2668          * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would 
2669          * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
2670 #if 0
2671         if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
2672                 return 0;
2673         }
2674 #endif
2675         sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
2676         sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
2677
2678         if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
2679                 struct berval ndn;
2680                 
2681                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2682                 if ( rc != LDAP_SUCCESS ) {
2683                         return 0;
2684                 }
2685
2686                 if ( dn_match( &op->o_ndn, &ndn ) ) {
2687                         rc = 1;
2688                 }
2689                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2690
2691                 return rc;
2692
2693         } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
2694                 struct berval ndn;
2695                 
2696                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2697                 if ( rc != LDAP_SUCCESS ) {
2698                         return 0;
2699                 }
2700
2701                 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
2702                         rc = 1;
2703                 }
2704                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2705
2706                 return rc;
2707
2708         } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
2709                 struct berval ndn, pndn;
2710                 
2711                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2712                 if ( rc != LDAP_SUCCESS ) {
2713                         return 0;
2714                 }
2715
2716                 dnParent( &ndn, &pndn );
2717
2718                 if ( dn_match( &op->o_ndn, &pndn ) ) {
2719                         rc = 1;
2720                 }
2721                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2722
2723                 return rc;
2724
2725         } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
2726                 struct berval ndn;
2727                 
2728                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2729                 if ( rc != LDAP_SUCCESS ) {
2730                         return 0;
2731                 }
2732
2733                 if ( !dn_match( &op->o_ndn, &ndn )
2734                                 && dnIsSuffix( &op->o_ndn, &ndn ) )
2735                 {
2736                         rc = 1;
2737                 }
2738                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2739
2740                 return rc;
2741
2742         } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
2743                 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
2744                         return 1;
2745                 }
2746
2747         } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
2748                 Attribute               *at;
2749                 AttributeDescription    *ad = NULL;
2750                 const char              *text;
2751
2752                 rc = slap_bv2ad( &sdn, &ad, &text );
2753
2754                 if( rc != LDAP_SUCCESS ) {
2755                         return 0;
2756                 }
2757
2758                 rc = 0;
2759
2760                 for ( at = attrs_find( e->e_attrs, ad );
2761                                 at != NULL;
2762                                 at = attrs_find( at->a_next, ad ) )
2763                 {
2764                         if ( value_find_ex( ad,
2765                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2766                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2767                                 at->a_nvals,
2768                                 &op->o_ndn, op->o_tmpmemctx ) == 0 )
2769                         {
2770                                 rc = 1;
2771                                 break;
2772                         }
2773                 }
2774
2775                 return rc;
2776
2777         } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
2778                 if ( aci_group_member( &sdn, &aci_bv_group_class,
2779                                 &aci_bv_group_attr, op, e, nmatch, matches ) )
2780                 {
2781                         return 1;
2782                 }
2783
2784         } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
2785                 if ( aci_group_member( &sdn, &aci_bv_role_class,
2786                                 &aci_bv_role_attr, op, e, nmatch, matches ) )
2787                 {
2788                         return 1;
2789                 }
2790
2791         } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
2792                 if ( aci_match_set( &sdn, op, e, 0 ) ) {
2793                         return 1;
2794                 }
2795
2796         } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
2797                 if ( aci_match_set( &sdn, op, e, 1 ) ) {
2798                         return 1;
2799                 }
2800         }
2801
2802         return 0;
2803 }
2804
2805 #ifdef SLAP_DYNACL
2806 /*
2807  * FIXME: there is a silly dependence that makes it difficult
2808  * to move ACIs in a run-time loadable module under the "dynacl" 
2809  * umbrella, because sets share some helpers with ACIs.
2810  */
2811 static int
2812 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
2813 {
2814         AttributeDescription    *ad = NULL;
2815         const char              *text = NULL;
2816
2817         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
2818                 fprintf( stderr, "%s: line %d: "
2819                         "inappropriate style \"%s\" in \"aci\" by clause\n",
2820                         fname, lineno, sty );
2821                 return -1;
2822         }
2823
2824         if ( right != NULL && *right != '\0' ) {
2825                 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
2826                         fprintf( stderr,
2827                                 "%s: line %d: aci \"%s\": %s\n",
2828                                 fname, lineno, right, text );
2829                         return -1;
2830                 }
2831
2832         } else {
2833                 ad = slap_schema.si_ad_aci;
2834         }
2835
2836         if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
2837                 fprintf( stderr, "%s: line %d: "
2838                         "aci \"%s\": inappropriate syntax: %s\n",
2839                         fname, lineno, right,
2840                         ad->ad_type->sat_syntax_oid );
2841                 return -1;
2842         }
2843
2844         *privp = (void *)ad;
2845
2846         return 0;
2847 }
2848
2849 static int
2850 dynacl_aci_unparse( void *priv, struct berval *bv )
2851 {
2852         AttributeDescription    *ad = ( AttributeDescription * )priv;
2853         char *ptr;
2854
2855         assert( ad );
2856
2857         bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
2858         ptr = lutil_strcopy( bv->bv_val, " aci=" );
2859         ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
2860         bv->bv_len = ptr - bv->bv_val;
2861
2862         return 0;
2863 }
2864
2865
2866 static int
2867 dynacl_aci_mask(
2868                 void                    *priv,
2869                 Operation               *op,
2870                 Entry                   *e,
2871                 AttributeDescription    *desc,
2872                 struct berval           *val,
2873                 int                     nmatch,
2874                 regmatch_t              *matches,
2875                 slap_access_t           *grantp,
2876                 slap_access_t           *denyp )
2877 {
2878         AttributeDescription    *ad = ( AttributeDescription * )priv;
2879         Attribute               *at;
2880         slap_access_t           tgrant, tdeny, grant, deny;
2881 #ifdef LDAP_DEBUG
2882         char                    accessmaskbuf[ACCESSMASK_MAXLEN];
2883         char                    accessmaskbuf1[ACCESSMASK_MAXLEN];
2884 #endif /* LDAP_DEBUG */
2885
2886         /* start out with nothing granted, nothing denied */
2887         ACL_INIT(tgrant);
2888         ACL_INIT(tdeny);
2889
2890         /* get the aci attribute */
2891         at = attr_find( e->e_attrs, ad );
2892         if ( at != NULL ) {
2893                 int             i;
2894
2895                 /* the aci is an multi-valued attribute.  The
2896                  * rights are determined by OR'ing the individual
2897                  * rights given by the acis.
2898                  */
2899                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2900                         if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
2901                                         nmatch, matches, &grant, &deny,
2902                                         SLAP_ACI_SCOPE_ENTRY ) != 0 )
2903                         {
2904                                 tgrant |= grant;
2905                                 tdeny |= deny;
2906                         }
2907                 }
2908                 
2909                 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2910                           accessmask2str( tgrant, accessmaskbuf, 1 ), 
2911                           accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
2912         }
2913
2914         /* If the entry level aci didn't contain anything valid for the 
2915          * current operation, climb up the tree and evaluate the
2916          * acis with scope set to subtree
2917          */
2918         if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
2919                 struct berval   parent_ndn;
2920
2921 #if 1
2922                 /* to solve the chicken'n'egg problem of accessing
2923                  * the OpenLDAPaci attribute, the direct access
2924                  * to the entry's attribute is unchecked; however,
2925                  * further accesses to OpenLDAPaci values in the 
2926                  * ancestors occur through backend_attribute(), i.e.
2927                  * with the identity of the operation, requiring
2928                  * further access checking.  For uniformity, this
2929                  * makes further requests occur as the rootdn, if
2930                  * any, i.e. searching for the OpenLDAPaci attribute
2931                  * is considered an internal search.  If this is not
2932                  * acceptable, then the same check needs be performed
2933                  * when accessing the entry's attribute. */
2934                 Operation       op2 = *op;
2935
2936                 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
2937                         op2.o_dn = op->o_bd->be_rootdn;
2938                         op2.o_ndn = op->o_bd->be_rootndn;
2939                 }
2940 #endif
2941
2942                 dnParent( &e->e_nname, &parent_ndn );
2943                 while ( !BER_BVISEMPTY( &parent_ndn ) ){
2944                         int             i;
2945                         BerVarray       bvals = NULL;
2946                         int             ret, stop;
2947
2948                         Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
2949                         ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
2950
2951                         switch ( ret ) {
2952                         case LDAP_SUCCESS :
2953                                 stop = 0;
2954                                 if ( !bvals ) {
2955                                         break;
2956                                 }
2957
2958                                 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
2959                                         if ( aci_mask( op, e, desc, val,
2960                                                         &bvals[i],
2961                                                         nmatch, matches,
2962                                                         &grant, &deny,
2963                                                         SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2964                                         {
2965                                                 tgrant |= grant;
2966                                                 tdeny |= deny;
2967                                                 /* evaluation stops as soon as either a "deny" or a 
2968                                                  * "grant" directive matches.
2969                                                  */
2970                                                 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
2971                                                         stop = 1;
2972                                                 }
2973                                         }
2974                                         Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
2975                                                 accessmask2str( tgrant, accessmaskbuf, 1 ),
2976                                                 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
2977                                 }
2978                                 break;
2979
2980                         case LDAP_NO_SUCH_ATTRIBUTE:
2981                                 /* just go on if the aci-Attribute is not present in
2982                                  * the current entry 
2983                                  */
2984                                 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
2985                                 stop = 0;
2986                                 break;
2987
2988                         case LDAP_NO_SUCH_OBJECT:
2989                                 /* We have reached the base object */
2990                                 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
2991                                 stop = 1;
2992                                 break;
2993
2994                         default:
2995                                 stop = 1;
2996                                 break;
2997                         }
2998
2999                         if ( stop ) {
3000                                 break;
3001                         }
3002                         dnParent( &parent_ndn, &parent_ndn );
3003                 }
3004         }
3005
3006         *grantp = tgrant;
3007         *denyp = tdeny;
3008
3009         return 0;
3010 }
3011
3012 /* need to register this at some point */
3013 static slap_dynacl_t    dynacl_aci = {
3014         "aci",
3015         dynacl_aci_parse,
3016         dynacl_aci_unparse,
3017         dynacl_aci_mask,
3018         NULL,
3019         NULL,
3020         NULL
3021 };
3022
3023 #endif /* SLAP_DYNACL */
3024
3025 #endif  /* SLAPD_ACI_ENABLED */
3026
3027 #ifdef SLAP_DYNACL
3028
3029 /*
3030  * dynamic ACL infrastructure
3031  */
3032 static slap_dynacl_t    *da_list = NULL;
3033
3034 int
3035 slap_dynacl_register( slap_dynacl_t *da )
3036 {
3037         slap_dynacl_t   *tmp;
3038
3039         for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
3040                 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
3041                         break;
3042                 }
3043         }
3044
3045         if ( tmp != NULL ) {
3046                 return -1;
3047         }
3048         
3049         if ( da->da_mask == NULL ) {
3050                 return -1;
3051         }
3052         
3053         da->da_private = NULL;
3054         da->da_next = da_list;
3055         da_list = da;
3056
3057         return 0;
3058 }
3059
3060 static slap_dynacl_t *
3061 slap_dynacl_next( slap_dynacl_t *da )
3062 {
3063         if ( da ) {
3064                 return da->da_next;
3065         }
3066         return da_list;
3067 }
3068
3069 slap_dynacl_t *
3070 slap_dynacl_get( const char *name )
3071 {
3072         slap_dynacl_t   *da;
3073
3074         for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
3075                 if ( strcasecmp( da->da_name, name ) == 0 ) {
3076                         break;
3077                 }
3078         }
3079
3080         return da;
3081 }
3082 #endif /* SLAP_DYNACL */
3083
3084 int
3085 acl_init( void )
3086 {
3087         int             i, rc;
3088 #ifdef SLAP_DYNACL
3089         slap_dynacl_t   *known_dynacl[] = {
3090 #ifdef SLAPD_ACI_ENABLED
3091                 &dynacl_aci,
3092 #endif  /* SLAPD_ACI_ENABLED */
3093                 NULL
3094         };
3095
3096         for ( i = 0; known_dynacl[ i ]; i++ ) {
3097                 rc = slap_dynacl_register( known_dynacl[ i ] ); 
3098                 if ( rc ) {
3099                         return rc;
3100                 }
3101         }
3102 #endif /* SLAP_DYNACL */
3103
3104         return 0;
3105 }
3106
3107 static int
3108 string_expand(
3109         struct berval   *bv,
3110         struct berval   *pat,
3111         char            *match,
3112         int             nmatch,
3113         regmatch_t      *matches)
3114 {
3115         ber_len_t       size;
3116         char   *sp;
3117         char   *dp;
3118         int     flag;
3119
3120         size = 0;
3121         bv->bv_val[0] = '\0';
3122         bv->bv_len--; /* leave space for lone $ */
3123
3124         flag = 0;
3125         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
3126                 sp < pat->bv_val + pat->bv_len ; sp++ )
3127         {
3128                 /* did we previously see a $ */
3129                 if ( flag ) {
3130                         if ( flag == 1 && *sp == '$' ) {
3131                                 *dp++ = '$';
3132                                 size++;
3133                                 flag = 0;
3134
3135                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
3136                                 flag = 2;
3137
3138                         } else if ( *sp >= '0' && *sp <= '9' ) {
3139                                 int     n;
3140                                 int     i;
3141                                 int     l;
3142
3143                                 n = *sp - '0';
3144
3145                                 if ( flag == 2 ) {
3146                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
3147                                                 if ( *sp >= '0' && *sp <= '9' ) {
3148                                                         n = 10*n + ( *sp - '0' );
3149                                                 }
3150                                         }
3151
3152                                         if ( *sp != /*'{'*/ '}' ) {
3153                                                 /* FIXME: error */
3154                                                 return 1;
3155                                         }
3156                                 }
3157
3158                                 if ( n >= nmatch ) {
3159                                         /* FIXME: error */
3160                                         return 1;
3161                                 }
3162                                 
3163                                 *dp = '\0';
3164                                 i = matches[n].rm_so;
3165                                 l = matches[n].rm_eo; 
3166                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
3167                                         *dp++ = match[i];
3168                                 }
3169                                 *dp = '\0';
3170
3171                                 flag = 0;
3172                         }
3173                 } else {
3174                         if (*sp == '$') {
3175                                 flag = 1;
3176                         } else {
3177                                 *dp++ = *sp;
3178                                 size++;
3179                         }
3180                 }
3181         }
3182
3183         if ( flag ) {
3184                 /* must have ended with a single $ */
3185                 *dp++ = '$';
3186                 size++;
3187         }
3188
3189         *dp = '\0';
3190         bv->bv_len = size;
3191
3192         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3193         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3194
3195         return 0;
3196 }
3197
3198 static int
3199 regex_matches(
3200         struct berval   *pat,           /* pattern to expand and match against */
3201         char            *str,           /* string to match against pattern */
3202         char            *buf,           /* buffer with $N expansion variables */
3203         int             nmatch, /* size of the matches array */
3204         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
3205 )
3206 {
3207         regex_t re;
3208         char newbuf[ACL_BUF_SIZE];
3209         struct berval bv;
3210         int     rc;
3211
3212         bv.bv_len = sizeof( newbuf ) - 1;
3213         bv.bv_val = newbuf;
3214
3215         if (str == NULL) {
3216                 str = "";
3217         };
3218
3219         string_expand( &bv, pat, buf, nmatch, matches );
3220         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3221         if ( rc ) {
3222                 char error[ACL_BUF_SIZE];
3223                 regerror( rc, &re, error, sizeof( error ) );
3224
3225                 Debug( LDAP_DEBUG_TRACE,
3226                     "compile( \"%s\", \"%s\") failed %s\n",
3227                         pat->bv_val, str, error );
3228                 return( 0 );
3229         }
3230
3231         rc = regexec( &re, str, 0, NULL, 0 );
3232         regfree( &re );
3233
3234         Debug( LDAP_DEBUG_TRACE,
3235             "=> regex_matches: string:   %s\n", str, 0, 0 );
3236         Debug( LDAP_DEBUG_TRACE,
3237             "=> regex_matches: rc: %d %s\n",
3238                 rc, !rc ? "matches" : "no matches", 0 );
3239         return( !rc );
3240 }
3241