]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
Add referral generator
[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
18 static AccessControl * acl_get(
19         AccessControl *ac, int *count,
20         Backend *be, Operation *op,
21         Entry *e,
22         AttributeDescription *desc,
23         int nmatches, regmatch_t *matches );
24
25 static slap_control_t acl_mask(
26         AccessControl *ac, slap_access_mask_t *mask,
27         Backend *be, Connection *conn, Operation *op,
28         Entry *e,
29         AttributeDescription *desc,
30         struct berval *val,
31         regmatch_t *matches );
32
33 #ifdef SLAPD_ACI_ENABLED
34 static int aci_mask(
35         Backend *be,
36     Connection *conn,
37         Operation *op,
38         Entry *e,
39         AttributeDescription *desc,
40         struct berval *val,
41         struct berval *aci,
42         regmatch_t *matches,
43         slap_access_t *grant,
44         slap_access_t *deny );
45 #endif
46
47 static int      regex_matches(
48         char *pat, char *str, char *buf, regmatch_t *matches);
49 static void     string_expand(
50         char *newbuf, int bufsiz, char *pattern,
51         char *match, regmatch_t *matches);
52
53
54 /*
55  * access_allowed - check whether op->o_ndn is allowed the requested access
56  * to entry e, attribute attr, value val.  if val is null, access to
57  * the whole attribute is assumed (all values).
58  *
59  * This routine loops through all access controls and calls
60  * acl_mask() on each applicable access control.
61  * The loop exits when a definitive answer is reached or
62  * or no more controls remain.
63  *
64  * returns:
65  *              0       access denied
66  *              1       access granted
67  */
68
69 int
70 access_allowed(
71     Backend             *be,
72     Connection          *conn,
73     Operation           *op,
74     Entry               *e,
75         AttributeDescription    *desc,
76     struct berval       *val,
77     slap_access_t       access )
78 {
79         int                             count;
80         AccessControl   *a;
81 #ifdef LDAP_DEBUG
82         char accessmaskbuf[ACCESSMASK_MAXLEN];
83 #endif
84         slap_access_mask_t mask;
85         slap_control_t control;
86
87         const char *attr = desc ? desc->ad_cname->bv_val : NULL;
88
89         regmatch_t       matches[MAXREMATCHES];
90
91         Debug( LDAP_DEBUG_ACL,
92                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
93             access2str( access ),
94                 e->e_dn, attr );
95
96         assert( be != NULL );
97         assert( e != NULL );
98         assert( attr != NULL );
99         assert( access > ACL_NONE );
100
101         /* grant database root access */
102         if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
103                 Debug( LDAP_DEBUG_ACL,
104                     "<= root access granted\n",
105                         0, 0, 0 );
106                 return 1;
107         }
108
109         /*
110          * no-user-modification operational attributes are ignored
111          * by ACL_WRITE checking as any found here are not provided
112          * by the user
113          */
114         if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type ) )
115         {
116                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
117                         " %s access granted\n",
118                         attr, 0, 0 );
119                 return 1;
120         }
121
122         /* use backend default access if no backend acls */
123         if( be != NULL && be->be_acl == NULL ) {
124                 Debug( LDAP_DEBUG_ACL,
125                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
126                         access2str( access ),
127                         be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
128
129                 return be->be_dfltaccess >= access;
130
131 #ifdef notdef
132         /* be is always non-NULL */
133         /* use global default access if no global acls */
134         } else if ( be == NULL && global_acl == NULL ) {
135                 Debug( LDAP_DEBUG_ACL,
136                         "=> access_allowed: global default %s access %s to \"%s\"\n",
137                         access2str( access ),
138                         global_default_access >= access ? "granted" : "denied", op->o_dn );
139
140                 return global_default_access >= access;
141 #endif
142         }
143
144         ACL_INIT(mask);
145         memset(matches, '\0', sizeof(matches));
146         
147         control = ACL_BREAK;
148         a = NULL;
149         count = 0;
150
151         while( a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches ) )
152         {
153                 int i;
154
155                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
156                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
157                                (int)matches[i].rm_so, (int)matches[i].rm_eo );
158
159                         if( matches[i].rm_so <= matches[0].rm_eo ) {
160                                 int n;
161                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
162                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
163                                 }
164                         }
165                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
166                 }
167
168                 control = acl_mask( a, &mask, be, conn, op,
169                         e, desc, val, matches );
170
171                 if ( control != ACL_BREAK ) {
172                         break;
173                 }
174
175                 memset(matches, '\0', sizeof(matches));
176         }
177
178         if ( ACL_IS_INVALID( mask ) ) {
179                 Debug( LDAP_DEBUG_ACL,
180                         "=> access_allowed: \"%s\" (%s) invalid!\n",
181                         e->e_dn, attr, 0 );
182                 ACL_INIT( mask );
183
184         } else if ( control == ACL_BREAK ) {
185                 Debug( LDAP_DEBUG_ACL,
186                         "=> access_allowed: no more rules\n", 0, 0, 0);
187                 ACL_INIT( mask );
188         }
189
190         Debug( LDAP_DEBUG_ACL,
191                 "=> access_allowed: %s access %s by %s\n",
192                 access2str( access ),
193                 ACL_GRANT(mask, access) ? "granted" : "denied",
194                 accessmask2str( mask, accessmaskbuf ) );
195
196         return ACL_GRANT(mask, access);
197 }
198
199 /*
200  * acl_get - return the acl applicable to entry e, attribute
201  * attr.  the acl returned is suitable for use in subsequent calls to
202  * acl_access_allowed().
203  */
204
205 static AccessControl *
206 acl_get(
207         AccessControl *a,
208         int                     *count,
209     Backend             *be,
210     Operation   *op,
211     Entry               *e,
212         AttributeDescription *desc,
213     int                 nmatch,
214     regmatch_t  *matches )
215 {
216         const char *attr;
217         int dnlen, patlen;
218
219         assert( e != NULL );
220         assert( count != NULL );
221
222         attr = desc ? desc->ad_cname->bv_val : NULL;
223
224         if( a == NULL ) {
225                 if( be == NULL ) {
226                         a = global_acl;
227                 } else {
228                         a = be->be_acl;
229                 }
230
231                 assert( a != NULL );
232
233         } else {
234                 a = a->acl_next;
235         }
236
237         dnlen = strlen(e->e_ndn);
238
239         for ( ; a != NULL; a = a->acl_next ) {
240                 (*count) ++;
241
242                 if (a->acl_dn_pat != NULL) {
243                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
244                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
245                                         *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
246
247                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
248                                         continue;
249
250                         } else {
251                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
252                                         *count, a->acl_dn_pat, 0 );
253
254                                 patlen = strlen( a->acl_dn_pat );
255                                 if ( dnlen < patlen )
256                                         continue;
257
258                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
259                                         /* base dn -- entire object DN must match */
260                                         if ( dnlen != patlen )
261                                                 continue;
262
263                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
264                                         char *rdn;
265                                         int rdnlen = -1;
266
267                                         if ( dnlen <= patlen )
268                                                 continue;
269
270                                         if ( e->e_ndn[dnlen - patlen - 1] != ',' )
271                                                 continue;
272
273                                         rdn = dn_rdn( NULL, e->e_ndn );
274                                         if ( rdn != NULL ) {
275                                                 rdnlen = strlen( rdn );
276                                                 ch_free( rdn );
277                                         }
278                                         if ( rdnlen != dnlen - patlen - 1 )
279                                                 continue;
280
281                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
282                                         if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
283                                                 continue;
284
285                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
286                                         if ( dnlen <= patlen )
287                                                 continue;
288                                         if ( e->e_ndn[dnlen - patlen - 1] != ',' )
289                                                 continue;
290                                 }
291
292                                 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
293                                         continue;
294                         }
295
296                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
297                                 *count, 0, 0 );
298                 }
299
300                 if ( a->acl_filter != NULL ) {
301                         ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
302                         if ( rc != LDAP_COMPARE_TRUE ) {
303                                 continue;
304                         }
305                 }
306
307         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
308                         *count, attr, 0);
309
310                 if ( attr == NULL || a->acl_attrs == NULL ||
311                         ad_inlist( desc, a->acl_attrs ) )
312                 {
313                         Debug( LDAP_DEBUG_ACL,
314                                 "<= acl_get: [%d] acl %s attr: %s\n",
315                                 *count, e->e_dn, attr );
316                         return a;
317                 }
318                 matches[0].rm_so = matches[0].rm_eo = -1;
319         }
320
321         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
322         return( NULL );
323 }
324
325
326 /*
327  * acl_mask - modifies mask based upon the given acl and the
328  * requested access to entry e, attribute attr, value val.  if val
329  * is null, access to the whole attribute is assumed (all values).
330  *
331  * returns      0       access NOT allowed
332  *              1       access allowed
333  */
334
335 static slap_control_t
336 acl_mask(
337     AccessControl       *a,
338         slap_access_mask_t *mask,
339     Backend             *be,
340     Connection  *conn,
341     Operation   *op,
342     Entry               *e,
343         AttributeDescription *desc,
344     struct berval       *val,
345         regmatch_t      *matches
346 )
347 {
348         int             i, odnlen, patlen;
349         Access  *b;
350 #ifdef LDAP_DEBUG
351         char accessmaskbuf[ACCESSMASK_MAXLEN];
352 #endif
353         const char *attr = desc ? desc->ad_cname->bv_val : NULL;
354
355         assert( a != NULL );
356         assert( mask != NULL );
357
358         Debug( LDAP_DEBUG_ACL,
359                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
360                 e->e_dn, attr, 0 );
361
362         Debug( LDAP_DEBUG_ACL,
363                 "=> acl_mask: to value \"%s\" by \"%s\", (%s) \n",
364                 val ? val->bv_val : "*",
365                 op->o_ndn ?  op->o_ndn : "",
366                 accessmask2str( *mask, accessmaskbuf ) );
367
368         for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
369                 slap_access_mask_t oldmask, modmask;
370
371                 ACL_INVALIDATE( modmask );
372
373                 /* AND <who> clauses */
374                 if ( b->a_dn_pat != NULL ) {
375                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
376                                 b->a_dn_pat, 0, 0);
377                         /*
378                          * if access applies to the entry itself, and the
379                          * user is bound as somebody in the same namespace as
380                          * the entry, OR the given dn matches the dn pattern
381                          */
382                         if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
383                                 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
384                                         continue;
385                                 }
386
387                         } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
388                                 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
389                                         continue;
390                                 }
391
392                         } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
393                                 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
394                                         continue;
395                                 }
396                                 
397                                 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
398                                         continue;
399                                 }
400
401                         } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
402                                 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
403                                         int ret = regex_matches( b->a_dn_pat,
404                                                 op->o_ndn, e->e_ndn, matches );
405
406                                         if( ret == 0 ) {
407                                                 continue;
408                                         }
409                                 }
410
411                         } else {
412                                 if ( e->e_dn == NULL )
413                                         continue;
414
415                                 patlen = strlen( b->a_dn_pat );
416                                 odnlen = strlen( op->o_ndn );
417                                 if ( odnlen < patlen )
418                                         continue;
419
420                                 if ( b->a_dn_style == ACL_STYLE_BASE ) {
421                                         /* base dn -- entire object DN must match */
422                                         if ( odnlen != patlen )
423                                                 continue;
424
425                                 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
426                                         char *rdn;
427                                         int rdnlen = -1;
428
429                                         if ( odnlen <= patlen )
430                                                 continue;
431
432                                         if ( op->o_ndn[odnlen - patlen - 1] != ',' )
433                                                 continue;
434
435                                         rdn = dn_rdn( NULL, op->o_ndn );
436                                         if ( rdn != NULL ) {
437                                                 rdnlen = strlen( rdn );
438                                                 ch_free( rdn );
439                                         }
440                                         if ( rdnlen != odnlen - patlen - 1 )
441                                                 continue;
442
443                                 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
444                                         if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
445                                                 continue;
446
447                                 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
448                                         if ( odnlen <= patlen )
449                                                 continue;
450                                         if ( op->o_ndn[odnlen - patlen - 1] != ',' )
451                                                 continue;
452                                 }
453
454                                 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
455                                         continue;
456
457                         }
458                 }
459
460                 if ( b->a_sockurl_pat != NULL ) {
461                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
462                                 b->a_sockurl_pat, 0, 0 );
463
464                         if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
465                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
466                                         if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
467                                                         e->e_ndn, matches ) ) 
468                                         {
469                                                 continue;
470                                         }
471                                 } else {
472                                         if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
473                                                 continue;
474                                 }
475                         }
476                 }
477
478                 if ( b->a_domain_pat != NULL ) {
479                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
480                                 b->a_domain_pat, 0, 0 );
481
482                         if ( strcmp( b->a_domain_pat, "*" ) != 0) {
483                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
484                                         if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
485                                                         e->e_ndn, matches ) ) 
486                                         {
487                                                 continue;
488                                         }
489                                 } else {
490                                         if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
491                                                 continue;
492                                 }
493                         }
494                 }
495
496                 if ( b->a_peername_pat != NULL ) {
497                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
498                                 b->a_peername_pat, 0, 0 );
499
500                         if ( strcmp( b->a_peername_pat, "*" ) != 0) {
501                                 if ( b->a_peername_style == ACL_STYLE_REGEX) {
502                                         if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
503                                                         e->e_ndn, matches ) ) 
504                                         {
505                                                 continue;
506                                         }
507                                 } else {
508                                         if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
509                                                 continue;
510                                 }
511                         }
512                 }
513
514                 if ( b->a_sockname_pat != NULL ) {
515                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
516                                 b->a_sockname_pat, 0, 0 );
517
518                         if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
519                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
520                                         if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
521                                                         e->e_ndn, matches ) ) 
522                                         {
523                                                 continue;
524                                         }
525                                 } else {
526                                         if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
527                                                 continue;
528                                 }
529                         }
530                 }
531
532                 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
533                         Attribute       *at;
534                         struct berval   bv;
535                         int match;
536                         const char *text;
537                         const char *desc = b->a_dn_at->ad_cname->bv_val;
538
539                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
540                                 desc, 0, 0);
541
542                         bv.bv_val = op->o_ndn;
543                         bv.bv_len = strlen( bv.bv_val );
544
545                         /* see if asker is listed in dnattr */
546                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
547                                 at != NULL;
548                                 at = attrs_find( at->a_next, b->a_dn_at ) )
549                         {
550                                 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
551                                         /* found it */
552                                         match = 1;
553                                         break;
554                                 }
555                         }
556
557                         if( match ) {
558                                 if ( b->a_dn_self && (val == NULL
559                                         || value_match( &match, b->a_dn_at,
560                                                 b->a_dn_at->ad_type->sat_equality, val, &bv, &text ) )
561                                                 != LDAP_SUCCESS
562                                         || match )
563                                 {
564                                         continue;
565                                 }
566                         } else if ( ! b->a_dn_self || val == NULL
567                                 || value_match( &match, b->a_dn_at,
568                                         b->a_dn_at->ad_type->sat_equality, val, &bv, &text )
569                                         != LDAP_SUCCESS
570                                 || match )
571                         {
572                                 continue;
573                         }
574                 }
575
576                 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
577                         char buf[1024];
578
579                         /* b->a_group is an unexpanded entry name, expanded it should be an 
580                          * entry with objectclass group* and we test to see if odn is one of
581                          * the values in the attribute group
582                          */
583                         /* see if asker is listed in dnattr */
584                         if ( b->a_group_style != ACL_STYLE_REGEX ) {
585                                 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
586                                 if ( dn_normalize(buf) == NULL ) {
587                                         /* did not expand to a valid dn */
588                                         continue;
589                                 }
590                         } else {
591                                 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
592                                 buf[sizeof(buf) - 1] = 0;
593                         }
594
595                         if (backend_group(be, e, buf, op->o_ndn,
596                                 b->a_group_oc, b->a_group_at) != 0)
597                         {
598                                 continue;
599                         }
600                 }
601
602 #ifdef SLAPD_ACI_ENABLED
603                 if ( b->a_aci_at != NULL ) {
604                         Attribute       *at;
605                         slap_access_t grant, deny, tgrant, tdeny;
606
607                         /* this case works different from the others above.
608                          * since aci's themselves give permissions, we need
609                          * to first check b->a_mask, the ACL's access level.
610                          */
611
612                         if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
613                                 continue;
614                         }
615
616                         if ( e->e_dn == NULL ) {
617                                 continue;
618                         }
619
620                         /* first check if the right being requested
621                          * is allowed by the ACL clause.
622                          */
623                         if ( ! ACL_GRANT( b->a_mask, *mask ) ) {
624                                 continue;
625                         }
626
627                         /* get the aci attribute */
628                         at = attr_find( e->e_attrs, b->a_aci_at );
629                         if ( at == NULL ) {
630                                 continue;
631                         }
632
633                         /* start out with nothing granted, nothing denied */
634                         ACL_INIT(tgrant);
635                         ACL_INIT(tdeny);
636
637                         /* the aci is an multi-valued attribute.  The
638                          * rights are determined by OR'ing the individual
639                          * rights given by the acis.
640                          */
641                         for ( i = 0; at->a_vals[i] != NULL; i++ ) {
642                                 if (aci_mask( be, conn, op,
643                                         e, desc, val, at->a_vals[i],
644                                         matches, &grant, &deny ) != 0)
645                                 {
646                                         tgrant |= grant;
647                                         tdeny |= deny;
648                                 }
649                         }
650
651                         /* remove anything that the ACL clause does not allow */
652                         tgrant &= b->a_mask & ACL_PRIV_MASK;
653                         tdeny &= ACL_PRIV_MASK;
654
655                         /* see if we have anything to contribute */
656                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
657                                 continue;
658                         }
659
660                         /* this could be improved by changing acl_mask so that it can deal with
661                          * by clauses that return grant/deny pairs.  Right now, it does either
662                          * additive or subtractive rights, but not both at the same time.  So,
663                          * we need to combine the grant/deny pair into a single rights mask in
664                          * a smart way:  if either grant or deny is "empty", then we use the
665                          * opposite as is, otherwise we remove any denied rights from the grant
666                          * rights mask and construct an additive mask.
667                          */
668                         if (ACL_IS_INVALID(tdeny)) {
669                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
670
671                         } else if (ACL_IS_INVALID(tgrant)) {
672                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
673
674                         } else {
675                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
676                         }
677
678                 } else
679 #endif
680                 {
681                         modmask = b->a_mask;
682                 }
683
684
685                 Debug( LDAP_DEBUG_ACL,
686                         "<= acl_mask: [%d] applying %s (%s)\n",
687                         i, accessmask2str( modmask, accessmaskbuf ), 
688                         b->a_type == ACL_CONTINUE
689                                 ? "continue"
690                                 : b->a_type == ACL_BREAK
691                                         ? "break"
692                                         : "stop" );
693
694                 /* save old mask */
695                 oldmask = *mask;
696
697                 if( ACL_IS_ADDITIVE(modmask) ) {
698                         /* add privs */
699                         ACL_PRIV_SET( *mask, modmask );
700
701                         /* cleanup */
702                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
703
704                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
705                         /* substract privs */
706                         ACL_PRIV_CLR( *mask, modmask );
707
708                         /* cleanup */
709                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
710
711                 } else {
712                         /* assign privs */
713                         *mask = modmask;
714                 }
715
716                 Debug( LDAP_DEBUG_ACL,
717                         "<= acl_mask: [%d] mask: %s\n",
718                         i, accessmask2str(*mask, accessmaskbuf), 0 );
719
720                 if( b->a_type == ACL_CONTINUE ) {
721                         continue;
722
723                 } else if ( b->a_type == ACL_BREAK ) {
724                         return ACL_BREAK;
725
726                 } else {
727                         return ACL_STOP;
728                 }
729         }
730
731         Debug( LDAP_DEBUG_ACL,
732                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
733                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
734         return ACL_STOP;
735 }
736
737 /*
738  * acl_check_modlist - check access control on the given entry to see if
739  * it allows the given modifications by the user associated with op.
740  * returns      1       if mods allowed ok
741  *                      0       mods not allowed
742  */
743
744 int
745 acl_check_modlist(
746     Backend     *be,
747     Connection  *conn,
748     Operation   *op,
749     Entry       *e,
750     Modifications       *mlist
751 )
752 {
753         int             i;
754
755         assert( be != NULL );
756
757         /* short circuit root database access */
758         if ( be_isroot( be, op->o_ndn ) ) {
759                 Debug( LDAP_DEBUG_ACL,
760                         "<= acl_access_allowed: granted to database root\n",
761                     0, 0, 0 );
762                 return 1;
763         }
764
765         /* use backend default access if no backend acls */
766         if( be != NULL && be->be_acl == NULL ) {
767                 Debug( LDAP_DEBUG_ACL,
768                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
769                         access2str( ACL_WRITE ),
770                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
771
772                 return be->be_dfltaccess >= ACL_WRITE;
773
774 #ifdef notdef
775         /* be is always non-NULL */
776         /* use global default access if no global acls */
777         } else if ( be == NULL && global_acl == NULL ) {
778                 Debug( LDAP_DEBUG_ACL,
779                         "=> access_allowed: global default %s access %s to \"%s\"\n",
780                         access2str( ACL_WRITE ),
781                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
782
783                 return global_default_access >= ACL_WRITE;
784 #endif
785         }
786
787         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
788                 /*
789                  * no-user-modification operational attributes are ignored
790                  * by ACL_WRITE checking as any found here are not provided
791                  * by the user
792                  */
793                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
794                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
795                                 " modify access granted\n",
796                                 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
797                         continue;
798                 }
799
800                 switch ( mlist->sml_op ) {
801                 case LDAP_MOD_REPLACE:
802                 case LDAP_MOD_ADD:
803                         if ( mlist->sml_bvalues == NULL ) {
804                                 break;
805                         }
806                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
807                                 if ( ! access_allowed( be, conn, op, e,
808                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
809                                 {
810                                         return( 0 );
811                                 }
812                         }
813                         break;
814
815                 case LDAP_MOD_DELETE:
816                         if ( mlist->sml_bvalues == NULL ) {
817                                 if ( ! access_allowed( be, conn, op, e,
818                                         mlist->sml_desc, NULL, ACL_WRITE ) )
819                                 {
820                                         return( 0 );
821                                 }
822                                 break;
823                         }
824                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
825                                 if ( ! access_allowed( be, conn, op, e,
826                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
827                                 {
828                                         return( 0 );
829                                 }
830                         }
831                         break;
832                 }
833         }
834
835         return( 1 );
836 }
837
838 #ifdef SLAPD_ACI_ENABLED
839 static char *
840 aci_bvstrdup( struct berval *bv )
841 {
842         char *s;
843
844         s = (char *)ch_malloc(bv->bv_len + 1);
845         if (s != NULL) {
846                 memcpy(s, bv->bv_val, bv->bv_len);
847                 s[bv->bv_len] = 0;
848         }
849         return(s);
850 }
851
852 static int
853 aci_strbvcmp(
854         const char *s,
855         struct berval *bv )
856 {
857         int res, len;
858
859         res = strncasecmp( s, bv->bv_val, bv->bv_len );
860         if (res)
861                 return(res);
862         len = strlen(s);
863         if (len > (int)bv->bv_len)
864                 return(1);
865         if (len < (int)bv->bv_len)
866                 return(-1);
867         return(0);
868 }
869
870 static int
871 aci_get_part(
872         struct berval *list,
873         int ix,
874         char sep,
875         struct berval *bv )
876 {
877         int len;
878         char *p;
879
880         if (bv) {
881                 bv->bv_len = 0;
882                 bv->bv_val = NULL;
883         }
884         len = list->bv_len;
885         p = list->bv_val;
886         while (len >= 0 && --ix >= 0) {
887                 while (--len >= 0 && *p++ != sep) ;
888         }
889         while (len >= 0 && *p == ' ') {
890                 len--;
891                 p++;
892         }
893         if (len < 0)
894                 return(-1);
895
896         if (!bv)
897                 return(0);
898
899         bv->bv_val = p;
900         while (--len >= 0 && *p != sep) {
901                 bv->bv_len++;
902                 p++;
903         }
904         while (bv->bv_len > 0 && *--p == ' ')
905                 bv->bv_len--;
906         return(bv->bv_len);
907 }
908
909 static int
910 aci_list_map_rights(
911         struct berval *list )
912 {
913         struct berval bv;
914         slap_access_t mask;
915         int i;
916
917         ACL_INIT(mask);
918         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
919                 if (bv.bv_len <= 0)
920                         continue;
921                 switch (*bv.bv_val) {
922                 case 'c':
923                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
924                         break;
925                 case 's':
926                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
927                          * the right 's' to mean "set", but in the examples states
928                          * that the right 's' means "search".  The latter definition
929                          * is used here.
930                          */
931                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
932                         break;
933                 case 'r':
934                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
935                         break;
936                 case 'w':
937                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
938                         break;
939                 case 'x':
940                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
941                          * define any equivalent to the AUTH right, so I've just used
942                          * 'x' for now.
943                          */
944                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
945                         break;
946                 default:
947                         break;
948                 }
949
950         }
951         return(mask);
952 }
953
954 static int
955 aci_list_has_attr(
956         struct berval *list,
957         const char *attr,
958         struct berval *val )
959 {
960         struct berval bv, left, right;
961         int i;
962
963         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
964                 if (aci_get_part(&bv, 0, '=', &left) < 0
965                         || aci_get_part(&bv, 1, '=', &right) < 0)
966                 {
967                         if (aci_strbvcmp(attr, &bv) == 0)
968                                 return(1);
969                 } else if (val == NULL) {
970                         if (aci_strbvcmp(attr, &left) == 0)
971                                 return(1);
972                 } else {
973                         if (aci_strbvcmp(attr, &left) == 0) {
974                                 /* this is experimental code that implements a
975                                  * simple (prefix) match of the attribute value.
976                                  * the ACI draft does not provide for aci's that
977                                  * apply to specific values, but it would be
978                                  * nice to have.  If the <attr> part of an aci's
979                                  * rights list is of the form <attr>=<value>,
980                                  * that means the aci applies only to attrs with
981                                  * the given value.  Furthermore, if the attr is
982                                  * of the form <attr>=<value>*, then <value> is
983                                  * treated as a prefix, and the aci applies to 
984                                  * any value with that prefix.
985                                  *
986                                  * Ideally, this would allow r.e. matches.
987                                  */
988                                 if (aci_get_part(&right, 0, '*', &left) < 0
989                                         || right.bv_len <= left.bv_len)
990                                 {
991                                         if (aci_strbvcmp(val->bv_val, &right) == 0)
992                                                 return(1);
993                                 } else if (val->bv_len >= left.bv_len) {
994                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
995                                                 return(1);
996                                 }
997                         }
998                 }
999         }
1000         return(0);
1001 }
1002
1003 static slap_access_t
1004 aci_list_get_attr_rights(
1005         struct berval *list,
1006         const char *attr,
1007         struct berval *val )
1008 {
1009     struct berval bv;
1010     slap_access_t mask;
1011     int i;
1012
1013         /* loop through each rights/attr pair, skip first part (action) */
1014         ACL_INIT(mask);
1015         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1016                 if (aci_list_has_attr(&bv, attr, val) == 0)
1017                         continue;
1018                 if (aci_get_part(list, i, ';', &bv) < 0)
1019                         continue;
1020                 mask |= aci_list_map_rights(&bv);
1021         }
1022         return(mask);
1023 }
1024
1025 static int
1026 aci_list_get_rights(
1027         struct berval *list,
1028         const char *attr,
1029         struct berval *val,
1030         slap_access_t *grant,
1031         slap_access_t *deny )
1032 {
1033     struct berval perm, actn;
1034     slap_access_t *mask;
1035     int i, found;
1036
1037         if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1038                 attr = "[entry]";
1039         }
1040
1041         found = 0;
1042         ACL_INIT(*grant);
1043         ACL_INIT(*deny);
1044         /* loop through each permissions clause */
1045         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1046                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1047                         continue;
1048                 if (aci_strbvcmp( "grant", &actn ) == 0) {
1049                         mask = grant;
1050                 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1051                         mask = deny;
1052                 } else {
1053                         continue;
1054                 }
1055
1056                 found = 1;
1057                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1058                 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1059         }
1060         return(found);
1061 }
1062
1063 static int
1064 aci_group_member (
1065         struct berval *subj,
1066         const char *defgrpoc,
1067         const char *defgrpat,
1068     Backend             *be,
1069     Entry               *e,
1070     Operation           *op,
1071         regmatch_t      *matches
1072 )
1073 {
1074         struct berval bv;
1075         char *subjdn, *grpdn = NULL;
1076         char *grpoc;
1077         char *grpat;
1078         ObjectClass *grp_oc = NULL;
1079         AttributeDescription *grp_ad = NULL;
1080         char *text;
1081         int rc;
1082
1083         /* format of string is "group/objectClassValue/groupAttrName" */
1084         if (aci_get_part(subj, 0, '/', &bv) < 0) {
1085                 return(0);
1086         }
1087
1088         subjdn = aci_bvstrdup(&bv);
1089         if (subjdn == NULL) {
1090                 return(0);
1091         }
1092
1093         if (aci_get_part(subj, 1, '/', &bv) < 0) {
1094                 grpoc = ch_strdup( defgrpoc );
1095         } else {
1096                 grpoc = aci_bvstrdup(&bv);
1097         }
1098
1099         if (aci_get_part(subj, 2, '/', &bv) < 0) {
1100                 grpat = ch_strdup( defgrpat );
1101         } else {
1102                 grpat = aci_bvstrdup(&bv);
1103         }
1104
1105         rc = slap_str2ad( grpat, &grp_ad, &text );
1106         if( rc != LDAP_SUCCESS ) {
1107                 rc = 0;
1108                 goto done;
1109         }
1110         rc = 0;
1111
1112         grp_oc = oc_find( grpoc );
1113         grpdn = (char *)ch_malloc(1024);
1114
1115         if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1116                 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1117                 if ( dn_normalize(grpdn) != NULL ) {
1118                         rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1119                 }
1120         }
1121
1122 done:
1123         if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1124         ch_free(grpdn);
1125         ch_free(grpat);
1126         ch_free(grpoc);
1127         ch_free(subjdn);
1128         return(rc);
1129 }
1130
1131 static int
1132 aci_mask(
1133     Backend                     *be,
1134     Connection          *conn,
1135     Operation           *op,
1136     Entry                       *e,
1137         AttributeDescription *desc,
1138     struct berval       *val,
1139     struct berval       *aci,
1140         regmatch_t              *matches,
1141         slap_access_t   *grant,
1142         slap_access_t   *deny
1143 )
1144 {
1145     struct berval bv, perms, sdn;
1146     char *subjdn;
1147         int rc;
1148         char *attr = desc->ad_cname->bv_val;
1149
1150         /* parse an aci of the form:
1151                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1152
1153            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1154            a full description of the format for this attribute.
1155
1156            For now, this routine only supports scope=entry.
1157          */
1158
1159         /* check that the aci has all 5 components */
1160         if (aci_get_part(aci, 4, '#', NULL) < 0)
1161                 return(0);
1162
1163         /* check that the aci family is supported */
1164         if (aci_get_part(aci, 0, '#', &bv) < 0)
1165                 return(0);
1166
1167         /* check that the scope is "entry" */
1168         if (aci_get_part(aci, 1, '#', &bv) < 0
1169                 || aci_strbvcmp( "entry", &bv ) != 0)
1170         {
1171                 return(0);
1172         }
1173
1174         /* get the list of permissions clauses, bail if empty */
1175         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1176                 return(0);
1177
1178         /* check if any permissions allow desired access */
1179         if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1180                 return(0);
1181
1182         /* see if we have a DN match */
1183         if (aci_get_part(aci, 3, '#', &bv) < 0)
1184                 return(0);
1185
1186         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1187                 return(0);
1188
1189         if (aci_strbvcmp( "access-id", &bv ) == 0) {
1190                 subjdn = aci_bvstrdup(&sdn);
1191                 if (subjdn == NULL)
1192                         return(0);
1193                 rc = 1;
1194                 if ( dn_normalize(subjdn) != NULL )
1195                         if (strcasecmp(op->o_ndn, subjdn) != 0)
1196                                 rc = 0;
1197                 ch_free(subjdn);
1198                 return(rc);
1199         }
1200
1201         if (aci_strbvcmp( "self", &bv ) == 0) {
1202                 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1203                         return(1);
1204
1205         } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1206                 char *dnattr = aci_bvstrdup(&sdn);
1207                 Attribute *at;
1208                 AttributeDescription *ad = NULL;
1209                 const char *text;
1210
1211                 rc = slap_str2ad( dnattr, &ad, &text );
1212                 ch_free( dnattr );
1213
1214                 if( rc != LDAP_SUCCESS ) {
1215                         return 0;
1216                 }
1217
1218                 rc = 0;
1219
1220                 bv.bv_val = op->o_ndn;
1221                 bv.bv_len = strlen( bv.bv_val );
1222
1223                 for(at = attrs_find( e->e_attrs, ad );
1224                         at != NULL;
1225                         at = attrs_find( at->a_next, ad ) )
1226                 {
1227                         if (value_find( ad, at->a_vals, &bv) == 0 ) {
1228                                 rc = 1;
1229                                 break;
1230                         }
1231                 }
1232
1233                 ad_free( ad, 1 );
1234                 return rc;
1235
1236
1237         } else if (aci_strbvcmp( "group", &bv ) == 0) {
1238                 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
1239                         return(1);
1240
1241         } else if (aci_strbvcmp( "role", &bv ) == 0) {
1242                 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
1243                         return(1);
1244
1245         }
1246
1247         return(0);
1248 }
1249
1250 #endif  /* SLAPD_ACI_ENABLED */
1251
1252 static void
1253 string_expand(
1254         char *newbuf,
1255         int bufsiz,
1256         char *pat,
1257         char *match,
1258         regmatch_t *matches)
1259 {
1260         int     size;
1261         char   *sp;
1262         char   *dp;
1263         int     flag;
1264
1265         size = 0;
1266         newbuf[0] = '\0';
1267         bufsiz--; /* leave space for lone $ */
1268
1269         flag = 0;
1270         for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1271                 /* did we previously see a $ */
1272                 if (flag) {
1273                         if (*sp == '$') {
1274                                 *dp++ = '$';
1275                                 size++;
1276                         } else if (*sp >= '0' && *sp <= '9' ) {
1277                                 int     n;
1278                                 int     i;
1279                                 int     l;
1280
1281                                 n = *sp - '0';
1282                                 *dp = '\0';
1283                                 i = matches[n].rm_so;
1284                                 l = matches[n].rm_eo; 
1285                                 for ( ; size < 512 && i < l; size++, i++ ) {
1286                                         *dp++ = match[i];
1287                                         size++;
1288                                 }
1289                                 *dp = '\0';
1290                         }
1291                         flag = 0;
1292                 } else {
1293                         if (*sp == '$') {
1294                                 flag = 1;
1295                         } else {
1296                                 *dp++ = *sp;
1297                                 size++;
1298                         }
1299                 }
1300         }
1301
1302         if (flag) {
1303                 /* must have ended with a single $ */
1304                 *dp++ = '$';
1305                 size++;
1306         }
1307
1308         *dp = '\0';
1309
1310         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %s\n", pat, 0, 0 );
1311         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1312 }
1313
1314 static int
1315 regex_matches(
1316         char *pat,                              /* pattern to expand and match against */
1317         char *str,                              /* string to match against pattern */
1318         char *buf,                              /* buffer with $N expansion variables */
1319         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1320 )
1321 {
1322         regex_t re;
1323         char newbuf[512];
1324         int     rc;
1325
1326         if(str == NULL) str = "";
1327
1328         string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1329         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1330                 char error[512];
1331                 regerror(rc, &re, error, sizeof(error));
1332
1333                 Debug( LDAP_DEBUG_TRACE,
1334                     "compile( \"%s\", \"%s\") failed %s\n",
1335                         pat, str, error );
1336                 return( 0 );
1337         }
1338
1339         rc = regexec(&re, str, 0, NULL, 0);
1340         regfree( &re );
1341
1342         Debug( LDAP_DEBUG_TRACE,
1343             "=> regex_matches: string:   %s\n", str, 0, 0 );
1344         Debug( LDAP_DEBUG_TRACE,
1345             "=> regex_matches: rc: %d %s\n",
1346                 rc, !rc ? "matches" : "no matches", 0 );
1347         return( !rc );
1348 }
1349