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