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