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