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