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