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