]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
ITS#6520
[openldap] / servers / slapd / aclparse.c
1 /* aclparse.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2010 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/ctype.h>
32 #include <ac/regex.h>
33 #include <ac/socket.h>
34 #include <ac/string.h>
35 #include <ac/unistd.h>
36
37 #include "slap.h"
38 #include "lber_pvt.h"
39 #include "lutil.h"
40
41 static const char style_base[] = "base";
42 const char *style_strings[] = {
43         "regex",
44         "expand",
45         "exact",
46         "one",
47         "subtree",
48         "children",
49         "level",
50         "attrof",
51         "anonymous",
52         "users",
53         "self",
54         "ip",
55         "ipv6",
56         "path",
57         NULL
58 };
59
60 static void             split(char *line, int splitchar, char **left, char **right);
61 static void             access_append(Access **l, Access *a);
62 static void             access_free( Access *a );
63 static int              acl_usage(void);
64
65 static void             acl_regex_normalized_dn(const char *src, struct berval *pat);
66
67 #ifdef LDAP_DEBUG
68 static void             print_acl(Backend *be, AccessControl *a);
69 #endif
70
71 static int              check_scope( BackendDB *be, AccessControl *a );
72
73 #ifdef SLAP_DYNACL
74 static int
75 slap_dynacl_config(
76         const char *fname,
77         int lineno,
78         Access *b,
79         const char *name,
80         const char *opts,
81         slap_style_t sty,
82         const char *right )
83 {
84         slap_dynacl_t   *da, *tmp;
85         int             rc = 0;
86
87         for ( da = b->a_dynacl; da; da = da->da_next ) {
88                 if ( strcasecmp( da->da_name, name ) == 0 ) {
89                         Debug( LDAP_DEBUG_ANY,
90                                 "%s: line %d: dynacl \"%s\" already specified.\n",
91                                 fname, lineno, name );
92                         return acl_usage();
93                 }
94         }
95
96         da = slap_dynacl_get( name );
97         if ( da == NULL ) {
98                 return -1;
99         }
100
101         tmp = ch_malloc( sizeof( slap_dynacl_t ) );
102         *tmp = *da;
103
104         if ( tmp->da_parse ) {
105                 rc = ( *tmp->da_parse )( fname, lineno, opts, sty, right, &tmp->da_private );
106                 if ( rc ) {
107                         ch_free( tmp );
108                         return rc;
109                 }
110         }
111
112         tmp->da_next = b->a_dynacl;
113         b->a_dynacl = tmp;
114
115         return 0;
116 }
117 #endif /* SLAP_DYNACL */
118
119 static void
120 regtest(const char *fname, int lineno, char *pat) {
121         int e;
122         regex_t re;
123
124         char            buf[ SLAP_TEXT_BUFLEN ];
125         unsigned        size;
126
127         char *sp;
128         char *dp;
129         int  flag;
130
131         sp = pat;
132         dp = buf;
133         size = 0;
134         buf[0] = '\0';
135
136         for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
137                 if (flag) {
138                         if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
139                                 *dp++ = *sp;
140                                 size++;
141                         }
142                         flag = 0;
143
144                 } else {
145                         if (*sp == '$') {
146                                 flag = 1;
147                         } else {
148                                 *dp++ = *sp;
149                                 size++;
150                         }
151                 }
152         }
153
154         *dp = '\0';
155         if ( size >= (sizeof(buf) - 1) ) {
156                 Debug( LDAP_DEBUG_ANY,
157                         "%s: line %d: regular expression \"%s\" too large\n",
158                         fname, lineno, pat );
159                 (void)acl_usage();
160                 exit( EXIT_FAILURE );
161         }
162
163         if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
164                 char error[ SLAP_TEXT_BUFLEN ];
165
166                 regerror(e, &re, error, sizeof(error));
167
168                 snprintf( buf, sizeof( buf ),
169                         "regular expression \"%s\" bad because of %s",
170                         pat, error );
171                 Debug( LDAP_DEBUG_ANY,
172                         "%s: line %d: %s\n",
173                         fname, lineno, buf );
174                 acl_usage();
175                 exit( EXIT_FAILURE );
176         }
177         regfree(&re);
178 }
179
180 /*
181  * Experimental
182  *
183  * Check if the pattern of an ACL, if any, matches the scope
184  * of the backend it is defined within.
185  */
186 #define ACL_SCOPE_UNKNOWN       (-2)
187 #define ACL_SCOPE_ERR           (-1)
188 #define ACL_SCOPE_OK            (0)
189 #define ACL_SCOPE_PARTIAL       (1)
190 #define ACL_SCOPE_WARN          (2)
191
192 static int
193 check_scope( BackendDB *be, AccessControl *a )
194 {
195         ber_len_t       patlen;
196         struct berval   dn;
197
198         dn = be->be_nsuffix[0];
199
200         if ( BER_BVISEMPTY( &dn ) ) {
201                 return ACL_SCOPE_OK;
202         }
203
204         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
205                         a->acl_dn_style != ACL_STYLE_REGEX )
206         {
207                 slap_style_t    style = a->acl_dn_style;
208
209                 if ( style == ACL_STYLE_REGEX ) {
210                         char            dnbuf[SLAP_LDAPDN_MAXLEN + 2];
211                         char            rebuf[SLAP_LDAPDN_MAXLEN + 1];
212                         ber_len_t       rebuflen;
213                         regex_t         re;
214                         int             rc;
215                         
216                         /* add trailing '$' to database suffix to form
217                          * a simple trial regex pattern "<suffix>$" */
218                         AC_MEMCPY( dnbuf, be->be_nsuffix[0].bv_val,
219                                 be->be_nsuffix[0].bv_len );
220                         dnbuf[be->be_nsuffix[0].bv_len] = '$';
221                         dnbuf[be->be_nsuffix[0].bv_len + 1] = '\0';
222
223                         if ( regcomp( &re, dnbuf, REG_EXTENDED|REG_ICASE ) ) {
224                                 return ACL_SCOPE_WARN;
225                         }
226
227                         /* remove trailing ')$', if any, from original
228                          * regex pattern */
229                         rebuflen = a->acl_dn_pat.bv_len;
230                         AC_MEMCPY( rebuf, a->acl_dn_pat.bv_val, rebuflen + 1 );
231                         if ( rebuf[rebuflen - 1] == '$' ) {
232                                 rebuf[--rebuflen] = '\0';
233                         }
234                         while ( rebuflen > be->be_nsuffix[0].bv_len && rebuf[rebuflen - 1] == ')' ) {
235                                 rebuf[--rebuflen] = '\0';
236                         }
237                         if ( rebuflen == be->be_nsuffix[0].bv_len ) {
238                                 rc = ACL_SCOPE_WARN;
239                                 goto regex_done;
240                         }
241
242                         /* not a clear indication of scoping error, though */
243                         rc = regexec( &re, rebuf, 0, NULL, 0 )
244                                 ? ACL_SCOPE_WARN : ACL_SCOPE_OK;
245
246 regex_done:;
247                         regfree( &re );
248                         return rc;
249                 }
250
251                 patlen = a->acl_dn_pat.bv_len;
252                 /* If backend suffix is longer than pattern,
253                  * it is a potential mismatch (in the sense
254                  * that a superior naming context could
255                  * match */
256                 if ( dn.bv_len > patlen ) {
257                         /* base is blatantly wrong */
258                         if ( style == ACL_STYLE_BASE ) return ACL_SCOPE_ERR;
259
260                         /* a style of one can be wrong if there is
261                          * more than one level between the suffix
262                          * and the pattern */
263                         if ( style == ACL_STYLE_ONE ) {
264                                 ber_len_t       rdnlen = 0;
265                                 int             sep = 0;
266
267                                 if ( patlen > 0 ) {
268                                         if ( !DN_SEPARATOR( dn.bv_val[dn.bv_len - patlen - 1] )) {
269                                                 return ACL_SCOPE_ERR;
270                                         }
271                                         sep = 1;
272                                 }
273
274                                 rdnlen = dn_rdnlen( NULL, &dn );
275                                 if ( rdnlen != dn.bv_len - patlen - sep )
276                                         return ACL_SCOPE_ERR;
277                         }
278
279                         /* if the trailing part doesn't match,
280                          * then it's an error */
281                         if ( strcmp( a->acl_dn_pat.bv_val,
282                                 &dn.bv_val[dn.bv_len - patlen] ) != 0 )
283                         {
284                                 return ACL_SCOPE_ERR;
285                         }
286
287                         return ACL_SCOPE_PARTIAL;
288                 }
289
290                 switch ( style ) {
291                 case ACL_STYLE_BASE:
292                 case ACL_STYLE_ONE:
293                 case ACL_STYLE_CHILDREN:
294                 case ACL_STYLE_SUBTREE:
295                         break;
296
297                 default:
298                         assert( 0 );
299                         break;
300                 }
301
302                 if ( dn.bv_len < patlen &&
303                         !DN_SEPARATOR( a->acl_dn_pat.bv_val[patlen - dn.bv_len - 1] ))
304                 {
305                         return ACL_SCOPE_ERR;
306                 }
307
308                 if ( strcmp( &a->acl_dn_pat.bv_val[patlen - dn.bv_len], dn.bv_val )
309                         != 0 )
310                 {
311                         return ACL_SCOPE_ERR;
312                 }
313
314                 return ACL_SCOPE_OK;
315         }
316
317         return ACL_SCOPE_UNKNOWN;
318 }
319
320 int
321 parse_acl(
322         Backend *be,
323         const char      *fname,
324         int             lineno,
325         int             argc,
326         char            **argv,
327         int             pos )
328 {
329         int             i;
330         char            *left, *right, *style;
331         struct berval   bv;
332         AccessControl   *a = NULL;
333         Access  *b = NULL;
334         int rc;
335         const char *text;
336
337         for ( i = 1; i < argc; i++ ) {
338                 /* to clause - select which entries are protected */
339                 if ( strcasecmp( argv[i], "to" ) == 0 ) {
340                         if ( a != NULL ) {
341                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
342                                         "only one to clause allowed in access line\n",
343                                     fname, lineno, 0 );
344                                 goto fail;
345                         }
346                         a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
347                         a->acl_attrval_style = ACL_STYLE_NONE;
348                         for ( ++i; i < argc; i++ ) {
349                                 if ( strcasecmp( argv[i], "by" ) == 0 ) {
350                                         i--;
351                                         break;
352                                 }
353
354                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
355                                         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
356                                                 a->acl_dn_style != ACL_STYLE_REGEX )
357                                         {
358                                                 Debug( LDAP_DEBUG_ANY,
359                                                         "%s: line %d: dn pattern"
360                                                         " already specified in to clause.\n",
361                                                         fname, lineno, 0 );
362                                                 goto fail;
363                                         }
364
365                                         ber_str2bv( "*", STRLENOF( "*" ), 1, &a->acl_dn_pat );
366                                         continue;
367                                 }
368
369                                 split( argv[i], '=', &left, &right );
370                                 split( left, '.', &left, &style );
371
372                                 if ( right == NULL ) {
373                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
374                                                 "missing \"=\" in \"%s\" in to clause\n",
375                                             fname, lineno, left );
376                                         goto fail;
377                                 }
378
379                                 if ( strcasecmp( left, "dn" ) == 0 ) {
380                                         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
381                                                 a->acl_dn_style != ACL_STYLE_REGEX )
382                                         {
383                                                 Debug( LDAP_DEBUG_ANY,
384                                                         "%s: line %d: dn pattern"
385                                                         " already specified in to clause.\n",
386                                                         fname, lineno, 0 );
387                                                 goto fail;
388                                         }
389
390                                         if ( style == NULL || *style == '\0' ||
391                                                 strcasecmp( style, "baseObject" ) == 0 ||
392                                                 strcasecmp( style, "base" ) == 0 ||
393                                                 strcasecmp( style, "exact" ) == 0 )
394                                         {
395                                                 a->acl_dn_style = ACL_STYLE_BASE;
396                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
397
398                                         } else if ( strcasecmp( style, "oneLevel" ) == 0 ||
399                                                 strcasecmp( style, "one" ) == 0 )
400                                         {
401                                                 a->acl_dn_style = ACL_STYLE_ONE;
402                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
403
404                                         } else if ( strcasecmp( style, "subtree" ) == 0 ||
405                                                 strcasecmp( style, "sub" ) == 0 )
406                                         {
407                                                 if( *right == '\0' ) {
408                                                         ber_str2bv( "*", STRLENOF( "*" ), 1, &a->acl_dn_pat );
409
410                                                 } else {
411                                                         a->acl_dn_style = ACL_STYLE_SUBTREE;
412                                                         ber_str2bv( right, 0, 1, &a->acl_dn_pat );
413                                                 }
414
415                                         } else if ( strcasecmp( style, "children" ) == 0 ) {
416                                                 a->acl_dn_style = ACL_STYLE_CHILDREN;
417                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
418
419                                         } else if ( strcasecmp( style, "regex" ) == 0 ) {
420                                                 a->acl_dn_style = ACL_STYLE_REGEX;
421
422                                                 if ( *right == '\0' ) {
423                                                         /* empty regex should match empty DN */
424                                                         a->acl_dn_style = ACL_STYLE_BASE;
425                                                         ber_str2bv( right, 0, 1, &a->acl_dn_pat );
426
427                                                 } else if ( strcmp(right, "*") == 0 
428                                                         || strcmp(right, ".*") == 0 
429                                                         || strcmp(right, ".*$") == 0 
430                                                         || strcmp(right, "^.*") == 0 
431                                                         || strcmp(right, "^.*$") == 0
432                                                         || strcmp(right, ".*$$") == 0 
433                                                         || strcmp(right, "^.*$$") == 0 )
434                                                 {
435                                                         ber_str2bv( "*", STRLENOF("*"), 1, &a->acl_dn_pat );
436
437                                                 } else {
438                                                         acl_regex_normalized_dn( right, &a->acl_dn_pat );
439                                                 }
440
441                                         } else {
442                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
443                                                         "unknown dn style \"%s\" in to clause\n",
444                                                     fname, lineno, style );
445                                                 goto fail;
446                                         }
447
448                                         continue;
449                                 }
450
451                                 if ( strcasecmp( left, "filter" ) == 0 ) {
452                                         if ( (a->acl_filter = str2filter( right )) == NULL ) {
453                                                 Debug( LDAP_DEBUG_ANY,
454                                 "%s: line %d: bad filter \"%s\" in to clause\n",
455                                                     fname, lineno, right );
456                                                 goto fail;
457                                         }
458
459                                 } else if ( strcasecmp( left, "attr" ) == 0             /* TOLERATED */
460                                                 || strcasecmp( left, "attrs" ) == 0 )   /* DOCUMENTED */
461                                 {
462                                         if ( strcasecmp( left, "attr" ) == 0 ) {
463                                                 Debug( LDAP_DEBUG_ANY,
464                                                         "%s: line %d: \"attr\" "
465                                                         "is deprecated (and undocumented); "
466                                                         "use \"attrs\" instead.\n",
467                                                         fname, lineno, 0 );
468                                         }
469
470                                         a->acl_attrs = str2anlist( a->acl_attrs,
471                                                 right, "," );
472                                         if ( a->acl_attrs == NULL ) {
473                                                 Debug( LDAP_DEBUG_ANY,
474                                 "%s: line %d: unknown attr \"%s\" in to clause\n",
475                                                     fname, lineno, right );
476                                                 goto fail;
477                                         }
478
479                                 } else if ( strncasecmp( left, "val", 3 ) == 0 ) {
480                                         struct berval   bv;
481                                         char            *mr;
482                                         
483                                         if ( !BER_BVISEMPTY( &a->acl_attrval ) ) {
484                                                 Debug( LDAP_DEBUG_ANY,
485                                 "%s: line %d: attr val already specified in to clause.\n",
486                                                         fname, lineno, 0 );
487                                                 goto fail;
488                                         }
489                                         if ( a->acl_attrs == NULL || !BER_BVISEMPTY( &a->acl_attrs[1].an_name ) )
490                                         {
491                                                 Debug( LDAP_DEBUG_ANY,
492                                 "%s: line %d: attr val requires a single attribute.\n",
493                                                         fname, lineno, 0 );
494                                                 goto fail;
495                                         }
496
497                                         ber_str2bv( right, 0, 0, &bv );
498                                         a->acl_attrval_style = ACL_STYLE_BASE;
499
500                                         mr = strchr( left, '/' );
501                                         if ( mr != NULL ) {
502                                                 mr[ 0 ] = '\0';
503                                                 mr++;
504
505                                                 a->acl_attrval_mr = mr_find( mr );
506                                                 if ( a->acl_attrval_mr == NULL ) {
507                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
508                                                                 "invalid matching rule \"%s\".\n",
509                                                                 fname, lineno, mr );
510                                                         goto fail;
511                                                 }
512
513                                                 if( !mr_usable_with_at( a->acl_attrval_mr, a->acl_attrs[ 0 ].an_desc->ad_type ) )
514                                                 {
515                                                         char    buf[ SLAP_TEXT_BUFLEN ];
516
517                                                         snprintf( buf, sizeof( buf ),
518                                                                 "matching rule \"%s\" use "
519                                                                 "with attr \"%s\" not appropriate.",
520                                                                 mr, a->acl_attrs[ 0 ].an_name.bv_val );
521                                                                 
522
523                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: %s\n",
524                                                                 fname, lineno, buf );
525                                                         goto fail;
526                                                 }
527                                         }
528                                         
529                                         if ( style != NULL ) {
530                                                 if ( strcasecmp( style, "regex" ) == 0 ) {
531                                                         int e = regcomp( &a->acl_attrval_re, bv.bv_val,
532                                                                 REG_EXTENDED | REG_ICASE );
533                                                         if ( e ) {
534                                                                 char    err[SLAP_TEXT_BUFLEN],
535                                                                         buf[ SLAP_TEXT_BUFLEN ];
536
537                                                                 regerror( e, &a->acl_attrval_re, err, sizeof( err ) );
538
539                                                                 snprintf( buf, sizeof( buf ),
540                                                                         "regular expression \"%s\" bad because of %s",
541                                                                         right, err );
542
543                                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: %s\n",
544                                                                         fname, lineno, buf );
545                                                                 goto fail;
546                                                         }
547                                                         a->acl_attrval_style = ACL_STYLE_REGEX;
548
549                                                 } else {
550                                                         /* FIXME: if the attribute has DN syntax, we might
551                                                          * allow one, subtree and children styles as well */
552                                                         if ( !strcasecmp( style, "base" ) ||
553                                                                 !strcasecmp( style, "exact" ) ) {
554                                                                 a->acl_attrval_style = ACL_STYLE_BASE;
555
556                                                         } else if ( a->acl_attrs[0].an_desc->ad_type->
557                                                                 sat_syntax == slap_schema.si_syn_distinguishedName )
558                                                         {
559                                                                 if ( !strcasecmp( style, "baseObject" ) ||
560                                                                         !strcasecmp( style, "base" ) )
561                                                                 {
562                                                                         a->acl_attrval_style = ACL_STYLE_BASE;
563                                                                 } else if ( !strcasecmp( style, "onelevel" ) ||
564                                                                         !strcasecmp( style, "one" ) )
565                                                                 {
566                                                                         a->acl_attrval_style = ACL_STYLE_ONE;
567                                                                 } else if ( !strcasecmp( style, "subtree" ) ||
568                                                                         !strcasecmp( style, "sub" ) )
569                                                                 {
570                                                                         a->acl_attrval_style = ACL_STYLE_SUBTREE;
571                                                                 } else if ( !strcasecmp( style, "children" ) ) {
572                                                                         a->acl_attrval_style = ACL_STYLE_CHILDREN;
573                                                                 } else {
574                                                                         char    buf[ SLAP_TEXT_BUFLEN ];
575
576                                                                         snprintf( buf, sizeof( buf ),
577                                                                                 "unknown val.<style> \"%s\" for attributeType \"%s\" "
578                                                                                         "with DN syntax.",
579                                                                                 style,
580                                                                                 a->acl_attrs[0].an_desc->ad_cname.bv_val );
581
582                                                                         Debug( LDAP_DEBUG_CONFIG | LDAP_DEBUG_ACL, 
583                                                                                 "%s: line %d: %s\n",
584                                                                                 fname, lineno, buf );
585                                                                         goto fail;
586                                                                 }
587
588                                                                 rc = dnNormalize( 0, NULL, NULL, &bv, &a->acl_attrval, NULL );
589                                                                 if ( rc != LDAP_SUCCESS ) {
590                                                                         char    buf[ SLAP_TEXT_BUFLEN ];
591
592                                                                         snprintf( buf, sizeof( buf ),
593                                                                                 "unable to normalize DN \"%s\" "
594                                                                                 "for attributeType \"%s\" (%d).",
595                                                                                 bv.bv_val,
596                                                                                 a->acl_attrs[0].an_desc->ad_cname.bv_val,
597                                                                                 rc );
598                                                                         Debug( LDAP_DEBUG_ANY, 
599                                                                                 "%s: line %d: %s\n",
600                                                                                 fname, lineno, buf );
601                                                                         goto fail;
602                                                                 }
603
604                                                         } else {
605                                                                 char    buf[ SLAP_TEXT_BUFLEN ];
606
607                                                                 snprintf( buf, sizeof( buf ),
608                                                                         "unknown val.<style> \"%s\" for attributeType \"%s\".",
609                                                                         style, a->acl_attrs[0].an_desc->ad_cname.bv_val );
610                                                                 Debug( LDAP_DEBUG_CONFIG | LDAP_DEBUG_ACL, 
611                                                                         "%s: line %d: %s\n",
612                                                                         fname, lineno, buf );
613                                                                 goto fail;
614                                                         }
615                                                 }
616                                         }
617
618                                         /* Check for appropriate matching rule */
619                                         if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
620                                                 ber_dupbv( &a->acl_attrval, &bv );
621
622                                         } else if ( BER_BVISNULL( &a->acl_attrval ) ) {
623                                                 int             rc;
624                                                 const char      *text;
625
626                                                 if ( a->acl_attrval_mr == NULL ) {
627                                                         a->acl_attrval_mr = a->acl_attrs[ 0 ].an_desc->ad_type->sat_equality;
628                                                 }
629
630                                                 if ( a->acl_attrval_mr == NULL ) {
631                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
632                                                                 "attr \"%s\" does not have an EQUALITY matching rule.\n",
633                                                                 fname, lineno, a->acl_attrs[ 0 ].an_name.bv_val );
634                                                         goto fail;
635                                                 }
636
637                                                 rc = asserted_value_validate_normalize(
638                                                         a->acl_attrs[ 0 ].an_desc,
639                                                         a->acl_attrval_mr,
640                                                         SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
641                                                         &bv,
642                                                         &a->acl_attrval,
643                                                         &text,
644                                                         NULL );
645                                                 if ( rc != LDAP_SUCCESS ) {
646                                                         char    buf[ SLAP_TEXT_BUFLEN ];
647
648                                                         snprintf( buf, sizeof( buf ), "%s: line %d: "
649                                                                 " attr \"%s\" normalization failed (%d: %s)",
650                                                                 fname, lineno,
651                                                                 a->acl_attrs[ 0 ].an_name.bv_val, rc, text );
652                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: %s.\n",
653                                                                 fname, lineno, buf );
654                                                         goto fail;
655                                                 }
656                                         }
657
658                                 } else {
659                                         Debug( LDAP_DEBUG_ANY,
660                                                 "%s: line %d: expecting <what> got \"%s\"\n",
661                                             fname, lineno, left );
662                                         goto fail;
663                                 }
664                         }
665
666                         if ( !BER_BVISNULL( &a->acl_dn_pat ) && 
667                                         ber_bvccmp( &a->acl_dn_pat, '*' ) )
668                         {
669                                 free( a->acl_dn_pat.bv_val );
670                                 BER_BVZERO( &a->acl_dn_pat );
671                                 a->acl_dn_style = ACL_STYLE_REGEX;
672                         }
673                         
674                         if ( !BER_BVISEMPTY( &a->acl_dn_pat ) ||
675                                         a->acl_dn_style != ACL_STYLE_REGEX ) 
676                         {
677                                 if ( a->acl_dn_style != ACL_STYLE_REGEX ) {
678                                         struct berval bv;
679                                         rc = dnNormalize( 0, NULL, NULL, &a->acl_dn_pat, &bv, NULL);
680                                         if ( rc != LDAP_SUCCESS ) {
681                                                 Debug( LDAP_DEBUG_ANY,
682                                                         "%s: line %d: bad DN \"%s\" in to DN clause\n",
683                                                         fname, lineno, a->acl_dn_pat.bv_val );
684                                                 goto fail;
685                                         }
686                                         free( a->acl_dn_pat.bv_val );
687                                         a->acl_dn_pat = bv;
688
689                                 } else {
690                                         int e = regcomp( &a->acl_dn_re, a->acl_dn_pat.bv_val,
691                                                 REG_EXTENDED | REG_ICASE );
692                                         if ( e ) {
693                                                 char    err[ SLAP_TEXT_BUFLEN ],
694                                                         buf[ SLAP_TEXT_BUFLEN ];
695
696                                                 regerror( e, &a->acl_dn_re, err, sizeof( err ) );
697                                                 snprintf( buf, sizeof( buf ),
698                                                         "regular expression \"%s\" bad because of %s",
699                                                         right, err );
700                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: %s\n",
701                                                         fname, lineno, buf );
702                                                 goto fail;
703                                         }
704                                 }
705                         }
706
707                 /* by clause - select who has what access to entries */
708                 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
709                         if ( a == NULL ) {
710                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
711                                         "to clause required before by clause in access line\n",
712                                         fname, lineno, 0 );
713                                 goto fail;
714                         }
715
716                         /*
717                          * by clause consists of <who> and <access>
718                          */
719
720                         if ( ++i == argc ) {
721                                 Debug( LDAP_DEBUG_ANY,
722                                         "%s: line %d: premature EOL: expecting <who>\n",
723                                         fname, lineno, 0 );
724                                 goto fail;
725                         }
726
727                         b = (Access *) ch_calloc( 1, sizeof(Access) );
728
729                         ACL_INVALIDATE( b->a_access_mask );
730
731                         /* get <who> */
732                         for ( ; i < argc; i++ ) {
733                                 slap_style_t    sty = ACL_STYLE_REGEX;
734                                 char            *style_modifier = NULL;
735                                 char            *style_level = NULL;
736                                 int             level = 0;
737                                 int             expand = 0;
738                                 slap_dn_access  *bdn = &b->a_dn;
739                                 int             is_realdn = 0;
740
741                                 split( argv[i], '=', &left, &right );
742                                 split( left, '.', &left, &style );
743                                 if ( style ) {
744                                         split( style, ',', &style, &style_modifier );
745
746                                         if ( strncasecmp( style, "level", STRLENOF( "level" ) ) == 0 ) {
747                                                 split( style, '{', &style, &style_level );
748                                                 if ( style_level != NULL ) {
749                                                         char *p = strchr( style_level, '}' );
750                                                         if ( p == NULL ) {
751                                                                 Debug( LDAP_DEBUG_ANY,
752                                                                         "%s: line %d: premature eol: "
753                                                                         "expecting closing '}' in \"level{n}\"\n",
754                                                                         fname, lineno, 0 );
755                                                                 goto fail;
756                                                         } else if ( p == style_level ) {
757                                                                 Debug( LDAP_DEBUG_ANY,
758                                                                         "%s: line %d: empty level "
759                                                                         "in \"level{n}\"\n",
760                                                                         fname, lineno, 0 );
761                                                                 goto fail;
762                                                         }
763                                                         p[0] = '\0';
764                                                 }
765                                         }
766                                 }
767
768                                 if ( style == NULL || *style == '\0' ||
769                                         strcasecmp( style, "exact" ) == 0 ||
770                                         strcasecmp( style, "baseObject" ) == 0 ||
771                                         strcasecmp( style, "base" ) == 0 )
772                                 {
773                                         sty = ACL_STYLE_BASE;
774
775                                 } else if ( strcasecmp( style, "onelevel" ) == 0 ||
776                                         strcasecmp( style, "one" ) == 0 )
777                                 {
778                                         sty = ACL_STYLE_ONE;
779
780                                 } else if ( strcasecmp( style, "subtree" ) == 0 ||
781                                         strcasecmp( style, "sub" ) == 0 )
782                                 {
783                                         sty = ACL_STYLE_SUBTREE;
784
785                                 } else if ( strcasecmp( style, "children" ) == 0 ) {
786                                         sty = ACL_STYLE_CHILDREN;
787
788                                 } else if ( strcasecmp( style, "level" ) == 0 )
789                                 {
790                                         if ( lutil_atoi( &level, style_level ) != 0 ) {
791                                                 Debug( LDAP_DEBUG_ANY,
792                                                         "%s: line %d: unable to parse level "
793                                                         "in \"level{n}\"\n",
794                                                         fname, lineno, 0 );
795                                                 goto fail;
796                                         }
797
798                                         sty = ACL_STYLE_LEVEL;
799
800                                 } else if ( strcasecmp( style, "regex" ) == 0 ) {
801                                         sty = ACL_STYLE_REGEX;
802
803                                 } else if ( strcasecmp( style, "expand" ) == 0 ) {
804                                         sty = ACL_STYLE_EXPAND;
805
806                                 } else if ( strcasecmp( style, "ip" ) == 0 ) {
807                                         sty = ACL_STYLE_IP;
808
809                                 } else if ( strcasecmp( style, "ipv6" ) == 0 ) {
810 #ifndef LDAP_PF_INET6
811                                         Debug( LDAP_DEBUG_ANY,
812                                                 "%s: line %d: IPv6 not supported\n",
813                                                 fname, lineno, 0 );
814 #endif /* ! LDAP_PF_INET6 */
815                                         sty = ACL_STYLE_IPV6;
816
817                                 } else if ( strcasecmp( style, "path" ) == 0 ) {
818                                         sty = ACL_STYLE_PATH;
819 #ifndef LDAP_PF_LOCAL
820                                         Debug( LDAP_DEBUG_CONFIG | LDAP_DEBUG_ACL,
821                                                 "%s: line %d: "
822                                                 "\"path\" style modifier is useless without local.\n",
823                                                 fname, lineno, 0 );
824                                         goto fail;
825 #endif /* LDAP_PF_LOCAL */
826
827                                 } else {
828                                         Debug( LDAP_DEBUG_ANY,
829                                                 "%s: line %d: unknown style \"%s\" in by clause\n",
830                                                 fname, lineno, style );
831                                         goto fail;
832                                 }
833
834                                 if ( style_modifier &&
835                                         strcasecmp( style_modifier, "expand" ) == 0 )
836                                 {
837                                         switch ( sty ) {
838                                         case ACL_STYLE_REGEX:
839                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
840                                                         "\"regex\" style implies \"expand\" modifier.\n",
841                                                         fname, lineno, 0 );
842                                                 goto fail;
843                                                 break;
844
845                                         case ACL_STYLE_EXPAND:
846                                                 break;
847
848                                         default:
849                                                 /* we'll see later if it's pertinent */
850                                                 expand = 1;
851                                                 break;
852                                         }
853                                 }
854
855                                 if ( strncasecmp( left, "real", STRLENOF( "real" ) ) == 0 ) {
856                                         is_realdn = 1;
857                                         bdn = &b->a_realdn;
858                                         left += STRLENOF( "real" );
859                                 }
860
861                                 if ( strcasecmp( left, "*" ) == 0 ) {
862                                         if ( is_realdn ) {
863                                                 goto fail;
864                                         }
865
866                                         ber_str2bv( "*", STRLENOF( "*" ), 1, &bv );
867                                         sty = ACL_STYLE_REGEX;
868
869                                 } else if ( strcasecmp( left, "anonymous" ) == 0 ) {
870                                         ber_str2bv("anonymous", STRLENOF( "anonymous" ), 1, &bv);
871                                         sty = ACL_STYLE_ANONYMOUS;
872
873                                 } else if ( strcasecmp( left, "users" ) == 0 ) {
874                                         ber_str2bv("users", STRLENOF( "users" ), 1, &bv);
875                                         sty = ACL_STYLE_USERS;
876
877                                 } else if ( strcasecmp( left, "self" ) == 0 ) {
878                                         ber_str2bv("self", STRLENOF( "self" ), 1, &bv);
879                                         sty = ACL_STYLE_SELF;
880
881                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
882                                         if ( sty == ACL_STYLE_REGEX ) {
883                                                 bdn->a_style = ACL_STYLE_REGEX;
884                                                 if ( right == NULL ) {
885                                                         /* no '=' */
886                                                         ber_str2bv("users",
887                                                                 STRLENOF( "users" ),
888                                                                 1, &bv);
889                                                         bdn->a_style = ACL_STYLE_USERS;
890
891                                                 } else if (*right == '\0' ) {
892                                                         /* dn="" */
893                                                         ber_str2bv("anonymous",
894                                                                 STRLENOF( "anonymous" ),
895                                                                 1, &bv);
896                                                         bdn->a_style = ACL_STYLE_ANONYMOUS;
897
898                                                 } else if ( strcmp( right, "*" ) == 0 ) {
899                                                         /* dn=* */
900                                                         /* any or users?  users for now */
901                                                         ber_str2bv("users",
902                                                                 STRLENOF( "users" ),
903                                                                 1, &bv);
904                                                         bdn->a_style = ACL_STYLE_USERS;
905
906                                                 } else if ( strcmp( right, ".+" ) == 0
907                                                         || strcmp( right, "^.+" ) == 0
908                                                         || strcmp( right, ".+$" ) == 0
909                                                         || strcmp( right, "^.+$" ) == 0
910                                                         || strcmp( right, ".+$$" ) == 0
911                                                         || strcmp( right, "^.+$$" ) == 0 )
912                                                 {
913                                                         ber_str2bv("users",
914                                                                 STRLENOF( "users" ),
915                                                                 1, &bv);
916                                                         bdn->a_style = ACL_STYLE_USERS;
917
918                                                 } else if ( strcmp( right, ".*" ) == 0
919                                                         || strcmp( right, "^.*" ) == 0
920                                                         || strcmp( right, ".*$" ) == 0
921                                                         || strcmp( right, "^.*$" ) == 0
922                                                         || strcmp( right, ".*$$" ) == 0
923                                                         || strcmp( right, "^.*$$" ) == 0 )
924                                                 {
925                                                         ber_str2bv("*",
926                                                                 STRLENOF( "*" ),
927                                                                 1, &bv);
928
929                                                 } else {
930                                                         acl_regex_normalized_dn( right, &bv );
931                                                         if ( !ber_bvccmp( &bv, '*' ) ) {
932                                                                 regtest( fname, lineno, bv.bv_val );
933                                                         }
934                                                 }
935
936                                         } else if ( right == NULL || *right == '\0' ) {
937                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
938                                                         "missing \"=\" in (or value after) \"%s\" "
939                                                         "in by clause\n",
940                                                         fname, lineno, left );
941                                                 goto fail;
942
943                                         } else {
944                                                 ber_str2bv( right, 0, 1, &bv );
945                                         }
946
947                                 } else {
948                                         BER_BVZERO( &bv );
949                                 }
950
951                                 if ( !BER_BVISNULL( &bv ) ) {
952                                         if ( !BER_BVISEMPTY( &bdn->a_pat ) ) {
953                                                 Debug( LDAP_DEBUG_ANY,
954                                                         "%s: line %d: dn pattern already specified.\n",
955                                                         fname, lineno, 0 );
956                                                 goto fail;
957                                         }
958
959                                         if ( sty != ACL_STYLE_REGEX &&
960                                                         sty != ACL_STYLE_ANONYMOUS &&
961                                                         sty != ACL_STYLE_USERS &&
962                                                         sty != ACL_STYLE_SELF &&
963                                                         expand == 0 )
964                                         {
965                                                 rc = dnNormalize(0, NULL, NULL,
966                                                         &bv, &bdn->a_pat, NULL);
967                                                 if ( rc != LDAP_SUCCESS ) {
968                                                         Debug( LDAP_DEBUG_ANY,
969                                                                 "%s: line %d: bad DN \"%s\" in by DN clause\n",
970                                                                 fname, lineno, bv.bv_val );
971                                                         goto fail;
972                                                 }
973                                                 free( bv.bv_val );
974                                                 if ( sty == ACL_STYLE_BASE
975                                                         && be != NULL
976                                                         && !BER_BVISNULL( &be->be_rootndn )
977                                                         && dn_match( &bdn->a_pat, &be->be_rootndn ) )
978                                                 {
979                                                         Debug( LDAP_DEBUG_ANY,
980                                                                 "%s: line %d: rootdn is always granted "
981                                                                 "unlimited privileges.\n",
982                                                                 fname, lineno, 0 );
983                                                 }
984
985                                         } else {
986                                                 bdn->a_pat = bv;
987                                         }
988                                         bdn->a_style = sty;
989                                         if ( expand ) {
990                                                 char    *exp;
991                                                 int     gotit = 0;
992
993                                                 for ( exp = strchr( bdn->a_pat.bv_val, '$' );
994                                                         exp && (ber_len_t)(exp - bdn->a_pat.bv_val)
995                                                                 < bdn->a_pat.bv_len;
996                                                         exp = strchr( exp, '$' ) )
997                                                 {
998                                                         if ( ( isdigit( (unsigned char) exp[ 1 ] ) ||
999                                                                     exp[ 1 ] == '{' ) ) {
1000                                                                 gotit = 1;
1001                                                                 break;
1002                                                         }
1003                                                 }
1004
1005                                                 if ( gotit == 1 ) {
1006                                                         bdn->a_expand = expand;
1007
1008                                                 } else {
1009                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1010                                                                 "\"expand\" used with no expansions in \"pattern\".\n",
1011                                                                 fname, lineno, 0 );
1012                                                         goto fail;
1013                                                 } 
1014                                         }
1015                                         if ( sty == ACL_STYLE_SELF ) {
1016                                                 bdn->a_self_level = level;
1017
1018                                         } else {
1019                                                 if ( level < 0 ) {
1020                                                         Debug( LDAP_DEBUG_ANY,
1021                                                                 "%s: line %d: bad negative level \"%d\" "
1022                                                                 "in by DN clause\n",
1023                                                                 fname, lineno, level );
1024                                                         goto fail;
1025                                                 } else if ( level == 1 ) {
1026                                                         Debug( LDAP_DEBUG_ANY,
1027                                                                 "%s: line %d: \"onelevel\" should be used "
1028                                                                 "instead of \"level{1}\" in by DN clause\n",
1029                                                                 fname, lineno, 0 );
1030                                                 } else if ( level == 0 && sty == ACL_STYLE_LEVEL ) {
1031                                                         Debug( LDAP_DEBUG_ANY,
1032                                                                 "%s: line %d: \"base\" should be used "
1033                                                                 "instead of \"level{0}\" in by DN clause\n",
1034                                                                 fname, lineno, 0 );
1035                                                 }
1036
1037                                                 bdn->a_level = level;
1038                                         }
1039                                         continue;
1040                                 }
1041
1042                                 if ( strcasecmp( left, "dnattr" ) == 0 ) {
1043                                         if ( right == NULL || right[0] == '\0' ) {
1044                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1045                                                         "missing \"=\" in (or value after) \"%s\" "
1046                                                         "in by clause\n",
1047                                                         fname, lineno, left );
1048                                                 goto fail;
1049                                         }
1050
1051                                         if( bdn->a_at != NULL ) {
1052                                                 Debug( LDAP_DEBUG_ANY,
1053                                                         "%s: line %d: dnattr already specified.\n",
1054                                                         fname, lineno, 0 );
1055                                                 goto fail;
1056                                         }
1057
1058                                         rc = slap_str2ad( right, &bdn->a_at, &text );
1059
1060                                         if( rc != LDAP_SUCCESS ) {
1061                                                 char    buf[ SLAP_TEXT_BUFLEN ];
1062
1063                                                 snprintf( buf, sizeof( buf ),
1064                                                         "dnattr \"%s\": %s",
1065                                                         right, text );
1066                                                 Debug( LDAP_DEBUG_ANY,
1067                                                         "%s: line %d: %s\n",
1068                                                         fname, lineno, buf );
1069                                                 goto fail;
1070                                         }
1071
1072
1073                                         if( !is_at_syntax( bdn->a_at->ad_type,
1074                                                 SLAPD_DN_SYNTAX ) &&
1075                                                 !is_at_syntax( bdn->a_at->ad_type,
1076                                                 SLAPD_NAMEUID_SYNTAX ))
1077                                         {
1078                                                 char    buf[ SLAP_TEXT_BUFLEN ];
1079
1080                                                 snprintf( buf, sizeof( buf ),
1081                                                         "dnattr \"%s\": "
1082                                                         "inappropriate syntax: %s\n",
1083                                                         right,
1084                                                         bdn->a_at->ad_type->sat_syntax_oid );
1085                                                 Debug( LDAP_DEBUG_ANY,
1086                                                         "%s: line %d: %s\n",
1087                                                         fname, lineno, buf );
1088                                                 goto fail;
1089                                         }
1090
1091                                         if( bdn->a_at->ad_type->sat_equality == NULL ) {
1092                                                 Debug( LDAP_DEBUG_ANY,
1093                                                         "%s: line %d: dnattr \"%s\": "
1094                                                         "inappropriate matching (no EQUALITY)\n",
1095                                                         fname, lineno, right );
1096                                                 goto fail;
1097                                         }
1098
1099                                         continue;
1100                                 }
1101
1102                                 if ( strncasecmp( left, "group", STRLENOF( "group" ) ) == 0 ) {
1103                                         char *name = NULL;
1104                                         char *value = NULL;
1105                                         char *attr_name = SLAPD_GROUP_ATTR;
1106
1107                                         switch ( sty ) {
1108                                         case ACL_STYLE_REGEX:
1109                                                 /* legacy, tolerated */
1110                                                 Debug( LDAP_DEBUG_CONFIG | LDAP_DEBUG_ACL,
1111                                                         "%s: line %d: "
1112                                                         "deprecated group style \"regex\"; "
1113                                                         "use \"expand\" instead.\n",
1114                                                         fname, lineno, 0 );
1115                                                 sty = ACL_STYLE_EXPAND;
1116                                                 break;
1117
1118                                         case ACL_STYLE_BASE:
1119                                                 /* legal, traditional */
1120                                         case ACL_STYLE_EXPAND:
1121                                                 /* legal, substring expansion; supersedes regex */
1122                                                 break;
1123
1124                                         default:
1125                                                 /* unknown */
1126                                                 Debug( LDAP_DEBUG_ANY,
1127                                                         "%s: line %d: "
1128                                                         "inappropriate style \"%s\" in by clause.\n",
1129                                                         fname, lineno, style );
1130                                                 goto fail;
1131                                         }
1132
1133                                         if ( right == NULL || right[0] == '\0' ) {
1134                                                 Debug( LDAP_DEBUG_ANY,
1135                                                         "%s: line %d: "
1136                                                         "missing \"=\" in (or value after) \"%s\" "
1137                                                         "in by clause.\n",
1138                                                         fname, lineno, left );
1139                                                 goto fail;
1140                                         }
1141
1142                                         if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
1143                                                 Debug( LDAP_DEBUG_ANY,
1144                                                         "%s: line %d: group pattern already specified.\n",
1145                                                         fname, lineno, 0 );
1146                                                 goto fail;
1147                                         }
1148
1149                                         /* format of string is
1150                                                 "group/objectClassValue/groupAttrName" */
1151                                         if ( ( value = strchr(left, '/') ) != NULL ) {
1152                                                 *value++ = '\0';
1153                                                 if ( *value && ( name = strchr( value, '/' ) ) != NULL ) {
1154                                                         *name++ = '\0';
1155                                                 }
1156                                         }
1157
1158                                         b->a_group_style = sty;
1159                                         if ( sty == ACL_STYLE_EXPAND ) {
1160                                                 acl_regex_normalized_dn( right, &bv );
1161                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1162                                                         regtest( fname, lineno, bv.bv_val );
1163                                                 }
1164                                                 b->a_group_pat = bv;
1165
1166                                         } else {
1167                                                 ber_str2bv( right, 0, 0, &bv );
1168                                                 rc = dnNormalize( 0, NULL, NULL, &bv,
1169                                                         &b->a_group_pat, NULL );
1170                                                 if ( rc != LDAP_SUCCESS ) {
1171                                                         Debug( LDAP_DEBUG_ANY,
1172                                                                 "%s: line %d: bad DN \"%s\".\n",
1173                                                                 fname, lineno, right );
1174                                                         goto fail;
1175                                                 }
1176                                         }
1177
1178                                         if ( value && *value ) {
1179                                                 b->a_group_oc = oc_find( value );
1180                                                 *--value = '/';
1181
1182                                                 if ( b->a_group_oc == NULL ) {
1183                                                         Debug( LDAP_DEBUG_ANY,
1184                                                                 "%s: line %d: group objectclass "
1185                                                                 "\"%s\" unknown.\n",
1186                                                                 fname, lineno, value );
1187                                                         goto fail;
1188                                                 }
1189
1190                                         } else {
1191                                                 b->a_group_oc = oc_find( SLAPD_GROUP_CLASS );
1192
1193                                                 if( b->a_group_oc == NULL ) {
1194                                                         Debug( LDAP_DEBUG_ANY,
1195                                                                 "%s: line %d: group default objectclass "
1196                                                                 "\"%s\" unknown.\n",
1197                                                                 fname, lineno, SLAPD_GROUP_CLASS );
1198                                                         goto fail;
1199                                                 }
1200                                         }
1201
1202                                         if ( is_object_subclass( slap_schema.si_oc_referral,
1203                                                 b->a_group_oc ) )
1204                                         {
1205                                                 Debug( LDAP_DEBUG_ANY,
1206                                                         "%s: line %d: group objectclass \"%s\" "
1207                                                         "is subclass of referral.\n",
1208                                                         fname, lineno, value );
1209                                                 goto fail;
1210                                         }
1211
1212                                         if ( is_object_subclass( slap_schema.si_oc_alias,
1213                                                 b->a_group_oc ) )
1214                                         {
1215                                                 Debug( LDAP_DEBUG_ANY,
1216                                                         "%s: line %d: group objectclass \"%s\" "
1217                                                         "is subclass of alias.\n",
1218                                                         fname, lineno, value );
1219                                                 goto fail;
1220                                         }
1221
1222                                         if ( name && *name ) {
1223                                                 attr_name = name;
1224                                                 *--name = '/';
1225
1226                                         }
1227
1228                                         rc = slap_str2ad( attr_name, &b->a_group_at, &text );
1229                                         if ( rc != LDAP_SUCCESS ) {
1230                                                 char    buf[ SLAP_TEXT_BUFLEN ];
1231
1232                                                 snprintf( buf, sizeof( buf ),
1233                                                         "group \"%s\": %s.",
1234                                                         right, text );
1235                                                 Debug( LDAP_DEBUG_ANY,
1236                                                         "%s: line %d: %s\n",
1237                                                         fname, lineno, buf );
1238                                                 goto fail;
1239                                         }
1240
1241                                         if ( !is_at_syntax( b->a_group_at->ad_type,
1242                                                         SLAPD_DN_SYNTAX ) /* e.g. "member" */
1243                                                 && !is_at_syntax( b->a_group_at->ad_type,
1244                                                         SLAPD_NAMEUID_SYNTAX ) /* e.g. memberUID */
1245                                                 && !is_at_subtype( b->a_group_at->ad_type,
1246                                                         slap_schema.si_ad_labeledURI->ad_type ) /* e.g. memberURL */ )
1247                                         {
1248                                                 char    buf[ SLAP_TEXT_BUFLEN ];
1249
1250                                                 snprintf( buf, sizeof( buf ),
1251                                                         "group \"%s\" attr \"%s\": inappropriate syntax: %s; "
1252                                                         "must be " SLAPD_DN_SYNTAX " (DN), "
1253                                                         SLAPD_NAMEUID_SYNTAX " (NameUID) "
1254                                                         "or a subtype of labeledURI.",
1255                                                         right,
1256                                                         attr_name,
1257                                                         at_syntax( b->a_group_at->ad_type ) );
1258                                                 Debug( LDAP_DEBUG_ANY,
1259                                                         "%s: line %d: %s\n",
1260                                                         fname, lineno, buf );
1261                                                 goto fail;
1262                                         }
1263
1264
1265                                         {
1266                                                 int rc;
1267                                                 ObjectClass *ocs[2];
1268
1269                                                 ocs[0] = b->a_group_oc;
1270                                                 ocs[1] = NULL;
1271
1272                                                 rc = oc_check_allowed( b->a_group_at->ad_type,
1273                                                         ocs, NULL );
1274
1275                                                 if( rc != 0 ) {
1276                                                         char    buf[ SLAP_TEXT_BUFLEN ];
1277
1278                                                         snprintf( buf, sizeof( buf ),
1279                                                                 "group: \"%s\" not allowed by \"%s\".",
1280                                                                 b->a_group_at->ad_cname.bv_val,
1281                                                                 b->a_group_oc->soc_oid );
1282                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: %s\n",
1283                                                                 fname, lineno, buf );
1284                                                         goto fail;
1285                                                 }
1286                                         }
1287                                         continue;
1288                                 }
1289
1290                                 if ( strcasecmp( left, "peername" ) == 0 ) {
1291                                         switch ( sty ) {
1292                                         case ACL_STYLE_REGEX:
1293                                         case ACL_STYLE_BASE:
1294                                                 /* legal, traditional */
1295                                         case ACL_STYLE_EXPAND:
1296                                                 /* cheap replacement to regex for simple expansion */
1297                                         case ACL_STYLE_IP:
1298                                         case ACL_STYLE_IPV6:
1299                                         case ACL_STYLE_PATH:
1300                                                 /* legal, peername specific */
1301                                                 break;
1302
1303                                         default:
1304                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1305                                                         "inappropriate style \"%s\" in by clause.\n",
1306                                                     fname, lineno, style );
1307                                                 goto fail;
1308                                         }
1309
1310                                         if ( right == NULL || right[0] == '\0' ) {
1311                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1312                                                         "missing \"=\" in (or value after) \"%s\" "
1313                                                         "in by clause.\n",
1314                                                         fname, lineno, left );
1315                                                 goto fail;
1316                                         }
1317
1318                                         if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
1319                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1320                                                         "peername pattern already specified.\n",
1321                                                         fname, lineno, 0 );
1322                                                 goto fail;
1323                                         }
1324
1325                                         b->a_peername_style = sty;
1326                                         if ( sty == ACL_STYLE_REGEX ) {
1327                                                 acl_regex_normalized_dn( right, &bv );
1328                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1329                                                         regtest( fname, lineno, bv.bv_val );
1330                                                 }
1331                                                 b->a_peername_pat = bv;
1332
1333                                         } else {
1334                                                 ber_str2bv( right, 0, 1, &b->a_peername_pat );
1335
1336                                                 if ( sty == ACL_STYLE_IP ) {
1337                                                         char            *addr = NULL,
1338                                                                         *mask = NULL,
1339                                                                         *port = NULL;
1340
1341                                                         split( right, '{', &addr, &port );
1342                                                         split( addr, '%', &addr, &mask );
1343
1344                                                         b->a_peername_addr = inet_addr( addr );
1345                                                         if ( b->a_peername_addr == (unsigned long)(-1) ) {
1346                                                                 /* illegal address */
1347                                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1348                                                                         "illegal peername address \"%s\".\n",
1349                                                                         fname, lineno, addr );
1350                                                                 goto fail;
1351                                                         }
1352
1353                                                         b->a_peername_mask = (unsigned long)(-1);
1354                                                         if ( mask != NULL ) {
1355                                                                 b->a_peername_mask = inet_addr( mask );
1356                                                                 if ( b->a_peername_mask ==
1357                                                                         (unsigned long)(-1) )
1358                                                                 {
1359                                                                         /* illegal mask */
1360                                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1361                                                                                 "illegal peername address mask "
1362                                                                                 "\"%s\".\n",
1363                                                                                 fname, lineno, mask );
1364                                                                         goto fail;
1365                                                                 }
1366                                                         } 
1367
1368                                                         b->a_peername_port = -1;
1369                                                         if ( port ) {
1370                                                                 char    *end = NULL;
1371
1372                                                                 b->a_peername_port = strtol( port, &end, 10 );
1373                                                                 if ( end == port || end[0] != '}' ) {
1374                                                                         /* illegal port */
1375                                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1376                                                                                 "illegal peername port specification "
1377                                                                                 "\"{%s}\".\n",
1378                                                                                 fname, lineno, port );
1379                                                                         goto fail;
1380                                                                 }
1381                                                         }
1382
1383 #ifdef LDAP_PF_INET6
1384                                                 } else if ( sty == ACL_STYLE_IPV6 ) {
1385                                                         char            *addr = NULL,
1386                                                                         *mask = NULL,
1387                                                                         *port = NULL;
1388
1389                                                         split( right, '{', &addr, &port );
1390                                                         split( addr, '%', &addr, &mask );
1391
1392                                                         if ( inet_pton( AF_INET6, addr, &b->a_peername_addr6 ) != 1 ) {
1393                                                                 /* illegal address */
1394                                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1395                                                                         "illegal peername address \"%s\".\n",
1396                                                                         fname, lineno, addr );
1397                                                                 goto fail;
1398                                                         }
1399
1400                                                         if ( mask == NULL ) {
1401                                                                 mask = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
1402                                                         }
1403
1404                                                         if ( inet_pton( AF_INET6, mask, &b->a_peername_mask6 ) != 1 ) {
1405                                                                 /* illegal mask */
1406                                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1407                                                                         "illegal peername address mask "
1408                                                                         "\"%s\".\n",
1409                                                                         fname, lineno, mask );
1410                                                                 goto fail;
1411                                                         }
1412
1413                                                         b->a_peername_port = -1;
1414                                                         if ( port ) {
1415                                                                 char    *end = NULL;
1416
1417                                                                 b->a_peername_port = strtol( port, &end, 10 );
1418                                                                 if ( end == port || end[0] != '}' ) {
1419                                                                         /* illegal port */
1420                                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1421                                                                                 "illegal peername port specification "
1422                                                                                 "\"{%s}\".\n",
1423                                                                                 fname, lineno, port );
1424                                                                         goto fail;
1425                                                                 }
1426                                                         }
1427 #endif /* LDAP_PF_INET6 */
1428                                                 }
1429                                         }
1430                                         continue;
1431                                 }
1432
1433                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
1434                                         switch ( sty ) {
1435                                         case ACL_STYLE_REGEX:
1436                                         case ACL_STYLE_BASE:
1437                                                 /* legal, traditional */
1438                                         case ACL_STYLE_EXPAND:
1439                                                 /* cheap replacement to regex for simple expansion */
1440                                                 break;
1441
1442                                         default:
1443                                                 /* unknown */
1444                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1445                                                         "inappropriate style \"%s\" in by clause\n",
1446                                                     fname, lineno, style );
1447                                                 goto fail;
1448                                         }
1449
1450                                         if ( right == NULL || right[0] == '\0' ) {
1451                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1452                                                         "missing \"=\" in (or value after) \"%s\" "
1453                                                         "in by clause\n",
1454                                                         fname, lineno, left );
1455                                                 goto fail;
1456                                         }
1457
1458                                         if ( !BER_BVISNULL( &b->a_sockname_pat ) ) {
1459                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1460                                                         "sockname pattern already specified.\n",
1461                                                         fname, lineno, 0 );
1462                                                 goto fail;
1463                                         }
1464
1465                                         b->a_sockname_style = sty;
1466                                         if ( sty == ACL_STYLE_REGEX ) {
1467                                                 acl_regex_normalized_dn( right, &bv );
1468                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1469                                                         regtest( fname, lineno, bv.bv_val );
1470                                                 }
1471                                                 b->a_sockname_pat = bv;
1472                                                 
1473                                         } else {
1474                                                 ber_str2bv( right, 0, 1, &b->a_sockname_pat );
1475                                         }
1476                                         continue;
1477                                 }
1478
1479                                 if ( strcasecmp( left, "domain" ) == 0 ) {
1480                                         switch ( sty ) {
1481                                         case ACL_STYLE_REGEX:
1482                                         case ACL_STYLE_BASE:
1483                                         case ACL_STYLE_SUBTREE:
1484                                                 /* legal, traditional */
1485                                                 break;
1486
1487                                         case ACL_STYLE_EXPAND:
1488                                                 /* tolerated: means exact,expand */
1489                                                 if ( expand ) {
1490                                                         Debug( LDAP_DEBUG_ANY,
1491                                                                 "%s: line %d: "
1492                                                                 "\"expand\" modifier "
1493                                                                 "with \"expand\" style.\n",
1494                                                                 fname, lineno, 0 );
1495                                                 }
1496                                                 sty = ACL_STYLE_BASE;
1497                                                 expand = 1;
1498                                                 break;
1499
1500                                         default:
1501                                                 /* unknown */
1502                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1503                                                         "inappropriate style \"%s\" in by clause.\n",
1504                                                     fname, lineno, style );
1505                                                 goto fail;
1506                                         }
1507
1508                                         if ( right == NULL || right[0] == '\0' ) {
1509                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1510                                                         "missing \"=\" in (or value after) \"%s\" "
1511                                                         "in by clause.\n",
1512                                                         fname, lineno, left );
1513                                                 goto fail;
1514                                         }
1515
1516                                         if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
1517                                                 Debug( LDAP_DEBUG_ANY,
1518                                                         "%s: line %d: domain pattern already specified.\n",
1519                                                         fname, lineno, 0 );
1520                                                 goto fail;
1521                                         }
1522
1523                                         b->a_domain_style = sty;
1524                                         b->a_domain_expand = expand;
1525                                         if ( sty == ACL_STYLE_REGEX ) {
1526                                                 acl_regex_normalized_dn( right, &bv );
1527                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1528                                                         regtest( fname, lineno, bv.bv_val );
1529                                                 }
1530                                                 b->a_domain_pat = bv;
1531
1532                                         } else {
1533                                                 ber_str2bv( right, 0, 1, &b->a_domain_pat );
1534                                         }
1535                                         continue;
1536                                 }
1537
1538                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
1539                                         switch ( sty ) {
1540                                         case ACL_STYLE_REGEX:
1541                                         case ACL_STYLE_BASE:
1542                                                 /* legal, traditional */
1543                                         case ACL_STYLE_EXPAND:
1544                                                 /* cheap replacement to regex for simple expansion */
1545                                                 break;
1546
1547                                         default:
1548                                                 /* unknown */
1549                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1550                                                         "inappropriate style \"%s\" in by clause.\n",
1551                                                     fname, lineno, style );
1552                                                 goto fail;
1553                                         }
1554
1555                                         if ( right == NULL || right[0] == '\0' ) {
1556                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1557                                                         "missing \"=\" in (or value after) \"%s\" "
1558                                                         "in by clause.\n",
1559                                                         fname, lineno, left );
1560                                                 goto fail;
1561                                         }
1562
1563                                         if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
1564                                                 Debug( LDAP_DEBUG_ANY,
1565                                                         "%s: line %d: sockurl pattern already specified.\n",
1566                                                         fname, lineno, 0 );
1567                                                 goto fail;
1568                                         }
1569
1570                                         b->a_sockurl_style = sty;
1571                                         if ( sty == ACL_STYLE_REGEX ) {
1572                                                 acl_regex_normalized_dn( right, &bv );
1573                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
1574                                                         regtest( fname, lineno, bv.bv_val );
1575                                                 }
1576                                                 b->a_sockurl_pat = bv;
1577                                                 
1578                                         } else {
1579                                                 ber_str2bv( right, 0, 1, &b->a_sockurl_pat );
1580                                         }
1581                                         continue;
1582                                 }
1583
1584                                 if ( strcasecmp( left, "set" ) == 0 ) {
1585                                         switch ( sty ) {
1586                                                 /* deprecated */
1587                                         case ACL_STYLE_REGEX:
1588                                                 Debug( LDAP_DEBUG_CONFIG | LDAP_DEBUG_ACL,
1589                                                         "%s: line %d: "
1590                                                         "deprecated set style "
1591                                                         "\"regex\" in <by> clause; "
1592                                                         "use \"expand\" instead.\n",
1593                                                         fname, lineno, 0 );
1594                                                 sty = ACL_STYLE_EXPAND;
1595                                                 /* FALLTHRU */
1596                                                 
1597                                         case ACL_STYLE_BASE:
1598                                         case ACL_STYLE_EXPAND:
1599                                                 break;
1600
1601                                         default:
1602                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1603                                                         "inappropriate style \"%s\" in by clause.\n",
1604                                                         fname, lineno, style );
1605                                                 goto fail;
1606                                         }
1607
1608                                         if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
1609                                                 Debug( LDAP_DEBUG_ANY,
1610                                                         "%s: line %d: set attribute already specified.\n",
1611                                                         fname, lineno, 0 );
1612                                                 goto fail;
1613                                         }
1614
1615                                         if ( right == NULL || *right == '\0' ) {
1616                                                 Debug( LDAP_DEBUG_ANY,
1617                                                         "%s: line %d: no set is defined.\n",
1618                                                         fname, lineno, 0 );
1619                                                 goto fail;
1620                                         }
1621
1622                                         b->a_set_style = sty;
1623                                         ber_str2bv( right, 0, 1, &b->a_set_pat );
1624
1625                                         continue;
1626                                 }
1627
1628 #ifdef SLAP_DYNACL
1629                                 {
1630                                         char            *name = NULL,
1631                                                         *opts = NULL;
1632
1633 #if 1 /* tolerate legacy "aci" <who> */
1634                                         if ( strcasecmp( left, "aci" ) == 0 ) {
1635                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1636                                                         "undocumented deprecated \"aci\" directive "
1637                                                         "is superseded by \"dynacl/aci\".\n",
1638                                                         fname, lineno, 0 );
1639                                                 name = "aci";
1640                                                 
1641                                         } else
1642 #endif /* tolerate legacy "aci" <who> */
1643                                         if ( strncasecmp( left, "dynacl/", STRLENOF( "dynacl/" ) ) == 0 ) {
1644                                                 name = &left[ STRLENOF( "dynacl/" ) ];
1645                                                 opts = strchr( name, '/' );
1646                                                 if ( opts ) {
1647                                                         opts[ 0 ] = '\0';
1648                                                         opts++;
1649                                                 }
1650                                         }
1651
1652                                         if ( name ) {
1653                                                 if ( slap_dynacl_config( fname, lineno, b, name, opts, sty, right ) ) {
1654                                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1655                                                                 "unable to configure dynacl \"%s\".\n",
1656                                                                 fname, lineno, name );
1657                                                         goto fail;
1658                                                 }
1659
1660                                                 continue;
1661                                         }
1662                                 }
1663 #endif /* SLAP_DYNACL */
1664
1665                                 if ( strcasecmp( left, "ssf" ) == 0 ) {
1666                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1667                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1668                                                         "inappropriate style \"%s\" in by clause.\n",
1669                                                     fname, lineno, style );
1670                                                 goto fail;
1671                                         }
1672
1673                                         if ( b->a_authz.sai_ssf ) {
1674                                                 Debug( LDAP_DEBUG_ANY,
1675                                                         "%s: line %d: ssf attribute already specified.\n",
1676                                                         fname, lineno, 0 );
1677                                                 goto fail;
1678                                         }
1679
1680                                         if ( right == NULL || *right == '\0' ) {
1681                                                 Debug( LDAP_DEBUG_ANY,
1682                                                         "%s: line %d: no ssf is defined.\n",
1683                                                         fname, lineno, 0 );
1684                                                 goto fail;
1685                                         }
1686
1687                                         if ( lutil_atou( &b->a_authz.sai_ssf, right ) != 0 ) {
1688                                                 Debug( LDAP_DEBUG_ANY,
1689                                                         "%s: line %d: unable to parse ssf value (%s).\n",
1690                                                         fname, lineno, right );
1691                                                 goto fail;
1692                                         }
1693
1694                                         if ( !b->a_authz.sai_ssf ) {
1695                                                 Debug( LDAP_DEBUG_ANY,
1696                                                         "%s: line %d: invalid ssf value (%s).\n",
1697                                                         fname, lineno, right );
1698                                                 goto fail;
1699                                         }
1700                                         continue;
1701                                 }
1702
1703                                 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
1704                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1705                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1706                                                         "inappropriate style \"%s\" in by clause.\n",
1707                                                         fname, lineno, style );
1708                                                 goto fail;
1709                                         }
1710
1711                                         if ( b->a_authz.sai_transport_ssf ) {
1712                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1713                                                         "transport_ssf attribute already specified.\n",
1714                                                         fname, lineno, 0 );
1715                                                 goto fail;
1716                                         }
1717
1718                                         if ( right == NULL || *right == '\0' ) {
1719                                                 Debug( LDAP_DEBUG_ANY,
1720                                                         "%s: line %d: no transport_ssf is defined.\n",
1721                                                         fname, lineno, 0 );
1722                                                 goto fail;
1723                                         }
1724
1725                                         if ( lutil_atou( &b->a_authz.sai_transport_ssf, right ) != 0 ) {
1726                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1727                                                         "unable to parse transport_ssf value (%s).\n",
1728                                                         fname, lineno, right );
1729                                                 goto fail;
1730                                         }
1731
1732                                         if ( !b->a_authz.sai_transport_ssf ) {
1733                                                 Debug( LDAP_DEBUG_ANY,
1734                                                         "%s: line %d: invalid transport_ssf value (%s).\n",
1735                                                         fname, lineno, right );
1736                                                 goto fail;
1737                                         }
1738                                         continue;
1739                                 }
1740
1741                                 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
1742                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1743                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1744                                                         "inappropriate style \"%s\" in by clause.\n",
1745                                                         fname, lineno, style );
1746                                                 goto fail;
1747                                         }
1748
1749                                         if ( b->a_authz.sai_tls_ssf ) {
1750                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1751                                                         "tls_ssf attribute already specified.\n",
1752                                                         fname, lineno, 0 );
1753                                                 goto fail;
1754                                         }
1755
1756                                         if ( right == NULL || *right == '\0' ) {
1757                                                 Debug( LDAP_DEBUG_ANY,
1758                                                         "%s: line %d: no tls_ssf is defined\n",
1759                                                         fname, lineno, 0 );
1760                                                 goto fail;
1761                                         }
1762
1763                                         if ( lutil_atou( &b->a_authz.sai_tls_ssf, right ) != 0 ) {
1764                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1765                                                         "unable to parse tls_ssf value (%s).\n",
1766                                                         fname, lineno, right );
1767                                                 goto fail;
1768                                         }
1769
1770                                         if ( !b->a_authz.sai_tls_ssf ) {
1771                                                 Debug( LDAP_DEBUG_ANY,
1772                                                         "%s: line %d: invalid tls_ssf value (%s).\n",
1773                                                         fname, lineno, right );
1774                                                 goto fail;
1775                                         }
1776                                         continue;
1777                                 }
1778
1779                                 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
1780                                         if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
1781                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1782                                                         "inappropriate style \"%s\" in by clause.\n",
1783                                                         fname, lineno, style );
1784                                                 goto fail;
1785                                         }
1786
1787                                         if ( b->a_authz.sai_sasl_ssf ) {
1788                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1789                                                         "sasl_ssf attribute already specified.\n",
1790                                                         fname, lineno, 0 );
1791                                                 goto fail;
1792                                         }
1793
1794                                         if ( right == NULL || *right == '\0' ) {
1795                                                 Debug( LDAP_DEBUG_ANY,
1796                                                         "%s: line %d: no sasl_ssf is defined.\n",
1797                                                         fname, lineno, 0 );
1798                                                 goto fail;
1799                                         }
1800
1801                                         if ( lutil_atou( &b->a_authz.sai_sasl_ssf, right ) != 0 ) {
1802                                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1803                                                         "unable to parse sasl_ssf value (%s).\n",
1804                                                         fname, lineno, right );
1805                                                 goto fail;
1806                                         }
1807
1808                                         if ( !b->a_authz.sai_sasl_ssf ) {
1809                                                 Debug( LDAP_DEBUG_ANY,
1810                                                         "%s: line %d: invalid sasl_ssf value (%s).\n",
1811                                                         fname, lineno, right );
1812                                                 goto fail;
1813                                         }
1814                                         continue;
1815                                 }
1816
1817                                 if ( right != NULL ) {
1818                                         /* unsplit */
1819                                         right[-1] = '=';
1820                                 }
1821                                 break;
1822                         }
1823
1824                         if ( i == argc || ( strcasecmp( left, "stop" ) == 0 ) ) { 
1825                                 /* out of arguments or plain stop */
1826
1827                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1828                                 ACL_PRIV_SET( b->a_access_mask, ACL_PRIV_NONE);
1829                                 b->a_type = ACL_STOP;
1830
1831                                 access_append( &a->acl_access, b );
1832                                 continue;
1833                         }
1834
1835                         if ( strcasecmp( left, "continue" ) == 0 ) {
1836                                 /* plain continue */
1837
1838                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1839                                 ACL_PRIV_SET( b->a_access_mask, ACL_PRIV_NONE);
1840                                 b->a_type = ACL_CONTINUE;
1841
1842                                 access_append( &a->acl_access, b );
1843                                 continue;
1844                         }
1845
1846                         if ( strcasecmp( left, "break" ) == 0 ) {
1847                                 /* plain continue */
1848
1849                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1850                                 ACL_PRIV_SET( b->a_access_mask, ACL_PRIV_NONE);
1851                                 b->a_type = ACL_BREAK;
1852
1853                                 access_append( &a->acl_access, b );
1854                                 continue;
1855                         }
1856
1857                         if ( strcasecmp( left, "by" ) == 0 ) {
1858                                 /* we've gone too far */
1859                                 --i;
1860                                 ACL_PRIV_ASSIGN( b->a_access_mask, ACL_PRIV_ADDITIVE );
1861                                 ACL_PRIV_SET( b->a_access_mask, ACL_PRIV_NONE);
1862                                 b->a_type = ACL_STOP;
1863
1864                                 access_append( &a->acl_access, b );
1865                                 continue;
1866                         }
1867
1868                         /* get <access> */
1869                         {
1870                                 char    *lleft = left;
1871
1872                                 if ( strncasecmp( left, "self", STRLENOF( "self" ) ) == 0 ) {
1873                                         b->a_dn_self = 1;
1874                                         lleft = &left[ STRLENOF( "self" ) ];
1875
1876                                 } else if ( strncasecmp( left, "realself", STRLENOF( "realself" ) ) == 0 ) {
1877                                         b->a_realdn_self = 1;
1878                                         lleft = &left[ STRLENOF( "realself" ) ];
1879                                 }
1880
1881                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( lleft ) );
1882                         }
1883
1884                         if ( ACL_IS_INVALID( b->a_access_mask ) ) {
1885                                 Debug( LDAP_DEBUG_ANY,
1886                                         "%s: line %d: expecting <access> got \"%s\".\n",
1887                                         fname, lineno, left );
1888                                 goto fail;
1889                         }
1890
1891                         b->a_type = ACL_STOP;
1892
1893                         if ( ++i == argc ) {
1894                                 /* out of arguments or plain stop */
1895                                 access_append( &a->acl_access, b );
1896                                 continue;
1897                         }
1898
1899                         if ( strcasecmp( argv[i], "continue" ) == 0 ) {
1900                                 /* plain continue */
1901                                 b->a_type = ACL_CONTINUE;
1902
1903                         } else if ( strcasecmp( argv[i], "break" ) == 0 ) {
1904                                 /* plain continue */
1905                                 b->a_type = ACL_BREAK;
1906
1907                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
1908                                 /* gone to far */
1909                                 i--;
1910                         }
1911
1912                         access_append( &a->acl_access, b );
1913                         b = NULL;
1914
1915                 } else {
1916                         Debug( LDAP_DEBUG_ANY,
1917                                 "%s: line %d: expecting \"to\" "
1918                                 "or \"by\" got \"%s\"\n",
1919                                 fname, lineno, argv[i] );
1920                         goto fail;
1921                 }
1922         }
1923
1924         /* if we have no real access clause, complain and do nothing */
1925         if ( a == NULL ) {
1926                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1927                         "warning: no access clause(s) specified in access line.\n",
1928                         fname, lineno, 0 );
1929                 goto fail;
1930
1931         } else {
1932 #ifdef LDAP_DEBUG
1933                 if ( slap_debug & LDAP_DEBUG_ACL ) {
1934                         print_acl( be, a );
1935                 }
1936 #endif
1937         
1938                 if ( a->acl_access == NULL ) {
1939                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1940                                 "warning: no by clause(s) specified in access line.\n",
1941                                 fname, lineno, 0 );
1942                         goto fail;
1943                 }
1944
1945                 if ( be != NULL ) {
1946                         if ( be->be_nsuffix == NULL ) {
1947                                 Debug( LDAP_DEBUG_ACL, "%s: line %d: warning: "
1948                                         "scope checking needs suffix before ACLs.\n",
1949                                         fname, lineno, 0 );
1950                                 /* go ahead, since checking is not authoritative */
1951                         } else if ( !BER_BVISNULL( &be->be_nsuffix[ 1 ] ) ) {
1952                                 Debug( LDAP_DEBUG_ACL, "%s: line %d: warning: "
1953                                         "scope checking only applies to single-valued "
1954                                         "suffix databases\n",
1955                                         fname, lineno, 0 );
1956                                 /* go ahead, since checking is not authoritative */
1957                         } else {
1958                                 switch ( check_scope( be, a ) ) {
1959                                 case ACL_SCOPE_UNKNOWN:
1960                                         Debug( LDAP_DEBUG_ACL, "%s: line %d: warning: "
1961                                                 "cannot assess the validity of the ACL scope within "
1962                                                 "backend naming context\n",
1963                                                 fname, lineno, 0 );
1964                                         break;
1965
1966                                 case ACL_SCOPE_WARN:
1967                                         Debug( LDAP_DEBUG_ACL, "%s: line %d: warning: "
1968                                                 "ACL could be out of scope within backend naming context\n",
1969                                                 fname, lineno, 0 );
1970                                         break;
1971
1972                                 case ACL_SCOPE_PARTIAL:
1973                                         Debug( LDAP_DEBUG_ACL, "%s: line %d: warning: "
1974                                                 "ACL appears to be partially out of scope within "
1975                                                 "backend naming context\n",
1976                                                 fname, lineno, 0 );
1977                                         break;
1978         
1979                                 case ACL_SCOPE_ERR:
1980                                         Debug( LDAP_DEBUG_ACL, "%s: line %d: warning: "
1981                                                 "ACL appears to be out of scope within "
1982                                                 "backend naming context\n",
1983                                                 fname, lineno, 0 );
1984                                         break;
1985
1986                                 default:
1987                                         break;
1988                                 }
1989                         }
1990                         acl_append( &be->be_acl, a, pos );
1991
1992                 } else {
1993                         acl_append( &frontendDB->be_acl, a, pos );
1994                 }
1995         }
1996
1997         return 0;
1998
1999 fail:
2000         if ( b ) access_free( b );
2001         if ( a ) acl_free( a );
2002         return acl_usage();
2003 }
2004
2005 char *
2006 accessmask2str( slap_mask_t mask, char *buf, int debug )
2007 {
2008         int     none = 1;
2009         char    *ptr = buf;
2010
2011         assert( buf != NULL );
2012
2013         if ( ACL_IS_INVALID( mask ) ) {
2014                 return "invalid";
2015         }
2016
2017         buf[0] = '\0';
2018
2019         if ( ACL_IS_LEVEL( mask ) ) {
2020                 if ( ACL_LVL_IS_NONE(mask) ) {
2021                         ptr = lutil_strcopy( ptr, "none" );
2022
2023                 } else if ( ACL_LVL_IS_DISCLOSE(mask) ) {
2024                         ptr = lutil_strcopy( ptr, "disclose" );
2025
2026                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
2027                         ptr = lutil_strcopy( ptr, "auth" );
2028
2029                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
2030                         ptr = lutil_strcopy( ptr, "compare" );
2031
2032                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
2033                         ptr = lutil_strcopy( ptr, "search" );
2034
2035                 } else if ( ACL_LVL_IS_READ(mask) ) {
2036                         ptr = lutil_strcopy( ptr, "read" );
2037
2038                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
2039                         ptr = lutil_strcopy( ptr, "write" );
2040
2041                 } else if ( ACL_LVL_IS_WADD(mask) ) {
2042                         ptr = lutil_strcopy( ptr, "add" );
2043
2044                 } else if ( ACL_LVL_IS_WDEL(mask) ) {
2045                         ptr = lutil_strcopy( ptr, "delete" );
2046
2047                 } else if ( ACL_LVL_IS_MANAGE(mask) ) {
2048                         ptr = lutil_strcopy( ptr, "manage" );
2049
2050                 } else {
2051                         ptr = lutil_strcopy( ptr, "unknown" );
2052                 }
2053                 
2054                 if ( !debug ) {
2055                         *ptr = '\0';
2056                         return buf;
2057                 }
2058                 *ptr++ = '(';
2059         }
2060
2061         if( ACL_IS_ADDITIVE( mask ) ) {
2062                 *ptr++ = '+';
2063
2064         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
2065                 *ptr++ = '-';
2066
2067         } else {
2068                 *ptr++ = '=';
2069         }
2070
2071         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_MANAGE) ) {
2072                 none = 0;
2073                 *ptr++ = 'm';
2074         } 
2075
2076         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
2077                 none = 0;
2078                 *ptr++ = 'w';
2079
2080         } else if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WADD) ) {
2081                 none = 0;
2082                 *ptr++ = 'a';
2083
2084         } else if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WDEL) ) {
2085                 none = 0;
2086                 *ptr++ = 'z';
2087         } 
2088
2089         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
2090                 none = 0;
2091                 *ptr++ = 'r';
2092         } 
2093
2094         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
2095                 none = 0;
2096                 *ptr++ = 's';
2097         } 
2098
2099         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
2100                 none = 0;
2101                 *ptr++ = 'c';
2102         } 
2103
2104         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
2105                 none = 0;
2106                 *ptr++ = 'x';
2107         } 
2108
2109         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_DISCLOSE) ) {
2110                 none = 0;
2111                 *ptr++ = 'd';
2112         } 
2113
2114         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
2115                 none = 0;
2116                 *ptr++ = '0';
2117         } 
2118
2119         if ( none ) {
2120                 ptr = buf;
2121         }
2122
2123         if ( ACL_IS_LEVEL( mask ) ) {
2124                 *ptr++ = ')';
2125         }
2126
2127         *ptr = '\0';
2128
2129         return buf;
2130 }
2131
2132 slap_mask_t
2133 str2accessmask( const char *str )
2134 {
2135         slap_mask_t     mask;
2136
2137         if( !ASCII_ALPHA(str[0]) ) {
2138                 int i;
2139
2140                 if ( str[0] == '=' ) {
2141                         ACL_INIT(mask);
2142
2143                 } else if( str[0] == '+' ) {
2144                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
2145
2146                 } else if( str[0] == '-' ) {
2147                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
2148
2149                 } else {
2150                         ACL_INVALIDATE(mask);
2151                         return mask;
2152                 }
2153
2154                 for( i=1; str[i] != '\0'; i++ ) {
2155                         if( TOLOWER((unsigned char) str[i]) == 'm' ) {
2156                                 ACL_PRIV_SET(mask, ACL_PRIV_MANAGE);
2157
2158                         } else if( TOLOWER((unsigned char) str[i]) == 'w' ) {
2159                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
2160
2161                         } else if( TOLOWER((unsigned char) str[i]) == 'a' ) {
2162                                 ACL_PRIV_SET(mask, ACL_PRIV_WADD);
2163
2164                         } else if( TOLOWER((unsigned char) str[i]) == 'z' ) {
2165                                 ACL_PRIV_SET(mask, ACL_PRIV_WDEL);
2166
2167                         } else if( TOLOWER((unsigned char) str[i]) == 'r' ) {
2168                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
2169
2170                         } else if( TOLOWER((unsigned char) str[i]) == 's' ) {
2171                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
2172
2173                         } else if( TOLOWER((unsigned char) str[i]) == 'c' ) {
2174                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
2175
2176                         } else if( TOLOWER((unsigned char) str[i]) == 'x' ) {
2177                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
2178
2179                         } else if( TOLOWER((unsigned char) str[i]) == 'd' ) {
2180                                 ACL_PRIV_SET(mask, ACL_PRIV_DISCLOSE);
2181
2182                         } else if( str[i] == '0' ) {
2183                                 ACL_PRIV_SET(mask, ACL_PRIV_NONE);
2184
2185                         } else {
2186                                 ACL_INVALIDATE(mask);
2187                                 return mask;
2188                         }
2189                 }
2190
2191                 return mask;
2192         }
2193
2194         if ( strcasecmp( str, "none" ) == 0 ) {
2195                 ACL_LVL_ASSIGN_NONE(mask);
2196
2197         } else if ( strcasecmp( str, "disclose" ) == 0 ) {
2198                 ACL_LVL_ASSIGN_DISCLOSE(mask);
2199
2200         } else if ( strcasecmp( str, "auth" ) == 0 ) {
2201                 ACL_LVL_ASSIGN_AUTH(mask);
2202
2203         } else if ( strcasecmp( str, "compare" ) == 0 ) {
2204                 ACL_LVL_ASSIGN_COMPARE(mask);
2205
2206         } else if ( strcasecmp( str, "search" ) == 0 ) {
2207                 ACL_LVL_ASSIGN_SEARCH(mask);
2208
2209         } else if ( strcasecmp( str, "read" ) == 0 ) {
2210                 ACL_LVL_ASSIGN_READ(mask);
2211
2212         } else if ( strcasecmp( str, "add" ) == 0 ) {
2213                 ACL_LVL_ASSIGN_WADD(mask);
2214
2215         } else if ( strcasecmp( str, "delete" ) == 0 ) {
2216                 ACL_LVL_ASSIGN_WDEL(mask);
2217
2218         } else if ( strcasecmp( str, "write" ) == 0 ) {
2219                 ACL_LVL_ASSIGN_WRITE(mask);
2220
2221         } else if ( strcasecmp( str, "manage" ) == 0 ) {
2222                 ACL_LVL_ASSIGN_MANAGE(mask);
2223
2224         } else {
2225                 ACL_INVALIDATE( mask );
2226         }
2227
2228         return mask;
2229 }
2230
2231 static int
2232 acl_usage( void )
2233 {
2234         char *access =
2235                 "<access clause> ::= access to <what> "
2236                                 "[ by <who> [ <access> ] [ <control> ] ]+ \n";
2237         char *what =
2238                 "<what> ::= * | dn[.<dnstyle>=<DN>] [filter=<filter>] [attrs=<attrspec>]\n"
2239                 "<attrspec> ::= <attrname> [val[/<matchingRule>][.<attrstyle>]=<value>] | <attrlist>\n"
2240                 "<attrlist> ::= <attr> [ , <attrlist> ]\n"
2241                 "<attr> ::= <attrname> | @<objectClass> | !<objectClass> | entry | children\n";
2242
2243         char *who =
2244                 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<DN> ]\n"
2245                         "\t[ realanonymous | realusers | realself | realdn[.<dnstyle>]=<DN> ]\n"
2246                         "\t[dnattr=<attrname>]\n"
2247                         "\t[realdnattr=<attrname>]\n"
2248                         "\t[group[/<objectclass>[/<attrname>]][.<style>]=<group>]\n"
2249                         "\t[peername[.<peernamestyle>]=<peer>] [sockname[.<style>]=<name>]\n"
2250                         "\t[domain[.<domainstyle>]=<domain>] [sockurl[.<style>]=<url>]\n"
2251 #ifdef SLAP_DYNACL
2252                         "\t[dynacl/<name>[/<options>][.<dynstyle>][=<pattern>]]\n"
2253 #endif /* SLAP_DYNACL */
2254                         "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
2255                 "<style> ::= exact | regex | base(Object)\n"
2256                 "<dnstyle> ::= base(Object) | one(level) | sub(tree) | children | "
2257                         "exact | regex\n"
2258                 "<attrstyle> ::= exact | regex | base(Object) | one(level) | "
2259                         "sub(tree) | children\n"
2260                 "<peernamestyle> ::= exact | regex | ip | ipv6 | path\n"
2261                 "<domainstyle> ::= exact | regex | base(Object) | sub(tree)\n"
2262                 "<access> ::= [[real]self]{<level>|<priv>}\n"
2263                 "<level> ::= none|disclose|auth|compare|search|read|{write|add|delete}|manage\n"
2264                 "<priv> ::= {=|+|-}{0|d|x|c|s|r|{w|a|z}|m}+\n"
2265                 "<control> ::= [ stop | continue | break ]\n"
2266 #ifdef SLAP_DYNACL
2267 #ifdef SLAPD_ACI_ENABLED
2268                 "dynacl:\n"
2269                 "\t<name>=ACI\t<pattern>=<attrname>\n"
2270 #endif /* SLAPD_ACI_ENABLED */
2271 #endif /* ! SLAP_DYNACL */
2272                 "";
2273
2274         Debug( LDAP_DEBUG_ANY, "%s%s%s\n", access, what, who );
2275
2276         return 1;
2277 }
2278
2279 /*
2280  * Set pattern to a "normalized" DN from src.
2281  * At present it simply eats the (optional) space after 
2282  * a RDN separator (,)
2283  * Eventually will evolve in a more complete normalization
2284  */
2285 static void
2286 acl_regex_normalized_dn(
2287         const char *src,
2288         struct berval *pattern )
2289 {
2290         char *str, *p;
2291         ber_len_t len;
2292
2293         str = ch_strdup( src );
2294         len = strlen( src );
2295
2296         for ( p = str; p && p[0]; p++ ) {
2297                 /* escape */
2298                 if ( p[0] == '\\' && p[1] ) {
2299                         /* 
2300                          * if escaping a hex pair we should
2301                          * increment p twice; however, in that 
2302                          * case the second hex number does 
2303                          * no harm
2304                          */
2305                         p++;
2306                 }
2307
2308                 if ( p[0] == ',' && p[1] == ' ' ) {
2309                         char *q;
2310                         
2311                         /*
2312                          * too much space should be an error if we are pedantic
2313                          */
2314                         for ( q = &p[2]; q[0] == ' '; q++ ) {
2315                                 /* DO NOTHING */ ;
2316                         }
2317                         AC_MEMCPY( p+1, q, len-(q-str)+1);
2318                 }
2319         }
2320         pattern->bv_val = str;
2321         pattern->bv_len = p - str;
2322
2323         return;
2324 }
2325
2326 static void
2327 split(
2328     char        *line,
2329     int         splitchar,
2330     char        **left,
2331     char        **right )
2332 {
2333         *left = line;
2334         if ( (*right = strchr( line, splitchar )) != NULL ) {
2335                 *((*right)++) = '\0';
2336         }
2337 }
2338
2339 static void
2340 access_append( Access **l, Access *a )
2341 {
2342         for ( ; *l != NULL; l = &(*l)->a_next ) {
2343                 ;       /* Empty */
2344         }
2345
2346         *l = a;
2347 }
2348
2349 void
2350 acl_append( AccessControl **l, AccessControl *a, int pos )
2351 {
2352         int i;
2353
2354         for (i=0 ; i != pos && *l != NULL; l = &(*l)->acl_next, i++ ) {
2355                 ;       /* Empty */
2356         }
2357         if ( *l && a )
2358                 a->acl_next = *l;
2359         *l = a;
2360 }
2361
2362 static void
2363 access_free( Access *a )
2364 {
2365         if ( !BER_BVISNULL( &a->a_dn_pat ) ) {
2366                 free( a->a_dn_pat.bv_val );
2367         }
2368         if ( !BER_BVISNULL( &a->a_realdn_pat ) ) {
2369                 free( a->a_realdn_pat.bv_val );
2370         }
2371         if ( !BER_BVISNULL( &a->a_peername_pat ) ) {
2372                 free( a->a_peername_pat.bv_val );
2373         }
2374         if ( !BER_BVISNULL( &a->a_sockname_pat ) ) {
2375                 free( a->a_sockname_pat.bv_val );
2376         }
2377         if ( !BER_BVISNULL( &a->a_domain_pat ) ) {
2378                 free( a->a_domain_pat.bv_val );
2379         }
2380         if ( !BER_BVISNULL( &a->a_sockurl_pat ) ) {
2381                 free( a->a_sockurl_pat.bv_val );
2382         }
2383         if ( !BER_BVISNULL( &a->a_set_pat ) ) {
2384                 free( a->a_set_pat.bv_val );
2385         }
2386         if ( !BER_BVISNULL( &a->a_group_pat ) ) {
2387                 free( a->a_group_pat.bv_val );
2388         }
2389 #ifdef SLAP_DYNACL
2390         if ( a->a_dynacl != NULL ) {
2391                 slap_dynacl_t   *da;
2392                 for ( da = a->a_dynacl; da; ) {
2393                         slap_dynacl_t   *tmp = da;
2394
2395                         da = da->da_next;
2396
2397                         if ( tmp->da_destroy ) {
2398                                 tmp->da_destroy( tmp->da_private );
2399                         }
2400
2401                         ch_free( tmp );
2402                 }
2403         }
2404 #endif /* SLAP_DYNACL */
2405         free( a );
2406 }
2407
2408 void
2409 acl_free( AccessControl *a )
2410 {
2411         Access *n;
2412         AttributeName *an;
2413
2414         if ( a->acl_filter ) {
2415                 filter_free( a->acl_filter );
2416         }
2417         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
2418                 if ( a->acl_dn_style == ACL_STYLE_REGEX ) {
2419                         regfree( &a->acl_dn_re );
2420                 }
2421                 free ( a->acl_dn_pat.bv_val );
2422         }
2423         if ( a->acl_attrs ) {
2424                 for ( an = a->acl_attrs; !BER_BVISNULL( &an->an_name ); an++ ) {
2425                         free( an->an_name.bv_val );
2426                 }
2427                 free( a->acl_attrs );
2428
2429                 if ( a->acl_attrval_style == ACL_STYLE_REGEX ) {
2430                         regfree( &a->acl_attrval_re );
2431                 }
2432
2433                 if ( !BER_BVISNULL( &a->acl_attrval ) ) {
2434                         ber_memfree( a->acl_attrval.bv_val );
2435                 }
2436         }
2437         for ( ; a->acl_access; a->acl_access = n ) {
2438                 n = a->acl_access->a_next;
2439                 access_free( a->acl_access );
2440         }
2441         free( a );
2442 }
2443
2444 void
2445 acl_destroy( AccessControl *a )
2446 {
2447         AccessControl *n;
2448
2449         for ( ; a; a = n ) {
2450                 n = a->acl_next;
2451                 acl_free( a );
2452         }
2453 }
2454
2455 char *
2456 access2str( slap_access_t access )
2457 {
2458         if ( access == ACL_NONE ) {
2459                 return "none";
2460
2461         } else if ( access == ACL_DISCLOSE ) {
2462                 return "disclose";
2463
2464         } else if ( access == ACL_AUTH ) {
2465                 return "auth";
2466
2467         } else if ( access == ACL_COMPARE ) {
2468                 return "compare";
2469
2470         } else if ( access == ACL_SEARCH ) {
2471                 return "search";
2472
2473         } else if ( access == ACL_READ ) {
2474                 return "read";
2475
2476         } else if ( access == ACL_WRITE ) {
2477                 return "write";
2478
2479         } else if ( access == ACL_WADD ) {
2480                 return "add";
2481
2482         } else if ( access == ACL_WDEL ) {
2483                 return "delete";
2484
2485         } else if ( access == ACL_MANAGE ) {
2486                 return "manage";
2487
2488         }
2489
2490         return "unknown";
2491 }
2492
2493 slap_access_t
2494 str2access( const char *str )
2495 {
2496         if ( strcasecmp( str, "none" ) == 0 ) {
2497                 return ACL_NONE;
2498
2499         } else if ( strcasecmp( str, "disclose" ) == 0 ) {
2500                 return ACL_DISCLOSE;
2501
2502         } else if ( strcasecmp( str, "auth" ) == 0 ) {
2503                 return ACL_AUTH;
2504
2505         } else if ( strcasecmp( str, "compare" ) == 0 ) {
2506                 return ACL_COMPARE;
2507
2508         } else if ( strcasecmp( str, "search" ) == 0 ) {
2509                 return ACL_SEARCH;
2510
2511         } else if ( strcasecmp( str, "read" ) == 0 ) {
2512                 return ACL_READ;
2513
2514         } else if ( strcasecmp( str, "write" ) == 0 ) {
2515                 return ACL_WRITE;
2516
2517         } else if ( strcasecmp( str, "add" ) == 0 ) {
2518                 return ACL_WADD;
2519
2520         } else if ( strcasecmp( str, "delete" ) == 0 ) {
2521                 return ACL_WDEL;
2522
2523         } else if ( strcasecmp( str, "manage" ) == 0 ) {
2524                 return ACL_MANAGE;
2525         }
2526
2527         return( ACL_INVALID_ACCESS );
2528 }
2529
2530 #define ACLBUF_MAXLEN   8192
2531
2532 static char aclbuf[ACLBUF_MAXLEN];
2533
2534 static char *
2535 dnaccess2text( slap_dn_access *bdn, char *ptr, int is_realdn )
2536 {
2537         *ptr++ = ' ';
2538
2539         if ( is_realdn ) {
2540                 ptr = lutil_strcopy( ptr, "real" );
2541         }
2542
2543         if ( ber_bvccmp( &bdn->a_pat, '*' ) ||
2544                 bdn->a_style == ACL_STYLE_ANONYMOUS ||
2545                 bdn->a_style == ACL_STYLE_USERS ||
2546                 bdn->a_style == ACL_STYLE_SELF )
2547         {
2548                 if ( is_realdn ) {
2549                         assert( ! ber_bvccmp( &bdn->a_pat, '*' ) );
2550                 }
2551                         
2552                 ptr = lutil_strcopy( ptr, bdn->a_pat.bv_val );
2553                 if ( bdn->a_style == ACL_STYLE_SELF && bdn->a_self_level != 0 ) {
2554                         int n = sprintf( ptr, ".level{%d}", bdn->a_self_level );
2555                         if ( n > 0 ) {
2556                                 ptr += n;
2557                         } /* else ? */
2558                 }
2559
2560         } else {
2561                 ptr = lutil_strcopy( ptr, "dn." );
2562                 if ( bdn->a_style == ACL_STYLE_BASE )
2563                         ptr = lutil_strcopy( ptr, style_base );
2564                 else 
2565                         ptr = lutil_strcopy( ptr, style_strings[bdn->a_style] );
2566                 if ( bdn->a_style == ACL_STYLE_LEVEL ) {
2567                         int n = sprintf( ptr, "{%d}", bdn->a_level );
2568                         if ( n > 0 ) {
2569                                 ptr += n;
2570                         } /* else ? */
2571                 }
2572                 if ( bdn->a_expand ) {
2573                         ptr = lutil_strcopy( ptr, ",expand" );
2574                 }
2575                 *ptr++ = '=';
2576                 *ptr++ = '"';
2577                 ptr = lutil_strcopy( ptr, bdn->a_pat.bv_val );
2578                 *ptr++ = '"';
2579         }
2580         return ptr;
2581 }
2582
2583 static char *
2584 access2text( Access *b, char *ptr )
2585 {
2586         char maskbuf[ACCESSMASK_MAXLEN];
2587
2588         ptr = lutil_strcopy( ptr, "\tby" );
2589
2590         if ( !BER_BVISEMPTY( &b->a_dn_pat ) ) {
2591                 ptr = dnaccess2text( &b->a_dn, ptr, 0 );
2592         }
2593         if ( b->a_dn_at ) {
2594                 ptr = lutil_strcopy( ptr, " dnattr=" );
2595                 ptr = lutil_strcopy( ptr, b->a_dn_at->ad_cname.bv_val );
2596         }
2597
2598         if ( !BER_BVISEMPTY( &b->a_realdn_pat ) ) {
2599                 ptr = dnaccess2text( &b->a_realdn, ptr, 1 );
2600         }
2601         if ( b->a_realdn_at ) {
2602                 ptr = lutil_strcopy( ptr, " realdnattr=" );
2603                 ptr = lutil_strcopy( ptr, b->a_realdn_at->ad_cname.bv_val );
2604         }
2605
2606         if ( !BER_BVISEMPTY( &b->a_group_pat ) ) {
2607                 ptr = lutil_strcopy( ptr, " group/" );
2608                 ptr = lutil_strcopy( ptr, b->a_group_oc ?
2609                         b->a_group_oc->soc_cname.bv_val : SLAPD_GROUP_CLASS );
2610                 *ptr++ = '/';
2611                 ptr = lutil_strcopy( ptr, b->a_group_at ?
2612                         b->a_group_at->ad_cname.bv_val : SLAPD_GROUP_ATTR );
2613                 *ptr++ = '.';
2614                 ptr = lutil_strcopy( ptr, style_strings[b->a_group_style] );
2615                 *ptr++ = '=';
2616                 *ptr++ = '"';
2617                 ptr = lutil_strcopy( ptr, b->a_group_pat.bv_val );
2618                 *ptr++ = '"';
2619         }
2620
2621         if ( !BER_BVISEMPTY( &b->a_peername_pat ) ) {
2622                 ptr = lutil_strcopy( ptr, " peername" );
2623                 *ptr++ = '.';
2624                 ptr = lutil_strcopy( ptr, style_strings[b->a_peername_style] );
2625                 *ptr++ = '=';
2626                 *ptr++ = '"';
2627                 ptr = lutil_strcopy( ptr, b->a_peername_pat.bv_val );
2628                 *ptr++ = '"';
2629         }
2630
2631         if ( !BER_BVISEMPTY( &b->a_sockname_pat ) ) {
2632                 ptr = lutil_strcopy( ptr, " sockname" );
2633                 *ptr++ = '.';
2634                 ptr = lutil_strcopy( ptr, style_strings[b->a_sockname_style] );
2635                 *ptr++ = '=';
2636                 *ptr++ = '"';
2637                 ptr = lutil_strcopy( ptr, b->a_sockname_pat.bv_val );
2638                 *ptr++ = '"';
2639         }
2640
2641         if ( !BER_BVISEMPTY( &b->a_domain_pat ) ) {
2642                 ptr = lutil_strcopy( ptr, " domain" );
2643                 *ptr++ = '.';
2644                 ptr = lutil_strcopy( ptr, style_strings[b->a_domain_style] );
2645                 if ( b->a_domain_expand ) {
2646                         ptr = lutil_strcopy( ptr, ",expand" );
2647                 }
2648                 *ptr++ = '=';
2649                 ptr = lutil_strcopy( ptr, b->a_domain_pat.bv_val );
2650         }
2651
2652         if ( !BER_BVISEMPTY( &b->a_sockurl_pat ) ) {
2653                 ptr = lutil_strcopy( ptr, " sockurl" );
2654                 *ptr++ = '.';
2655                 ptr = lutil_strcopy( ptr, style_strings[b->a_sockurl_style] );
2656                 *ptr++ = '=';
2657                 *ptr++ = '"';
2658                 ptr = lutil_strcopy( ptr, b->a_sockurl_pat.bv_val );
2659                 *ptr++ = '"';
2660         }
2661
2662         if ( !BER_BVISEMPTY( &b->a_set_pat ) ) {
2663                 ptr = lutil_strcopy( ptr, " set" );
2664                 *ptr++ = '.';
2665                 ptr = lutil_strcopy( ptr, style_strings[b->a_set_style] );
2666                 *ptr++ = '=';
2667                 *ptr++ = '"';
2668                 ptr = lutil_strcopy( ptr, b->a_set_pat.bv_val );
2669                 *ptr++ = '"';
2670         }
2671
2672 #ifdef SLAP_DYNACL
2673         if ( b->a_dynacl ) {
2674                 slap_dynacl_t   *da;
2675
2676                 for ( da = b->a_dynacl; da; da = da->da_next ) {
2677                         if ( da->da_unparse ) {
2678                                 struct berval bv = BER_BVNULL;
2679                                 (void)( *da->da_unparse )( da->da_private, &bv );
2680                                 assert( !BER_BVISNULL( &bv ) );
2681                                 ptr = lutil_strcopy( ptr, bv.bv_val );
2682                                 ch_free( bv.bv_val );
2683                         }
2684                 }
2685         }
2686 #endif /* SLAP_DYNACL */
2687
2688         /* Security Strength Factors */
2689         if ( b->a_authz.sai_ssf ) {
2690                 ptr += sprintf( ptr, " ssf=%u", 
2691                         b->a_authz.sai_ssf );
2692         }
2693         if ( b->a_authz.sai_transport_ssf ) {
2694                 ptr += sprintf( ptr, " transport_ssf=%u",
2695                         b->a_authz.sai_transport_ssf );
2696         }
2697         if ( b->a_authz.sai_tls_ssf ) {
2698                 ptr += sprintf( ptr, " tls_ssf=%u",
2699                         b->a_authz.sai_tls_ssf );
2700         }
2701         if ( b->a_authz.sai_sasl_ssf ) {
2702                 ptr += sprintf( ptr, " sasl_ssf=%u",
2703                         b->a_authz.sai_sasl_ssf );
2704         }
2705
2706         *ptr++ = ' ';
2707         if ( b->a_dn_self ) {
2708                 ptr = lutil_strcopy( ptr, "self" );
2709         } else if ( b->a_realdn_self ) {
2710                 ptr = lutil_strcopy( ptr, "realself" );
2711         }
2712         ptr = lutil_strcopy( ptr, accessmask2str( b->a_access_mask, maskbuf, 0 ));
2713         if ( !maskbuf[0] ) ptr--;
2714
2715         if( b->a_type == ACL_BREAK ) {
2716                 ptr = lutil_strcopy( ptr, " break" );
2717
2718         } else if( b->a_type == ACL_CONTINUE ) {
2719                 ptr = lutil_strcopy( ptr, " continue" );
2720
2721         } else if( b->a_type != ACL_STOP ) {
2722                 ptr = lutil_strcopy( ptr, " unknown-control" );
2723         } else {
2724                 if ( !maskbuf[0] ) ptr = lutil_strcopy( ptr, " stop" );
2725         }
2726         *ptr++ = '\n';
2727
2728         return ptr;
2729 }
2730
2731 void
2732 acl_unparse( AccessControl *a, struct berval *bv )
2733 {
2734         Access  *b;
2735         char    *ptr;
2736         int     to = 0;
2737
2738         bv->bv_val = aclbuf;
2739         bv->bv_len = 0;
2740
2741         ptr = bv->bv_val;
2742
2743         ptr = lutil_strcopy( ptr, "to" );
2744         if ( !BER_BVISNULL( &a->acl_dn_pat ) ) {
2745                 to++;
2746                 ptr = lutil_strcopy( ptr, " dn." );
2747                 if ( a->acl_dn_style == ACL_STYLE_BASE )
2748                         ptr = lutil_strcopy( ptr, style_base );
2749                 else
2750                         ptr = lutil_strcopy( ptr, style_strings[a->acl_dn_style] );
2751                 *ptr++ = '=';
2752                 *ptr++ = '"';
2753                 ptr = lutil_strcopy( ptr, a->acl_dn_pat.bv_val );
2754                 ptr = lutil_strcopy( ptr, "\"\n" );
2755         }
2756
2757         if ( a->acl_filter != NULL ) {
2758                 struct berval   bv = BER_BVNULL;
2759
2760                 to++;
2761                 filter2bv( a->acl_filter, &bv );
2762                 ptr = lutil_strcopy( ptr, " filter=\"" );
2763                 ptr = lutil_strcopy( ptr, bv.bv_val );
2764                 *ptr++ = '"';
2765                 *ptr++ = '\n';
2766                 ch_free( bv.bv_val );
2767         }
2768
2769         if ( a->acl_attrs != NULL ) {
2770                 int     first = 1;
2771                 AttributeName *an;
2772                 to++;
2773
2774                 ptr = lutil_strcopy( ptr, " attrs=" );
2775                 for ( an = a->acl_attrs; an && !BER_BVISNULL( &an->an_name ); an++ ) {
2776                         if ( ! first ) *ptr++ = ',';
2777                         if (an->an_oc) {
2778                                 *ptr++ = ( an->an_flags & SLAP_AN_OCEXCLUDE ) ? '!' : '@';
2779                                 ptr = lutil_strcopy( ptr, an->an_oc->soc_cname.bv_val );
2780
2781                         } else {
2782                                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
2783                         }
2784                         first = 0;
2785                 }
2786                 *ptr++ = '\n';
2787         }
2788
2789         if ( !BER_BVISEMPTY( &a->acl_attrval ) ) {
2790                 to++;
2791                 ptr = lutil_strcopy( ptr, " val." );
2792                 if ( a->acl_attrval_style == ACL_STYLE_BASE &&
2793                         a->acl_attrs[0].an_desc->ad_type->sat_syntax ==
2794                                 slap_schema.si_syn_distinguishedName )
2795                         ptr = lutil_strcopy( ptr, style_base );
2796                 else
2797                         ptr = lutil_strcopy( ptr, style_strings[a->acl_attrval_style] );
2798                 *ptr++ = '=';
2799                 *ptr++ = '"';
2800                 ptr = lutil_strcopy( ptr, a->acl_attrval.bv_val );
2801                 *ptr++ = '"';
2802                 *ptr++ = '\n';
2803         }
2804
2805         if( !to ) {
2806                 ptr = lutil_strcopy( ptr, " *\n" );
2807         }
2808
2809         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
2810                 ptr = access2text( b, ptr );
2811         }
2812         *ptr = '\0';
2813         bv->bv_len = ptr - bv->bv_val;
2814 }
2815
2816 #ifdef LDAP_DEBUG
2817 static void
2818 print_acl( Backend *be, AccessControl *a )
2819 {
2820         struct berval bv;
2821
2822         acl_unparse( a, &bv );
2823         fprintf( stderr, "%s ACL: access %s\n",
2824                 be == NULL ? "Global" : "Backend", bv.bv_val );
2825 }
2826 #endif /* LDAP_DEBUG */