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