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