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