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