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