]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
bc79cbe7a820208a9f5e6d39c7ac1bf08820e246
[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 #ifdef SLAP_NVALUES
904                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
905                                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
906                                         at->a_nvals,
907 #else
908                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
909                                         at->a_vals,
910 #endif
911                                         &bv ) == 0 )
912                                 {
913                                         /* found it */
914                                         match = 1;
915                                         break;
916                                 }
917                         }
918
919                         if( match ) {
920                                 /* have a dnattr match. if this is a self clause then
921                                  * the target must also match the op dn.
922                                  */
923                                 if ( b->a_dn_self ) {
924                                         /* check if the target is an attribute. */
925                                         if ( val == NULL ) continue;
926
927                                         /* target is attribute, check if the attribute value
928                                          * is the op dn.
929                                          */
930                                         rc = value_match( &match, b->a_dn_at,
931                                                 b->a_dn_at->ad_type->sat_equality, 0,
932                                                 val, &bv, &text );
933                                         /* on match error or no match, fail the ACL clause */
934                                         if (rc != LDAP_SUCCESS || match != 0 )
935                                                 continue;
936                                 }
937                         } else {
938                                 /* no dnattr match, check if this is a self clause */
939                                 if ( ! b->a_dn_self )
940                                         continue;
941
942                                 ACL_RECORD_VALUE_STATE;
943                                 
944                                 /* this is a self clause, check if the target is an
945                                  * attribute.
946                                  */
947                                 if ( val == NULL )
948                                         continue;
949
950                                 /* target is attribute, check if the attribute value
951                                  * is the op dn.
952                                  */
953                                 rc = value_match( &match, b->a_dn_at,
954                                         b->a_dn_at->ad_type->sat_equality, 0,
955                                         val, &bv, &text );
956
957                                 /* on match error or no match, fail the ACL clause */
958                                 if (rc != LDAP_SUCCESS || match != 0 )
959                                         continue;
960                         }
961                 }
962
963                 if ( b->a_group_pat.bv_len ) {
964                         struct berval bv;
965                         struct berval ndn = { 0, NULL };
966                         int rc;
967
968                         if ( op->o_ndn.bv_len == 0 ) {
969                                 continue;
970                         }
971
972                         /* b->a_group is an unexpanded entry name, expanded it should be an 
973                          * entry with objectclass group* and we test to see if odn is one of
974                          * the values in the attribute group
975                          */
976                         /* see if asker is listed in dnattr */
977                         if ( b->a_group_style == ACL_STYLE_REGEX ) {
978                                 char buf[ACL_BUF_SIZE];
979                                 bv.bv_len = sizeof(buf) - 1;
980                                 bv.bv_val = buf; 
981
982                                 string_expand( &bv, &b->a_group_pat, e->e_ndn, matches );
983                                 if ( dnNormalize2( NULL, &bv, &ndn ) != LDAP_SUCCESS ) {
984                                         /* did not expand to a valid dn */
985                                         continue;
986                                 }
987
988                                 bv = ndn;
989
990                         } else {
991                                 bv = b->a_group_pat;
992                         }
993
994                         rc = backend_group( op, e, &bv, &op->o_ndn,
995                                 b->a_group_oc, b->a_group_at );
996
997                         if ( ndn.bv_val ) free( ndn.bv_val );
998
999                         if ( rc != 0 ) {
1000                                 continue;
1001                         }
1002                 }
1003
1004                 if ( b->a_set_pat.bv_len != 0 ) {
1005                         struct berval bv;
1006                         char buf[ACL_BUF_SIZE];
1007                         if( b->a_set_style == ACL_STYLE_REGEX ){
1008                                 bv.bv_len = sizeof(buf) - 1;
1009                                 bv.bv_val = buf;
1010                                 string_expand( &bv, &b->a_set_pat, e->e_ndn, matches );
1011                         }else{
1012                                 bv = b->a_set_pat;
1013                         }
1014                         if (aci_match_set( &bv, op, e, 0 ) == 0) {
1015                                 continue;
1016                         }
1017                 }
1018
1019                 if ( b->a_authz.sai_ssf ) {
1020 #ifdef NEW_LOGGING
1021                         LDAP_LOG( ACL, DETAIL1, 
1022                                 "acl_mask: conn %lu  check a_authz.sai_ssf: ACL %u > OP %u\n",
1023                                 op->o_connid, b->a_authz.sai_ssf, op->o_ssf  );
1024 #else
1025                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1026                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1027 #endif
1028                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1029                                 continue;
1030                         }
1031                 }
1032
1033                 if ( b->a_authz.sai_transport_ssf ) {
1034 #ifdef NEW_LOGGING
1035                         LDAP_LOG( ACL, DETAIL1, 
1036                                 "acl_mask: conn %lu  check a_authz.sai_transport_ssf: "
1037                                 "ACL %u > OP %u\n",
1038                                 op->o_connid, b->a_authz.sai_transport_ssf, 
1039                                 op->o_transport_ssf  );
1040 #else
1041                         Debug( LDAP_DEBUG_ACL,
1042                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1043                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1044 #endif
1045                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1046                                 continue;
1047                         }
1048                 }
1049
1050                 if ( b->a_authz.sai_tls_ssf ) {
1051 #ifdef NEW_LOGGING
1052                         LDAP_LOG( ACL, DETAIL1, 
1053                                 "acl_mask: conn %lu  check a_authz.sai_tls_ssf: ACL %u > "
1054                                 "OP %u\n",
1055                                 op->o_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf  );
1056 #else
1057                         Debug( LDAP_DEBUG_ACL,
1058                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1059                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1060 #endif
1061                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1062                                 continue;
1063                         }
1064                 }
1065
1066                 if ( b->a_authz.sai_sasl_ssf ) {
1067 #ifdef NEW_LOGGING
1068                         LDAP_LOG( ACL, DETAIL1, 
1069                            "acl_mask: conn %lu check a_authz.sai_sasl_ssf: " 
1070                            "ACL %u > OP %u\n",
1071                                 op->o_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf );
1072 #else
1073                         Debug( LDAP_DEBUG_ACL,
1074                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1075                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1076 #endif
1077                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1078                                 continue;
1079                         }
1080                 }
1081
1082 #ifdef SLAPD_ACI_ENABLED
1083                 if ( b->a_aci_at != NULL ) {
1084                         Attribute       *at;
1085                         slap_access_t grant, deny, tgrant, tdeny;
1086
1087                         /* this case works different from the others above.
1088                          * since aci's themselves give permissions, we need
1089                          * to first check b->a_access_mask, the ACL's access level.
1090                          */
1091
1092                         if ( e->e_nname.bv_len == 0 ) {
1093                                 /* no ACIs in the root DSE */
1094                                 continue;
1095                         }
1096
1097                         /* first check if the right being requested
1098                          * is allowed by the ACL clause.
1099                          */
1100                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
1101                                 continue;
1102                         }
1103
1104                         /* get the aci attribute */
1105                         at = attr_find( e->e_attrs, b->a_aci_at );
1106                         if ( at == NULL ) {
1107                                 continue;
1108                         }
1109
1110                         ACL_RECORD_VALUE_STATE;
1111
1112                         /* start out with nothing granted, nothing denied */
1113                         ACL_INIT(tgrant);
1114                         ACL_INIT(tdeny);
1115
1116                         /* the aci is an multi-valued attribute.  The
1117                          * rights are determined by OR'ing the individual
1118                          * rights given by the acis.
1119                          */
1120                         for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
1121                                 if (aci_mask( op,
1122                                         e, desc, val,
1123 #ifdef SLAP_NVALUES
1124                                         &at->a_nvals[i],
1125 #else
1126                                         &at->a_vals[i],
1127 #endif
1128                                         matches, &grant, &deny ) != 0)
1129                                 {
1130                                         tgrant |= grant;
1131                                         tdeny |= deny;
1132                                 }
1133                         }
1134
1135                         /* remove anything that the ACL clause does not allow */
1136                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1137                         tdeny &= ACL_PRIV_MASK;
1138
1139                         /* see if we have anything to contribute */
1140                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1141                                 continue;
1142                         }
1143
1144                         /* this could be improved by changing acl_mask so that it can deal with
1145                          * by clauses that return grant/deny pairs.  Right now, it does either
1146                          * additive or subtractive rights, but not both at the same time.  So,
1147                          * we need to combine the grant/deny pair into a single rights mask in
1148                          * a smart way:  if either grant or deny is "empty", then we use the
1149                          * opposite as is, otherwise we remove any denied rights from the grant
1150                          * rights mask and construct an additive mask.
1151                          */
1152                         if (ACL_IS_INVALID(tdeny)) {
1153                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1154
1155                         } else if (ACL_IS_INVALID(tgrant)) {
1156                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1157
1158                         } else {
1159                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1160                         }
1161
1162                 } else
1163 #endif
1164                 {
1165                         modmask = b->a_access_mask;
1166                 }
1167
1168 #ifdef NEW_LOGGING
1169                 LDAP_LOG( ACL, RESULTS, 
1170                            "acl_mask: [%d] applying %s (%s)\n",
1171                            i, accessmask2str( modmask, accessmaskbuf),
1172                            b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
1173                            ? "break" : "stop"  );
1174 #else
1175                 Debug( LDAP_DEBUG_ACL,
1176                         "<= acl_mask: [%d] applying %s (%s)\n",
1177                         i, accessmask2str( modmask, accessmaskbuf ), 
1178                         b->a_type == ACL_CONTINUE
1179                                 ? "continue"
1180                                 : b->a_type == ACL_BREAK
1181                                         ? "break"
1182                                         : "stop" );
1183 #endif
1184                 /* save old mask */
1185                 oldmask = *mask;
1186
1187                 if( ACL_IS_ADDITIVE(modmask) ) {
1188                         /* add privs */
1189                         ACL_PRIV_SET( *mask, modmask );
1190
1191                         /* cleanup */
1192                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1193
1194                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1195                         /* substract privs */
1196                         ACL_PRIV_CLR( *mask, modmask );
1197
1198                         /* cleanup */
1199                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1200
1201                 } else {
1202                         /* assign privs */
1203                         *mask = modmask;
1204                 }
1205
1206 #ifdef NEW_LOGGING
1207                 LDAP_LOG( ACL, DETAIL1, 
1208                            "acl_mask: conn %lu  [%d] mask: %s\n",
1209                            op->o_connid, i, accessmask2str( *mask, accessmaskbuf)  );
1210 #else
1211                 Debug( LDAP_DEBUG_ACL,
1212                         "<= acl_mask: [%d] mask: %s\n",
1213                         i, accessmask2str(*mask, accessmaskbuf), 0 );
1214 #endif
1215
1216                 if( b->a_type == ACL_CONTINUE ) {
1217                         continue;
1218
1219                 } else if ( b->a_type == ACL_BREAK ) {
1220                         return ACL_BREAK;
1221
1222                 } else {
1223                         return ACL_STOP;
1224                 }
1225         }
1226
1227         /* implicit "by * none" clause */
1228         ACL_INIT(*mask);
1229
1230 #ifdef NEW_LOGGING
1231         LDAP_LOG( ACL, RESULTS, 
1232                    "acl_mask: conn %lu  no more <who> clauses, returning %d (stop)\n",
1233                    op->o_connid, accessmask2str( *mask, accessmaskbuf) , 0 );
1234 #else
1235         Debug( LDAP_DEBUG_ACL,
1236                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1237                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1238 #endif
1239         return ACL_STOP;
1240 }
1241
1242 /*
1243  * acl_check_modlist - check access control on the given entry to see if
1244  * it allows the given modifications by the user associated with op.
1245  * returns      1       if mods allowed ok
1246  *                      0       mods not allowed
1247  */
1248
1249 int
1250 acl_check_modlist(
1251         Operation       *op,
1252         Entry   *e,
1253         Modifications   *mlist
1254 )
1255 {
1256         struct berval *bv;
1257         AccessControlState state = ACL_STATE_INIT;
1258
1259         assert( op->o_bd != NULL );
1260
1261         /* short circuit root database access */
1262         if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
1263 #ifdef NEW_LOGGING
1264                 LDAP_LOG( ACL, DETAIL1, 
1265                            "acl_check_modlist: conn %lu  access granted to root user\n",
1266                            op->o_connid, 0, 0 );
1267 #else
1268                 Debug( LDAP_DEBUG_ACL,
1269                         "<= acl_access_allowed: granted to database root\n",
1270                     0, 0, 0 );
1271 #endif
1272                 return 1;
1273         }
1274
1275         /* use backend default access if no backend acls */
1276         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1277 #ifdef NEW_LOGGING
1278                 LDAP_LOG( ACL, DETAIL1, 
1279                         "acl_check_modlist: backend default %s access %s to \"%s\"\n",
1280                         access2str( ACL_WRITE ),
1281                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", 
1282                         op->o_dn.bv_val  );
1283 #else
1284                 Debug( LDAP_DEBUG_ACL,
1285                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1286                         access2str( ACL_WRITE ),
1287                         op->o_bd->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1288 #endif
1289                 return op->o_bd->be_dfltaccess >= ACL_WRITE;
1290
1291 #ifdef notdef
1292         /* op->o_bd is always non-NULL */
1293         /* use global default access if no global acls */
1294         } else if ( op->o_bd == NULL && global_acl == NULL ) {
1295 #ifdef NEW_LOGGING
1296                 LDAP_LOG( ACL, DETAIL1, 
1297                         "acl_check_modlist: global default %s access %s to \"%s\"\n",
1298                    access2str( ACL_WRITE ),
1299                    global_default_access >= ACL_WRITE ? "granted" : "denied", 
1300                    op->o_dn  );
1301 #else
1302                 Debug( LDAP_DEBUG_ACL,
1303                         "=> access_allowed: global default %s access %s to \"%s\"\n",
1304                         access2str( ACL_WRITE ),
1305                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1306 #endif
1307                 return global_default_access >= ACL_WRITE;
1308 #endif
1309         }
1310
1311         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1312                 /*
1313                  * no-user-modification operational attributes are ignored
1314                  * by ACL_WRITE checking as any found here are not provided
1315                  * by the user
1316                  */
1317                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1318 #ifdef NEW_LOGGING
1319                         LDAP_LOG( ACL, DETAIL1, 
1320                                    "acl_check_modlist: conn %lu  no-user-mod %s: modify access granted\n",
1321                                    op->o_connid, mlist->sml_desc->ad_cname.bv_val , 0 );
1322 #else
1323                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1324                                 " modify access granted\n",
1325                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1326 #endif
1327                         continue;
1328                 }
1329
1330                 switch ( mlist->sml_op ) {
1331                 case LDAP_MOD_REPLACE:
1332                         /*
1333                          * We must check both permission to delete the whole
1334                          * attribute and permission to add the specific attributes.
1335                          * This prevents abuse from selfwriters.
1336                          */
1337                         if ( ! access_allowed( op, e,
1338                                 mlist->sml_desc, NULL, ACL_WRITE, &state ) )
1339                         {
1340                                 return( 0 );
1341                         }
1342
1343                         if ( mlist->sml_bvalues == NULL ) break;
1344
1345                         /* fall thru to check value to add */
1346
1347                 case LDAP_MOD_ADD:
1348                         assert( mlist->sml_bvalues != NULL );
1349
1350 #ifdef SLAP_NVALUES
1351                         for ( bv = mlist->sml_nvalues
1352                                         ? mlist->sml_nvalues : mlist->sml_values;
1353                                 bv->bv_val != NULL; bv++ )
1354 #else
1355                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ )
1356 #endif
1357                         {
1358                                 if ( ! access_allowed( op, e,
1359                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1360                                 {
1361                                         return( 0 );
1362                                 }
1363                         }
1364                         break;
1365
1366                 case LDAP_MOD_DELETE:
1367                         if ( mlist->sml_bvalues == NULL ) {
1368                                 if ( ! access_allowed( op, e,
1369                                         mlist->sml_desc, NULL, ACL_WRITE, NULL ) )
1370                                 {
1371                                         return( 0 );
1372                                 }
1373                                 break;
1374                         }
1375 #ifdef SLAP_NVALUES
1376                         for ( bv = mlist->sml_nvalues
1377                                         ? mlist->sml_nvalues : mlist->sml_values;
1378                                 bv->bv_val != NULL; bv++ )
1379 #else
1380                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ )
1381 #endif
1382                         {
1383                                 if ( ! access_allowed( op, e,
1384                                         mlist->sml_desc, bv, ACL_WRITE, &state ) )
1385                                 {
1386                                         return( 0 );
1387                                 }
1388                         }
1389                         break;
1390
1391                 case SLAP_MOD_SOFTADD:
1392                         /* allow adding attribute via modrdn thru */
1393                         break;
1394
1395                 default:
1396                         assert( 0 );
1397                         return( 0 );
1398                 }
1399         }
1400
1401         return( 1 );
1402 }
1403
1404 static int
1405 aci_get_part(
1406         struct berval *list,
1407         int ix,
1408         char sep,
1409         struct berval *bv )
1410 {
1411         int len;
1412         char *p;
1413
1414         if (bv) {
1415                 bv->bv_len = 0;
1416                 bv->bv_val = NULL;
1417         }
1418         len = list->bv_len;
1419         p = list->bv_val;
1420         while (len >= 0 && --ix >= 0) {
1421                 while (--len >= 0 && *p++ != sep) ;
1422         }
1423         while (len >= 0 && *p == ' ') {
1424                 len--;
1425                 p++;
1426         }
1427         if (len < 0)
1428                 return(-1);
1429
1430         if (!bv)
1431                 return(0);
1432
1433         bv->bv_val = p;
1434         while (--len >= 0 && *p != sep) {
1435                 bv->bv_len++;
1436                 p++;
1437         }
1438         while (bv->bv_len > 0 && *--p == ' ')
1439                 bv->bv_len--;
1440         return(bv->bv_len);
1441 }
1442
1443 BerVarray
1444 aci_set_gather (void *cookie, struct berval *name, struct berval *attr)
1445 {
1446         AciSetCookie *cp = cookie;
1447         BerVarray bvals = NULL;
1448         struct berval ndn;
1449
1450         /* this routine needs to return the bervals instead of
1451          * plain strings, since syntax is not known.  It should
1452          * also return the syntax or some "comparison cookie".
1453          */
1454
1455         if (dnNormalize2(NULL, name, &ndn) == LDAP_SUCCESS) {
1456                 const char *text;
1457                 AttributeDescription *desc = NULL;
1458                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1459                         backend_attribute(cp->op,
1460                                 cp->e, &ndn, desc, &bvals);
1461                 }
1462                 free(ndn.bv_val);
1463         }
1464         return(bvals);
1465 }
1466
1467 static int
1468 aci_match_set (
1469         struct berval *subj,
1470         Operation *op,
1471         Entry *e,
1472         int setref
1473 )
1474 {
1475         struct berval set = { 0, NULL };
1476         int rc = 0;
1477         AciSetCookie cookie;
1478
1479         if (setref == 0) {
1480                 ber_dupbv( &set, subj );
1481         } else {
1482                 struct berval subjdn, ndn = { 0, NULL };
1483                 struct berval setat;
1484                 BerVarray bvals;
1485                 const char *text;
1486                 AttributeDescription *desc = NULL;
1487
1488                 /* format of string is "entry/setAttrName" */
1489                 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1490                         return(0);
1491                 }
1492
1493                 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1494                         setat.bv_val = SLAPD_ACI_SET_ATTR;
1495                         setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1496                 }
1497
1498                 if ( setat.bv_val != NULL ) {
1499                         /*
1500                          * NOTE: dnNormalize2 honors the ber_len field
1501                          * as the length of the dn to be normalized
1502                          */
1503                         if ( dnNormalize2(NULL, &subjdn, &ndn) == LDAP_SUCCESS
1504                                 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1505                         {
1506                                 backend_attribute(op, e,
1507                                         &ndn, desc, &bvals);
1508                                 if ( bvals != NULL ) {
1509                                         if ( bvals[0].bv_val != NULL ) {
1510                                                 int i;
1511                                                 set = bvals[0];
1512                                                 bvals[0].bv_val = NULL;
1513                                                 for (i=1;bvals[i].bv_val;i++);
1514                                                 bvals[0].bv_val = bvals[i-1].bv_val;
1515                                                 bvals[i-1].bv_val = NULL;
1516                                         }
1517                                         ber_bvarray_free(bvals);
1518                                 }
1519                         }
1520                         if (ndn.bv_val)
1521                                 free(ndn.bv_val);
1522                 }
1523         }
1524
1525         if (set.bv_val != NULL) {
1526                 cookie.op = op;
1527                 cookie.e = e;
1528                 rc = (slap_set_filter(aci_set_gather, &cookie, &set,
1529                         &op->o_ndn, &e->e_nname, NULL) > 0);
1530                 ch_free(set.bv_val);
1531         }
1532         return(rc);
1533 }
1534
1535 #ifdef SLAPD_ACI_ENABLED
1536 static int
1537 aci_list_map_rights(
1538         struct berval *list )
1539 {
1540         struct berval bv;
1541         slap_access_t mask;
1542         int i;
1543
1544         ACL_INIT(mask);
1545         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1546                 if (bv.bv_len <= 0)
1547                         continue;
1548                 switch (*bv.bv_val) {
1549                 case 'c':
1550                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1551                         break;
1552                 case 's':
1553                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1554                          * the right 's' to mean "set", but in the examples states
1555                          * that the right 's' means "search".  The latter definition
1556                          * is used here.
1557                          */
1558                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1559                         break;
1560                 case 'r':
1561                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1562                         break;
1563                 case 'w':
1564                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1565                         break;
1566                 case 'x':
1567                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1568                          * define any equivalent to the AUTH right, so I've just used
1569                          * 'x' for now.
1570                          */
1571                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1572                         break;
1573                 default:
1574                         break;
1575                 }
1576
1577         }
1578         return(mask);
1579 }
1580
1581 static int
1582 aci_list_has_attr(
1583         struct berval *list,
1584         const struct berval *attr,
1585         struct berval *val )
1586 {
1587         struct berval bv, left, right;
1588         int i;
1589
1590         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1591                 if (aci_get_part(&bv, 0, '=', &left) < 0
1592                         || aci_get_part(&bv, 1, '=', &right) < 0)
1593                 {
1594                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1595                                 return(1);
1596                 } else if (val == NULL) {
1597                         if (ber_bvstrcasecmp(attr, &left) == 0)
1598                                 return(1);
1599                 } else {
1600                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1601                                 /* this is experimental code that implements a
1602                                  * simple (prefix) match of the attribute value.
1603                                  * the ACI draft does not provide for aci's that
1604                                  * apply to specific values, but it would be
1605                                  * nice to have.  If the <attr> part of an aci's
1606                                  * rights list is of the form <attr>=<value>,
1607                                  * that means the aci applies only to attrs with
1608                                  * the given value.  Furthermore, if the attr is
1609                                  * of the form <attr>=<value>*, then <value> is
1610                                  * treated as a prefix, and the aci applies to 
1611                                  * any value with that prefix.
1612                                  *
1613                                  * Ideally, this would allow r.e. matches.
1614                                  */
1615                                 if (aci_get_part(&right, 0, '*', &left) < 0
1616                                         || right.bv_len <= left.bv_len)
1617                                 {
1618                                         if (ber_bvstrcasecmp(val, &right) == 0)
1619                                                 return(1);
1620                                 } else if (val->bv_len >= left.bv_len) {
1621                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1622                                                 return(1);
1623                                 }
1624                         }
1625                 }
1626         }
1627         return(0);
1628 }
1629
1630 static slap_access_t
1631 aci_list_get_attr_rights(
1632         struct berval *list,
1633         const struct berval *attr,
1634         struct berval *val )
1635 {
1636     struct berval bv;
1637     slap_access_t mask;
1638     int i;
1639
1640         /* loop through each rights/attr pair, skip first part (action) */
1641         ACL_INIT(mask);
1642         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1643                 if (aci_list_has_attr(&bv, attr, val) == 0)
1644                         continue;
1645                 if (aci_get_part(list, i, ';', &bv) < 0)
1646                         continue;
1647                 mask |= aci_list_map_rights(&bv);
1648         }
1649         return(mask);
1650 }
1651
1652 static int
1653 aci_list_get_rights(
1654         struct berval *list,
1655         const struct berval *attr,
1656         struct berval *val,
1657         slap_access_t *grant,
1658         slap_access_t *deny )
1659 {
1660     struct berval perm, actn;
1661     slap_access_t *mask;
1662     int i, found;
1663
1664         if (attr == NULL || attr->bv_len == 0 
1665                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1666                 attr = &aci_bv_br_entry;
1667         }
1668
1669         found = 0;
1670         ACL_INIT(*grant);
1671         ACL_INIT(*deny);
1672         /* loop through each permissions clause */
1673         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1674                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1675                         continue;
1676                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1677                         mask = grant;
1678                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1679                         mask = deny;
1680                 } else {
1681                         continue;
1682                 }
1683
1684                 found = 1;
1685                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1686                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1687         }
1688         return(found);
1689 }
1690
1691 static int
1692 aci_group_member (
1693         struct berval *subj,
1694         struct berval *defgrpoc,
1695         struct berval *defgrpat,
1696         Operation               *op,
1697         Entry           *e,
1698         regmatch_t      *matches
1699 )
1700 {
1701         struct berval subjdn;
1702         struct berval grpoc;
1703         struct berval grpat;
1704         ObjectClass *grp_oc = NULL;
1705         AttributeDescription *grp_ad = NULL;
1706         const char *text;
1707         int rc;
1708
1709         /* format of string is "group/objectClassValue/groupAttrName" */
1710         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1711                 return(0);
1712         }
1713
1714         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1715                 grpoc = *defgrpoc;
1716         }
1717
1718         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1719                 grpat = *defgrpat;
1720         }
1721
1722         rc = slap_bv2ad( &grpat, &grp_ad, &text );
1723         if( rc != LDAP_SUCCESS ) {
1724                 rc = 0;
1725                 goto done;
1726         }
1727         rc = 0;
1728
1729         grp_oc = oc_bvfind( &grpoc );
1730
1731         if (grp_oc != NULL && grp_ad != NULL ) {
1732                 char buf[ACL_BUF_SIZE];
1733                 struct berval bv, ndn;
1734                 bv.bv_len = sizeof( buf ) - 1;
1735                 bv.bv_val = (char *)&buf;
1736                 string_expand(&bv, &subjdn, e->e_ndn, matches);
1737                 if ( dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS ) {
1738                         rc = (backend_group(op, e, &ndn, &op->o_ndn,
1739                                 grp_oc, grp_ad) == 0);
1740                         free( ndn.bv_val );
1741                 }
1742         }
1743
1744 done:
1745         return(rc);
1746 }
1747
1748 static int
1749 aci_mask(
1750     Operation           *op,
1751     Entry                       *e,
1752         AttributeDescription *desc,
1753     struct berval       *val,
1754     struct berval       *aci,
1755         regmatch_t              *matches,
1756         slap_access_t   *grant,
1757         slap_access_t   *deny
1758 )
1759 {
1760     struct berval bv, perms, sdn;
1761         int rc;
1762                 
1763
1764         assert( desc->ad_cname.bv_val != NULL );
1765
1766         /* parse an aci of the form:
1767                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1768
1769            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1770            a full description of the format for this attribute.
1771            Differences: "this" in the draft is "self" here, and
1772            "self" and "public" is in the position of dnType.
1773
1774            For now, this routine only supports scope=entry.
1775          */
1776
1777         /* check that the aci has all 5 components */
1778         if (aci_get_part(aci, 4, '#', NULL) < 0)
1779                 return(0);
1780
1781         /* check that the aci family is supported */
1782         if (aci_get_part(aci, 0, '#', &bv) < 0)
1783                 return(0);
1784
1785         /* check that the scope is "entry" */
1786         if (aci_get_part(aci, 1, '#', &bv) < 0
1787                 || ber_bvstrcasecmp( &aci_bv_entry, &bv ) != 0)
1788         {
1789                 return(0);
1790         }
1791
1792         /* get the list of permissions clauses, bail if empty */
1793         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1794                 return(0);
1795
1796         /* check if any permissions allow desired access */
1797         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1798                 return(0);
1799
1800         /* see if we have a DN match */
1801         if (aci_get_part(aci, 3, '#', &bv) < 0)
1802                 return(0);
1803
1804         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1805                 return(0);
1806
1807         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1808                 struct berval ndn;
1809                 rc = 0;
1810                 if ( dnNormalize2(NULL, &sdn, &ndn) == LDAP_SUCCESS ) {
1811                         if (dn_match( &op->o_ndn, &ndn))
1812                                 rc = 1;
1813                         free(ndn.bv_val);
1814                 }
1815                 return (rc);
1816
1817         } else if (ber_bvstrcasecmp( &aci_bv_public, &bv ) == 0) {
1818                 return(1);
1819
1820         } else if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1821                 if (dn_match(&op->o_ndn, &e->e_nname))
1822                         return(1);
1823
1824         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1825                 Attribute *at;
1826                 AttributeDescription *ad = NULL;
1827                 const char *text;
1828
1829                 rc = slap_bv2ad( &sdn, &ad, &text );
1830
1831                 if( rc != LDAP_SUCCESS ) {
1832                         return 0;
1833                 }
1834
1835                 rc = 0;
1836
1837                 bv = op->o_ndn;
1838
1839                 for(at = attrs_find( e->e_attrs, ad );
1840                         at != NULL;
1841                         at = attrs_find( at->a_next, ad ) )
1842                 {
1843                         if (value_find_ex( ad,
1844 #ifdef SLAP_NVALUES
1845                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1846                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1847                                 at->a_nvals,
1848 #else
1849                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1850                                 at->a_vals,
1851 #endif
1852                                 &bv) == 0 )
1853                         {
1854                                 rc = 1;
1855                                 break;
1856                         }
1857                 }
1858
1859                 return rc;
1860
1861
1862         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
1863                 if (aci_group_member(&sdn, &aci_bv_group_class, &aci_bv_group_attr, op, e, matches))
1864                         return(1);
1865
1866         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
1867                 if (aci_group_member(&sdn, &aci_bv_role_class, &aci_bv_role_attr, op, e, matches))
1868                         return(1);
1869
1870         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
1871                 if (aci_match_set(&sdn, op, e, 0))
1872                         return(1);
1873
1874         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
1875                 if (aci_match_set(&sdn, op, e, 1))
1876                         return(1);
1877
1878         }
1879
1880         return(0);
1881 }
1882
1883 #endif  /* SLAPD_ACI_ENABLED */
1884
1885 static void
1886 string_expand(
1887         struct berval *bv,
1888         struct berval *pat,
1889         char *match,
1890         regmatch_t *matches)
1891 {
1892         ber_len_t       size;
1893         char   *sp;
1894         char   *dp;
1895         int     flag;
1896
1897         size = 0;
1898         bv->bv_val[0] = '\0';
1899         bv->bv_len--; /* leave space for lone $ */
1900
1901         flag = 0;
1902         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
1903                 sp < pat->bv_val + pat->bv_len ; sp++ )
1904         {
1905                 /* did we previously see a $ */
1906                 if ( flag ) {
1907                         if ( flag == 1 && *sp == '$' ) {
1908                                 *dp++ = '$';
1909                                 size++;
1910                                 flag = 0;
1911
1912                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
1913                                 flag = 2;
1914
1915                         } else if ( *sp >= '0' && *sp <= '9' ) {
1916                                 int     n;
1917                                 int     i;
1918                                 int     l;
1919
1920                                 n = *sp - '0';
1921
1922                                 if ( flag == 2 ) {
1923                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
1924                                                 if ( *sp >= '0' && *sp <= '9' ) {
1925                                                         n = 10*n + ( *sp - '0' );
1926                                                 }
1927                                         }
1928
1929                                         if ( *sp != /*'{'*/ '}' ) {
1930                                                 /* error */
1931                                         }
1932                                 }
1933
1934                                 if ( n >= MAXREMATCHES ) {
1935                                 
1936                                 }
1937                                 
1938                                 *dp = '\0';
1939                                 i = matches[n].rm_so;
1940                                 l = matches[n].rm_eo; 
1941                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
1942                                         *dp++ = match[i];
1943                                 }
1944                                 *dp = '\0';
1945
1946                                 flag = 0;
1947                         }
1948                 } else {
1949                         if (*sp == '$') {
1950                                 flag = 1;
1951                         } else {
1952                                 *dp++ = *sp;
1953                                 size++;
1954                         }
1955                 }
1956         }
1957
1958         if ( flag ) {
1959                 /* must have ended with a single $ */
1960                 *dp++ = '$';
1961                 size++;
1962         }
1963
1964         *dp = '\0';
1965         bv->bv_len = size;
1966
1967 #ifdef NEW_LOGGING
1968         LDAP_LOG( ACL, DETAIL1, 
1969            "string_expand:  pattern = %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
1970         LDAP_LOG( ACL, DETAIL1, "string_expand:  expanded = %s\n", bv->bv_val, 0, 0 );
1971 #else
1972         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
1973         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
1974 #endif
1975 }
1976
1977 static int
1978 regex_matches(
1979         struct berval *pat,                     /* pattern to expand and match against */
1980         char *str,                              /* string to match against pattern */
1981         char *buf,                              /* buffer with $N expansion variables */
1982         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1983 )
1984 {
1985         regex_t re;
1986         char newbuf[ACL_BUF_SIZE];
1987         struct berval bv;
1988         int     rc;
1989
1990         bv.bv_len = sizeof(newbuf) - 1;
1991         bv.bv_val = newbuf;
1992
1993         if(str == NULL) str = "";
1994
1995         string_expand(&bv, pat, buf, matches);
1996         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1997                 char error[ACL_BUF_SIZE];
1998                 regerror(rc, &re, error, sizeof(error));
1999
2000 #ifdef NEW_LOGGING
2001                 LDAP_LOG( ACL, ERR, 
2002                            "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
2003                            pat->bv_val, str, error  );
2004 #else
2005                 Debug( LDAP_DEBUG_TRACE,
2006                     "compile( \"%s\", \"%s\") failed %s\n",
2007                         pat->bv_val, str, error );
2008 #endif
2009                 return( 0 );
2010         }
2011
2012         rc = regexec(&re, str, 0, NULL, 0);
2013         regfree( &re );
2014
2015 #ifdef NEW_LOGGING
2016         LDAP_LOG( ACL, DETAIL2, "regex_matches: string:   %s\n", str, 0, 0 );
2017         LDAP_LOG( ACL, DETAIL2, "regex_matches: rc:     %d  %s\n",
2018                    rc, rc ? "matches" : "no matches", 0  );
2019 #else
2020         Debug( LDAP_DEBUG_TRACE,
2021             "=> regex_matches: string:   %s\n", str, 0, 0 );
2022         Debug( LDAP_DEBUG_TRACE,
2023             "=> regex_matches: rc: %d %s\n",
2024                 rc, !rc ? "matches" : "no matches", 0 );
2025 #endif
2026         return( !rc );
2027 }
2028