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