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