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