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