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