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