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