]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
98b3ea32b0351e1fa528ba96af730a80f118cf21
[openldap] / servers / slapd / acl.c
1 /* acl.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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
19 static AccessControl * acl_get(
20         AccessControl *ac, int *count,
21         Backend *be, Operation *op,
22         Entry *e,
23         AttributeDescription *desc,
24         int nmatches, regmatch_t *matches );
25
26 static slap_control_t acl_mask(
27         AccessControl *ac, slap_mask_t *mask,
28         Backend *be, Connection *conn, Operation *op,
29         Entry *e,
30         AttributeDescription *desc,
31         struct berval *val,
32         regmatch_t *matches );
33
34 #ifdef SLAPD_ACI_ENABLED
35 static int aci_mask(
36         Backend *be,
37     Connection *conn,
38         Operation *op,
39         Entry *e,
40         AttributeDescription *desc,
41         struct berval *val,
42         struct berval *aci,
43         regmatch_t *matches,
44         slap_access_t *grant,
45         slap_access_t *deny );
46 #endif
47
48 static int      regex_matches(
49         char *pat, char *str, char *buf, regmatch_t *matches);
50 static void     string_expand(
51         char *newbuf, int bufsiz, char *pattern,
52         char *match, regmatch_t *matches);
53
54 char **aci_set_gather (void *cookie, char *name, char *attr);
55 static int aci_match_set ( struct berval *subj, Backend *be,
56     Entry *e, Connection *conn, Operation *op, int setref );
57
58 /*
59  * access_allowed - check whether op->o_ndn is allowed the requested access
60  * to entry e, attribute attr, value val.  if val is null, access to
61  * the whole attribute is assumed (all values).
62  *
63  * This routine loops through all access controls and calls
64  * acl_mask() on each applicable access control.
65  * The loop exits when a definitive answer is reached or
66  * or no more controls remain.
67  *
68  * returns:
69  *              0       access denied
70  *              1       access granted
71  */
72
73 int
74 access_allowed(
75     Backend             *be,
76     Connection          *conn,
77     Operation           *op,
78     Entry               *e,
79         AttributeDescription    *desc,
80     struct berval       *val,
81     slap_access_t       access )
82 {
83         int                             count;
84         AccessControl   *a;
85 #ifdef LDAP_DEBUG
86         char accessmaskbuf[ACCESSMASK_MAXLEN];
87 #endif
88         slap_mask_t mask;
89         slap_control_t control;
90
91         const char *attr = desc ? desc->ad_cname->bv_val : NULL;
92
93         regmatch_t       matches[MAXREMATCHES];
94
95         Debug( LDAP_DEBUG_ACL,
96                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
97             access2str( access ),
98                 e->e_dn, attr );
99
100         assert( be != NULL );
101         assert( e != NULL );
102         assert( attr != NULL );
103         assert( access > ACL_NONE );
104
105         /* grant database root access */
106         if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
107                 Debug( LDAP_DEBUG_ACL,
108                     "<= root access granted\n",
109                         0, 0, 0 );
110                 return 1;
111         }
112
113         /*
114          * no-user-modification operational attributes are ignored
115          * by ACL_WRITE checking as any found here are not provided
116          * by the user
117          */
118         if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
119                 && desc != slap_schema.si_ad_entry
120                 && desc != slap_schema.si_ad_children )
121         {
122                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
123                         " %s access granted\n",
124                         attr, 0, 0 );
125                 return 1;
126         }
127
128         /* use backend default access if no backend acls */
129         if( be != NULL && be->be_acl == NULL ) {
130                 Debug( LDAP_DEBUG_ACL,
131                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
132                         access2str( access ),
133                         be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
134
135                 return be->be_dfltaccess >= access;
136
137 #ifdef notdef
138         /* be is always non-NULL */
139         /* use global default access if no global acls */
140         } else if ( be == NULL && global_acl == NULL ) {
141                 Debug( LDAP_DEBUG_ACL,
142                         "=> access_allowed: global default %s access %s to \"%s\"\n",
143                         access2str( access ),
144                         global_default_access >= access ? "granted" : "denied", op->o_dn );
145
146                 return global_default_access >= access;
147 #endif
148         }
149
150         ACL_INIT(mask);
151         memset(matches, '\0', sizeof(matches));
152         
153         control = ACL_BREAK;
154         a = NULL;
155         count = 0;
156
157         while((a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches )) != NULL)
158         {
159                 int i;
160
161                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
162                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
163                                (int)matches[i].rm_so, (int)matches[i].rm_eo );
164
165                         if( matches[i].rm_so <= matches[0].rm_eo ) {
166                                 int n;
167                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
168                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
169                                 }
170                         }
171                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
172                 }
173
174                 control = acl_mask( a, &mask, be, conn, op,
175                         e, desc, val, matches );
176
177                 if ( control != ACL_BREAK ) {
178                         break;
179                 }
180
181                 memset(matches, '\0', sizeof(matches));
182         }
183
184         if ( ACL_IS_INVALID( mask ) ) {
185                 Debug( LDAP_DEBUG_ACL,
186                         "=> access_allowed: \"%s\" (%s) invalid!\n",
187                         e->e_dn, attr, 0 );
188                 ACL_INIT( mask );
189
190         } else if ( control == ACL_BREAK ) {
191                 Debug( LDAP_DEBUG_ACL,
192                         "=> access_allowed: no more rules\n", 0, 0, 0);
193                 ACL_INIT( mask );
194         }
195
196         Debug( LDAP_DEBUG_ACL,
197                 "=> access_allowed: %s access %s by %s\n",
198                 access2str( access ),
199                 ACL_GRANT(mask, access) ? "granted" : "denied",
200                 accessmask2str( mask, accessmaskbuf ) );
201
202         return ACL_GRANT(mask, access);
203 }
204
205 /*
206  * acl_get - return the acl applicable to entry e, attribute
207  * attr.  the acl returned is suitable for use in subsequent calls to
208  * acl_access_allowed().
209  */
210
211 static AccessControl *
212 acl_get(
213         AccessControl *a,
214         int                     *count,
215     Backend             *be,
216     Operation   *op,
217     Entry               *e,
218         AttributeDescription *desc,
219     int                 nmatch,
220     regmatch_t  *matches )
221 {
222         const char *attr;
223         int dnlen, patlen;
224
225         assert( e != NULL );
226         assert( count != NULL );
227
228         attr = desc ? desc->ad_cname->bv_val : NULL;
229
230         if( a == NULL ) {
231                 if( be == NULL ) {
232                         a = global_acl;
233                 } else {
234                         a = be->be_acl;
235                 }
236
237                 assert( a != NULL );
238
239         } else {
240                 a = a->acl_next;
241         }
242
243         dnlen = strlen(e->e_ndn);
244
245         for ( ; a != NULL; a = a->acl_next ) {
246                 (*count) ++;
247
248                 if (a->acl_dn_pat != NULL) {
249                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
250                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
251                                         *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
252
253                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
254                                         continue;
255
256                         } else {
257                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
258                                         *count, a->acl_dn_pat, 0 );
259
260                                 patlen = strlen( a->acl_dn_pat );
261                                 if ( dnlen < patlen )
262                                         continue;
263
264                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
265                                         /* base dn -- entire object DN must match */
266                                         if ( dnlen != patlen )
267                                                 continue;
268
269                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
270                                         char *rdn;
271                                         int rdnlen = -1;
272
273                                         if ( dnlen <= patlen )
274                                                 continue;
275
276                                         if ( e->e_ndn[dnlen - patlen - 1] != ',' )
277                                                 continue;
278
279                                         rdn = dn_rdn( NULL, e->e_ndn );
280                                         if ( rdn != NULL ) {
281                                                 rdnlen = strlen( rdn );
282                                                 ch_free( rdn );
283                                         }
284                                         if ( rdnlen != dnlen - patlen - 1 )
285                                                 continue;
286
287                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
288                                         if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
289                                                 continue;
290
291                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
292                                         if ( dnlen <= patlen )
293                                                 continue;
294                                         if ( e->e_ndn[dnlen - patlen - 1] != ',' )
295                                                 continue;
296                                 }
297
298                                 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
299                                         continue;
300                         }
301
302                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
303                                 *count, 0, 0 );
304                 }
305
306                 if ( a->acl_filter != NULL ) {
307                         ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
308                         if ( rc != LDAP_COMPARE_TRUE ) {
309                                 continue;
310                         }
311                 }
312
313         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
314                         *count, attr, 0);
315
316                 if ( attr == NULL || a->acl_attrs == NULL ||
317                         ad_inlist( desc, a->acl_attrs ) )
318                 {
319                         Debug( LDAP_DEBUG_ACL,
320                                 "<= acl_get: [%d] acl %s attr: %s\n",
321                                 *count, e->e_dn, attr );
322                         return a;
323                 }
324                 matches[0].rm_so = matches[0].rm_eo = -1;
325         }
326
327         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
328         return( NULL );
329 }
330
331
332 /*
333  * acl_mask - modifies mask based upon the given acl and the
334  * requested access to entry e, attribute attr, value val.  if val
335  * is null, access to the whole attribute is assumed (all values).
336  *
337  * returns      0       access NOT allowed
338  *              1       access allowed
339  */
340
341 static slap_control_t
342 acl_mask(
343     AccessControl       *a,
344         slap_mask_t *mask,
345     Backend             *be,
346     Connection  *conn,
347     Operation   *op,
348     Entry               *e,
349         AttributeDescription *desc,
350     struct berval       *val,
351         regmatch_t      *matches
352 )
353 {
354         int             i, odnlen, patlen;
355         Access  *b;
356 #ifdef LDAP_DEBUG
357         char accessmaskbuf[ACCESSMASK_MAXLEN];
358 #endif
359         const char *attr = desc ? desc->ad_cname->bv_val : NULL;
360
361         assert( a != NULL );
362         assert( mask != NULL );
363
364         Debug( LDAP_DEBUG_ACL,
365                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
366                 e->e_dn, attr, 0 );
367
368         Debug( LDAP_DEBUG_ACL,
369                 "=> acl_mask: to %s by \"%s\", (%s) \n",
370                 val ? "value" : "all values",
371                 op->o_ndn ?  op->o_ndn : "",
372                 accessmask2str( *mask, accessmaskbuf ) );
373
374         for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
375                 slap_mask_t oldmask, modmask;
376
377                 ACL_INVALIDATE( modmask );
378
379                 /* AND <who> clauses */
380                 if ( b->a_dn_pat != NULL ) {
381                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
382                                 b->a_dn_pat, 0, 0);
383                         /*
384                          * if access applies to the entry itself, and the
385                          * user is bound as somebody in the same namespace as
386                          * the entry, OR the given dn matches the dn pattern
387                          */
388                         if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
389                                 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
390                                         continue;
391                                 }
392
393                         } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
394                                 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
395                                         continue;
396                                 }
397
398                         } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
399                                 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
400                                         continue;
401                                 }
402                                 
403                                 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
404                                         continue;
405                                 }
406
407                         } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
408                                 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
409                                         int ret = regex_matches( b->a_dn_pat,
410                                                 op->o_ndn, e->e_ndn, matches );
411
412                                         if( ret == 0 ) {
413                                                 continue;
414                                         }
415                                 }
416
417                         } else {
418                                 if ( e->e_dn == NULL )
419                                         continue;
420
421                                 patlen = strlen( b->a_dn_pat );
422                                 odnlen = strlen( op->o_ndn );
423                                 if ( odnlen < patlen )
424                                         continue;
425
426                                 if ( b->a_dn_style == ACL_STYLE_BASE ) {
427                                         /* base dn -- entire object DN must match */
428                                         if ( odnlen != patlen )
429                                                 continue;
430
431                                 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
432                                         char *rdn;
433                                         int rdnlen = -1;
434
435                                         if ( odnlen <= patlen )
436                                                 continue;
437
438                                         if ( op->o_ndn[odnlen - patlen - 1] != ',' )
439                                                 continue;
440
441                                         rdn = dn_rdn( NULL, op->o_ndn );
442                                         if ( rdn != NULL ) {
443                                                 rdnlen = strlen( rdn );
444                                                 ch_free( rdn );
445                                         }
446                                         if ( rdnlen != odnlen - patlen - 1 )
447                                                 continue;
448
449                                 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
450                                         if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
451                                                 continue;
452
453                                 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
454                                         if ( odnlen <= patlen )
455                                                 continue;
456                                         if ( op->o_ndn[odnlen - patlen - 1] != ',' )
457                                                 continue;
458                                 }
459
460                                 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
461                                         continue;
462
463                         }
464                 }
465
466                 if ( b->a_sockurl_pat != NULL ) {
467                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
468                                 b->a_sockurl_pat, 0, 0 );
469
470                         if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
471                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
472                                         if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
473                                                         e->e_ndn, matches ) ) 
474                                         {
475                                                 continue;
476                                         }
477                                 } else {
478                                         if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
479                                                 continue;
480                                 }
481                         }
482                 }
483
484                 if ( b->a_domain_pat != NULL ) {
485                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
486                                 b->a_domain_pat, 0, 0 );
487
488                         if ( strcmp( b->a_domain_pat, "*" ) != 0) {
489                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
490                                         if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
491                                                         e->e_ndn, matches ) ) 
492                                         {
493                                                 continue;
494                                         }
495                                 } else {
496                                         if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
497                                                 continue;
498                                 }
499                         }
500                 }
501
502                 if ( b->a_peername_pat != NULL ) {
503                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
504                                 b->a_peername_pat, 0, 0 );
505
506                         if ( strcmp( b->a_peername_pat, "*" ) != 0) {
507                                 if ( b->a_peername_style == ACL_STYLE_REGEX) {
508                                         if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
509                                                         e->e_ndn, matches ) ) 
510                                         {
511                                                 continue;
512                                         }
513                                 } else {
514                                         if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
515                                                 continue;
516                                 }
517                         }
518                 }
519
520                 if ( b->a_sockname_pat != NULL ) {
521                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
522                                 b->a_sockname_pat, 0, 0 );
523
524                         if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
525                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
526                                         if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
527                                                         e->e_ndn, matches ) ) 
528                                         {
529                                                 continue;
530                                         }
531                                 } else {
532                                         if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
533                                                 continue;
534                                 }
535                         }
536                 }
537
538                 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
539                         Attribute       *at;
540                         struct berval   bv;
541                         int rc, match = 0;
542                         const char *text;
543                         const char *desc = b->a_dn_at->ad_cname->bv_val;
544
545                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
546                                 desc, 0, 0);
547
548                         bv.bv_val = op->o_ndn;
549                         bv.bv_len = strlen( bv.bv_val );
550
551                         /* see if asker is listed in dnattr */
552                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
553                                 at != NULL;
554                                 at = attrs_find( at->a_next, b->a_dn_at ) )
555                         {
556                                 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
557                                         /* found it */
558                                         match = 1;
559                                         break;
560                                 }
561                         }
562
563                         if( match ) {
564                                 /* have a dnattr match. if this is a self clause then
565                                  * the target must also match the op dn.
566                                  */
567                                 if ( b->a_dn_self ) {
568                                         /* check if the target is an attribute. */
569                                         if ( val == NULL )
570                                                 continue;
571                                         /* target is attribute, check if the attribute value
572                                          * is the op dn.
573                                          */
574                                         rc = value_match( &match, b->a_dn_at,
575                                                 b->a_dn_at->ad_type->sat_equality, 0,
576                                                 val, &bv, &text );
577                                         /* on match error or no match, fail the ACL clause */
578                                         if (rc != LDAP_SUCCESS || match != 0 )
579                                                 continue;
580                                 }
581                         } else {
582                                 /* no dnattr match, check if this is a self clause */
583                                 if ( ! b->a_dn_self )
584                                         continue;
585                                 /* this is a self clause, check if the target is an
586                                  * attribute.
587                                  */
588                                 if ( val == NULL )
589                                         continue;
590                                 /* target is attribute, check if the attribute value
591                                  * is the op dn.
592                                  */
593                                 rc = value_match( &match, b->a_dn_at,
594                                         b->a_dn_at->ad_type->sat_equality, 0,
595                                         val, &bv, &text );
596
597                                 /* on match error or no match, fail the ACL clause */
598                                 if (rc != LDAP_SUCCESS || match != 0 )
599                                         continue;
600                         }
601                 }
602
603                 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
604                         char buf[1024];
605
606                         /* b->a_group is an unexpanded entry name, expanded it should be an 
607                          * entry with objectclass group* and we test to see if odn is one of
608                          * the values in the attribute group
609                          */
610                         /* see if asker is listed in dnattr */
611                         if ( b->a_group_style == ACL_STYLE_REGEX ) {
612                                 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
613                                 if ( dn_normalize(buf) == NULL ) {
614                                         /* did not expand to a valid dn */
615                                         continue;
616                                 }
617                         } else {
618                                 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
619                                 buf[sizeof(buf) - 1] = 0;
620                         }
621
622                         if (backend_group(be, conn, op, e, buf, op->o_ndn,
623                                 b->a_group_oc, b->a_group_at) != 0)
624                         {
625                                 continue;
626                         }
627                 }
628
629                 if ( b->a_set_pat != NULL ) {
630                         struct berval bv;
631
632                         bv.bv_val = b->a_set_pat;
633                         bv.bv_len = strlen(b->a_set_pat);
634                         if (aci_match_set( &bv, be, e, conn, op, 0 ) == 0) {
635                                 continue;
636                         }
637                 }
638
639                 if ( b->a_authz.sai_ssf ) {
640                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
641                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
642
643                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
644                                 continue;
645                         }
646                 }
647
648                 if ( b->a_authz.sai_transport_ssf ) {
649                         Debug( LDAP_DEBUG_ACL,
650                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
651                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
652
653                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
654                                 continue;
655                         }
656                 }
657
658                 if ( b->a_authz.sai_tls_ssf ) {
659                         Debug( LDAP_DEBUG_ACL,
660                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
661                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
662
663                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
664                                 continue;
665                         }
666                 }
667
668                 if ( b->a_authz.sai_sasl_ssf ) {
669                         Debug( LDAP_DEBUG_ACL,
670                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
671                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
672
673                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
674                                 continue;
675                         }
676                 }
677
678 #ifdef SLAPD_ACI_ENABLED
679                 if ( b->a_aci_at != NULL ) {
680                         Attribute       *at;
681                         slap_access_t grant, deny, tgrant, tdeny;
682
683                         /* this case works different from the others above.
684                          * since aci's themselves give permissions, we need
685                          * to first check b->a_access_mask, the ACL's access level.
686                          */
687
688                         if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
689                                 continue;
690                         }
691
692                         if ( e->e_dn == NULL ) {
693                                 continue;
694                         }
695
696                         /* first check if the right being requested
697                          * is allowed by the ACL clause.
698                          */
699                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
700                                 continue;
701                         }
702
703                         /* get the aci attribute */
704                         at = attr_find( e->e_attrs, b->a_aci_at );
705                         if ( at == NULL ) {
706                                 continue;
707                         }
708
709                         /* start out with nothing granted, nothing denied */
710                         ACL_INIT(tgrant);
711                         ACL_INIT(tdeny);
712
713                         /* the aci is an multi-valued attribute.  The
714                          * rights are determined by OR'ing the individual
715                          * rights given by the acis.
716                          */
717                         for ( i = 0; at->a_vals[i] != NULL; i++ ) {
718                                 if (aci_mask( be, conn, op,
719                                         e, desc, val, at->a_vals[i],
720                                         matches, &grant, &deny ) != 0)
721                                 {
722                                         tgrant |= grant;
723                                         tdeny |= deny;
724                                 }
725                         }
726
727                         /* remove anything that the ACL clause does not allow */
728                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
729                         tdeny &= ACL_PRIV_MASK;
730
731                         /* see if we have anything to contribute */
732                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
733                                 continue;
734                         }
735
736                         /* this could be improved by changing acl_mask so that it can deal with
737                          * by clauses that return grant/deny pairs.  Right now, it does either
738                          * additive or subtractive rights, but not both at the same time.  So,
739                          * we need to combine the grant/deny pair into a single rights mask in
740                          * a smart way:  if either grant or deny is "empty", then we use the
741                          * opposite as is, otherwise we remove any denied rights from the grant
742                          * rights mask and construct an additive mask.
743                          */
744                         if (ACL_IS_INVALID(tdeny)) {
745                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
746
747                         } else if (ACL_IS_INVALID(tgrant)) {
748                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
749
750                         } else {
751                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
752                         }
753
754                 } else
755 #endif
756                 {
757                         modmask = b->a_access_mask;
758                 }
759
760
761                 Debug( LDAP_DEBUG_ACL,
762                         "<= acl_mask: [%d] applying %s (%s)\n",
763                         i, accessmask2str( modmask, accessmaskbuf ), 
764                         b->a_type == ACL_CONTINUE
765                                 ? "continue"
766                                 : b->a_type == ACL_BREAK
767                                         ? "break"
768                                         : "stop" );
769
770                 /* save old mask */
771                 oldmask = *mask;
772
773                 if( ACL_IS_ADDITIVE(modmask) ) {
774                         /* add privs */
775                         ACL_PRIV_SET( *mask, modmask );
776
777                         /* cleanup */
778                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
779
780                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
781                         /* substract privs */
782                         ACL_PRIV_CLR( *mask, modmask );
783
784                         /* cleanup */
785                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
786
787                 } else {
788                         /* assign privs */
789                         *mask = modmask;
790                 }
791
792                 Debug( LDAP_DEBUG_ACL,
793                         "<= acl_mask: [%d] mask: %s\n",
794                         i, accessmask2str(*mask, accessmaskbuf), 0 );
795
796                 if( b->a_type == ACL_CONTINUE ) {
797                         continue;
798
799                 } else if ( b->a_type == ACL_BREAK ) {
800                         return ACL_BREAK;
801
802                 } else {
803                         return ACL_STOP;
804                 }
805         }
806
807         Debug( LDAP_DEBUG_ACL,
808                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
809                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
810         return ACL_STOP;
811 }
812
813 /*
814  * acl_check_modlist - check access control on the given entry to see if
815  * it allows the given modifications by the user associated with op.
816  * returns      1       if mods allowed ok
817  *                      0       mods not allowed
818  */
819
820 int
821 acl_check_modlist(
822     Backend     *be,
823     Connection  *conn,
824     Operation   *op,
825     Entry       *e,
826     Modifications       *mlist
827 )
828 {
829         int             i;
830
831         assert( be != NULL );
832
833         /* short circuit root database access */
834         if ( be_isroot( be, op->o_ndn ) ) {
835                 Debug( LDAP_DEBUG_ACL,
836                         "<= acl_access_allowed: granted to database root\n",
837                     0, 0, 0 );
838                 return 1;
839         }
840
841         /* use backend default access if no backend acls */
842         if( be != NULL && be->be_acl == NULL ) {
843                 Debug( LDAP_DEBUG_ACL,
844                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
845                         access2str( ACL_WRITE ),
846                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
847
848                 return be->be_dfltaccess >= ACL_WRITE;
849
850 #ifdef notdef
851         /* be is always non-NULL */
852         /* use global default access if no global acls */
853         } else if ( be == NULL && global_acl == NULL ) {
854                 Debug( LDAP_DEBUG_ACL,
855                         "=> access_allowed: global default %s access %s to \"%s\"\n",
856                         access2str( ACL_WRITE ),
857                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
858
859                 return global_default_access >= ACL_WRITE;
860 #endif
861         }
862
863         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
864                 /*
865                  * no-user-modification operational attributes are ignored
866                  * by ACL_WRITE checking as any found here are not provided
867                  * by the user
868                  */
869                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
870                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
871                                 " modify access granted\n",
872                                 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
873                         continue;
874                 }
875
876                 switch ( mlist->sml_op ) {
877                 case LDAP_MOD_REPLACE:
878                 case LDAP_MOD_ADD:
879                         if ( mlist->sml_bvalues == NULL ) {
880                                 break;
881                         }
882                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
883                                 if ( ! access_allowed( be, conn, op, e,
884                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
885                                 {
886                                         return( 0 );
887                                 }
888                         }
889                         break;
890
891                 case LDAP_MOD_DELETE:
892                         if ( mlist->sml_bvalues == NULL ) {
893                                 if ( ! access_allowed( be, conn, op, e,
894                                         mlist->sml_desc, NULL, ACL_WRITE ) )
895                                 {
896                                         return( 0 );
897                                 }
898                                 break;
899                         }
900                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
901                                 if ( ! access_allowed( be, conn, op, e,
902                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
903                                 {
904                                         return( 0 );
905                                 }
906                         }
907                         break;
908                 }
909         }
910
911         return( 1 );
912 }
913
914 static char *
915 aci_bvstrdup( struct berval *bv )
916 {
917         char *s;
918
919         s = (char *)ch_malloc(bv->bv_len + 1);
920         if (s != NULL) {
921                 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
922                 s[bv->bv_len] = 0;
923         }
924         return(s);
925 }
926
927 static int
928 aci_strbvcmp(
929         const char *s,
930         struct berval *bv )
931 {
932         int res, len;
933
934         res = strncasecmp( s, bv->bv_val, bv->bv_len );
935         if (res)
936                 return(res);
937         len = strlen(s);
938         if (len > (int)bv->bv_len)
939                 return(1);
940         if (len < (int)bv->bv_len)
941                 return(-1);
942         return(0);
943 }
944
945 static int
946 aci_get_part(
947         struct berval *list,
948         int ix,
949         char sep,
950         struct berval *bv )
951 {
952         int len;
953         char *p;
954
955         if (bv) {
956                 bv->bv_len = 0;
957                 bv->bv_val = NULL;
958         }
959         len = list->bv_len;
960         p = list->bv_val;
961         while (len >= 0 && --ix >= 0) {
962                 while (--len >= 0 && *p++ != sep) ;
963         }
964         while (len >= 0 && *p == ' ') {
965                 len--;
966                 p++;
967         }
968         if (len < 0)
969                 return(-1);
970
971         if (!bv)
972                 return(0);
973
974         bv->bv_val = p;
975         while (--len >= 0 && *p != sep) {
976                 bv->bv_len++;
977                 p++;
978         }
979         while (bv->bv_len > 0 && *--p == ' ')
980                 bv->bv_len--;
981         return(bv->bv_len);
982 }
983
984 char **
985 aci_set_gather (void *cookie, char *name, char *attr)
986 {
987         struct {
988         Backend *be;
989         Entry *e;
990         Connection *conn;
991         Operation *op;
992         } *cp = (void *)cookie;
993         struct berval **bvals = NULL;
994         char **vals = NULL;
995         char *ndn;
996         int i;
997
998         /* this routine needs to return the bervals instead of
999          * plain strings, since syntax is not known.  It should
1000          * also return the syntax or some "comparison cookie".
1001          */
1002
1003         if ((ndn = ch_strdup(name)) != NULL) {
1004                 if (dn_normalize(ndn) != NULL) {
1005                         const char *text;
1006                         AttributeDescription *desc = NULL;
1007                         if (slap_str2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1008                                 backend_attribute(cp->be, NULL /*cp->conn*/,
1009                                                                         NULL /*cp->op*/, cp->e,
1010                                                                         ndn, desc, &bvals);
1011                                 if (bvals != NULL) {
1012                                         for (i = 0; bvals[i] != NULL; i++) { }
1013                                         vals = ch_calloc(i + 1, sizeof(char *));
1014                                         if (vals != NULL) {
1015                                                 while (--i >= 0) {
1016                                                         vals[i] = bvals[i]->bv_val;
1017                                                         bvals[i]->bv_val = NULL;
1018                                                 }
1019                                         }
1020                                         ber_bvecfree(bvals);
1021                                 }
1022                                 ad_free(desc, 1);
1023                         }
1024                 }
1025                 ch_free(ndn);
1026         }
1027         return(vals);
1028 }
1029
1030 static int
1031 aci_match_set (
1032         struct berval *subj,
1033     Backend *be,
1034     Entry *e,
1035     Connection *conn,
1036     Operation *op,
1037     int setref
1038 )
1039 {
1040         char *set = NULL;
1041         int rc = 0;
1042         struct {
1043         Backend *be;
1044         Entry *e;
1045         Connection *conn;
1046         Operation *op;
1047         } cookie;
1048
1049         if (setref == 0) {
1050                 set = aci_bvstrdup(subj);
1051         } else {
1052                 struct berval bv;
1053                 char *subjdn;
1054                 char *setat;
1055                 struct berval **bvals;
1056                 const char *text;
1057                 AttributeDescription *desc = NULL;
1058
1059                 /* format of string is "entry/setAttrName" */
1060                 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1061                         return(0);
1062                 }
1063
1064                 subjdn = aci_bvstrdup(&bv);
1065                 if ( subjdn == NULL ) {
1066                         return(0);
1067                 }
1068
1069                 if ( aci_get_part(subj, 1, '/', &bv) < 0 ) {
1070                         setat = ch_strdup( SLAPD_ACI_SET_ATTR );
1071                 } else {
1072                         setat = aci_bvstrdup(&bv);
1073                 }
1074                 if ( setat != NULL ) {
1075                         if ( dn_normalize(subjdn) != NULL
1076                                 && slap_str2ad(setat, &desc, &text) == LDAP_SUCCESS )
1077                         {
1078                                 backend_attribute(be, NULL, NULL, e,
1079                                                                 subjdn, desc, &bvals);
1080                                 ad_free(desc, 1);
1081                                 if ( bvals != NULL ) {
1082                                         if ( bvals[0] != NULL )
1083                                                 set = ch_strdup(bvals[0]->bv_val);
1084                                         ber_bvecfree(bvals);
1085                                 }
1086                         }
1087                         ch_free(setat);
1088                 }
1089                 ch_free(subjdn);
1090         }
1091
1092         if (set != NULL) {
1093                 cookie.be = be;
1094                 cookie.e = e;
1095                 cookie.conn = conn;
1096                 cookie.op = op;
1097                 rc = (set_filter(aci_set_gather, &cookie, set, op->o_ndn, e->e_ndn, NULL) > 0);
1098                 ch_free(set);
1099         }
1100         return(rc);
1101 }
1102
1103 #ifdef SLAPD_ACI_ENABLED
1104 static int
1105 aci_list_map_rights(
1106         struct berval *list )
1107 {
1108         struct berval bv;
1109         slap_access_t mask;
1110         int i;
1111
1112         ACL_INIT(mask);
1113         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1114                 if (bv.bv_len <= 0)
1115                         continue;
1116                 switch (*bv.bv_val) {
1117                 case 'c':
1118                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1119                         break;
1120                 case 's':
1121                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1122                          * the right 's' to mean "set", but in the examples states
1123                          * that the right 's' means "search".  The latter definition
1124                          * is used here.
1125                          */
1126                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1127                         break;
1128                 case 'r':
1129                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1130                         break;
1131                 case 'w':
1132                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1133                         break;
1134                 case 'x':
1135                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1136                          * define any equivalent to the AUTH right, so I've just used
1137                          * 'x' for now.
1138                          */
1139                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1140                         break;
1141                 default:
1142                         break;
1143                 }
1144
1145         }
1146         return(mask);
1147 }
1148
1149 static int
1150 aci_list_has_attr(
1151         struct berval *list,
1152         const char *attr,
1153         struct berval *val )
1154 {
1155         struct berval bv, left, right;
1156         int i;
1157
1158         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1159                 if (aci_get_part(&bv, 0, '=', &left) < 0
1160                         || aci_get_part(&bv, 1, '=', &right) < 0)
1161                 {
1162                         if (aci_strbvcmp(attr, &bv) == 0)
1163                                 return(1);
1164                 } else if (val == NULL) {
1165                         if (aci_strbvcmp(attr, &left) == 0)
1166                                 return(1);
1167                 } else {
1168                         if (aci_strbvcmp(attr, &left) == 0) {
1169                                 /* this is experimental code that implements a
1170                                  * simple (prefix) match of the attribute value.
1171                                  * the ACI draft does not provide for aci's that
1172                                  * apply to specific values, but it would be
1173                                  * nice to have.  If the <attr> part of an aci's
1174                                  * rights list is of the form <attr>=<value>,
1175                                  * that means the aci applies only to attrs with
1176                                  * the given value.  Furthermore, if the attr is
1177                                  * of the form <attr>=<value>*, then <value> is
1178                                  * treated as a prefix, and the aci applies to 
1179                                  * any value with that prefix.
1180                                  *
1181                                  * Ideally, this would allow r.e. matches.
1182                                  */
1183                                 if (aci_get_part(&right, 0, '*', &left) < 0
1184                                         || right.bv_len <= left.bv_len)
1185                                 {
1186                                         if (aci_strbvcmp(val->bv_val, &right) == 0)
1187                                                 return(1);
1188                                 } else if (val->bv_len >= left.bv_len) {
1189                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1190                                                 return(1);
1191                                 }
1192                         }
1193                 }
1194         }
1195         return(0);
1196 }
1197
1198 static slap_access_t
1199 aci_list_get_attr_rights(
1200         struct berval *list,
1201         const char *attr,
1202         struct berval *val )
1203 {
1204     struct berval bv;
1205     slap_access_t mask;
1206     int i;
1207
1208         /* loop through each rights/attr pair, skip first part (action) */
1209         ACL_INIT(mask);
1210         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1211                 if (aci_list_has_attr(&bv, attr, val) == 0)
1212                         continue;
1213                 if (aci_get_part(list, i, ';', &bv) < 0)
1214                         continue;
1215                 mask |= aci_list_map_rights(&bv);
1216         }
1217         return(mask);
1218 }
1219
1220 static int
1221 aci_list_get_rights(
1222         struct berval *list,
1223         const char *attr,
1224         struct berval *val,
1225         slap_access_t *grant,
1226         slap_access_t *deny )
1227 {
1228     struct berval perm, actn;
1229     slap_access_t *mask;
1230     int i, found;
1231
1232         if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1233                 attr = "[entry]";
1234         }
1235
1236         found = 0;
1237         ACL_INIT(*grant);
1238         ACL_INIT(*deny);
1239         /* loop through each permissions clause */
1240         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1241                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1242                         continue;
1243                 if (aci_strbvcmp( "grant", &actn ) == 0) {
1244                         mask = grant;
1245                 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1246                         mask = deny;
1247                 } else {
1248                         continue;
1249                 }
1250
1251                 found = 1;
1252                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1253                 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1254         }
1255         return(found);
1256 }
1257
1258 static int
1259 aci_group_member (
1260         struct berval *subj,
1261         const char *defgrpoc,
1262         const char *defgrpat,
1263     Backend             *be,
1264     Entry               *e,
1265     Operation           *op,
1266         regmatch_t      *matches
1267 )
1268 {
1269         struct berval bv;
1270         char *subjdn, *grpdn = NULL;
1271         char *grpoc;
1272         char *grpat;
1273         ObjectClass *grp_oc = NULL;
1274         AttributeDescription *grp_ad = NULL;
1275         char *text;
1276         int rc;
1277
1278         /* format of string is "group/objectClassValue/groupAttrName" */
1279         if (aci_get_part(subj, 0, '/', &bv) < 0) {
1280                 return(0);
1281         }
1282
1283         subjdn = aci_bvstrdup(&bv);
1284         if (subjdn == NULL) {
1285                 return(0);
1286         }
1287
1288         if (aci_get_part(subj, 1, '/', &bv) < 0) {
1289                 grpoc = ch_strdup( defgrpoc );
1290         } else {
1291                 grpoc = aci_bvstrdup(&bv);
1292         }
1293
1294         if (aci_get_part(subj, 2, '/', &bv) < 0) {
1295                 grpat = ch_strdup( defgrpat );
1296         } else {
1297                 grpat = aci_bvstrdup(&bv);
1298         }
1299
1300         rc = slap_str2ad( grpat, &grp_ad, &text );
1301         if( rc != LDAP_SUCCESS ) {
1302                 rc = 0;
1303                 goto done;
1304         }
1305         rc = 0;
1306
1307         grp_oc = oc_find( grpoc );
1308         grpdn = (char *)ch_malloc(1024);
1309
1310         if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1311                 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1312                 if ( dn_normalize(grpdn) != NULL ) {
1313                         rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1314                 }
1315         }
1316
1317 done:
1318         if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1319         ch_free(grpdn);
1320         ch_free(grpat);
1321         ch_free(grpoc);
1322         ch_free(subjdn);
1323         return(rc);
1324 }
1325
1326 static int
1327 aci_mask(
1328     Backend                     *be,
1329     Connection          *conn,
1330     Operation           *op,
1331     Entry                       *e,
1332         AttributeDescription *desc,
1333     struct berval       *val,
1334     struct berval       *aci,
1335         regmatch_t              *matches,
1336         slap_access_t   *grant,
1337         slap_access_t   *deny
1338 )
1339 {
1340     struct berval bv, perms, sdn;
1341     char *subjdn;
1342         int rc;
1343         char *attr = desc->ad_cname->bv_val;
1344
1345         /* parse an aci of the form:
1346                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1347
1348            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1349            a full description of the format for this attribute.
1350
1351            For now, this routine only supports scope=entry.
1352          */
1353
1354         /* check that the aci has all 5 components */
1355         if (aci_get_part(aci, 4, '#', NULL) < 0)
1356                 return(0);
1357
1358         /* check that the aci family is supported */
1359         if (aci_get_part(aci, 0, '#', &bv) < 0)
1360                 return(0);
1361
1362         /* check that the scope is "entry" */
1363         if (aci_get_part(aci, 1, '#', &bv) < 0
1364                 || aci_strbvcmp( "entry", &bv ) != 0)
1365         {
1366                 return(0);
1367         }
1368
1369         /* get the list of permissions clauses, bail if empty */
1370         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1371                 return(0);
1372
1373         /* check if any permissions allow desired access */
1374         if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1375                 return(0);
1376
1377         /* see if we have a DN match */
1378         if (aci_get_part(aci, 3, '#', &bv) < 0)
1379                 return(0);
1380
1381         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1382                 return(0);
1383
1384         if (aci_strbvcmp( "access-id", &bv ) == 0) {
1385                 subjdn = aci_bvstrdup(&sdn);
1386                 if (subjdn == NULL)
1387                         return(0);
1388                 rc = 1;
1389                 if ( dn_normalize(subjdn) != NULL )
1390                         if (strcasecmp(op->o_ndn, subjdn) != 0)
1391                                 rc = 0;
1392                 ch_free(subjdn);
1393                 return(rc);
1394         }
1395
1396         if (aci_strbvcmp( "self", &bv ) == 0) {
1397                 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1398                         return(1);
1399
1400         } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1401                 char *dnattr = aci_bvstrdup(&sdn);
1402                 Attribute *at;
1403                 AttributeDescription *ad = NULL;
1404                 const char *text;
1405
1406                 rc = slap_str2ad( dnattr, &ad, &text );
1407                 ch_free( dnattr );
1408
1409                 if( rc != LDAP_SUCCESS ) {
1410                         return 0;
1411                 }
1412
1413                 rc = 0;
1414
1415                 bv.bv_val = op->o_ndn;
1416                 bv.bv_len = strlen( bv.bv_val );
1417
1418                 for(at = attrs_find( e->e_attrs, ad );
1419                         at != NULL;
1420                         at = attrs_find( at->a_next, ad ) )
1421                 {
1422                         if (value_find( ad, at->a_vals, &bv) == 0 ) {
1423                                 rc = 1;
1424                                 break;
1425                         }
1426                 }
1427
1428                 ad_free( ad, 1 );
1429                 return rc;
1430
1431
1432         } else if (aci_strbvcmp( "group", &bv ) == 0) {
1433                 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
1434                         return(1);
1435
1436         } else if (aci_strbvcmp( "role", &bv ) == 0) {
1437                 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
1438                         return(1);
1439
1440         } else if (aci_strbvcmp( "set", &bv ) == 0) {
1441                 if (aci_match_set(&sdn, be, e, conn, op, 0))
1442                         return(1);
1443
1444         } else if (aci_strbvcmp( "set-ref", &bv ) == 0) {
1445                 if (aci_match_set(&sdn, be, e, conn, op, 1))
1446                         return(1);
1447
1448         }
1449
1450         return(0);
1451 }
1452
1453 #endif  /* SLAPD_ACI_ENABLED */
1454
1455 static void
1456 string_expand(
1457         char *newbuf,
1458         int bufsiz,
1459         char *pat,
1460         char *match,
1461         regmatch_t *matches)
1462 {
1463         int     size;
1464         char   *sp;
1465         char   *dp;
1466         int     flag;
1467
1468         size = 0;
1469         newbuf[0] = '\0';
1470         bufsiz--; /* leave space for lone $ */
1471
1472         flag = 0;
1473         for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1474                 /* did we previously see a $ */
1475                 if (flag) {
1476                         if (*sp == '$') {
1477                                 *dp++ = '$';
1478                                 size++;
1479                         } else if (*sp >= '0' && *sp <= '9' ) {
1480                                 int     n;
1481                                 int     i;
1482                                 int     l;
1483
1484                                 n = *sp - '0';
1485                                 *dp = '\0';
1486                                 i = matches[n].rm_so;
1487                                 l = matches[n].rm_eo; 
1488                                 for ( ; size < 512 && i < l; size++, i++ ) {
1489                                         *dp++ = match[i];
1490                                         size++;
1491                                 }
1492                                 *dp = '\0';
1493                         }
1494                         flag = 0;
1495                 } else {
1496                         if (*sp == '$') {
1497                                 flag = 1;
1498                         } else {
1499                                 *dp++ = *sp;
1500                                 size++;
1501                         }
1502                 }
1503         }
1504
1505         if (flag) {
1506                 /* must have ended with a single $ */
1507                 *dp++ = '$';
1508                 size++;
1509         }
1510
1511         *dp = '\0';
1512
1513         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %s\n", pat, 0, 0 );
1514         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1515 }
1516
1517 static int
1518 regex_matches(
1519         char *pat,                              /* pattern to expand and match against */
1520         char *str,                              /* string to match against pattern */
1521         char *buf,                              /* buffer with $N expansion variables */
1522         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1523 )
1524 {
1525         regex_t re;
1526         char newbuf[512];
1527         int     rc;
1528
1529         if(str == NULL) str = "";
1530
1531         string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1532         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1533                 char error[512];
1534                 regerror(rc, &re, error, sizeof(error));
1535
1536                 Debug( LDAP_DEBUG_TRACE,
1537                     "compile( \"%s\", \"%s\") failed %s\n",
1538                         pat, str, error );
1539                 return( 0 );
1540         }
1541
1542         rc = regexec(&re, str, 0, NULL, 0);
1543         regfree( &re );
1544
1545         Debug( LDAP_DEBUG_TRACE,
1546             "=> regex_matches: string:   %s\n", str, 0, 0 );
1547         Debug( LDAP_DEBUG_TRACE,
1548             "=> regex_matches: rc: %d %s\n",
1549                 rc, !rc ? "matches" : "no matches", 0 );
1550         return( !rc );
1551 }
1552