]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
0150d1c7e9ad67b1b8f4e691054fddf22c0d1341
[openldap] / servers / slapd / acl.c
1 /* acl.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/regex.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
34
35 #include "slap.h"
36 #include "sets.h"
37 #include "lber_pvt.h"
38
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_MANAGE;
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
1745                                 ? "granted" : "denied",
1746                         op->o_dn.bv_val );
1747                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1748                 goto done;
1749         }
1750
1751         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1752                 /*
1753                  * no-user-modification operational attributes are ignored
1754                  * by ACL_WRITE checking as any found here are not provided
1755                  * by the user
1756                  */
1757                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1758                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1759                                 " modify access granted\n",
1760                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1761                         continue;
1762                 }
1763
1764                 switch ( mlist->sml_op ) {
1765                 case LDAP_MOD_REPLACE:
1766                         /*
1767                          * We must check both permission to delete the whole
1768                          * attribute and permission to add the specific attributes.
1769                          * This prevents abuse from selfwriters.
1770                          */
1771                         if ( ! access_allowed( op, e,
1772                                 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1773                         {
1774                                 ret = 0;
1775                                 goto done;
1776                         }
1777
1778                         if ( mlist->sml_values == NULL ) break;
1779
1780                         /* fall thru to check value to add */
1781
1782                 case LDAP_MOD_ADD:
1783                         assert( mlist->sml_values != NULL );
1784
1785                         for ( bv = mlist->sml_nvalues
1786                                         ? mlist->sml_nvalues : mlist->sml_values;
1787                                 bv->bv_val != NULL; bv++ )
1788                         {
1789                                 if ( ! access_allowed( op, e,
1790                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1791                                 {
1792                                         ret = 0;
1793                                         goto done;
1794                                 }
1795                         }
1796                         break;
1797
1798                 case LDAP_MOD_DELETE:
1799                         if ( mlist->sml_values == NULL ) {
1800                                 if ( ! access_allowed( op, e,
1801                                         mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1802                                 {
1803                                         ret = 0;
1804                                         goto done;
1805                                 }
1806                                 break;
1807                         }
1808                         for ( bv = mlist->sml_nvalues
1809                                         ? mlist->sml_nvalues : mlist->sml_values;
1810                                 bv->bv_val != NULL; bv++ )
1811                         {
1812                                 if ( ! access_allowed( op, e,
1813                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1814                                 {
1815                                         ret = 0;
1816                                         goto done;
1817                                 }
1818                         }
1819                         break;
1820
1821                 case SLAP_MOD_SOFTADD:
1822                         /* allow adding attribute via modrdn thru */
1823                         break;
1824
1825                 default:
1826                         assert( 0 );
1827                         /* not reached */
1828                         ret = 0;
1829                         break;
1830                 }
1831         }
1832
1833 done:
1834         if (be_null) op->o_bd = NULL;
1835         return( ret );
1836 }
1837
1838 static int
1839 aci_get_part(
1840         struct berval   *list,
1841         int             ix,
1842         char            sep,
1843         struct berval   *bv )
1844 {
1845         int     len;
1846         char    *p;
1847
1848         if ( bv ) {
1849                 BER_BVZERO( bv );
1850         }
1851         len = list->bv_len;
1852         p = list->bv_val;
1853         while ( len >= 0 && --ix >= 0 ) {
1854                 while ( --len >= 0 && *p++ != sep )
1855                         ;
1856         }
1857         while ( len >= 0 && *p == ' ' ) {
1858                 len--;
1859                 p++;
1860         }
1861         if ( len < 0 ) {
1862                 return -1;
1863         }
1864
1865         if ( !bv ) {
1866                 return 0;
1867         }
1868
1869         bv->bv_val = p;
1870         while ( --len >= 0 && *p != sep ) {
1871                 bv->bv_len++;
1872                 p++;
1873         }
1874         while ( bv->bv_len > 0 && *--p == ' ' ) {
1875                 bv->bv_len--;
1876         }
1877         
1878         return bv->bv_len;
1879 }
1880
1881 typedef struct aci_set_gather_t {
1882         SetCookie               *cookie;
1883         BerVarray               bvals;
1884 } aci_set_gather_t;
1885
1886 static int
1887 aci_set_cb_gather( Operation *op, SlapReply *rs )
1888 {
1889         aci_set_gather_t        *p = (aci_set_gather_t *)op->o_callback->sc_private;
1890         
1891         if ( rs->sr_type == REP_SEARCH ) {
1892                 BerValue        bvals[ 2 ];
1893                 BerVarray       bvalsp = NULL;
1894                 int             j;
1895
1896                 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
1897                         AttributeDescription    *desc = rs->sr_attrs[ j ].an_desc;
1898                         
1899                         if ( desc == slap_schema.si_ad_entryDN ) {
1900                                 bvalsp = bvals;
1901                                 bvals[ 0 ] = rs->sr_entry->e_nname;
1902                                 BER_BVZERO( &bvals[ 1 ] );
1903
1904                         } else {
1905                                 Attribute       *a;
1906
1907                                 a = attr_find( rs->sr_entry->e_attrs, desc );
1908                                 if ( a != NULL ) {
1909                                         int     i;
1910
1911                                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
1912                                                 ;
1913
1914                                         bvalsp = a->a_nvals;
1915                                 }
1916                         }
1917                 }
1918
1919                 if ( bvals ) {
1920                         p->bvals = slap_set_join( p->cookie, p->bvals,
1921                                         ( '|' | SLAP_SET_RREF ), bvalsp );
1922                 }
1923
1924         } else {
1925                 assert( rs->sr_type == REP_RESULT );
1926         }
1927
1928         return 0;
1929 }
1930
1931 BerVarray
1932 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
1933 {
1934         AciSetCookie            *cp = (AciSetCookie *)cookie;
1935         int                     rc = 0;
1936         LDAPURLDesc             *ludp = NULL;
1937         Operation               op2 = { 0 };
1938         SlapReply               rs = {REP_RESULT};
1939         AttributeName           anlist[ 2 ], *anlistp = NULL;
1940         int                     nattrs = 0;
1941         slap_callback           cb = { NULL, aci_set_cb_gather, NULL, NULL };
1942         aci_set_gather_t        p = { 0 };
1943         const char              *text = NULL;
1944         static struct berval    defaultFilter_bv = BER_BVC( "(objectClass=*)" );
1945
1946         /* this routine needs to return the bervals instead of
1947          * plain strings, since syntax is not known.  It should
1948          * also return the syntax or some "comparison cookie".
1949          */
1950         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
1951                 return aci_set_gather2( cookie, name, desc );
1952         }
1953
1954         rc = ldap_url_parse( name->bv_val, &ludp );
1955         if ( rc != LDAP_URL_SUCCESS ) {
1956                 rc = LDAP_PROTOCOL_ERROR;
1957                 goto url_done;
1958         }
1959         
1960         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
1961         {
1962                 /* host part must be empty */
1963                 /* extensions parts must be empty */
1964                 rc = LDAP_PROTOCOL_ERROR;
1965                 goto url_done;
1966         }
1967
1968         /* Grab the searchbase and see if an appropriate database can be found */
1969         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
1970         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
1971                         &op2.o_req_ndn, cp->op->o_tmpmemctx );
1972         BER_BVZERO( &op2.o_req_dn );
1973         if ( rc != LDAP_SUCCESS ) {
1974                 goto url_done;
1975         }
1976
1977         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
1978         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
1979                 rc = LDAP_NO_SUCH_OBJECT;
1980                 goto url_done;
1981         }
1982
1983         /* Grab the filter */
1984         if ( ludp->lud_filter ) {
1985                 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
1986                                 cp->op->o_tmpmemctx );
1987                 
1988         } else {
1989                 op2.ors_filterstr = defaultFilter_bv;
1990         }
1991
1992         op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
1993         if ( op2.ors_filter == NULL ) {
1994                 rc = LDAP_PROTOCOL_ERROR;
1995                 goto url_done;
1996         }
1997
1998         /* Grab the scope */
1999         op2.ors_scope = ludp->lud_scope;
2000
2001         /* Grap the attributes */
2002         if ( ludp->lud_attrs ) {
2003                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2004                         ;
2005
2006                 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2007                                 cp->op->o_tmpmemctx );
2008
2009                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2010                         ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2011                         anlistp[ nattrs ].an_desc = NULL;
2012                         rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2013                                         &anlistp[ nattrs ].an_desc, &text );
2014                         if ( rc != LDAP_SUCCESS ) {
2015                                 goto url_done;
2016                         }
2017                 }
2018
2019         } else {
2020                 anlistp = anlist;
2021         }
2022
2023         anlistp[ nattrs ].an_name = desc->ad_cname;
2024         anlistp[ nattrs ].an_desc = desc;
2025
2026         BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2027         
2028         p.cookie = cookie;
2029         
2030         op2.o_hdr = cp->op->o_hdr;
2031         op2.o_tag = LDAP_REQ_SEARCH;
2032         op2.o_ndn = op2.o_bd->be_rootndn;
2033         op2.o_callback = &cb;
2034         op2.o_time = slap_get_time();
2035         op2.o_do_not_cache = 1;
2036         op2.o_is_auth_check = 0;
2037         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
2038         op2.ors_slimit = SLAP_NO_LIMIT;
2039         op2.ors_tlimit = SLAP_NO_LIMIT;
2040         op2.ors_attrs = anlistp;
2041         op2.ors_attrsonly = 0;
2042
2043         cb.sc_private = &p;
2044
2045         rc = op2.o_bd->be_search( &op2, &rs );
2046         if ( rc != 0 ) {
2047                 goto url_done;
2048         }
2049
2050 url_done:;
2051         if ( op2.ors_filter ) {
2052                 filter_free_x( cp->op, op2.ors_filter );
2053         }
2054         if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2055                 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
2056         }
2057         if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2058                 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
2059         }
2060         if ( ludp ) {
2061                 ldap_free_urldesc( ludp );
2062         }
2063         if ( anlistp && anlistp != anlist ) {
2064                 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
2065         }
2066
2067         return p.bvals;
2068 }
2069
2070 BerVarray
2071 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2072 {
2073         AciSetCookie    *cp = (AciSetCookie *)cookie;
2074         BerVarray       bvals = NULL;
2075         struct berval   ndn;
2076         int             rc = 0;
2077
2078         /* this routine needs to return the bervals instead of
2079          * plain strings, since syntax is not known.  It should
2080          * also return the syntax or some "comparison cookie".
2081          */
2082         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2083         if ( rc == LDAP_SUCCESS ) {
2084                 if ( desc == slap_schema.si_ad_entryDN ) {
2085                         bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2086                                         cp->op->o_tmpmemctx );
2087                         bvals[ 0 ] = ndn;
2088                         BER_BVZERO( &bvals[ 1 ] );
2089                         BER_BVZERO( &ndn );
2090
2091                 } else {
2092                         backend_attribute( cp->op,
2093                                 cp->e, &ndn, desc, &bvals, ACL_NONE );
2094                 }
2095
2096                 if ( !BER_BVISNULL( &ndn ) ) {
2097                         slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2098                 }
2099         }
2100
2101         return bvals;
2102 }
2103
2104 static int
2105 aci_match_set (
2106         struct berval *subj,
2107         Operation *op,
2108         Entry *e,
2109         int setref
2110 )
2111 {
2112         struct berval   set = BER_BVNULL;
2113         int             rc = 0;
2114         AciSetCookie    cookie;
2115
2116         if (setref == 0) {
2117                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2118         } else {
2119                 struct berval           subjdn, ndn = BER_BVNULL;
2120                 struct berval           setat;
2121                 BerVarray               bvals;
2122                 const char              *text;
2123                 AttributeDescription    *desc = NULL;
2124
2125                 /* format of string is "entry/setAttrName" */
2126                 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2127                         return(0);
2128                 }
2129
2130                 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2131                         setat = aci_bv_set_attr;
2132                 }
2133
2134                 /*
2135                  * NOTE: dnNormalize honors the ber_len field
2136                  * as the length of the dn to be normalized
2137                  */
2138                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2139                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2140                         {
2141                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2142                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2143                                         int     i;
2144
2145                                         set = bvals[0];
2146                                         BER_BVZERO( &bvals[0] );
2147                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2148                                                 /* count */ ;
2149                                         bvals[0].bv_val = bvals[i-1].bv_val;
2150                                         BER_BVZERO( &bvals[i-1] );
2151                                 }
2152                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2153                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2154                         }
2155                 }
2156         }
2157
2158         if ( !BER_BVISNULL( &set ) ) {
2159                 cookie.op = op;
2160                 cookie.e = e;
2161                 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2162                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2163                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2164         }
2165
2166         return(rc);
2167 }
2168
2169 #ifdef SLAPD_ACI_ENABLED
2170 static int
2171 aci_list_map_rights(
2172         struct berval *list )
2173 {
2174         struct berval bv;
2175         slap_access_t mask;
2176         int i;
2177
2178         ACL_INIT(mask);
2179         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2180                 if (bv.bv_len <= 0)
2181                         continue;
2182                 switch (*bv.bv_val) {
2183                 case 'c':
2184                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2185                         break;
2186                 case 's':
2187                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2188                          * the right 's' to mean "set", but in the examples states
2189                          * that the right 's' means "search".  The latter definition
2190                          * is used here.
2191                          */
2192                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2193                         break;
2194                 case 'r':
2195                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
2196                         break;
2197                 case 'w':
2198                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2199                         break;
2200                 case 'x':
2201                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
2202                          * define any equivalent to the AUTH right, so I've just used
2203                          * 'x' for now.
2204                          */
2205                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2206                         break;
2207                 default:
2208                         break;
2209                 }
2210
2211         }
2212         return(mask);
2213 }
2214
2215 static int
2216 aci_list_has_attr(
2217         struct berval *list,
2218         const struct berval *attr,
2219         struct berval *val )
2220 {
2221         struct berval bv, left, right;
2222         int i;
2223
2224         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2225                 if (aci_get_part(&bv, 0, '=', &left) < 0
2226                         || aci_get_part(&bv, 1, '=', &right) < 0)
2227                 {
2228                         if (ber_bvstrcasecmp(attr, &bv) == 0)
2229                                 return(1);
2230                 } else if (val == NULL) {
2231                         if (ber_bvstrcasecmp(attr, &left) == 0)
2232                                 return(1);
2233                 } else {
2234                         if (ber_bvstrcasecmp(attr, &left) == 0) {
2235                                 /* this is experimental code that implements a
2236                                  * simple (prefix) match of the attribute value.
2237                                  * the ACI draft does not provide for aci's that
2238                                  * apply to specific values, but it would be
2239                                  * nice to have.  If the <attr> part of an aci's
2240                                  * rights list is of the form <attr>=<value>,
2241                                  * that means the aci applies only to attrs with
2242                                  * the given value.  Furthermore, if the attr is
2243                                  * of the form <attr>=<value>*, then <value> is
2244                                  * treated as a prefix, and the aci applies to 
2245                                  * any value with that prefix.
2246                                  *
2247                                  * Ideally, this would allow r.e. matches.
2248                                  */
2249                                 if (aci_get_part(&right, 0, '*', &left) < 0
2250                                         || right.bv_len <= left.bv_len)
2251                                 {
2252                                         if (ber_bvstrcasecmp(val, &right) == 0)
2253                                                 return(1);
2254                                 } else if (val->bv_len >= left.bv_len) {
2255                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2256                                                 return(1);
2257                                 }
2258                         }
2259                 }
2260         }
2261         return(0);
2262 }
2263
2264 static slap_access_t
2265 aci_list_get_attr_rights(
2266         struct berval *list,
2267         const struct berval *attr,
2268         struct berval *val )
2269 {
2270     struct berval bv;
2271     slap_access_t mask;
2272     int i;
2273
2274         /* loop through each rights/attr pair, skip first part (action) */
2275         ACL_INIT(mask);
2276         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2277                 if (aci_list_has_attr(&bv, attr, val) == 0)
2278                         continue;
2279                 if (aci_get_part(list, i, ';', &bv) < 0)
2280                         continue;
2281                 mask |= aci_list_map_rights(&bv);
2282         }
2283         return(mask);
2284 }
2285
2286 static int
2287 aci_list_get_rights(
2288         struct berval *list,
2289         const struct berval *attr,
2290         struct berval *val,
2291         slap_access_t *grant,
2292         slap_access_t *deny )
2293 {
2294     struct berval perm, actn;
2295     slap_access_t *mask;
2296     int i, found;
2297
2298         if (attr == NULL || attr->bv_len == 0 
2299                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2300                 attr = &aci_bv_br_entry;
2301         }
2302
2303         found = 0;
2304         ACL_INIT(*grant);
2305         ACL_INIT(*deny);
2306         /* loop through each permissions clause */
2307         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2308                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2309                         continue;
2310                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2311                         mask = grant;
2312                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2313                         mask = deny;
2314                 } else {
2315                         continue;
2316                 }
2317
2318                 found = 1;
2319                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2320                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2321         }
2322         return(found);
2323 }
2324
2325 static int
2326 aci_group_member (
2327         struct berval   *subj,
2328         struct berval   *defgrpoc,
2329         struct berval   *defgrpat,
2330         Operation       *op,
2331         Entry           *e,
2332         int             nmatch,
2333         regmatch_t      *matches
2334 )
2335 {
2336         struct berval subjdn;
2337         struct berval grpoc;
2338         struct berval grpat;
2339         ObjectClass *grp_oc = NULL;
2340         AttributeDescription *grp_ad = NULL;
2341         const char *text;
2342         int rc;
2343
2344         /* format of string is "group/objectClassValue/groupAttrName" */
2345         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2346                 return(0);
2347         }
2348
2349         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2350                 grpoc = *defgrpoc;
2351         }
2352
2353         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2354                 grpat = *defgrpat;
2355         }
2356
2357         rc = slap_bv2ad( &grpat, &grp_ad, &text );
2358         if( rc != LDAP_SUCCESS ) {
2359                 rc = 0;
2360                 goto done;
2361         }
2362         rc = 0;
2363
2364         grp_oc = oc_bvfind( &grpoc );
2365
2366         if (grp_oc != NULL && grp_ad != NULL ) {
2367                 char buf[ACL_BUF_SIZE];
2368                 struct berval bv, ndn;
2369                 bv.bv_len = sizeof( buf ) - 1;
2370                 bv.bv_val = (char *)&buf;
2371                 if ( string_expand(&bv, &subjdn,
2372                                 e->e_ndn, nmatch, matches) )
2373                 {
2374                         rc = LDAP_OTHER;
2375                         goto done;
2376                 }
2377                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2378                         rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2379                                 grp_oc, grp_ad ) == 0 );
2380                         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2381                 }
2382         }
2383
2384 done:
2385         return(rc);
2386 }
2387
2388 static int
2389 aci_mask(
2390         Operation               *op,
2391         Entry                   *e,
2392         AttributeDescription    *desc,
2393         struct berval           *val,
2394         struct berval           *aci,
2395         int                     nmatch,
2396         regmatch_t              *matches,
2397         slap_access_t           *grant,
2398         slap_access_t           *deny,
2399         slap_aci_scope_t        asserted_scope
2400 )
2401 {
2402         struct berval           bv, scope, perms, type, sdn;
2403         int                     rc;
2404                 
2405
2406         assert( !BER_BVISNULL( &desc->ad_cname ) );
2407
2408         /* parse an aci of the form:
2409                 oid # scope # action;rights;attr;rights;attr 
2410                         $ action;rights;attr;rights;attr # type # subject
2411
2412            [NOTE: the following comment is very outdated,
2413            as the draft version it refers to (Ando, 2004-11-20)].
2414
2415            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2416            a full description of the format for this attribute.
2417            Differences: "this" in the draft is "self" here, and
2418            "self" and "public" is in the position of type.
2419
2420            <scope> = {entry|children|subtree}
2421            <type> = {public|users|access-id|subtree|onelevel|children|
2422                      self|dnattr|group|role|set|set-ref}
2423
2424            This routine now supports scope={ENTRY,CHILDREN}
2425            with the semantics:
2426              - ENTRY applies to "entry" and "subtree";
2427              - CHILDREN aplies to "children" and "subtree"
2428          */
2429
2430         /* check that the aci has all 5 components */
2431         if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2432                 return 0;
2433         }
2434
2435         /* check that the aci family is supported */
2436         if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2437                 return 0;
2438         }
2439
2440         /* check that the scope matches */
2441         if ( aci_get_part( aci, 1, '#', &scope ) < 0 ) {
2442                 return 0;
2443         }
2444
2445         /* note: scope can be either ENTRY or CHILDREN;
2446          * they respectively match "entry" and "children" in bv
2447          * both match "subtree" */
2448         switch ( asserted_scope ) {
2449         case SLAP_ACI_SCOPE_ENTRY:
2450                 if ( ber_bvstrcasecmp( &scope, &aci_bv_entry ) != 0
2451                                 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2452                 {
2453                         return 0;
2454                 }
2455                 break;
2456
2457         case SLAP_ACI_SCOPE_CHILDREN:
2458                 if ( ber_bvstrcasecmp( &scope, &aci_bv_children ) != 0
2459                                 && ber_bvstrcasecmp( &scope, &aci_bv_subtree ) != 0 )
2460                 {
2461                         return 0;
2462                 }
2463                 break;
2464
2465         default:
2466                 return 0;
2467         }
2468
2469         /* get the list of permissions clauses, bail if empty */
2470         if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2471                 return 0;
2472         }
2473
2474         /* check if any permissions allow desired access */
2475         if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2476                 return 0;
2477         }
2478
2479         /* see if we have a DN match */
2480         if ( aci_get_part( aci, 3, '#', &type ) < 0 ) {
2481                 return 0;
2482         }
2483
2484         /* see if we have a public (i.e. anonymous) access */
2485         if ( ber_bvstrcasecmp( &aci_bv_public, &type ) == 0 ) {
2486                 return 1;
2487         }
2488         
2489         /* otherwise require an identity */
2490         if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
2491                 return 0;
2492         }
2493
2494         /* see if we have a users access */
2495         if ( ber_bvstrcasecmp( &aci_bv_users, &type ) == 0 ) {
2496                 return 1;
2497         }
2498         
2499         /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
2500          * just grab all the berval up to its end (ITS#3303).
2501          * NOTE: the problem could be solved by providing the DN with
2502          * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would 
2503          * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
2504 #if 0
2505         if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
2506                 return 0;
2507         }
2508 #endif
2509         sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
2510         sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
2511
2512         if ( ber_bvstrcasecmp( &aci_bv_access_id, &type ) == 0 ) {
2513                 struct berval ndn;
2514                 
2515                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2516                 if ( rc != LDAP_SUCCESS ) {
2517                         return 0;
2518                 }
2519
2520                 if ( dn_match( &op->o_ndn, &ndn ) ) {
2521                         rc = 1;
2522                 }
2523                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2524
2525                 return rc;
2526
2527         } else if ( ber_bvstrcasecmp( &aci_bv_subtree, &type ) == 0 ) {
2528                 struct berval ndn;
2529                 
2530                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2531                 if ( rc != LDAP_SUCCESS ) {
2532                         return 0;
2533                 }
2534
2535                 if ( dnIsSuffix( &op->o_ndn, &ndn ) ) {
2536                         rc = 1;
2537                 }
2538                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2539
2540                 return rc;
2541
2542         } else if ( ber_bvstrcasecmp( &aci_bv_onelevel, &type ) == 0 ) {
2543                 struct berval ndn, pndn;
2544                 
2545                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2546                 if ( rc != LDAP_SUCCESS ) {
2547                         return 0;
2548                 }
2549
2550                 dnParent( &ndn, &pndn );
2551
2552                 if ( dn_match( &op->o_ndn, &pndn ) ) {
2553                         rc = 1;
2554                 }
2555                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2556
2557                 return rc;
2558
2559         } else if ( ber_bvstrcasecmp( &aci_bv_children, &type ) == 0 ) {
2560                 struct berval ndn;
2561                 
2562                 rc = dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx );
2563                 if ( rc != LDAP_SUCCESS ) {
2564                         return 0;
2565                 }
2566
2567                 if ( !dn_match( &op->o_ndn, &ndn )
2568                                 && dnIsSuffix( &op->o_ndn, &ndn ) )
2569                 {
2570                         rc = 1;
2571                 }
2572                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2573
2574                 return rc;
2575
2576         } else if ( ber_bvstrcasecmp( &aci_bv_self, &type ) == 0 ) {
2577                 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
2578                         return 1;
2579                 }
2580
2581         } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &type ) == 0 ) {
2582                 Attribute               *at;
2583                 AttributeDescription    *ad = NULL;
2584                 const char              *text;
2585
2586                 rc = slap_bv2ad( &sdn, &ad, &text );
2587
2588                 if( rc != LDAP_SUCCESS ) {
2589                         return 0;
2590                 }
2591
2592                 rc = 0;
2593
2594                 for ( at = attrs_find( e->e_attrs, ad );
2595                                 at != NULL;
2596                                 at = attrs_find( at->a_next, ad ) )
2597                 {
2598                         if ( value_find_ex( ad,
2599                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2600                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2601                                 at->a_nvals,
2602                                 &op->o_ndn, op->o_tmpmemctx ) == 0 )
2603                         {
2604                                 rc = 1;
2605                                 break;
2606                         }
2607                 }
2608
2609                 return rc;
2610
2611         } else if ( ber_bvstrcasecmp( &aci_bv_group, &type ) == 0 ) {
2612                 if ( aci_group_member( &sdn, &aci_bv_group_class,
2613                                 &aci_bv_group_attr, op, e, nmatch, matches ) )
2614                 {
2615                         return 1;
2616                 }
2617
2618         } else if ( ber_bvstrcasecmp( &aci_bv_role, &type ) == 0 ) {
2619                 if ( aci_group_member( &sdn, &aci_bv_role_class,
2620                                 &aci_bv_role_attr, op, e, nmatch, matches ) )
2621                 {
2622                         return 1;
2623                 }
2624
2625         } else if ( ber_bvstrcasecmp( &aci_bv_set, &type ) == 0 ) {
2626                 if ( aci_match_set( &sdn, op, e, 0 ) ) {
2627                         return 1;
2628                 }
2629
2630         } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &type ) == 0 ) {
2631                 if ( aci_match_set( &sdn, op, e, 1 ) ) {
2632                         return 1;
2633                 }
2634         }
2635
2636         return 0;
2637 }
2638
2639 #ifdef SLAP_DYNACL
2640 static int
2641 dynacl_aci_parse( const char *fname, int lineno, slap_style_t sty, const char *right, void **privp )
2642 {
2643         AttributeDescription    *ad = NULL;
2644         const char              *text = NULL;
2645
2646         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
2647                 fprintf( stderr, "%s: line %d: "
2648                         "inappropriate style \"%s\" in \"aci\" by clause\n",
2649                         fname, lineno, sty );
2650                 return -1;
2651         }
2652
2653         if ( right != NULL && *right != '\0' ) {
2654                 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
2655                         fprintf( stderr,
2656                                 "%s: line %d: aci \"%s\": %s\n",
2657                                 fname, lineno, right, text );
2658                         return -1;
2659                 }
2660
2661         } else {
2662                 ad = slap_schema.si_ad_aci;
2663         }
2664
2665         if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
2666                 fprintf( stderr, "%s: line %d: "
2667                         "aci \"%s\": inappropriate syntax: %s\n",
2668                         fname, lineno, right,
2669                         ad->ad_type->sat_syntax_oid );
2670                 return -1;
2671         }
2672
2673         *privp = (void *)ad;
2674
2675         return 0;
2676 }
2677
2678 static int
2679 dynacl_aci_print( void *priv )
2680 {
2681         AttributeDescription    *ad = ( AttributeDescription * )priv;
2682
2683         assert( ad );
2684
2685         fprintf( stderr, " aci=%s", ad->ad_cname.bv_val );
2686
2687         return 0;
2688 }
2689
2690
2691 static int
2692 dynacl_aci_mask(
2693                 void                    *priv,
2694                 Operation               *op,
2695                 Entry                   *e,
2696                 AttributeDescription    *desc,
2697                 struct berval           *val,
2698                 int                     nmatch,
2699                 regmatch_t              *matches,
2700                 slap_access_t           *grantp,
2701                 slap_access_t           *denyp )
2702 {
2703         AttributeDescription    *ad = ( AttributeDescription * )priv;
2704         Attribute               *at;
2705         slap_access_t           tgrant, tdeny, grant, deny;
2706 #ifdef LDAP_DEBUG
2707         char                    accessmaskbuf[ACCESSMASK_MAXLEN];
2708         char                    accessmaskbuf1[ACCESSMASK_MAXLEN];
2709 #endif /* LDAP_DEBUG */
2710
2711         /* start out with nothing granted, nothing denied */
2712         ACL_INIT(tgrant);
2713         ACL_INIT(tdeny);
2714
2715         /* get the aci attribute */
2716         at = attr_find( e->e_attrs, ad );
2717         if ( at != NULL ) {
2718                 int             i;
2719
2720                 /* the aci is an multi-valued attribute.  The
2721                  * rights are determined by OR'ing the individual
2722                  * rights given by the acis.
2723                  */
2724                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
2725                         if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
2726                                         nmatch, matches, &grant, &deny,
2727                                         SLAP_ACI_SCOPE_ENTRY ) != 0 )
2728                         {
2729                                 tgrant |= grant;
2730                                 tdeny |= deny;
2731                         }
2732                 }
2733                 
2734                 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
2735                           accessmask2str( tgrant, accessmaskbuf ), 
2736                           accessmask2str( tdeny, accessmaskbuf1 ), 0 );
2737         }
2738
2739         /* If the entry level aci didn't contain anything valid for the 
2740          * current operation, climb up the tree and evaluate the
2741          * acis with scope set to subtree
2742          */
2743         if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
2744                 struct berval   parent_ndn;
2745                 struct berval   old_parent_ndn = BER_BVNULL;
2746
2747 #if 1
2748                 /* to solve the chicken'n'egg problem of accessing
2749                  * the OpenLDAPaci attribute, the direct access
2750                  * to the entry's attribute is unchecked; however,
2751                  * further accesses to OpenLDAPaci values in the 
2752                  * ancestors occur through backend_attribute(), i.e.
2753                  * with the identity of the operation, requiring
2754                  * further access checking.  For uniformity, this
2755                  * makes further requests occur as the rootdn, if
2756                  * any, i.e. searching for the OpenLDAPaci attribute
2757                  * is considered an internal search.  If this is not
2758                  * acceptable, then the same check needs be performed
2759                  * when accessing the entry's attribute. */
2760                 Operation       op2 = *op;
2761
2762                 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
2763                         op2.o_dn = op->o_bd->be_rootdn;
2764                         op2.o_ndn = op->o_bd->be_rootndn;
2765                 }
2766 #endif
2767
2768                 dnParent( &e->e_nname, &parent_ndn );
2769                 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
2770                         int             i;
2771                         BerVarray       bvals = NULL;
2772                         int             ret, stop;
2773
2774                         old_parent_ndn = parent_ndn;
2775                         Debug( LDAP_DEBUG_ACL, "checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
2776                         ret = backend_attribute( &op2, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
2777
2778                         switch ( ret ) {
2779                         case LDAP_SUCCESS :
2780                                 stop = 0;
2781                                 if ( !bvals ) {
2782                                         break;
2783                                 }
2784
2785                                 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++) {
2786                                         if ( aci_mask( op, e, desc, val,
2787                                                         &bvals[i],
2788                                                         nmatch, matches,
2789                                                         &grant, &deny,
2790                                                         SLAP_ACI_SCOPE_CHILDREN ) != 0 )
2791                                         {
2792                                                 tgrant |= grant;
2793                                                 tdeny |= deny;
2794                                                 /* evaluation stops as soon as either a "deny" or a 
2795                                                  * "grant" directive matches.
2796                                                  */
2797                                                 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
2798                                                         stop = 1;
2799                                                 }
2800                                         }
2801                                         Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
2802                                                 accessmask2str( tgrant, accessmaskbuf ),
2803                                                 accessmask2str( tdeny, accessmaskbuf1 ), 0 );
2804                                 }
2805                                 break;
2806
2807                         case LDAP_NO_SUCH_ATTRIBUTE:
2808                                 /* just go on if the aci-Attribute is not present in
2809                                  * the current entry 
2810                                  */
2811                                 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
2812                                 stop = 0;
2813                                 break;
2814
2815                         case LDAP_NO_SUCH_OBJECT:
2816                                 /* We have reached the base object */
2817                                 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
2818                                 stop = 1;
2819                                 break;
2820
2821                         default:
2822                                 stop = 1;
2823                                 break;
2824                         }
2825
2826                         if ( stop ) {
2827                                 break;
2828                         }
2829                         dnParent( &old_parent_ndn, &parent_ndn );
2830                 }
2831         }
2832
2833         *grantp = tgrant;
2834         *denyp = tdeny;
2835
2836         return 0;
2837 }
2838
2839 /* need to register this at some point */
2840 static slap_dynacl_t    dynacl_aci = {
2841         "aci",
2842         dynacl_aci_parse,
2843         dynacl_aci_print,
2844         dynacl_aci_mask,
2845         NULL,
2846         NULL,
2847         NULL
2848 };
2849
2850 #endif /* SLAP_DYNACL */
2851
2852 #endif  /* SLAPD_ACI_ENABLED */
2853
2854 #ifdef SLAP_DYNACL
2855
2856 /*
2857  * dynamic ACL infrastructure
2858  */
2859 static slap_dynacl_t    *da_list = NULL;
2860
2861 int
2862 slap_dynacl_register( slap_dynacl_t *da )
2863 {
2864         slap_dynacl_t   *tmp;
2865
2866         for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2867                 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2868                         break;
2869                 }
2870         }
2871
2872         if ( tmp != NULL ) {
2873                 return -1;
2874         }
2875         
2876         if ( da->da_mask == NULL ) {
2877                 return -1;
2878         }
2879         
2880         da->da_private = NULL;
2881         da->da_next = da_list;
2882         da_list = da;
2883
2884         return 0;
2885 }
2886
2887 static slap_dynacl_t *
2888 slap_dynacl_next( slap_dynacl_t *da )
2889 {
2890         if ( da ) {
2891                 return da->da_next;
2892         }
2893         return da_list;
2894 }
2895
2896 slap_dynacl_t *
2897 slap_dynacl_get( const char *name )
2898 {
2899         slap_dynacl_t   *da;
2900
2901         for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2902                 if ( strcasecmp( da->da_name, name ) == 0 ) {
2903                         break;
2904                 }
2905         }
2906
2907         return da;
2908 }
2909 #endif /* SLAP_DYNACL */
2910
2911 int
2912 acl_init( void )
2913 {
2914         int             i, rc;
2915 #ifdef SLAP_DYNACL
2916         slap_dynacl_t   *known_dynacl[] = {
2917 #ifdef SLAPD_ACI_ENABLED
2918                 &dynacl_aci,
2919 #endif  /* SLAPD_ACI_ENABLED */
2920                 NULL
2921         };
2922
2923         for ( i = 0; known_dynacl[ i ]; i++ ) {
2924                 rc = slap_dynacl_register( known_dynacl[ i ] ); 
2925                 if ( rc ) {
2926                         return rc;
2927                 }
2928         }
2929 #endif /* SLAP_DYNACL */
2930
2931         return 0;
2932 }
2933
2934 static int
2935 string_expand(
2936         struct berval   *bv,
2937         struct berval   *pat,
2938         char            *match,
2939         int             nmatch,
2940         regmatch_t      *matches)
2941 {
2942         ber_len_t       size;
2943         char   *sp;
2944         char   *dp;
2945         int     flag;
2946
2947         size = 0;
2948         bv->bv_val[0] = '\0';
2949         bv->bv_len--; /* leave space for lone $ */
2950
2951         flag = 0;
2952         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2953                 sp < pat->bv_val + pat->bv_len ; sp++ )
2954         {
2955                 /* did we previously see a $ */
2956                 if ( flag ) {
2957                         if ( flag == 1 && *sp == '$' ) {
2958                                 *dp++ = '$';
2959                                 size++;
2960                                 flag = 0;
2961
2962                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2963                                 flag = 2;
2964
2965                         } else if ( *sp >= '0' && *sp <= '9' ) {
2966                                 int     n;
2967                                 int     i;
2968                                 int     l;
2969
2970                                 n = *sp - '0';
2971
2972                                 if ( flag == 2 ) {
2973                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2974                                                 if ( *sp >= '0' && *sp <= '9' ) {
2975                                                         n = 10*n + ( *sp - '0' );
2976                                                 }
2977                                         }
2978
2979                                         if ( *sp != /*'{'*/ '}' ) {
2980                                                 /* FIXME: error */
2981                                                 return 1;
2982                                         }
2983                                 }
2984
2985                                 if ( n >= nmatch ) {
2986                                         /* FIXME: error */
2987                                         return 1;
2988                                 }
2989                                 
2990                                 *dp = '\0';
2991                                 i = matches[n].rm_so;
2992                                 l = matches[n].rm_eo; 
2993                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2994                                         *dp++ = match[i];
2995                                 }
2996                                 *dp = '\0';
2997
2998                                 flag = 0;
2999                         }
3000                 } else {
3001                         if (*sp == '$') {
3002                                 flag = 1;
3003                         } else {
3004                                 *dp++ = *sp;
3005                                 size++;
3006                         }
3007                 }
3008         }
3009
3010         if ( flag ) {
3011                 /* must have ended with a single $ */
3012                 *dp++ = '$';
3013                 size++;
3014         }
3015
3016         *dp = '\0';
3017         bv->bv_len = size;
3018
3019         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
3020         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
3021
3022         return 0;
3023 }
3024
3025 static int
3026 regex_matches(
3027         struct berval   *pat,           /* pattern to expand and match against */
3028         char            *str,           /* string to match against pattern */
3029         char            *buf,           /* buffer with $N expansion variables */
3030         int             nmatch, /* size of the matches array */
3031         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
3032 )
3033 {
3034         regex_t re;
3035         char newbuf[ACL_BUF_SIZE];
3036         struct berval bv;
3037         int     rc;
3038
3039         bv.bv_len = sizeof( newbuf ) - 1;
3040         bv.bv_val = newbuf;
3041
3042         if (str == NULL) {
3043                 str = "";
3044         };
3045
3046         string_expand( &bv, pat, buf, nmatch, matches );
3047         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
3048         if ( rc ) {
3049                 char error[ACL_BUF_SIZE];
3050                 regerror( rc, &re, error, sizeof( error ) );
3051
3052                 Debug( LDAP_DEBUG_TRACE,
3053                     "compile( \"%s\", \"%s\") failed %s\n",
3054                         pat->bv_val, str, error );
3055                 return( 0 );
3056         }
3057
3058         rc = regexec( &re, str, 0, NULL, 0 );
3059         regfree( &re );
3060
3061         Debug( LDAP_DEBUG_TRACE,
3062             "=> regex_matches: string:   %s\n", str, 0, 0 );
3063         Debug( LDAP_DEBUG_TRACE,
3064             "=> regex_matches: rc: %d %s\n",
3065                 rc, !rc ? "matches" : "no matches", 0 );
3066         return( !rc );
3067 }
3068