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