]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
744b416a66e6a04aba8b3439fbb6e123c8feceff
[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         struct berval *pat, char *str, char *buf, regmatch_t *matches);
71 static void     string_expand(
72         struct berval *newbuf, struct berval *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 SLAP_SET_GATHER aci_set_gather;
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] ) )
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] ) )
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] ) )
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,
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] ) )
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] ) )
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] ) )
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.bv_len ) {
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.bv_val ));
612 #else
613                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
614                                 b->a_sockurl_pat.bv_val, 0, 0 );
615 #endif
616
617                         if ( ber_bvccmp( &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.bv_val,
620                                                         e->e_ndn, matches ) ) 
621                                         {
622                                                 continue;
623                                         }
624                                 } else {
625                                         if ( ber_bvstrcasecmp( &b->a_sockurl_pat, &conn->c_listener_url ) != 0 )
626                                                 continue;
627                                 }
628                         }
629                 }
630
631                 if ( b->a_domain_pat.bv_len ) {
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.bv_val ));
636 #else
637                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
638                                 b->a_domain_pat.bv_val, 0, 0 );
639 #endif
640                         if ( ber_bvccmp( &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.bv_val,
643                                                         e->e_ndn, matches ) ) 
644                                         {
645                                                 continue;
646                                         }
647                                 } else {
648                                         if ( ber_bvstrcasecmp( &b->a_domain_pat, &conn->c_peer_domain ) != 0 )
649                                                 continue;
650                                 }
651                         }
652                 }
653
654                 if ( b->a_peername_pat.bv_len ) {
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.bv_val ));
659 #else
660                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
661                                 b->a_peername_pat.bv_val, 0, 0 );
662 #endif
663                         if ( ber_bvccmp( &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.bv_val,
666                                                         e->e_ndn, matches ) ) 
667                                         {
668                                                 continue;
669                                         }
670                                 } else {
671                                         if ( ber_bvstrcasecmp( &b->a_peername_pat, &conn->c_peer_name ) != 0 )
672                                                 continue;
673                                 }
674                         }
675                 }
676
677                 if ( b->a_sockname_pat.bv_len ) {
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.bv_val ));
682 #else
683                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
684                                 b->a_sockname_pat.bv_val, 0, 0 );
685 #endif
686                         if ( ber_bvccmp( &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.bv_val,
689                                                         e->e_ndn, matches ) ) 
690                                         {
691                                                 continue;
692                                         }
693                                 } else {
694                                         if ( ber_bvstrcasecmp( &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, 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                 case SLAP_MOD_SOFTADD:
1155                         /* allow adding attribute via modrdn thru */
1156                         break;
1157
1158                 default:
1159                         assert( 0 );
1160                         return( 0 );
1161                 }
1162         }
1163
1164         return( 1 );
1165 }
1166
1167 static char *
1168 aci_bvstrdup( struct berval *bv )
1169 {
1170         char *s;
1171
1172         s = (char *)ch_malloc(bv->bv_len + 1);
1173         if (s != NULL) {
1174                 AC_MEMCPY(s, bv->bv_val, bv->bv_len);
1175                 s[bv->bv_len] = 0;
1176         }
1177         return(s);
1178 }
1179
1180 static int
1181 aci_get_part(
1182         struct berval *list,
1183         int ix,
1184         char sep,
1185         struct berval *bv )
1186 {
1187         int len;
1188         char *p;
1189
1190         if (bv) {
1191                 bv->bv_len = 0;
1192                 bv->bv_val = NULL;
1193         }
1194         len = list->bv_len;
1195         p = list->bv_val;
1196         while (len >= 0 && --ix >= 0) {
1197                 while (--len >= 0 && *p++ != sep) ;
1198         }
1199         while (len >= 0 && *p == ' ') {
1200                 len--;
1201                 p++;
1202         }
1203         if (len < 0)
1204                 return(-1);
1205
1206         if (!bv)
1207                 return(0);
1208
1209         bv->bv_val = p;
1210         while (--len >= 0 && *p != sep) {
1211                 bv->bv_len++;
1212                 p++;
1213         }
1214         while (bv->bv_len > 0 && *--p == ' ')
1215                 bv->bv_len--;
1216         return(bv->bv_len);
1217 }
1218
1219 BerVarray
1220 aci_set_gather (void *cookie, struct berval *name, struct berval *attr)
1221 {
1222         AciSetCookie *cp = cookie;
1223         BerVarray bvals = NULL;
1224         struct berval ndn;
1225
1226         /* this routine needs to return the bervals instead of
1227          * plain strings, since syntax is not known.  It should
1228          * also return the syntax or some "comparison cookie".
1229          */
1230
1231         if (dnNormalize2(NULL, name, &ndn) == LDAP_SUCCESS) {
1232                 const char *text;
1233                 AttributeDescription *desc = NULL;
1234                 if (slap_bv2ad(attr, &desc, &text) == LDAP_SUCCESS) {
1235                         backend_attribute(cp->be, NULL, NULL,
1236                                 cp->e, &ndn, desc, &bvals);
1237                 }
1238                 free(ndn.bv_val);
1239         }
1240         return(bvals);
1241 }
1242
1243 static int
1244 aci_match_set (
1245         struct berval *subj,
1246     Backend *be,
1247     Entry *e,
1248     Connection *conn,
1249     Operation *op,
1250     int setref
1251 )
1252 {
1253         struct berval set = { 0, NULL };
1254         int rc = 0;
1255         AciSetCookie cookie;
1256
1257         if (setref == 0) {
1258                 ber_dupbv( &set, subj );
1259         } else {
1260                 struct berval subjdn, ndn = { 0, NULL };
1261                 struct berval setat;
1262                 BerVarray bvals;
1263                 const char *text;
1264                 AttributeDescription *desc = NULL;
1265
1266                 /* format of string is "entry/setAttrName" */
1267                 if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1268                         return(0);
1269                 } else {
1270                         /* FIXME: If dnNormalize was based on ldap_bv2dn
1271                          * instead of ldap_str2dn and would honor the bv_len
1272                          * we could skip this step and not worry about the
1273                          * unterminated string.
1274                          */
1275                         char *s = ch_malloc(subjdn.bv_len + 1);
1276                         AC_MEMCPY(s, subjdn.bv_val, subjdn.bv_len);
1277                         subjdn.bv_val = s;
1278                 }
1279
1280                 if ( aci_get_part(subj, 1, '/', &setat) < 0 ) {
1281                         setat.bv_val = SLAPD_ACI_SET_ATTR;
1282                         setat.bv_len = sizeof(SLAPD_ACI_SET_ATTR)-1;
1283                 }
1284                 if ( setat.bv_val != NULL ) {
1285                         if ( dnNormalize2(NULL, &subjdn, &ndn) == LDAP_SUCCESS
1286                                 && slap_bv2ad(&setat, &desc, &text) == LDAP_SUCCESS )
1287                         {
1288                                 backend_attribute(be, NULL, NULL, e,
1289                                         &ndn, desc, &bvals);
1290                                 if ( bvals != NULL ) {
1291                                         if ( bvals[0].bv_val != NULL ) {
1292                                                 int i;
1293                                                 set = bvals[0];
1294                                                 bvals[0].bv_val = NULL;
1295                                                 for (i=1;bvals[i].bv_val;i++);
1296                                                 bvals[0].bv_val = bvals[i-1].bv_val;
1297                                                 bvals[i-1].bv_val = NULL;
1298                                         }
1299                                         ber_bvarray_free(bvals);
1300                                 }
1301                         }
1302                         if (ndn.bv_val)
1303                                 free(ndn.bv_val);
1304                 }
1305                 ch_free(subjdn.bv_val);
1306         }
1307
1308         if (set.bv_val != NULL) {
1309                 cookie.be = be;
1310                 cookie.e = e;
1311                 cookie.conn = conn;
1312                 cookie.op = op;
1313                 rc = (slap_set_filter(aci_set_gather, &cookie, &set,
1314                         &op->o_ndn, &e->e_nname, NULL) > 0);
1315                 ch_free(set.bv_val);
1316         }
1317         return(rc);
1318 }
1319
1320 #ifdef SLAPD_ACI_ENABLED
1321 static int
1322 aci_list_map_rights(
1323         struct berval *list )
1324 {
1325         struct berval bv;
1326         slap_access_t mask;
1327         int i;
1328
1329         ACL_INIT(mask);
1330         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1331                 if (bv.bv_len <= 0)
1332                         continue;
1333                 switch (*bv.bv_val) {
1334                 case 'c':
1335                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1336                         break;
1337                 case 's':
1338                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
1339                          * the right 's' to mean "set", but in the examples states
1340                          * that the right 's' means "search".  The latter definition
1341                          * is used here.
1342                          */
1343                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1344                         break;
1345                 case 'r':
1346                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
1347                         break;
1348                 case 'w':
1349                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1350                         break;
1351                 case 'x':
1352                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
1353                          * define any equivalent to the AUTH right, so I've just used
1354                          * 'x' for now.
1355                          */
1356                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1357                         break;
1358                 default:
1359                         break;
1360                 }
1361
1362         }
1363         return(mask);
1364 }
1365
1366 static int
1367 aci_list_has_attr(
1368         struct berval *list,
1369         const struct berval *attr,
1370         struct berval *val )
1371 {
1372         struct berval bv, left, right;
1373         int i;
1374
1375         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
1376                 if (aci_get_part(&bv, 0, '=', &left) < 0
1377                         || aci_get_part(&bv, 1, '=', &right) < 0)
1378                 {
1379                         if (ber_bvstrcasecmp(attr, &bv) == 0)
1380                                 return(1);
1381                 } else if (val == NULL) {
1382                         if (ber_bvstrcasecmp(attr, &left) == 0)
1383                                 return(1);
1384                 } else {
1385                         if (ber_bvstrcasecmp(attr, &left) == 0) {
1386                                 /* this is experimental code that implements a
1387                                  * simple (prefix) match of the attribute value.
1388                                  * the ACI draft does not provide for aci's that
1389                                  * apply to specific values, but it would be
1390                                  * nice to have.  If the <attr> part of an aci's
1391                                  * rights list is of the form <attr>=<value>,
1392                                  * that means the aci applies only to attrs with
1393                                  * the given value.  Furthermore, if the attr is
1394                                  * of the form <attr>=<value>*, then <value> is
1395                                  * treated as a prefix, and the aci applies to 
1396                                  * any value with that prefix.
1397                                  *
1398                                  * Ideally, this would allow r.e. matches.
1399                                  */
1400                                 if (aci_get_part(&right, 0, '*', &left) < 0
1401                                         || right.bv_len <= left.bv_len)
1402                                 {
1403                                         if (ber_bvstrcasecmp(val, &right) == 0)
1404                                                 return(1);
1405                                 } else if (val->bv_len >= left.bv_len) {
1406                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1407                                                 return(1);
1408                                 }
1409                         }
1410                 }
1411         }
1412         return(0);
1413 }
1414
1415 static slap_access_t
1416 aci_list_get_attr_rights(
1417         struct berval *list,
1418         const struct berval *attr,
1419         struct berval *val )
1420 {
1421     struct berval bv;
1422     slap_access_t mask;
1423     int i;
1424
1425         /* loop through each rights/attr pair, skip first part (action) */
1426         ACL_INIT(mask);
1427         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1428                 if (aci_list_has_attr(&bv, attr, val) == 0)
1429                         continue;
1430                 if (aci_get_part(list, i, ';', &bv) < 0)
1431                         continue;
1432                 mask |= aci_list_map_rights(&bv);
1433         }
1434         return(mask);
1435 }
1436
1437 static int
1438 aci_list_get_rights(
1439         struct berval *list,
1440         const struct berval *attr,
1441         struct berval *val,
1442         slap_access_t *grant,
1443         slap_access_t *deny )
1444 {
1445     struct berval perm, actn;
1446     slap_access_t *mask;
1447     int i, found;
1448
1449         if (attr == NULL || attr->bv_len == 0 
1450                         || ber_bvstrcasecmp( attr, &aci_bv_entry ) == 0) {
1451                 attr = &aci_bv_br_entry;
1452         }
1453
1454         found = 0;
1455         ACL_INIT(*grant);
1456         ACL_INIT(*deny);
1457         /* loop through each permissions clause */
1458         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1459                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1460                         continue;
1461                 if (ber_bvstrcasecmp( &aci_bv_grant, &actn ) == 0) {
1462                         mask = grant;
1463                 } else if (ber_bvstrcasecmp( &aci_bv_deny, &actn ) == 0) {
1464                         mask = deny;
1465                 } else {
1466                         continue;
1467                 }
1468
1469                 found = 1;
1470                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1471                 *mask |= aci_list_get_attr_rights(&perm, &aci_bv_br_all, NULL);
1472         }
1473         return(found);
1474 }
1475
1476 static int
1477 aci_group_member (
1478         struct berval *subj,
1479         struct berval *defgrpoc,
1480         struct berval *defgrpat,
1481     Backend             *be,
1482     Entry               *e,
1483     Connection          *conn,
1484     Operation           *op,
1485         regmatch_t      *matches
1486 )
1487 {
1488         struct berval bv;
1489         struct berval subjdn;
1490         struct berval grpoc;
1491         struct berval grpat;
1492         ObjectClass *grp_oc = NULL;
1493         AttributeDescription *grp_ad = NULL;
1494         const char *text;
1495         int rc;
1496
1497         /* format of string is "group/objectClassValue/groupAttrName" */
1498         if (aci_get_part(subj, 0, '/', &subjdn) < 0) {
1499                 return(0);
1500         }
1501
1502         if (aci_get_part(subj, 1, '/', &grpoc) < 0) {
1503                 grpoc = *defgrpoc;
1504         }
1505
1506         if (aci_get_part(subj, 2, '/', &grpat) < 0) {
1507                 grpat = *defgrpat;
1508         }
1509
1510         rc = slap_bv2ad( &grpat, &grp_ad, &text );
1511         if( rc != LDAP_SUCCESS ) {
1512                 rc = 0;
1513                 goto done;
1514         }
1515         rc = 0;
1516
1517         grp_oc = oc_bvfind( &grpoc );
1518
1519         if (grp_oc != NULL && grp_ad != NULL ) {
1520                 struct berval ndn;
1521                 bv.bv_val = (char *)ch_malloc(1024);
1522                 bv.bv_len = 1024;
1523                 string_expand(&bv, &subjdn, e->e_ndn, matches);
1524                 if ( dnNormalize2(NULL, &bv, &ndn) == LDAP_SUCCESS ) {
1525                         rc = (backend_group(be, conn, op, e, &ndn, &op->o_ndn, grp_oc, grp_ad) == 0);
1526                         free( ndn.bv_val );
1527                 }
1528                 ch_free(bv.bv_val);
1529         }
1530
1531 done:
1532         return(rc);
1533 }
1534
1535 static struct berval GroupClass = {
1536         sizeof(SLAPD_GROUP_CLASS)-1, SLAPD_GROUP_CLASS };
1537 static struct berval GroupAttr = {
1538         sizeof(SLAPD_GROUP_ATTR)-1, SLAPD_GROUP_ATTR };
1539 static struct berval RoleClass = {
1540         sizeof(SLAPD_ROLE_CLASS)-1, SLAPD_ROLE_CLASS };
1541 static struct berval RoleAttr = {
1542         sizeof(SLAPD_ROLE_ATTR)-1, SLAPD_ROLE_ATTR };
1543
1544 static int
1545 aci_mask(
1546     Backend                     *be,
1547     Connection          *conn,
1548     Operation           *op,
1549     Entry                       *e,
1550         AttributeDescription *desc,
1551     struct berval       *val,
1552     struct berval       *aci,
1553         regmatch_t              *matches,
1554         slap_access_t   *grant,
1555         slap_access_t   *deny
1556 )
1557 {
1558     struct berval bv, perms, sdn;
1559         int rc;
1560                 
1561
1562         assert( desc->ad_cname.bv_val != NULL );
1563
1564         /* parse an aci of the form:
1565                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1566
1567            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1568            a full description of the format for this attribute.
1569
1570            For now, this routine only supports scope=entry.
1571          */
1572
1573         /* check that the aci has all 5 components */
1574         if (aci_get_part(aci, 4, '#', NULL) < 0)
1575                 return(0);
1576
1577         /* check that the aci family is supported */
1578         if (aci_get_part(aci, 0, '#', &bv) < 0)
1579                 return(0);
1580
1581         /* check that the scope is "entry" */
1582         if (aci_get_part(aci, 1, '#', &bv) < 0
1583                 || ber_bvstrcasecmp( &aci_bv_entry, &bv ) != 0)
1584         {
1585                 return(0);
1586         }
1587
1588         /* get the list of permissions clauses, bail if empty */
1589         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1590                 return(0);
1591
1592         /* check if any permissions allow desired access */
1593         if (aci_list_get_rights(&perms, &desc->ad_cname, val, grant, deny) == 0)
1594                 return(0);
1595
1596         /* see if we have a DN match */
1597         if (aci_get_part(aci, 3, '#', &bv) < 0)
1598                 return(0);
1599
1600         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1601                 return(0);
1602
1603         if (ber_bvstrcasecmp( &aci_bv_access_id, &bv ) == 0) {
1604                 struct berval ndn;
1605                 rc = 1;
1606                 if ( dnNormalize2(NULL, &sdn, &ndn) == LDAP_SUCCESS ) {
1607                         if (!dn_match( &op->o_ndn, &ndn))
1608                                 rc = 0;
1609                         free(ndn.bv_val);
1610                 }
1611                 return(rc);
1612         }
1613
1614         if (ber_bvstrcasecmp( &aci_bv_self, &bv ) == 0) {
1615                 if (dn_match(&op->o_ndn, &e->e_nname))
1616                         return(1);
1617
1618         } else if (ber_bvstrcasecmp( &aci_bv_dnattr, &bv ) == 0) {
1619                 Attribute *at;
1620                 AttributeDescription *ad = NULL;
1621                 const char *text;
1622
1623                 rc = slap_bv2ad( &sdn, &ad, &text );
1624
1625                 if( rc != LDAP_SUCCESS ) {
1626                         return 0;
1627                 }
1628
1629                 rc = 0;
1630
1631                 bv = op->o_ndn;
1632
1633                 for(at = attrs_find( e->e_attrs, ad );
1634                         at != NULL;
1635                         at = attrs_find( at->a_next, ad ) )
1636                 {
1637                         if (value_find( ad, at->a_vals, &bv) == 0 ) {
1638                                 rc = 1;
1639                                 break;
1640                         }
1641                 }
1642
1643                 return rc;
1644
1645
1646         } else if (ber_bvstrcasecmp( &aci_bv_group, &bv ) == 0) {
1647                 if (aci_group_member(&sdn, &GroupClass, &GroupAttr, be, e, conn, op, matches))
1648                         return(1);
1649
1650         } else if (ber_bvstrcasecmp( &aci_bv_role, &bv ) == 0) {
1651                 if (aci_group_member(&sdn, &RoleClass, &RoleAttr, be, e, conn, op, matches))
1652                         return(1);
1653
1654         } else if (ber_bvstrcasecmp( &aci_bv_set, &bv ) == 0) {
1655                 if (aci_match_set(&sdn, be, e, conn, op, 0))
1656                         return(1);
1657
1658         } else if (ber_bvstrcasecmp( &aci_bv_set_ref, &bv ) == 0) {
1659                 if (aci_match_set(&sdn, be, e, conn, op, 1))
1660                         return(1);
1661
1662         }
1663
1664         return(0);
1665 }
1666
1667 #endif  /* SLAPD_ACI_ENABLED */
1668
1669 static void
1670 string_expand(
1671         struct berval *bv,
1672         struct berval *pat,
1673         char *match,
1674         regmatch_t *matches)
1675 {
1676         ber_len_t       size;
1677         char   *sp;
1678         char   *dp;
1679         int     flag;
1680
1681         size = 0;
1682         bv->bv_val[0] = '\0';
1683         bv->bv_len--; /* leave space for lone $ */
1684
1685         flag = 0;
1686         for ( dp = bv->bv_val, sp = pat->bv_val; size < bv->bv_len &&
1687                 sp < pat->bv_val + pat->bv_len ; sp++) {
1688                 /* did we previously see a $ */
1689                 if (flag) {
1690                         if (*sp == '$') {
1691                                 *dp++ = '$';
1692                                 size++;
1693                         } else if (*sp >= '0' && *sp <= '9' ) {
1694                                 int     n;
1695                                 int     i;
1696                                 int     l;
1697
1698                                 n = *sp - '0';
1699                                 *dp = '\0';
1700                                 i = matches[n].rm_so;
1701                                 l = matches[n].rm_eo; 
1702                                 for ( ; size < bv->bv_len && i < l; size++, i++ ) {
1703                                         *dp++ = match[i];
1704                                         size++;
1705                                 }
1706                                 *dp = '\0';
1707                         }
1708                         flag = 0;
1709                 } else {
1710                         if (*sp == '$') {
1711                                 flag = 1;
1712                         } else {
1713                                 *dp++ = *sp;
1714                                 size++;
1715                         }
1716                 }
1717         }
1718
1719         if (flag) {
1720                 /* must have ended with a single $ */
1721                 *dp++ = '$';
1722                 size++;
1723         }
1724
1725         *dp = '\0';
1726         bv->bv_len = size;
1727
1728 #ifdef NEW_LOGGING
1729         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1730                    "string_expand:  pattern = %.*s\n", pat->bv_len, pat->bv_val ));
1731         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL1,
1732                    "string_expand:  expanded = %s\n", bv->bv_val ));
1733 #else
1734         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %.*s\n", pat->bv_len, pat->bv_val, 0 );
1735         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", bv->bv_val, 0, 0 );
1736 #endif
1737 }
1738
1739 static int
1740 regex_matches(
1741         struct berval *pat,                     /* pattern to expand and match against */
1742         char *str,                              /* string to match against pattern */
1743         char *buf,                              /* buffer with $N expansion variables */
1744         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1745 )
1746 {
1747         regex_t re;
1748         char newbuf[512];
1749         struct berval bv = {sizeof(newbuf), newbuf};
1750         int     rc;
1751
1752         if(str == NULL) str = "";
1753
1754         string_expand(&bv, pat, buf, matches);
1755         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1756                 char error[512];
1757                 regerror(rc, &re, error, sizeof(error));
1758
1759 #ifdef NEW_LOGGING
1760                 LDAP_LOG(( "aci", LDAP_LEVEL_ERR,
1761                            "regex_matches: compile( \"%s\", \"%s\") failed %s\n",
1762                            pat->bv_val, str, error ));
1763 #else
1764                 Debug( LDAP_DEBUG_TRACE,
1765                     "compile( \"%s\", \"%s\") failed %s\n",
1766                         pat->bv_val, str, error );
1767 #endif
1768                 return( 0 );
1769         }
1770
1771         rc = regexec(&re, str, 0, NULL, 0);
1772         regfree( &re );
1773
1774 #ifdef NEW_LOGGING
1775         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1776                    "regex_matches: string:   %s\n", str ));
1777         LDAP_LOG(( "aci", LDAP_LEVEL_DETAIL2,
1778                    "regex_matches: rc:  %d  %s\n",
1779                    rc, rc ? "matches" : "no matches" ));
1780 #else
1781         Debug( LDAP_DEBUG_TRACE,
1782             "=> regex_matches: string:   %s\n", str, 0, 0 );
1783         Debug( LDAP_DEBUG_TRACE,
1784             "=> regex_matches: rc: %d %s\n",
1785                 rc, !rc ? "matches" : "no matches", 0 );
1786 #endif
1787         return( !rc );
1788 }
1789