]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
20935cad3d863989b83aeecb664ea5fc80394b5e
[openldap] / servers / slapd / acl.c
1 /* acl.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/regex.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15
16 #include "slap.h"
17 #include "sets.h"
18 #include "lber_pvt.h"
19
20 #define ACL_BUF_SIZE    1024    /* use most appropriate size */
21
22 /*
23  * speed up compares
24  */
25 static struct berval 
26         aci_bv_entry            = BER_BVC("entry"),
27         aci_bv_br_entry         = BER_BVC("[entry]"),
28         aci_bv_br_all           = BER_BVC("[all]"),
29         aci_bv_access_id        = BER_BVC("access-id"),
30         aci_bv_anonymous        = BER_BVC("anonymous"),
31         aci_bv_public           = BER_BVC("public"),
32         aci_bv_users            = BER_BVC("users"),
33         aci_bv_self             = BER_BVC("self"),
34         aci_bv_dnattr           = BER_BVC("dnattr"),
35         aci_bv_group            = BER_BVC("group"),
36         aci_bv_role             = BER_BVC("role"),
37         aci_bv_set              = BER_BVC("set"),
38         aci_bv_set_ref          = BER_BVC("set-ref"),
39         aci_bv_grant            = BER_BVC("grant"),
40         aci_bv_deny             = BER_BVC("deny"),
41         
42         aci_bv_group_class      = BER_BVC(SLAPD_GROUP_CLASS),
43         aci_bv_group_attr       = BER_BVC(SLAPD_GROUP_ATTR),
44         aci_bv_role_class       = BER_BVC(SLAPD_ROLE_CLASS),
45         aci_bv_role_attr        = BER_BVC(SLAPD_ROLE_ATTR);
46
47
48 static AccessControl * acl_get(
49         AccessControl *ac, int *count,
50         Backend *be, Operation *op,
51         Entry *e,
52         AttributeDescription *desc,
53         int nmatches, regmatch_t *matches );
54
55 static slap_control_t acl_mask(
56         AccessControl *ac, slap_mask_t *mask,
57         Backend *be, Connection *conn, Operation *op,
58         Entry *e,
59         AttributeDescription *desc,
60         struct berval *val,
61         regmatch_t *matches,
62         int count,
63         AccessControlState *state );
64
65 #ifdef SLAPD_ACI_ENABLED
66 static int aci_mask(
67         Backend *be,
68     Connection *conn,
69         Operation *op,
70         Entry *e,
71         AttributeDescription *desc,
72         struct berval *val,
73         struct berval *aci,
74         regmatch_t *matches,
75         slap_access_t *grant,
76         slap_access_t *deny );
77 #endif
78
79 static int      regex_matches(
80         struct berval *pat, char *str, char *buf, regmatch_t *matches);
81 static void     string_expand(
82         struct berval *newbuf, struct berval *pattern,
83         char *match, regmatch_t *matches);
84
85 typedef struct AciSetCookie {
86         Backend *be;
87         Entry *e;
88         Connection *conn;
89         Operation *op;
90 } AciSetCookie;
91
92 SLAP_SET_GATHER aci_set_gather;
93 static int aci_match_set ( struct berval *subj, Backend *be,
94     Entry *e, Connection *conn, Operation *op, int setref );
95
96 /*
97  * access_allowed - check whether op->o_ndn is allowed the requested access
98  * to entry e, attribute attr, value val.  if val is null, access to
99  * the whole attribute is assumed (all values).
100  *
101  * This routine loops through all access controls and calls
102  * acl_mask() on each applicable access control.
103  * The loop exits when a definitive answer is reached or
104  * or no more controls remain.
105  *
106  * returns:
107  *              0       access denied
108  *              1       access granted
109  */
110
111 int
112 access_allowed(
113     Backend             *be,
114     Connection          *conn,
115     Operation           *op,
116     Entry               *e,
117         AttributeDescription    *desc,
118     struct berval       *val,
119     slap_access_t       access,
120         AccessControlState *state )
121 {
122         int                             ret = 1;
123         int                             count;
124         AccessControl                   *a = NULL;
125
126 #ifdef LDAP_DEBUG
127         char accessmaskbuf[ACCESSMASK_MAXLEN];
128 #endif
129         slap_mask_t mask;
130         slap_control_t control;
131         const char *attr;
132         regmatch_t matches[MAXREMATCHES];
133         int        st_same_attr = 0;
134         int        st_initialized = 0;
135         static AccessControlState state_init = ACL_STATE_INIT;
136
137         assert( e != NULL );
138         assert( desc != NULL );
139         assert( access > ACL_NONE );
140
141         attr = desc->ad_cname.bv_val;
142
143         assert( attr != NULL );
144
145         if( state && state->as_recorded && state->as_vd_ad==desc) { 
146                 if( state->as_recorded & ACL_STATE_RECORDED_NV &&
147                         val == NULL )
148                 {
149                         return state->as_result;
150
151                 } else if ( state->as_recorded & ACL_STATE_RECORDED_VD &&
152                         val != NULL && state->as_vd_acl == NULL )
153                 {
154                         return state->as_result;
155                 }
156                 st_same_attr = 1;
157         } if (state) {
158                 state->as_vd_ad=desc;
159         }
160
161 #ifdef NEW_LOGGING
162         LDAP_LOG( ACL, ENTRY, 
163                 "access_allowed: %s access to \"%s\" \"%s\" requested\n",
164                 access2str( access ), e->e_dn, attr );
165 #else
166         Debug( LDAP_DEBUG_ACL,
167                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
168             access2str( access ), e->e_dn, attr );
169 #endif
170
171         if ( op == NULL ) {
172                 /* no-op call */
173                 goto done;
174         }
175
176         if ( be == NULL ) be = &backends[0];
177         assert( be != NULL );
178
179         /* grant database root access */
180         if ( be != NULL && be_isroot( be, &op->o_ndn ) ) {
181 #ifdef NEW_LOGGING
182                 LDAP_LOG( ACL, INFO, 
183                         "access_allowed: conn %lu root access granted\n", 
184                         conn->c_connid, 0, 0 );
185 #else
186                 Debug( LDAP_DEBUG_ACL,
187                     "<= root access granted\n",
188                         0, 0, 0 );
189 #endif
190                 goto done;
191         }
192
193         /*
194          * no-user-modification operational attributes are ignored
195          * by ACL_WRITE checking as any found here are not provided
196          * by the user
197          */
198         if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
199                 && desc != slap_schema.si_ad_entry
200                 && desc != slap_schema.si_ad_children )
201         {
202 #ifdef NEW_LOGGING
203                 LDAP_LOG( ACL, DETAIL1, 
204                         "access_allowed: conn %lu NoUserMod Operational attribute: %s "
205                         "access granted\n", conn->c_connid, attr , 0 );
206 #else
207                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
208                         " %s access granted\n",
209                         attr, 0, 0 );
210 #endif
211                 goto done;
212         }
213
214         /* use backend default access if no backend acls */
215         if( be != NULL && be->be_acl == NULL ) {
216 #ifdef NEW_LOGGING
217                 LDAP_LOG( ACL, DETAIL1, 
218                         "access_allowed: backend default %s access %s to \"%s\"\n",
219                     access2str( access ),
220                     be->be_dfltaccess >= access ? "granted" : "denied", 
221                         op->o_dn.bv_val );
222 #else
223                 Debug( LDAP_DEBUG_ACL,
224                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
225                         access2str( access ),
226                         be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
227 #endif
228                 ret = be->be_dfltaccess >= access;
229                 goto done;
230
231 #ifdef notdef
232         /* be is always non-NULL */
233         /* use global default access if no global acls */
234         } else if ( be == NULL && global_acl == NULL ) {
235 #ifdef NEW_LOGGING
236                 LDAP_LOG( ACL, DETAIL1, 
237                         "access_allowed: global default %s access %s to \"%s\"\n",
238                     access2str( access ),
239                     global_default_access >= access ? "granted" : "denied", 
240                         op->o_dn.bv_val );
241 #else
242                 Debug( LDAP_DEBUG_ACL,
243                         "=> access_allowed: global default %s access %s to \"%s\"\n",
244                         access2str( access ),
245                         global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
246 #endif
247                 ret = global_default_access >= access;
248                 goto done;
249 #endif
250         }
251
252         ret = 0;
253         control = ACL_BREAK;
254
255         if( st_same_attr ) {
256                 assert( state->as_vd_acl != NULL );
257
258                 a = state->as_vd_acl;
259                 mask = state->as_vd_acl_mask;
260                 count = state->as_vd_acl_count;
261                 AC_MEMCPY( matches, state->as_vd_acl_matches,
262                         sizeof(matches) );
263                 goto vd_access;
264
265         } else {
266                 a = NULL;
267                 ACL_INIT(mask);
268                 count = 0;
269                 memset(matches, '\0', sizeof(matches));
270         }
271
272         while((a = acl_get( a, &count, be, op, e, desc,
273                 MAXREMATCHES, matches )) != NULL)
274         {
275                 int i;
276
277                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
278 #ifdef NEW_LOGGING
279                         LDAP_LOG( ACL, DETAIL1, 
280                                 "access_allowed: match[%d]:  %d %d ",
281                             i, (int)matches[i].rm_so, (int)matches[i].rm_eo );
282 #else
283                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
284                             (int)matches[i].rm_so, (int)matches[i].rm_eo );
285 #endif
286                         if( matches[i].rm_so <= matches[0].rm_eo ) {
287                                 int n;
288                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
289                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
290                                 }
291                         }
292 #ifdef NEW_LOGGING
293                         LDAP_LOG( ACL, ARGS, "\n" , 0, 0, 0 );
294 #else
295                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
296 #endif
297                 }
298
299                 if (state) {
300                         if (state->as_vi_acl == a && (state->as_recorded & ACL_STATE_RECORDED_NV)) {
301                                 Debug( LDAP_DEBUG_ACL, "access_allowed: result from state (%s)\n", attr, 0, 0 );
302                                 return state->as_result;
303                         } else if (!st_initialized) {
304                                 Debug( LDAP_DEBUG_ACL, "access_allowed: no res from state (%s)\n", attr, 0, 0);
305                             *state = state_init;
306                                 state->as_vd_ad=desc;
307                                 st_initialized=1;
308                         }
309                 }
310
311 vd_access:
312                 control = acl_mask( a, &mask, be, conn, op,
313                         e, desc, val, matches, count, state );
314
315                 if ( control != ACL_BREAK ) {
316                         break;
317                 }
318
319                 memset(matches, '\0', sizeof(matches));
320         }
321
322         if ( ACL_IS_INVALID( mask ) ) {
323 #ifdef NEW_LOGGING
324                 LDAP_LOG( ACL, DETAIL1, 
325                         "access_allowed: conn %lu \"%s\" (%s) invalid!\n",
326                     conn->c_connid, e->e_dn, attr );
327 #else
328                 Debug( LDAP_DEBUG_ACL,
329                         "=> access_allowed: \"%s\" (%s) invalid!\n",
330                         e->e_dn, attr, 0 );
331 #endif
332                 ACL_INIT(mask);
333
334         } else if ( control == ACL_BREAK ) {
335 #ifdef NEW_LOGGING
336                 LDAP_LOG( ACL, DETAIL1, 
337                         "access_allowed: conn %lu        no more rules\n", conn->c_connid, 0,0 );
338 #else
339                 Debug( LDAP_DEBUG_ACL,
340                         "=> access_allowed: no more rules\n", 0, 0, 0);
341 #endif
342
343                 goto done;
344         }
345
346 #ifdef NEW_LOGGING
347         LDAP_LOG( ACL, ENTRY, 
348                 "access_allowed: %s access %s by %s\n", 
349                 access2str( access ), ACL_GRANT( mask, access ) ? "granted" : "denied",
350                 accessmask2str( mask, accessmaskbuf ) );
351 #else
352         Debug( LDAP_DEBUG_ACL,
353                 "=> access_allowed: %s access %s by %s\n",
354                 access2str( access ),
355                 ACL_GRANT(mask, access) ? "granted" : "denied",
356                 accessmask2str( mask, accessmaskbuf ) );
357 #endif
358
359         ret = ACL_GRANT(mask, access);
360
361 done:
362         if( state != NULL ) {
363                 /* If not value-dependent, save ACL in case of more attrs */
364                 if ( !(state->as_recorded & ACL_STATE_RECORDED_VD) )
365                         state->as_vi_acl = a;
366                 state->as_recorded |= ACL_STATE_RECORDED;
367                 state->as_result = ret;
368         }
369         return ret;
370 }
371
372 /*
373  * acl_get - return the acl applicable to entry e, attribute
374  * attr.  the acl returned is suitable for use in subsequent calls to
375  * acl_access_allowed().
376  */
377
378 static AccessControl *
379 acl_get(
380         AccessControl *a,
381         int                     *count,
382     Backend             *be,
383     Operation   *op,
384     Entry               *e,
385         AttributeDescription *desc,
386     int                 nmatch,
387     regmatch_t  *matches )
388 {
389         const char *attr;
390         int dnlen, patlen;
391
392         assert( e != NULL );
393         assert( count != NULL );
394         assert( desc != NULL );
395
396         attr = desc->ad_cname.bv_val;
397
398         assert( attr != NULL );
399
400         if( a == NULL ) {
401                 if( be == NULL ) {
402                         a = global_acl;
403                 } else {
404                         a = be->be_acl;
405                 }
406
407                 assert( a != NULL );
408
409         } else {
410                 a = a->acl_next;
411         }
412
413         dnlen = e->e_nname.bv_len;
414
415         for ( ; a != NULL; a = a->acl_next ) {
416                 (*count) ++;
417
418                 if ( a->acl_dn_pat.bv_len || ( a->acl_dn_style != ACL_STYLE_REGEX )) {
419                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
420 #ifdef NEW_LOGGING
421                                 LDAP_LOG( ACL, DETAIL1, 
422                                         "acl_get: dnpat [%d] %s nsub: %d\n",
423                                         *count, a->acl_dn_pat.bv_val, 
424                                         (int) a->acl_dn_re.re_nsub );
425 #else
426                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
427                                         *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
428 #endif
429                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
430                                         continue;
431
432                         } else {
433 #ifdef NEW_LOGGING
434                                 LDAP_LOG( ACL, DETAIL1, "acl_get: dn [%d] %s\n",
435                                            *count, a->acl_dn_pat.bv_val, 0 );
436 #else
437                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
438                                         *count, a->acl_dn_pat.bv_val, 0 );
439 #endif
440                                 patlen = a->acl_dn_pat.bv_len;
441                                 if ( dnlen < patlen )
442                                         continue;
443
444                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
445                                         /* base dn -- entire object DN must match */
446                                         if ( dnlen != patlen )
447                                                 continue;
448
449                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
450                                         int rdnlen = -1;
451
452                                         if ( dnlen <= patlen )
453                                                 continue;
454
455                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
456                                                 continue;
457
458                                         rdnlen = dn_rdnlen( NULL, &e->e_nname );
459                                         if ( rdnlen != dnlen - patlen - 1 )
460                                                 continue;
461
462                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
463                                         if ( dnlen > patlen && !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
464                                                 continue;
465
466                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
467                                         if ( dnlen <= patlen )
468                                                 continue;
469                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) )
470                                                 continue;
471                                 }
472
473                                 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
474                                         continue;
475                         }
476
477 #ifdef NEW_LOGGING
478                         LDAP_LOG( ACL, DETAIL1, 
479                                 "acl_get: [%d] matched\n", *count, 0, 0 );
480 #else
481                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
482                                 *count, 0, 0 );
483 #endif
484                 }
485
486                 if ( a->acl_filter != NULL ) {
487                         ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
488                         if ( rc != LDAP_COMPARE_TRUE ) {
489                                 continue;
490                         }
491                 }
492
493 #ifdef NEW_LOGGING
494                 LDAP_LOG( ACL, DETAIL1, 
495                         "acl_get: [%d] check attr %s\n", *count, attr ,0 );
496 #else
497                 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
498                        *count, attr, 0);
499 #endif
500                 if ( attr == NULL || a->acl_attrs == NULL ||
501                         ad_inlist( desc, a->acl_attrs ) )
502                 {
503 #ifdef NEW_LOGGING
504                         LDAP_LOG( ACL, DETAIL1, 
505                                 "acl_get:  [%d] acl %s attr: %s\n", *count, e->e_dn, attr );
506 #else
507                         Debug( LDAP_DEBUG_ACL,
508                                 "<= acl_get: [%d] acl %s attr: %s\n",
509                                 *count, e->e_dn, attr );
510 #endif
511                         return a;
512                 }
513                 matches[0].rm_so = matches[0].rm_eo = -1;
514         }
515
516 #ifdef NEW_LOGGING
517         LDAP_LOG( ACL, RESULTS, "acl_get: done.\n", 0, 0, 0 );
518 #else
519         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
520 #endif
521         return( NULL );
522 }
523
524 /*
525  * Record value-dependent access control state
526  */
527 #define ACL_RECORD_VALUE_STATE do { \
528                 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
529                         state->as_recorded |= ACL_STATE_RECORDED_VD; \
530                         state->as_vd_acl = a; \
531                         AC_MEMCPY( state->as_vd_acl_matches, matches, \
532                                 sizeof( state->as_vd_acl_matches )) ; \
533                         state->as_vd_acl_count = count; \
534                         state->as_vd_access = b; \
535                         state->as_vd_access_count = i; \
536                 } \
537         } while( 0 )
538
539 /*
540  * acl_mask - modifies mask based upon the given acl and the
541  * requested access to entry e, attribute attr, value val.  if val
542  * is null, access to the whole attribute is assumed (all values).
543  *
544  * returns      0       access NOT allowed
545  *              1       access allowed
546  */
547
548 static slap_control_t
549 acl_mask(
550     AccessControl       *a,
551         slap_mask_t *mask,
552     Backend             *be,
553     Connection  *conn,
554     Operation   *op,
555     Entry               *e,
556         AttributeDescription *desc,
557     struct berval       *val,
558         regmatch_t      *matches,
559         int     count,
560         AccessControlState *state )
561 {
562         int             i, odnlen, patlen;
563         Access  *b;
564 #ifdef LDAP_DEBUG
565         char accessmaskbuf[ACCESSMASK_MAXLEN];
566 #endif
567         const char *attr;
568
569         assert( a != NULL );
570         assert( mask != NULL );
571         assert( desc != NULL );
572
573         attr = desc->ad_cname.bv_val;
574
575         assert( attr != NULL );
576
577 #ifdef NEW_LOGGING
578         LDAP_LOG( ACL, ENTRY, 
579                 "acl_mask: conn %lu  access to entry \"%s\", attr \"%s\" requested\n",
580                 conn->c_connid, e->e_dn, attr );
581
582         LDAP_LOG( ACL, ARGS, 
583                 " to %s by \"%s\", (%s) \n", val ? "value" : "all values",
584                 op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
585                 accessmask2str( *mask, accessmaskbuf ) );
586 #else
587         Debug( LDAP_DEBUG_ACL,
588                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
589                 e->e_dn, attr, 0 );
590
591         Debug( LDAP_DEBUG_ACL,
592                 "=> acl_mask: to %s by \"%s\", (%s) \n",
593                 val ? "value" : "all values",
594                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
595                 accessmask2str( *mask, accessmaskbuf ) );
596 #endif
597
598         if( state && ( state->as_recorded & ACL_STATE_RECORDED_VD )
599                 && state->as_vd_acl == a )
600         {
601                 b = state->as_vd_access;
602                 i = state->as_vd_access_count;
603
604         } else {
605                 b = a->acl_access;
606                 i = 1;
607         }
608
609         for ( ; b != NULL; b = b->a_next, i++ ) {
610                 slap_mask_t oldmask, modmask;
611
612                 ACL_INVALIDATE( modmask );
613
614                 /* AND <who> clauses */
615                 if ( b->a_dn_pat.bv_len != 0 ) {
616 #ifdef NEW_LOGGING
617                         LDAP_LOG( ACL, DETAIL1, 
618                                 "acl_mask: conn %lu  check a_dn_pat: %s\n",
619                                 conn->c_connid, b->a_dn_pat.bv_val ,0 );
620 #else
621                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
622                                 b->a_dn_pat.bv_val, 0, 0);
623 #endif
624                         /*
625                          * if access applies to the entry itself, and the
626                          * user is bound as somebody in the same namespace as
627                          * the entry, OR the given dn matches the dn pattern
628                          */
629                         if ( bvmatch( &b->a_dn_pat, &aci_bv_anonymous ) ) {
630                                 if ( op->o_ndn.bv_len != 0 ) {
631                                         continue;
632                                 }
633
634                         } else if ( bvmatch( &b->a_dn_pat, &aci_bv_users ) ) {
635                                 if ( op->o_ndn.bv_len == 0 ) {
636                                         continue;
637                                 }
638
639                         } else if ( bvmatch( &b->a_dn_pat, &aci_bv_self ) ) {
640                                 if ( op->o_ndn.bv_len == 0 ) {
641                                         continue;
642                                 }
643                                 
644                                 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
645                                         continue;
646                                 }
647
648                         } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
649                                 if ( !ber_bvccmp( &b->a_dn_pat, '*' ) ) {
650                                         int ret = regex_matches( &b->a_dn_pat,
651                                                 op->o_ndn.bv_val, e->e_ndn, matches );
652
653                                         if( ret == 0 ) {
654                                                 continue;
655                                         }
656                                 }
657
658                         } else {
659                                 struct berval pat;
660                                 int got_match = 0;
661
662                                 if ( e->e_dn == NULL )
663                                         continue;
664
665                                 if ( b->a_dn_expand ) {
666                                         struct berval bv;
667                                         char buf[ACL_BUF_SIZE];
668
669                                         bv.bv_len = sizeof( buf ) - 1;
670                                         bv.bv_val = buf;
671
672                                         string_expand(&bv, &b->a_dn_pat, 
673                                                         e->e_ndn, matches);
674                                         if ( dnNormalize2(NULL, &bv, &pat) != LDAP_SUCCESS ) {
675                                                 /* did not expand to a valid dn */
676                                                 continue;
677                                         }
678                                 } else {
679                                         pat = b->a_dn_pat;
680                                 }
681
682                                 patlen = pat.bv_len;
683                                 odnlen = op->o_ndn.bv_len;
684                                 if ( odnlen < patlen ) {
685                                         goto dn_match_cleanup;
686
687                                 }
688
689                                 if ( b->a_dn_style == ACL_STYLE_BASE ) {
690                                         /* base dn -- entire object DN must match */
691                                         if ( odnlen != patlen ) {
692                                                 goto dn_match_cleanup;
693                                         }
694
695                                 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
696                                         int rdnlen = -1;
697
698                                         if ( odnlen <= patlen ) {
699                                                 goto dn_match_cleanup;
700                                         }
701
702                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
703                                                 goto dn_match_cleanup;
704                                         }
705
706                                         rdnlen = dn_rdnlen( NULL, &op->o_ndn );
707                                         if ( rdnlen != odnlen - patlen - 1 ) {
708                                                 goto dn_match_cleanup;
709                                         }
710
711                                 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
712                                         if ( odnlen > patlen && !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
713                                                 goto dn_match_cleanup;
714                                         }
715
716                                 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
717                                         if ( odnlen <= patlen ) {
718                                                 goto dn_match_cleanup;
719                                         }
720
721                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) ) {
722                                                 goto dn_match_cleanup;
723                                         }
724                                 }
725
726                                 got_match = !strcmp( pat.bv_val, op->o_ndn.bv_val + odnlen - patlen );
727
728 dn_match_cleanup:;
729                                 if ( pat.bv_val != b->a_dn_pat.bv_val ) {
730                                         free( pat.bv_val );
731                                 }
732
733                                 if ( !got_match ) {
734                                         continue;
735                                 }
736                         }
737                 }
738
739                 if ( b->a_sockurl_pat.bv_len ) {
740 #ifdef NEW_LOGGING
741                         LDAP_LOG( ACL, DETAIL1, 
742                                    "acl_mask: conn %lu  check a_sockurl_pat: %s\n",
743                                    conn->c_connid, b->a_sockurl_pat.bv_val , 0 );
744 #else
745                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
746                                 b->a_sockurl_pat.bv_val, 0, 0 );
747 #endif
748
749                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
750                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
751                                         if (!regex_matches( &b->a_sockurl_pat, conn->c_listener_url.bv_val,
752                                                         e->e_ndn, matches ) ) 
753                                         {
754                                                 continue;
755                                         }
756                                 } else {
757                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &conn->c_listener_url ) != 0 )
758                                                 continue;
759                                 }
760                         }
761                 }
762
763                 if ( b->a_domain_pat.bv_len ) {
764 #ifdef NEW_LOGGING
765                         LDAP_LOG( ACL, DETAIL1, 
766                                    "acl_mask: conn %lu  check a_domain_pat: %s\n",
767                                    conn->c_connid, b->a_domain_pat.bv_val , 0 );
768 #else
769                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
770                                 b->a_domain_pat.bv_val, 0, 0 );
771 #endif
772                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
773                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
774                                         if (!regex_matches( &b->a_domain_pat, conn->c_peer_domain.bv_val,
775                                                         e->e_ndn, matches ) ) 
776                                         {
777                                                 continue;
778                                         }
779                                 } else {
780                                         char buf[ACL_BUF_SIZE];
781
782                                         struct berval   cmp = conn->c_peer_domain;
783                                         struct berval   pat = b->a_domain_pat;
784
785                                         if ( b->a_domain_expand ) {
786                                                 struct berval bv;
787
788                                                 bv.bv_len = sizeof(buf) - 1;
789                                                 bv.bv_val = buf;
790
791                                                 string_expand(&bv, &b->a_domain_pat, e->e_ndn, matches);
792                                                 pat = bv;
793                                         }
794
795                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
796                                                 int offset = cmp.bv_len - pat.bv_len;
797                                                 if ( offset < 0 ) {
798                                                         continue;
799                                                 }
800
801                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
802                                                         continue;
803                                                 }
804
805                                                 /* trim the domain */
806                                                 cmp.bv_val = &cmp.bv_val[ offset ];
807                                                 cmp.bv_len -= offset;
808                                         }
809                                         
810                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
811                                                 continue;
812                                         }
813                                 }
814                         }
815                 }
816
817                 if ( b->a_peername_pat.bv_len ) {
818 #ifdef NEW_LOGGING
819                         LDAP_LOG( ACL, DETAIL1, 
820                                    "acl_mask: conn %lu  check a_perrname_path: %s\n",
821                                    conn->c_connid, b->a_peername_pat.bv_val , 0 );
822 #else
823                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
824                                 b->a_peername_pat.bv_val, 0, 0 );
825 #endif
826                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
827                                 if ( b->a_peername_style == ACL_STYLE_REGEX) {
828                                         if (!regex_matches( &b->a_peername_pat, conn->c_peer_name.bv_val,
829                                                         e->e_ndn, matches ) ) 
830                                         {
831                                                 continue;
832                                         }
833                                 } else {
834                                         if ( ber_bvstrcasecmp( &b->a_peername_pat, &conn->c_peer_name ) != 0 )
835                                                 continue;
836                                 }
837                         }
838                 }
839
840                 if ( b->a_sockname_pat.bv_len ) {
841 #ifdef NEW_LOGGING
842                         LDAP_LOG( ACL, DETAIL1, 
843                                    "acl_mask: conn %lu  check a_sockname_path: %s\n",
844                                    conn->c_connid, b->a_sockname_pat.bv_val , 0 );
845 #else
846                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
847                                 b->a_sockname_pat.bv_val, 0, 0 );
848 #endif
849                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
850                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
851                                         if (!regex_matches( &b->a_sockname_pat, conn->c_sock_name.bv_val,
852                                                         e->e_ndn, matches ) ) 
853                                         {
854                                                 continue;
855                                         }
856                                 } else {
857                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &conn->c_sock_name ) != 0 )
858                                                 continue;
859                                 }
860                         }
861                 }
862
863                 if ( b->a_dn_at != NULL ) {
864                         Attribute       *at;
865                         struct berval   bv;
866                         int rc, match = 0;
867                         const char *text;
868                         const char *attr = b->a_dn_at->ad_cname.bv_val;
869
870                         assert( attr != NULL );
871
872                         if ( op->o_ndn.bv_len == 0 ) {
873                                 continue;
874                         }
875
876 #ifdef NEW_LOGGING
877                         LDAP_LOG( ACL, DETAIL1, 
878                                    "acl_mask: conn %lu  check a_dn_pat: %s\n",
879                                    conn->c_connid, attr , 0 );
880 #else
881                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
882                                 attr, 0, 0);
883 #endif
884                         bv = op->o_ndn;
885
886                         /* see if asker is listed in dnattr */
887                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
888                                 at != NULL;
889                                 at = attrs_find( at->a_next, b->a_dn_at ) )
890                         {
891                                 if( value_find_ex( b->a_dn_at,
892                                         SLAP_MR_VALUE_NORMALIZED_MATCH, at->a_vals, &bv ) == 0 ) {
893                                         /* found it */
894                                         match = 1;
895                                         break;
896                                 }
897                         }
898
899                         if( match ) {
900                                 /* have a dnattr match. if this is a self clause then
901                                  * the target must also match the op dn.
902                                  */
903                                 if ( b->a_dn_self ) {
904                                         /* check if the target is an attribute. */
905                                         if ( val == NULL )
906                                                 continue;
907                                         /* target is attribute, check if the attribute value
908                                          * is the op dn.
909                                          */
910                                         rc = value_match( &match, b->a_dn_at,
911                                                 b->a_dn_at->ad_type->sat_equality, 0,
912                                                 val, &bv, &text );
913                                         /* on match error or no match, fail the ACL clause */
914                                         if (rc != LDAP_SUCCESS || match != 0 )
915                                                 continue;
916                                 }
917                         } else {
918                                 /* no dnattr match, check if this is a self clause */
919                                 if ( ! b->a_dn_self )
920                                         continue;
921
922                                 ACL_RECORD_VALUE_STATE;
923                                 
924                                 /* this is a self clause, check if the target is an
925                                  * attribute.
926                                  */
927                                 if ( val == NULL )
928                                         continue;
929
930                                 /* target is attribute, check if the attribute value
931                                  * is the op dn.
932                                  */
933                                 rc = value_match( &match, b->a_dn_at,
934                                         b->a_dn_at->ad_type->sat_equality, 0,
935                                         val, &bv, &text );
936
937                                 /* on match error or no match, fail the ACL clause */
938                                 if (rc != LDAP_SUCCESS || match != 0 )
939                                         continue;
940                         }
941                 }
942
943                 if ( b->a_group_pat.bv_len ) {
944                         struct berval bv;
945                         struct berval ndn = { 0, NULL };
946                         int rc;
947
948                         if ( op->o_ndn.bv_len == 0 ) {
949                                 continue;
950                         }
951
952                         /* b->a_group is an unexpanded entry name, expanded it should be an 
953                          * entry with objectclass group* and we test to see if odn is one of
954                          * the values in the attribute group
955                          */
956                         /* see if asker is listed in dnattr */
957                         if ( b->a_group_style == ACL_STYLE_REGEX ) {
958                                 char buf[ACL_BUF_SIZE];
959                                 bv.bv_len = sizeof(buf) - 1;
960                                 bv.bv_val = buf; 
961
962                                 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
963                                 if ( dnNormalize2( NULL, &bv, &ndn ) != LDAP_SUCCESS ) {
964                                         /* did not expand to a valid dn */
965                                         continue;
966                                 }
967
968                                 bv = ndn;
969
970                         } else {
971                                 bv = b->a_group_pat;
972                         }
973
974                         rc = backend_group( be, conn, op, e, &bv, &op->o_ndn,
975                                 b->a_group_oc, b->a_group_at );
976
977                         if ( ndn.bv_val ) free( ndn.bv_val );
978
979                         if ( rc != 0 ) {
980                                 continue;
981                         }
982                 }
983
984                 if ( b->a_set_pat.bv_len != 0 ) {
985                         struct berval bv;
986                         char buf[ACL_BUF_SIZE];
987                         if( b->a_set_style == ACL_STYLE_REGEX ){
988                                 bv.bv_len = sizeof(buf) - 1;
989                                 bv.bv_val = buf;
990                                 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
991                         }else{
992                                 bv = b->a_set_pat;
993                         }
994                         if (aci_match_set( &bv, be, e, conn, op, 0 ) == 0) {
995                                 continue;
996                         }
997                 }
998
999                 if ( b->a_authz.sai_ssf ) {
1000 #ifdef NEW_LOGGING
1001                         LDAP_LOG( ACL, DETAIL1, 
1002                                 "acl_mask: conn %lu  check a_authz.sai_ssf: ACL %u > OP %u\n",
1003                                 conn->c_connid, b->a_authz.sai_ssf, op->o_ssf  );
1004 #else
1005                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1006                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1007 #endif
1008                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1009                                 continue;
1010                         }
1011                 }
1012
1013                 if ( b->a_authz.sai_transport_ssf ) {
1014 #ifdef NEW_LOGGING
1015                         LDAP_LOG( ACL, DETAIL1, 
1016                                 "acl_mask: conn %lu  check a_authz.sai_transport_ssf: "
1017                                 "ACL %u > OP %u\n",
1018                                 conn->c_connid, b->a_authz.sai_transport_ssf, 
1019                                 op->o_transport_ssf  );
1020 #else
1021                         Debug( LDAP_DEBUG_ACL,
1022                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1023                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1024 #endif
1025                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1026                                 continue;
1027                         }
1028                 }
1029
1030                 if ( b->a_authz.sai_tls_ssf ) {
1031 #ifdef NEW_LOGGING
1032                         LDAP_LOG( ACL, DETAIL1, 
1033                                 "acl_mask: conn %lu  check a_authz.sai_tls_ssf: ACL %u > "
1034                                 "OP %u\n",
1035                                 conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf  );
1036 #else
1037                         Debug( LDAP_DEBUG_ACL,
1038                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1039                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1040 #endif
1041                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1042                                 continue;
1043                         }
1044                 }
1045
1046                 if ( b->a_authz.sai_sasl_ssf ) {
1047 #ifdef NEW_LOGGING
1048                         LDAP_LOG( ACL, DETAIL1, 
1049                            "acl_mask: conn %lu check a_authz.sai_sasl_ssf: " 
1050                            "ACL %u > OP %u\n",
1051                                 conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1052 #else
1053                         Debug( LDAP_DEBUG_ACL,
1054                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1055                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1056 #endif
1057                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1058                                 continue;
1059                         }
1060                 }
1061
1062 #ifdef SLAPD_ACI_ENABLED
1063                 if ( b->a_aci_at != NULL ) {
1064                         Attribute       *at;
1065                         slap_access_t grant, deny, tgrant, tdeny;
1066
1067                         /* this case works different from the others above.
1068                          * since aci's themselves give permissions, we need
1069                          * to first check b->a_access_mask, the ACL's access level.
1070                          */
1071
1072                         if ( e->e_nname.bv_len == 0 ) {
1073                                 /* no ACIs in the root DSE */
1074                                 continue;
1075                         }
1076
1077                         /* first check if the right being requested
1078                          * is allowed by the ACL clause.
1079                          */
1080                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1081                                 continue;
1082                         }
1083
1084                         /* get the aci attribute */
1085                         at = attr_find( e->e_attrs, b->a_aci_at );
1086                         if ( at == NULL ) {
1087                                 continue;
1088                         }
1089
1090                         ACL_RECORD_VALUE_STATE;
1091
1092                         /* start out with nothing granted, nothing denied */
1093                         ACL_INIT(tgrant);
1094                         ACL_INIT(tdeny);
1095
1096                         /* the aci is an multi-valued attribute.  The
1097                          * rights are determined by OR'ing the individual
1098                          * rights given by the acis.
1099                          */
1100                         for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1101                                 if (aci_mask( be, conn, op,
1102                                         e, desc, val, &at->a_vals[i],
1103                                         matches, &grant, &deny ) != 0)
1104                                 {
1105                                         tgrant |= grant;
1106                                         tdeny |= deny;
1107                                 }
1108                         }
1109
1110                         /* remove anything that the ACL clause does not allow */
1111                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1112                         tdeny &= ACL_PRIV_MASK;
1113
1114                         /* see if we have anything to contribute */
1115                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1116                                 continue;
1117                         }
1118
1119                         /* this could be improved by changing acl_mask so that it can deal with
1120                          * by clauses that return grant/deny pairs.  Right now, it does either
1121                          * additive or subtractive rights, but not both at the same time.  So,
1122                          * we need to combine the grant/deny pair into a single rights mask in
1123                          * a smart way:  if either grant or deny is "empty", then we use the
1124                          * opposite as is, otherwise we remove any denied rights from the grant
1125                          * rights mask and construct an additive mask.
1126                          */
1127                         if (ACL_IS_INVALID(tdeny)) {
1128                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1129
1130                         } else if (ACL_IS_INVALID(tgrant)) {
1131                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1132
1133                         } else {
1134                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1135                         }
1136
1137                 } else
1138 #endif
1139                 {
1140                         modmask = b->a_access_mask;
1141                 }
1142
1143 #ifdef NEW_LOGGING
1144                 LDAP_LOG( ACL, RESULTS, 
1145                            "acl_mask: [%d] applying %s (%s)\n",
1146                            i, accessmask2str( modmask, accessmaskbuf),
1147                            b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1148                            ? "break" : "stop"  );
1149 #else
1150                 Debug( LDAP_DEBUG_ACL,
1151                         "<= acl_mask: [%d] applying %s (%s)\n",
1152                         i, accessmask2str( modmask, accessmaskbuf ), 
1153                         b->a_type == ACL_CONTINUE
1154                                 ? "continue"
1155                                 : b->a_type == ACL_BREAK
1156                                         ? "break"
1157                                         : "stop" );
1158 #endif
1159                 /* save old mask */
1160                 oldmask = *mask;
1161
1162                 if( ACL_IS_ADDITIVE(modmask) ) {
1163                         /* add privs */
1164                         ACL_PRIV_SET( *mask, modmask );
1165
1166                         /* cleanup */
1167                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1168
1169                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1170                         /* substract privs */
1171                         ACL_PRIV_CLR( *mask, modmask );
1172
1173                         /* cleanup */
1174                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1175
1176                 } else {
1177                         /* assign privs */
1178                         *mask = modmask;
1179                 }
1180
1181 #ifdef NEW_LOGGING
1182                 LDAP_LOG( ACL, DETAIL1, 
1183                            "acl_mask: conn %lu  [%d] mask: %s\n",
1184                            conn->c_connid, i, accessmask2str( *mask, accessmaskbuf)  );
1185 #else
1186                 Debug( LDAP_DEBUG_ACL,
1187                         "<= acl_mask: [%d] mask: %s\n",
1188                         i, accessmask2str(*mask, accessmaskbuf), 0 );
1189 #endif
1190
1191                 if( b->a_type == ACL_CONTINUE ) {
1192                         continue;
1193
1194                 } else if ( b->a_type == ACL_BREAK ) {
1195                         return ACL_BREAK;
1196
1197                 } else {
1198                         return ACL_STOP;
1199                 }
1200         }
1201
1202         /* implicit "by * none" clause */
1203         ACL_INIT(*mask);
1204
1205 #ifdef NEW_LOGGING
1206         LDAP_LOG( ACL, RESULTS, 
1207                    "acl_mask: conn %lu  no more <who> clauses, returning %d (stop)\n",
1208                    conn->c_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1209 #else
1210         Debug( LDAP_DEBUG_ACL,
1211                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1212                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1213 #endif
1214         return ACL_STOP;
1215 }
1216
1217 /*
1218  * acl_check_modlist - check access control on the given entry to see if
1219  * it allows the given modifications by the user associated with op.
1220  * returns      1       if mods allowed ok
1221  *                      0       mods not allowed
1222  */
1223
1224 int
1225 acl_check_modlist(
1226     Backend     *be,
1227     Connection  *conn,
1228     Operation   *op,
1229     Entry       *e,
1230     Modifications       *mlist
1231 )
1232 {
1233         struct berval *bv;
1234         AccessControlState state = ACL_STATE_INIT;
1235
1236         assert( be != NULL );
1237
1238         /* short circuit root database access */
1239         if ( be_isroot( be, &op->o_ndn ) ) {
1240 #ifdef NEW_LOGGING
1241                 LDAP_LOG( ACL, DETAIL1, 
1242                            "acl_check_modlist: conn %lu  access granted to root user\n",
1243                            conn->c_connid, 0, 0 );
1244 #else
1245                 Debug( LDAP_DEBUG_ACL,
1246                         "<= acl_access_allowed: granted to database root\n",
1247                     0, 0, 0 );
1248 #endif
1249                 return 1;
1250         }
1251
1252         /* use backend default access if no backend acls */
1253         if( be != NULL && be->be_acl == NULL ) {
1254 #ifdef NEW_LOGGING
1255                 LDAP_LOG( ACL, DETAIL1, 
1256                         "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1257                         access2str( ACL_WRITE ),
1258                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", 
1259                         op->o_dn.bv_val  );
1260 #else
1261                 Debug( LDAP_DEBUG_ACL,
1262                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1263                         access2str( ACL_WRITE ),
1264                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1265 #endif
1266                 return be->be_dfltaccess >= ACL_WRITE;
1267
1268 #ifdef notdef
1269         /* be is always non-NULL */
1270         /* use global default access if no global acls */
1271         } else if ( be == NULL && global_acl == NULL ) {
1272 #ifdef NEW_LOGGING
1273                 LDAP_LOG( ACL, DETAIL1, 
1274                         "acl_check_modlist: global default %s access %s to \"%s\"\n",
1275                    access2str( ACL_WRITE ),
1276                    global_default_access >= ACL_WRITE ? "granted" : "denied", 
1277                    op->o_dn  );
1278 #else
1279                 Debug( LDAP_DEBUG_ACL,
1280                         "=> access_allowed: global default %s access %s to \"%s\"\n",
1281                         access2str( ACL_WRITE ),
1282                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1283 #endif
1284                 return global_default_access >= ACL_WRITE;
1285 #endif
1286         }
1287
1288         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1289                 /*
1290                  * no-user-modification operational attributes are ignored
1291                  * by ACL_WRITE checking as any found here are not provided
1292                  * by the user
1293                  */
1294                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1295 #ifdef NEW_LOGGING
1296                         LDAP_LOG( ACL, DETAIL1, 
1297                                    "acl_check_modlist: conn %lu  no-user-mod %s: modify access granted\n",
1298                                    conn->c_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1299 #else
1300                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1301                                 " modify access granted\n",
1302                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1303 #endif
1304                         continue;
1305                 }
1306
1307                 switch ( mlist->sml_op ) {
1308                 case LDAP_MOD_REPLACE:
1309                         /*
1310                          * We must check both permission to delete the whole
1311                          * attribute and permission to add the specific attributes.
1312                          * This prevents abuse from selfwriters.
1313                          */
1314                         if ( ! access_allowed( be, conn, op, e,
1315                                 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1316                         {
1317                                 return( 0 );
1318                         }
1319
1320                         if ( mlist->sml_bvalues == NULL ) break;
1321
1322                         /* fall thru to check value to add */
1323
1324                 case LDAP_MOD_ADD:
1325                         assert( mlist->sml_bvalues != NULL );
1326
1327                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1328                                 if ( ! access_allowed( be, conn, op, e,
1329                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1330                                 {
1331                                         return( 0 );
1332                                 }
1333                         }
1334                         break;
1335
1336                 case LDAP_MOD_DELETE:
1337                         if ( mlist->sml_bvalues == NULL ) {
1338                                 if ( ! access_allowed( be, conn, op, e,
1339                                         mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1340                                 {
1341                                         return( 0 );
1342                                 }
1343                                 break;
1344                         }
1345                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1346                                 if ( ! access_allowed( be, conn, op, e,
1347                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1348                                 {
1349                                         return( 0 );
1350                                 }
1351                         }
1352                         break;
1353
1354                 case SLAP_MOD_SOFTADD:
1355                         /* allow adding attribute via modrdn thru */
1356                         break;
1357
1358                 default:
1359                         assert( 0 );
1360                         return( 0 );
1361                 }
1362         }
1363
1364         return( 1 );
1365 }
1366
1367 static int
1368 aci_get_part(
1369         struct berval *list,
1370         int ix,
1371         char sep,
1372         struct berval *bv )
1373 {
1374         int len;
1375         char *p;
1376
1377         if (bv) {
1378                 bv->bv_len = 0;
1379                 bv->bv_val = NULL;
1380         }
1381         len = list->bv_len;
1382         p = list->bv_val;
1383         while (len >= 0 && --ix >= 0) {
1384                 while (--len >= 0 && *p++ != sep) ;
1385         }
1386         while (len >= 0 && *p == ' ') {
1387                 len--;
1388                 p++;
1389         }
1390         if (len < 0)
1391                 return(-1);
1392
1393         if (!bv)
1394                 return(0);
1395
1396         bv->bv_val = p;
1397         while (--len >= 0 && *p != sep) {
1398                 bv->bv_len++;
1399                 p++;
1400         }
1401         while (bv->bv_len > 0 && *--p == ' ')
1402                 bv->bv_len--;
1403         return(bv->bv_len);
1404 }
1405
1406 BerVarray
1407 aci_set_gather (void *cookie, struct berval *name, struct berval *attr)
1408 {
1409         AciSetCookie *cp = cookie;
1410         BerVarray bvals = NULL;
1411         struct berval ndn;
1412
1413         /* this routine needs to return the bervals instead of
1414          * plain strings, since syntax is not known.  It should
1415          * also return the syntax or some "comparison cookie".
1416          */
1417
1418         if (dnNormalize2(NULL, name, &ndn) == LDAP_SUCCESS) {
1419                 const char *text;
1420                 AttributeDescription *desc = NULL;
1421                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1422                         backend_attribute(cp->be, NULL, cp->op,
1423                                 cp->e, &ndn, desc, &bvals);
1424                 }
1425                 free(ndn.bv_val);
1426         }
1427         return(bvals);
1428 }
1429
1430 static int
1431 aci_match_set (
1432         struct berval *subj,
1433     Backend *be,
1434     Entry *e,
1435     Connection *conn,
1436     Operation *op,
1437     int setref
1438 )
1439 {
1440         struct berval set = { 0, NULL };
1441         int rc = 0;
1442         AciSetCookie cookie;
1443
1444         if (setref == 0) {
1445                 ber_dupbv( &set, subj );
1446         } else {
1447                 struct berval subjdn, ndn = { 0, NULL };
1448                 struct berval setat;
1449                 BerVarray bvals;
1450                 const char *text;
1451                 AttributeDescription *desc = NULL;
1452
1453                 /* format of string is "entry/setAttrName" */
1454                 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1455                         return(0);
1456                 }
1457
1458                 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1459                         setat.bv_val = SLAPD_ACI_SET_ATTR;
1460                         setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1461                 }
1462
1463                 if ( setat.bv_val != NULL ) {
1464                         /*
1465                          * NOTE: dnNormalize2 honors the ber_len field
1466                          * as the length of the dn to be normalized
1467                          */
1468                         if ( dnNormalize2(NULL, &subjdn, &ndn) == LDAP_SUCCESS
1469                                 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1470                         {
1471                                 backend_attribute(be, NULL, op, e,
1472                                         &ndn, desc, &bvals);
1473                                 if ( bvals != NULL ) {
1474                                         if ( bvals[0].bv_val != NULL ) {
1475                                                 int i;
1476                                                 set = bvals[0];
1477                                                 bvals[0].bv_val = NULL;
1478                                                 for (i=1;bvals[i].bv_val;i++);
1479                                                 bvals[0].bv_val = bvals[i-1].bv_val;
1480                                                 bvals[i-1].bv_val = NULL;
1481                                         }
1482                                         ber_bvarray_free(bvals);
1483                                 }
1484                         }
1485                         if (ndn.bv_val)
1486                                 free(ndn.bv_val);
1487                 }
1488         }
1489
1490         if (set.bv_val != NULL) {
1491                 cookie.be = be;
1492                 cookie.e = e;
1493                 cookie.conn = conn;
1494                 cookie.op = op;
1495                 rc = (slap_set_filter(aci_set_gather, &cookie, &set,
1496                         &op->o_ndn, &e->e_nname, NULL) > 0);
1497                 ch_free(set.bv_val);
1498         }
1499         return(rc);
1500 }
1501
1502 #ifdef SLAPD_ACI_ENABLED
1503 static int
1504 aci_list_map_rights(
1505         struct berval *list )
1506 {
1507         struct berval bv;
1508         slap_access_t mask;
1509         int i;
1510
1511         ACL_INIT(mask);
1512         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1513                 if (bv.bv_len <= 0)
1514                         continue;
1515                 switch (*bv.bv_val) {
1516                 case 'c':
1517                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1518                         break;
1519                 case 's':
1520                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1521                          * the right 's' to mean "set", but in the examples states
1522                          * that the right 's' means "search".  The latter definition
1523                          * is used here.
1524                          */
1525                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1526                         break;
1527                 case 'r':
1528                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1529                         break;
1530                 case 'w':
1531                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1532                         break;
1533                 case 'x':
1534                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1535                          * define any equivalent to the AUTH right, so I've just used
1536                          * 'x' for now.
1537                          */
1538                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1539                         break;
1540                 default:
1541                         break;
1542                 }
1543
1544         }
1545         return(mask);
1546 }
1547
1548 static int
1549 aci_list_has_attr(
1550         struct berval *list,
1551         const struct berval *attr,
1552         struct berval *val )
1553 {
1554         struct berval bv, left, right;
1555         int i;
1556
1557         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1558                 if (aci_get_part(&bv, 0, '=', &left) < 0
1559                         || aci_get_part(&bv, 1, '=', &right) < 0)
1560                 {
1561                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1562                                 return(1);
1563                 } else if (val == NULL) {
1564                         if (ber_bvstrcasecmp(attr, &left) == 0)
1565                                 return(1);
1566                 } else {
1567                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1568                                 /* this is experimental code that implements a
1569                                  * simple (prefix) match of the attribute value.
1570                                  * the ACI draft does not provide for aci's that
1571                                  * apply to specific values, but it would be
1572                                  * nice to have.  If the <attr> part of an aci's
1573                                  * rights list is of the form <attr>=<value>,
1574                                  * that means the aci applies only to attrs with
1575                                  * the given value.  Furthermore, if the attr is
1576                                  * of the form <attr>=<value>*, then <value> is
1577                                  * treated as a prefix, and the aci applies to 
1578                                  * any value with that prefix.
1579                                  *
1580                                  * Ideally, this would allow r.e. matches.
1581                                  */
1582                                 if (aci_get_part(&right, 0, '*', &left) < 0
1583                                         || right.bv_len <= left.bv_len)
1584                                 {
1585                                         if (ber_bvstrcasecmp(val, &right) == 0)
1586                                                 return(1);
1587                                 } else if (val->bv_len >= left.bv_len) {
1588                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1589                                                 return(1);
1590                                 }
1591                         }
1592                 }
1593         }
1594         return(0);
1595 }
1596
1597 static slap_access_t
1598 aci_list_get_attr_rights(
1599         struct berval *list,
1600         const struct berval *attr,
1601         struct berval *val )
1602 {
1603     struct berval bv;
1604     slap_access_t mask;
1605     int i;
1606
1607         /* loop through each rights/attr pair, skip first part (action) */
1608         ACL_INIT(mask);
1609         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1610                 if (aci_list_has_attr(&bv, attr, val) == 0)
1611                         continue;
1612                 if (aci_get_part(list, i, ';', &bv) < 0)
1613                         continue;
1614                 mask |= aci_list_map_rights(&bv);
1615         }
1616         return(mask);
1617 }
1618
1619 static int
1620 aci_list_get_rights(
1621         struct berval *list,
1622         const struct berval *attr,
1623         struct berval *val,
1624         slap_access_t *grant,
1625         slap_access_t *deny )
1626 {
1627     struct berval perm, actn;
1628     slap_access_t *mask;
1629     int i, found;
1630
1631         if (attr == NULL || attr->bv_len == 0 
1632                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1633                 attr = &aci_bv_br_entry;
1634         }
1635
1636         found = 0;
1637         ACL_INIT(*grant);
1638         ACL_INIT(*deny);
1639         /* loop through each permissions clause */
1640         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1641                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1642                         continue;
1643                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1644                         mask = grant;
1645                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1646                         mask = deny;
1647                 } else {
1648                         continue;
1649                 }
1650
1651                 found = 1;
1652                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1653                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1654         }
1655         return(found);
1656 }
1657
1658 static int
1659 aci_group_member (
1660         struct berval *subj,
1661         struct berval *defgrpoc,
1662         struct berval *defgrpat,
1663     Backend             *be,
1664     Entry               *e,
1665     Connection          *conn,
1666     Operation           *op,
1667         regmatch_t      *matches
1668 )
1669 {
1670         struct berval subjdn;
1671         struct berval grpoc;
1672         struct berval grpat;
1673         ObjectClass *grp_oc = NULL;
1674         AttributeDescription *grp_ad = NULL;
1675         const char *text;
1676         int rc;
1677
1678         /* format of string is "group/objectClassValue/groupAttrName" */
1679         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1680                 return(0);
1681         }
1682
1683         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1684                 grpoc = *defgrpoc;
1685         }
1686
1687         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1688                 grpat = *defgrpat;
1689         }
1690
1691         rc = slap_bv2ad( &grpat, &grp_ad, &text );
1692         if( rc != LDAP_SUCCESS ) {
1693                 rc = 0;
1694                 goto done;
1695         }
1696         rc = 0;
1697
1698         grp_oc = oc_bvfind( &grpoc );
1699
1700         if (grp_oc != NULL && grp_ad != NULL ) {
1701                 char buf[ACL_BUF_SIZE];
1702                 struct berval bv, ndn;
1703                 bv.bv_len = sizeof( buf ) - 1;
1704                 bv.bv_val = (char *)&buf;
1705                 string_expand(&bv, &subjdn, e->e_ndn, matches);
1706                 if ( dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS ) {
1707                         rc = (backend_group(be, conn, op, e, &ndn, &op->o_ndn,
1708                                 grp_oc, grp_ad) == 0);
1709                         free( ndn.bv_val );
1710                 }
1711         }
1712
1713 done:
1714         return(rc);
1715 }
1716
1717 static int
1718 aci_mask(
1719     Backend                     *be,
1720     Connection          *conn,
1721     Operation           *op,
1722     Entry                       *e,
1723         AttributeDescription *desc,
1724     struct berval       *val,
1725     struct berval       *aci,
1726         regmatch_t              *matches,
1727         slap_access_t   *grant,
1728         slap_access_t   *deny
1729 )
1730 {
1731     struct berval bv, perms, sdn;
1732         int rc;
1733                 
1734
1735         assert( desc->ad_cname.bv_val != NULL );
1736
1737         /* parse an aci of the form:
1738                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1739
1740            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1741            a full description of the format for this attribute.
1742            Differences: "this" in the draft is "self" here, and
1743            "self" and "public" is in the position of dnType.
1744
1745            For now, this routine only supports scope=entry.
1746          */
1747
1748         /* check that the aci has all 5 components */
1749         if (aci_get_part(aci, 4, '#', NULL) < 0)
1750                 return(0);
1751
1752         /* check that the aci family is supported */
1753         if (aci_get_part(aci, 0, '#', &bv) < 0)
1754                 return(0);
1755
1756         /* check that the scope is "entry" */
1757         if (aci_get_part(aci, 1, '#', &bv) < 0
1758                 || ber_bvstrcasecmp( &aci_bv_entry, &bv ) != 0)
1759         {
1760                 return(0);
1761         }
1762
1763         /* get the list of permissions clauses, bail if empty */
1764         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1765                 return(0);
1766
1767         /* check if any permissions allow desired access */
1768         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1769                 return(0);
1770
1771         /* see if we have a DN match */
1772         if (aci_get_part(aci, 3, '#', &bv) < 0)
1773                 return(0);
1774
1775         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1776                 return(0);
1777
1778         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1779                 struct berval ndn;
1780                 rc = 0;
1781                 if ( dnNormalize2(NULL, &sdn, &ndn) == LDAP_SUCCESS ) {
1782                         if (dn_match( &op->o_ndn, &ndn))
1783                                 rc = 1;
1784                         free(ndn.bv_val);
1785                 }
1786                 return (rc);
1787
1788         } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
1789                 return(1);
1790
1791         } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1792                 if (dn_match(&op->o_ndn, &e->e_nname))
1793                         return(1);
1794
1795         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1796                 Attribute *at;
1797                 AttributeDescription *ad = NULL;
1798                 const char *text;
1799
1800                 rc = slap_bv2ad( &sdn, &ad, &text );
1801
1802                 if( rc != LDAP_SUCCESS ) {
1803                         return 0;
1804                 }
1805
1806                 rc = 0;
1807
1808                 bv = op->o_ndn;
1809
1810                 for(at = attrs_find( e->e_attrs, ad );
1811                         at != NULL;
1812                         at = attrs_find( at->a_next, ad ) )
1813                 {
1814                         if (value_find_ex( ad, SLAP_MR_VALUE_NORMALIZED_MATCH, at->a_vals, &bv) == 0 ) {
1815                                 rc = 1;
1816                                 break;
1817                         }
1818                 }
1819
1820                 return rc;
1821
1822
1823         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
1824                 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, be, e, conn, op, matches))
1825                         return(1);
1826
1827         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
1828                 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, be, e, conn, op, matches))
1829                         return(1);
1830
1831         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
1832                 if (aci_match_set(&sdn, be, e, conn, op, 0))
1833                         return(1);
1834
1835         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
1836                 if (aci_match_set(&sdn, be, e, conn, op, 1))
1837                         return(1);
1838
1839         }
1840
1841         return(0);
1842 }
1843
1844 #endif  /* SLAPD_ACI_ENABLED */
1845
1846 static void
1847 string_expand(
1848         struct berval *bv,
1849         struct berval *pat,
1850         char *match,
1851         regmatch_t *matches)
1852 {
1853         ber_len_t       size;
1854         char   *sp;
1855         char   *dp;
1856         int     flag;
1857
1858         size = 0;
1859         bv->bv_val[0] = '\0';
1860         bv->bv_len--; /* leave space for lone $ */
1861
1862         flag = 0;
1863         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
1864                 sp < pat->bv_val + pat->bv_len ; sp++ )
1865         {
1866                 /* did we previously see a $ */
1867                 if ( flag ) {
1868                         if ( flag == 1 && *sp == '$' ) {
1869                                 *dp++ = '$';
1870                                 size++;
1871                                 flag = 0;
1872
1873                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
1874                                 flag = 2;
1875
1876                         } else if ( *sp >= '0' && *sp <= '9' ) {
1877                                 int     n;
1878                                 int     i;
1879                                 int     l;
1880
1881                                 n = *sp - '0';
1882
1883                                 if ( flag == 2 ) {
1884                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
1885                                                 if ( *sp >= '0' && *sp <= '9' ) {
1886                                                         n = 10*n + ( *sp - '0' );
1887                                                 }
1888                                         }
1889
1890                                         if ( *sp != /*'{'*/ '}' ) {
1891                                                 /* error */
1892                                         }
1893                                 }
1894
1895                                 if ( n >= MAXREMATCHES ) {
1896                                 
1897                                 }
1898                                 
1899                                 *dp = '\0';
1900                                 i = matches[n].rm_so;
1901                                 l = matches[n].rm_eo; 
1902                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
1903                                         *dp++ = match[i];
1904                                 }
1905                                 *dp = '\0';
1906
1907                                 flag = 0;
1908                         }
1909                 } else {
1910                         if (*sp == '$') {
1911                                 flag = 1;
1912                         } else {
1913                                 *dp++ = *sp;
1914                                 size++;
1915                         }
1916                 }
1917         }
1918
1919         if ( flag ) {
1920                 /* must have ended with a single $ */
1921                 *dp++ = '$';
1922                 size++;
1923         }
1924
1925         *dp = '\0';
1926         bv->bv_len = size;
1927
1928 #ifdef NEW_LOGGING
1929         LDAP_LOG( ACL, DETAIL1, 
1930            "string_expand:  pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
1931         LDAP_LOG( ACL, DETAIL1, "string_expand:  expanded = %s\n", bv->bv_val, 0, 0 );
1932 #else
1933         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
1934         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
1935 #endif
1936 }
1937
1938 static int
1939 regex_matches(
1940         struct berval *pat,                     /* pattern to expand and match against */
1941         char *str,                              /* string to match against pattern */
1942         char *buf,                              /* buffer with $N expansion variables */
1943         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1944 )
1945 {
1946         regex_t re;
1947         char newbuf[ACL_BUF_SIZE];
1948         struct berval bv;
1949         int     rc;
1950
1951         bv.bv_len = sizeof(newbuf) - 1;
1952         bv.bv_val = newbuf;
1953
1954         if(str == NULL) str = "";
1955
1956         string_expand(&bv, pat, buf, matches);
1957         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1958                 char error[ACL_BUF_SIZE];
1959                 regerror(rc, &re, error, sizeof(error));
1960
1961 #ifdef NEW_LOGGING
1962                 LDAP_LOG( ACL, ERR, 
1963                            "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1964                            pat->bv_val, str, error  );
1965 #else
1966                 Debug( LDAP_DEBUG_TRACE,
1967                     "compile( \"%s\", \"%s\") failed %s\n",
1968                         pat->bv_val, str, error );
1969 #endif
1970                 return( 0 );
1971         }
1972
1973         rc = regexec(&re, str, 0, NULL, 0);
1974         regfree( &re );
1975
1976 #ifdef NEW_LOGGING
1977         LDAP_LOG( ACL, DETAIL2, "regex_matches: string:   %s\n", str, 0, 0 );
1978         LDAP_LOG( ACL, DETAIL2, "regex_matches: rc:     %d  %s\n",
1979                    rc, rc ? "matches" : "no matches", 0  );
1980 #else
1981         Debug( LDAP_DEBUG_TRACE,
1982             "=> regex_matches: string:   %s\n", str, 0, 0 );
1983         Debug( LDAP_DEBUG_TRACE,
1984             "=> regex_matches: rc: %d %s\n",
1985                 rc, !rc ? "matches" : "no matches", 0 );
1986 #endif
1987         return( !rc );
1988 }
1989