]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
77cbcc2012e6842ad25ca8a6287570f6cb8c3cb4
[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         AttributeDescription    *desc;
1797         BerVarray               bvals;
1798 } aci_set_gather_t;
1799
1800 static int
1801 aci_set_cb_gather( Operation *op, SlapReply *rs )
1802 {
1803         aci_set_gather_t        *p = (aci_set_gather_t *)op->o_callback->sc_private;
1804         
1805         if ( rs->sr_type == REP_SEARCH ) {
1806                 BerVarray       bvals = NULL;
1807
1808                 if ( p->desc == slap_schema.si_ad_entryDN ) {
1809                         bvals = slap_sl_malloc( sizeof( BerValue ) * 2, op->o_tmpmemctx );
1810                         ber_dupbv_x( &bvals[ 0 ], &rs->sr_entry->e_nname, op->o_tmpmemctx );
1811                         BER_BVZERO( &bvals[ 1 ] );
1812
1813                 } else {
1814                         Attribute       *a;
1815
1816                         a = attr_find( rs->sr_entry->e_attrs, p->desc );
1817                         if ( a != NULL ) {
1818                                 int     i;
1819
1820                                 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
1821                                         ;
1822
1823                                 bvals = slap_sl_malloc( sizeof( BerValue ) * ( i + 1 ), op->o_tmpmemctx );
1824                                 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
1825                                         ber_dupbv_x( &bvals[ i ], &a->a_nvals[ i ], op->o_tmpmemctx );
1826                                 }
1827                                 BER_BVZERO( &bvals[ i ] );
1828                         }
1829                 }
1830
1831                 if ( bvals ) {
1832                         p->bvals = slap_set_join( p->cookie, p->bvals, '|', bvals );
1833                 }
1834
1835         } else {
1836                 assert( rs->sr_type == REP_RESULT );
1837         }
1838
1839         return 0;
1840 }
1841
1842 BerVarray
1843 aci_set_gather( SetCookie *cookie, struct berval *name, struct berval *attr )
1844 {
1845         AciSetCookie            *cp = (AciSetCookie *)cookie;
1846         int                     rc = 0;
1847         LDAPURLDesc             *ludp = NULL;
1848         Operation               op2 = { 0 };
1849         SlapReply               rs = {REP_RESULT};
1850         AttributeName           anlist[2];
1851         slap_callback           cb = { NULL, aci_set_cb_gather, NULL, NULL };
1852         aci_set_gather_t        p = { 0 };
1853         struct berval           aname;
1854         const char              *text = NULL;
1855
1856         /* this routine needs to return the bervals instead of
1857          * plain strings, since syntax is not known.  It should
1858          * also return the syntax or some "comparison cookie".
1859          */
1860         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
1861                 return aci_set_gather2( cookie, name, attr );
1862         }
1863
1864         rc = ldap_url_parse( name->bv_val, &ludp );
1865         if ( rc != LDAP_URL_SUCCESS ) {
1866                 rc = LDAP_PROTOCOL_ERROR;
1867                 goto url_done;
1868         }
1869         
1870         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
1871         {
1872                 /* host part must be empty */
1873                 /* extensions parts must be empty */
1874                 rc = LDAP_PROTOCOL_ERROR;
1875                 goto url_done;
1876         }
1877
1878         /* Grab the filter */
1879         if ( ludp->lud_filter ) {
1880                 op2.ors_filter = str2filter_x( cp->op, ludp->lud_filter );
1881                 if ( op2.ors_filter == NULL ) {
1882                         rc = LDAP_PROTOCOL_ERROR;
1883                         goto url_done;
1884                 }
1885                 ber_str2bv( ludp->lud_filter, 0, 0, &op2.ors_filterstr );
1886         }
1887
1888         /* Grab the searchbase */
1889         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
1890
1891         /* Grab the scope */
1892         op2.ors_scope = ludp->lud_scope;
1893
1894         if ( ludp->lud_attrs && ludp->lud_attrs[0] ) {
1895                 if ( ludp->lud_attrs[1] ) {
1896                         rc = LDAP_PROTOCOL_ERROR;
1897                         goto url_done;
1898                 }
1899                 ber_str2bv( ludp->lud_attrs[0], 0, 0, &aname );
1900
1901         } else {
1902                 aname = *attr;
1903         }
1904
1905         rc = slap_bv2ad( &aname, &p.desc, &text );
1906         if ( rc != LDAP_SUCCESS ) {
1907                 goto url_done;
1908         }
1909         p.cookie = cookie;
1910         
1911         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
1912                         &op2.o_req_ndn, cp->op->o_tmpmemctx );
1913
1914         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
1915         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
1916                 rc = LDAP_NO_SUCH_OBJECT;
1917                 goto url_done;
1918         }
1919
1920         op2.o_tag = LDAP_REQ_SEARCH;
1921         op2.o_protocol = LDAP_VERSION3;
1922         op2.o_ndn = op2.o_bd->be_rootndn;
1923         op2.o_callback = &cb;
1924         op2.o_time = slap_get_time();
1925         op2.o_do_not_cache = 1;
1926         op2.o_is_auth_check = 0;
1927         op2.o_threadctx = cp->op->o_threadctx;
1928         op2.o_tmpmemctx = cp->op->o_tmpmemctx;
1929         op2.o_tmpmfuncs = cp->op->o_tmpmfuncs;
1930 #ifdef LDAP_SLAPI
1931         op2.o_pb = cp->op->o_pb;
1932 #endif
1933         op2.o_conn = cp->op->o_conn;
1934         op2.o_connid = cp->op->o_connid;
1935         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
1936         op2.ors_slimit = SLAP_NO_LIMIT;
1937         op2.ors_tlimit = SLAP_NO_LIMIT;
1938         anlist[0].an_name = p.desc->ad_cname;
1939         anlist[0].an_desc = p.desc;
1940         BER_BVZERO( &anlist[1].an_name );
1941         op2.ors_attrs = anlist;
1942         op2.ors_attrsonly = 0;
1943         op2.o_sync_slog_size = -1;
1944
1945         cb.sc_private = &p;
1946
1947         rc = op2.o_bd->be_search( &op2, &rs );
1948         if ( rc != 0 ) {
1949                 goto url_done;
1950         }
1951
1952 url_done:;
1953         if ( op2.ors_filter ) {
1954                 filter_free_x( cp->op, op2.ors_filter );
1955         }
1956         slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
1957         ldap_free_urldesc( ludp );
1958
1959         return p.bvals;
1960 }
1961
1962 BerVarray
1963 aci_set_gather2( SetCookie *cookie, struct berval *name, struct berval *attr )
1964 {
1965         AciSetCookie    *cp = (AciSetCookie *)cookie;
1966         BerVarray       bvals = NULL;
1967         struct berval   ndn;
1968         int             rc = 0;
1969
1970         /* this routine needs to return the bervals instead of
1971          * plain strings, since syntax is not known.  It should
1972          * also return the syntax or some "comparison cookie".
1973          */
1974         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
1975         if ( rc == LDAP_SUCCESS ) {
1976                 const char *text;
1977                 AttributeDescription *desc = NULL;
1978                 if ( slap_bv2ad( attr, &desc, &text ) == LDAP_SUCCESS ) {
1979                         backend_attribute( cp->op,
1980                                 cp->e, &ndn, desc, &bvals, ACL_NONE );
1981                 }
1982                 slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
1983         }
1984
1985         return( bvals );
1986 }
1987
1988 static int
1989 aci_match_set (
1990         struct berval *subj,
1991         Operation *op,
1992         Entry *e,
1993         int setref
1994 )
1995 {
1996         struct berval   set = BER_BVNULL;
1997         int             rc = 0;
1998         AciSetCookie    cookie;
1999
2000         if (setref == 0) {
2001                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2002         } else {
2003                 struct berval           subjdn, ndn = BER_BVNULL;
2004                 struct berval           setat;
2005                 BerVarray               bvals;
2006                 const char              *text;
2007                 AttributeDescription    *desc = NULL;
2008
2009                 /* format of string is "entry/setAttrName" */
2010                 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2011                         return(0);
2012                 }
2013
2014                 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2015                         setat = aci_bv_set_attr;
2016                 }
2017
2018                 /*
2019                  * NOTE: dnNormalize honors the ber_len field
2020                  * as the length of the dn to be normalized
2021                  */
2022                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2023                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2024                         {
2025                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2026                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2027                                         int     i;
2028
2029                                         set = bvals[0];
2030                                         BER_BVZERO( &bvals[0] );
2031                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2032                                                 /* count */ ;
2033                                         bvals[0].bv_val = bvals[i-1].bv_val;
2034                                         BER_BVZERO( &bvals[i-1] );
2035                                 }
2036                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2037                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2038                         }
2039                 }
2040         }
2041
2042         if ( !BER_BVISNULL( &set ) ) {
2043                 cookie.op = op;
2044                 cookie.e = e;
2045                 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2046                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2047                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2048         }
2049
2050         return(rc);
2051 }
2052
2053 #ifdef SLAPD_ACI_ENABLED
2054 static int
2055 aci_list_map_rights(
2056         struct berval *list )
2057 {
2058         struct berval bv;
2059         slap_access_t mask;
2060         int i;
2061
2062         ACL_INIT(mask);
2063         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2064                 if (bv.bv_len <= 0)
2065                         continue;
2066                 switch (*bv.bv_val) {
2067                 case 'c':
2068                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2069                         break;
2070                 case 's':
2071                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2072                          * the right 's' to mean "set", but in the examples states
2073                          * that the right 's' means "search".  The latter definition
2074                          * is used here.
2075                          */
2076                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2077                         break;
2078                 case 'r':
2079                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
2080                         break;
2081                 case 'w':
2082                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2083                         break;
2084                 case 'x':
2085                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
2086                          * define any equivalent to the AUTH right, so I've just used
2087                          * 'x' for now.
2088                          */
2089                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2090                         break;
2091                 default:
2092                         break;
2093                 }
2094
2095         }
2096         return(mask);
2097 }
2098
2099 static int
2100 aci_list_has_attr(
2101         struct berval *list,
2102         const struct berval *attr,
2103         struct berval *val )
2104 {
2105         struct berval bv, left, right;
2106         int i;
2107
2108         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2109                 if (aci_get_part(&bv, 0, '=', &left) < 0
2110                         || aci_get_part(&bv, 1, '=', &right) < 0)
2111                 {
2112                         if (ber_bvstrcasecmp(attr, &bv) == 0)
2113                                 return(1);
2114                 } else if (val == NULL) {
2115                         if (ber_bvstrcasecmp(attr, &left) == 0)
2116                                 return(1);
2117                 } else {
2118                         if (ber_bvstrcasecmp(attr, &left) == 0) {
2119                                 /* this is experimental code that implements a
2120                                  * simple (prefix) match of the attribute value.
2121                                  * the ACI draft does not provide for aci's that
2122                                  * apply to specific values, but it would be
2123                                  * nice to have.  If the <attr> part of an aci's
2124                                  * rights list is of the form <attr>=<value>,
2125                                  * that means the aci applies only to attrs with
2126                                  * the given value.  Furthermore, if the attr is
2127                                  * of the form <attr>=<value>*, then <value> is
2128                                  * treated as a prefix, and the aci applies to 
2129                                  * any value with that prefix.
2130                                  *
2131                                  * Ideally, this would allow r.e. matches.
2132                                  */
2133                                 if (aci_get_part(&right, 0, '*', &left) < 0
2134                                         || right.bv_len <= left.bv_len)
2135                                 {
2136                                         if (ber_bvstrcasecmp(val, &right) == 0)
2137                                                 return(1);
2138                                 } else if (val->bv_len >= left.bv_len) {
2139                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2140                                                 return(1);
2141                                 }
2142                         }
2143                 }
2144         }
2145         return(0);
2146 }
2147
2148 static slap_access_t
2149 aci_list_get_attr_rights(
2150         struct berval *list,
2151         const struct berval *attr,
2152         struct berval *val )
2153 {
2154     struct berval bv;
2155     slap_access_t mask;
2156     int i;
2157
2158         /* loop through each rights/attr pair, skip first part (action) */
2159         ACL_INIT(mask);
2160         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2161                 if (aci_list_has_attr(&bv, attr, val) == 0)
2162                         continue;
2163                 if (aci_get_part(list, i, ';', &bv) < 0)
2164                         continue;
2165                 mask |= aci_list_map_rights(&bv);
2166         }
2167         return(mask);
2168 }
2169
2170 static int
2171 aci_list_get_rights(
2172         struct berval *list,
2173         const struct berval *attr,
2174         struct berval *val,
2175         slap_access_t *grant,
2176         slap_access_t *deny )
2177 {
2178     struct berval perm, actn;
2179     slap_access_t *mask;
2180     int i, found;
2181
2182         if (attr == NULL || attr->bv_len == 0 
2183                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2184                 attr = &aci_bv_br_entry;
2185         }
2186
2187         found = 0;
2188         ACL_INIT(*grant);
2189         ACL_INIT(*deny);
2190         /* loop through each permissions clause */
2191         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2192                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2193                         continue;
2194                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2195                         mask = grant;
2196                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2197                         mask = deny;
2198                 } else {
2199                         continue;
2200                 }
2201
2202                 found = 1;
2203                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2204                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2205         }
2206         return(found);
2207 }
2208
2209 static int
2210 aci_group_member (
2211         struct berval   *subj,
2212         struct berval   *defgrpoc,
2213         struct berval   *defgrpat,
2214         Operation       *op,
2215         Entry           *e,
2216         int             nmatch,
2217         regmatch_t      *matches
2218 )
2219 {
2220         struct berval subjdn;
2221         struct berval grpoc;
2222         struct berval grpat;
2223         ObjectClass *grp_oc = NULL;
2224         AttributeDescription *grp_ad = NULL;
2225         const char *text;
2226         int rc;
2227
2228         /* format of string is "group/objectClassValue/groupAttrName" */
2229         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2230                 return(0);
2231         }
2232
2233         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2234                 grpoc = *defgrpoc;
2235         }
2236
2237         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2238                 grpat = *defgrpat;
2239         }
2240
2241         rc = slap_bv2ad( &grpat, &grp_ad, &text );
2242         if( rc != LDAP_SUCCESS ) {
2243                 rc = 0;
2244                 goto done;
2245         }
2246         rc = 0;
2247
2248         grp_oc = oc_bvfind( &grpoc );
2249
2250         if (grp_oc != NULL && grp_ad != NULL ) {
2251                 char buf[ACL_BUF_SIZE];
2252                 struct berval bv, ndn;
2253                 bv.bv_len = sizeof( buf ) - 1;
2254                 bv.bv_val = (char *)&buf;
2255                 if ( string_expand(&bv, &subjdn,
2256                                 e->e_ndn, nmatch, matches) )
2257                 {
2258                         rc = LDAP_OTHER;
2259                         goto done;
2260                 }
2261                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2262                         rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2263                                 grp_oc, grp_ad ) == 0 );
2264                         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2265                 }
2266         }
2267
2268 done:
2269         return(rc);
2270 }
2271
2272 static int
2273 aci_mask(
2274         Operation               *op,
2275         Entry                   *e,
2276         AttributeDescription    *desc,
2277         struct berval           *val,
2278         struct berval           *aci,
2279         int                     nmatch,
2280         regmatch_t              *matches,
2281         slap_access_t           *grant,
2282         slap_access_t           *deny,
2283         struct berval           *scope
2284 )
2285 {
2286     struct berval bv, perms, sdn;
2287         int rc;
2288                 
2289
2290         assert( desc->ad_cname.bv_val != NULL );
2291
2292         /* parse an aci of the form:
2293                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2294
2295            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2296            a full description of the format for this attribute.
2297            Differences: "this" in the draft is "self" here, and
2298            "self" and "public" is in the position of dnType.
2299
2300            For now, this routine only supports scope=entry.
2301          */
2302         /* check that the aci has all 5 components */
2303         if (aci_get_part(aci, 4, '#', NULL) < 0)
2304                 return(0);
2305
2306         /* check that the aci family is supported */
2307         if (aci_get_part(aci, 0, '#', &bv) < 0)
2308                 return(0);
2309
2310         /* check that the scope matches */
2311         if (aci_get_part(aci, 1, '#', &bv) < 0
2312                 || ber_bvstrcasecmp( scope, &bv ) != 0)
2313         {
2314                 return(0);
2315         }
2316
2317         /* get the list of permissions clauses, bail if empty */
2318         if (aci_get_part(aci, 2, '#', &perms) <= 0)
2319                 return(0);
2320
2321         /* check if any permissions allow desired access */
2322         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
2323                 return(0);
2324
2325         /* see if we have a DN match */
2326         if (aci_get_part(aci, 3, '#', &bv) < 0)
2327                 return(0);
2328
2329         if (aci_get_part(aci, 4, '#', &sdn) < 0)
2330                 return(0);
2331
2332         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
2333                 struct berval ndn;
2334                 rc = 0;
2335                 if ( dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2336                         if ( dn_match( &op->o_ndn, &ndn ) ) {
2337                                 rc = 1;
2338                         }
2339                         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2340                 }
2341                 return (rc);
2342
2343         } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
2344                 return(1);
2345
2346         } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
2347                 if (dn_match(&op->o_ndn, &e->e_nname))
2348                         return(1);
2349
2350         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
2351                 Attribute *at;
2352                 AttributeDescription *ad = NULL;
2353                 const char *text;
2354
2355                 rc = slap_bv2ad( &sdn, &ad, &text );
2356
2357                 if( rc != LDAP_SUCCESS ) {
2358                         return 0;
2359                 }
2360
2361                 rc = 0;
2362
2363                 bv = op->o_ndn;
2364
2365                 for(at = attrs_find( e->e_attrs, ad );
2366                         at != NULL;
2367                         at = attrs_find( at->a_next, ad ) )
2368                 {
2369                         if (value_find_ex( ad,
2370                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2371                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2372                                 at->a_nvals,
2373                                 &bv, op->o_tmpmemctx) == 0 )
2374                         {
2375                                 rc = 1;
2376                                 break;
2377                         }
2378                 }
2379
2380                 return rc;
2381
2382
2383         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
2384                 if (aci_group_member(&sdn, &aci_bv_group_class,
2385                                 &aci_bv_group_attr, op, e, nmatch, matches))
2386                         return(1);
2387
2388         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
2389                 if (aci_group_member(&sdn, &aci_bv_role_class,
2390                                 &aci_bv_role_attr, op, e, nmatch, matches))
2391                         return(1);
2392
2393         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
2394                 if (aci_match_set(&sdn, op, e, 0))
2395                         return(1);
2396
2397         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
2398                 if (aci_match_set(&sdn, op, e, 1))
2399                         return(1);
2400
2401         }
2402
2403         return(0);
2404 }
2405
2406 #endif  /* SLAPD_ACI_ENABLED */
2407
2408 static int
2409 string_expand(
2410         struct berval   *bv,
2411         struct berval   *pat,
2412         char            *match,
2413         int             nmatch,
2414         regmatch_t      *matches)
2415 {
2416         ber_len_t       size;
2417         char   *sp;
2418         char   *dp;
2419         int     flag;
2420
2421         size = 0;
2422         bv->bv_val[0] = '\0';
2423         bv->bv_len--; /* leave space for lone $ */
2424
2425         flag = 0;
2426         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2427                 sp < pat->bv_val + pat->bv_len ; sp++ )
2428         {
2429                 /* did we previously see a $ */
2430                 if ( flag ) {
2431                         if ( flag == 1 && *sp == '$' ) {
2432                                 *dp++ = '$';
2433                                 size++;
2434                                 flag = 0;
2435
2436                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2437                                 flag = 2;
2438
2439                         } else if ( *sp >= '0' && *sp <= '9' ) {
2440                                 int     n;
2441                                 int     i;
2442                                 int     l;
2443
2444                                 n = *sp - '0';
2445
2446                                 if ( flag == 2 ) {
2447                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2448                                                 if ( *sp >= '0' && *sp <= '9' ) {
2449                                                         n = 10*n + ( *sp - '0' );
2450                                                 }
2451                                         }
2452
2453                                         if ( *sp != /*'{'*/ '}' ) {
2454                                                 /* FIXME: error */
2455                                                 return 1;
2456                                         }
2457                                 }
2458
2459                                 if ( n >= nmatch ) {
2460                                         /* FIXME: error */
2461                                         return 1;
2462                                 }
2463                                 
2464                                 *dp = '\0';
2465                                 i = matches[n].rm_so;
2466                                 l = matches[n].rm_eo; 
2467                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2468                                         *dp++ = match[i];
2469                                 }
2470                                 *dp = '\0';
2471
2472                                 flag = 0;
2473                         }
2474                 } else {
2475                         if (*sp == '$') {
2476                                 flag = 1;
2477                         } else {
2478                                 *dp++ = *sp;
2479                                 size++;
2480                         }
2481                 }
2482         }
2483
2484         if ( flag ) {
2485                 /* must have ended with a single $ */
2486                 *dp++ = '$';
2487                 size++;
2488         }
2489
2490         *dp = '\0';
2491         bv->bv_len = size;
2492
2493         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2494         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2495
2496         return 0;
2497 }
2498
2499 static int
2500 regex_matches(
2501         struct berval   *pat,           /* pattern to expand and match against */
2502         char            *str,           /* string to match against pattern */
2503         char            *buf,           /* buffer with $N expansion variables */
2504         int             nmatch, /* size of the matches array */
2505         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
2506 )
2507 {
2508         regex_t re;
2509         char newbuf[ACL_BUF_SIZE];
2510         struct berval bv;
2511         int     rc;
2512
2513         bv.bv_len = sizeof( newbuf ) - 1;
2514         bv.bv_val = newbuf;
2515
2516         if (str == NULL) {
2517                 str = "";
2518         };
2519
2520         string_expand( &bv, pat, buf, nmatch, matches );
2521         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2522         if ( rc ) {
2523                 char error[ACL_BUF_SIZE];
2524                 regerror( rc, &re, error, sizeof( error ) );
2525
2526                 Debug( LDAP_DEBUG_TRACE,
2527                     "compile( \"%s\", \"%s\") failed %s\n",
2528                         pat->bv_val, str, error );
2529                 return( 0 );
2530         }
2531
2532         rc = regexec( &re, str, 0, NULL, 0 );
2533         regfree( &re );
2534
2535         Debug( LDAP_DEBUG_TRACE,
2536             "=> regex_matches: string:   %s\n", str, 0, 0 );
2537         Debug( LDAP_DEBUG_TRACE,
2538             "=> regex_matches: rc: %d %s\n",
2539                 rc, !rc ? "matches" : "no matches", 0 );
2540         return( !rc );
2541 }
2542