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