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