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