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