]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
d422534d76c46ebcd0d12ae234b252bbb31eb634
[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 ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
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                         /*
700                          * NOTE: styles "anonymous", "users" and "self" 
701                          * have been moved to an enumeration, * whose value
702                          * is set in a_dn_style; however, the string
703                          * is maintaned in a_dn_pat.
704                          */
705                         if ( b->a_dn_style == ACL_STYLE_ANONYMOUS /* bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) */ ) {
706                                 if ( op->o_ndn.bv_len != 0 ) {
707                                         continue;
708                                 }
709
710                         } else if ( b->a_dn_style == ACL_STYLE_USERS /* bvmatch( &b->a_dn_pat, &aci_bv_users ) */ ) {
711                                 if ( op->o_ndn.bv_len == 0 ) {
712                                         continue;
713                                 }
714
715                         } else if ( b->a_dn_style == ACL_STYLE_SELF /* bvmatch( &b->a_dn_pat, &aci_bv_self ) */ ) {
716                                 if ( op->o_ndn.bv_len == 0 ) {
717                                         continue;
718                                 }
719                                 
720                                 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
721                                         continue;
722                                 }
723
724                         } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
725                                 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
726                                         int             tmp_nmatch;
727                                         regmatch_t      tmp_matches[2],
728                                                         *tmp_matchesp = tmp_matches;
729
730                                         int             rc = 0;
731
732                                         switch ( a->acl_dn_style ) {
733                                         case ACL_STYLE_REGEX:
734                                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
735                                                         tmp_matchesp = matches;
736                                                         tmp_nmatch = nmatch;
737                                                         break;
738                                                 }
739                                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
740
741                                         case ACL_STYLE_BASE:
742                                                 tmp_matches[0].rm_so = 0;
743                                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
744                                                 tmp_nmatch = 1;
745                                                 break;
746
747                                         case ACL_STYLE_ONE:
748                                         case ACL_STYLE_SUBTREE:
749                                         case ACL_STYLE_CHILDREN:
750                                                 tmp_matches[0].rm_so = 0;
751                                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
752                                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
753                                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
754                                                 tmp_nmatch = 2;
755                                                 break;
756
757                                         default:
758                                                 /* error */
759                                                 rc = 1;
760                                                 break;
761                                         }
762
763                                         if ( rc ) {
764                                                 continue;
765                                         }
766
767                                         if ( !regex_matches( &b->a_dn_pat,
768                                                 op->o_ndn.bv_val, e->e_ndn,
769                                                 tmp_nmatch, tmp_matchesp ) )
770                                         {
771                                                 continue;
772                                         }
773                                 }
774
775                         } else {
776                                 struct berval pat;
777                                 int got_match = 0;
778
779                                 if ( e->e_dn == NULL )
780                                         continue;
781
782                                 if ( b->a_dn_expand ) {
783                                         struct berval   bv;
784                                         char            buf[ACL_BUF_SIZE];
785                                         
786                                         int             tmp_nmatch;
787                                         regmatch_t      tmp_matches[2],
788                                                         *tmp_matchesp = tmp_matches;
789
790                                         int             rc = 0;
791
792                                         bv.bv_len = sizeof( buf ) - 1;
793                                         bv.bv_val = buf;
794
795                                         switch ( a->acl_dn_style ) {
796                                         case ACL_STYLE_REGEX:
797                                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
798                                                         tmp_matchesp = matches;
799                                                         tmp_nmatch = nmatch;
800                                                         break;
801                                                 }
802                                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
803
804                                         case ACL_STYLE_BASE:
805                                                 tmp_matches[0].rm_so = 0;
806                                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
807                                                 tmp_nmatch = 1;
808                                                 break;
809
810                                         case ACL_STYLE_ONE:
811                                         case ACL_STYLE_SUBTREE:
812                                         case ACL_STYLE_CHILDREN:
813                                                 tmp_matches[0].rm_so = 0;
814                                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
815                                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
816                                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
817                                                 tmp_nmatch = 2;
818                                                 break;
819
820                                         default:
821                                                 /* error */
822                                                 rc = 1;
823                                                 break;
824                                         }
825
826                                         if ( rc ) {
827                                                 continue;
828                                         }
829
830                                         if ( string_expand( &bv, &b->a_dn_pat, 
831                                                         e->e_nname.bv_val,
832                                                         tmp_nmatch, tmp_matchesp ) )
833                                         {
834                                                 continue;
835                                         }
836                                         
837                                         if ( dnNormalize(0, NULL, NULL, &bv,
838                                                         &pat, op->o_tmpmemctx )
839                                                         != LDAP_SUCCESS )
840                                         {
841                                                 /* did not expand to a valid dn */
842                                                 continue;
843                                         }
844
845                                 } else {
846                                         pat = b->a_dn_pat;
847                                 }
848
849                                 patlen = pat.bv_len;
850                                 odnlen = op->o_ndn.bv_len;
851                                 if ( odnlen < patlen ) {
852                                         goto dn_match_cleanup;
853
854                                 }
855
856                                 if ( b->a_dn_style == ACL_STYLE_BASE ) {
857                                         /* base dn -- entire object DN must match */
858                                         if ( odnlen != patlen ) {
859                                                 goto dn_match_cleanup;
860                                         }
861
862                                 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
863                                         int rdnlen = -1;
864
865                                         if ( odnlen <= patlen ) {
866                                                 goto dn_match_cleanup;
867                                         }
868
869                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
870                                                 goto dn_match_cleanup;
871                                         }
872
873                                         rdnlen = dn_rdnlen( NULL, &op->o_ndn );
874                                         if ( rdnlen != odnlen - patlen - 1 ) {
875                                                 goto dn_match_cleanup;
876                                         }
877
878                                 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
879                                         if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
880                                                 goto dn_match_cleanup;
881                                         }
882
883                                 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
884                                         if ( odnlen <= patlen ) {
885                                                 goto dn_match_cleanup;
886                                         }
887
888                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
889                                                 goto dn_match_cleanup;
890                                         }
891                                 }
892
893                                 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
894
895 dn_match_cleanup:;
896                                 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
897                                         slap_sl_free( pat.bv_val, op->o_tmpmemctx );
898                                 }
899
900                                 if ( !got_match ) {
901                                         continue;
902                                 }
903                         }
904                 }
905
906                 if ( b->a_sockurl_pat.bv_len ) {
907                         if ( ! op->o_conn->c_listener ) {
908                                 continue;
909                         }
910                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
911                                 b->a_sockurl_pat.bv_val, 0, 0 );
912
913                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
914                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
915                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
916                                                         e->e_ndn, nmatch, matches ) ) 
917                                         {
918                                                 continue;
919                                         }
920
921                                 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
922                                         struct berval   bv;
923                                         char buf[ACL_BUF_SIZE];
924
925                                         bv.bv_len = sizeof( buf ) - 1;
926                                         bv.bv_val = buf;
927                                         if ( string_expand( &bv, &b->a_sockurl_pat,
928                                                         e->e_ndn, nmatch, matches ) )
929                                         {
930                                                 continue;
931                                         }
932
933                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
934                                         {
935                                                 continue;
936                                         }
937
938                                 } else {
939                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
940                                         {
941                                                 continue;
942                                         }
943                                 }
944                         }
945                 }
946
947                 if ( b->a_domain_pat.bv_len ) {
948                         if ( !op->o_conn->c_peer_domain.bv_val ) {
949                                 continue;
950                         }
951                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
952                                 b->a_domain_pat.bv_val, 0, 0 );
953                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
954                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
955                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
956                                                         e->e_ndn, nmatch, matches ) ) 
957                                         {
958                                                 continue;
959                                         }
960                                 } else {
961                                         char buf[ACL_BUF_SIZE];
962
963                                         struct berval   cmp = op->o_conn->c_peer_domain;
964                                         struct berval   pat = b->a_domain_pat;
965
966                                         if ( b->a_domain_expand ) {
967                                                 struct berval bv;
968
969                                                 bv.bv_len = sizeof(buf) - 1;
970                                                 bv.bv_val = buf;
971
972                                                 if ( string_expand(&bv, &b->a_domain_pat,
973                                                                 e->e_ndn, nmatch, matches) )
974                                                 {
975                                                         continue;
976                                                 }
977                                                 pat = bv;
978                                         }
979
980                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
981                                                 int offset = cmp.bv_len - pat.bv_len;
982                                                 if ( offset < 0 ) {
983                                                         continue;
984                                                 }
985
986                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
987                                                         continue;
988                                                 }
989
990                                                 /* trim the domain */
991                                                 cmp.bv_val = &cmp.bv_val[ offset ];
992                                                 cmp.bv_len -= offset;
993                                         }
994                                         
995                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
996                                                 continue;
997                                         }
998                                 }
999                         }
1000                 }
1001
1002                 if ( b->a_peername_pat.bv_len ) {
1003                         if ( !op->o_conn->c_peer_name.bv_val ) {
1004                                 continue;
1005                         }
1006                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1007                                 b->a_peername_pat.bv_val, 0, 0 );
1008                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1009                                 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1010                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1011                                                         e->e_ndn, nmatch, matches ) ) 
1012                                         {
1013                                                 continue;
1014                                         }
1015
1016                                 } else {
1017                                         /* try exact match */
1018                                         if ( b->a_peername_style == ACL_STYLE_BASE ) {
1019                                                 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1020                                                         continue;
1021                                                 }
1022
1023                                         } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1024                                                 struct berval   bv;
1025                                                 char buf[ACL_BUF_SIZE];
1026
1027                                                 bv.bv_len = sizeof( buf ) - 1;
1028                                                 bv.bv_val = buf;
1029                                                 if ( string_expand( &bv, &b->a_peername_pat,
1030                                                                 e->e_ndn, nmatch, matches ) )
1031                                                 {
1032                                                         continue;
1033                                                 }
1034
1035                                                 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1036                                                         continue;
1037                                                 }
1038
1039                                         /* extract IP and try exact match */
1040                                         } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1041                                                 char            *port;
1042                                                 char            buf[] = "255.255.255.255";
1043                                                 struct berval   ip;
1044                                                 unsigned long   addr;
1045                                                 int             port_number = -1;
1046                                                 
1047                                                 if ( strncasecmp( op->o_conn->c_peer_name.bv_val, 
1048                                                                         aci_bv_ip_eq.bv_val, aci_bv_ip_eq.bv_len ) != 0 ) 
1049                                                         continue;
1050
1051                                                 ip.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_ip_eq.bv_len;
1052                                                 ip.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_ip_eq.bv_len;
1053
1054                                                 port = strrchr( ip.bv_val, ':' );
1055                                                 if ( port ) {
1056                                                         char    *next;
1057                                                         
1058                                                         ip.bv_len = port - ip.bv_val;
1059                                                         ++port;
1060                                                         port_number = strtol( port, &next, 10 );
1061                                                         if ( next[0] != '\0' )
1062                                                                 continue;
1063                                                 }
1064                                                 
1065                                                 /* the port check can be anticipated here */
1066                                                 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1067                                                         continue;
1068                                                 
1069                                                 /* address longer than expected? */
1070                                                 if ( ip.bv_len >= sizeof(buf) )
1071                                                         continue;
1072
1073                                                 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1074                                                 buf[ ip.bv_len ] = '\0';
1075
1076                                                 addr = inet_addr( buf );
1077
1078                                                 /* unable to convert? */
1079                                                 if ( addr == (unsigned long)(-1) )
1080                                                         continue;
1081
1082                                                 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1083                                                         continue;
1084
1085 #ifdef LDAP_PF_LOCAL
1086                                         /* extract path and try exact match */
1087                                         } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1088                                                 struct berval path;
1089                                                 
1090                                                 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1091                                                                         aci_bv_path_eq.bv_val, aci_bv_path_eq.bv_len ) != 0 )
1092                                                         continue;
1093
1094                                                 path.bv_val = op->o_conn->c_peer_name.bv_val + aci_bv_path_eq.bv_len;
1095                                                 path.bv_len = op->o_conn->c_peer_name.bv_len - aci_bv_path_eq.bv_len;
1096
1097                                                 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1098                                                         continue;
1099
1100 #endif /* LDAP_PF_LOCAL */
1101
1102                                         /* exact match (very unlikely...) */
1103                                         } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1104                                                         continue;
1105                                         }
1106                                 }
1107                         }
1108                 }
1109
1110                 if ( b->a_sockname_pat.bv_len ) {
1111                         if ( !op->o_conn->c_sock_name.bv_val ) {
1112                                 continue;
1113                         }
1114                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1115                                 b->a_sockname_pat.bv_val, 0, 0 );
1116                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1117                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1118                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1119                                                         e->e_ndn, nmatch, matches ) ) 
1120                                         {
1121                                                 continue;
1122                                         }
1123
1124                                 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1125                                         struct berval   bv;
1126                                         char buf[ACL_BUF_SIZE];
1127
1128                                         bv.bv_len = sizeof( buf ) - 1;
1129                                         bv.bv_val = buf;
1130                                         if ( string_expand( &bv, &b->a_sockname_pat,
1131                                                         e->e_ndn, nmatch, matches ) )
1132                                         {
1133                                                 continue;
1134                                         }
1135
1136                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1137                                                 continue;
1138                                         }
1139
1140                                 } else {
1141                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1142                                                 continue;
1143                                         }
1144                                 }
1145                         }
1146                 }
1147
1148                 if ( b->a_dn_at != NULL ) {
1149                         Attribute       *at;
1150                         struct berval   bv;
1151                         int rc, match = 0;
1152                         const char *text;
1153                         const char *attr = b->a_dn_at->ad_cname.bv_val;
1154
1155                         assert( attr != NULL );
1156
1157                         if ( op->o_ndn.bv_len == 0 ) {
1158                                 continue;
1159                         }
1160
1161                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
1162                                 attr, 0, 0);
1163                         bv = op->o_ndn;
1164
1165                         /* see if asker is listed in dnattr */
1166                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
1167                                 at != NULL;
1168                                 at = attrs_find( at->a_next, b->a_dn_at ) )
1169                         {
1170                                 if( value_find_ex( b->a_dn_at,
1171                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1172                                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1173                                         at->a_nvals,
1174                                         &bv, op->o_tmpmemctx ) == 0 )
1175                                 {
1176                                         /* found it */
1177                                         match = 1;
1178                                         break;
1179                                 }
1180                         }
1181
1182                         if( match ) {
1183                                 /* have a dnattr match. if this is a self clause then
1184                                  * the target must also match the op dn.
1185                                  */
1186                                 if ( b->a_dn_self ) {
1187                                         /* check if the target is an attribute. */
1188                                         if ( val == NULL ) continue;
1189
1190                                         /* target is attribute, check if the attribute value
1191                                          * is the op dn.
1192                                          */
1193                                         rc = value_match( &match, b->a_dn_at,
1194                                                 b->a_dn_at->ad_type->sat_equality, 0,
1195                                                 val, &bv, &text );
1196                                         /* on match error or no match, fail the ACL clause */
1197                                         if (rc != LDAP_SUCCESS || match != 0 )
1198                                                 continue;
1199                                 }
1200                         } else {
1201                                 /* no dnattr match, check if this is a self clause */
1202                                 if ( ! b->a_dn_self )
1203                                         continue;
1204
1205                                 ACL_RECORD_VALUE_STATE;
1206                                 
1207                                 /* this is a self clause, check if the target is an
1208                                  * attribute.
1209                                  */
1210                                 if ( val == NULL )
1211                                         continue;
1212
1213                                 /* target is attribute, check if the attribute value
1214                                  * is the op dn.
1215                                  */
1216                                 rc = value_match( &match, b->a_dn_at,
1217                                         b->a_dn_at->ad_type->sat_equality, 0,
1218                                         val, &bv, &text );
1219
1220                                 /* on match error or no match, fail the ACL clause */
1221                                 if (rc != LDAP_SUCCESS || match != 0 )
1222                                         continue;
1223                         }
1224                 }
1225
1226                 if ( b->a_group_pat.bv_len ) {
1227                         struct berval bv;
1228                         struct berval ndn = BER_BVNULL;
1229                         int rc;
1230
1231                         if ( op->o_ndn.bv_len == 0 ) {
1232                                 continue;
1233                         }
1234
1235                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1236                          * entry with objectclass group* and we test to see if odn is one of
1237                          * the values in the attribute group
1238                          */
1239                         /* see if asker is listed in dnattr */
1240                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1241                                 char            buf[ACL_BUF_SIZE];
1242                                 int             tmp_nmatch;
1243                                 regmatch_t      tmp_matches[2],
1244                                                 *tmp_matchesp = tmp_matches;
1245
1246                                 bv.bv_len = sizeof(buf) - 1;
1247                                 bv.bv_val = buf;
1248
1249                                 rc = 0;
1250
1251                                 switch ( a->acl_dn_style ) {
1252                                 case ACL_STYLE_REGEX:
1253                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1254                                                 tmp_matchesp = matches;
1255                                                 tmp_nmatch = nmatch;
1256                                                 break;
1257                                         }
1258
1259                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1260                                 case ACL_STYLE_BASE:
1261                                         tmp_matches[0].rm_so = 0;
1262                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1263                                         tmp_nmatch = 1;
1264                                         break;
1265
1266                                 case ACL_STYLE_ONE:
1267                                 case ACL_STYLE_SUBTREE:
1268                                 case ACL_STYLE_CHILDREN:
1269                                         tmp_matches[0].rm_so = 0;
1270                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1271                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1272                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1273                                         tmp_nmatch = 2;
1274                                         break;
1275
1276                                 default:
1277                                         /* error */
1278                                         rc = 1;
1279                                         break;
1280                                 }
1281
1282                                 if ( rc ) {
1283                                         continue;
1284                                 }
1285                                 
1286                                 if ( string_expand( &bv, &b->a_group_pat,
1287                                                 e->e_nname.bv_val,
1288                                                 tmp_nmatch, tmp_matchesp ) )
1289                                 {
1290                                         continue;
1291                                 }
1292
1293                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1294                                                 op->o_tmpmemctx ) != LDAP_SUCCESS )
1295                                 {
1296                                         /* did not expand to a valid dn */
1297                                         continue;
1298                                 }
1299
1300                                 bv = ndn;
1301
1302                         } else {
1303                                 bv = b->a_group_pat;
1304                         }
1305
1306                         rc = backend_group( op, e, &bv, &op->o_ndn,
1307                                 b->a_group_oc, b->a_group_at );
1308
1309                         if ( ndn.bv_val ) {
1310                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1311                         }
1312
1313                         if ( rc != 0 ) {
1314                                 continue;
1315                         }
1316                 }
1317
1318                 if ( b->a_set_pat.bv_len != 0 ) {
1319                         struct berval bv;
1320                         char buf[ACL_BUF_SIZE];
1321
1322                         if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1323                                 int             tmp_nmatch;
1324                                 regmatch_t      tmp_matches[2],
1325                                                 *tmp_matchesp = tmp_matches;
1326                                 int             rc = 0;
1327
1328                                 bv.bv_len = sizeof(buf) - 1;
1329                                 bv.bv_val = buf;
1330
1331                                 rc = 0;
1332
1333                                 switch ( a->acl_dn_style ) {
1334                                 case ACL_STYLE_REGEX:
1335                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1336                                                 tmp_matchesp = matches;
1337                                                 tmp_nmatch = nmatch;
1338                                                 break;
1339                                         }
1340
1341                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1342                                 case ACL_STYLE_BASE:
1343                                         tmp_matches[0].rm_so = 0;
1344                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1345                                         tmp_nmatch = 1;
1346                                         break;
1347
1348                                 case ACL_STYLE_ONE:
1349                                 case ACL_STYLE_SUBTREE:
1350                                 case ACL_STYLE_CHILDREN:
1351                                         tmp_matches[0].rm_so = 0;
1352                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1353                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1354                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1355                                         tmp_nmatch = 2;
1356                                         break;
1357
1358                                 default:
1359                                         /* error */
1360                                         rc = 1;
1361                                         break;
1362                                 }
1363
1364                                 if ( rc ) {
1365                                         continue;
1366                                 }
1367                                 
1368                                 if ( string_expand( &bv, &b->a_set_pat,
1369                                                 e->e_nname.bv_val,
1370                                                 tmp_nmatch, tmp_matchesp ) )
1371                                 {
1372                                         continue;
1373                                 }
1374
1375                         }else{
1376                                 bv = b->a_set_pat;
1377                         }
1378                         if (aci_match_set( &bv, op, e, 0 ) == 0) {
1379                                 continue;
1380                         }
1381                 }
1382
1383                 if ( b->a_authz.sai_ssf ) {
1384                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1385                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1386                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1387                                 continue;
1388                         }
1389                 }
1390
1391                 if ( b->a_authz.sai_transport_ssf ) {
1392                         Debug( LDAP_DEBUG_ACL,
1393                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1394                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1395                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1396                                 continue;
1397                         }
1398                 }
1399
1400                 if ( b->a_authz.sai_tls_ssf ) {
1401                         Debug( LDAP_DEBUG_ACL,
1402                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1403                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1404                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1405                                 continue;
1406                         }
1407                 }
1408
1409                 if ( b->a_authz.sai_sasl_ssf ) {
1410                         Debug( LDAP_DEBUG_ACL,
1411                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1412                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1413                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1414                                 continue;
1415                         }
1416                 }
1417
1418 #ifdef SLAPD_ACI_ENABLED
1419                 if ( b->a_aci_at != NULL ) {
1420                         Attribute       *at;
1421                         slap_access_t grant, deny, tgrant, tdeny;
1422                         struct berval parent_ndn, old_parent_ndn;
1423                         BerVarray bvals = NULL;
1424                         int ret,stop;
1425
1426                         /* this case works different from the others above.
1427                          * since aci's themselves give permissions, we need
1428                          * to first check b->a_access_mask, the ACL's access level.
1429                          */
1430
1431                         if ( e->e_nname.bv_len == 0 ) {
1432                                 /* no ACIs in the root DSE */
1433                                 continue;
1434                         }
1435
1436                         /* first check if the right being requested
1437                          * is allowed by the ACL clause.
1438                          */
1439                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1440                                 continue;
1441                         }
1442                         /* start out with nothing granted, nothing denied */
1443                         ACL_INIT(tgrant);
1444                         ACL_INIT(tdeny);
1445
1446                         /* get the aci attribute */
1447                         at = attr_find( e->e_attrs, b->a_aci_at );
1448                         if ( at != NULL ) {
1449 #if 0
1450                                 /* FIXME: this breaks acl caching;
1451                                  * see also ACL_RECORD_VALUE_STATE below */
1452                                 ACL_RECORD_VALUE_STATE;
1453 #endif
1454                                 /* the aci is an multi-valued attribute.  The
1455                                 * rights are determined by OR'ing the individual
1456                                 * rights given by the acis.
1457                                 */
1458                                 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
1459                                         if (aci_mask( op,
1460                                                 e, desc, val,
1461                                                 &at->a_nvals[i],
1462                                                 nmatch, matches,
1463                                                 &grant, &deny,  &aci_bv_entry ) != 0)
1464                                         {
1465                                                 tgrant |= grant;
1466                                                 tdeny |= deny;
1467                                         }
1468                                 }
1469                                 Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
1470                                           accessmask2str(tgrant,accessmaskbuf), 
1471                                           accessmask2str(tdeny, accessmaskbuf1), 0);
1472
1473                         }
1474                         /* If the entry level aci didn't contain anything valid for the 
1475                          * current operation, climb up the tree and evaluate the
1476                          * acis with scope set to subtree
1477                          */
1478                         if( (tgrant == ACL_PRIV_NONE) && (tdeny == ACL_PRIV_NONE) ){
1479                                 dnParent(&(e->e_nname), &parent_ndn);
1480                                 while ( parent_ndn.bv_val != old_parent_ndn.bv_val ){
1481                                         old_parent_ndn = parent_ndn;
1482                                         Debug(LDAP_DEBUG_ACL, "checking ACI of %s\n", parent_ndn.bv_val, 0, 0);
1483                                         ret = backend_attribute(op, NULL, &parent_ndn, b->a_aci_at, &bvals, ACL_AUTH);
1484                                         switch(ret){
1485                                         case LDAP_SUCCESS :
1486                                                 stop = 0;
1487                                                 if (!bvals){
1488                                                         break;
1489                                                 }
1490
1491                                                 for( i = 0; bvals[i].bv_val != NULL; i++){
1492 #if 0
1493                                                         /* FIXME: this breaks acl caching;
1494                                                          * see also ACL_RECORD_VALUE_STATE above */
1495                                                         ACL_RECORD_VALUE_STATE;
1496 #endif
1497                                                         if (aci_mask(op, e, desc, val, &bvals[i],
1498                                                                         nmatch, matches,
1499                                                                         &grant, &deny, &aci_bv_children) != 0)
1500                                                         {
1501                                                                 tgrant |= grant;
1502                                                                 tdeny |= deny;
1503                                                                 /* evaluation stops as soon as either a "deny" or a 
1504                                                                  * "grant" directive matches.
1505                                                                  */
1506                                                                 if( (tgrant != ACL_PRIV_NONE) || (tdeny != ACL_PRIV_NONE) ){
1507                                                                         stop = 1;
1508                                                                 }
1509                                                         }
1510                                                         Debug(LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n", 
1511                                                                 accessmask2str(tgrant,accessmaskbuf),
1512                                                                 accessmask2str(tdeny, accessmaskbuf1), 0);
1513                                                 }
1514                                                 break;
1515
1516                                         case LDAP_NO_SUCH_ATTRIBUTE:
1517                                                 /* just go on if the aci-Attribute is not present in
1518                                                  * the current entry 
1519                                                  */
1520                                                 Debug(LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0);
1521                                                 stop = 0;
1522                                                 break;
1523
1524                                         case LDAP_NO_SUCH_OBJECT:
1525                                                 /* We have reached the base object */
1526                                                 Debug(LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0);
1527                                                 stop = 1;
1528                                                 break;
1529
1530                                         default:
1531                                                 stop = 1;
1532                                                 break;
1533                                         }
1534                                         if (stop){
1535                                                 break;
1536                                         }
1537                                         dnParent(&old_parent_ndn, &parent_ndn);
1538                                 }
1539                         }
1540
1541
1542                         /* remove anything that the ACL clause does not allow */
1543                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1544                         tdeny &= ACL_PRIV_MASK;
1545
1546                         /* see if we have anything to contribute */
1547                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1548                                 continue;
1549                         }
1550
1551                         /* this could be improved by changing acl_mask so that it can deal with
1552                          * by clauses that return grant/deny pairs.  Right now, it does either
1553                          * additive or subtractive rights, but not both at the same time.  So,
1554                          * we need to combine the grant/deny pair into a single rights mask in
1555                          * a smart way:  if either grant or deny is "empty", then we use the
1556                          * opposite as is, otherwise we remove any denied rights from the grant
1557                          * rights mask and construct an additive mask.
1558                          */
1559                         if (ACL_IS_INVALID(tdeny)) {
1560                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1561
1562                         } else if (ACL_IS_INVALID(tgrant)) {
1563                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1564
1565                         } else {
1566                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1567                         }
1568
1569                 } else
1570 #endif
1571                 {
1572                         modmask = b->a_access_mask;
1573                 }
1574
1575                 Debug( LDAP_DEBUG_ACL,
1576                         "<= acl_mask: [%d] applying %s (%s)\n",
1577                         i, accessmask2str( modmask, accessmaskbuf ), 
1578                         b->a_type == ACL_CONTINUE
1579                                 ? "continue"
1580                                 : b->a_type == ACL_BREAK
1581                                         ? "break"
1582                                         : "stop" );
1583                 /* save old mask */
1584                 oldmask = *mask;
1585
1586                 if( ACL_IS_ADDITIVE(modmask) ) {
1587                         /* add privs */
1588                         ACL_PRIV_SET( *mask, modmask );
1589
1590                         /* cleanup */
1591                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1592
1593                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1594                         /* substract privs */
1595                         ACL_PRIV_CLR( *mask, modmask );
1596
1597                         /* cleanup */
1598                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1599
1600                 } else {
1601                         /* assign privs */
1602                         *mask = modmask;
1603                 }
1604
1605                 Debug( LDAP_DEBUG_ACL,
1606                         "<= acl_mask: [%d] mask: %s\n",
1607                         i, accessmask2str(*mask, accessmaskbuf), 0 );
1608
1609                 if( b->a_type == ACL_CONTINUE ) {
1610                         continue;
1611
1612                 } else if ( b->a_type == ACL_BREAK ) {
1613                         return ACL_BREAK;
1614
1615                 } else {
1616                         return ACL_STOP;
1617                 }
1618         }
1619
1620         /* implicit "by * none" clause */
1621         ACL_INIT(*mask);
1622
1623         Debug( LDAP_DEBUG_ACL,
1624                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1625                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1626         return ACL_STOP;
1627 }
1628
1629 /*
1630  * acl_check_modlist - check access control on the given entry to see if
1631  * it allows the given modifications by the user associated with op.
1632  * returns      1       if mods allowed ok
1633  *              0       mods not allowed
1634  */
1635
1636 int
1637 acl_check_modlist(
1638         Operation       *op,
1639         Entry   *e,
1640         Modifications   *mlist
1641 )
1642 {
1643         struct berval *bv;
1644         AccessControlState state = ACL_STATE_INIT;
1645         Backend *be;
1646         int be_null = 0;
1647         int ret = 1; /* default is access allowed */
1648
1649         be = op->o_bd;
1650         if ( be == NULL ) {
1651                 be = &backends[0];
1652                 be_null = 1;
1653                 op->o_bd = be;
1654         }
1655         assert( be != NULL );
1656
1657         /* short circuit root database access */
1658         if ( be_isroot( op ) ) {
1659                 Debug( LDAP_DEBUG_ACL,
1660                         "<= acl_access_allowed: granted to database root\n",
1661                     0, 0, 0 );
1662                 goto done;
1663         }
1664
1665         /* use backend default access if no backend acls */
1666         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1667                 Debug( LDAP_DEBUG_ACL,
1668                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1669                         access2str( ACL_WRITE ),
1670                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1671                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1672                 goto done;
1673         }
1674
1675         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1676                 /*
1677                  * no-user-modification operational attributes are ignored
1678                  * by ACL_WRITE checking as any found here are not provided
1679                  * by the user
1680                  */
1681                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1682                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1683                                 " modify access granted\n",
1684                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1685                         continue;
1686                 }
1687
1688                 switch ( mlist->sml_op ) {
1689                 case LDAP_MOD_REPLACE:
1690                         /*
1691                          * We must check both permission to delete the whole
1692                          * attribute and permission to add the specific attributes.
1693                          * This prevents abuse from selfwriters.
1694                          */
1695                         if ( ! access_allowed( op, e,
1696                                 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1697                         {
1698                                 ret = 0;
1699                                 goto done;
1700                         }
1701
1702                         if ( mlist->sml_values == NULL ) break;
1703
1704                         /* fall thru to check value to add */
1705
1706                 case LDAP_MOD_ADD:
1707                         assert( mlist->sml_values != NULL );
1708
1709                         for ( bv = mlist->sml_nvalues
1710                                         ? mlist->sml_nvalues : mlist->sml_values;
1711                                 bv->bv_val != NULL; bv++ )
1712                         {
1713                                 if ( ! access_allowed( op, e,
1714                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1715                                 {
1716                                         ret = 0;
1717                                         goto done;
1718                                 }
1719                         }
1720                         break;
1721
1722                 case LDAP_MOD_DELETE:
1723                         if ( mlist->sml_values == NULL ) {
1724                                 if ( ! access_allowed( op, e,
1725                                         mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1726                                 {
1727                                         ret = 0;
1728                                         goto done;
1729                                 }
1730                                 break;
1731                         }
1732                         for ( bv = mlist->sml_nvalues
1733                                         ? mlist->sml_nvalues : mlist->sml_values;
1734                                 bv->bv_val != NULL; bv++ )
1735                         {
1736                                 if ( ! access_allowed( op, e,
1737                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1738                                 {
1739                                         ret = 0;
1740                                         goto done;
1741                                 }
1742                         }
1743                         break;
1744
1745                 case SLAP_MOD_SOFTADD:
1746                         /* allow adding attribute via modrdn thru */
1747                         break;
1748
1749                 default:
1750                         assert( 0 );
1751                         /* not reached */
1752                         ret = 0;
1753                         break;
1754                 }
1755         }
1756
1757 done:
1758         if (be_null) op->o_bd = NULL;
1759         return( ret );
1760 }
1761
1762 static int
1763 aci_get_part(
1764         struct berval *list,
1765         int ix,
1766         char sep,
1767         struct berval *bv )
1768 {
1769         int len;
1770         char *p;
1771
1772         if (bv) {
1773                 BER_BVZERO( bv );
1774         }
1775         len = list->bv_len;
1776         p = list->bv_val;
1777         while (len >= 0 && --ix >= 0) {
1778                 while (--len >= 0 && *p++ != sep) ;
1779         }
1780         while (len >= 0 && *p == ' ') {
1781                 len--;
1782                 p++;
1783         }
1784         if (len < 0)
1785                 return(-1);
1786
1787         if (!bv)
1788                 return(0);
1789
1790         bv->bv_val = p;
1791         while (--len >= 0 && *p != sep) {
1792                 bv->bv_len++;
1793                 p++;
1794         }
1795         while (bv->bv_len > 0 && *--p == ' ')
1796                 bv->bv_len--;
1797         return(bv->bv_len);
1798 }
1799
1800 typedef struct aci_set_gather_t {
1801         SetCookie               *cookie;
1802         BerVarray               bvals;
1803 } aci_set_gather_t;
1804
1805 static int
1806 aci_set_cb_gather( Operation *op, SlapReply *rs )
1807 {
1808         aci_set_gather_t        *p = (aci_set_gather_t *)op->o_callback->sc_private;
1809         
1810         if ( rs->sr_type == REP_SEARCH ) {
1811                 BerValue        bvals[ 2 ];
1812                 BerVarray       bvalsp = NULL;
1813                 int             j;
1814
1815                 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
1816                         AttributeDescription    *desc = rs->sr_attrs[ j ].an_desc;
1817                         
1818                         if ( desc == slap_schema.si_ad_entryDN ) {
1819                                 bvalsp = bvals;
1820                                 bvals[ 0 ] = rs->sr_entry->e_nname;
1821                                 BER_BVZERO( &bvals[ 1 ] );
1822
1823                         } else {
1824                                 Attribute       *a;
1825
1826                                 a = attr_find( rs->sr_entry->e_attrs, desc );
1827                                 if ( a != NULL ) {
1828                                         int     i;
1829
1830                                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
1831                                                 ;
1832
1833                                         bvalsp = a->a_nvals;
1834                                 }
1835                         }
1836                 }
1837
1838                 if ( bvals ) {
1839                         p->bvals = slap_set_join( p->cookie, p->bvals,
1840                                         ( '|' | SLAP_SET_RREF ), bvalsp );
1841                 }
1842
1843         } else {
1844                 assert( rs->sr_type == REP_RESULT );
1845         }
1846
1847         return 0;
1848 }
1849
1850 BerVarray
1851 aci_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
1852 {
1853         AciSetCookie            *cp = (AciSetCookie *)cookie;
1854         int                     rc = 0;
1855         LDAPURLDesc             *ludp = NULL;
1856         Operation               op2 = { 0 };
1857         SlapReply               rs = {REP_RESULT};
1858         AttributeName           anlist[ 2 ], *anlistp = NULL;
1859         int                     nattrs = 0;
1860         slap_callback           cb = { NULL, aci_set_cb_gather, NULL, NULL };
1861         aci_set_gather_t        p = { 0 };
1862         const char              *text = NULL;
1863         static struct berval    defaultFilter_bv = BER_BVC( "(objectClass=*)" );
1864
1865         /* this routine needs to return the bervals instead of
1866          * plain strings, since syntax is not known.  It should
1867          * also return the syntax or some "comparison cookie".
1868          */
1869         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
1870                 return aci_set_gather2( cookie, name, desc );
1871         }
1872
1873         rc = ldap_url_parse( name->bv_val, &ludp );
1874         if ( rc != LDAP_URL_SUCCESS ) {
1875                 rc = LDAP_PROTOCOL_ERROR;
1876                 goto url_done;
1877         }
1878         
1879         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
1880         {
1881                 /* host part must be empty */
1882                 /* extensions parts must be empty */
1883                 rc = LDAP_PROTOCOL_ERROR;
1884                 goto url_done;
1885         }
1886
1887         /* Grab the searchbase and see if an appropriate database can be found */
1888         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
1889         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
1890                         &op2.o_req_ndn, cp->op->o_tmpmemctx );
1891         BER_BVZERO( &op2.o_req_dn );
1892         if ( rc != LDAP_SUCCESS ) {
1893                 goto url_done;
1894         }
1895
1896         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
1897         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
1898                 rc = LDAP_NO_SUCH_OBJECT;
1899                 goto url_done;
1900         }
1901
1902         /* Grab the filter */
1903         if ( ludp->lud_filter ) {
1904                 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
1905                                 cp->op->o_tmpmemctx );
1906                 
1907         } else {
1908                 op2.ors_filterstr = defaultFilter_bv;
1909         }
1910
1911         op2.ors_filter = str2filter_x( cp->op, op2.ors_filterstr.bv_val );
1912         if ( op2.ors_filter == NULL ) {
1913                 rc = LDAP_PROTOCOL_ERROR;
1914                 goto url_done;
1915         }
1916
1917         /* Grab the scope */
1918         op2.ors_scope = ludp->lud_scope;
1919
1920         /* Grap the attributes */
1921         if ( ludp->lud_attrs ) {
1922                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
1923                         ;
1924
1925                 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
1926                                 cp->op->o_tmpmemctx );
1927
1928                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
1929                         ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
1930                         anlistp[ nattrs ].an_desc = NULL;
1931                         rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
1932                                         &anlistp[ nattrs ].an_desc, &text );
1933                         if ( rc != LDAP_SUCCESS ) {
1934                                 goto url_done;
1935                         }
1936                 }
1937
1938         } else {
1939                 anlistp = anlist;
1940         }
1941
1942         anlistp[ nattrs ].an_name = desc->ad_cname;
1943         anlistp[ nattrs ].an_desc = desc;
1944
1945         BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
1946         
1947         p.cookie = cookie;
1948         
1949         op2.o_tag = LDAP_REQ_SEARCH;
1950         op2.o_protocol = LDAP_VERSION3;
1951         op2.o_ndn = op2.o_bd->be_rootndn;
1952         op2.o_callback = &cb;
1953         op2.o_time = slap_get_time();
1954         op2.o_do_not_cache = 1;
1955         op2.o_is_auth_check = 0;
1956         op2.o_threadctx = cp->op->o_threadctx;
1957         op2.o_tmpmemctx = cp->op->o_tmpmemctx;
1958         op2.o_tmpmfuncs = cp->op->o_tmpmfuncs;
1959 #ifdef LDAP_SLAPI
1960         op2.o_pb = cp->op->o_pb;
1961 #endif
1962         op2.o_conn = cp->op->o_conn;
1963         op2.o_connid = cp->op->o_connid;
1964         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->op->o_tmpmemctx );
1965         op2.ors_slimit = SLAP_NO_LIMIT;
1966         op2.ors_tlimit = SLAP_NO_LIMIT;
1967         op2.ors_attrs = anlistp;
1968         op2.ors_attrsonly = 0;
1969         op2.o_sync_slog_size = -1;
1970
1971         cb.sc_private = &p;
1972
1973         rc = op2.o_bd->be_search( &op2, &rs );
1974         if ( rc != 0 ) {
1975                 goto url_done;
1976         }
1977
1978 url_done:;
1979         if ( op2.ors_filter ) {
1980                 filter_free_x( cp->op, op2.ors_filter );
1981         }
1982         if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
1983                 slap_sl_free( op2.o_req_ndn.bv_val, cp->op->o_tmpmemctx );
1984         }
1985         if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
1986                 slap_sl_free( op2.o_req_dn.bv_val, cp->op->o_tmpmemctx );
1987         }
1988         if ( ludp ) {
1989                 ldap_free_urldesc( ludp );
1990         }
1991         if ( anlistp && anlistp != anlist ) {
1992                 slap_sl_free( anlistp, cp->op->o_tmpmemctx );
1993         }
1994
1995         return p.bvals;
1996 }
1997
1998 BerVarray
1999 aci_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2000 {
2001         AciSetCookie    *cp = (AciSetCookie *)cookie;
2002         BerVarray       bvals = NULL;
2003         struct berval   ndn;
2004         int             rc = 0;
2005
2006         /* this routine needs to return the bervals instead of
2007          * plain strings, since syntax is not known.  It should
2008          * also return the syntax or some "comparison cookie".
2009          */
2010         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->op->o_tmpmemctx );
2011         if ( rc == LDAP_SUCCESS ) {
2012                 if ( desc == slap_schema.si_ad_entryDN ) {
2013                         bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2014                                         cp->op->o_tmpmemctx );
2015                         bvals[ 0 ] = ndn;
2016                         BER_BVZERO( &bvals[ 1 ] );
2017                         BER_BVZERO( &ndn );
2018
2019                 } else {
2020                         backend_attribute( cp->op,
2021                                 cp->e, &ndn, desc, &bvals, ACL_NONE );
2022                 }
2023
2024                 if ( !BER_BVISNULL( &ndn ) ) {
2025                         slap_sl_free( ndn.bv_val, cp->op->o_tmpmemctx );
2026                 }
2027         }
2028
2029         return bvals;
2030 }
2031
2032 static int
2033 aci_match_set (
2034         struct berval *subj,
2035         Operation *op,
2036         Entry *e,
2037         int setref
2038 )
2039 {
2040         struct berval   set = BER_BVNULL;
2041         int             rc = 0;
2042         AciSetCookie    cookie;
2043
2044         if (setref == 0) {
2045                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2046         } else {
2047                 struct berval           subjdn, ndn = BER_BVNULL;
2048                 struct berval           setat;
2049                 BerVarray               bvals;
2050                 const char              *text;
2051                 AttributeDescription    *desc = NULL;
2052
2053                 /* format of string is "entry/setAttrName" */
2054                 if ( aci_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2055                         return(0);
2056                 }
2057
2058                 if ( aci_get_part( subj, 1, '/', &setat ) < 0 ) {
2059                         setat = aci_bv_set_attr;
2060                 }
2061
2062                 /*
2063                  * NOTE: dnNormalize honors the ber_len field
2064                  * as the length of the dn to be normalized
2065                  */
2066                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2067                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2068                         {
2069                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2070                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2071                                         int     i;
2072
2073                                         set = bvals[0];
2074                                         BER_BVZERO( &bvals[0] );
2075                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2076                                                 /* count */ ;
2077                                         bvals[0].bv_val = bvals[i-1].bv_val;
2078                                         BER_BVZERO( &bvals[i-1] );
2079                                 }
2080                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2081                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2082                         }
2083                 }
2084         }
2085
2086         if ( !BER_BVISNULL( &set ) ) {
2087                 cookie.op = op;
2088                 cookie.e = e;
2089                 rc = ( slap_set_filter( aci_set_gather, (SetCookie *)&cookie, &set,
2090                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2091                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2092         }
2093
2094         return(rc);
2095 }
2096
2097 #ifdef SLAPD_ACI_ENABLED
2098 static int
2099 aci_list_map_rights(
2100         struct berval *list )
2101 {
2102         struct berval bv;
2103         slap_access_t mask;
2104         int i;
2105
2106         ACL_INIT(mask);
2107         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2108                 if (bv.bv_len <= 0)
2109                         continue;
2110                 switch (*bv.bv_val) {
2111                 case 'c':
2112                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2113                         break;
2114                 case 's':
2115                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
2116                          * the right 's' to mean "set", but in the examples states
2117                          * that the right 's' means "search".  The latter definition
2118                          * is used here.
2119                          */
2120                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2121                         break;
2122                 case 'r':
2123                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
2124                         break;
2125                 case 'w':
2126                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2127                         break;
2128                 case 'x':
2129                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
2130                          * define any equivalent to the AUTH right, so I've just used
2131                          * 'x' for now.
2132                          */
2133                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2134                         break;
2135                 default:
2136                         break;
2137                 }
2138
2139         }
2140         return(mask);
2141 }
2142
2143 static int
2144 aci_list_has_attr(
2145         struct berval *list,
2146         const struct berval *attr,
2147         struct berval *val )
2148 {
2149         struct berval bv, left, right;
2150         int i;
2151
2152         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
2153                 if (aci_get_part(&bv, 0, '=', &left) < 0
2154                         || aci_get_part(&bv, 1, '=', &right) < 0)
2155                 {
2156                         if (ber_bvstrcasecmp(attr, &bv) == 0)
2157                                 return(1);
2158                 } else if (val == NULL) {
2159                         if (ber_bvstrcasecmp(attr, &left) == 0)
2160                                 return(1);
2161                 } else {
2162                         if (ber_bvstrcasecmp(attr, &left) == 0) {
2163                                 /* this is experimental code that implements a
2164                                  * simple (prefix) match of the attribute value.
2165                                  * the ACI draft does not provide for aci's that
2166                                  * apply to specific values, but it would be
2167                                  * nice to have.  If the <attr> part of an aci's
2168                                  * rights list is of the form <attr>=<value>,
2169                                  * that means the aci applies only to attrs with
2170                                  * the given value.  Furthermore, if the attr is
2171                                  * of the form <attr>=<value>*, then <value> is
2172                                  * treated as a prefix, and the aci applies to 
2173                                  * any value with that prefix.
2174                                  *
2175                                  * Ideally, this would allow r.e. matches.
2176                                  */
2177                                 if (aci_get_part(&right, 0, '*', &left) < 0
2178                                         || right.bv_len <= left.bv_len)
2179                                 {
2180                                         if (ber_bvstrcasecmp(val, &right) == 0)
2181                                                 return(1);
2182                                 } else if (val->bv_len >= left.bv_len) {
2183                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
2184                                                 return(1);
2185                                 }
2186                         }
2187                 }
2188         }
2189         return(0);
2190 }
2191
2192 static slap_access_t
2193 aci_list_get_attr_rights(
2194         struct berval *list,
2195         const struct berval *attr,
2196         struct berval *val )
2197 {
2198     struct berval bv;
2199     slap_access_t mask;
2200     int i;
2201
2202         /* loop through each rights/attr pair, skip first part (action) */
2203         ACL_INIT(mask);
2204         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
2205                 if (aci_list_has_attr(&bv, attr, val) == 0)
2206                         continue;
2207                 if (aci_get_part(list, i, ';', &bv) < 0)
2208                         continue;
2209                 mask |= aci_list_map_rights(&bv);
2210         }
2211         return(mask);
2212 }
2213
2214 static int
2215 aci_list_get_rights(
2216         struct berval *list,
2217         const struct berval *attr,
2218         struct berval *val,
2219         slap_access_t *grant,
2220         slap_access_t *deny )
2221 {
2222     struct berval perm, actn;
2223     slap_access_t *mask;
2224     int i, found;
2225
2226         if (attr == NULL || attr->bv_len == 0 
2227                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
2228                 attr = &aci_bv_br_entry;
2229         }
2230
2231         found = 0;
2232         ACL_INIT(*grant);
2233         ACL_INIT(*deny);
2234         /* loop through each permissions clause */
2235         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
2236                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
2237                         continue;
2238                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
2239                         mask = grant;
2240                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
2241                         mask = deny;
2242                 } else {
2243                         continue;
2244                 }
2245
2246                 found = 1;
2247                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
2248                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
2249         }
2250         return(found);
2251 }
2252
2253 static int
2254 aci_group_member (
2255         struct berval   *subj,
2256         struct berval   *defgrpoc,
2257         struct berval   *defgrpat,
2258         Operation       *op,
2259         Entry           *e,
2260         int             nmatch,
2261         regmatch_t      *matches
2262 )
2263 {
2264         struct berval subjdn;
2265         struct berval grpoc;
2266         struct berval grpat;
2267         ObjectClass *grp_oc = NULL;
2268         AttributeDescription *grp_ad = NULL;
2269         const char *text;
2270         int rc;
2271
2272         /* format of string is "group/objectClassValue/groupAttrName" */
2273         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
2274                 return(0);
2275         }
2276
2277         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
2278                 grpoc = *defgrpoc;
2279         }
2280
2281         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
2282                 grpat = *defgrpat;
2283         }
2284
2285         rc = slap_bv2ad( &grpat, &grp_ad, &text );
2286         if( rc != LDAP_SUCCESS ) {
2287                 rc = 0;
2288                 goto done;
2289         }
2290         rc = 0;
2291
2292         grp_oc = oc_bvfind( &grpoc );
2293
2294         if (grp_oc != NULL && grp_ad != NULL ) {
2295                 char buf[ACL_BUF_SIZE];
2296                 struct berval bv, ndn;
2297                 bv.bv_len = sizeof( buf ) - 1;
2298                 bv.bv_val = (char *)&buf;
2299                 if ( string_expand(&bv, &subjdn,
2300                                 e->e_ndn, nmatch, matches) )
2301                 {
2302                         rc = LDAP_OTHER;
2303                         goto done;
2304                 }
2305                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
2306                         rc = ( backend_group( op, e, &ndn, &op->o_ndn,
2307                                 grp_oc, grp_ad ) == 0 );
2308                         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2309                 }
2310         }
2311
2312 done:
2313         return(rc);
2314 }
2315
2316 static int
2317 aci_mask(
2318         Operation               *op,
2319         Entry                   *e,
2320         AttributeDescription    *desc,
2321         struct berval           *val,
2322         struct berval           *aci,
2323         int                     nmatch,
2324         regmatch_t              *matches,
2325         slap_access_t           *grant,
2326         slap_access_t           *deny,
2327         struct berval           *scope
2328 )
2329 {
2330         struct berval   bv, perms, sdn;
2331         int             rc;
2332                 
2333
2334         assert( !BER_BVISNULL( &desc->ad_cname ) );
2335
2336         /* parse an aci of the form:
2337                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
2338
2339            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
2340            a full description of the format for this attribute.
2341            Differences: "this" in the draft is "self" here, and
2342            "self" and "public" is in the position of dnType.
2343
2344            For now, this routine only supports scope=entry.
2345          */
2346         /* check that the aci has all 5 components */
2347         if ( aci_get_part( aci, 4, '#', NULL ) < 0 ) {
2348                 return 0;
2349         }
2350
2351         /* check that the aci family is supported */
2352         if ( aci_get_part( aci, 0, '#', &bv ) < 0 ) {
2353                 return 0;
2354         }
2355
2356         /* check that the scope matches */
2357         if ( aci_get_part( aci, 1, '#', &bv ) < 0
2358                 || ber_bvstrcasecmp( scope, &bv ) != 0 )
2359         {
2360                 return 0;
2361         }
2362
2363         /* get the list of permissions clauses, bail if empty */
2364         if ( aci_get_part( aci, 2, '#', &perms ) <= 0 ) {
2365                 return 0;
2366         }
2367
2368         /* check if any permissions allow desired access */
2369         if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
2370                 return 0;
2371         }
2372
2373         /* see if we have a DN match */
2374         if ( aci_get_part( aci, 3, '#', &bv ) < 0 ) {
2375                 return 0;
2376         }
2377
2378         if ( aci_get_part( aci, 4, '#', &sdn ) < 0 ) {
2379                 return 0;
2380         }
2381
2382         if ( ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0 ) {
2383                 struct berval ndn;
2384                 
2385                 rc = 0;
2386                 if ( dnNormalize( 0, NULL, NULL, &sdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2387                 {
2388                         if ( dn_match( &op->o_ndn, &ndn ) ) {
2389                                 rc = 1;
2390                         }
2391                         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2392                 }
2393                 return rc;
2394
2395         } else if ( ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0 ) {
2396                 return 1;
2397
2398         } else if ( ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0 ) {
2399                 if ( dn_match( &op->o_ndn, &e->e_nname ) ) {
2400                         return 1;
2401                 }
2402
2403         } else if ( ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0 ) {
2404                 Attribute               *at;
2405                 AttributeDescription    *ad = NULL;
2406                 const char              *text;
2407
2408                 rc = slap_bv2ad( &sdn, &ad, &text );
2409
2410                 if( rc != LDAP_SUCCESS ) {
2411                         return 0;
2412                 }
2413
2414                 rc = 0;
2415
2416                 bv = op->o_ndn;
2417
2418                 for ( at = attrs_find( e->e_attrs, ad );
2419                                 at != NULL;
2420                                 at = attrs_find( at->a_next, ad ) )
2421                 {
2422                         if ( value_find_ex( ad,
2423                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2424                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
2425                                 at->a_nvals,
2426                                 &bv, op->o_tmpmemctx ) == 0 )
2427                         {
2428                                 rc = 1;
2429                                 break;
2430                         }
2431                 }
2432
2433                 return rc;
2434
2435
2436         } else if ( ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0 ) {
2437                 if ( aci_group_member( &sdn, &aci_bv_group_class,
2438                                 &aci_bv_group_attr, op, e, nmatch, matches ) )
2439                 {
2440                         return 1;
2441                 }
2442
2443         } else if ( ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0 ) {
2444                 if ( aci_group_member( &sdn, &aci_bv_role_class,
2445                                 &aci_bv_role_attr, op, e, nmatch, matches ) )
2446                 {
2447                         return 1;
2448                 }
2449
2450         } else if ( ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0 ) {
2451                 if ( aci_match_set( &sdn, op, e, 0 ) ) {
2452                         return 1;
2453                 }
2454
2455         } else if ( ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0 ) {
2456                 if ( aci_match_set( &sdn, op, e, 1 ) ) {
2457                         return 1;
2458                 }
2459         }
2460
2461         return 0;
2462 }
2463
2464 #endif  /* SLAPD_ACI_ENABLED */
2465
2466 static int
2467 string_expand(
2468         struct berval   *bv,
2469         struct berval   *pat,
2470         char            *match,
2471         int             nmatch,
2472         regmatch_t      *matches)
2473 {
2474         ber_len_t       size;
2475         char   *sp;
2476         char   *dp;
2477         int     flag;
2478
2479         size = 0;
2480         bv->bv_val[0] = '\0';
2481         bv->bv_len--; /* leave space for lone $ */
2482
2483         flag = 0;
2484         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2485                 sp < pat->bv_val + pat->bv_len ; sp++ )
2486         {
2487                 /* did we previously see a $ */
2488                 if ( flag ) {
2489                         if ( flag == 1 && *sp == '$' ) {
2490                                 *dp++ = '$';
2491                                 size++;
2492                                 flag = 0;
2493
2494                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2495                                 flag = 2;
2496
2497                         } else if ( *sp >= '0' && *sp <= '9' ) {
2498                                 int     n;
2499                                 int     i;
2500                                 int     l;
2501
2502                                 n = *sp - '0';
2503
2504                                 if ( flag == 2 ) {
2505                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2506                                                 if ( *sp >= '0' && *sp <= '9' ) {
2507                                                         n = 10*n + ( *sp - '0' );
2508                                                 }
2509                                         }
2510
2511                                         if ( *sp != /*'{'*/ '}' ) {
2512                                                 /* FIXME: error */
2513                                                 return 1;
2514                                         }
2515                                 }
2516
2517                                 if ( n >= nmatch ) {
2518                                         /* FIXME: error */
2519                                         return 1;
2520                                 }
2521                                 
2522                                 *dp = '\0';
2523                                 i = matches[n].rm_so;
2524                                 l = matches[n].rm_eo; 
2525                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2526                                         *dp++ = match[i];
2527                                 }
2528                                 *dp = '\0';
2529
2530                                 flag = 0;
2531                         }
2532                 } else {
2533                         if (*sp == '$') {
2534                                 flag = 1;
2535                         } else {
2536                                 *dp++ = *sp;
2537                                 size++;
2538                         }
2539                 }
2540         }
2541
2542         if ( flag ) {
2543                 /* must have ended with a single $ */
2544                 *dp++ = '$';
2545                 size++;
2546         }
2547
2548         *dp = '\0';
2549         bv->bv_len = size;
2550
2551         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2552         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2553
2554         return 0;
2555 }
2556
2557 static int
2558 regex_matches(
2559         struct berval   *pat,           /* pattern to expand and match against */
2560         char            *str,           /* string to match against pattern */
2561         char            *buf,           /* buffer with $N expansion variables */
2562         int             nmatch, /* size of the matches array */
2563         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
2564 )
2565 {
2566         regex_t re;
2567         char newbuf[ACL_BUF_SIZE];
2568         struct berval bv;
2569         int     rc;
2570
2571         bv.bv_len = sizeof( newbuf ) - 1;
2572         bv.bv_val = newbuf;
2573
2574         if (str == NULL) {
2575                 str = "";
2576         };
2577
2578         string_expand( &bv, pat, buf, nmatch, matches );
2579         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2580         if ( rc ) {
2581                 char error[ACL_BUF_SIZE];
2582                 regerror( rc, &re, error, sizeof( error ) );
2583
2584                 Debug( LDAP_DEBUG_TRACE,
2585                     "compile( \"%s\", \"%s\") failed %s\n",
2586                         pat->bv_val, str, error );
2587                 return( 0 );
2588         }
2589
2590         rc = regexec( &re, str, 0, NULL, 0 );
2591         regfree( &re );
2592
2593         Debug( LDAP_DEBUG_TRACE,
2594             "=> regex_matches: string:   %s\n", str, 0, 0 );
2595         Debug( LDAP_DEBUG_TRACE,
2596             "=> regex_matches: rc: %d %s\n",
2597                 rc, !rc ? "matches" : "no matches", 0 );
2598         return( !rc );
2599 }
2600