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