]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
Each refint op needs a unique timestamp, must perform searches as rootdn
[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 /*
653  * Record value-dependent access control state
654  */
655 #define ACL_RECORD_VALUE_STATE do { \
656                 if( state && !( state->as_recorded & ACL_STATE_RECORDED_VD )) { \
657                         state->as_recorded |= ACL_STATE_RECORDED_VD; \
658                         state->as_vd_acl = a; \
659                         state->as_vd_acl_count = count; \
660                 } \
661         } while( 0 )
662
663 static int
664 acl_mask_dn(
665         Operation               *op,
666         Entry                   *e,
667         AttributeDescription    *desc,
668         struct berval           *val,
669         AccessControl           *a,
670         int                     nmatch,
671         regmatch_t              *matches,
672         slap_dn_access          *bdn,
673         struct berval           *opndn )
674 {
675         /*
676          * if access applies to the entry itself, and the
677          * user is bound as somebody in the same namespace as
678          * the entry, OR the given dn matches the dn pattern
679          */
680         /*
681          * NOTE: styles "anonymous", "users" and "self" 
682          * have been moved to enum slap_style_t, whose 
683          * value is set in a_dn_style; however, the string
684          * is maintaned in a_dn_pat.
685          */
686
687         if ( bdn->a_style == ACL_STYLE_ANONYMOUS ) {
688                 if ( !BER_BVISEMPTY( opndn ) ) {
689                         return 1;
690                 }
691
692         } else if ( bdn->a_style == ACL_STYLE_USERS ) {
693                 if ( BER_BVISEMPTY( opndn ) ) {
694                         return 1;
695                 }
696
697         } else if ( bdn->a_style == ACL_STYLE_SELF ) {
698                 struct berval   ndn, selfndn;
699                 int             level;
700
701                 if ( BER_BVISEMPTY( opndn ) || BER_BVISNULL( &e->e_nname ) ) {
702                         return 1;
703                 }
704
705                 level = bdn->a_self_level;
706                 if ( level < 0 ) {
707                         selfndn = *opndn;
708                         ndn = e->e_nname;
709                         level = -level;
710
711                 } else {
712                         ndn = *opndn;
713                         selfndn = e->e_nname;
714                 }
715
716                 for ( ; level > 0; level-- ) {
717                         if ( BER_BVISEMPTY( &ndn ) ) {
718                                 break;
719                         }
720                         dnParent( &ndn, &ndn );
721                 }
722                         
723                 if ( BER_BVISEMPTY( &ndn ) || !dn_match( &ndn, &selfndn ) )
724                 {
725                         return 1;
726                 }
727
728         } else if ( bdn->a_style == ACL_STYLE_REGEX ) {
729                 if ( !ber_bvccmp( &bdn->a_pat, '*' ) ) {
730                         int             tmp_nmatch;
731                         regmatch_t      tmp_matches[2],
732                                         *tmp_matchesp = tmp_matches;
733
734                         int             rc = 0;
735
736                         switch ( a->acl_dn_style ) {
737                         case ACL_STYLE_REGEX:
738                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
739                                         tmp_matchesp = matches;
740                                         tmp_nmatch = nmatch;
741                                         break;
742                                 }
743                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
744
745                         case ACL_STYLE_BASE:
746                                 tmp_matches[0].rm_so = 0;
747                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
748                                 tmp_nmatch = 1;
749                                 break;
750
751                         case ACL_STYLE_ONE:
752                         case ACL_STYLE_SUBTREE:
753                         case ACL_STYLE_CHILDREN:
754                                 tmp_matches[0].rm_so = 0;
755                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
756                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
757                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
758                                 tmp_nmatch = 2;
759                                 break;
760
761                         default:
762                                 /* error */
763                                 rc = 1;
764                                 break;
765                         }
766
767                         if ( rc ) {
768                                 return 1;
769                         }
770
771                         if ( !regex_matches( &bdn->a_pat, opndn->bv_val,
772                                 e->e_ndn, tmp_nmatch, tmp_matchesp ) )
773                         {
774                                 return 1;
775                         }
776                 }
777
778         } else {
779                 struct berval   pat;
780                 ber_len_t       patlen, odnlen;
781                 int             got_match = 0;
782
783                 if ( e->e_dn == NULL )
784                         return 1;
785
786                 if ( bdn->a_expand ) {
787                         struct berval   bv;
788                         char            buf[ACL_BUF_SIZE];
789                         
790                         int             tmp_nmatch;
791                         regmatch_t      tmp_matches[2],
792                                         *tmp_matchesp = tmp_matches;
793
794                         int             rc = 0;
795
796                         bv.bv_len = sizeof( buf ) - 1;
797                         bv.bv_val = buf;
798
799                         switch ( a->acl_dn_style ) {
800                         case ACL_STYLE_REGEX:
801                                 if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
802                                         tmp_matchesp = matches;
803                                         tmp_nmatch = nmatch;
804                                         break;
805                                 }
806                         /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
807
808                         case ACL_STYLE_BASE:
809                                 tmp_matches[0].rm_so = 0;
810                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
811                                 tmp_nmatch = 1;
812                                 break;
813
814                         case ACL_STYLE_ONE:
815                         case ACL_STYLE_SUBTREE:
816                         case ACL_STYLE_CHILDREN:
817                                 tmp_matches[0].rm_so = 0;
818                                 tmp_matches[0].rm_eo = e->e_nname.bv_len;
819                                 tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
820                                 tmp_matches[1].rm_eo = e->e_nname.bv_len;
821                                 tmp_nmatch = 2;
822                                 break;
823
824                         default:
825                                 /* error */
826                                 rc = 1;
827                                 break;
828                         }
829
830                         if ( rc ) {
831                                 return 1;
832                         }
833
834                         if ( acl_string_expand( &bv, &bdn->a_pat, 
835                                         e->e_nname.bv_val,
836                                         tmp_nmatch, tmp_matchesp ) )
837                         {
838                                 return 1;
839                         }
840                         
841                         if ( dnNormalize(0, NULL, NULL, &bv,
842                                         &pat, op->o_tmpmemctx )
843                                         != LDAP_SUCCESS )
844                         {
845                                 /* did not expand to a valid dn */
846                                 return 1;
847                         }
848
849                 } else {
850                         pat = bdn->a_pat;
851                 }
852
853                 patlen = pat.bv_len;
854                 odnlen = opndn->bv_len;
855                 if ( odnlen < patlen ) {
856                         goto dn_match_cleanup;
857
858                 }
859
860                 if ( bdn->a_style == ACL_STYLE_BASE ) {
861                         /* base dn -- entire object DN must match */
862                         if ( odnlen != patlen ) {
863                                 goto dn_match_cleanup;
864                         }
865
866                 } else if ( bdn->a_style == ACL_STYLE_ONE ) {
867                         ber_len_t       rdnlen = 0;
868
869                         if ( odnlen <= patlen ) {
870                                 goto dn_match_cleanup;
871                         }
872
873                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
874                                 goto dn_match_cleanup;
875                         }
876
877                         rdnlen = dn_rdnlen( NULL, opndn );
878                         if ( rdnlen - ( odnlen - patlen - 1 ) != 0 ) {
879                                 goto dn_match_cleanup;
880                         }
881
882                 } else if ( bdn->a_style == ACL_STYLE_SUBTREE ) {
883                         if ( odnlen > patlen && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
884                                 goto dn_match_cleanup;
885                         }
886
887                 } else if ( bdn->a_style == ACL_STYLE_CHILDREN ) {
888                         if ( odnlen <= patlen ) {
889                                 goto dn_match_cleanup;
890                         }
891
892                         if ( !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) ) {
893                                 goto dn_match_cleanup;
894                         }
895
896                 } else if ( bdn->a_style == ACL_STYLE_LEVEL ) {
897                         int             level = bdn->a_level;
898                         struct berval   ndn;
899
900                         if ( odnlen <= patlen ) {
901                                 goto dn_match_cleanup;
902                         }
903
904                         if ( level > 0 && !DN_SEPARATOR( opndn->bv_val[odnlen - patlen - 1] ) )
905                         {
906                                 goto dn_match_cleanup;
907                         }
908                         
909                         ndn = *opndn;
910                         for ( ; level > 0; level-- ) {
911                                 if ( BER_BVISEMPTY( &ndn ) ) {
912                                         goto dn_match_cleanup;
913                                 }
914                                 dnParent( &ndn, &ndn );
915                                 if ( ndn.bv_len < patlen ) {
916                                         goto dn_match_cleanup;
917                                 }
918                         }
919                         
920                         if ( ndn.bv_len != patlen ) {
921                                 goto dn_match_cleanup;
922                         }
923                 }
924
925                 got_match = !strcmp( pat.bv_val, &opndn->bv_val[ odnlen - patlen ] );
926
927 dn_match_cleanup:;
928                 if ( pat.bv_val != bdn->a_pat.bv_val ) {
929                         slap_sl_free( pat.bv_val, op->o_tmpmemctx );
930                 }
931
932                 if ( !got_match ) {
933                         return 1;
934                 }
935         }
936
937         return 0;
938 }
939
940 static int
941 acl_mask_dnattr(
942         Operation               *op,
943         Entry                   *e,
944         struct berval           *val,
945         AccessControl           *a,
946         Access                  *b,
947         int                     i,
948         regmatch_t              *matches,
949         int                     count,
950         AccessControlState      *state,
951         slap_dn_access          *bdn,
952         struct berval           *opndn )
953 {
954         Attribute       *at;
955         struct berval   bv;
956         int             rc, match = 0;
957         const char      *text;
958         const char      *attr = bdn->a_at->ad_cname.bv_val;
959
960         assert( attr != NULL );
961
962         if ( BER_BVISEMPTY( opndn ) ) {
963                 return 1;
964         }
965
966         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n", attr, 0, 0 );
967         bv = *opndn;
968
969         /* see if asker is listed in dnattr */
970         for ( at = attrs_find( e->e_attrs, bdn->a_at );
971                 at != NULL;
972                 at = attrs_find( at->a_next, bdn->a_at ) )
973         {
974                 if ( value_find_ex( bdn->a_at,
975                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
976                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
977                         at->a_nvals,
978                         &bv, op->o_tmpmemctx ) == 0 )
979                 {
980                         /* found it */
981                         match = 1;
982                         break;
983                 }
984         }
985
986         if ( match ) {
987                 /* have a dnattr match. if this is a self clause then
988                  * the target must also match the op dn.
989                  */
990                 if ( bdn->a_self ) {
991                         /* check if the target is an attribute. */
992                         if ( val == NULL ) return 1;
993
994                         /* target is attribute, check if the attribute value
995                          * is the op dn.
996                          */
997                         rc = value_match( &match, bdn->a_at,
998                                 bdn->a_at->ad_type->sat_equality, 0,
999                                 val, &bv, &text );
1000                         /* on match error or no match, fail the ACL clause */
1001                         if ( rc != LDAP_SUCCESS || match != 0 )
1002                                 return 1;
1003                 }
1004
1005         } else {
1006                 /* no dnattr match, check if this is a self clause */
1007                 if ( ! bdn->a_self )
1008                         return 1;
1009
1010                 ACL_RECORD_VALUE_STATE;
1011
1012                 /* this is a self clause, check if the target is an
1013                  * attribute.
1014                  */
1015                 if ( val == NULL )
1016                         return 1;
1017
1018                 /* target is attribute, check if the attribute value
1019                  * is the op dn.
1020                  */
1021                 rc = value_match( &match, bdn->a_at,
1022                         bdn->a_at->ad_type->sat_equality, 0,
1023                         val, &bv, &text );
1024
1025                 /* on match error or no match, fail the ACL clause */
1026                 if ( rc != LDAP_SUCCESS || match != 0 )
1027                         return 1;
1028         }
1029
1030         return 0;
1031 }
1032
1033
1034 /*
1035  * slap_acl_mask - modifies mask based upon the given acl and the
1036  * requested access to entry e, attribute attr, value val.  if val
1037  * is null, access to the whole attribute is assumed (all values).
1038  *
1039  * returns      0       access NOT allowed
1040  *              1       access allowed
1041  */
1042
1043 static slap_control_t
1044 slap_acl_mask(
1045         AccessControl           *a,
1046         slap_mask_t             *mask,
1047         Operation               *op,
1048         Entry                   *e,
1049         AttributeDescription    *desc,
1050         struct berval           *val,
1051         int                     nmatch,
1052         regmatch_t              *matches,
1053         int                     count,
1054         AccessControlState      *state )
1055 {
1056         int             i;
1057         Access          *b;
1058 #ifdef LDAP_DEBUG
1059         char            accessmaskbuf[ACCESSMASK_MAXLEN];
1060 #endif /* DEBUG */
1061         const char      *attr;
1062         slap_mask_t     a2pmask = ACL_ACCESS2PRIV( *mask );
1063
1064         assert( a != NULL );
1065         assert( mask != NULL );
1066         assert( desc != NULL );
1067
1068         attr = desc->ad_cname.bv_val;
1069
1070         assert( attr != NULL );
1071
1072         Debug( LDAP_DEBUG_ACL,
1073                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
1074                 e->e_dn, attr, 0 );
1075
1076         Debug( LDAP_DEBUG_ACL,
1077                 "=> acl_mask: to %s by \"%s\", (%s) \n",
1078                 val ? "value" : "all values",
1079                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
1080                 accessmask2str( *mask, accessmaskbuf, 1 ) );
1081
1082
1083         b = a->acl_access;
1084         i = 1;
1085
1086         for ( ; b != NULL; b = b->a_next, i++ ) {
1087                 slap_mask_t oldmask, modmask;
1088
1089                 ACL_INVALIDATE( modmask );
1090
1091                 /* AND <who> clauses */
1092                 if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
1093                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
1094                                 b->a_dn_pat.bv_val, 0, 0);
1095                         /*
1096                          * if access applies to the entry itself, and the
1097                          * user is bound as somebody in the same namespace as
1098                          * the entry, OR the given dn matches the dn pattern
1099                          */
1100                         /*
1101                          * NOTE: styles "anonymous", "users" and "self" 
1102                          * have been moved to enum slap_style_t, whose 
1103                          * value is set in a_dn_style; however, the string
1104                          * is maintaned in a_dn_pat.
1105                          */
1106
1107                         if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1108                                 &b->a_dn, &op->o_ndn ) )
1109                         {
1110                                 continue;
1111                         }
1112                 }
1113
1114                 if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
1115                         struct berval   ndn;
1116
1117                         Debug( LDAP_DEBUG_ACL, "<= check a_realdn_pat: %s\n",
1118                                 b->a_realdn_pat.bv_val, 0, 0);
1119                         /*
1120                          * if access applies to the entry itself, and the
1121                          * user is bound as somebody in the same namespace as
1122                          * the entry, OR the given dn matches the dn pattern
1123                          */
1124                         /*
1125                          * NOTE: styles "anonymous", "users" and "self" 
1126                          * have been moved to enum slap_style_t, whose 
1127                          * value is set in a_dn_style; however, the string
1128                          * is maintaned in a_dn_pat.
1129                          */
1130
1131                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1132                         {
1133                                 ndn = op->o_conn->c_ndn;
1134                         } else {
1135                                 ndn = op->o_ndn;
1136                         }
1137
1138                         if ( acl_mask_dn( op, e, desc, val, a, nmatch, matches,
1139                                 &b->a_realdn, &ndn ) )
1140                         {
1141                                 continue;
1142                         }
1143                 }
1144
1145                 if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1146                         if ( ! op->o_conn->c_listener ) {
1147                                 continue;
1148                         }
1149                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
1150                                 b->a_sockurl_pat.bv_val, 0, 0 );
1151
1152                         if ( !ber_bvccmp( &b->a_sockurl_pat, '*' ) ) {
1153                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
1154                                         if (!regex_matches( &b->a_sockurl_pat, op->o_conn->c_listener_url.bv_val,
1155                                                         e->e_ndn, nmatch, matches ) ) 
1156                                         {
1157                                                 continue;
1158                                         }
1159
1160                                 } else if ( b->a_sockurl_style == ACL_STYLE_EXPAND ) {
1161                                         struct berval   bv;
1162                                         char buf[ACL_BUF_SIZE];
1163
1164                                         bv.bv_len = sizeof( buf ) - 1;
1165                                         bv.bv_val = buf;
1166                                         if ( acl_string_expand( &bv, &b->a_sockurl_pat,
1167                                                         e->e_ndn, nmatch, matches ) )
1168                                         {
1169                                                 continue;
1170                                         }
1171
1172                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_listener_url ) != 0 )
1173                                         {
1174                                                 continue;
1175                                         }
1176
1177                                 } else {
1178                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &op->o_conn->c_listener_url ) != 0 )
1179                                         {
1180                                                 continue;
1181                                         }
1182                                 }
1183                         }
1184                 }
1185
1186                 if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1187                         if ( !op->o_conn->c_peer_domain.bv_val ) {
1188                                 continue;
1189                         }
1190                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
1191                                 b->a_domain_pat.bv_val, 0, 0 );
1192                         if ( !ber_bvccmp( &b->a_domain_pat, '*' ) ) {
1193                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
1194                                         if (!regex_matches( &b->a_domain_pat, op->o_conn->c_peer_domain.bv_val,
1195                                                         e->e_ndn, nmatch, matches ) ) 
1196                                         {
1197                                                 continue;
1198                                         }
1199                                 } else {
1200                                         char buf[ACL_BUF_SIZE];
1201
1202                                         struct berval   cmp = op->o_conn->c_peer_domain;
1203                                         struct berval   pat = b->a_domain_pat;
1204
1205                                         if ( b->a_domain_expand ) {
1206                                                 struct berval bv;
1207
1208                                                 bv.bv_len = sizeof(buf) - 1;
1209                                                 bv.bv_val = buf;
1210
1211                                                 if ( acl_string_expand(&bv, &b->a_domain_pat,
1212                                                                 e->e_ndn, nmatch, matches) )
1213                                                 {
1214                                                         continue;
1215                                                 }
1216                                                 pat = bv;
1217                                         }
1218
1219                                         if ( b->a_domain_style == ACL_STYLE_SUBTREE ) {
1220                                                 int offset = cmp.bv_len - pat.bv_len;
1221                                                 if ( offset < 0 ) {
1222                                                         continue;
1223                                                 }
1224
1225                                                 if ( offset == 1 || ( offset > 1 && cmp.bv_val[ offset - 1 ] != '.' ) ) {
1226                                                         continue;
1227                                                 }
1228
1229                                                 /* trim the domain */
1230                                                 cmp.bv_val = &cmp.bv_val[ offset ];
1231                                                 cmp.bv_len -= offset;
1232                                         }
1233                                         
1234                                         if ( ber_bvstrcasecmp( &pat, &cmp ) != 0 ) {
1235                                                 continue;
1236                                         }
1237                                 }
1238                         }
1239                 }
1240
1241                 if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1242                         if ( !op->o_conn->c_peer_name.bv_val ) {
1243                                 continue;
1244                         }
1245                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
1246                                 b->a_peername_pat.bv_val, 0, 0 );
1247                         if ( !ber_bvccmp( &b->a_peername_pat, '*' ) ) {
1248                                 if ( b->a_peername_style == ACL_STYLE_REGEX ) {
1249                                         if (!regex_matches( &b->a_peername_pat, op->o_conn->c_peer_name.bv_val,
1250                                                         e->e_ndn, nmatch, matches ) ) 
1251                                         {
1252                                                 continue;
1253                                         }
1254
1255                                 } else {
1256                                         /* try exact match */
1257                                         if ( b->a_peername_style == ACL_STYLE_BASE ) {
1258                                                 if ( ber_bvstrcasecmp( &b->a_peername_pat, &op->o_conn->c_peer_name ) != 0 ) {
1259                                                         continue;
1260                                                 }
1261
1262                                         } else if ( b->a_peername_style == ACL_STYLE_EXPAND ) {
1263                                                 struct berval   bv;
1264                                                 char buf[ACL_BUF_SIZE];
1265
1266                                                 bv.bv_len = sizeof( buf ) - 1;
1267                                                 bv.bv_val = buf;
1268                                                 if ( acl_string_expand( &bv, &b->a_peername_pat,
1269                                                                 e->e_ndn, nmatch, matches ) )
1270                                                 {
1271                                                         continue;
1272                                                 }
1273
1274                                                 if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_peer_name ) != 0 ) {
1275                                                         continue;
1276                                                 }
1277
1278                                         /* extract IP and try exact match */
1279                                         } else if ( b->a_peername_style == ACL_STYLE_IP ) {
1280                                                 char            *port;
1281                                                 char            buf[] = "255.255.255.255";
1282                                                 struct berval   ip;
1283                                                 unsigned long   addr;
1284                                                 int             port_number = -1;
1285                                                 
1286                                                 if ( strncasecmp( op->o_conn->c_peer_name.bv_val, 
1287                                                                         acl_bv_ip_eq.bv_val,
1288                                                                         acl_bv_ip_eq.bv_len ) != 0 ) 
1289                                                         continue;
1290
1291                                                 ip.bv_val = op->o_conn->c_peer_name.bv_val + acl_bv_ip_eq.bv_len;
1292                                                 ip.bv_len = op->o_conn->c_peer_name.bv_len - acl_bv_ip_eq.bv_len;
1293
1294                                                 port = strrchr( ip.bv_val, ':' );
1295                                                 if ( port ) {
1296                                                         ip.bv_len = port - ip.bv_val;
1297                                                         ++port;
1298                                                         if ( lutil_atoi( &port_number, port ) != 0 )
1299                                                                 continue;
1300                                                 }
1301                                                 
1302                                                 /* the port check can be anticipated here */
1303                                                 if ( b->a_peername_port != -1 && port_number != b->a_peername_port )
1304                                                         continue;
1305                                                 
1306                                                 /* address longer than expected? */
1307                                                 if ( ip.bv_len >= sizeof(buf) )
1308                                                         continue;
1309
1310                                                 AC_MEMCPY( buf, ip.bv_val, ip.bv_len );
1311                                                 buf[ ip.bv_len ] = '\0';
1312
1313                                                 addr = inet_addr( buf );
1314
1315                                                 /* unable to convert? */
1316                                                 if ( addr == (unsigned long)(-1) )
1317                                                         continue;
1318
1319                                                 if ( (addr & b->a_peername_mask) != b->a_peername_addr )
1320                                                         continue;
1321
1322 #ifdef LDAP_PF_LOCAL
1323                                         /* extract path and try exact match */
1324                                         } else if ( b->a_peername_style == ACL_STYLE_PATH ) {
1325                                                 struct berval path;
1326                                                 
1327                                                 if ( strncmp( op->o_conn->c_peer_name.bv_val,
1328                                                                         acl_bv_path_eq.bv_val,
1329                                                                         acl_bv_path_eq.bv_len ) != 0 )
1330                                                         continue;
1331
1332                                                 path.bv_val = op->o_conn->c_peer_name.bv_val
1333                                                         + acl_bv_path_eq.bv_len;
1334                                                 path.bv_len = op->o_conn->c_peer_name.bv_len
1335                                                         - acl_bv_path_eq.bv_len;
1336
1337                                                 if ( ber_bvcmp( &b->a_peername_pat, &path ) != 0 )
1338                                                         continue;
1339
1340 #endif /* LDAP_PF_LOCAL */
1341
1342                                         /* exact match (very unlikely...) */
1343                                         } else if ( ber_bvcmp( &op->o_conn->c_peer_name, &b->a_peername_pat ) != 0 ) {
1344                                                         continue;
1345                                         }
1346                                 }
1347                         }
1348                 }
1349
1350                 if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
1351                         if ( BER_BVISNULL( &op->o_conn->c_sock_name ) ) {
1352                                 continue;
1353                         }
1354                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
1355                                 b->a_sockname_pat.bv_val, 0, 0 );
1356                         if ( !ber_bvccmp( &b->a_sockname_pat, '*' ) ) {
1357                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
1358                                         if (!regex_matches( &b->a_sockname_pat, op->o_conn->c_sock_name.bv_val,
1359                                                         e->e_ndn, nmatch, matches ) ) 
1360                                         {
1361                                                 continue;
1362                                         }
1363
1364                                 } else if ( b->a_sockname_style == ACL_STYLE_EXPAND ) {
1365                                         struct berval   bv;
1366                                         char buf[ACL_BUF_SIZE];
1367
1368                                         bv.bv_len = sizeof( buf ) - 1;
1369                                         bv.bv_val = buf;
1370                                         if ( acl_string_expand( &bv, &b->a_sockname_pat,
1371                                                         e->e_ndn, nmatch, matches ) )
1372                                         {
1373                                                 continue;
1374                                         }
1375
1376                                         if ( ber_bvstrcasecmp( &bv, &op->o_conn->c_sock_name ) != 0 ) {
1377                                                 continue;
1378                                         }
1379
1380                                 } else {
1381                                         if ( ber_bvstrcasecmp( &b->a_sockname_pat, &op->o_conn->c_sock_name ) != 0 ) {
1382                                                 continue;
1383                                         }
1384                                 }
1385                         }
1386                 }
1387
1388                 if ( b->a_dn_at != NULL ) {
1389                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1390                                         matches, count, state,
1391                                         &b->a_dn, &op->o_ndn ) )
1392                         {
1393                                 continue;
1394                         }
1395                 }
1396
1397                 if ( b->a_realdn_at != NULL ) {
1398                         struct berval   ndn;
1399
1400                         if ( op->o_conn && !BER_BVISNULL( &op->o_conn->c_ndn ) )
1401                         {
1402                                 ndn = op->o_conn->c_ndn;
1403                         } else {
1404                                 ndn = op->o_ndn;
1405                         }
1406
1407                         if ( acl_mask_dnattr( op, e, val, a, b, i,
1408                                         matches, count, state,
1409                                         &b->a_realdn, &ndn ) )
1410                         {
1411                                 continue;
1412                         }
1413                 }
1414
1415                 if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1416                         struct berval bv;
1417                         struct berval ndn = BER_BVNULL;
1418                         int rc;
1419
1420                         if ( op->o_ndn.bv_len == 0 ) {
1421                                 continue;
1422                         }
1423
1424                         Debug( LDAP_DEBUG_ACL, "<= check a_group_pat: %s\n",
1425                                 b->a_group_pat.bv_val, 0, 0 );
1426
1427                         /* b->a_group is an unexpanded entry name, expanded it should be an 
1428                          * entry with objectclass group* and we test to see if odn is one of
1429                          * the values in the attribute group
1430                          */
1431                         /* see if asker is listed in dnattr */
1432                         if ( b->a_group_style == ACL_STYLE_EXPAND ) {
1433                                 char            buf[ACL_BUF_SIZE];
1434                                 int             tmp_nmatch;
1435                                 regmatch_t      tmp_matches[2],
1436                                                 *tmp_matchesp = tmp_matches;
1437
1438                                 bv.bv_len = sizeof(buf) - 1;
1439                                 bv.bv_val = buf;
1440
1441                                 rc = 0;
1442
1443                                 switch ( a->acl_dn_style ) {
1444                                 case ACL_STYLE_REGEX:
1445                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1446                                                 tmp_matchesp = matches;
1447                                                 tmp_nmatch = nmatch;
1448                                                 break;
1449                                         }
1450
1451                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1452                                 case ACL_STYLE_BASE:
1453                                         tmp_matches[0].rm_so = 0;
1454                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1455                                         tmp_nmatch = 1;
1456                                         break;
1457
1458                                 case ACL_STYLE_ONE:
1459                                 case ACL_STYLE_SUBTREE:
1460                                 case ACL_STYLE_CHILDREN:
1461                                         tmp_matches[0].rm_so = 0;
1462                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1463                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1464                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1465                                         tmp_nmatch = 2;
1466                                         break;
1467
1468                                 default:
1469                                         /* error */
1470                                         rc = 1;
1471                                         break;
1472                                 }
1473
1474                                 if ( rc ) {
1475                                         continue;
1476                                 }
1477                                 
1478                                 if ( acl_string_expand( &bv, &b->a_group_pat,
1479                                                 e->e_nname.bv_val,
1480                                                 tmp_nmatch, tmp_matchesp ) )
1481                                 {
1482                                         continue;
1483                                 }
1484
1485                                 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn,
1486                                                 op->o_tmpmemctx ) != LDAP_SUCCESS )
1487                                 {
1488                                         /* did not expand to a valid dn */
1489                                         continue;
1490                                 }
1491
1492                                 bv = ndn;
1493
1494                         } else {
1495                                 bv = b->a_group_pat;
1496                         }
1497
1498                         rc = backend_group( op, e, &bv, &op->o_ndn,
1499                                 b->a_group_oc, b->a_group_at );
1500
1501                         if ( ndn.bv_val ) {
1502                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1503                         }
1504
1505                         if ( rc != 0 ) {
1506                                 continue;
1507                         }
1508                 }
1509
1510                 if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1511                         struct berval   bv;
1512                         char            buf[ACL_BUF_SIZE];
1513
1514                         Debug( LDAP_DEBUG_ACL, "<= check a_set_pat: %s\n",
1515                                 b->a_set_pat.bv_val, 0, 0 );
1516
1517                         if ( b->a_set_style == ACL_STYLE_EXPAND ) {
1518                                 int             tmp_nmatch;
1519                                 regmatch_t      tmp_matches[2],
1520                                                 *tmp_matchesp = tmp_matches;
1521                                 int             rc = 0;
1522
1523                                 bv.bv_len = sizeof( buf ) - 1;
1524                                 bv.bv_val = buf;
1525
1526                                 rc = 0;
1527
1528                                 switch ( a->acl_dn_style ) {
1529                                 case ACL_STYLE_REGEX:
1530                                         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
1531                                                 tmp_matchesp = matches;
1532                                                 tmp_nmatch = nmatch;
1533                                                 break;
1534                                         }
1535
1536                                 /* FALLTHRU: applies also to ACL_STYLE_REGEX when pattern is "*" */
1537                                 case ACL_STYLE_BASE:
1538                                         tmp_matches[0].rm_so = 0;
1539                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1540                                         tmp_nmatch = 1;
1541                                         break;
1542
1543                                 case ACL_STYLE_ONE:
1544                                 case ACL_STYLE_SUBTREE:
1545                                 case ACL_STYLE_CHILDREN:
1546                                         tmp_matches[0].rm_so = 0;
1547                                         tmp_matches[0].rm_eo = e->e_nname.bv_len;
1548                                         tmp_matches[1].rm_so = e->e_nname.bv_len - a->acl_dn_pat.bv_len;
1549                                         tmp_matches[1].rm_eo = e->e_nname.bv_len;
1550                                         tmp_nmatch = 2;
1551                                         break;
1552
1553                                 default:
1554                                         /* error */
1555                                         rc = 1;
1556                                         break;
1557                                 }
1558
1559                                 if ( rc ) {
1560                                         continue;
1561                                 }
1562                                 
1563                                 if ( acl_string_expand( &bv, &b->a_set_pat,
1564                                                 e->e_nname.bv_val,
1565                                                 tmp_nmatch, tmp_matchesp ) )
1566                                 {
1567                                         continue;
1568                                 }
1569
1570                         } else {
1571                                 bv = b->a_set_pat;
1572                         }
1573                         
1574                         if ( acl_match_set( &bv, op, e, NULL ) == 0 ) {
1575                                 continue;
1576                         }
1577                 }
1578
1579                 if ( b->a_authz.sai_ssf ) {
1580                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
1581                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
1582                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
1583                                 continue;
1584                         }
1585                 }
1586
1587                 if ( b->a_authz.sai_transport_ssf ) {
1588                         Debug( LDAP_DEBUG_ACL,
1589                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
1590                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
1591                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
1592                                 continue;
1593                         }
1594                 }
1595
1596                 if ( b->a_authz.sai_tls_ssf ) {
1597                         Debug( LDAP_DEBUG_ACL,
1598                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
1599                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
1600                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
1601                                 continue;
1602                         }
1603                 }
1604
1605                 if ( b->a_authz.sai_sasl_ssf ) {
1606                         Debug( LDAP_DEBUG_ACL,
1607                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
1608                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
1609                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
1610                                 continue;
1611                         }
1612                 }
1613
1614                 /* check for the "self" modifier in the <access> field */
1615                 if ( b->a_dn.a_self ) {
1616                         const char *dummy;
1617                         int rc, match = 0;
1618
1619                         ACL_RECORD_VALUE_STATE;
1620
1621                         /* must have DN syntax */
1622                         if ( desc->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName &&
1623                                 !is_at_syntax( desc->ad_type, SLAPD_NAMEUID_SYNTAX )) continue;
1624
1625                         /* check if the target is an attribute. */
1626                         if ( val == NULL ) continue;
1627
1628                         /* a DN must be present */
1629                         if ( BER_BVISEMPTY( &op->o_ndn ) ) {
1630                                 continue;
1631                         }
1632
1633                         /* target is attribute, check if the attribute value
1634                          * is the op dn.
1635                          */
1636                         rc = value_match( &match, desc,
1637                                 desc->ad_type->sat_equality, 0,
1638                                 val, &op->o_ndn, &dummy );
1639                         /* on match error or no match, fail the ACL clause */
1640                         if ( rc != LDAP_SUCCESS || match != 0 )
1641                                 continue;
1642                 }
1643
1644 #ifdef SLAP_DYNACL
1645                 if ( b->a_dynacl ) {
1646                         slap_dynacl_t   *da;
1647                         slap_access_t   tgrant, tdeny;
1648
1649                         Debug( LDAP_DEBUG_ACL, "<= check a_dynacl\n",
1650                                 0, 0, 0 );
1651
1652                         /* this case works different from the others above.
1653                          * since dynamic ACL's themselves give permissions, we need
1654                          * to first check b->a_access_mask, the ACL's access level.
1655                          */
1656                         /* first check if the right being requested
1657                          * is allowed by the ACL clause.
1658                          */
1659                         if ( ! ACL_PRIV_ISSET( b->a_access_mask, a2pmask ) ) {
1660                                 continue;
1661                         }
1662
1663                         /* start out with nothing granted, nothing denied */
1664                         ACL_INVALIDATE(tgrant);
1665                         ACL_INVALIDATE(tdeny);
1666
1667                         for ( da = b->a_dynacl; da; da = da->da_next ) {
1668                                 slap_access_t   grant,
1669                                                 deny;
1670
1671                                 ACL_INVALIDATE(grant);
1672                                 ACL_INVALIDATE(deny);
1673
1674                                 Debug( LDAP_DEBUG_ACL, "    <= check a_dynacl: %s\n",
1675                                         da->da_name, 0, 0 );
1676
1677                                 (void)( *da->da_mask )( da->da_private, op, e, desc, val, nmatch, matches, &grant, &deny );
1678
1679                                 tgrant |= grant;
1680                                 tdeny |= deny;
1681                         }
1682
1683                         /* remove anything that the ACL clause does not allow */
1684                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
1685                         tdeny &= ACL_PRIV_MASK;
1686
1687                         /* see if we have anything to contribute */
1688                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
1689                                 continue;
1690                         }
1691
1692                         /* this could be improved by changing slap_acl_mask so that it can deal with
1693                          * by clauses that return grant/deny pairs.  Right now, it does either
1694                          * additive or subtractive rights, but not both at the same time.  So,
1695                          * we need to combine the grant/deny pair into a single rights mask in
1696                          * a smart way:  if either grant or deny is "empty", then we use the
1697                          * opposite as is, otherwise we remove any denied rights from the grant
1698                          * rights mask and construct an additive mask.
1699                          */
1700                         if (ACL_IS_INVALID(tdeny)) {
1701                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
1702
1703                         } else if (ACL_IS_INVALID(tgrant)) {
1704                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
1705
1706                         } else {
1707                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
1708                         }
1709
1710                 } else
1711 #endif /* SLAP_DYNACL */
1712                 {
1713                         modmask = b->a_access_mask;
1714                 }
1715
1716                 Debug( LDAP_DEBUG_ACL,
1717                         "<= acl_mask: [%d] applying %s (%s)\n",
1718                         i, accessmask2str( modmask, accessmaskbuf, 1 ), 
1719                         b->a_type == ACL_CONTINUE
1720                                 ? "continue"
1721                                 : b->a_type == ACL_BREAK
1722                                         ? "break"
1723                                         : "stop" );
1724                 /* save old mask */
1725                 oldmask = *mask;
1726
1727                 if( ACL_IS_ADDITIVE(modmask) ) {
1728                         /* add privs */
1729                         ACL_PRIV_SET( *mask, modmask );
1730
1731                         /* cleanup */
1732                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1733
1734                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
1735                         /* substract privs */
1736                         ACL_PRIV_CLR( *mask, modmask );
1737
1738                         /* cleanup */
1739                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
1740
1741                 } else {
1742                         /* assign privs */
1743                         *mask = modmask;
1744                 }
1745
1746                 a2pmask = *mask;
1747
1748                 Debug( LDAP_DEBUG_ACL,
1749                         "<= acl_mask: [%d] mask: %s\n",
1750                         i, accessmask2str(*mask, accessmaskbuf, 1), 0 );
1751
1752                 if( b->a_type == ACL_CONTINUE ) {
1753                         continue;
1754
1755                 } else if ( b->a_type == ACL_BREAK ) {
1756                         return ACL_BREAK;
1757
1758                 } else {
1759                         return ACL_STOP;
1760                 }
1761         }
1762
1763         /* implicit "by * none" clause */
1764         ACL_INIT(*mask);
1765
1766         Debug( LDAP_DEBUG_ACL,
1767                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1768                 accessmask2str(*mask, accessmaskbuf, 1), 0, 0 );
1769         return ACL_STOP;
1770 }
1771
1772 /*
1773  * acl_check_modlist - check access control on the given entry to see if
1774  * it allows the given modifications by the user associated with op.
1775  * returns      1       if mods allowed ok
1776  *              0       mods not allowed
1777  */
1778
1779 int
1780 acl_check_modlist(
1781         Operation       *op,
1782         Entry   *e,
1783         Modifications   *mlist )
1784 {
1785         struct berval *bv;
1786         AccessControlState state = ACL_STATE_INIT;
1787         Backend *be;
1788         int be_null = 0;
1789         int ret = 1; /* default is access allowed */
1790
1791         be = op->o_bd;
1792         if ( be == NULL ) {
1793                 be = LDAP_STAILQ_FIRST(&backendDB);
1794                 be_null = 1;
1795                 op->o_bd = be;
1796         }
1797         assert( be != NULL );
1798
1799         /* short circuit root database access */
1800         if ( be_isroot( op ) ) {
1801                 Debug( LDAP_DEBUG_ACL,
1802                         "<= acl_access_allowed: granted to database root\n",
1803                     0, 0, 0 );
1804                 goto done;
1805         }
1806
1807         /* use backend default access if no backend acls */
1808         if( op->o_bd != NULL && op->o_bd->be_acl == NULL ) {
1809                 Debug( LDAP_DEBUG_ACL,
1810                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1811                         access2str( ACL_WRITE ),
1812                         op->o_bd->be_dfltaccess >= ACL_WRITE
1813                                 ? "granted" : "denied",
1814                         op->o_dn.bv_val );
1815                 ret = (op->o_bd->be_dfltaccess >= ACL_WRITE);
1816                 goto done;
1817         }
1818
1819         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1820                 /*
1821                  * Internal mods are ignored by ACL_WRITE checking
1822                  */
1823                 if ( mlist->sml_flags & SLAP_MOD_INTERNAL ) {
1824                         Debug( LDAP_DEBUG_ACL, "acl: internal mod %s:"
1825                                 " modify access granted\n",
1826                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1827                         continue;
1828                 }
1829
1830                 /*
1831                  * no-user-modification operational attributes are ignored
1832                  * by ACL_WRITE checking as any found here are not provided
1833                  * by the user
1834                  */
1835                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type )
1836                                 && ! ( mlist->sml_flags & SLAP_MOD_MANAGING ) )
1837                 {
1838                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1839                                 " modify access granted\n",
1840                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1841                         continue;
1842                 }
1843
1844                 switch ( mlist->sml_op ) {
1845                 case LDAP_MOD_REPLACE:
1846                 case LDAP_MOD_INCREMENT:
1847                         /*
1848                          * We must check both permission to delete the whole
1849                          * attribute and permission to add the specific attributes.
1850                          * This prevents abuse from selfwriters.
1851                          */
1852                         if ( ! access_allowed( op, e,
1853                                 mlist->sml_desc, NULL,
1854                                 ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1855                                 &state ) )
1856                         {
1857                                 ret = 0;
1858                                 goto done;
1859                         }
1860
1861                         if ( mlist->sml_values == NULL ) break;
1862
1863                         /* fall thru to check value to add */
1864
1865                 case LDAP_MOD_ADD:
1866                         assert( mlist->sml_values != NULL );
1867
1868                         for ( bv = mlist->sml_nvalues
1869                                         ? mlist->sml_nvalues : mlist->sml_values;
1870                                 bv->bv_val != NULL; bv++ )
1871                         {
1872                                 if ( ! access_allowed( op, e,
1873                                         mlist->sml_desc, bv,
1874                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WADD,
1875                                         &state ) )
1876                                 {
1877                                         ret = 0;
1878                                         goto done;
1879                                 }
1880                         }
1881                         break;
1882
1883                 case LDAP_MOD_DELETE:
1884                         if ( mlist->sml_values == NULL ) {
1885                                 if ( ! access_allowed( op, e,
1886                                         mlist->sml_desc, NULL,
1887                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1888                                         NULL ) )
1889                                 {
1890                                         ret = 0;
1891                                         goto done;
1892                                 }
1893                                 break;
1894                         }
1895                         for ( bv = mlist->sml_nvalues
1896                                         ? mlist->sml_nvalues : mlist->sml_values;
1897                                 bv->bv_val != NULL; bv++ )
1898                         {
1899                                 if ( ! access_allowed( op, e,
1900                                         mlist->sml_desc, bv,
1901                                         ( mlist->sml_flags & SLAP_MOD_MANAGING ) ? ACL_MANAGE : ACL_WDEL,
1902                                         &state ) )
1903                                 {
1904                                         ret = 0;
1905                                         goto done;
1906                                 }
1907                         }
1908                         break;
1909
1910                 case SLAP_MOD_SOFTADD:
1911                         /* allow adding attribute via modrdn thru */
1912                         break;
1913
1914                 default:
1915                         assert( 0 );
1916                         /* not reached */
1917                         ret = 0;
1918                         break;
1919                 }
1920         }
1921
1922 done:
1923         if (be_null) op->o_bd = NULL;
1924         return( ret );
1925 }
1926
1927 int
1928 acl_get_part(
1929         struct berval   *list,
1930         int             ix,
1931         char            sep,
1932         struct berval   *bv )
1933 {
1934         int     len;
1935         char    *p;
1936
1937         if ( bv ) {
1938                 BER_BVZERO( bv );
1939         }
1940         len = list->bv_len;
1941         p = list->bv_val;
1942         while ( len >= 0 && --ix >= 0 ) {
1943                 while ( --len >= 0 && *p++ != sep )
1944                         ;
1945         }
1946         while ( len >= 0 && *p == ' ' ) {
1947                 len--;
1948                 p++;
1949         }
1950         if ( len < 0 ) {
1951                 return -1;
1952         }
1953
1954         if ( !bv ) {
1955                 return 0;
1956         }
1957
1958         bv->bv_val = p;
1959         while ( --len >= 0 && *p != sep ) {
1960                 bv->bv_len++;
1961                 p++;
1962         }
1963         while ( bv->bv_len > 0 && *--p == ' ' ) {
1964                 bv->bv_len--;
1965         }
1966         
1967         return bv->bv_len;
1968 }
1969
1970 typedef struct acl_set_gather_t {
1971         SetCookie               *cookie;
1972         BerVarray               bvals;
1973 } acl_set_gather_t;
1974
1975 static int
1976 acl_set_cb_gather( Operation *op, SlapReply *rs )
1977 {
1978         acl_set_gather_t        *p = (acl_set_gather_t *)op->o_callback->sc_private;
1979         
1980         if ( rs->sr_type == REP_SEARCH ) {
1981                 BerValue        bvals[ 2 ];
1982                 BerVarray       bvalsp = NULL;
1983                 int             j;
1984
1985                 for ( j = 0; !BER_BVISNULL( &rs->sr_attrs[ j ].an_name ); j++ ) {
1986                         AttributeDescription    *desc = rs->sr_attrs[ j ].an_desc;
1987                         
1988                         if ( desc == slap_schema.si_ad_entryDN ) {
1989                                 bvalsp = bvals;
1990                                 bvals[ 0 ] = rs->sr_entry->e_nname;
1991                                 BER_BVZERO( &bvals[ 1 ] );
1992
1993                         } else {
1994                                 Attribute       *a;
1995
1996                                 a = attr_find( rs->sr_entry->e_attrs, desc );
1997                                 if ( a != NULL ) {
1998                                         int     i;
1999
2000                                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ )
2001                                                 ;
2002
2003                                         bvalsp = a->a_nvals;
2004                                 }
2005                         }
2006                 }
2007
2008                 if ( bvals ) {
2009                         p->bvals = slap_set_join( p->cookie, p->bvals,
2010                                         ( '|' | SLAP_SET_RREF ), bvalsp );
2011                 }
2012
2013         } else {
2014                 assert( rs->sr_type == REP_RESULT );
2015         }
2016
2017         return 0;
2018 }
2019
2020 BerVarray
2021 acl_set_gather( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2022 {
2023         AclSetCookie            *cp = (AclSetCookie *)cookie;
2024         int                     rc = 0;
2025         LDAPURLDesc             *ludp = NULL;
2026         Operation               op2 = { 0 };
2027         SlapReply               rs = {REP_RESULT};
2028         AttributeName           anlist[ 2 ], *anlistp = NULL;
2029         int                     nattrs = 0;
2030         slap_callback           cb = { NULL, acl_set_cb_gather, NULL, NULL };
2031         acl_set_gather_t        p = { 0 };
2032         const char              *text = NULL;
2033         static struct berval    defaultFilter_bv = BER_BVC( "(objectClass=*)" );
2034
2035         /* this routine needs to return the bervals instead of
2036          * plain strings, since syntax is not known.  It should
2037          * also return the syntax or some "comparison cookie".
2038          */
2039         if ( strncasecmp( name->bv_val, "ldap:///", STRLENOF( "ldap:///" ) ) != 0 ) {
2040                 return acl_set_gather2( cookie, name, desc );
2041         }
2042
2043         rc = ldap_url_parse( name->bv_val, &ludp );
2044         if ( rc != LDAP_URL_SUCCESS ) {
2045                 rc = LDAP_PROTOCOL_ERROR;
2046                 goto url_done;
2047         }
2048         
2049         if ( ( ludp->lud_host && ludp->lud_host[0] ) || ludp->lud_exts )
2050         {
2051                 /* host part must be empty */
2052                 /* extensions parts must be empty */
2053                 rc = LDAP_PROTOCOL_ERROR;
2054                 goto url_done;
2055         }
2056
2057         /* Grab the searchbase and see if an appropriate database can be found */
2058         ber_str2bv( ludp->lud_dn, 0, 0, &op2.o_req_dn );
2059         rc = dnNormalize( 0, NULL, NULL, &op2.o_req_dn,
2060                         &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2061         BER_BVZERO( &op2.o_req_dn );
2062         if ( rc != LDAP_SUCCESS ) {
2063                 goto url_done;
2064         }
2065
2066         op2.o_bd = select_backend( &op2.o_req_ndn, 0, 1 );
2067         if ( ( op2.o_bd == NULL ) || ( op2.o_bd->be_search == NULL ) ) {
2068                 rc = LDAP_NO_SUCH_OBJECT;
2069                 goto url_done;
2070         }
2071
2072         /* Grab the filter */
2073         if ( ludp->lud_filter ) {
2074                 ber_str2bv_x( ludp->lud_filter, 0, 0, &op2.ors_filterstr,
2075                                 cp->asc_op->o_tmpmemctx );
2076                 
2077         } else {
2078                 op2.ors_filterstr = defaultFilter_bv;
2079         }
2080
2081         op2.ors_filter = str2filter_x( cp->asc_op, op2.ors_filterstr.bv_val );
2082         if ( op2.ors_filter == NULL ) {
2083                 rc = LDAP_PROTOCOL_ERROR;
2084                 goto url_done;
2085         }
2086
2087         /* Grab the scope */
2088         op2.ors_scope = ludp->lud_scope;
2089
2090         /* Grap the attributes */
2091         if ( ludp->lud_attrs ) {
2092                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ )
2093                         ;
2094
2095                 anlistp = slap_sl_malloc( sizeof( AttributeName ) * ( nattrs + 2 ),
2096                                 cp->asc_op->o_tmpmemctx );
2097
2098                 for ( ; ludp->lud_attrs[ nattrs ]; nattrs++ ) {
2099                         ber_str2bv( ludp->lud_attrs[ nattrs ], 0, 0, &anlistp[ nattrs ].an_name );
2100                         anlistp[ nattrs ].an_desc = NULL;
2101                         rc = slap_bv2ad( &anlistp[ nattrs ].an_name,
2102                                         &anlistp[ nattrs ].an_desc, &text );
2103                         if ( rc != LDAP_SUCCESS ) {
2104                                 goto url_done;
2105                         }
2106                 }
2107
2108         } else {
2109                 anlistp = anlist;
2110         }
2111
2112         anlistp[ nattrs ].an_name = desc->ad_cname;
2113         anlistp[ nattrs ].an_desc = desc;
2114
2115         BER_BVZERO( &anlistp[ nattrs + 1 ].an_name );
2116         
2117         p.cookie = cookie;
2118         
2119         op2.o_hdr = cp->asc_op->o_hdr;
2120         op2.o_tag = LDAP_REQ_SEARCH;
2121         op2.o_ndn = op2.o_bd->be_rootndn;
2122         op2.o_callback = &cb;
2123         slap_op_time( &op2.o_time, &op2.o_tincr );
2124         op2.o_do_not_cache = 1;
2125         op2.o_is_auth_check = 0;
2126         ber_dupbv_x( &op2.o_req_dn, &op2.o_req_ndn, cp->asc_op->o_tmpmemctx );
2127         op2.ors_slimit = SLAP_NO_LIMIT;
2128         op2.ors_tlimit = SLAP_NO_LIMIT;
2129         op2.ors_attrs = anlistp;
2130         op2.ors_attrsonly = 0;
2131         op2.o_private = cp->asc_op->o_private;
2132
2133         cb.sc_private = &p;
2134
2135         rc = op2.o_bd->be_search( &op2, &rs );
2136         if ( rc != 0 ) {
2137                 goto url_done;
2138         }
2139
2140 url_done:;
2141         if ( op2.ors_filter ) {
2142                 filter_free_x( cp->asc_op, op2.ors_filter );
2143         }
2144         if ( !BER_BVISNULL( &op2.o_req_ndn ) ) {
2145                 slap_sl_free( op2.o_req_ndn.bv_val, cp->asc_op->o_tmpmemctx );
2146         }
2147         if ( !BER_BVISNULL( &op2.o_req_dn ) ) {
2148                 slap_sl_free( op2.o_req_dn.bv_val, cp->asc_op->o_tmpmemctx );
2149         }
2150         if ( ludp ) {
2151                 ldap_free_urldesc( ludp );
2152         }
2153         if ( anlistp && anlistp != anlist ) {
2154                 slap_sl_free( anlistp, cp->asc_op->o_tmpmemctx );
2155         }
2156
2157         return p.bvals;
2158 }
2159
2160 BerVarray
2161 acl_set_gather2( SetCookie *cookie, struct berval *name, AttributeDescription *desc )
2162 {
2163         AclSetCookie    *cp = (AclSetCookie *)cookie;
2164         BerVarray       bvals = NULL;
2165         struct berval   ndn;
2166         int             rc = 0;
2167
2168         /* this routine needs to return the bervals instead of
2169          * plain strings, since syntax is not known.  It should
2170          * also return the syntax or some "comparison cookie".
2171          */
2172         rc = dnNormalize( 0, NULL, NULL, name, &ndn, cp->asc_op->o_tmpmemctx );
2173         if ( rc == LDAP_SUCCESS ) {
2174                 if ( desc == slap_schema.si_ad_entryDN ) {
2175                         bvals = (BerVarray)slap_sl_malloc( sizeof( BerValue ) * 2,
2176                                         cp->asc_op->o_tmpmemctx );
2177                         bvals[ 0 ] = ndn;
2178                         BER_BVZERO( &bvals[ 1 ] );
2179                         BER_BVZERO( &ndn );
2180
2181                 } else {
2182                         backend_attribute( cp->asc_op,
2183                                 cp->asc_e, &ndn, desc, &bvals, ACL_NONE );
2184                 }
2185
2186                 if ( !BER_BVISNULL( &ndn ) ) {
2187                         slap_sl_free( ndn.bv_val, cp->asc_op->o_tmpmemctx );
2188                 }
2189         }
2190
2191         return bvals;
2192 }
2193
2194 int
2195 acl_match_set (
2196         struct berval *subj,
2197         Operation *op,
2198         Entry *e,
2199         struct berval *default_set_attribute )
2200 {
2201         struct berval   set = BER_BVNULL;
2202         int             rc = 0;
2203         AclSetCookie    cookie;
2204
2205         if ( default_set_attribute == NULL ) {
2206                 ber_dupbv_x( &set, subj, op->o_tmpmemctx );
2207
2208         } else {
2209                 struct berval           subjdn, ndn = BER_BVNULL;
2210                 struct berval           setat;
2211                 BerVarray               bvals = NULL;
2212                 const char              *text;
2213                 AttributeDescription    *desc = NULL;
2214
2215                 /* format of string is "entry/setAttrName" */
2216                 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
2217                         return 0;
2218                 }
2219
2220                 if ( acl_get_part( subj, 1, '/', &setat ) < 0 ) {
2221                         setat = *default_set_attribute;
2222                 }
2223
2224                 /*
2225                  * NOTE: dnNormalize honors the ber_len field
2226                  * as the length of the dn to be normalized
2227                  */
2228                 if ( slap_bv2ad( &setat, &desc, &text ) == LDAP_SUCCESS ) {
2229                         if ( dnNormalize( 0, NULL, NULL, &subjdn, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
2230                         {
2231                                 backend_attribute( op, e, &ndn, desc, &bvals, ACL_NONE );
2232                                 if ( bvals != NULL && !BER_BVISNULL( &bvals[0] ) ) {
2233                                         int     i;
2234
2235                                         set = bvals[0];
2236                                         BER_BVZERO( &bvals[0] );
2237                                         for ( i = 1; !BER_BVISNULL( &bvals[i] ); i++ )
2238                                                 /* count */ ;
2239                                         bvals[0].bv_val = bvals[i-1].bv_val;
2240                                         BER_BVZERO( &bvals[i-1] );
2241                                 }
2242                                 ber_bvarray_free_x( bvals, op->o_tmpmemctx );
2243                                 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2244                         }
2245                 }
2246         }
2247
2248         if ( !BER_BVISNULL( &set ) ) {
2249                 cookie.asc_op = op;
2250                 cookie.asc_e = e;
2251                 rc = ( slap_set_filter(
2252                         acl_set_gather,
2253                         (SetCookie *)&cookie, &set,
2254                         &op->o_ndn, &e->e_nname, NULL ) > 0 );
2255                 slap_sl_free( set.bv_val, op->o_tmpmemctx );
2256         }
2257
2258         return(rc);
2259 }
2260
2261 #ifdef SLAP_DYNACL
2262
2263 /*
2264  * dynamic ACL infrastructure
2265  */
2266 static slap_dynacl_t    *da_list = NULL;
2267
2268 int
2269 slap_dynacl_register( slap_dynacl_t *da )
2270 {
2271         slap_dynacl_t   *tmp;
2272
2273         for ( tmp = da_list; tmp; tmp = tmp->da_next ) {
2274                 if ( strcasecmp( da->da_name, tmp->da_name ) == 0 ) {
2275                         break;
2276                 }
2277         }
2278
2279         if ( tmp != NULL ) {
2280                 return -1;
2281         }
2282         
2283         if ( da->da_mask == NULL ) {
2284                 return -1;
2285         }
2286         
2287         da->da_private = NULL;
2288         da->da_next = da_list;
2289         da_list = da;
2290
2291         return 0;
2292 }
2293
2294 static slap_dynacl_t *
2295 slap_dynacl_next( slap_dynacl_t *da )
2296 {
2297         if ( da ) {
2298                 return da->da_next;
2299         }
2300         return da_list;
2301 }
2302
2303 slap_dynacl_t *
2304 slap_dynacl_get( const char *name )
2305 {
2306         slap_dynacl_t   *da;
2307
2308         for ( da = slap_dynacl_next( NULL ); da; da = slap_dynacl_next( da ) ) {
2309                 if ( strcasecmp( da->da_name, name ) == 0 ) {
2310                         break;
2311                 }
2312         }
2313
2314         return da;
2315 }
2316 #endif /* SLAP_DYNACL */
2317
2318 /*
2319  * statically built-in dynamic ACL initialization
2320  */
2321 static int (*acl_init_func[])( void ) = {
2322 #ifdef SLAP_DYNACL
2323         /* TODO: remove when ACI will only be dynamic */
2324 #if SLAPD_ACI_ENABLED == SLAPD_MOD_STATIC
2325         dynacl_aci_init,
2326 #endif /* SLAPD_ACI_ENABLED */
2327 #endif /* SLAP_DYNACL */
2328
2329         NULL
2330 };
2331
2332 int
2333 acl_init( void )
2334 {
2335         int     i, rc;
2336
2337         for ( i = 0; acl_init_func[ i ] != NULL; i++ ) {
2338                 rc = (*(acl_init_func[ i ]))();
2339                 if ( rc != 0 ) {
2340                         return rc;
2341                 }
2342         }
2343
2344         return 0;
2345 }
2346
2347 int
2348 acl_string_expand(
2349         struct berval   *bv,
2350         struct berval   *pat,
2351         char            *match,
2352         int             nmatch,
2353         regmatch_t      *matches)
2354 {
2355         ber_len_t       size;
2356         char   *sp;
2357         char   *dp;
2358         int     flag;
2359
2360         size = 0;
2361         bv->bv_val[0] = '\0';
2362         bv->bv_len--; /* leave space for lone $ */
2363
2364         flag = 0;
2365         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
2366                 sp < pat->bv_val + pat->bv_len ; sp++ )
2367         {
2368                 /* did we previously see a $ */
2369                 if ( flag ) {
2370                         if ( flag == 1 && *sp == '$' ) {
2371                                 *dp++ = '$';
2372                                 size++;
2373                                 flag = 0;
2374
2375                         } else if ( flag == 1 && *sp == '{' /*'}'*/) {
2376                                 flag = 2;
2377
2378                         } else if ( *sp >= '0' && *sp <= '9' ) {
2379                                 int     n;
2380                                 int     i;
2381                                 int     l;
2382
2383                                 n = *sp - '0';
2384
2385                                 if ( flag == 2 ) {
2386                                         for ( sp++; *sp != '\0' && *sp != /*'{'*/ '}'; sp++ ) {
2387                                                 if ( *sp >= '0' && *sp <= '9' ) {
2388                                                         n = 10*n + ( *sp - '0' );
2389                                                 }
2390                                         }
2391
2392                                         if ( *sp != /*'{'*/ '}' ) {
2393                                                 /* FIXME: error */
2394                                                 return 1;
2395                                         }
2396                                 }
2397
2398                                 if ( n >= nmatch ) {
2399                                         /* FIXME: error */
2400                                         return 1;
2401                                 }
2402                                 
2403                                 *dp = '\0';
2404                                 i = matches[n].rm_so;
2405                                 l = matches[n].rm_eo; 
2406                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
2407                                         *dp++ = match[i];
2408                                 }
2409                                 *dp = '\0';
2410
2411                                 flag = 0;
2412                         }
2413                 } else {
2414                         if (*sp == '$') {
2415                                 flag = 1;
2416                         } else {
2417                                 *dp++ = *sp;
2418                                 size++;
2419                         }
2420                 }
2421         }
2422
2423         if ( flag ) {
2424                 /* must have ended with a single $ */
2425                 *dp++ = '$';
2426                 size++;
2427         }
2428
2429         *dp = '\0';
2430         bv->bv_len = size;
2431
2432         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: pattern:  %.*s\n", (int)pat->bv_len, pat->bv_val, 0 );
2433         Debug( LDAP_DEBUG_TRACE, "=> acl_string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
2434
2435         return 0;
2436 }
2437
2438 static int
2439 regex_matches(
2440         struct berval   *pat,           /* pattern to expand and match against */
2441         char            *str,           /* string to match against pattern */
2442         char            *buf,           /* buffer with $N expansion variables */
2443         int             nmatch, /* size of the matches array */
2444         regmatch_t      *matches        /* offsets in buffer for $N expansion variables */
2445 )
2446 {
2447         regex_t re;
2448         char newbuf[ACL_BUF_SIZE];
2449         struct berval bv;
2450         int     rc;
2451
2452         bv.bv_len = sizeof( newbuf ) - 1;
2453         bv.bv_val = newbuf;
2454
2455         if (str == NULL) {
2456                 str = "";
2457         };
2458
2459         acl_string_expand( &bv, pat, buf, nmatch, matches );
2460         rc = regcomp( &re, newbuf, REG_EXTENDED|REG_ICASE );
2461         if ( rc ) {
2462                 char error[ACL_BUF_SIZE];
2463                 regerror( rc, &re, error, sizeof( error ) );
2464
2465                 Debug( LDAP_DEBUG_TRACE,
2466                     "compile( \"%s\", \"%s\") failed %s\n",
2467                         pat->bv_val, str, error );
2468                 return( 0 );
2469         }
2470
2471         rc = regexec( &re, str, 0, NULL, 0 );
2472         regfree( &re );
2473
2474         Debug( LDAP_DEBUG_TRACE,
2475             "=> regex_matches: string:   %s\n", str, 0, 0 );
2476         Debug( LDAP_DEBUG_TRACE,
2477             "=> regex_matches: rc: %d %s\n",
2478                 rc, !rc ? "matches" : "no matches", 0 );
2479         return( !rc );
2480 }
2481