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