]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
ea08d0bddaac229a17430d5cf3325ab29d56c6bd
[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 rc, match = 0;
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                                 /* have a dnattr match. if this is a self clause then
559                                  * the target must also match the op dn.
560                                  */
561                                 if ( b->a_dn_self ) {
562                                         /* check if the target is an attribute. */
563                                         if ( val == NULL )
564                                                 continue;
565                                         /* target is attribute, check if the attribute value
566                                          * is the op dn.
567                                          */
568                                         rc = value_match( &match, b->a_dn_at,
569                                                 b->a_dn_at->ad_type->sat_equality, 0,
570                                                 val, &bv, &text );
571                                         /* on match error or no match, fail the ACL clause */
572                                         if (rc != LDAP_SUCCESS || match != 0 )
573                                                 continue;
574                                 }
575                         } else {
576                                 /* no dnattr match, check if this is a self clause */
577                                 if ( ! b->a_dn_self )
578                                         continue;
579                                 /* this is a self clause, check if the target is an
580                                  * attribute.
581                                  */
582                                 if ( val == NULL )
583                                         continue;
584                                 /* target is attribute, check if the attribute value
585                                  * is the op dn.
586                                  */
587                                 rc = value_match( &match, b->a_dn_at,
588                                         b->a_dn_at->ad_type->sat_equality, 0,
589                                         val, &bv, &text );
590
591                                 /* on match error or no match, fail the ACL clause */
592                                 if (rc != LDAP_SUCCESS || match != 0 )
593                                         continue;
594                         }
595                 }
596
597                 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
598                         char buf[1024];
599
600                         /* b->a_group is an unexpanded entry name, expanded it should be an 
601                          * entry with objectclass group* and we test to see if odn is one of
602                          * the values in the attribute group
603                          */
604                         /* see if asker is listed in dnattr */
605                         if ( b->a_group_style == ACL_STYLE_REGEX ) {
606                                 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
607                                 if ( dn_normalize(buf) == NULL ) {
608                                         /* did not expand to a valid dn */
609                                         continue;
610                                 }
611                         } else {
612                                 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
613                                 buf[sizeof(buf) - 1] = 0;
614                         }
615
616                         if (backend_group(be, e, buf, op->o_ndn,
617                                 b->a_group_oc, b->a_group_at) != 0)
618                         {
619                                 continue;
620                         }
621                 }
622
623 #ifdef SLAPD_ACI_ENABLED
624                 if ( b->a_aci_at != NULL ) {
625                         Attribute       *at;
626                         slap_access_t grant, deny, tgrant, tdeny;
627
628                         /* this case works different from the others above.
629                          * since aci's themselves give permissions, we need
630                          * to first check b->a_mask, the ACL's access level.
631                          */
632
633                         if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
634                                 continue;
635                         }
636
637                         if ( e->e_dn == NULL ) {
638                                 continue;
639                         }
640
641                         /* first check if the right being requested
642                          * is allowed by the ACL clause.
643                          */
644                         if ( ! ACL_GRANT( b->a_mask, *mask ) ) {
645                                 continue;
646                         }
647
648                         /* get the aci attribute */
649                         at = attr_find( e->e_attrs, b->a_aci_at );
650                         if ( at == NULL ) {
651                                 continue;
652                         }
653
654                         /* start out with nothing granted, nothing denied */
655                         ACL_INIT(tgrant);
656                         ACL_INIT(tdeny);
657
658                         /* the aci is an multi-valued attribute.  The
659                          * rights are determined by OR'ing the individual
660                          * rights given by the acis.
661                          */
662                         for ( i = 0; at->a_vals[i] != NULL; i++ ) {
663                                 if (aci_mask( be, conn, op,
664                                         e, desc, val, at->a_vals[i],
665                                         matches, &grant, &deny ) != 0)
666                                 {
667                                         tgrant |= grant;
668                                         tdeny |= deny;
669                                 }
670                         }
671
672                         /* remove anything that the ACL clause does not allow */
673                         tgrant &= b->a_mask & ACL_PRIV_MASK;
674                         tdeny &= ACL_PRIV_MASK;
675
676                         /* see if we have anything to contribute */
677                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
678                                 continue;
679                         }
680
681                         /* this could be improved by changing acl_mask so that it can deal with
682                          * by clauses that return grant/deny pairs.  Right now, it does either
683                          * additive or subtractive rights, but not both at the same time.  So,
684                          * we need to combine the grant/deny pair into a single rights mask in
685                          * a smart way:  if either grant or deny is "empty", then we use the
686                          * opposite as is, otherwise we remove any denied rights from the grant
687                          * rights mask and construct an additive mask.
688                          */
689                         if (ACL_IS_INVALID(tdeny)) {
690                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
691
692                         } else if (ACL_IS_INVALID(tgrant)) {
693                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
694
695                         } else {
696                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
697                         }
698
699                 } else
700 #endif
701                 {
702                         modmask = b->a_mask;
703                 }
704
705
706                 Debug( LDAP_DEBUG_ACL,
707                         "<= acl_mask: [%d] applying %s (%s)\n",
708                         i, accessmask2str( modmask, accessmaskbuf ), 
709                         b->a_type == ACL_CONTINUE
710                                 ? "continue"
711                                 : b->a_type == ACL_BREAK
712                                         ? "break"
713                                         : "stop" );
714
715                 /* save old mask */
716                 oldmask = *mask;
717
718                 if( ACL_IS_ADDITIVE(modmask) ) {
719                         /* add privs */
720                         ACL_PRIV_SET( *mask, modmask );
721
722                         /* cleanup */
723                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
724
725                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
726                         /* substract privs */
727                         ACL_PRIV_CLR( *mask, modmask );
728
729                         /* cleanup */
730                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
731
732                 } else {
733                         /* assign privs */
734                         *mask = modmask;
735                 }
736
737                 Debug( LDAP_DEBUG_ACL,
738                         "<= acl_mask: [%d] mask: %s\n",
739                         i, accessmask2str(*mask, accessmaskbuf), 0 );
740
741                 if( b->a_type == ACL_CONTINUE ) {
742                         continue;
743
744                 } else if ( b->a_type == ACL_BREAK ) {
745                         return ACL_BREAK;
746
747                 } else {
748                         return ACL_STOP;
749                 }
750         }
751
752         Debug( LDAP_DEBUG_ACL,
753                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
754                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
755         return ACL_STOP;
756 }
757
758 /*
759  * acl_check_modlist - check access control on the given entry to see if
760  * it allows the given modifications by the user associated with op.
761  * returns      1       if mods allowed ok
762  *                      0       mods not allowed
763  */
764
765 int
766 acl_check_modlist(
767     Backend     *be,
768     Connection  *conn,
769     Operation   *op,
770     Entry       *e,
771     Modifications       *mlist
772 )
773 {
774         int             i;
775
776         assert( be != NULL );
777
778         /* short circuit root database access */
779         if ( be_isroot( be, op->o_ndn ) ) {
780                 Debug( LDAP_DEBUG_ACL,
781                         "<= acl_access_allowed: granted to database root\n",
782                     0, 0, 0 );
783                 return 1;
784         }
785
786         /* use backend default access if no backend acls */
787         if( be != NULL && be->be_acl == NULL ) {
788                 Debug( LDAP_DEBUG_ACL,
789                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
790                         access2str( ACL_WRITE ),
791                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
792
793                 return be->be_dfltaccess >= ACL_WRITE;
794
795 #ifdef notdef
796         /* be is always non-NULL */
797         /* use global default access if no global acls */
798         } else if ( be == NULL && global_acl == NULL ) {
799                 Debug( LDAP_DEBUG_ACL,
800                         "=> access_allowed: global default %s access %s to \"%s\"\n",
801                         access2str( ACL_WRITE ),
802                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
803
804                 return global_default_access >= ACL_WRITE;
805 #endif
806         }
807
808         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
809                 /*
810                  * no-user-modification operational attributes are ignored
811                  * by ACL_WRITE checking as any found here are not provided
812                  * by the user
813                  */
814                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
815                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
816                                 " modify access granted\n",
817                                 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
818                         continue;
819                 }
820
821                 switch ( mlist->sml_op ) {
822                 case LDAP_MOD_REPLACE:
823                 case LDAP_MOD_ADD:
824                         if ( mlist->sml_bvalues == NULL ) {
825                                 break;
826                         }
827                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
828                                 if ( ! access_allowed( be, conn, op, e,
829                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
830                                 {
831                                         return( 0 );
832                                 }
833                         }
834                         break;
835
836                 case LDAP_MOD_DELETE:
837                         if ( mlist->sml_bvalues == NULL ) {
838                                 if ( ! access_allowed( be, conn, op, e,
839                                         mlist->sml_desc, NULL, ACL_WRITE ) )
840                                 {
841                                         return( 0 );
842                                 }
843                                 break;
844                         }
845                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
846                                 if ( ! access_allowed( be, conn, op, e,
847                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
848                                 {
849                                         return( 0 );
850                                 }
851                         }
852                         break;
853                 }
854         }
855
856         return( 1 );
857 }
858
859 #ifdef SLAPD_ACI_ENABLED
860 static char *
861 aci_bvstrdup( struct berval *bv )
862 {
863         char *s;
864
865         s = (char *)ch_malloc(bv->bv_len + 1);
866         if (s != NULL) {
867                 memcpy(s, bv->bv_val, bv->bv_len);
868                 s[bv->bv_len] = 0;
869         }
870         return(s);
871 }
872
873 static int
874 aci_strbvcmp(
875         const char *s,
876         struct berval *bv )
877 {
878         int res, len;
879
880         res = strncasecmp( s, bv->bv_val, bv->bv_len );
881         if (res)
882                 return(res);
883         len = strlen(s);
884         if (len > (int)bv->bv_len)
885                 return(1);
886         if (len < (int)bv->bv_len)
887                 return(-1);
888         return(0);
889 }
890
891 static int
892 aci_get_part(
893         struct berval *list,
894         int ix,
895         char sep,
896         struct berval *bv )
897 {
898         int len;
899         char *p;
900
901         if (bv) {
902                 bv->bv_len = 0;
903                 bv->bv_val = NULL;
904         }
905         len = list->bv_len;
906         p = list->bv_val;
907         while (len >= 0 && --ix >= 0) {
908                 while (--len >= 0 && *p++ != sep) ;
909         }
910         while (len >= 0 && *p == ' ') {
911                 len--;
912                 p++;
913         }
914         if (len < 0)
915                 return(-1);
916
917         if (!bv)
918                 return(0);
919
920         bv->bv_val = p;
921         while (--len >= 0 && *p != sep) {
922                 bv->bv_len++;
923                 p++;
924         }
925         while (bv->bv_len > 0 && *--p == ' ')
926                 bv->bv_len--;
927         return(bv->bv_len);
928 }
929
930 static int
931 aci_list_map_rights(
932         struct berval *list )
933 {
934         struct berval bv;
935         slap_access_t mask;
936         int i;
937
938         ACL_INIT(mask);
939         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
940                 if (bv.bv_len <= 0)
941                         continue;
942                 switch (*bv.bv_val) {
943                 case 'c':
944                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
945                         break;
946                 case 's':
947                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
948                          * the right 's' to mean "set", but in the examples states
949                          * that the right 's' means "search".  The latter definition
950                          * is used here.
951                          */
952                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
953                         break;
954                 case 'r':
955                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
956                         break;
957                 case 'w':
958                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
959                         break;
960                 case 'x':
961                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
962                          * define any equivalent to the AUTH right, so I've just used
963                          * 'x' for now.
964                          */
965                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
966                         break;
967                 default:
968                         break;
969                 }
970
971         }
972         return(mask);
973 }
974
975 static int
976 aci_list_has_attr(
977         struct berval *list,
978         const char *attr,
979         struct berval *val )
980 {
981         struct berval bv, left, right;
982         int i;
983
984         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
985                 if (aci_get_part(&bv, 0, '=', &left) < 0
986                         || aci_get_part(&bv, 1, '=', &right) < 0)
987                 {
988                         if (aci_strbvcmp(attr, &bv) == 0)
989                                 return(1);
990                 } else if (val == NULL) {
991                         if (aci_strbvcmp(attr, &left) == 0)
992                                 return(1);
993                 } else {
994                         if (aci_strbvcmp(attr, &left) == 0) {
995                                 /* this is experimental code that implements a
996                                  * simple (prefix) match of the attribute value.
997                                  * the ACI draft does not provide for aci's that
998                                  * apply to specific values, but it would be
999                                  * nice to have.  If the <attr> part of an aci's
1000                                  * rights list is of the form <attr>=<value>,
1001                                  * that means the aci applies only to attrs with
1002                                  * the given value.  Furthermore, if the attr is
1003                                  * of the form <attr>=<value>*, then <value> is
1004                                  * treated as a prefix, and the aci applies to 
1005                                  * any value with that prefix.
1006                                  *
1007                                  * Ideally, this would allow r.e. matches.
1008                                  */
1009                                 if (aci_get_part(&right, 0, '*', &left) < 0
1010                                         || right.bv_len <= left.bv_len)
1011                                 {
1012                                         if (aci_strbvcmp(val->bv_val, &right) == 0)
1013                                                 return(1);
1014                                 } else if (val->bv_len >= left.bv_len) {
1015                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1016                                                 return(1);
1017                                 }
1018                         }
1019                 }
1020         }
1021         return(0);
1022 }
1023
1024 static slap_access_t
1025 aci_list_get_attr_rights(
1026         struct berval *list,
1027         const char *attr,
1028         struct berval *val )
1029 {
1030     struct berval bv;
1031     slap_access_t mask;
1032     int i;
1033
1034         /* loop through each rights/attr pair, skip first part (action) */
1035         ACL_INIT(mask);
1036         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1037                 if (aci_list_has_attr(&bv, attr, val) == 0)
1038                         continue;
1039                 if (aci_get_part(list, i, ';', &bv) < 0)
1040                         continue;
1041                 mask |= aci_list_map_rights(&bv);
1042         }
1043         return(mask);
1044 }
1045
1046 static int
1047 aci_list_get_rights(
1048         struct berval *list,
1049         const char *attr,
1050         struct berval *val,
1051         slap_access_t *grant,
1052         slap_access_t *deny )
1053 {
1054     struct berval perm, actn;
1055     slap_access_t *mask;
1056     int i, found;
1057
1058         if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1059                 attr = "[entry]";
1060         }
1061
1062         found = 0;
1063         ACL_INIT(*grant);
1064         ACL_INIT(*deny);
1065         /* loop through each permissions clause */
1066         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1067                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1068                         continue;
1069                 if (aci_strbvcmp( "grant", &actn ) == 0) {
1070                         mask = grant;
1071                 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1072                         mask = deny;
1073                 } else {
1074                         continue;
1075                 }
1076
1077                 found = 1;
1078                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1079                 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1080         }
1081         return(found);
1082 }
1083
1084 static int
1085 aci_group_member (
1086         struct berval *subj,
1087         const char *defgrpoc,
1088         const char *defgrpat,
1089     Backend             *be,
1090     Entry               *e,
1091     Operation           *op,
1092         regmatch_t      *matches
1093 )
1094 {
1095         struct berval bv;
1096         char *subjdn, *grpdn = NULL;
1097         char *grpoc;
1098         char *grpat;
1099         ObjectClass *grp_oc = NULL;
1100         AttributeDescription *grp_ad = NULL;
1101         char *text;
1102         int rc;
1103
1104         /* format of string is "group/objectClassValue/groupAttrName" */
1105         if (aci_get_part(subj, 0, '/', &bv) < 0) {
1106                 return(0);
1107         }
1108
1109         subjdn = aci_bvstrdup(&bv);
1110         if (subjdn == NULL) {
1111                 return(0);
1112         }
1113
1114         if (aci_get_part(subj, 1, '/', &bv) < 0) {
1115                 grpoc = ch_strdup( defgrpoc );
1116         } else {
1117                 grpoc = aci_bvstrdup(&bv);
1118         }
1119
1120         if (aci_get_part(subj, 2, '/', &bv) < 0) {
1121                 grpat = ch_strdup( defgrpat );
1122         } else {
1123                 grpat = aci_bvstrdup(&bv);
1124         }
1125
1126         rc = slap_str2ad( grpat, &grp_ad, &text );
1127         if( rc != LDAP_SUCCESS ) {
1128                 rc = 0;
1129                 goto done;
1130         }
1131         rc = 0;
1132
1133         grp_oc = oc_find( grpoc );
1134         grpdn = (char *)ch_malloc(1024);
1135
1136         if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1137                 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1138                 if ( dn_normalize(grpdn) != NULL ) {
1139                         rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1140                 }
1141         }
1142
1143 done:
1144         if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1145         ch_free(grpdn);
1146         ch_free(grpat);
1147         ch_free(grpoc);
1148         ch_free(subjdn);
1149         return(rc);
1150 }
1151
1152 static int
1153 aci_mask(
1154     Backend                     *be,
1155     Connection          *conn,
1156     Operation           *op,
1157     Entry                       *e,
1158         AttributeDescription *desc,
1159     struct berval       *val,
1160     struct berval       *aci,
1161         regmatch_t              *matches,
1162         slap_access_t   *grant,
1163         slap_access_t   *deny
1164 )
1165 {
1166     struct berval bv, perms, sdn;
1167     char *subjdn;
1168         int rc;
1169         char *attr = desc->ad_cname->bv_val;
1170
1171         /* parse an aci of the form:
1172                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1173
1174            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1175            a full description of the format for this attribute.
1176
1177            For now, this routine only supports scope=entry.
1178          */
1179
1180         /* check that the aci has all 5 components */
1181         if (aci_get_part(aci, 4, '#', NULL) < 0)
1182                 return(0);
1183
1184         /* check that the aci family is supported */
1185         if (aci_get_part(aci, 0, '#', &bv) < 0)
1186                 return(0);
1187
1188         /* check that the scope is "entry" */
1189         if (aci_get_part(aci, 1, '#', &bv) < 0
1190                 || aci_strbvcmp( "entry", &bv ) != 0)
1191         {
1192                 return(0);
1193         }
1194
1195         /* get the list of permissions clauses, bail if empty */
1196         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1197                 return(0);
1198
1199         /* check if any permissions allow desired access */
1200         if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1201                 return(0);
1202
1203         /* see if we have a DN match */
1204         if (aci_get_part(aci, 3, '#', &bv) < 0)
1205                 return(0);
1206
1207         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1208                 return(0);
1209
1210         if (aci_strbvcmp( "access-id", &bv ) == 0) {
1211                 subjdn = aci_bvstrdup(&sdn);
1212                 if (subjdn == NULL)
1213                         return(0);
1214                 rc = 1;
1215                 if ( dn_normalize(subjdn) != NULL )
1216                         if (strcasecmp(op->o_ndn, subjdn) != 0)
1217                                 rc = 0;
1218                 ch_free(subjdn);
1219                 return(rc);
1220         }
1221
1222         if (aci_strbvcmp( "self", &bv ) == 0) {
1223                 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1224                         return(1);
1225
1226         } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1227                 char *dnattr = aci_bvstrdup(&sdn);
1228                 Attribute *at;
1229                 AttributeDescription *ad = NULL;
1230                 const char *text;
1231
1232                 rc = slap_str2ad( dnattr, &ad, &text );
1233                 ch_free( dnattr );
1234
1235                 if( rc != LDAP_SUCCESS ) {
1236                         return 0;
1237                 }
1238
1239                 rc = 0;
1240
1241                 bv.bv_val = op->o_ndn;
1242                 bv.bv_len = strlen( bv.bv_val );
1243
1244                 for(at = attrs_find( e->e_attrs, ad );
1245                         at != NULL;
1246                         at = attrs_find( at->a_next, ad ) )
1247                 {
1248                         if (value_find( ad, at->a_vals, &bv) == 0 ) {
1249                                 rc = 1;
1250                                 break;
1251                         }
1252                 }
1253
1254                 ad_free( ad, 1 );
1255                 return rc;
1256
1257
1258         } else if (aci_strbvcmp( "group", &bv ) == 0) {
1259                 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
1260                         return(1);
1261
1262         } else if (aci_strbvcmp( "role", &bv ) == 0) {
1263                 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
1264                         return(1);
1265
1266         }
1267
1268         return(0);
1269 }
1270
1271 #endif  /* SLAPD_ACI_ENABLED */
1272
1273 static void
1274 string_expand(
1275         char *newbuf,
1276         int bufsiz,
1277         char *pat,
1278         char *match,
1279         regmatch_t *matches)
1280 {
1281         int     size;
1282         char   *sp;
1283         char   *dp;
1284         int     flag;
1285
1286         size = 0;
1287         newbuf[0] = '\0';
1288         bufsiz--; /* leave space for lone $ */
1289
1290         flag = 0;
1291         for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1292                 /* did we previously see a $ */
1293                 if (flag) {
1294                         if (*sp == '$') {
1295                                 *dp++ = '$';
1296                                 size++;
1297                         } else if (*sp >= '0' && *sp <= '9' ) {
1298                                 int     n;
1299                                 int     i;
1300                                 int     l;
1301
1302                                 n = *sp - '0';
1303                                 *dp = '\0';
1304                                 i = matches[n].rm_so;
1305                                 l = matches[n].rm_eo; 
1306                                 for ( ; size < 512 && i < l; size++, i++ ) {
1307                                         *dp++ = match[i];
1308                                         size++;
1309                                 }
1310                                 *dp = '\0';
1311                         }
1312                         flag = 0;
1313                 } else {
1314                         if (*sp == '$') {
1315                                 flag = 1;
1316                         } else {
1317                                 *dp++ = *sp;
1318                                 size++;
1319                         }
1320                 }
1321         }
1322
1323         if (flag) {
1324                 /* must have ended with a single $ */
1325                 *dp++ = '$';
1326                 size++;
1327         }
1328
1329         *dp = '\0';
1330
1331         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %s\n", pat, 0, 0 );
1332         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1333 }
1334
1335 static int
1336 regex_matches(
1337         char *pat,                              /* pattern to expand and match against */
1338         char *str,                              /* string to match against pattern */
1339         char *buf,                              /* buffer with $N expansion variables */
1340         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1341 )
1342 {
1343         regex_t re;
1344         char newbuf[512];
1345         int     rc;
1346
1347         if(str == NULL) str = "";
1348
1349         string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1350         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1351                 char error[512];
1352                 regerror(rc, &re, error, sizeof(error));
1353
1354                 Debug( LDAP_DEBUG_TRACE,
1355                     "compile( \"%s\", \"%s\") failed %s\n",
1356                         pat, str, error );
1357                 return( 0 );
1358         }
1359
1360         rc = regexec(&re, str, 0, NULL, 0);
1361         regfree( &re );
1362
1363         Debug( LDAP_DEBUG_TRACE,
1364             "=> regex_matches: string:   %s\n", str, 0, 0 );
1365         Debug( LDAP_DEBUG_TRACE,
1366             "=> regex_matches: rc: %d %s\n",
1367                 rc, !rc ? "matches" : "no matches", 0 );
1368         return( !rc );
1369 }
1370