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