]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
Eliminate second session protocol version field.
[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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
717                                         &mlist->sml_desc,
718 #else
719                                         mlist->sml_type,
720 #endif
721                                         mlist->sml_bvalues[i], ACL_WRITE ) )
722                                 {
723                                         return( 0 );
724                                 }
725                         }
726                         break;
727
728                 case LDAP_MOD_DELETE:
729                         if ( mlist->sml_bvalues == NULL ) {
730                                 if ( ! access_allowed( be, conn, op, e,
731 #ifdef SLAPD_SCHEMA_NOT_COMPAT
732                                         &mlist->sml_desc,
733 #else
734                                         mlist->sml_type,
735 #endif
736                                         NULL,  ACL_WRITE ) )
737                                 {
738                                         return( 0 );
739                                 }
740                                 break;
741                         }
742                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
743                                 if ( ! access_allowed( be, conn, op, e,
744 #ifdef SLAPD_SCHEMA_NOT_COMPAT
745                                         &mlist->sml_desc,
746 #else
747                                         mlist->sml_type,
748 #endif
749                                         mlist->sml_bvalues[i], ACL_WRITE ) )
750                                 {
751                                         return( 0 );
752                                 }
753                         }
754                         break;
755                 }
756         }
757
758         return( 1 );
759 }
760
761 #ifdef SLAPD_ACI_ENABLED
762 static char *
763 aci_bvstrdup( struct berval *bv )
764 {
765         char *s;
766
767         s = (char *)ch_malloc(bv->bv_len + 1);
768         if (s != NULL) {
769                 memcpy(s, bv->bv_val, bv->bv_len);
770                 s[bv->bv_len] = 0;
771         }
772         return(s);
773 }
774
775 static int
776 aci_strbvcmp(
777         const char *s,
778         struct berval *bv )
779 {
780         int res, len;
781
782         res = strncasecmp( s, bv->bv_val, bv->bv_len );
783         if (res)
784                 return(res);
785         len = strlen(s);
786         if (len > (int)bv->bv_len)
787                 return(1);
788         if (len < (int)bv->bv_len)
789                 return(-1);
790         return(0);
791 }
792
793 static int
794 aci_get_part(
795         struct berval *list,
796         int ix,
797         char sep,
798         struct berval *bv )
799 {
800         int len;
801         char *p;
802
803         if (bv) {
804                 bv->bv_len = 0;
805                 bv->bv_val = NULL;
806         }
807         len = list->bv_len;
808         p = list->bv_val;
809         while (len >= 0 && --ix >= 0) {
810                 while (--len >= 0 && *p++ != sep) ;
811         }
812         while (len >= 0 && *p == ' ') {
813                 len--;
814                 p++;
815         }
816         if (len < 0)
817                 return(-1);
818
819         if (!bv)
820                 return(0);
821
822         bv->bv_val = p;
823         while (--len >= 0 && *p != sep) {
824                 bv->bv_len++;
825                 p++;
826         }
827         while (bv->bv_len > 0 && *--p == ' ')
828                 bv->bv_len--;
829         return(bv->bv_len);
830 }
831
832 static int
833 aci_list_map_rights(
834         struct berval *list )
835 {
836         struct berval bv;
837         slap_access_t mask;
838         int i;
839
840         ACL_INIT(mask);
841         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
842                 if (bv.bv_len <= 0)
843                         continue;
844                 switch (*bv.bv_val) {
845                 case 'c':
846                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
847                         break;
848                 case 's':
849                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
850                          * the right 's' to mean "set", but in the examples states
851                          * that the right 's' means "search".  The latter definition
852                          * is used here.
853                          */
854                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
855                         break;
856                 case 'r':
857                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
858                         break;
859                 case 'w':
860                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
861                         break;
862                 case 'x':
863                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
864                          * define any equivalent to the AUTH right, so I've just used
865                          * 'x' for now.
866                          */
867                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
868                         break;
869                 default:
870                         break;
871                 }
872
873         }
874         return(mask);
875 }
876
877 static int
878 aci_list_has_attr(
879         struct berval *list,
880         const char *attr,
881         struct berval *val )
882 {
883         struct berval bv, left, right;
884         int i;
885
886         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
887                 if (aci_get_part(&bv, 0, '=', &left) < 0
888                         || aci_get_part(&bv, 1, '=', &right) < 0)
889                 {
890                         if (aci_strbvcmp(attr, &bv) == 0)
891                                 return(1);
892                 } else if (val == NULL) {
893                         if (aci_strbvcmp(attr, &left) == 0)
894                                 return(1);
895                 } else {
896                         if (aci_strbvcmp(attr, &left) == 0) {
897                                 /* this is experimental code that implements a
898                                  * simple (prefix) match of the attribute value.
899                                  * the ACI draft does not provide for aci's that
900                                  * apply to specific values, but it would be
901                                  * nice to have.  If the <attr> part of an aci's
902                                  * rights list is of the form <attr>=<value>,
903                                  * that means the aci applies only to attrs with
904                                  * the given value.  Furthermore, if the attr is
905                                  * of the form <attr>=<value>*, then <value> is
906                                  * treated as a prefix, and the aci applies to 
907                                  * any value with that prefix.
908                                  *
909                                  * Ideally, this would allow r.e. matches.
910                                  */
911                                 if (aci_get_part(&right, 0, '*', &left) < 0
912                                         || right.bv_len <= left.bv_len)
913                                 {
914                                         if (aci_strbvcmp(val->bv_val, &right) == 0)
915                                                 return(1);
916                                 } else if (val->bv_len >= left.bv_len) {
917                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
918                                                 return(1);
919                                 }
920                         }
921                 }
922         }
923         return(0);
924 }
925
926 static slap_access_t
927 aci_list_get_attr_rights(
928         struct berval *list,
929         const char *attr,
930         struct berval *val )
931 {
932     struct berval bv;
933     slap_access_t mask;
934     int i;
935
936         /* loop through each rights/attr pair, skip first part (action) */
937         ACL_INIT(mask);
938         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
939                 if (aci_list_has_attr(&bv, attr, val) == 0)
940                         continue;
941                 if (aci_get_part(list, i, ';', &bv) < 0)
942                         continue;
943                 mask |= aci_list_map_rights(&bv);
944         }
945         return(mask);
946 }
947
948 static int
949 aci_list_get_rights(
950         struct berval *list,
951         const char *attr,
952         struct berval *val,
953         slap_access_t *grant,
954         slap_access_t *deny )
955 {
956     struct berval perm, actn;
957     slap_access_t *mask;
958     int i, found;
959
960         if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
961                 attr = "[entry]";
962         }
963
964         found = 0;
965         ACL_INIT(*grant);
966         ACL_INIT(*deny);
967         /* loop through each permissions clause */
968         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
969                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
970                         continue;
971                 if (aci_strbvcmp( "grant", &actn ) == 0) {
972                         mask = grant;
973                 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
974                         mask = deny;
975                 } else {
976                         continue;
977                 }
978
979                 found = 1;
980                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
981                 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
982         }
983         return(found);
984 }
985
986 static int
987 aci_group_member (
988         struct berval *subj,
989         char *grpoc,
990         char *grpat,
991     Backend             *be,
992     Entry               *e,
993     Operation           *op,
994         regmatch_t      *matches
995 )
996 {
997         struct berval bv;
998         char *subjdn, *grpdn;
999         int rc = 0;
1000
1001         /* format of string is "group/objectClassValue/groupAttrName" */
1002         if (aci_get_part(subj, 0, '/', &bv) < 0)
1003                 return(0);
1004         subjdn = aci_bvstrdup(&bv);
1005         if (subjdn == NULL)
1006                 return(0);
1007
1008         if (aci_get_part(subj, 1, '/', &bv) < 0)
1009                 grpoc = ch_strdup(grpoc);
1010         else
1011                 grpoc = aci_bvstrdup(&bv);
1012
1013         if (aci_get_part(subj, 2, '/', &bv) < 0)
1014                 grpat = ch_strdup(grpat);
1015         else
1016                 grpat = aci_bvstrdup(&bv);
1017
1018         grpdn = (char *)ch_malloc(1024);
1019         if (grpoc != NULL && grpat != NULL && grpdn != NULL) {
1020                 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1021                 if ( dn_normalize(grpdn) != NULL ) {
1022                         rc = (backend_group(be, e, grpdn, op->o_ndn, grpoc, grpat) == 0);
1023                 }
1024                 ch_free(grpdn);
1025         }
1026         if (grpat != NULL)
1027                 ch_free(grpat);
1028         if (grpoc != NULL)
1029                 ch_free(grpoc);
1030         ch_free(subjdn);
1031         return(rc);
1032 }
1033
1034 static int
1035 aci_mask(
1036     Backend                     *be,
1037     Operation           *op,
1038     Entry                       *e,
1039 #ifdef SLAPD_SCHEMA_NOT_COMPAT
1040         AttributeDescription *desc,
1041 #else
1042     const char          *attr,
1043 #endif
1044     struct berval       *val,
1045     struct berval       *aci,
1046         regmatch_t              *matches,
1047         slap_access_t   *grant,
1048         slap_access_t   *deny
1049 )
1050 {
1051     struct berval bv, perms, sdn;
1052     char *subjdn;
1053         int rc, i;
1054
1055         /* parse an aci of the form:
1056                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1057
1058            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1059            a full description of the format for this attribute.
1060
1061            For now, this routine only supports scope=entry.
1062          */
1063
1064         /* check that the aci has all 5 components */
1065         if (aci_get_part(aci, 4, '#', NULL) < 0)
1066                 return(0);
1067
1068         /* check that the aci family is supported */
1069         if (aci_get_part(aci, 0, '#', &bv) < 0)
1070                 return(0);
1071         for (i = 0; supportedACIMechs[i] != NULL; i++) {
1072                 if (aci_strbvcmp( supportedACIMechs[i], &bv ) == 0)
1073                         break;
1074         }
1075         if (supportedACIMechs[i] == NULL)
1076                 return(0);
1077
1078         /* check that the scope is "entry" */
1079         if (aci_get_part(aci, 1, '#', &bv) < 0
1080                 || aci_strbvcmp( "entry", &bv ) != 0)
1081         {
1082                 return(0);
1083         }
1084
1085         /* get the list of permissions clauses, bail if empty */
1086         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1087                 return(0);
1088
1089         /* check if any permissions allow desired access */
1090         if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1091                 return(0);
1092
1093         /* see if we have a DN match */
1094         if (aci_get_part(aci, 3, '#', &bv) < 0)
1095                 return(0);
1096
1097         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1098                 return(0);
1099
1100         if (aci_strbvcmp( "access-id", &bv ) == 0) {
1101                 subjdn = aci_bvstrdup(&sdn);
1102                 if (subjdn == NULL)
1103                         return(0);
1104                 rc = 1;
1105                 if ( dn_normalize(subjdn) != NULL )
1106                         if (strcasecmp(op->o_ndn, subjdn) != 0)
1107                                 rc = 0;
1108                 ch_free(subjdn);
1109                 return(rc);
1110         }
1111
1112         if (aci_strbvcmp( "self", &bv ) == 0) {
1113                 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1114                         return(1);
1115
1116         } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1117                 Attribute *at;
1118                 char *attrname = aci_bvstrdup(&sdn);
1119                 at = attr_find(e->e_attrs, attrname);
1120                 ch_free(attrname);
1121
1122                 if (at != NULL) {
1123                         bv.bv_val = op->o_ndn;
1124                         bv.bv_len = strlen( bv.bv_val );
1125
1126 #ifdef SLAPD_SCHEMA_NOT_COMPAT
1127                         /* not yet implemented */
1128 #else
1129                         if (value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
1130                                 return(1);
1131 #endif
1132                 }
1133
1134         } else if (aci_strbvcmp( "group", &bv ) == 0) {
1135                 if (aci_group_member(&sdn, "groupOfNames", "member", be, e, op, matches))
1136                         return(1);
1137
1138         } else if (aci_strbvcmp( "role", &bv ) == 0) {
1139                 if (aci_group_member(&sdn, "organizationalRole", "roleOccupant", be, e, op, matches))
1140                         return(1);
1141         }
1142
1143         return(0);
1144 }
1145
1146 char *
1147 get_supported_acimech(
1148         int index )
1149 {
1150         if (index < 0 || index >= (sizeof(supportedACIMechs) / sizeof(char *)))
1151                 return(NULL);
1152         return(supportedACIMechs[index]);
1153 }
1154
1155 #endif  /* SLAPD_ACI_ENABLED */
1156
1157 static void
1158 string_expand(
1159         char *newbuf,
1160         int bufsiz,
1161         char *pat,
1162         char *match,
1163         regmatch_t *matches)
1164 {
1165         int     size;
1166         char   *sp;
1167         char   *dp;
1168         int     flag;
1169
1170         size = 0;
1171         newbuf[0] = '\0';
1172         bufsiz--; /* leave space for lone $ */
1173
1174         flag = 0;
1175         for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1176                 /* did we previously see a $ */
1177                 if (flag) {
1178                         if (*sp == '$') {
1179                                 *dp++ = '$';
1180                                 size++;
1181                         } else if (*sp >= '0' && *sp <= '9' ) {
1182                                 int     n;
1183                                 int     i;
1184                                 int     l;
1185
1186                                 n = *sp - '0';
1187                                 *dp = '\0';
1188                                 i = matches[n].rm_so;
1189                                 l = matches[n].rm_eo; 
1190                                 for ( ; size < 512 && i < l; size++, i++ ) {
1191                                         *dp++ = match[i];
1192                                         size++;
1193                                 }
1194                                 *dp = '\0';
1195                         }
1196                         flag = 0;
1197                 } else {
1198                         if (*sp == '$') {
1199                                 flag = 1;
1200                         } else {
1201                                 *dp++ = *sp;
1202                                 size++;
1203                         }
1204                 }
1205         }
1206
1207         if (flag) {
1208                 /* must have ended with a single $ */
1209                 *dp++ = '$';
1210                 size++;
1211         }
1212
1213         *dp = '\0';
1214
1215         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %s\n", pat, 0, 0 );
1216         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1217 }
1218
1219 static int
1220 regex_matches(
1221         char *pat,                              /* pattern to expand and match against */
1222         char *str,                              /* string to match against pattern */
1223         char *buf,                              /* buffer with $N expansion variables */
1224         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1225 )
1226 {
1227         regex_t re;
1228         char newbuf[512];
1229         int     rc;
1230
1231         if(str == NULL) str = "";
1232
1233         string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1234         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1235                 char error[512];
1236                 regerror(rc, &re, error, sizeof(error));
1237
1238                 Debug( LDAP_DEBUG_TRACE,
1239                     "compile( \"%s\", \"%s\") failed %s\n",
1240                         pat, str, error );
1241                 return( 0 );
1242         }
1243
1244         rc = regexec(&re, str, 0, NULL, 0);
1245         regfree( &re );
1246
1247         Debug( LDAP_DEBUG_TRACE,
1248             "=> regex_matches: string:   %s\n", str, 0, 0 );
1249         Debug( LDAP_DEBUG_TRACE,
1250             "=> regex_matches: rc: %d %s\n",
1251                 rc, !rc ? "matches" : "no matches", 0 );
1252         return( !rc );
1253 }
1254