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