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