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