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