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