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