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