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