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