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