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