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