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