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