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