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