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