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