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