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