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