]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
3df2dd09635499ce97bb3ff31e6c6165c4b3c9ba
[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_bvstrcmp( &b->a_dn_pat, &aci_bv_anonymous ) == 0 ) {
536                                 if ( op->o_ndn.bv_len != 0 ) {
537                                         continue;
538                                 }
539
540                         } else if ( ber_bvstrcmp( &b->a_dn_pat, &aci_bv_users ) == 0 ) {
541                                 if ( op->o_ndn.bv_len == 0 ) {
542                                         continue;
543                                 }
544
545                         } else if ( ber_bvstrcmp( &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 ( strcmp( 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 ( strcmp( 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 ( strcmp( 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 ( strcmp( 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                         {
799                                 continue;
800                         }
801                 }
802
803                 if ( b->a_set_pat.bv_len != 0 ) {
804                         if (aci_match_set( &b->a_set_pat, be, e, conn, op, 0 ) == 0) {
805                                 continue;
806                         }
807                 }
808
809                 if ( b->a_authz.sai_ssf ) {
810 #ifdef NEW_LOGGING
811                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
812                                    "acl_mask: conn %d  check a_authz.sai_ssf: ACL %u > OP %u\n",
813                                    conn->c_connid, b->a_authz.sai_ssf, op->o_ssf ));
814 #else
815                         Debug( LDAP_DEBUG_ACL, "<= check a_authz.sai_ssf: ACL %u > OP %u\n",
816                                 b->a_authz.sai_ssf, op->o_ssf, 0 );
817 #endif
818                         if ( b->a_authz.sai_ssf >  op->o_ssf ) {
819                                 continue;
820                         }
821                 }
822
823                 if ( b->a_authz.sai_transport_ssf ) {
824 #ifdef NEW_LOGGING
825                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
826                                    "acl_mask: conn %d  check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
827                                    conn->c_connid, b->a_authz.sai_transport_ssf, op->o_transport_ssf ));
828 #else
829                         Debug( LDAP_DEBUG_ACL,
830                                 "<= check a_authz.sai_transport_ssf: ACL %u > OP %u\n",
831                                 b->a_authz.sai_transport_ssf, op->o_transport_ssf, 0 );
832 #endif
833                         if ( b->a_authz.sai_transport_ssf >  op->o_transport_ssf ) {
834                                 continue;
835                         }
836                 }
837
838                 if ( b->a_authz.sai_tls_ssf ) {
839 #ifdef NEW_LOGGING
840                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
841                                    "acl_mask: conn %d  check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
842                                    conn->c_connid, b->a_authz.sai_tls_ssf, op->o_tls_ssf ));
843 #else
844                         Debug( LDAP_DEBUG_ACL,
845                                 "<= check a_authz.sai_tls_ssf: ACL %u > OP %u\n",
846                                 b->a_authz.sai_tls_ssf, op->o_tls_ssf, 0 );
847 #endif
848                         if ( b->a_authz.sai_tls_ssf >  op->o_tls_ssf ) {
849                                 continue;
850                         }
851                 }
852
853                 if ( b->a_authz.sai_sasl_ssf ) {
854 #ifdef NEW_LOGGING
855                         LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
856                                    "acl_mask: conn %d check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
857                                    conn->c_connid, b->a_authz.sai_sasl_ssf, op->o_sasl_ssf ));
858 #else
859                         Debug( LDAP_DEBUG_ACL,
860                                 "<= check a_authz.sai_sasl_ssf: ACL %u > OP %u\n",
861                                 b->a_authz.sai_sasl_ssf, op->o_sasl_ssf, 0 );
862 #endif
863                         if ( b->a_authz.sai_sasl_ssf >  op->o_sasl_ssf ) {
864                                 continue;
865                         }
866                 }
867
868 #ifdef SLAPD_ACI_ENABLED
869                 if ( b->a_aci_at != NULL ) {
870                         Attribute       *at;
871                         slap_access_t grant, deny, tgrant, tdeny;
872
873                         /* this case works different from the others above.
874                          * since aci's themselves give permissions, we need
875                          * to first check b->a_access_mask, the ACL's access level.
876                          */
877
878                         if ( e->e_nname.bv_len == 0 ) {
879                                 /* no ACIs in the root DSE */
880                                 continue;
881                         }
882
883                         /* first check if the right being requested
884                          * is allowed by the ACL clause.
885                          */
886                         if ( ! ACL_GRANT( b->a_access_mask, *mask ) ) {
887                                 continue;
888                         }
889
890                         /* get the aci attribute */
891                         at = attr_find( e->e_attrs, b->a_aci_at );
892                         if ( at == NULL ) {
893                                 continue;
894                         }
895
896                         /* start out with nothing granted, nothing denied */
897                         ACL_INIT(tgrant);
898                         ACL_INIT(tdeny);
899
900                         /* the aci is an multi-valued attribute.  The
901                          * rights are determined by OR'ing the individual
902                          * rights given by the acis.
903                          */
904                         for ( i = 0; at->a_vals[i].bv_val != NULL; i++ ) {
905                                 if (aci_mask( be, conn, op,
906                                         e, desc, val, &at->a_vals[i],
907                                         matches, &grant, &deny ) != 0)
908                                 {
909                                         tgrant |= grant;
910                                         tdeny |= deny;
911                                 }
912                         }
913
914                         /* remove anything that the ACL clause does not allow */
915                         tgrant &= b->a_access_mask & ACL_PRIV_MASK;
916                         tdeny &= ACL_PRIV_MASK;
917
918                         /* see if we have anything to contribute */
919                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
920                                 continue;
921                         }
922
923                         /* this could be improved by changing acl_mask so that it can deal with
924                          * by clauses that return grant/deny pairs.  Right now, it does either
925                          * additive or subtractive rights, but not both at the same time.  So,
926                          * we need to combine the grant/deny pair into a single rights mask in
927                          * a smart way:  if either grant or deny is "empty", then we use the
928                          * opposite as is, otherwise we remove any denied rights from the grant
929                          * rights mask and construct an additive mask.
930                          */
931                         if (ACL_IS_INVALID(tdeny)) {
932                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
933
934                         } else if (ACL_IS_INVALID(tgrant)) {
935                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
936
937                         } else {
938                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
939                         }
940
941                 } else
942 #endif
943                 {
944                         modmask = b->a_access_mask;
945                 }
946
947 #ifdef NEW_LOGGING
948                 LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
949                            "acl_mask: conn %d  [%d] applying %s (%s)\n",
950                            conn->c_connid, i, accessmask2str( modmask, accessmaskbuf),
951                            b->a_type == ACL_CONTINUE ? "continue" : b->a_type == ACL_BREAK
952                            ? "break" : "stop" ));
953 #else
954                 Debug( LDAP_DEBUG_ACL,
955                         "<= acl_mask: [%d] applying %s (%s)\n",
956                         i, accessmask2str( modmask, accessmaskbuf ), 
957                         b->a_type == ACL_CONTINUE
958                                 ? "continue"
959                                 : b->a_type == ACL_BREAK
960                                         ? "break"
961                                         : "stop" );
962 #endif
963                 /* save old mask */
964                 oldmask = *mask;
965
966                 if( ACL_IS_ADDITIVE(modmask) ) {
967                         /* add privs */
968                         ACL_PRIV_SET( *mask, modmask );
969
970                         /* cleanup */
971                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
972
973                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
974                         /* substract privs */
975                         ACL_PRIV_CLR( *mask, modmask );
976
977                         /* cleanup */
978                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
979
980                 } else {
981                         /* assign privs */
982                         *mask = modmask;
983                 }
984
985 #ifdef NEW_LOGGING
986                 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
987                            "acl_mask: conn %d  [%d] mask: %s\n",
988                            conn->c_connid, i, accessmask2str( *mask, accessmaskbuf) ));
989 #else
990                 Debug( LDAP_DEBUG_ACL,
991                         "<= acl_mask: [%d] mask: %s\n",
992                         i, accessmask2str(*mask, accessmaskbuf), 0 );
993 #endif
994
995                 if( b->a_type == ACL_CONTINUE ) {
996                         continue;
997
998                 } else if ( b->a_type == ACL_BREAK ) {
999                         return ACL_BREAK;
1000
1001                 } else {
1002                         return ACL_STOP;
1003                 }
1004         }
1005
1006         /* implicit "by * none" clause */
1007         ACL_INIT(*mask);
1008
1009 #ifdef NEW_LOGGING
1010         LDAP_LOG(( "acl", LDAP_LEVEL_RESULTS,
1011                    "acl_mask: conn %d  no more <who> clauses, returning %d (stop)\n",
1012                    conn->c_connid, accessmask2str( *mask, accessmaskbuf) ));
1013 #else
1014         Debug( LDAP_DEBUG_ACL,
1015                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
1016                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
1017 #endif
1018         return ACL_STOP;
1019 }
1020
1021 /*
1022  * acl_check_modlist - check access control on the given entry to see if
1023  * it allows the given modifications by the user associated with op.
1024  * returns      1       if mods allowed ok
1025  *                      0       mods not allowed
1026  */
1027
1028 int
1029 acl_check_modlist(
1030     Backend     *be,
1031     Connection  *conn,
1032     Operation   *op,
1033     Entry       *e,
1034     Modifications       *mlist
1035 )
1036 {
1037         struct berval *bv;
1038
1039         assert( be != NULL );
1040
1041         /* short circuit root database access */
1042         if ( be_isroot( be, &op->o_ndn ) ) {
1043 #ifdef NEW_LOGGING
1044                 LDAP_LOG(( "acl", LDAP_LEVEL_DETAIL1,
1045                            "acl_check_modlist: conn %d  access granted to root user\n",
1046                            conn->c_connid ));
1047 #else
1048                 Debug( LDAP_DEBUG_ACL,
1049                         "<= acl_access_allowed: granted to database root\n",
1050                     0, 0, 0 );
1051 #endif
1052                 return 1;
1053         }
1054
1055         /* use backend default access if no backend acls */
1056         if( be != NULL && be->be_acl == NULL ) {
1057 #ifdef NEW_LOGGING
1058                 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1059                            "acl_check_modlist: conn %d  backend default %s access %s to \"%s\"\n",
1060                            conn->c_connid, access2str( ACL_WRITE ),
1061                            be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val ));
1062 #else
1063                 Debug( LDAP_DEBUG_ACL,
1064                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
1065                         access2str( ACL_WRITE ),
1066                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn.bv_val );
1067 #endif
1068                 return be->be_dfltaccess >= ACL_WRITE;
1069
1070 #ifdef notdef
1071         /* be is always non-NULL */
1072         /* use global default access if no global acls */
1073         } else if ( be == NULL && global_acl == NULL ) {
1074 #ifdef NEW_LOGGING
1075                 LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1076                            "acl_check_modlist: conn %d  global default %s access %s to \"%s\"\n",
1077                            conn->c_connid, access2str( ACL_WRITE ),
1078                            global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn ));
1079 #else
1080                 Debug( LDAP_DEBUG_ACL,
1081                         "=> access_allowed: global default %s access %s to \"%s\"\n",
1082                         access2str( ACL_WRITE ),
1083                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
1084 #endif
1085                 return global_default_access >= ACL_WRITE;
1086 #endif
1087         }
1088
1089         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
1090                 /*
1091                  * no-user-modification operational attributes are ignored
1092                  * by ACL_WRITE checking as any found here are not provided
1093                  * by the user
1094                  */
1095                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
1096 #ifdef NEW_LOGGING
1097                         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1098                                    "acl_check_modlist: conn %d  no-user-mod %s: modify access granted\n",
1099                                    conn->c_connid, mlist->sml_desc->ad_cname.bv_val ));
1100 #else
1101                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
1102                                 " modify access granted\n",
1103                                 mlist->sml_desc->ad_cname.bv_val, 0, 0 );
1104 #endif
1105                         continue;
1106                 }
1107
1108                 switch ( mlist->sml_op ) {
1109                 case LDAP_MOD_REPLACE:
1110                         /*
1111                          * We must check both permission to delete the whole
1112                          * attribute and permission to add the specific attributes.
1113                          * This prevents abuse from selfwriters.
1114                          */
1115                         if ( ! access_allowed( be, conn, op, e,
1116                                 mlist->sml_desc, NULL, ACL_WRITE ) )
1117                         {
1118                                 return( 0 );
1119                         }
1120
1121                         if ( mlist->sml_bvalues == NULL ) break;
1122
1123                         /* fall thru to check value to add */
1124
1125                 case LDAP_MOD_ADD:
1126                         assert( mlist->sml_bvalues != NULL );
1127
1128                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1129                                 if ( ! access_allowed( be, conn, op, e,
1130                                         mlist->sml_desc, bv, ACL_WRITE ) )
1131                                 {
1132                                         return( 0 );
1133                                 }
1134                         }
1135                         break;
1136
1137                 case LDAP_MOD_DELETE:
1138                         if ( mlist->sml_bvalues == NULL ) {
1139                                 if ( ! access_allowed( be, conn, op, e,
1140                                         mlist->sml_desc, NULL, ACL_WRITE ) )
1141                                 {
1142                                         return( 0 );
1143                                 }
1144                                 break;
1145                         }
1146                         for ( bv = mlist->sml_bvalues; bv->bv_val != NULL; bv++ ) {
1147                                 if ( ! access_allowed( be, conn, op, e,
1148                                         mlist->sml_desc, bv, ACL_WRITE ) )
1149                                 {
1150                                         return( 0 );
1151                                 }
1152                         }
1153                         break;
1154
1155                 default:
1156                         assert( 0 );
1157                         return( 0 );
1158                 }
1159         }
1160
1161         return( 1 );
1162 }
1163
1164 static char *
1165 aci_bvstrdup( struct berval *bv )
1166 {
1167         char *s;
1168
1169         s = (char *)ch_malloc(bv->bv_len + 1);
1170         if (s != NULL) {
1171                 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
1172                 s[bv->bv_len] = 0;
1173         }
1174         return(s);
1175 }
1176
1177 #ifdef SLAPD_ACI_ENABLED
1178 static int
1179 aci_strbvcmp(
1180         const char *s,
1181         struct berval *bv )
1182 {
1183         int res, len;
1184
1185         res = strncasecmp( s, bv->bv_val, bv->bv_len );
1186         if (res)
1187                 return(res);
1188         len = strlen(s);
1189         if (len > (int)bv->bv_len)
1190                 return(1);
1191         if (len < (int)bv->bv_len)
1192                 return(-1);
1193         return(0);
1194 }
1195 #endif
1196
1197 static int
1198 aci_get_part(
1199         struct berval *list,
1200         int ix,
1201         char sep,
1202         struct berval *bv )
1203 {
1204         int len;
1205         char *p;
1206
1207         if (bv) {
1208                 bv->bv_len = 0;
1209                 bv->bv_val = NULL;
1210         }
1211         len = list->bv_len;
1212         p = list->bv_val;
1213         while (len >= 0 && --ix >= 0) {
1214                 while (--len >= 0 && *p++ != sep) ;
1215         }
1216         while (len >= 0 && *p == ' ') {
1217                 len--;
1218                 p++;
1219         }
1220         if (len < 0)
1221                 return(-1);
1222
1223         if (!bv)
1224                 return(0);
1225
1226         bv->bv_val = p;
1227         while (--len >= 0 && *p != sep) {
1228                 bv->bv_len++;
1229                 p++;
1230         }
1231         while (bv->bv_len > 0 && *--p == ' ')
1232                 bv->bv_len--;
1233         return(bv->bv_len);
1234 }
1235
1236 BerVarray
1237 aci_set_gather (void *cookie, char *name, struct berval *attr)
1238 {
1239         AciSetCookie *cp = cookie;
1240         BerVarray bvals = NULL;
1241         struct berval bv, ndn;
1242
1243         /* this routine needs to return the bervals instead of
1244          * plain strings, since syntax is not known.  It should
1245          * also return the syntax or some "comparison cookie".
1246          */
1247
1248         bv.bv_val = name;
1249         bv.bv_len = strlen( name );
1250         if (dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS) {
1251                 const char *text;
1252                 AttributeDescription *desc = NULL;
1253                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1254                         backend_attribute(cp->be, NULL, NULL,
1255                                 cp->e, &ndn, desc, &bvals);
1256                 }
1257                 free(ndn.bv_val);
1258         }
1259         return(bvals);
1260 }
1261
1262 static int
1263 aci_match_set (
1264         struct berval *subj,
1265     Backend *be,
1266     Entry *e,
1267     Connection *conn,
1268     Operation *op,
1269     int setref
1270 )
1271 {
1272         struct berval set = { 0, NULL };
1273         int rc = 0;
1274         AciSetCookie cookie;
1275
1276         if (setref == 0) {
1277                 ber_dupbv( &set, subj );
1278         } else {
1279                 struct berval subjdn, ndn = { 0, NULL };
1280                 struct berval setat;
1281                 BerVarray bvals;
1282                 const char *text;
1283                 AttributeDescription *desc = NULL;
1284
1285                 /* format of string is "entry/setAttrName" */
1286                 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1287                         return(0);
1288                 } else {
1289                         /* FIXME: If dnNormalize was based on ldap_bv2dn
1290                          * instead of ldap_str2dn and would honor the bv_len
1291                          * we could skip this step and not worry about the
1292                          * unterminated string.
1293                          */
1294                         char *s = ch_malloc(subjdn.bv_len + 1);
1295                         AC_MEMCPY(s, subjdn.bv_val, subjdn.bv_len);
1296                         subjdn.bv_val = s;
1297                 }
1298
1299                 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1300                         setat.bv_val = SLAPD_ACI_SET_ATTR;
1301                         setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1302                 }
1303                 if ( setat.bv_val != NULL ) {
1304                         if ( dnNormalize2(NULL, &subjdn, &ndn) == LDAP_SUCCESS
1305                                 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1306                         {
1307                                 backend_attribute(be, NULL, NULL, e,
1308                                         &ndn, desc, &bvals);
1309                                 if ( bvals != NULL ) {
1310                                         if ( bvals[0].bv_val != NULL ) {
1311                                                 int i;
1312                                                 set = bvals[0];
1313                                                 bvals[0].bv_val = NULL;
1314                                                 for (i=1;bvals[i].bv_val;i++);
1315                                                 bvals[0].bv_val = bvals[i-1].bv_val;
1316                                                 bvals[i-1].bv_val = NULL;
1317                                         }
1318                                         ber_bvarray_free(bvals);
1319                                 }
1320                         }
1321                         if (ndn.bv_val)
1322                                 free(ndn.bv_val);
1323                 }
1324                 ch_free(subjdn.bv_val);
1325         }
1326
1327         if (set.bv_val != NULL) {
1328                 cookie.be = be;
1329                 cookie.e = e;
1330                 cookie.conn = conn;
1331                 cookie.op = op;
1332                 rc = (slap_set_filter(aci_set_gather, &cookie, &set,
1333                         op->o_ndn.bv_val, e->e_ndn, NULL) > 0);
1334                 ch_free(set.bv_val);
1335         }
1336         return(rc);
1337 }
1338
1339 #ifdef SLAPD_ACI_ENABLED
1340 static int
1341 aci_list_map_rights(
1342         struct berval *list )
1343 {
1344         struct berval bv;
1345         slap_access_t mask;
1346         int i;
1347
1348         ACL_INIT(mask);
1349         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1350                 if (bv.bv_len <= 0)
1351                         continue;
1352                 switch (*bv.bv_val) {
1353                 case 'c':
1354                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1355                         break;
1356                 case 's':
1357                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1358                          * the right 's' to mean "set", but in the examples states
1359                          * that the right 's' means "search".  The latter definition
1360                          * is used here.
1361                          */
1362                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1363                         break;
1364                 case 'r':
1365                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1366                         break;
1367                 case 'w':
1368                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1369                         break;
1370                 case 'x':
1371                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1372                          * define any equivalent to the AUTH right, so I've just used
1373                          * 'x' for now.
1374                          */
1375                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1376                         break;
1377                 default:
1378                         break;
1379                 }
1380
1381         }
1382         return(mask);
1383 }
1384
1385 static int
1386 aci_list_has_attr(
1387         struct berval *list,
1388         const struct berval *attr,
1389         struct berval *val )
1390 {
1391         struct berval bv, left, right;
1392         int i;
1393
1394         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1395                 if (aci_get_part(&bv, 0, '=', &left) < 0
1396                         || aci_get_part(&bv, 1, '=', &right) < 0)
1397                 {
1398                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1399                                 return(1);
1400                 } else if (val == NULL) {
1401                         if (ber_bvstrcasecmp(attr, &left) == 0)
1402                                 return(1);
1403                 } else {
1404                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1405                                 /* this is experimental code that implements a
1406                                  * simple (prefix) match of the attribute value.
1407                                  * the ACI draft does not provide for aci's that
1408                                  * apply to specific values, but it would be
1409                                  * nice to have.  If the <attr> part of an aci's
1410                                  * rights list is of the form <attr>=<value>,
1411                                  * that means the aci applies only to attrs with
1412                                  * the given value.  Furthermore, if the attr is
1413                                  * of the form <attr>=<value>*, then <value> is
1414                                  * treated as a prefix, and the aci applies to 
1415                                  * any value with that prefix.
1416                                  *
1417                                  * Ideally, this would allow r.e. matches.
1418                                  */
1419                                 if (aci_get_part(&right, 0, '*', &left) < 0
1420                                         || right.bv_len <= left.bv_len)
1421                                 {
1422                                         if (ber_bvstrcasecmp(val, &right) == 0)
1423                                                 return(1);
1424                                 } else if (val->bv_len >= left.bv_len) {
1425                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1426                                                 return(1);
1427                                 }
1428                         }
1429                 }
1430         }
1431         return(0);
1432 }
1433
1434 static slap_access_t
1435 aci_list_get_attr_rights(
1436         struct berval *list,
1437         const struct berval *attr,
1438         struct berval *val )
1439 {
1440     struct berval bv;
1441     slap_access_t mask;
1442     int i;
1443
1444         /* loop through each rights/attr pair, skip first part (action) */
1445         ACL_INIT(mask);
1446         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1447                 if (aci_list_has_attr(&bv, attr, val) == 0)
1448                         continue;
1449                 if (aci_get_part(list, i, ';', &bv) < 0)
1450                         continue;
1451                 mask |= aci_list_map_rights(&bv);
1452         }
1453         return(mask);
1454 }
1455
1456 static int
1457 aci_list_get_rights(
1458         struct berval *list,
1459         const struct berval *attr,
1460         struct berval *val,
1461         slap_access_t *grant,
1462         slap_access_t *deny )
1463 {
1464     struct berval perm, actn;
1465     slap_access_t *mask;
1466     int i, found;
1467
1468         if (attr == NULL || attr->bv_len == 0 
1469                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1470                 attr = &aci_bv_br_entry;
1471         }
1472
1473         found = 0;
1474         ACL_INIT(*grant);
1475         ACL_INIT(*deny);
1476         /* loop through each permissions clause */
1477         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1478                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1479                         continue;
1480                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1481                         mask = grant;
1482                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1483                         mask = deny;
1484                 } else {
1485                         continue;
1486                 }
1487
1488                 found = 1;
1489                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1490                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1491         }
1492         return(found);
1493 }
1494
1495 static int
1496 aci_group_member (
1497         struct berval *subj,
1498         struct berval *defgrpoc,
1499         struct berval *defgrpat,
1500     Backend             *be,
1501     Entry               *e,
1502     Connection          *conn,
1503     Operation           *op,
1504         regmatch_t      *matches
1505 )
1506 {
1507         struct berval bv;
1508         char *subjdn;
1509         struct berval grpoc;
1510         struct berval grpat;
1511         ObjectClass *grp_oc = NULL;
1512         AttributeDescription *grp_ad = NULL;
1513         const char *text;
1514         int rc;
1515
1516         /* format of string is "group/objectClassValue/groupAttrName" */
1517         if (aci_get_part(subj, 0, '/', &bv) < 0) {
1518                 return(0);
1519         }
1520
1521         subjdn = aci_bvstrdup(&bv);
1522         if (subjdn == NULL) {
1523                 return(0);
1524         }
1525
1526         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1527                 grpoc = *defgrpoc;
1528         }
1529
1530         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1531                 grpat = *defgrpat;
1532         }
1533
1534         rc = slap_bv2ad( &grpat, &grp_ad, &text );
1535         if( rc != LDAP_SUCCESS ) {
1536                 rc = 0;
1537                 goto done;
1538         }
1539         rc = 0;
1540
1541         grp_oc = oc_bvfind( &grpoc );
1542
1543         if (grp_oc != NULL && grp_ad != NULL ) {
1544                 struct berval ndn;
1545                 bv.bv_val = (char *)ch_malloc(1024);
1546                 bv.bv_len = 1024;
1547                 string_expand(&bv, subjdn, e->e_ndn, matches);
1548                 if ( dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS ) {
1549                         rc = (backend_group(be, conn, op, e, &ndn, &op->o_ndn, grp_oc, grp_ad) == 0);
1550                         free( ndn.bv_val );
1551                 }
1552                 ch_free(bv.bv_val);
1553         }
1554
1555 done:
1556         ch_free(subjdn);
1557         return(rc);
1558 }
1559
1560 static struct berval GroupClass = {
1561         sizeof(SLAPD_GROUP_CLASS)-1, SLAPD_GROUP_CLASS };
1562 static struct berval GroupAttr = {
1563         sizeof(SLAPD_GROUP_ATTR)-1, SLAPD_GROUP_ATTR };
1564 static struct berval RoleClass = {
1565         sizeof(SLAPD_ROLE_CLASS)-1, SLAPD_ROLE_CLASS };
1566 static struct berval RoleAttr = {
1567         sizeof(SLAPD_ROLE_ATTR)-1, SLAPD_ROLE_ATTR };
1568
1569 static int
1570 aci_mask(
1571     Backend                     *be,
1572     Connection          *conn,
1573     Operation           *op,
1574     Entry                       *e,
1575         AttributeDescription *desc,
1576     struct berval       *val,
1577     struct berval       *aci,
1578         regmatch_t              *matches,
1579         slap_access_t   *grant,
1580         slap_access_t   *deny
1581 )
1582 {
1583     struct berval bv, perms, sdn;
1584         int rc;
1585                 
1586
1587         assert( desc->ad_cname.bv_val != NULL );
1588
1589         /* parse an aci of the form:
1590                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1591
1592            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1593            a full description of the format for this attribute.
1594
1595            For now, this routine only supports scope=entry.
1596          */
1597
1598         /* check that the aci has all 5 components */
1599         if (aci_get_part(aci, 4, '#', NULL) < 0)
1600                 return(0);
1601
1602         /* check that the aci family is supported */
1603         if (aci_get_part(aci, 0, '#', &bv) < 0)
1604                 return(0);
1605
1606         /* check that the scope is "entry" */
1607         if (aci_get_part(aci, 1, '#', &bv) < 0
1608                 || ber_bvstrcasecmp( &aci_bv_entry, &bv ) != 0)
1609         {
1610                 return(0);
1611         }
1612
1613         /* get the list of permissions clauses, bail if empty */
1614         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1615                 return(0);
1616
1617         /* check if any permissions allow desired access */
1618         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1619                 return(0);
1620
1621         /* see if we have a DN match */
1622         if (aci_get_part(aci, 3, '#', &bv) < 0)
1623                 return(0);
1624
1625         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1626                 return(0);
1627
1628         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1629                 struct berval ndn;
1630                 rc = 1;
1631                 if ( dnNormalize2(NULL, &sdn, &ndn) == LDAP_SUCCESS ) {
1632                         if (!dn_match( &op->o_ndn, &ndn))
1633                                 rc = 0;
1634                         free(ndn.bv_val);
1635                 }
1636                 return(rc);
1637         }
1638
1639         if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1640                 if (dn_match(&op->o_ndn, &e->e_nname))
1641                         return(1);
1642
1643         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1644                 Attribute *at;
1645                 AttributeDescription *ad = NULL;
1646                 const char *text;
1647
1648                 rc = slap_bv2ad( &sdn, &ad, &text );
1649
1650                 if( rc != LDAP_SUCCESS ) {
1651                         return 0;
1652                 }
1653
1654                 rc = 0;
1655
1656                 bv = op->o_ndn;
1657
1658                 for(at = attrs_find( e->e_attrs, ad );
1659                         at != NULL;
1660                         at = attrs_find( at->a_next, ad ) )
1661                 {
1662                         if (value_find( ad, at->a_vals, &bv) == 0 ) {
1663                                 rc = 1;
1664                                 break;
1665                         }
1666                 }
1667
1668                 return rc;
1669
1670
1671         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
1672                 if (aci_group_member(&sdn, &GroupClass, &GroupAttr, be, e, conn, op, matches))
1673                         return(1);
1674
1675         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
1676                 if (aci_group_member(&sdn, &RoleClass, &RoleAttr, be, e, conn, op, matches))
1677                         return(1);
1678
1679         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
1680                 if (aci_match_set(&sdn, be, e, conn, op, 0))
1681                         return(1);
1682
1683         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
1684                 if (aci_match_set(&sdn, be, e, conn, op, 1))
1685                         return(1);
1686
1687         }
1688
1689         return(0);
1690 }
1691
1692 #endif  /* SLAPD_ACI_ENABLED */
1693
1694 static void
1695 string_expand(
1696         struct berval *bv,
1697         char *pat,
1698         char *match,
1699         regmatch_t *matches)
1700 {
1701         ber_len_t       size;
1702         char   *sp;
1703         char   *dp;
1704         int     flag;
1705
1706         size = 0;
1707         bv->bv_val[0] = '\0';
1708         bv->bv_len--; /* leave space for lone $ */
1709
1710         flag = 0;
1711         for ( dp = bv->bv_val, sp = pat; size < bv->bv_len && *sp ; sp++) {
1712                 /* did we previously see a $ */
1713                 if (flag) {
1714                         if (*sp == '$') {
1715                                 *dp++ = '$';
1716                                 size++;
1717                         } else if (*sp >= '0' && *sp <= '9' ) {
1718                                 int     n;
1719                                 int     i;
1720                                 int     l;
1721
1722                                 n = *sp - '0';
1723                                 *dp = '\0';
1724                                 i = matches[n].rm_so;
1725                                 l = matches[n].rm_eo; 
1726                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
1727                                         *dp++ = match[i];
1728                                         size++;
1729                                 }
1730                                 *dp = '\0';
1731                         }
1732                         flag = 0;
1733                 } else {
1734                         if (*sp == '$') {
1735                                 flag = 1;
1736                         } else {
1737                                 *dp++ = *sp;
1738                                 size++;
1739                         }
1740                 }
1741         }
1742
1743         if (flag) {
1744                 /* must have ended with a single $ */
1745                 *dp++ = '$';
1746                 size++;
1747         }
1748
1749         *dp = '\0';
1750         bv->bv_len = size;
1751
1752 #ifdef NEW_LOGGING
1753         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1754                    "string_expand:  pattern = %s\n", pat ));
1755         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1756                    "string_expand:  expanded = %s\n", bv->bv_val ));
1757 #else
1758         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %s\n", pat, 0, 0 );
1759         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
1760 #endif
1761 }
1762
1763 static int
1764 regex_matches(
1765         char *pat,                              /* pattern to expand and match against */
1766         char *str,                              /* string to match against pattern */
1767         char *buf,                              /* buffer with $N expansion variables */
1768         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1769 )
1770 {
1771         regex_t re;
1772         char newbuf[512];
1773         struct berval bv = {sizeof(newbuf), newbuf};
1774         int     rc;
1775
1776         if(str == NULL) str = "";
1777
1778         string_expand(&bv, pat, buf, matches);
1779         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1780                 char error[512];
1781                 regerror(rc, &re, error, sizeof(error));
1782
1783 #ifdef NEW_LOGGING
1784                 LDAP_LOG(( "aci", LDAP_LEVEL_ERR,
1785                            "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1786                            pat, str, error ));
1787 #else
1788                 Debug( LDAP_DEBUG_TRACE,
1789                     "compile( \"%s\", \"%s\") failed %s\n",
1790                         pat, str, error );
1791 #endif
1792                 return( 0 );
1793         }
1794
1795         rc = regexec(&re, str, 0, NULL, 0);
1796         regfree( &re );
1797
1798 #ifdef NEW_LOGGING
1799         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1800                    "regex_matches: string:   %s\n", str ));
1801         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1802                    "regex_matches: rc:  %d  %s\n",
1803                    rc, rc ? "matches" : "no matches" ));
1804 #else
1805         Debug( LDAP_DEBUG_TRACE,
1806             "=> regex_matches: string:   %s\n", str, 0, 0 );
1807         Debug( LDAP_DEBUG_TRACE,
1808             "=> regex_matches: rc: %d %s\n",
1809                 rc, !rc ? "matches" : "no matches", 0 );
1810 #endif
1811         return( !rc );
1812 }
1813