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