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