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