]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
0b78173e19f948c16183201ad08c38520c34e2e8
[openldap] / servers / slapd / acl.c
1 /* acl.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/regex.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15
16 #include "slap.h"
17 #include "sets.h"
18 #include "lber_pvt.h"
19
20
21 /*
22  * speed up compares
23  */
24 static struct berval 
25         aci_bv_entry            = { sizeof("entry") - 1,        "entry" },
26         aci_bv_br_entry         = { sizeof("[entry]") - 1,      "[entry]" },
27         aci_bv_br_all           = { sizeof("[all]") - 1,        "[all]" },
28         aci_bv_access_id        = { sizeof("access-id") - 1,    "access-id" },
29         aci_bv_anonymous        = { sizeof("anonymous") - 1,    "anonymous" },
30         aci_bv_users            = { sizeof("users") - 1,        "users" },
31         aci_bv_self             = { sizeof("self") - 1,         "self" },
32         aci_bv_dnattr           = { sizeof("dnattr") - 1,       "dnattr" },
33         aci_bv_group            = { sizeof("group") - 1,        "group" },
34         aci_bv_role             = { sizeof("role") - 1,         "role" },
35         aci_bv_set              = { sizeof("set") - 1,          "set" },
36         aci_bv_set_ref          = { sizeof("set-ref") - 1,      "set-ref"},
37         aci_bv_grant            = { sizeof("grant") - 1,        "grant" },
38         aci_bv_deny             = { sizeof("deny") - 1,         "deny" };
39
40 static AccessControl * acl_get(
41         AccessControl *ac, int *count,
42         Backend *be, Operation *op,
43         Entry *e,
44         AttributeDescription *desc,
45         int nmatches, regmatch_t *matches );
46
47 static slap_control_t acl_mask(
48         AccessControl *ac, slap_mask_t *mask,
49         Backend *be, Connection *conn, Operation *op,
50         Entry *e,
51         AttributeDescription *desc,
52         struct berval *val,
53         regmatch_t *matches );
54
55 #ifdef SLAPD_ACI_ENABLED
56 static int aci_mask(
57         Backend *be,
58     Connection *conn,
59         Operation *op,
60         Entry *e,
61         AttributeDescription *desc,
62         struct berval *val,
63         struct berval *aci,
64         regmatch_t *matches,
65         slap_access_t *grant,
66         slap_access_t *deny );
67 #endif
68
69 static int      regex_matches(
70         char *pat, char *str, char *buf, regmatch_t *matches);
71 static void     string_expand(
72         struct berval *newbuf, char *pattern,
73         char *match, regmatch_t *matches);
74
75 typedef struct AciSetCookie {
76         Backend *be;
77         Entry *e;
78         Connection *conn;
79         Operation *op;
80 } AciSetCookie;
81
82 BerVarray aci_set_gather (void *cookie, char *name, struct berval *attr);
83 static int aci_match_set ( struct berval *subj, Backend *be,
84     Entry *e, Connection *conn, Operation *op, int setref );
85
86 /*
87  * access_allowed - check whether op->o_ndn is allowed the requested access
88  * to entry e, attribute attr, value val.  if val is null, access to
89  * the whole attribute is assumed (all values).
90  *
91  * This routine loops through all access controls and calls
92  * acl_mask() on each applicable access control.
93  * The loop exits when a definitive answer is reached or
94  * or no more controls remain.
95  *
96  * returns:
97  *              0       access denied
98  *              1       access granted
99  */
100
101 int
102 access_allowed(
103     Backend             *be,
104     Connection          *conn,
105     Operation           *op,
106     Entry               *e,
107         AttributeDescription    *desc,
108     struct berval       *val,
109     slap_access_t       access )
110 {
111         int                             count;
112         AccessControl   *a;
113 #ifdef LDAP_DEBUG
114         char accessmaskbuf[ACCESSMASK_MAXLEN];
115 #endif
116         slap_mask_t mask;
117         slap_control_t control;
118         const char *attr;
119         regmatch_t matches[MAXREMATCHES];
120
121         assert( e != NULL );
122         assert( desc != NULL );
123         assert( access > ACL_NONE );
124
125         attr = desc->ad_cname.bv_val;
126
127         assert( attr != NULL );
128
129 #ifdef NEW_LOGGING
130         LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
131                 "access_allowed: conn %d %s access to \"%s\" \"%s\" requested\n",
132                 conn ? conn->c_connid : -1, access2str( access ), e->e_dn, attr ));
133 #else
134         Debug( LDAP_DEBUG_ACL,
135                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
136             access2str( access ), e->e_dn, attr );
137 #endif
138
139         if ( op == NULL ) {
140                 /* no-op call */
141                 return 1;
142         }
143
144         if ( be == NULL ) be = &backends[0];
145         assert( be != NULL );
146
147         /* grant database root access */
148         if ( be != NULL && be_isroot( be, &op->o_ndn ) ) {
149 #ifdef NEW_LOGGING
150                 LDAP_LOG(( "acl", LDAP_LEVEL_INFO,
151                        "access_allowed: conn %d root access granted\n",
152                        conn->c_connid));
153 #else
154                 Debug( LDAP_DEBUG_ACL,
155                     "<= root access granted\n",
156                         0, 0, 0 );
157 #endif
158                 return 1;
159         }
160
161         /*
162          * no-user-modification operational attributes are ignored
163          * by ACL_WRITE checking as any found here are not provided
164          * by the user
165          */
166         if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type )
167                 && desc != slap_schema.si_ad_entry
168                 && desc != slap_schema.si_ad_children )
169         {
170 #ifdef NEW_LOGGING
171                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
172                        "access_allowed: conn %d NoUserMod Operational attribute: %s access granted\n",
173                        conn->c_connid, attr ));
174 #else
175                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
176                         " %s access granted\n",
177                         attr, 0, 0 );
178 #endif
179                 return 1;
180         }
181
182         /* use backend default access if no backend acls */
183         if( be != NULL && be->be_acl == NULL ) {
184 #ifdef NEW_LOGGING
185                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
186                        "access_allowed: conn %d backend default %s access %s to \"%s\"\n",
187                        conn->c_connid, access2str( access ),
188                        be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val ));
189 #else
190                 Debug( LDAP_DEBUG_ACL,
191                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
192                         access2str( access ),
193                         be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn.bv_val );
194 #endif
195                 return be->be_dfltaccess >= access;
196
197 #ifdef notdef
198         /* be is always non-NULL */
199         /* use global default access if no global acls */
200         } else if ( be == NULL && global_acl == NULL ) {
201 #ifdef NEW_LOGGING
202                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
203                        "access_allowed: conn %d global default %s access %s to \"%s\"\n",
204                        conn->c_connid, access2str( access ),
205                        global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val ));
206 #else
207                 Debug( LDAP_DEBUG_ACL,
208                         "=> access_allowed: global default %s access %s to \"%s\"\n",
209                         access2str( access ),
210                         global_default_access >= access ? "granted" : "denied", op->o_dn.bv_val );
211 #endif
212                 return global_default_access >= access;
213 #endif
214         }
215
216         ACL_INIT(mask);
217         memset(matches, '\0', sizeof(matches));
218         
219         control = ACL_BREAK;
220         a = NULL;
221         count = 0;
222
223         while((a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches )) != NULL)
224         {
225                 int i;
226
227                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
228 #ifdef NEW_LOGGING
229                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
230                                "access_allowed: conn %d match[%d]:  %d %d ",
231                                conn->c_connid, i, (int)matches[i].rm_so, (int)matches[i].rm_eo ));
232 #else
233                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
234                                (int)matches[i].rm_so, (int)matches[i].rm_eo );
235 #endif
236                         if( matches[i].rm_so <= matches[0].rm_eo ) {
237                                 int n;
238                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
239                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
240                                 }
241                         }
242 #ifdef NEW_LOGGING
243                         LDAP_LOG(( "acl", LDAP_LEVEL_ARGS, "\n" ));
244 #else
245                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
246 #endif
247                 }
248
249                 control = acl_mask( a, &mask, be, conn, op,
250                         e, desc, val, matches );
251
252                 if ( control != ACL_BREAK ) {
253                         break;
254                 }
255
256                 memset(matches, '\0', sizeof(matches));
257         }
258
259         if ( ACL_IS_INVALID( mask ) ) {
260 #ifdef NEW_LOGGING
261                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
262                        "access_allowed: conn %d  \"%s\" (%s) invalid!\n",
263                        conn->c_connid, e->e_dn, attr ));
264 #else
265                 Debug( LDAP_DEBUG_ACL,
266                         "=> access_allowed: \"%s\" (%s) invalid!\n",
267                         e->e_dn, attr, 0 );
268 #endif
269                 ACL_INIT( mask );
270
271         } else if ( control == ACL_BREAK ) {
272 #ifdef NEW_LOGGING
273                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
274                        "access_allowed: conn %d  no more rules\n", conn->c_connid ));
275 #else
276                 Debug( LDAP_DEBUG_ACL,
277                         "=> access_allowed: no more rules\n", 0, 0, 0);
278 #endif
279                 ACL_INIT( mask );
280         }
281
282 #ifdef NEW_LOGGING
283         LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
284                    "access_allowed: conn %d  %s access %s by %s\n",
285                    conn->c_connid,
286                    access2str( access ),
287                    ACL_GRANT( mask, access ) ? "granted" : "denied",
288                    accessmask2str( mask, accessmaskbuf ) ));
289 #else
290         Debug( LDAP_DEBUG_ACL,
291                 "=> access_allowed: %s access %s by %s\n",
292                 access2str( access ),
293                 ACL_GRANT(mask, access) ? "granted" : "denied",
294                 accessmask2str( mask, accessmaskbuf ) );
295 #endif
296         return ACL_GRANT(mask, access);
297 }
298
299 /*
300  * acl_get - return the acl applicable to entry e, attribute
301  * attr.  the acl returned is suitable for use in subsequent calls to
302  * acl_access_allowed().
303  */
304
305 static AccessControl *
306 acl_get(
307         AccessControl *a,
308         int                     *count,
309     Backend             *be,
310     Operation   *op,
311     Entry               *e,
312         AttributeDescription *desc,
313     int                 nmatch,
314     regmatch_t  *matches )
315 {
316         const char *attr;
317         int dnlen, patlen;
318
319         assert( e != NULL );
320         assert( count != NULL );
321         assert( desc != NULL );
322
323         attr = desc->ad_cname.bv_val;
324
325         assert( attr != NULL );
326
327         if( a == NULL ) {
328                 if( be == NULL ) {
329                         a = global_acl;
330                 } else {
331                         a = be->be_acl;
332                 }
333
334                 assert( a != NULL );
335
336         } else {
337                 a = a->acl_next;
338         }
339
340         dnlen = e->e_nname.bv_len;
341
342         for ( ; a != NULL; a = a->acl_next ) {
343                 (*count) ++;
344
345                 if (a->acl_dn_pat.bv_len != 0) {
346                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
347 #ifdef NEW_LOGGING
348                                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
349                                            "acl_get: dnpat [%d] %s nsub: %d\n",
350                                            *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub ));
351 #else
352                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
353                                         *count, a->acl_dn_pat.bv_val, (int) a->acl_dn_re.re_nsub );
354 #endif
355                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
356                                         continue;
357
358                         } else {
359 #ifdef NEW_LOGGING
360                                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
361                                            "acl_get: dn [%d] %s\n",
362                                            *count, a->acl_dn_pat.bv_val ));
363 #else
364                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
365                                         *count, a->acl_dn_pat.bv_val, 0 );
366 #endif
367                                 patlen = a->acl_dn_pat.bv_len;
368                                 if ( dnlen < patlen )
369                                         continue;
370
371                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
372                                         /* base dn -- entire object DN must match */
373                                         if ( dnlen != patlen )
374                                                 continue;
375
376                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
377                                         int rdnlen = -1;
378
379                                         if ( dnlen <= patlen )
380                                                 continue;
381
382                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) )
383                                                 continue;
384
385                                         rdnlen = dn_rdnlen( NULL, &e->e_nname );
386                                         if ( rdnlen != dnlen - patlen - 1 )
387                                                 continue;
388
389                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
390                                         if ( dnlen > patlen && ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) ) )
391                                                 continue;
392
393                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
394                                         if ( dnlen <= patlen )
395                                                 continue;
396                                         if ( !DN_SEPARATOR( e->e_ndn[dnlen - patlen - 1] ) || DN_ESCAPE( e->e_ndn[dnlen - patlen - 2] ) )
397                                                 continue;
398                                 }
399
400                                 if ( strcmp( a->acl_dn_pat.bv_val, e->e_ndn + dnlen - patlen ) != 0 )
401                                         continue;
402                         }
403
404 #ifdef NEW_LOGGING
405                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
406                                    "acl_get: [%d] matched\n",
407                                    *count ));
408 #else
409                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
410                                 *count, 0, 0 );
411 #endif
412                 }
413
414                 if ( a->acl_filter != NULL ) {
415                         ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
416                         if ( rc != LDAP_COMPARE_TRUE ) {
417                                 continue;
418                         }
419                 }
420
421 #ifdef NEW_LOGGING
422                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
423                            "acl_get: [%d] check attr %s\n",
424                            *count, attr ));
425 #else
426                 Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
427                        *count, attr, 0);
428 #endif
429                 if ( attr == NULL || a->acl_attrs == NULL ||
430                         ad_inlist( desc, a->acl_attrs ) )
431                 {
432 #ifdef NEW_LOGGING
433                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
434                                    "acl_get:  [%d] acl %s attr: %s\n",
435                                    *count, e->e_dn, attr ));
436 #else
437                         Debug( LDAP_DEBUG_ACL,
438                                 "<= acl_get: [%d] acl %s attr: %s\n",
439                                 *count, e->e_dn, attr );
440 #endif
441                         return a;
442                 }
443                 matches[0].rm_so = matches[0].rm_eo = -1;
444         }
445
446 #ifdef NEW_LOGGING
447         LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
448                    "acl_get: done.\n" ));
449 #else
450         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
451 #endif
452         return( NULL );
453 }
454
455
456 /*
457  * acl_mask - modifies mask based upon the given acl and the
458  * requested access to entry e, attribute attr, value val.  if val
459  * is null, access to the whole attribute is assumed (all values).
460  *
461  * returns      0       access NOT allowed
462  *              1       access allowed
463  */
464
465 static slap_control_t
466 acl_mask(
467     AccessControl       *a,
468         slap_mask_t *mask,
469     Backend             *be,
470     Connection  *conn,
471     Operation   *op,
472     Entry               *e,
473         AttributeDescription *desc,
474     struct berval       *val,
475         regmatch_t      *matches
476 )
477 {
478         int             i, odnlen, patlen;
479         Access  *b;
480 #ifdef LDAP_DEBUG
481         char accessmaskbuf[ACCESSMASK_MAXLEN];
482 #endif
483         const char *attr;
484
485         assert( a != NULL );
486         assert( mask != NULL );
487         assert( desc != NULL );
488
489         attr = desc->ad_cname.bv_val;
490
491         assert( attr != NULL );
492
493 #ifdef NEW_LOGGING
494         LDAP_LOG(( "acl", LDAP_LEVEL_ENTRY,
495                    "acl_mask: conn %d  access to entry \"%s\", attr \"%s\" requested\n",
496                    conn->c_connid, e->e_dn, attr ));
497
498         LDAP_LOG(( "acl", LDAP_LEVEL_ARGS,
499                    " to %s by \"%s\", (%s) \n",
500                    val ? "value" : "all values",
501                    op->o_ndn.bv_val ? op->o_ndn.bv_val : "",
502                    accessmask2str( *mask, accessmaskbuf ) ));
503 #else
504         Debug( LDAP_DEBUG_ACL,
505                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
506                 e->e_dn, attr, 0 );
507
508         Debug( LDAP_DEBUG_ACL,
509                 "=> acl_mask: to %s by \"%s\", (%s) \n",
510                 val ? "value" : "all values",
511                 op->o_ndn.bv_val ?  op->o_ndn.bv_val : "",
512                 accessmask2str( *mask, accessmaskbuf ) );
513 #endif
514
515         for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
516                 slap_mask_t oldmask, modmask;
517
518                 ACL_INVALIDATE( modmask );
519
520                 /* AND <who> clauses */
521                 if ( b->a_dn_pat.bv_len != 0 ) {
522 #ifdef NEW_LOGGING
523                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
524                                    "acl_mask: conn %d  check a_dn_pat: %s\n",
525                                    conn->c_connid, b->a_dn_pat.bv_val ));
526 #else
527                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
528                                 b->a_dn_pat.bv_val, 0, 0);
529 #endif
530                         /*
531                          * if access applies to the entry itself, and the
532                          * user is bound as somebody in the same namespace as
533                          * the entry, OR the given dn matches the dn pattern
534                          */
535                         if ( ber_bvcmp( &b->a_dn_pat, &aci_bv_anonymous ) == 0 ) {
536                                 if ( op->o_ndn.bv_len != 0 ) {
537                                         continue;
538                                 }
539
540                         } else if ( ber_bvcmp( &b->a_dn_pat, &aci_bv_users ) == 0 ) {
541                                 if ( op->o_ndn.bv_len == 0 ) {
542                                         continue;
543                                 }
544
545                         } else if ( ber_bvcmp( &b->a_dn_pat, &aci_bv_self ) == 0 ) {
546                                 if ( op->o_ndn.bv_len == 0 ) {
547                                         continue;
548                                 }
549                                 
550                                 if ( e->e_dn == NULL || !dn_match( &e->e_nname, &op->o_ndn ) ) {
551                                         continue;
552                                 }
553
554                         } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
555                                 if ( ber_bvccmp( &b->a_dn_pat, '*' ) == 0 ) {
556                                         int ret = regex_matches( b->a_dn_pat.bv_val,
557                                                 op->o_ndn.bv_val, e->e_ndn, matches );
558
559                                         if( ret == 0 ) {
560                                                 continue;
561                                         }
562                                 }
563
564                         } else {
565                                 if ( e->e_dn == NULL )
566                                         continue;
567
568                                 patlen = b->a_dn_pat.bv_len;
569                                 odnlen = op->o_ndn.bv_len;
570                                 if ( odnlen < patlen )
571                                         continue;
572
573                                 if ( b->a_dn_style == ACL_STYLE_BASE ) {
574                                         /* base dn -- entire object DN must match */
575                                         if ( odnlen != patlen )
576                                                 continue;
577
578                                 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
579                                         int rdnlen = -1;
580
581                                         if ( odnlen <= patlen )
582                                                 continue;
583
584                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn.bv_val[odnlen - patlen - 2] ) )
585                                                 continue;
586
587                                         rdnlen = dn_rdnlen( NULL, &op->o_ndn );
588                                         if ( rdnlen != odnlen - patlen - 1 )
589                                                 continue;
590
591                                 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
592                                         if ( odnlen > patlen && ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn.bv_val[odnlen - patlen - 2] ) ) )
593                                                 continue;
594
595                                 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
596                                         if ( odnlen <= patlen )
597                                                 continue;
598                                         if ( !DN_SEPARATOR( op->o_ndn.bv_val[odnlen - patlen - 1] ) || DN_ESCAPE( op->o_ndn.bv_val[odnlen - patlen - 2] ) )
599                                                 continue;
600                                 }
601
602                                 if ( strcmp( b->a_dn_pat.bv_val, op->o_ndn.bv_val + odnlen - patlen ) != 0 )
603                                         continue;
604                         }
605                 }
606
607                 if ( b->a_sockurl_pat != NULL ) {
608 #ifdef NEW_LOGGING
609                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
610                                    "acl_mask: conn %d  check a_sockurl_pat: %s\n",
611                                    conn->c_connid, b->a_sockurl_pat ));
612 #else
613                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
614                                 b->a_sockurl_pat, 0, 0 );
615 #endif
616
617                         if ( ber_strccmp( b->a_sockurl_pat, '*' ) != 0) {
618                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
619                                         if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
620                                                         e->e_ndn, matches ) ) 
621                                         {
622                                                 continue;
623                                         }
624                                 } else {
625                                         if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) != 0 )
626                                                 continue;
627                                 }
628                         }
629                 }
630
631                 if ( b->a_domain_pat != NULL ) {
632 #ifdef NEW_LOGGING
633                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
634                                    "acl_mask: conn %d  check a_domain_pat: %s\n",
635                                    conn->c_connid, b->a_domain_pat ));
636 #else
637                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
638                                 b->a_domain_pat, 0, 0 );
639 #endif
640                         if ( ber_strccmp( b->a_domain_pat, '*' ) != 0) {
641                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
642                                         if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
643                                                         e->e_ndn, matches ) ) 
644                                         {
645                                                 continue;
646                                         }
647                                 } else {
648                                         if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) != 0 )
649                                                 continue;
650                                 }
651                         }
652                 }
653
654                 if ( b->a_peername_pat != NULL ) {
655 #ifdef NEW_LOGGING
656                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
657                                    "acl_mask: conn %d  check a_perrname_path: %s\n",
658                                    conn->c_connid, b->a_peername_pat ));
659 #else
660                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
661                                 b->a_peername_pat, 0, 0 );
662 #endif
663                         if ( ber_strccmp( b->a_peername_pat, '*' ) != 0) {
664                                 if ( b->a_peername_style == ACL_STYLE_REGEX) {
665                                         if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
666                                                         e->e_ndn, matches ) ) 
667                                         {
668                                                 continue;
669                                         }
670                                 } else {
671                                         if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) != 0 )
672                                                 continue;
673                                 }
674                         }
675                 }
676
677                 if ( b->a_sockname_pat != NULL ) {
678 #ifdef NEW_LOGGING
679                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
680                                    "acl_mask: conn %d  check a_sockname_path: %s\n",
681                                    conn->c_connid, b->a_sockname_pat ));
682 #else
683                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
684                                 b->a_sockname_pat, 0, 0 );
685 #endif
686                         if ( ber_strccmp( b->a_sockname_pat, '*' ) != 0) {
687                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
688                                         if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
689                                                         e->e_ndn, matches ) ) 
690                                         {
691                                                 continue;
692                                         }
693                                 } else {
694                                         if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) != 0 )
695                                                 continue;
696                                 }
697                         }
698                 }
699
700                 if ( b->a_dn_at != NULL && op->o_ndn.bv_len != 0 ) {
701                         Attribute       *at;
702                         struct berval   bv;
703                         int rc, match = 0;
704                         const char *text;
705                         const char *attr = b->a_dn_at->ad_cname.bv_val;
706
707                         assert( attr != NULL );
708
709 #ifdef NEW_LOGGING
710                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
711                                    "acl_mask: conn %d  check a_dn_pat: %s\n",
712                                    conn->c_connid, attr ));
713 #else
714                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
715                                 attr, 0, 0);
716 #endif
717                         bv = op->o_ndn;
718
719                         /* see if asker is listed in dnattr */
720                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
721                                 at != NULL;
722                                 at = attrs_find( at->a_next, b->a_dn_at ) )
723                         {
724                                 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
725                                         /* found it */
726                                         match = 1;
727                                         break;
728                                 }
729                         }
730
731                         if( match ) {
732                                 /* have a dnattr match. if this is a self clause then
733                                  * the target must also match the op dn.
734                                  */
735                                 if ( b->a_dn_self ) {
736                                         /* check if the target is an attribute. */
737                                         if ( val == NULL )
738                                                 continue;
739                                         /* target is attribute, check if the attribute value
740                                          * is the op dn.
741                                          */
742                                         rc = value_match( &match, b->a_dn_at,
743                                                 b->a_dn_at->ad_type->sat_equality, 0,
744                                                 val, &bv, &text );
745                                         /* on match error or no match, fail the ACL clause */
746                                         if (rc != LDAP_SUCCESS || match != 0 )
747                                                 continue;
748                                 }
749                         } else {
750                                 /* no dnattr match, check if this is a self clause */
751                                 if ( ! b->a_dn_self )
752                                         continue;
753                                 /* this is a self clause, check if the target is an
754                                  * attribute.
755                                  */
756                                 if ( val == NULL )
757                                         continue;
758                                 /* target is attribute, check if the attribute value
759                                  * is the op dn.
760                                  */
761                                 rc = value_match( &match, b->a_dn_at,
762                                         b->a_dn_at->ad_type->sat_equality, 0,
763                                         val, &bv, &text );
764
765                                 /* on match error or no match, fail the ACL clause */
766                                 if (rc != LDAP_SUCCESS || match != 0 )
767                                         continue;
768                         }
769                 }
770
771                 if ( b->a_group_pat.bv_len && op->o_ndn.bv_len ) {
772                         char buf[1024];
773                         struct berval bv = { sizeof(buf) - 1, buf };
774                         struct berval ndn = { 0, NULL };
775                         int rc;
776
777                         /* b->a_group is an unexpanded entry name, expanded it should be an 
778                          * entry with objectclass group* and we test to see if odn is one of
779                          * the values in the attribute group
780                          */
781                         /* see if asker is listed in dnattr */
782                         if ( b->a_group_style == ACL_STYLE_REGEX ) {
783                                 string_expand(&bv, b->a_group_pat.bv_val, e->e_ndn, matches);
784                                 if ( dnNormalize2(NULL, &bv, &ndn) != LDAP_SUCCESS ) {
785                                         /* did not expand to a valid dn */
786                                         continue;
787                                 }
788                                 bv = ndn;
789                         } else {
790                                 bv = b->a_group_pat;
791                         }
792
793                         rc = backend_group(be, conn, op, e, &bv, &op->o_ndn,
794                                 b->a_group_oc, b->a_group_at);
795                         if ( ndn.bv_val )
796                                 free( ndn.bv_val );
797                         if ( rc != 0 ) {
798                                 continue;
799                         }
800                 }
801
802                 if ( b->a_set_pat.bv_len != 0 ) {
803                         if (aci_match_set( &b->a_set_pat, be, e, conn, op, 0 ) == 0) {
804                                 continue;
805                         }
806                 }
807
808                 if ( b->a_authz.sai_ssf ) {
809 #ifdef NEW_LOGGING
810                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
811                                    "acl_mask: conn %d  check a_authz.sai_ssf: ACL %u > OP %u\n",
812                                    conn->c_connid, b->a_authz.sai_ssf, op->o_ssf ));
813 #else
814                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
815                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
816 #endif
817                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
818                                 continue;
819                         }
820                 }
821
822                 if ( b->a_authz.sai_transport_ssf ) {
823 #ifdef NEW_LOGGING
824                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
825                                    "acl_mask: conn %d  check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
826                                    conn->c_connid, b->a_authz.sai_transport_ssf, op->o_transport_ssf ));
827 #else
828                         Debug( LDAP_DEBUG_ACL,
829                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
830                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
831 #endif
832                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
833                                 continue;
834                         }
835                 }
836
837                 if ( b->a_authz.sai_tls_ssf ) {
838 #ifdef NEW_LOGGING
839                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
840                                    "acl_mask: conn %d  check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
841                                    conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf ));
842 #else
843                         Debug( LDAP_DEBUG_ACL,
844                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
845                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
846 #endif
847                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
848                                 continue;
849                         }
850                 }
851
852                 if ( b->a_authz.sai_sasl_ssf ) {
853 #ifdef NEW_LOGGING
854                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
855                                    "acl_mask: conn %d check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
856                                    conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf ));
857 #else
858                         Debug( LDAP_DEBUG_ACL,
859                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
860                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
861 #endif
862                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
863                                 continue;
864                         }
865                 }
866
867 #ifdef SLAPD_ACI_ENABLED
868                 if ( b->a_aci_at != NULL ) {
869                         Attribute       *at;
870                         slap_access_t grant, deny, tgrant, tdeny;
871
872                         /* this case works different from the others above.
873                          * since aci's themselves give permissions, we need
874                          * to first check b->a_access_mask, the ACL's access level.
875                          */
876
877                         if ( e->e_nname.bv_len == 0 ) {
878                                 /* no ACIs in the root DSE */
879                                 continue;
880                         }
881
882                         /* first check if the right being requested
883                          * is allowed by the ACL clause.
884                          */
885                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
886                                 continue;
887                         }
888
889                         /* get the aci attribute */
890                         at = attr_find( e->e_attrs, b->a_aci_at );
891                         if ( at == NULL ) {
892                                 continue;
893                         }
894
895                         /* start out with nothing granted, nothing denied */
896                         ACL_INIT(tgrant);
897                         ACL_INIT(tdeny);
898
899                         /* the aci is an multi-valued attribute.  The
900                          * rights are determined by OR'ing the individual
901                          * rights given by the acis.
902                          */
903                         for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
904                                 if (aci_mask( be, conn, op,
905                                         e, desc, val, &at->a_vals[i],
906                                         matches, &grant, &deny ) != 0)
907                                 {
908                                         tgrant |= grant;
909                                         tdeny |= deny;
910                                 }
911                         }
912
913                         /* remove anything that the ACL clause does not allow */
914                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
915                         tdeny &= ACL_PRIV_MASK;
916
917                         /* see if we have anything to contribute */
918                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
919                                 continue;
920                         }
921
922                         /* this could be improved by changing acl_mask so that it can deal with
923                          * by clauses that return grant/deny pairs.  Right now, it does either
924                          * additive or subtractive rights, but not both at the same time.  So,
925                          * we need to combine the grant/deny pair into a single rights mask in
926                          * a smart way:  if either grant or deny is "empty", then we use the
927                          * opposite as is, otherwise we remove any denied rights from the grant
928                          * rights mask and construct an additive mask.
929                          */
930                         if (ACL_IS_INVALID(tdeny)) {
931                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
932
933                         } else if (ACL_IS_INVALID(tgrant)) {
934                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
935
936                         } else {
937                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
938                         }
939
940                 } else
941 #endif
942                 {
943                         modmask = b->a_access_mask;
944                 }
945
946 #ifdef NEW_LOGGING
947                 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
948                            "acl_mask: conn %d  [%d] applying %s (%s)\n",
949                            conn->c_connid, i, accessmask2str( modmask, accessmaskbuf),
950                            b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
951                            ? "break" : "stop" ));
952 #else
953                 Debug( LDAP_DEBUG_ACL,
954                         "<= acl_mask: [%d] applying %s (%s)\n",
955                         i, accessmask2str( modmask, accessmaskbuf ), 
956                         b->a_type == ACL_CONTINUE
957                                 ? "continue"
958                                 : b->a_type == ACL_BREAK
959                                         ? "break"
960                                         : "stop" );
961 #endif
962                 /* save old mask */
963                 oldmask = *mask;
964
965                 if( ACL_IS_ADDITIVE(modmask) ) {
966                         /* add privs */
967                         ACL_PRIV_SET( *mask, modmask );
968
969                         /* cleanup */
970                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
971
972                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
973                         /* substract privs */
974                         ACL_PRIV_CLR( *mask, modmask );
975
976                         /* cleanup */
977                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
978
979                 } else {
980                         /* assign privs */
981                         *mask = modmask;
982                 }
983
984 #ifdef NEW_LOGGING
985                 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
986                            "acl_mask: conn %d  [%d] mask: %s\n",
987                            conn->c_connid, i, accessmask2str( *mask, accessmaskbuf) ));
988 #else
989                 Debug( LDAP_DEBUG_ACL,
990                         "<= acl_mask: [%d] mask: %s\n",
991                         i, accessmask2str(*mask, accessmaskbuf), 0 );
992 #endif
993
994                 if( b->a_type == ACL_CONTINUE ) {
995                         continue;
996
997                 } else if ( b->a_type == ACL_BREAK ) {
998                         return ACL_BREAK;
999
1000                 } else {
1001                         return ACL_STOP;
1002                 }
1003         }
1004
1005         /* implicit "by * none" clause */
1006         ACL_INIT(*mask);
1007
1008 #ifdef NEW_LOGGING
1009         LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
1010                    "acl_mask: conn %d  no more <who> clauses, returning %d (stop)\n",
1011                    conn->c_connid, accessmask2str( *mask, accessmaskbuf) ));
1012 #else
1013         Debug( LDAP_DEBUG_ACL,
1014                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1015                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1016 #endif
1017         return ACL_STOP;
1018 }
1019
1020 /*
1021  * acl_check_modlist - check access control on the given entry to see if
1022  * it allows the given modifications by the user associated with op.
1023  * returns      1       if mods allowed ok
1024  *                      0       mods not allowed
1025  */
1026
1027 int
1028 acl_check_modlist(
1029     Backend     *be,
1030     Connection  *conn,
1031     Operation   *op,
1032     Entry       *e,
1033     Modifications       *mlist
1034 )
1035 {
1036         struct berval *bv;
1037
1038         assert( be != NULL );
1039
1040         /* short circuit root database access */
1041         if ( be_isroot( be, &op->o_ndn ) ) {
1042 #ifdef NEW_LOGGING
1043                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
1044                            "acl_check_modlist: conn %d  access granted to root user\n",
1045                            conn->c_connid ));
1046 #else
1047                 Debug( LDAP_DEBUG_ACL,
1048                         "<= acl_access_allowed: granted to database root\n",
1049                     0, 0, 0 );
1050 #endif
1051                 return 1;
1052         }
1053
1054         /* use backend default access if no backend acls */
1055         if( be != NULL && be->be_acl == NULL ) {
1056 #ifdef NEW_LOGGING
1057                 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1058                            "acl_check_modlist: conn %d  backend default %s access %s to \"%s\"\n",
1059                            conn->c_connid, access2str( ACL_WRITE ),
1060                            be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val ));
1061 #else
1062                 Debug( LDAP_DEBUG_ACL,
1063                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1064                         access2str( ACL_WRITE ),
1065                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1066 #endif
1067                 return be->be_dfltaccess >= ACL_WRITE;
1068
1069 #ifdef notdef
1070         /* be is always non-NULL */
1071         /* use global default access if no global acls */
1072         } else if ( be == NULL && global_acl == NULL ) {
1073 #ifdef NEW_LOGGING
1074                 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1075                            "acl_check_modlist: conn %d  global default %s access %s to \"%s\"\n",
1076                            conn->c_connid, access2str( ACL_WRITE ),
1077                            global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1078 #else
1079                 Debug( LDAP_DEBUG_ACL,
1080                         "=> access_allowed: global default %s access %s to \"%s\"\n",
1081                         access2str( ACL_WRITE ),
1082                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1083 #endif
1084                 return global_default_access >= ACL_WRITE;
1085 #endif
1086         }
1087
1088         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1089                 /*
1090                  * no-user-modification operational attributes are ignored
1091                  * by ACL_WRITE checking as any found here are not provided
1092                  * by the user
1093                  */
1094                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1095 #ifdef NEW_LOGGING
1096                         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1097                                    "acl_check_modlist: conn %d  no-user-mod %s: modify access granted\n",
1098                                    conn->c_connid, mlist->sml_desc->ad_cname.bv_val ));
1099 #else
1100                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1101                                 " modify access granted\n",
1102                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1103 #endif
1104                         continue;
1105                 }
1106
1107                 switch ( mlist->sml_op ) {
1108                 case LDAP_MOD_REPLACE:
1109                         /*
1110                          * We must check both permission to delete the whole
1111                          * attribute and permission to add the specific attributes.
1112                          * This prevents abuse from selfwriters.
1113                          */
1114                         if ( ! access_allowed( be, conn, op, e,
1115                                 mlist->sml_desc, NULL, ACL_WRITE ) )
1116                         {
1117                                 return( 0 );
1118                         }
1119
1120                         if ( mlist->sml_bvalues == NULL ) break;
1121
1122                         /* fall thru to check value to add */
1123
1124                 case LDAP_MOD_ADD:
1125                         assert( mlist->sml_bvalues != NULL );
1126
1127                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1128                                 if ( ! access_allowed( be, conn, op, e,
1129                                         mlist->sml_desc, bv, ACL_WRITE ) )
1130                                 {
1131                                         return( 0 );
1132                                 }
1133                         }
1134                         break;
1135
1136                 case LDAP_MOD_DELETE:
1137                         if ( mlist->sml_bvalues == NULL ) {
1138                                 if ( ! access_allowed( be, conn, op, e,
1139                                         mlist->sml_desc, NULL, ACL_WRITE ) )
1140                                 {
1141                                         return( 0 );
1142                                 }
1143                                 break;
1144                         }
1145                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1146                                 if ( ! access_allowed( be, conn, op, e,
1147                                         mlist->sml_desc, bv, ACL_WRITE ) )
1148                                 {
1149                                         return( 0 );
1150                                 }
1151                         }
1152                         break;
1153
1154                 default:
1155                         assert( 0 );
1156                         return( 0 );
1157                 }
1158         }
1159
1160         return( 1 );
1161 }
1162
1163 static char *
1164 aci_bvstrdup( struct berval *bv )
1165 {
1166         char *s;
1167
1168         s = (char *)ch_malloc(bv->bv_len + 1);
1169         if (s != NULL) {
1170                 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
1171                 s[bv->bv_len] = 0;
1172         }
1173         return(s);
1174 }
1175
1176 #ifdef SLAPD_ACI_ENABLED
1177 static int
1178 aci_strbvcmp(
1179         const char *s,
1180         struct berval *bv )
1181 {
1182         int res, len;
1183
1184         res = strncasecmp( s, bv->bv_val, bv->bv_len );
1185         if (res)
1186                 return(res);
1187         len = strlen(s);
1188         if (len > (int)bv->bv_len)
1189                 return(1);
1190         if (len < (int)bv->bv_len)
1191                 return(-1);
1192         return(0);
1193 }
1194 #endif
1195
1196 static int
1197 aci_get_part(
1198         struct berval *list,
1199         int ix,
1200         char sep,
1201         struct berval *bv )
1202 {
1203         int len;
1204         char *p;
1205
1206         if (bv) {
1207                 bv->bv_len = 0;
1208                 bv->bv_val = NULL;
1209         }
1210         len = list->bv_len;
1211         p = list->bv_val;
1212         while (len >= 0 && --ix >= 0) {
1213                 while (--len >= 0 && *p++ != sep) ;
1214         }
1215         while (len >= 0 && *p == ' ') {
1216                 len--;
1217                 p++;
1218         }
1219         if (len < 0)
1220                 return(-1);
1221
1222         if (!bv)
1223                 return(0);
1224
1225         bv->bv_val = p;
1226         while (--len >= 0 && *p != sep) {
1227                 bv->bv_len++;
1228                 p++;
1229         }
1230         while (bv->bv_len > 0 && *--p == ' ')
1231                 bv->bv_len--;
1232         return(bv->bv_len);
1233 }
1234
1235 BerVarray
1236 aci_set_gather (void *cookie, char *name, struct berval *attr)
1237 {
1238         AciSetCookie *cp = cookie;
1239         BerVarray bvals = NULL;
1240         struct berval bv, ndn;
1241
1242         /* this routine needs to return the bervals instead of
1243          * plain strings, since syntax is not known.  It should
1244          * also return the syntax or some "comparison cookie".
1245          */
1246
1247         bv.bv_val = name;
1248         bv.bv_len = strlen( name );
1249         if (dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS) {
1250                 const char *text;
1251                 AttributeDescription *desc = NULL;
1252                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1253                         backend_attribute(cp->be, NULL, NULL,
1254                                 cp->e, &ndn, desc, &bvals);
1255                 }
1256                 free(ndn.bv_val);
1257         }
1258         return(bvals);
1259 }
1260
1261 static int
1262 aci_match_set (
1263         struct berval *subj,
1264     Backend *be,
1265     Entry *e,
1266     Connection *conn,
1267     Operation *op,
1268     int setref
1269 )
1270 {
1271         struct berval set = { 0, NULL };
1272         int rc = 0;
1273         AciSetCookie cookie;
1274
1275         if (setref == 0) {
1276                 ber_dupbv( &set, subj );
1277         } else {
1278                 struct berval subjdn, ndn = { 0, NULL };
1279                 struct berval setat;
1280                 BerVarray bvals;
1281                 const char *text;
1282                 AttributeDescription *desc = NULL;
1283
1284                 /* format of string is "entry/setAttrName" */
1285                 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1286                         return(0);
1287                 } else {
1288                         /* FIXME: If dnNormalize was based on ldap_bv2dn
1289                          * instead of ldap_str2dn and would honor the bv_len
1290                          * we could skip this step and not worry about the
1291                          * unterminated string.
1292                          */
1293                         char *s = ch_malloc(subjdn.bv_len + 1);
1294                         AC_MEMCPY(s, subjdn.bv_val, subjdn.bv_len);
1295                         subjdn.bv_val = s;
1296                 }
1297
1298                 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1299                         setat.bv_val = SLAPD_ACI_SET_ATTR;
1300                         setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1301                 }
1302                 if ( setat.bv_val != NULL ) {
1303                         if ( dnNormalize2(NULL, &subjdn, &ndn) == LDAP_SUCCESS
1304                                 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1305                         {
1306                                 backend_attribute(be, NULL, NULL, e,
1307                                         &ndn, desc, &bvals);
1308                                 if ( bvals != NULL ) {
1309                                         if ( bvals[0].bv_val != NULL ) {
1310                                                 int i;
1311                                                 set = bvals[0];
1312                                                 bvals[0].bv_val = NULL;
1313                                                 for (i=1;bvals[i].bv_val;i++);
1314                                                 bvals[0].bv_val = bvals[i-1].bv_val;
1315                                                 bvals[i-1].bv_val = NULL;
1316                                         }
1317                                         ber_bvarray_free(bvals);
1318                                 }
1319                         }
1320                         if (ndn.bv_val)
1321                                 free(ndn.bv_val);
1322                 }
1323                 ch_free(subjdn.bv_val);
1324         }
1325
1326         if (set.bv_val != NULL) {
1327                 cookie.be = be;
1328                 cookie.e = e;
1329                 cookie.conn = conn;
1330                 cookie.op = op;
1331                 rc = (slap_set_filter(aci_set_gather, &cookie, &set,
1332                         op->o_ndn.bv_val, e->e_ndn, NULL) > 0);
1333                 ch_free(set.bv_val);
1334         }
1335         return(rc);
1336 }
1337
1338 #ifdef SLAPD_ACI_ENABLED
1339 static int
1340 aci_list_map_rights(
1341         struct berval *list )
1342 {
1343         struct berval bv;
1344         slap_access_t mask;
1345         int i;
1346
1347         ACL_INIT(mask);
1348         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1349                 if (bv.bv_len <= 0)
1350                         continue;
1351                 switch (*bv.bv_val) {
1352                 case 'c':
1353                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1354                         break;
1355                 case 's':
1356                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1357                          * the right 's' to mean "set", but in the examples states
1358                          * that the right 's' means "search".  The latter definition
1359                          * is used here.
1360                          */
1361                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1362                         break;
1363                 case 'r':
1364                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1365                         break;
1366                 case 'w':
1367                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1368                         break;
1369                 case 'x':
1370                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1371                          * define any equivalent to the AUTH right, so I've just used
1372                          * 'x' for now.
1373                          */
1374                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1375                         break;
1376                 default:
1377                         break;
1378                 }
1379
1380         }
1381         return(mask);
1382 }
1383
1384 static int
1385 aci_list_has_attr(
1386         struct berval *list,
1387         const struct berval *attr,
1388         struct berval *val )
1389 {
1390         struct berval bv, left, right;
1391         int i;
1392
1393         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1394                 if (aci_get_part(&bv, 0, '=', &left) < 0
1395                         || aci_get_part(&bv, 1, '=', &right) < 0)
1396                 {
1397                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1398                                 return(1);
1399                 } else if (val == NULL) {
1400                         if (ber_bvstrcasecmp(attr, &left) == 0)
1401                                 return(1);
1402                 } else {
1403                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1404                                 /* this is experimental code that implements a
1405                                  * simple (prefix) match of the attribute value.
1406                                  * the ACI draft does not provide for aci's that
1407                                  * apply to specific values, but it would be
1408                                  * nice to have.  If the <attr> part of an aci's
1409                                  * rights list is of the form <attr>=<value>,
1410                                  * that means the aci applies only to attrs with
1411                                  * the given value.  Furthermore, if the attr is
1412                                  * of the form <attr>=<value>*, then <value> is
1413                                  * treated as a prefix, and the aci applies to 
1414                                  * any value with that prefix.
1415                                  *
1416                                  * Ideally, this would allow r.e. matches.
1417                                  */
1418                                 if (aci_get_part(&right, 0, '*', &left) < 0
1419                                         || right.bv_len <= left.bv_len)
1420                                 {
1421                                         if (ber_bvstrcasecmp(val, &right) == 0)
1422                                                 return(1);
1423                                 } else if (val->bv_len >= left.bv_len) {
1424                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1425                                                 return(1);
1426                                 }
1427                         }
1428                 }
1429         }
1430         return(0);
1431 }
1432
1433 static slap_access_t
1434 aci_list_get_attr_rights(
1435         struct berval *list,
1436         const struct berval *attr,
1437         struct berval *val )
1438 {
1439     struct berval bv;
1440     slap_access_t mask;
1441     int i;
1442
1443         /* loop through each rights/attr pair, skip first part (action) */
1444         ACL_INIT(mask);
1445         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1446                 if (aci_list_has_attr(&bv, attr, val) == 0)
1447                         continue;
1448                 if (aci_get_part(list, i, ';', &bv) < 0)
1449                         continue;
1450                 mask |= aci_list_map_rights(&bv);
1451         }
1452         return(mask);
1453 }
1454
1455 static int
1456 aci_list_get_rights(
1457         struct berval *list,
1458         const struct berval *attr,
1459         struct berval *val,
1460         slap_access_t *grant,
1461         slap_access_t *deny )
1462 {
1463     struct berval perm, actn;
1464     slap_access_t *mask;
1465     int i, found;
1466
1467         if (attr == NULL || attr->bv_len == 0 
1468                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1469                 attr = &aci_bv_br_entry;
1470         }
1471
1472         found = 0;
1473         ACL_INIT(*grant);
1474         ACL_INIT(*deny);
1475         /* loop through each permissions clause */
1476         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1477                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1478                         continue;
1479                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1480                         mask = grant;
1481                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1482                         mask = deny;
1483                 } else {
1484                         continue;
1485                 }
1486
1487                 found = 1;
1488                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1489                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1490         }
1491         return(found);
1492 }
1493
1494 static int
1495 aci_group_member (
1496         struct berval *subj,
1497         struct berval *defgrpoc,
1498         struct berval *defgrpat,
1499     Backend             *be,
1500     Entry               *e,
1501     Connection          *conn,
1502     Operation           *op,
1503         regmatch_t      *matches
1504 )
1505 {
1506         struct berval bv;
1507         char *subjdn;
1508         struct berval grpoc;
1509         struct berval grpat;
1510         ObjectClass *grp_oc = NULL;
1511         AttributeDescription *grp_ad = NULL;
1512         const char *text;
1513         int rc;
1514
1515         /* format of string is "group/objectClassValue/groupAttrName" */
1516         if (aci_get_part(subj, 0, '/', &bv) < 0) {
1517                 return(0);
1518         }
1519
1520         subjdn = aci_bvstrdup(&bv);
1521         if (subjdn == NULL) {
1522                 return(0);
1523         }
1524
1525         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1526                 grpoc = *defgrpoc;
1527         }
1528
1529         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1530                 grpat = *defgrpat;
1531         }
1532
1533         rc = slap_bv2ad( &grpat, &grp_ad, &text );
1534         if( rc != LDAP_SUCCESS ) {
1535                 rc = 0;
1536                 goto done;
1537         }
1538         rc = 0;
1539
1540         grp_oc = oc_bvfind( &grpoc );
1541
1542         if (grp_oc != NULL && grp_ad != NULL ) {
1543                 struct berval ndn;
1544                 bv.bv_val = (char *)ch_malloc(1024);
1545                 bv.bv_len = 1024;
1546                 string_expand(&bv, subjdn, e->e_ndn, matches);
1547                 if ( dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS ) {
1548                         rc = (backend_group(be, conn, op, e, &ndn, &op->o_ndn, grp_oc, grp_ad) == 0);
1549                         free( ndn.bv_val );
1550                 }
1551                 ch_free(bv.bv_val);
1552         }
1553
1554 done:
1555         ch_free(subjdn);
1556         return(rc);
1557 }
1558
1559 static struct berval GroupClass = {
1560         sizeof(SLAPD_GROUP_CLASS)-1, SLAPD_GROUP_CLASS };
1561 static struct berval GroupAttr = {
1562         sizeof(SLAPD_GROUP_ATTR)-1, SLAPD_GROUP_ATTR };
1563 static struct berval RoleClass = {
1564         sizeof(SLAPD_ROLE_CLASS)-1, SLAPD_ROLE_CLASS };
1565 static struct berval RoleAttr = {
1566         sizeof(SLAPD_ROLE_ATTR)-1, SLAPD_ROLE_ATTR };
1567
1568 static int
1569 aci_mask(
1570     Backend                     *be,
1571     Connection          *conn,
1572     Operation           *op,
1573     Entry                       *e,
1574         AttributeDescription *desc,
1575     struct berval       *val,
1576     struct berval       *aci,
1577         regmatch_t              *matches,
1578         slap_access_t   *grant,
1579         slap_access_t   *deny
1580 )
1581 {
1582     struct berval bv, perms, sdn;
1583         int rc;
1584                 
1585
1586         assert( desc->ad_cname.bv_val != NULL );
1587
1588         /* parse an aci of the form:
1589                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1590
1591            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1592            a full description of the format for this attribute.
1593
1594            For now, this routine only supports scope=entry.
1595          */
1596
1597         /* check that the aci has all 5 components */
1598         if (aci_get_part(aci, 4, '#', NULL) < 0)
1599                 return(0);
1600
1601         /* check that the aci family is supported */
1602         if (aci_get_part(aci, 0, '#', &bv) < 0)
1603                 return(0);
1604
1605         /* check that the scope is "entry" */
1606         if (aci_get_part(aci, 1, '#', &bv) < 0
1607                 || ber_bvstrcasecmp( &aci_bv_entry, &bv ) != 0)
1608         {
1609                 return(0);
1610         }
1611
1612         /* get the list of permissions clauses, bail if empty */
1613         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1614                 return(0);
1615
1616         /* check if any permissions allow desired access */
1617         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1618                 return(0);
1619
1620         /* see if we have a DN match */
1621         if (aci_get_part(aci, 3, '#', &bv) < 0)
1622                 return(0);
1623
1624         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1625                 return(0);
1626
1627         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1628                 struct berval ndn;
1629                 rc = 1;
1630                 if ( dnNormalize2(NULL, &sdn, &ndn) == LDAP_SUCCESS ) {
1631                         if (!dn_match( &op->o_ndn, &ndn))
1632                                 rc = 0;
1633                         free(ndn.bv_val);
1634                 }
1635                 return(rc);
1636         }
1637
1638         if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1639                 if (dn_match(&op->o_ndn, &e->e_nname))
1640                         return(1);
1641
1642         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1643                 Attribute *at;
1644                 AttributeDescription *ad = NULL;
1645                 const char *text;
1646
1647                 rc = slap_bv2ad( &sdn, &ad, &text );
1648
1649                 if( rc != LDAP_SUCCESS ) {
1650                         return 0;
1651                 }
1652
1653                 rc = 0;
1654
1655                 bv = op->o_ndn;
1656
1657                 for(at = attrs_find( e->e_attrs, ad );
1658                         at != NULL;
1659                         at = attrs_find( at->a_next, ad ) )
1660                 {
1661                         if (value_find( ad, at->a_vals, &bv) == 0 ) {
1662                                 rc = 1;
1663                                 break;
1664                         }
1665                 }
1666
1667                 return rc;
1668
1669
1670         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
1671                 if (aci_group_member(&sdn, &GroupClass, &GroupAttr, be, e, conn, op, matches))
1672                         return(1);
1673
1674         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
1675                 if (aci_group_member(&sdn, &RoleClass, &RoleAttr, be, e, conn, op, matches))
1676                         return(1);
1677
1678         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
1679                 if (aci_match_set(&sdn, be, e, conn, op, 0))
1680                         return(1);
1681
1682         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
1683                 if (aci_match_set(&sdn, be, e, conn, op, 1))
1684                         return(1);
1685
1686         }
1687
1688         return(0);
1689 }
1690
1691 #endif  /* SLAPD_ACI_ENABLED */
1692
1693 static void
1694 string_expand(
1695         struct berval *bv,
1696         char *pat,
1697         char *match,
1698         regmatch_t *matches)
1699 {
1700         ber_len_t       size;
1701         char   *sp;
1702         char   *dp;
1703         int     flag;
1704
1705         size = 0;
1706         bv->bv_val[0] = '\0';
1707         bv->bv_len--; /* leave space for lone $ */
1708
1709         flag = 0;
1710         for ( dp = bv->bv_val, sp = pat; size < bv->bv_len && *sp ; sp++) {
1711                 /* did we previously see a $ */
1712                 if (flag) {
1713                         if (*sp == '$') {
1714                                 *dp++ = '$';
1715                                 size++;
1716                         } else if (*sp >= '0' && *sp <= '9' ) {
1717                                 int     n;
1718                                 int     i;
1719                                 int     l;
1720
1721                                 n = *sp - '0';
1722                                 *dp = '\0';
1723                                 i = matches[n].rm_so;
1724                                 l = matches[n].rm_eo; 
1725                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
1726                                         *dp++ = match[i];
1727                                         size++;
1728                                 }
1729                                 *dp = '\0';
1730                         }
1731                         flag = 0;
1732                 } else {
1733                         if (*sp == '$') {
1734                                 flag = 1;
1735                         } else {
1736                                 *dp++ = *sp;
1737                                 size++;
1738                         }
1739                 }
1740         }
1741
1742         if (flag) {
1743                 /* must have ended with a single $ */
1744                 *dp++ = '$';
1745                 size++;
1746         }
1747
1748         *dp = '\0';
1749         bv->bv_len = size;
1750
1751 #ifdef NEW_LOGGING
1752         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1753                    "string_expand:  pattern = %s\n", pat ));
1754         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1755                    "string_expand:  expanded = %s\n", bv->bv_val ));
1756 #else
1757         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %s\n", pat, 0, 0 );
1758         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
1759 #endif
1760 }
1761
1762 static int
1763 regex_matches(
1764         char *pat,                              /* pattern to expand and match against */
1765         char *str,                              /* string to match against pattern */
1766         char *buf,                              /* buffer with $N expansion variables */
1767         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1768 )
1769 {
1770         regex_t re;
1771         char newbuf[512];
1772         struct berval bv = {sizeof(newbuf), newbuf};
1773         int     rc;
1774
1775         if(str == NULL) str = "";
1776
1777         string_expand(&bv, pat, buf, matches);
1778         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1779                 char error[512];
1780                 regerror(rc, &re, error, sizeof(error));
1781
1782 #ifdef NEW_LOGGING
1783                 LDAP_LOG(( "aci", LDAP_LEVEL_ERR,
1784                            "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1785                            pat, str, error ));
1786 #else
1787                 Debug( LDAP_DEBUG_TRACE,
1788                     "compile( \"%s\", \"%s\") failed %s\n",
1789                         pat, str, error );
1790 #endif
1791                 return( 0 );
1792         }
1793
1794         rc = regexec(&re, str, 0, NULL, 0);
1795         regfree( &re );
1796
1797 #ifdef NEW_LOGGING
1798         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1799                    "regex_matches: string:   %s\n", str ));
1800         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1801                    "regex_matches: rc:  %d  %s\n",
1802                    rc, rc ? "matches" : "no matches" ));
1803 #else
1804         Debug( LDAP_DEBUG_TRACE,
1805             "=> regex_matches: string:   %s\n", str, 0, 0 );
1806         Debug( LDAP_DEBUG_TRACE,
1807             "=> regex_matches: rc: %d %s\n",
1808                 rc, !rc ? "matches" : "no matches", 0 );
1809 #endif
1810         return( !rc );
1811 }
1812