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