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