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