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