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