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