]> git.sur5r.net Git - openldap/blob - servers/slapd/acl.c
3efffe79be57038d3cf8b2bb26d0e73bf528c534
[openldap] / servers / slapd / acl.c
1 /* acl.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/regex.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15
16 #include "slap.h"
17 #include "sets.h"
18
19 static AccessControl * acl_get(
20         AccessControl *ac, int *count,
21         Backend *be, Operation *op,
22         Entry *e,
23         AttributeDescription *desc,
24         int nmatches, regmatch_t *matches );
25
26 static slap_control_t acl_mask(
27         AccessControl *ac, slap_access_mask_t *mask,
28         Backend *be, Connection *conn, Operation *op,
29         Entry *e,
30         AttributeDescription *desc,
31         struct berval *val,
32         regmatch_t *matches );
33
34 #ifdef SLAPD_ACI_ENABLED
35 static int aci_mask(
36         Backend *be,
37     Connection *conn,
38         Operation *op,
39         Entry *e,
40         AttributeDescription *desc,
41         struct berval *val,
42         struct berval *aci,
43         regmatch_t *matches,
44         slap_access_t *grant,
45         slap_access_t *deny );
46 #endif
47
48 static int      regex_matches(
49         char *pat, char *str, char *buf, regmatch_t *matches);
50 static void     string_expand(
51         char *newbuf, int bufsiz, char *pattern,
52         char *match, regmatch_t *matches);
53
54 char **aci_set_gather (void *cookie, char *name, char *attr);
55 static int aci_match_set ( struct berval *subj, Backend *be,
56     Entry *e, Connection *conn, Operation *op, int setref );
57
58 /*
59  * access_allowed - check whether op->o_ndn is allowed the requested access
60  * to entry e, attribute attr, value val.  if val is null, access to
61  * the whole attribute is assumed (all values).
62  *
63  * This routine loops through all access controls and calls
64  * acl_mask() on each applicable access control.
65  * The loop exits when a definitive answer is reached or
66  * or no more controls remain.
67  *
68  * returns:
69  *              0       access denied
70  *              1       access granted
71  */
72
73 int
74 access_allowed(
75     Backend             *be,
76     Connection          *conn,
77     Operation           *op,
78     Entry               *e,
79         AttributeDescription    *desc,
80     struct berval       *val,
81     slap_access_t       access )
82 {
83         int                             count;
84         AccessControl   *a;
85 #ifdef LDAP_DEBUG
86         char accessmaskbuf[ACCESSMASK_MAXLEN];
87 #endif
88         slap_access_mask_t mask;
89         slap_control_t control;
90
91         const char *attr = desc ? desc->ad_cname->bv_val : NULL;
92
93         regmatch_t       matches[MAXREMATCHES];
94
95         Debug( LDAP_DEBUG_ACL,
96                 "=> access_allowed: %s access to \"%s\" \"%s\" requested\n",
97             access2str( access ),
98                 e->e_dn, attr );
99
100         assert( be != NULL );
101         assert( e != NULL );
102         assert( attr != NULL );
103         assert( access > ACL_NONE );
104
105         /* grant database root access */
106         if ( be != NULL && be_isroot( be, op->o_ndn ) ) {
107                 Debug( LDAP_DEBUG_ACL,
108                     "<= root access granted\n",
109                         0, 0, 0 );
110                 return 1;
111         }
112
113         /*
114          * no-user-modification operational attributes are ignored
115          * by ACL_WRITE checking as any found here are not provided
116          * by the user
117          */
118         if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type ) )
119         {
120                 Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
121                         " %s access granted\n",
122                         attr, 0, 0 );
123                 return 1;
124         }
125
126         /* use backend default access if no backend acls */
127         if( be != NULL && be->be_acl == NULL ) {
128                 Debug( LDAP_DEBUG_ACL,
129                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
130                         access2str( access ),
131                         be->be_dfltaccess >= access ? "granted" : "denied", op->o_dn );
132
133                 return be->be_dfltaccess >= access;
134
135 #ifdef notdef
136         /* be is always non-NULL */
137         /* use global default access if no global acls */
138         } else if ( be == NULL && global_acl == NULL ) {
139                 Debug( LDAP_DEBUG_ACL,
140                         "=> access_allowed: global default %s access %s to \"%s\"\n",
141                         access2str( access ),
142                         global_default_access >= access ? "granted" : "denied", op->o_dn );
143
144                 return global_default_access >= access;
145 #endif
146         }
147
148         ACL_INIT(mask);
149         memset(matches, '\0', sizeof(matches));
150         
151         control = ACL_BREAK;
152         a = NULL;
153         count = 0;
154
155         while( a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches ) )
156         {
157                 int i;
158
159                 for (i = 0; i < MAXREMATCHES && matches[i].rm_so > 0; i++) {
160                         Debug( LDAP_DEBUG_ACL, "=> match[%d]: %d %d ", i,
161                                (int)matches[i].rm_so, (int)matches[i].rm_eo );
162
163                         if( matches[i].rm_so <= matches[0].rm_eo ) {
164                                 int n;
165                                 for ( n = matches[i].rm_so; n < matches[i].rm_eo; n++) {
166                                         Debug( LDAP_DEBUG_ACL, "%c", e->e_ndn[n], 0, 0 );
167                                 }
168                         }
169                         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
170                 }
171
172                 control = acl_mask( a, &mask, be, conn, op,
173                         e, desc, val, matches );
174
175                 if ( control != ACL_BREAK ) {
176                         break;
177                 }
178
179                 memset(matches, '\0', sizeof(matches));
180         }
181
182         if ( ACL_IS_INVALID( mask ) ) {
183                 Debug( LDAP_DEBUG_ACL,
184                         "=> access_allowed: \"%s\" (%s) invalid!\n",
185                         e->e_dn, attr, 0 );
186                 ACL_INIT( mask );
187
188         } else if ( control == ACL_BREAK ) {
189                 Debug( LDAP_DEBUG_ACL,
190                         "=> access_allowed: no more rules\n", 0, 0, 0);
191                 ACL_INIT( mask );
192         }
193
194         Debug( LDAP_DEBUG_ACL,
195                 "=> access_allowed: %s access %s by %s\n",
196                 access2str( access ),
197                 ACL_GRANT(mask, access) ? "granted" : "denied",
198                 accessmask2str( mask, accessmaskbuf ) );
199
200         return ACL_GRANT(mask, access);
201 }
202
203 /*
204  * acl_get - return the acl applicable to entry e, attribute
205  * attr.  the acl returned is suitable for use in subsequent calls to
206  * acl_access_allowed().
207  */
208
209 static AccessControl *
210 acl_get(
211         AccessControl *a,
212         int                     *count,
213     Backend             *be,
214     Operation   *op,
215     Entry               *e,
216         AttributeDescription *desc,
217     int                 nmatch,
218     regmatch_t  *matches )
219 {
220         const char *attr;
221         int dnlen, patlen;
222
223         assert( e != NULL );
224         assert( count != NULL );
225
226         attr = desc ? desc->ad_cname->bv_val : NULL;
227
228         if( a == NULL ) {
229                 if( be == NULL ) {
230                         a = global_acl;
231                 } else {
232                         a = be->be_acl;
233                 }
234
235                 assert( a != NULL );
236
237         } else {
238                 a = a->acl_next;
239         }
240
241         dnlen = strlen(e->e_ndn);
242
243         for ( ; a != NULL; a = a->acl_next ) {
244                 (*count) ++;
245
246                 if (a->acl_dn_pat != NULL) {
247                         if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
248                                 Debug( LDAP_DEBUG_ACL, "=> dnpat: [%d] %s nsub: %d\n", 
249                                         *count, a->acl_dn_pat, (int) a->acl_dn_re.re_nsub );
250
251                                 if (regexec(&a->acl_dn_re, e->e_ndn, nmatch, matches, 0))
252                                         continue;
253
254                         } else {
255                                 Debug( LDAP_DEBUG_ACL, "=> dn: [%d] %s\n", 
256                                         *count, a->acl_dn_pat, 0 );
257
258                                 patlen = strlen( a->acl_dn_pat );
259                                 if ( dnlen < patlen )
260                                         continue;
261
262                                 if ( a->acl_dn_style == ACL_STYLE_BASE ) {
263                                         /* base dn -- entire object DN must match */
264                                         if ( dnlen != patlen )
265                                                 continue;
266
267                                 } else if ( a->acl_dn_style == ACL_STYLE_ONE ) {
268                                         char *rdn;
269                                         int rdnlen = -1;
270
271                                         if ( dnlen <= patlen )
272                                                 continue;
273
274                                         if ( e->e_ndn[dnlen - patlen - 1] != ',' )
275                                                 continue;
276
277                                         rdn = dn_rdn( NULL, e->e_ndn );
278                                         if ( rdn != NULL ) {
279                                                 rdnlen = strlen( rdn );
280                                                 ch_free( rdn );
281                                         }
282                                         if ( rdnlen != dnlen - patlen - 1 )
283                                                 continue;
284
285                                 } else if ( a->acl_dn_style == ACL_STYLE_SUBTREE ) {
286                                         if ( dnlen > patlen && e->e_ndn[dnlen - patlen - 1] != ',' )
287                                                 continue;
288
289                                 } else if ( a->acl_dn_style == ACL_STYLE_CHILDREN ) {
290                                         if ( dnlen <= patlen )
291                                                 continue;
292                                         if ( e->e_ndn[dnlen - patlen - 1] != ',' )
293                                                 continue;
294                                 }
295
296                                 if ( strcmp( a->acl_dn_pat, e->e_ndn + dnlen - patlen ) != 0 )
297                                         continue;
298                         }
299
300                         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] matched\n",
301                                 *count, 0, 0 );
302                 }
303
304                 if ( a->acl_filter != NULL ) {
305                         ber_int_t rc = test_filter( NULL, NULL, NULL, e, a->acl_filter );
306                         if ( rc != LDAP_COMPARE_TRUE ) {
307                                 continue;
308                         }
309                 }
310
311         Debug( LDAP_DEBUG_ACL, "=> acl_get: [%d] check attr %s\n",
312                         *count, attr, 0);
313
314                 if ( attr == NULL || a->acl_attrs == NULL ||
315                         ad_inlist( desc, a->acl_attrs ) )
316                 {
317                         Debug( LDAP_DEBUG_ACL,
318                                 "<= acl_get: [%d] acl %s attr: %s\n",
319                                 *count, e->e_dn, attr );
320                         return a;
321                 }
322                 matches[0].rm_so = matches[0].rm_eo = -1;
323         }
324
325         Debug( LDAP_DEBUG_ACL, "<= acl_get: done.\n", 0, 0, 0 );
326         return( NULL );
327 }
328
329
330 /*
331  * acl_mask - modifies mask based upon the given acl and the
332  * requested access to entry e, attribute attr, value val.  if val
333  * is null, access to the whole attribute is assumed (all values).
334  *
335  * returns      0       access NOT allowed
336  *              1       access allowed
337  */
338
339 static slap_control_t
340 acl_mask(
341     AccessControl       *a,
342         slap_access_mask_t *mask,
343     Backend             *be,
344     Connection  *conn,
345     Operation   *op,
346     Entry               *e,
347         AttributeDescription *desc,
348     struct berval       *val,
349         regmatch_t      *matches
350 )
351 {
352         int             i, odnlen, patlen;
353         Access  *b;
354 #ifdef LDAP_DEBUG
355         char accessmaskbuf[ACCESSMASK_MAXLEN];
356 #endif
357         const char *attr = desc ? desc->ad_cname->bv_val : NULL;
358
359         assert( a != NULL );
360         assert( mask != NULL );
361
362         Debug( LDAP_DEBUG_ACL,
363                 "=> acl_mask: access to entry \"%s\", attr \"%s\" requested\n",
364                 e->e_dn, attr, 0 );
365
366         Debug( LDAP_DEBUG_ACL,
367                 "=> acl_mask: to value \"%s\" by \"%s\", (%s) \n",
368                 val ? val->bv_val : "*",
369                 op->o_ndn ?  op->o_ndn : "",
370                 accessmask2str( *mask, accessmaskbuf ) );
371
372         for ( i = 1, b = a->acl_access; b != NULL; b = b->a_next, i++ ) {
373                 slap_access_mask_t oldmask, modmask;
374
375                 ACL_INVALIDATE( modmask );
376
377                 /* AND <who> clauses */
378                 if ( b->a_dn_pat != NULL ) {
379                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_pat: %s\n",
380                                 b->a_dn_pat, 0, 0);
381                         /*
382                          * if access applies to the entry itself, and the
383                          * user is bound as somebody in the same namespace as
384                          * the entry, OR the given dn matches the dn pattern
385                          */
386                         if ( strcmp( b->a_dn_pat, "anonymous" ) == 0 ) {
387                                 if (op->o_ndn != NULL && op->o_ndn[0] != '\0' ) {
388                                         continue;
389                                 }
390
391                         } else if ( strcmp( b->a_dn_pat, "users" ) == 0 ) {
392                                 if (op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
393                                         continue;
394                                 }
395
396                         } else if ( strcmp( b->a_dn_pat, "self" ) == 0 ) {
397                                 if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
398                                         continue;
399                                 }
400                                 
401                                 if ( e->e_dn == NULL || strcmp( e->e_ndn, op->o_ndn ) != 0 ) {
402                                         continue;
403                                 }
404
405                         } else if ( b->a_dn_style == ACL_STYLE_REGEX ) {
406                                 if ( strcmp( b->a_dn_pat, "*" ) != 0 ) {
407                                         int ret = regex_matches( b->a_dn_pat,
408                                                 op->o_ndn, e->e_ndn, matches );
409
410                                         if( ret == 0 ) {
411                                                 continue;
412                                         }
413                                 }
414
415                         } else {
416                                 if ( e->e_dn == NULL )
417                                         continue;
418
419                                 patlen = strlen( b->a_dn_pat );
420                                 odnlen = strlen( op->o_ndn );
421                                 if ( odnlen < patlen )
422                                         continue;
423
424                                 if ( b->a_dn_style == ACL_STYLE_BASE ) {
425                                         /* base dn -- entire object DN must match */
426                                         if ( odnlen != patlen )
427                                                 continue;
428
429                                 } else if ( b->a_dn_style == ACL_STYLE_ONE ) {
430                                         char *rdn;
431                                         int rdnlen = -1;
432
433                                         if ( odnlen <= patlen )
434                                                 continue;
435
436                                         if ( op->o_ndn[odnlen - patlen - 1] != ',' )
437                                                 continue;
438
439                                         rdn = dn_rdn( NULL, op->o_ndn );
440                                         if ( rdn != NULL ) {
441                                                 rdnlen = strlen( rdn );
442                                                 ch_free( rdn );
443                                         }
444                                         if ( rdnlen != odnlen - patlen - 1 )
445                                                 continue;
446
447                                 } else if ( b->a_dn_style == ACL_STYLE_SUBTREE ) {
448                                         if ( odnlen > patlen && op->o_ndn[odnlen - patlen - 1] != ',' )
449                                                 continue;
450
451                                 } else if ( b->a_dn_style == ACL_STYLE_CHILDREN ) {
452                                         if ( odnlen <= patlen )
453                                                 continue;
454                                         if ( op->o_ndn[odnlen - patlen - 1] != ',' )
455                                                 continue;
456                                 }
457
458                                 if ( strcmp( b->a_dn_pat, op->o_ndn + odnlen - patlen ) != 0 )
459                                         continue;
460
461                         }
462                 }
463
464                 if ( b->a_sockurl_pat != NULL ) {
465                         Debug( LDAP_DEBUG_ACL, "<= check a_sockurl_pat: %s\n",
466                                 b->a_sockurl_pat, 0, 0 );
467
468                         if ( strcmp( b->a_sockurl_pat, "*" ) != 0) {
469                                 if ( b->a_sockurl_style == ACL_STYLE_REGEX) {
470                                         if (!regex_matches( b->a_sockurl_pat, conn->c_listener_url,
471                                                         e->e_ndn, matches ) ) 
472                                         {
473                                                 continue;
474                                         }
475                                 } else {
476                                         if ( strcasecmp( b->a_sockurl_pat, conn->c_listener_url ) == 0 )
477                                                 continue;
478                                 }
479                         }
480                 }
481
482                 if ( b->a_domain_pat != NULL ) {
483                         Debug( LDAP_DEBUG_ACL, "<= check a_domain_pat: %s\n",
484                                 b->a_domain_pat, 0, 0 );
485
486                         if ( strcmp( b->a_domain_pat, "*" ) != 0) {
487                                 if ( b->a_domain_style == ACL_STYLE_REGEX) {
488                                         if (!regex_matches( b->a_domain_pat, conn->c_peer_domain,
489                                                         e->e_ndn, matches ) ) 
490                                         {
491                                                 continue;
492                                         }
493                                 } else {
494                                         if ( strcasecmp( b->a_domain_pat, conn->c_peer_domain ) == 0 )
495                                                 continue;
496                                 }
497                         }
498                 }
499
500                 if ( b->a_peername_pat != NULL ) {
501                         Debug( LDAP_DEBUG_ACL, "<= check a_peername_path: %s\n",
502                                 b->a_peername_pat, 0, 0 );
503
504                         if ( strcmp( b->a_peername_pat, "*" ) != 0) {
505                                 if ( b->a_peername_style == ACL_STYLE_REGEX) {
506                                         if (!regex_matches( b->a_peername_pat, conn->c_peer_name,
507                                                         e->e_ndn, matches ) ) 
508                                         {
509                                                 continue;
510                                         }
511                                 } else {
512                                         if ( strcasecmp( b->a_peername_pat, conn->c_peer_name ) == 0 )
513                                                 continue;
514                                 }
515                         }
516                 }
517
518                 if ( b->a_sockname_pat != NULL ) {
519                         Debug( LDAP_DEBUG_ACL, "<= check a_sockname_path: %s\n",
520                                 b->a_sockname_pat, 0, 0 );
521
522                         if ( strcmp( b->a_sockname_pat, "*" ) != 0) {
523                                 if ( b->a_sockname_style == ACL_STYLE_REGEX) {
524                                         if (!regex_matches( b->a_sockname_pat, conn->c_sock_name,
525                                                         e->e_ndn, matches ) ) 
526                                         {
527                                                 continue;
528                                         }
529                                 } else {
530                                         if ( strcasecmp( b->a_sockname_pat, conn->c_sock_name ) == 0 )
531                                                 continue;
532                                 }
533                         }
534                 }
535
536                 if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
537                         Attribute       *at;
538                         struct berval   bv;
539                         int rc, match = 0;
540                         const char *text;
541                         const char *desc = b->a_dn_at->ad_cname->bv_val;
542
543                         Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
544                                 desc, 0, 0);
545
546                         bv.bv_val = op->o_ndn;
547                         bv.bv_len = strlen( bv.bv_val );
548
549                         /* see if asker is listed in dnattr */
550                         for( at = attrs_find( e->e_attrs, b->a_dn_at );
551                                 at != NULL;
552                                 at = attrs_find( at->a_next, b->a_dn_at ) )
553                         {
554                                 if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
555                                         /* found it */
556                                         match = 1;
557                                         break;
558                                 }
559                         }
560
561                         if( match ) {
562                                 /* have a dnattr match. if this is a self clause then
563                                  * the target must also match the op dn.
564                                  */
565                                 if ( b->a_dn_self ) {
566                                         /* check if the target is an attribute. */
567                                         if ( val == NULL )
568                                                 continue;
569                                         /* target is attribute, check if the attribute value
570                                          * is the op dn.
571                                          */
572                                         rc = value_match( &match, b->a_dn_at,
573                                                 b->a_dn_at->ad_type->sat_equality, 0,
574                                                 val, &bv, &text );
575                                         /* on match error or no match, fail the ACL clause */
576                                         if (rc != LDAP_SUCCESS || match != 0 )
577                                                 continue;
578                                 }
579                         } else {
580                                 /* no dnattr match, check if this is a self clause */
581                                 if ( ! b->a_dn_self )
582                                         continue;
583                                 /* this is a self clause, check if the target is an
584                                  * attribute.
585                                  */
586                                 if ( val == NULL )
587                                         continue;
588                                 /* target is attribute, check if the attribute value
589                                  * is the op dn.
590                                  */
591                                 rc = value_match( &match, b->a_dn_at,
592                                         b->a_dn_at->ad_type->sat_equality, 0,
593                                         val, &bv, &text );
594
595                                 /* on match error or no match, fail the ACL clause */
596                                 if (rc != LDAP_SUCCESS || match != 0 )
597                                         continue;
598                         }
599                 }
600
601                 if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
602                         char buf[1024];
603
604                         /* b->a_group is an unexpanded entry name, expanded it should be an 
605                          * entry with objectclass group* and we test to see if odn is one of
606                          * the values in the attribute group
607                          */
608                         /* see if asker is listed in dnattr */
609                         if ( b->a_group_style == ACL_STYLE_REGEX ) {
610                                 string_expand(buf, sizeof(buf), b->a_group_pat, e->e_ndn, matches);
611                                 if ( dn_normalize(buf) == NULL ) {
612                                         /* did not expand to a valid dn */
613                                         continue;
614                                 }
615                         } else {
616                                 strncpy( buf, b->a_group_pat, sizeof(buf) - 1 );
617                                 buf[sizeof(buf) - 1] = 0;
618                         }
619
620                         if (backend_group(be, e, buf, op->o_ndn,
621                                 b->a_group_oc, b->a_group_at) != 0)
622                         {
623                                 continue;
624                         }
625                 }
626
627                 if ( b->a_set_pat != NULL ) {
628                         struct berval bv;
629
630                         bv.bv_val = b->a_set_pat;
631                         bv.bv_len = strlen(b->a_set_pat);
632                         if (aci_match_set( &bv, be, e, conn, op, 0 ) == 0) {
633                                 continue;
634                         }
635                 }
636
637 #ifdef SLAPD_ACI_ENABLED
638                 if ( b->a_aci_at != NULL ) {
639                         Attribute       *at;
640                         slap_access_t grant, deny, tgrant, tdeny;
641
642                         /* this case works different from the others above.
643                          * since aci's themselves give permissions, we need
644                          * to first check b->a_mask, the ACL's access level.
645                          */
646
647                         if( op->o_ndn == NULL || op->o_ndn[0] == '\0' ) {
648                                 continue;
649                         }
650
651                         if ( e->e_dn == NULL ) {
652                                 continue;
653                         }
654
655                         /* first check if the right being requested
656                          * is allowed by the ACL clause.
657                          */
658                         if ( ! ACL_GRANT( b->a_mask, *mask ) ) {
659                                 continue;
660                         }
661
662                         /* get the aci attribute */
663                         at = attr_find( e->e_attrs, b->a_aci_at );
664                         if ( at == NULL ) {
665                                 continue;
666                         }
667
668                         /* start out with nothing granted, nothing denied */
669                         ACL_INIT(tgrant);
670                         ACL_INIT(tdeny);
671
672                         /* the aci is an multi-valued attribute.  The
673                          * rights are determined by OR'ing the individual
674                          * rights given by the acis.
675                          */
676                         for ( i = 0; at->a_vals[i] != NULL; i++ ) {
677                                 if (aci_mask( be, conn, op,
678                                         e, desc, val, at->a_vals[i],
679                                         matches, &grant, &deny ) != 0)
680                                 {
681                                         tgrant |= grant;
682                                         tdeny |= deny;
683                                 }
684                         }
685
686                         /* remove anything that the ACL clause does not allow */
687                         tgrant &= b->a_mask & ACL_PRIV_MASK;
688                         tdeny &= ACL_PRIV_MASK;
689
690                         /* see if we have anything to contribute */
691                         if( ACL_IS_INVALID(tgrant) && ACL_IS_INVALID(tdeny) ) { 
692                                 continue;
693                         }
694
695                         /* this could be improved by changing acl_mask so that it can deal with
696                          * by clauses that return grant/deny pairs.  Right now, it does either
697                          * additive or subtractive rights, but not both at the same time.  So,
698                          * we need to combine the grant/deny pair into a single rights mask in
699                          * a smart way:  if either grant or deny is "empty", then we use the
700                          * opposite as is, otherwise we remove any denied rights from the grant
701                          * rights mask and construct an additive mask.
702                          */
703                         if (ACL_IS_INVALID(tdeny)) {
704                                 modmask = tgrant | ACL_PRIV_ADDITIVE;
705
706                         } else if (ACL_IS_INVALID(tgrant)) {
707                                 modmask = tdeny | ACL_PRIV_SUBSTRACTIVE;
708
709                         } else {
710                                 modmask = (tgrant & ~tdeny) | ACL_PRIV_ADDITIVE;
711                         }
712
713                 } else
714 #endif
715                 {
716                         modmask = b->a_mask;
717                 }
718
719
720                 Debug( LDAP_DEBUG_ACL,
721                         "<= acl_mask: [%d] applying %s (%s)\n",
722                         i, accessmask2str( modmask, accessmaskbuf ), 
723                         b->a_type == ACL_CONTINUE
724                                 ? "continue"
725                                 : b->a_type == ACL_BREAK
726                                         ? "break"
727                                         : "stop" );
728
729                 /* save old mask */
730                 oldmask = *mask;
731
732                 if( ACL_IS_ADDITIVE(modmask) ) {
733                         /* add privs */
734                         ACL_PRIV_SET( *mask, modmask );
735
736                         /* cleanup */
737                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
738
739                 } else if( ACL_IS_SUBTRACTIVE(modmask) ) {
740                         /* substract privs */
741                         ACL_PRIV_CLR( *mask, modmask );
742
743                         /* cleanup */
744                         ACL_PRIV_CLR( *mask, ~ACL_PRIV_MASK );
745
746                 } else {
747                         /* assign privs */
748                         *mask = modmask;
749                 }
750
751                 Debug( LDAP_DEBUG_ACL,
752                         "<= acl_mask: [%d] mask: %s\n",
753                         i, accessmask2str(*mask, accessmaskbuf), 0 );
754
755                 if( b->a_type == ACL_CONTINUE ) {
756                         continue;
757
758                 } else if ( b->a_type == ACL_BREAK ) {
759                         return ACL_BREAK;
760
761                 } else {
762                         return ACL_STOP;
763                 }
764         }
765
766         Debug( LDAP_DEBUG_ACL,
767                 "<= acl_mask: no more <who> clauses, returning %s (stop)\n",
768                 accessmask2str(*mask, accessmaskbuf), 0, 0 );
769         return ACL_STOP;
770 }
771
772 /*
773  * acl_check_modlist - check access control on the given entry to see if
774  * it allows the given modifications by the user associated with op.
775  * returns      1       if mods allowed ok
776  *                      0       mods not allowed
777  */
778
779 int
780 acl_check_modlist(
781     Backend     *be,
782     Connection  *conn,
783     Operation   *op,
784     Entry       *e,
785     Modifications       *mlist
786 )
787 {
788         int             i;
789
790         assert( be != NULL );
791
792         /* short circuit root database access */
793         if ( be_isroot( be, op->o_ndn ) ) {
794                 Debug( LDAP_DEBUG_ACL,
795                         "<= acl_access_allowed: granted to database root\n",
796                     0, 0, 0 );
797                 return 1;
798         }
799
800         /* use backend default access if no backend acls */
801         if( be != NULL && be->be_acl == NULL ) {
802                 Debug( LDAP_DEBUG_ACL,
803                         "=> access_allowed: backend default %s access %s to \"%s\"\n",
804                         access2str( ACL_WRITE ),
805                         be->be_dfltaccess >= ACL_WRITE ? "granted" : "denied", op->o_dn );
806
807                 return be->be_dfltaccess >= ACL_WRITE;
808
809 #ifdef notdef
810         /* be is always non-NULL */
811         /* use global default access if no global acls */
812         } else if ( be == NULL && global_acl == NULL ) {
813                 Debug( LDAP_DEBUG_ACL,
814                         "=> access_allowed: global default %s access %s to \"%s\"\n",
815                         access2str( ACL_WRITE ),
816                         global_default_access >= ACL_WRITE ? "granted" : "denied", op->o_dn );
817
818                 return global_default_access >= ACL_WRITE;
819 #endif
820         }
821
822         for ( ; mlist != NULL; mlist = mlist->sml_next ) {
823                 /*
824                  * no-user-modification operational attributes are ignored
825                  * by ACL_WRITE checking as any found here are not provided
826                  * by the user
827                  */
828                 if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
829                         Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
830                                 " modify access granted\n",
831                                 mlist->sml_desc->ad_cname->bv_val, 0, 0 );
832                         continue;
833                 }
834
835                 switch ( mlist->sml_op ) {
836                 case LDAP_MOD_REPLACE:
837                 case LDAP_MOD_ADD:
838                         if ( mlist->sml_bvalues == NULL ) {
839                                 break;
840                         }
841                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
842                                 if ( ! access_allowed( be, conn, op, e,
843                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
844                                 {
845                                         return( 0 );
846                                 }
847                         }
848                         break;
849
850                 case LDAP_MOD_DELETE:
851                         if ( mlist->sml_bvalues == NULL ) {
852                                 if ( ! access_allowed( be, conn, op, e,
853                                         mlist->sml_desc, NULL, ACL_WRITE ) )
854                                 {
855                                         return( 0 );
856                                 }
857                                 break;
858                         }
859                         for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
860                                 if ( ! access_allowed( be, conn, op, e,
861                                         mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
862                                 {
863                                         return( 0 );
864                                 }
865                         }
866                         break;
867                 }
868         }
869
870         return( 1 );
871 }
872
873 #ifdef SLAPD_ACI_ENABLED
874 static char *
875 aci_bvstrdup( struct berval *bv )
876 {
877         char *s;
878
879         s = (char *)ch_malloc(bv->bv_len + 1);
880         if (s != NULL) {
881                 memcpy(s, bv->bv_val, bv->bv_len);
882                 s[bv->bv_len] = 0;
883         }
884         return(s);
885 }
886
887 static int
888 aci_strbvcmp(
889         const char *s,
890         struct berval *bv )
891 {
892         int res, len;
893
894         res = strncasecmp( s, bv->bv_val, bv->bv_len );
895         if (res)
896                 return(res);
897         len = strlen(s);
898         if (len > (int)bv->bv_len)
899                 return(1);
900         if (len < (int)bv->bv_len)
901                 return(-1);
902         return(0);
903 }
904
905 static int
906 aci_get_part(
907         struct berval *list,
908         int ix,
909         char sep,
910         struct berval *bv )
911 {
912         int len;
913         char *p;
914
915         if (bv) {
916                 bv->bv_len = 0;
917                 bv->bv_val = NULL;
918         }
919         len = list->bv_len;
920         p = list->bv_val;
921         while (len >= 0 && --ix >= 0) {
922                 while (--len >= 0 && *p++ != sep) ;
923         }
924         while (len >= 0 && *p == ' ') {
925                 len--;
926                 p++;
927         }
928         if (len < 0)
929                 return(-1);
930
931         if (!bv)
932                 return(0);
933
934         bv->bv_val = p;
935         while (--len >= 0 && *p != sep) {
936                 bv->bv_len++;
937                 p++;
938         }
939         while (bv->bv_len > 0 && *--p == ' ')
940                 bv->bv_len--;
941         return(bv->bv_len);
942 }
943
944 static int
945 aci_list_map_rights(
946         struct berval *list )
947 {
948         struct berval bv;
949         slap_access_t mask;
950         int i;
951
952         ACL_INIT(mask);
953         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
954                 if (bv.bv_len <= 0)
955                         continue;
956                 switch (*bv.bv_val) {
957                 case 'c':
958                         ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
959                         break;
960                 case 's':
961                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
962                          * the right 's' to mean "set", but in the examples states
963                          * that the right 's' means "search".  The latter definition
964                          * is used here.
965                          */
966                         ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
967                         break;
968                 case 'r':
969                         ACL_PRIV_SET(mask, ACL_PRIV_READ);
970                         break;
971                 case 'w':
972                         ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
973                         break;
974                 case 'x':
975                         /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not 
976                          * define any equivalent to the AUTH right, so I've just used
977                          * 'x' for now.
978                          */
979                         ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
980                         break;
981                 default:
982                         break;
983                 }
984
985         }
986         return(mask);
987 }
988
989 static int
990 aci_list_has_attr(
991         struct berval *list,
992         const char *attr,
993         struct berval *val )
994 {
995         struct berval bv, left, right;
996         int i;
997
998         for (i = 0; aci_get_part(list, i, ',', &bv) >= 0; i++) {
999                 if (aci_get_part(&bv, 0, '=', &left) < 0
1000                         || aci_get_part(&bv, 1, '=', &right) < 0)
1001                 {
1002                         if (aci_strbvcmp(attr, &bv) == 0)
1003                                 return(1);
1004                 } else if (val == NULL) {
1005                         if (aci_strbvcmp(attr, &left) == 0)
1006                                 return(1);
1007                 } else {
1008                         if (aci_strbvcmp(attr, &left) == 0) {
1009                                 /* this is experimental code that implements a
1010                                  * simple (prefix) match of the attribute value.
1011                                  * the ACI draft does not provide for aci's that
1012                                  * apply to specific values, but it would be
1013                                  * nice to have.  If the <attr> part of an aci's
1014                                  * rights list is of the form <attr>=<value>,
1015                                  * that means the aci applies only to attrs with
1016                                  * the given value.  Furthermore, if the attr is
1017                                  * of the form <attr>=<value>*, then <value> is
1018                                  * treated as a prefix, and the aci applies to 
1019                                  * any value with that prefix.
1020                                  *
1021                                  * Ideally, this would allow r.e. matches.
1022                                  */
1023                                 if (aci_get_part(&right, 0, '*', &left) < 0
1024                                         || right.bv_len <= left.bv_len)
1025                                 {
1026                                         if (aci_strbvcmp(val->bv_val, &right) == 0)
1027                                                 return(1);
1028                                 } else if (val->bv_len >= left.bv_len) {
1029                                         if (strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0)
1030                                                 return(1);
1031                                 }
1032                         }
1033                 }
1034         }
1035         return(0);
1036 }
1037
1038 static slap_access_t
1039 aci_list_get_attr_rights(
1040         struct berval *list,
1041         const char *attr,
1042         struct berval *val )
1043 {
1044     struct berval bv;
1045     slap_access_t mask;
1046     int i;
1047
1048         /* loop through each rights/attr pair, skip first part (action) */
1049         ACL_INIT(mask);
1050         for (i = 1; aci_get_part(list, i + 1, ';', &bv) >= 0; i += 2) {
1051                 if (aci_list_has_attr(&bv, attr, val) == 0)
1052                         continue;
1053                 if (aci_get_part(list, i, ';', &bv) < 0)
1054                         continue;
1055                 mask |= aci_list_map_rights(&bv);
1056         }
1057         return(mask);
1058 }
1059
1060 static int
1061 aci_list_get_rights(
1062         struct berval *list,
1063         const char *attr,
1064         struct berval *val,
1065         slap_access_t *grant,
1066         slap_access_t *deny )
1067 {
1068     struct berval perm, actn;
1069     slap_access_t *mask;
1070     int i, found;
1071
1072         if (attr == NULL || *attr == 0 || strcasecmp(attr, "entry") == 0) {
1073                 attr = "[entry]";
1074         }
1075
1076         found = 0;
1077         ACL_INIT(*grant);
1078         ACL_INIT(*deny);
1079         /* loop through each permissions clause */
1080         for (i = 0; aci_get_part(list, i, '$', &perm) >= 0; i++) {
1081                 if (aci_get_part(&perm, 0, ';', &actn) < 0)
1082                         continue;
1083                 if (aci_strbvcmp( "grant", &actn ) == 0) {
1084                         mask = grant;
1085                 } else if (aci_strbvcmp( "deny", &actn ) == 0) {
1086                         mask = deny;
1087                 } else {
1088                         continue;
1089                 }
1090
1091                 found = 1;
1092                 *mask |= aci_list_get_attr_rights(&perm, attr, val);
1093                 *mask |= aci_list_get_attr_rights(&perm, "[all]", NULL);
1094         }
1095         return(found);
1096 }
1097
1098 static int
1099 aci_group_member (
1100         struct berval *subj,
1101         const char *defgrpoc,
1102         const char *defgrpat,
1103     Backend             *be,
1104     Entry               *e,
1105     Operation           *op,
1106         regmatch_t      *matches
1107 )
1108 {
1109         struct berval bv;
1110         char *subjdn, *grpdn = NULL;
1111         char *grpoc;
1112         char *grpat;
1113         ObjectClass *grp_oc = NULL;
1114         AttributeDescription *grp_ad = NULL;
1115         char *text;
1116         int rc;
1117
1118         /* format of string is "group/objectClassValue/groupAttrName" */
1119         if (aci_get_part(subj, 0, '/', &bv) < 0) {
1120                 return(0);
1121         }
1122
1123         subjdn = aci_bvstrdup(&bv);
1124         if (subjdn == NULL) {
1125                 return(0);
1126         }
1127
1128         if (aci_get_part(subj, 1, '/', &bv) < 0) {
1129                 grpoc = ch_strdup( defgrpoc );
1130         } else {
1131                 grpoc = aci_bvstrdup(&bv);
1132         }
1133
1134         if (aci_get_part(subj, 2, '/', &bv) < 0) {
1135                 grpat = ch_strdup( defgrpat );
1136         } else {
1137                 grpat = aci_bvstrdup(&bv);
1138         }
1139
1140         rc = slap_str2ad( grpat, &grp_ad, &text );
1141         if( rc != LDAP_SUCCESS ) {
1142                 rc = 0;
1143                 goto done;
1144         }
1145         rc = 0;
1146
1147         grp_oc = oc_find( grpoc );
1148         grpdn = (char *)ch_malloc(1024);
1149
1150         if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
1151                 string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
1152                 if ( dn_normalize(grpdn) != NULL ) {
1153                         rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
1154                 }
1155         }
1156
1157 done:
1158         if( grp_ad != NULL ) ad_free( grp_ad, 1 );
1159         ch_free(grpdn);
1160         ch_free(grpat);
1161         ch_free(grpoc);
1162         ch_free(subjdn);
1163         return(rc);
1164 }
1165
1166 char **
1167 aci_set_gather (void *cookie, char *name, char *attr)
1168 {
1169         struct {
1170         Backend *be;
1171         Entry *e;
1172         Connection *conn;
1173         Operation *op;
1174         } *cp = (void *)cookie;
1175         struct berval **bvals = NULL;
1176         char **vals = NULL;
1177         char *ndn;
1178         int i;
1179
1180         /* this routine needs to return the bervals instead of
1181          * plain strings, since syntax is not known.  It should
1182          * also return the syntax or some "comparison cookie".
1183          */
1184
1185         if ((ndn = ch_strdup(name)) != NULL) {
1186                 if (dn_normalize(ndn) != NULL) {
1187                         char *text;
1188                         AttributeDescription *desc = NULL;
1189                         if (slap_str2ad(attr, &desc, &text) == 0) {
1190                                 backend_attribute(cp->be, NULL /*cp->conn*/,
1191                                                                         NULL /*cp->op*/, cp->e,
1192                                                                         ndn, desc, &bvals);
1193                                 if (bvals != NULL) {
1194                                         for (i = 0; bvals[i] != NULL; i++) { }
1195                                         vals = ch_calloc(i + 1, sizeof(char *));
1196                                         if (vals != NULL) {
1197                                                 while (--i >= 0) {
1198                                                         vals[i] = bvals[i]->bv_val;
1199                                                         bvals[i]->bv_val = NULL;
1200                                                 }
1201                                         }
1202                                         ber_bvecfree(bvals);
1203                                 }
1204                                 ad_free(desc, 1);
1205                         }
1206                 }
1207                 ch_free(ndn);
1208         }
1209         return(vals);
1210 }
1211
1212 static int
1213 aci_match_set (
1214         struct berval *subj,
1215     Backend *be,
1216     Entry *e,
1217     Connection *conn,
1218     Operation *op,
1219     int setref
1220 )
1221 {
1222         char *set = NULL;
1223         int rc = 0;
1224         struct {
1225         Backend *be;
1226         Entry *e;
1227         Connection *conn;
1228         Operation *op;
1229         } cookie;
1230
1231         if (setref == 0) {
1232                 set = aci_bvstrdup(subj);
1233         } else {
1234                 struct berval bv;
1235                 char *subjdn;
1236                 char *setat;
1237                 struct berval **bvals;
1238                 char *text;
1239                 AttributeDescription *desc = NULL;
1240
1241                 /* format of string is "entry/setAttrName" */
1242                 if (aci_get_part(subj, 0, '/', &bv) < 0) {
1243                         return(0);
1244                 }
1245
1246                 subjdn = aci_bvstrdup(&bv);
1247                 if ( subjdn == NULL ) {
1248                         return(0);
1249                 }
1250
1251                 if ( aci_get_part(subj, 1, '/', &bv) < 0 ) {
1252                         setat = ch_strdup( SLAPD_ACI_SET_ATTR );
1253                 } else {
1254                         setat = aci_bvstrdup(&bv);
1255                 }
1256                 if ( setat != NULL ) {
1257                         if ( dn_normalize(subjdn) != NULL
1258                                 && slap_str2ad(setat, &desc, &text) == 0 )
1259                         {
1260                                 backend_attribute(be, NULL, NULL, e,
1261                                                                 subjdn, desc, &bvals);
1262                                 ad_free(desc, 1);
1263                                 if ( bvals != NULL ) {
1264                                         if ( bvals[0] != NULL )
1265                                                 set = ch_strdup(bvals[0]->bv_val);
1266                                         ber_bvecfree(bvals);
1267                                 }
1268                         }
1269                         ch_free(setat);
1270                 }
1271                 ch_free(subjdn);
1272         }
1273
1274         if (set != NULL) {
1275                 cookie.be = be;
1276                 cookie.e = e;
1277                 cookie.conn = conn;
1278                 cookie.op = op;
1279                 rc = (set_filter(aci_set_gather, &cookie, set, op->o_ndn, e->e_ndn, NULL) > 0);
1280                 ch_free(set);
1281         }
1282         return(rc);
1283 }
1284
1285 static int
1286 aci_mask(
1287     Backend                     *be,
1288     Connection          *conn,
1289     Operation           *op,
1290     Entry                       *e,
1291         AttributeDescription *desc,
1292     struct berval       *val,
1293     struct berval       *aci,
1294         regmatch_t              *matches,
1295         slap_access_t   *grant,
1296         slap_access_t   *deny
1297 )
1298 {
1299     struct berval bv, perms, sdn;
1300     char *subjdn;
1301         int rc;
1302         char *attr = desc->ad_cname->bv_val;
1303
1304         /* parse an aci of the form:
1305                 oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
1306
1307            See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
1308            a full description of the format for this attribute.
1309
1310            For now, this routine only supports scope=entry.
1311          */
1312
1313         /* check that the aci has all 5 components */
1314         if (aci_get_part(aci, 4, '#', NULL) < 0)
1315                 return(0);
1316
1317         /* check that the aci family is supported */
1318         if (aci_get_part(aci, 0, '#', &bv) < 0)
1319                 return(0);
1320
1321         /* check that the scope is "entry" */
1322         if (aci_get_part(aci, 1, '#', &bv) < 0
1323                 || aci_strbvcmp( "entry", &bv ) != 0)
1324         {
1325                 return(0);
1326         }
1327
1328         /* get the list of permissions clauses, bail if empty */
1329         if (aci_get_part(aci, 2, '#', &perms) <= 0)
1330                 return(0);
1331
1332         /* check if any permissions allow desired access */
1333         if (aci_list_get_rights(&perms, attr, val, grant, deny) == 0)
1334                 return(0);
1335
1336         /* see if we have a DN match */
1337         if (aci_get_part(aci, 3, '#', &bv) < 0)
1338                 return(0);
1339
1340         if (aci_get_part(aci, 4, '#', &sdn) < 0)
1341                 return(0);
1342
1343         if (aci_strbvcmp( "access-id", &bv ) == 0) {
1344                 subjdn = aci_bvstrdup(&sdn);
1345                 if (subjdn == NULL)
1346                         return(0);
1347                 rc = 1;
1348                 if ( dn_normalize(subjdn) != NULL )
1349                         if (strcasecmp(op->o_ndn, subjdn) != 0)
1350                                 rc = 0;
1351                 ch_free(subjdn);
1352                 return(rc);
1353         }
1354
1355         if (aci_strbvcmp( "self", &bv ) == 0) {
1356                 if (strcasecmp(op->o_ndn, e->e_ndn) == 0)
1357                         return(1);
1358
1359         } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
1360                 char *dnattr = aci_bvstrdup(&sdn);
1361                 Attribute *at;
1362                 AttributeDescription *ad = NULL;
1363                 const char *text;
1364
1365                 rc = slap_str2ad( dnattr, &ad, &text );
1366                 ch_free( dnattr );
1367
1368                 if( rc != LDAP_SUCCESS ) {
1369                         return 0;
1370                 }
1371
1372                 rc = 0;
1373
1374                 bv.bv_val = op->o_ndn;
1375                 bv.bv_len = strlen( bv.bv_val );
1376
1377                 for(at = attrs_find( e->e_attrs, ad );
1378                         at != NULL;
1379                         at = attrs_find( at->a_next, ad ) )
1380                 {
1381                         if (value_find( ad, at->a_vals, &bv) == 0 ) {
1382                                 rc = 1;
1383                                 break;
1384                         }
1385                 }
1386
1387                 ad_free( ad, 1 );
1388                 return rc;
1389
1390
1391         } else if (aci_strbvcmp( "group", &bv ) == 0) {
1392                 if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
1393                         return(1);
1394
1395         } else if (aci_strbvcmp( "role", &bv ) == 0) {
1396                 if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
1397                         return(1);
1398
1399         } else if (aci_strbvcmp( "set", &bv ) == 0) {
1400                 if (aci_match_set(&sdn, be, e, conn, op, 0))
1401                         return(1);
1402
1403         } else if (aci_strbvcmp( "set-ref", &bv ) == 0) {
1404                 if (aci_match_set(&sdn, be, e, conn, op, 1))
1405                         return(1);
1406
1407         }
1408
1409         return(0);
1410 }
1411
1412 #endif  /* SLAPD_ACI_ENABLED */
1413
1414 static void
1415 string_expand(
1416         char *newbuf,
1417         int bufsiz,
1418         char *pat,
1419         char *match,
1420         regmatch_t *matches)
1421 {
1422         int     size;
1423         char   *sp;
1424         char   *dp;
1425         int     flag;
1426
1427         size = 0;
1428         newbuf[0] = '\0';
1429         bufsiz--; /* leave space for lone $ */
1430
1431         flag = 0;
1432         for ( dp = newbuf, sp = pat; size < bufsiz && *sp ; sp++) {
1433                 /* did we previously see a $ */
1434                 if (flag) {
1435                         if (*sp == '$') {
1436                                 *dp++ = '$';
1437                                 size++;
1438                         } else if (*sp >= '0' && *sp <= '9' ) {
1439                                 int     n;
1440                                 int     i;
1441                                 int     l;
1442
1443                                 n = *sp - '0';
1444                                 *dp = '\0';
1445                                 i = matches[n].rm_so;
1446                                 l = matches[n].rm_eo; 
1447                                 for ( ; size < 512 && i < l; size++, i++ ) {
1448                                         *dp++ = match[i];
1449                                         size++;
1450                                 }
1451                                 *dp = '\0';
1452                         }
1453                         flag = 0;
1454                 } else {
1455                         if (*sp == '$') {
1456                                 flag = 1;
1457                         } else {
1458                                 *dp++ = *sp;
1459                                 size++;
1460                         }
1461                 }
1462         }
1463
1464         if (flag) {
1465                 /* must have ended with a single $ */
1466                 *dp++ = '$';
1467                 size++;
1468         }
1469
1470         *dp = '\0';
1471
1472         Debug( LDAP_DEBUG_TRACE, "=> string_expand: pattern:  %s\n", pat, 0, 0 );
1473         Debug( LDAP_DEBUG_TRACE, "=> string_expand: expanded: %s\n", newbuf, 0, 0 );
1474 }
1475
1476 static int
1477 regex_matches(
1478         char *pat,                              /* pattern to expand and match against */
1479         char *str,                              /* string to match against pattern */
1480         char *buf,                              /* buffer with $N expansion variables */
1481         regmatch_t *matches             /* offsets in buffer for $N expansion variables */
1482 )
1483 {
1484         regex_t re;
1485         char newbuf[512];
1486         int     rc;
1487
1488         if(str == NULL) str = "";
1489
1490         string_expand(newbuf, sizeof(newbuf), pat, buf, matches);
1491         if (( rc = regcomp(&re, newbuf, REG_EXTENDED|REG_ICASE))) {
1492                 char error[512];
1493                 regerror(rc, &re, error, sizeof(error));
1494
1495                 Debug( LDAP_DEBUG_TRACE,
1496                     "compile( \"%s\", \"%s\") failed %s\n",
1497                         pat, str, error );
1498                 return( 0 );
1499         }
1500
1501         rc = regexec(&re, str, 0, NULL, 0);
1502         regfree( &re );
1503
1504         Debug( LDAP_DEBUG_TRACE,
1505             "=> regex_matches: string:   %s\n", str, 0, 0 );
1506         Debug( LDAP_DEBUG_TRACE,
1507             "=> regex_matches: rc: %d %s\n",
1508                 rc, !rc ? "matches" : "no matches", 0 );
1509         return( !rc );
1510 }
1511