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