]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
Updated notices
[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-2003 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 void             split(char *line, int splitchar, char **left, char **right);
42 static void             access_append(Access **l, Access *a);
43 static void             acl_usage(void) LDAP_GCCATTR((noreturn));
44
45 static void             acl_regex_normalized_dn(const char *src, struct berval *pat);
46
47 #ifdef LDAP_DEBUG
48 static void             print_acl(Backend *be, AccessControl *a);
49 static void             print_access(Access *b);
50 #endif
51
52 static void
53 regtest(const char *fname, int lineno, char *pat) {
54         int e;
55         regex_t re;
56
57         char buf[512];
58         unsigned size;
59
60         char *sp;
61         char *dp;
62         int  flag;
63
64         sp = pat;
65         dp = buf;
66         size = 0;
67         buf[0] = '\0';
68
69         for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
70                 if (flag) {
71                         if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
72                                 *dp++ = *sp;
73                                 size++;
74                         }
75                         flag = 0;
76
77                 } else {
78                         if (*sp == '$') {
79                                 flag = 1;
80                         } else {
81                                 *dp++ = *sp;
82                                 size++;
83                         }
84                 }
85         }
86
87         *dp = '\0';
88         if ( size >= (sizeof(buf)-1) ) {
89                 fprintf( stderr,
90                         "%s: line %d: regular expression \"%s\" too large\n",
91                         fname, lineno, pat );
92                 acl_usage();
93         }
94
95         if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
96                 char error[512];
97                 regerror(e, &re, error, sizeof(error));
98                 fprintf( stderr,
99                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
100                         fname, lineno, pat, error );
101                 acl_usage();
102         }
103         regfree(&re);
104 }
105
106 void
107 parse_acl(
108     Backend     *be,
109     const char  *fname,
110     int         lineno,
111     int         argc,
112     char        **argv
113 )
114 {
115         int             i;
116         char            *left, *right, *style;
117         struct berval   bv;
118         AccessControl   *a;
119         Access  *b;
120         int rc;
121         const char *text;
122
123         a = NULL;
124         for ( i = 1; i < argc; i++ ) {
125                 /* to clause - select which entries are protected */
126                 if ( strcasecmp( argv[i], "to" ) == 0 ) {
127                         if ( a != NULL ) {
128                                 fprintf( stderr, "%s: line %d: "
129                                         "only one to clause allowed in access line\n",
130                                     fname, lineno );
131                                 acl_usage();
132                         }
133                         a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
134                         for ( ++i; i < argc; i++ ) {
135                                 if ( strcasecmp( argv[i], "by" ) == 0 ) {
136                                         i--;
137                                         break;
138                                 }
139
140                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
141                                         if( a->acl_dn_pat.bv_len ||
142                                                 ( a->acl_dn_style != ACL_STYLE_REGEX ) )
143                                         {
144                                                 fprintf( stderr,
145                                                         "%s: line %d: dn pattern"
146                                                         " already specified in to clause.\n",
147                                                         fname, lineno );
148                                                 acl_usage();
149                                         }
150
151                                         a->acl_dn_pat.bv_val = ch_strdup( "*" );
152                                         a->acl_dn_pat.bv_len = 1;
153                                         continue;
154                                 }
155
156                                 split( argv[i], '=', &left, &right );
157                                 split( left, '.', &left, &style );
158
159                                 if ( right == NULL ) {
160                                         fprintf( stderr, "%s: line %d: "
161                                                 "missing \"=\" in \"%s\" in to clause\n",
162                                             fname, lineno, left );
163                                         acl_usage();
164                                 }
165
166                                 if ( strcasecmp( left, "dn" ) == 0 ) {
167                                         if( a->acl_dn_pat.bv_len != 0 ||
168                                                 ( a->acl_dn_style != ACL_STYLE_REGEX ) )
169                                         {
170                                                 fprintf( stderr,
171                                                         "%s: line %d: dn pattern"
172                                                         " already specified in to clause.\n",
173                                                         fname, lineno );
174                                                 acl_usage();
175                                         }
176
177                                         if ( style == NULL || *style == '\0' ||
178                                                 ( strcasecmp( style, "base" ) == 0 ) ||
179                                                 ( strcasecmp( style, "exact" ) == 0 ))
180                                         {
181                                                 a->acl_dn_style = ACL_STYLE_BASE;
182                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
183
184                                         } else if ( strcasecmp( style, "one" ) == 0 ) {
185                                                 a->acl_dn_style = ACL_STYLE_ONE;
186                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
187
188                                         } else if ( strcasecmp( style, "subtree" ) == 0
189                                                 || strcasecmp( style, "sub" ) == 0 )
190                                         {
191                                                 if( *right == '\0' ) {
192                                                         a->acl_dn_pat.bv_val = ch_strdup( "*" );
193                                                         a->acl_dn_pat.bv_len = 1;
194
195                                                 } else {
196                                                         a->acl_dn_style = ACL_STYLE_SUBTREE;
197                                                         ber_str2bv( right, 0, 1, &a->acl_dn_pat );
198                                                 }
199
200                                         } else if ( strcasecmp( style, "children" ) == 0 ) {
201                                                 a->acl_dn_style = ACL_STYLE_CHILDREN;
202                                                 ber_str2bv( right, 0, 1, &a->acl_dn_pat );
203
204                                         } else if ( strcasecmp( style, "regex" ) == 0 ) {
205                                                 a->acl_dn_style = ACL_STYLE_REGEX;
206
207                                                 if ( *right == '\0' ) {
208                                                         /* empty regex should match empty DN */
209                                                         a->acl_dn_style = ACL_STYLE_BASE;
210                                                         ber_str2bv( right, 0, 1, &a->acl_dn_pat );
211
212                                                 } else if ( strcmp(right, "*") == 0 
213                                                         || strcmp(right, ".*") == 0 
214                                                         || strcmp(right, ".*$") == 0 
215                                                         || strcmp(right, "^.*") == 0 
216                                                         || strcmp(right, "^.*$") == 0
217                                                         || strcmp(right, ".*$$") == 0 
218                                                         || strcmp(right, "^.*$$") == 0 )
219                                                 {
220                                                         a->acl_dn_pat.bv_val = ch_strdup( "*" );
221                                                         a->acl_dn_pat.bv_len = sizeof("*")-1;
222
223                                                 } else {
224                                                         acl_regex_normalized_dn( right, &a->acl_dn_pat );
225                                                 }
226
227                                         } else {
228                                                 fprintf( stderr, "%s: line %d: "
229                                                         "unknown dn style \"%s\" in to clause\n",
230                                                     fname, lineno, style );
231                                                 acl_usage();
232                                         }
233
234                                         continue;
235                                 }
236
237                                 if ( strcasecmp( left, "filter" ) == 0 ) {
238                                         if ( (a->acl_filter = str2filter( right )) == NULL ) {
239                                                 fprintf( stderr,
240                                 "%s: line %d: bad filter \"%s\" in to clause\n",
241                                                     fname, lineno, right );
242                                                 acl_usage();
243                                         }
244
245                                 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
246                                         a->acl_attrs = str2anlist( a->acl_attrs,
247                                                 right, "," );
248                                         if ( a->acl_attrs == NULL ) {
249                                                 fprintf( stderr,
250                                 "%s: line %d: unknown attr \"%s\" in to clause\n",
251                                                     fname, lineno, right );
252                                                 acl_usage();
253                                         }
254
255                                 } else if ( strncasecmp( left, "val", 3 ) == 0 ) {
256                                         if ( a->acl_attrval.bv_len ) {
257                                                 fprintf( stderr,
258                                 "%s: line %d: attr val already specified in to clause.\n",
259                                                         fname, lineno );
260                                                 acl_usage();
261                                         }
262                                         if ( a->acl_attrs == NULL || a->acl_attrs[1].an_name.bv_val ) {
263                                                 fprintf( stderr,
264                                 "%s: line %d: attr val requires a single attribute.\n",
265                                                         fname, lineno );
266                                                 acl_usage();
267                                         }
268                                         ber_str2bv( right, 0, 1, &a->acl_attrval );
269                                         if ( style && strcasecmp( style, "regex" ) == 0 ) {
270                                                 int e = regcomp( &a->acl_attrval_re, a->acl_attrval.bv_val,
271                                                         REG_EXTENDED | REG_ICASE | REG_NOSUB );
272                                                 if ( e ) {
273                                                         char buf[512];
274                                                         regerror( e, &a->acl_attrval_re, buf, sizeof(buf) );
275                                                         fprintf( stderr, "%s: line %d: "
276                                                                 "regular expression \"%s\" bad because of %s\n",
277                                                                 fname, lineno, right, buf );
278                                                         acl_usage();
279                                                 }
280                                                 a->acl_attrval_style = ACL_STYLE_REGEX;
281                                         } else {
282                                                 a->acl_attrval_style = ACL_STYLE_BASE;
283                                         }
284                                         
285                                 } else {
286                                         fprintf( stderr,
287                                                 "%s: line %d: expecting <what> got \"%s\"\n",
288                                             fname, lineno, left );
289                                         acl_usage();
290                                 }
291                         }
292
293                         if ( a->acl_dn_pat.bv_len != 0 &&
294                                 strcmp(a->acl_dn_pat.bv_val, "*") == 0 )
295                         {
296                                 free( a->acl_dn_pat.bv_val );
297                                 a->acl_dn_pat.bv_val = NULL;
298                                 a->acl_dn_pat.bv_len = 0;
299                         }
300                         
301                         if( a->acl_dn_pat.bv_len != 0 ||
302                                 ( a->acl_dn_style != ACL_STYLE_REGEX ) )
303                         {
304                                 if ( a->acl_dn_style != ACL_STYLE_REGEX ) {
305                                         struct berval bv;
306                                         rc = dnNormalize( 0, NULL, NULL, &a->acl_dn_pat, &bv, NULL);
307                                         if ( rc != LDAP_SUCCESS ) {
308                                                 fprintf( stderr,
309                                                         "%s: line %d: bad DN \"%s\" in to DN clause\n",
310                                                         fname, lineno, a->acl_dn_pat.bv_val );
311                                                 acl_usage();
312                                         }
313                                         free( a->acl_dn_pat.bv_val );
314                                         a->acl_dn_pat = bv;
315                                 } else {
316                                         int e = regcomp( &a->acl_dn_re, a->acl_dn_pat.bv_val,
317                                                 REG_EXTENDED | REG_ICASE );
318                                         if ( e ) {
319                                                 char buf[512];
320                                                 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
321                                                 fprintf( stderr, "%s: line %d: "
322                                                         "regular expression \"%s\" bad because of %s\n",
323                                                         fname, lineno, right, buf );
324                                                 acl_usage();
325                                         }
326                                 }
327                         }
328
329                 /* by clause - select who has what access to entries */
330                 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
331                         if ( a == NULL ) {
332                                 fprintf( stderr, "%s: line %d: "
333                                         "to clause required before by clause in access line\n",
334                                     fname, lineno );
335                                 acl_usage();
336                         }
337
338                         /*
339                          * by clause consists of <who> and <access>
340                          */
341
342                         b = (Access *) ch_calloc( 1, sizeof(Access) );
343
344                         ACL_INVALIDATE( b->a_access_mask );
345
346                         if ( ++i == argc ) {
347                                 fprintf( stderr,
348                             "%s: line %d: premature eol: expecting <who>\n",
349                                     fname, lineno );
350                                 acl_usage();
351                         }
352
353                         /* get <who> */
354                         for ( ; i < argc; i++ ) {
355                                 slap_style_t sty = ACL_STYLE_REGEX;
356                                 char *style_modifier = NULL;
357                                 int expand = 0;
358
359                                 split( argv[i], '=', &left, &right );
360                                 split( left, '.', &left, &style );
361                                 if ( style ) {
362                                         split( style, ',', &style, &style_modifier);
363                                 }
364
365                                 if ( style == NULL || *style == '\0' ||
366                                         strcasecmp( style, "exact" ) == 0 ||
367                                         strcasecmp( style, "base" ) == 0 )
368                                 {
369                                         sty = ACL_STYLE_BASE;
370
371                                 } else if ( strcasecmp( style, "one" ) == 0 ) {
372                                         sty = ACL_STYLE_ONE;
373
374                                 } else if ( strcasecmp( style, "subtree" ) == 0 ||
375                                         strcasecmp( style, "sub" ) == 0 )
376                                 {
377                                         sty = ACL_STYLE_SUBTREE;
378
379                                 } else if ( strcasecmp( style, "children" ) == 0 ) {
380                                         sty = ACL_STYLE_CHILDREN;
381
382                                 } else if ( strcasecmp( style, "regex" ) == 0 ) {
383                                         sty = ACL_STYLE_REGEX;
384
385                                 } else {
386                                         fprintf( stderr,
387                                                 "%s: line %d: unknown style \"%s\" in by clause\n",
388                                             fname, lineno, style );
389                                         acl_usage();
390                                 }
391
392                                 if ( style_modifier &&
393                                         strcasecmp( style_modifier, "expand" ) == 0 )
394                                 {
395                                         expand = 1;
396                                 }
397
398                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
399                                         bv.bv_val = ch_strdup( "*" );
400                                         bv.bv_len = 1;
401                                         sty = ACL_STYLE_REGEX;
402
403                                 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
404                                         ber_str2bv("anonymous", sizeof("anonymous")-1, 1, &bv);
405                                         sty = ACL_STYLE_REGEX;
406
407                                 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
408                                         ber_str2bv("self", sizeof("self")-1, 1, &bv);
409                                         sty = ACL_STYLE_REGEX;
410
411                                 } else if ( strcasecmp( argv[i], "users" ) == 0 ) {
412                                         ber_str2bv("users", sizeof("users")-1, 1, &bv);
413                                         sty = ACL_STYLE_REGEX;
414
415                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
416                                         if ( sty == ACL_STYLE_REGEX ) {
417                                                 b->a_dn_style = ACL_STYLE_REGEX;
418                                                 if( right == NULL ) {
419                                                         /* no '=' */
420                                                         ber_str2bv("users",
421                                                                 sizeof("users")-1,
422                                                                 1, &bv);
423                                                 } else if (*right == '\0' ) {
424                                                         /* dn="" */
425                                                         ber_str2bv("anonymous",
426                                                                 sizeof("anonymous")-1,
427                                                                 1, &bv);
428                                                 } else if ( strcmp( right, "*" ) == 0 ) {
429                                                         /* dn=* */
430                                                         /* any or users?  users for now */
431                                                         ber_str2bv("users",
432                                                                 sizeof("users")-1,
433                                                                 1, &bv);
434                                                 } else if ( strcmp( right, ".+" ) == 0
435                                                         || strcmp( right, "^.+" ) == 0
436                                                         || strcmp( right, ".+$" ) == 0
437                                                         || strcmp( right, "^.+$" ) == 0
438                                                         || strcmp( right, ".+$$" ) == 0
439                                                         || strcmp( right, "^.+$$" ) == 0 )
440                                                 {
441                                                         ber_str2bv("users",
442                                                                 sizeof("users")-1,
443                                                                 1, &bv);
444                                                 } else if ( strcmp( right, ".*" ) == 0
445                                                         || strcmp( right, "^.*" ) == 0
446                                                         || strcmp( right, ".*$" ) == 0
447                                                         || strcmp( right, "^.*$" ) == 0
448                                                         || strcmp( right, ".*$$" ) == 0
449                                                         || strcmp( right, "^.*$$" ) == 0 )
450                                                 {
451                                                         ber_str2bv("*",
452                                                                 sizeof("*")-1,
453                                                                 1, &bv);
454
455                                                 } else {
456                                                         acl_regex_normalized_dn( right, &bv );
457                                                         if ( !ber_bvccmp( &bv, '*' ) ) {
458                                                                 regtest(fname, lineno, bv.bv_val);
459                                                         }
460                                                 }
461                                         } else if ( right == NULL || *right == '\0' ) {
462                                                 fprintf( stderr, "%s: line %d: "
463                                                         "missing \"=\" in (or value after) \"%s\" "
464                                                         "in by clause\n",
465                                                     fname, lineno, left );
466                                                 acl_usage();
467
468                                         } else {
469                                                 ber_str2bv( right, 0, 1, &bv );
470                                         }
471
472                                 } else {
473                                         bv.bv_val = NULL;
474                                 }
475
476                                 if( bv.bv_val != NULL ) {
477                                         if( b->a_dn_pat.bv_len != 0 ) {
478                                                 fprintf( stderr,
479                                                     "%s: line %d: dn pattern already specified.\n",
480                                                     fname, lineno );
481                                                 acl_usage();
482                                         }
483
484                                         if ( sty != ACL_STYLE_REGEX && expand == 0 ) {
485                                                 rc = dnNormalize(0, NULL, NULL,
486                                                         &bv, &b->a_dn_pat, NULL);
487                                                 if ( rc != LDAP_SUCCESS ) {
488                                                         fprintf( stderr,
489                                                                 "%s: line %d: bad DN \"%s\" in by DN clause\n",
490                                                                 fname, lineno, bv.bv_val );
491                                                         acl_usage();
492                                                 }
493                                                 free(bv.bv_val);
494                                         } else {
495                                                 b->a_dn_pat = bv;
496                                         }
497                                         b->a_dn_style = sty;
498                                         b->a_dn_expand = expand;
499                                         continue;
500                                 }
501
502                                 if ( strcasecmp( left, "dnattr" ) == 0 ) {
503                                         if ( right == NULL || right[ 0 ] == '\0' ) {
504                                                 fprintf( stderr,
505                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
506                                                         fname, lineno, left );
507                                                 acl_usage();
508                                         }
509
510                                         if( b->a_dn_at != NULL ) {
511                                                 fprintf( stderr,
512                                                         "%s: line %d: dnattr already specified.\n",
513                                                         fname, lineno );
514                                                 acl_usage();
515                                         }
516
517                                         rc = slap_str2ad( right, &b->a_dn_at, &text );
518
519                                         if( rc != LDAP_SUCCESS ) {
520                                                 fprintf( stderr,
521                                                         "%s: line %d: dnattr \"%s\": %s\n",
522                                                         fname, lineno, right, text );
523                                                 acl_usage();
524                                         }
525
526
527                                         if( !is_at_syntax( b->a_dn_at->ad_type,
528                                                 SLAPD_DN_SYNTAX ) &&
529                                                 !is_at_syntax( b->a_dn_at->ad_type,
530                                                 SLAPD_NAMEUID_SYNTAX ))
531                                         {
532                                                 fprintf( stderr,
533                                                         "%s: line %d: dnattr \"%s\": "
534                                                         "inappropriate syntax: %s\n",
535                                                         fname, lineno, right,
536                                                         b->a_dn_at->ad_type->sat_syntax_oid );
537                                                 acl_usage();
538                                         }
539
540                                         if( b->a_dn_at->ad_type->sat_equality == NULL ) {
541                                                 fprintf( stderr,
542                                                         "%s: line %d: dnattr \"%s\": "
543                                                         "inappropriate matching (no EQUALITY)\n",
544                                                         fname, lineno, right );
545                                                 acl_usage();
546                                         }
547
548                                         continue;
549                                 }
550
551                                 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
552                                         char *name = NULL;
553                                         char *value = NULL;
554
555                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
556                                                 fprintf( stderr, "%s: line %d: "
557                                                         "inappropriate style \"%s\" in by clause\n",
558                                                     fname, lineno, style );
559                                                 acl_usage();
560                                         }
561
562                                         if ( right == NULL || right[ 0 ] == '\0' ) {
563                                                 fprintf( stderr, "%s: line %d: "
564                                                         "missing \"=\" in (or value after) \"%s\" "
565                                                         "in by clause\n",
566                                                         fname, lineno, left );
567                                                 acl_usage();
568                                         }
569
570                                         if( b->a_group_pat.bv_len ) {
571                                                 fprintf( stderr,
572                                                         "%s: line %d: group pattern already specified.\n",
573                                                         fname, lineno );
574                                                 acl_usage();
575                                         }
576
577                                         /* format of string is
578                                                 "group/objectClassValue/groupAttrName" */
579                                         if ((value = strchr(left, '/')) != NULL) {
580                                                 *value++ = '\0';
581                                                 if (*value && (name = strchr(value, '/')) != NULL) {
582                                                         *name++ = '\0';
583                                                 }
584                                         }
585
586                                         b->a_group_style = sty;
587                                         if (sty == ACL_STYLE_REGEX) {
588                                                 acl_regex_normalized_dn( right, &bv );
589                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
590                                                         regtest(fname, lineno, bv.bv_val);
591                                                 }
592                                                 b->a_group_pat = bv;
593                                         } else {
594                                                 ber_str2bv( right, 0, 0, &bv );
595                                                 rc = dnNormalize( 0, NULL, NULL, &bv,
596                                                         &b->a_group_pat, NULL );
597                                                 if ( rc != LDAP_SUCCESS ) {
598                                                         fprintf( stderr,
599                                                                 "%s: line %d: bad DN \"%s\"\n",
600                                                                 fname, lineno, right );
601                                                         acl_usage();
602                                                 }
603                                         }
604
605                                         if (value && *value) {
606                                                 b->a_group_oc = oc_find( value );
607                                                 *--value = '/';
608
609                                                 if( b->a_group_oc == NULL ) {
610                                                         fprintf( stderr,
611                                                                 "%s: line %d: group objectclass "
612                                                                 "\"%s\" unknown\n",
613                                                                 fname, lineno, value );
614                                                         acl_usage();
615                                                 }
616                                         } else {
617                                                 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
618
619                                                 if( b->a_group_oc == NULL ) {
620                                                         fprintf( stderr,
621                                                                 "%s: line %d: group default objectclass "
622                                                                 "\"%s\" unknown\n",
623                                                                 fname, lineno, SLAPD_GROUP_CLASS );
624                                                         acl_usage();
625                                                 }
626                                         }
627
628                                         if( is_object_subclass( slap_schema.si_oc_referral,
629                                                 b->a_group_oc ))
630                                         {
631                                                 fprintf( stderr,
632                                                         "%s: line %d: group objectclass \"%s\" "
633                                                         "is subclass of referral\n",
634                                                         fname, lineno, value );
635                                                 acl_usage();
636                                         }
637
638                                         if( is_object_subclass( slap_schema.si_oc_alias,
639                                                 b->a_group_oc ))
640                                         {
641                                                 fprintf( stderr,
642                                                         "%s: line %d: group objectclass \"%s\" "
643                                                         "is subclass of alias\n",
644                                                         fname, lineno, value );
645                                                 acl_usage();
646                                         }
647
648                                         if (name && *name) {
649                                                 rc = slap_str2ad( name, &b->a_group_at, &text );
650
651                                                 if( rc != LDAP_SUCCESS ) {
652                                                         fprintf( stderr,
653                                                                 "%s: line %d: group \"%s\": %s\n",
654                                                                 fname, lineno, right, text );
655                                                         acl_usage();
656                                                 }
657                                                 *--name = '/';
658                                         } else {
659                                                 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
660
661                                                 if( rc != LDAP_SUCCESS ) {
662                                                         fprintf( stderr,
663                                                                 "%s: line %d: group \"%s\": %s\n",
664                                                                 fname, lineno, SLAPD_GROUP_ATTR, text );
665                                                         acl_usage();
666                                                 }
667                                         }
668
669                                         if( !is_at_syntax( b->a_group_at->ad_type,
670                                                 SLAPD_DN_SYNTAX ) &&
671                                             !is_at_syntax( b->a_group_at->ad_type,
672                                                 SLAPD_NAMEUID_SYNTAX ) &&
673                                                 !is_at_subtype( b->a_group_at->ad_type, slap_schema.si_ad_labeledURI->ad_type ))
674                                         {
675                                                 fprintf( stderr,
676                                                         "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
677                                                         fname, lineno, right,
678                                                         b->a_group_at->ad_type->sat_syntax_oid );
679                                                 acl_usage();
680                                         }
681
682
683                                         {
684                                                 int rc;
685                                                 struct berval vals[2];
686
687                                                 vals[0].bv_val = b->a_group_oc->soc_oid;
688                                                 vals[0].bv_len = strlen(vals[0].bv_val);
689                                                 vals[1].bv_val = NULL;
690
691
692                                                 rc = oc_check_allowed( b->a_group_at->ad_type,
693                                                         vals, NULL );
694
695                                                 if( rc != 0 ) {
696                                                         fprintf( stderr, "%s: line %d: "
697                                                                 "group: \"%s\" not allowed by \"%s\"\n",
698                                                                 fname, lineno,
699                                                                 b->a_group_at->ad_cname.bv_val,
700                                                                 b->a_group_oc->soc_oid );
701                                                         acl_usage();
702                                                 }
703                                         }
704                                         continue;
705                                 }
706
707                                 if ( strcasecmp( left, "peername" ) == 0 ) {
708                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
709                                                 fprintf( stderr, "%s: line %d: "
710                                                         "inappropriate style \"%s\" in by clause\n",
711                                                     fname, lineno, style );
712                                                 acl_usage();
713                                         }
714
715                                         if ( right == NULL || right[ 0 ] == '\0' ) {
716                                                 fprintf( stderr, "%s: line %d: "
717                                                         "missing \"=\" in (or value after) \"%s\" "
718                                                         "in by clause\n",
719                                                         fname, lineno, left );
720                                                 acl_usage();
721                                         }
722
723                                         if( b->a_peername_pat.bv_len ) {
724                                                 fprintf( stderr, "%s: line %d: "
725                                                         "peername pattern already specified.\n",
726                                                         fname, lineno );
727                                                 acl_usage();
728                                         }
729
730                                         b->a_peername_style = sty;
731                                         if (sty == ACL_STYLE_REGEX) {
732                                                 acl_regex_normalized_dn( right, &bv );
733                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
734                                                         regtest(fname, lineno, bv.bv_val);
735                                                 }
736                                                 b->a_peername_pat = bv;
737                                         } else {
738                                                 ber_str2bv( right, 0, 1, &b->a_peername_pat );
739                                         }
740                                         continue;
741                                 }
742
743                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
744                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
745                                                 fprintf( stderr, "%s: line %d: "
746                                                         "inappropriate style \"%s\" in by clause\n",
747                                                     fname, lineno, style );
748                                                 acl_usage();
749                                         }
750
751                                         if ( right == NULL || right[ 0 ] == '\0' ) {
752                                                 fprintf( stderr, "%s: line %d: "
753                                                         "missing \"=\" in (or value after) \"%s\" "
754                                                         "in by clause\n",
755                                                         fname, lineno, left );
756                                                 acl_usage();
757                                         }
758
759                                         if( b->a_sockname_pat.bv_len ) {
760                                                 fprintf( stderr, "%s: line %d: "
761                                                         "sockname pattern already specified.\n",
762                                                         fname, lineno );
763                                                 acl_usage();
764                                         }
765
766                                         b->a_sockname_style = sty;
767                                         if (sty == ACL_STYLE_REGEX) {
768                                                 acl_regex_normalized_dn( right, &bv );
769                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
770                                                         regtest(fname, lineno, bv.bv_val);
771                                                 }
772                                                 b->a_sockname_pat = bv;
773                                         } else {
774                                                 ber_str2bv( right, 0, 1, &b->a_sockname_pat );
775                                         }
776                                         continue;
777                                 }
778
779                                 if ( strcasecmp( left, "domain" ) == 0 ) {
780                                         switch ( sty ) {
781                                         case ACL_STYLE_REGEX:
782                                         case ACL_STYLE_BASE:
783                                         case ACL_STYLE_SUBTREE:
784                                                 break;
785
786                                         default:
787                                                 fprintf( stderr,
788                                                         "%s: line %d: inappropriate style \"%s\" in by clause\n",
789                                                     fname, lineno, style );
790                                                 acl_usage();
791                                         }
792
793                                         if ( right == NULL || right[ 0 ] == '\0' ) {
794                                                 fprintf( stderr,
795                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
796                                                         fname, lineno, left );
797                                                 acl_usage();
798                                         }
799
800                                         if( b->a_domain_pat.bv_len ) {
801                                                 fprintf( stderr,
802                                                         "%s: line %d: domain pattern already specified.\n",
803                                                         fname, lineno );
804                                                 acl_usage();
805                                         }
806
807                                         b->a_domain_style = sty;
808                                         b->a_domain_expand = expand;
809                                         if (sty == ACL_STYLE_REGEX) {
810                                                 acl_regex_normalized_dn( right, &bv );
811                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
812                                                         regtest(fname, lineno, bv.bv_val);
813                                                 }
814                                                 b->a_domain_pat = bv;
815                                         } else {
816                                                 ber_str2bv( right, 0, 1, &b->a_domain_pat );
817                                         }
818                                         continue;
819                                 }
820
821                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
822                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
823                                                 fprintf( stderr,
824                                                         "%s: line %d: inappropriate style \"%s\" in by clause\n",
825                                                     fname, lineno, style );
826                                                 acl_usage();
827                                         }
828
829                                         if ( right == NULL || right[ 0 ] == '\0' ) {
830                                                 fprintf( stderr,
831                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
832                                                         fname, lineno, left );
833                                                 acl_usage();
834                                         }
835
836                                         if( b->a_sockurl_pat.bv_len ) {
837                                                 fprintf( stderr,
838                                                         "%s: line %d: sockurl pattern already specified.\n",
839                                                         fname, lineno );
840                                                 acl_usage();
841                                         }
842
843                                         b->a_sockurl_style = sty;
844                                         if (sty == ACL_STYLE_REGEX) {
845                                                 acl_regex_normalized_dn( right, &bv );
846                                                 if ( !ber_bvccmp( &bv, '*' ) ) {
847                                                         regtest(fname, lineno, bv.bv_val);
848                                                 }
849                                                 b->a_sockurl_pat = bv;
850                                         } else {
851                                                 ber_str2bv( right, 0, 1, &b->a_sockurl_pat );
852                                         }
853                                         continue;
854                                 }
855
856                                 if ( strcasecmp( left, "set" ) == 0 ) {
857                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
858                                                 fprintf( stderr,
859                                                         "%s: line %d: inappropriate style \"%s\" in by clause\n",
860                                                     fname, lineno, style );
861                                                 acl_usage();
862                                         }
863
864                                         if( b->a_set_pat.bv_len != 0 ) {
865                                                 fprintf( stderr,
866                                                         "%s: line %d: set attribute already specified.\n",
867                                                         fname, lineno );
868                                                 acl_usage();
869                                         }
870
871                                         if ( right == NULL || *right == '\0' ) {
872                                                 fprintf( stderr,
873                                                         "%s: line %d: no set is defined\n",
874                                                         fname, lineno );
875                                                 acl_usage();
876                                         }
877
878                                         b->a_set_style = sty;
879                                         ber_str2bv( right, 0, 1, &b->a_set_pat );
880
881                                         continue;
882                                 }
883
884 #ifdef SLAPD_ACI_ENABLED
885                                 if ( strcasecmp( left, "aci" ) == 0 ) {
886                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
887                                                 fprintf( stderr,
888                                                         "%s: line %d: inappropriate style \"%s\" in by clause\n",
889                                                     fname, lineno, style );
890                                                 acl_usage();
891                                         }
892
893                                         if( b->a_aci_at != NULL ) {
894                                                 fprintf( stderr,
895                                                         "%s: line %d: aci attribute already specified.\n",
896                                                         fname, lineno );
897                                                 acl_usage();
898                                         }
899
900                                         if ( right != NULL && *right != '\0' ) {
901                                                 rc = slap_str2ad( right, &b->a_aci_at, &text );
902
903                                                 if( rc != LDAP_SUCCESS ) {
904                                                         fprintf( stderr,
905                                                                 "%s: line %d: aci \"%s\": %s\n",
906                                                                 fname, lineno, right, text );
907                                                         acl_usage();
908                                                 }
909
910                                         } else {
911                                                 b->a_aci_at = slap_schema.si_ad_aci;
912                                         }
913
914                                         if( !is_at_syntax( b->a_aci_at->ad_type,
915                                                 SLAPD_ACI_SYNTAX) )
916                                         {
917                                                 fprintf( stderr,
918                                                         "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
919                                                         fname, lineno, right,
920                                                         b->a_aci_at->ad_type->sat_syntax_oid );
921                                                 acl_usage();
922                                         }
923
924                                         continue;
925                                 }
926 #endif /* SLAPD_ACI_ENABLED */
927
928                                 if ( strcasecmp( left, "ssf" ) == 0 ) {
929                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
930                                                 fprintf( stderr,
931                                                         "%s: line %d: inappropriate style \"%s\" in by clause\n",
932                                                     fname, lineno, style );
933                                                 acl_usage();
934                                         }
935
936                                         if( b->a_authz.sai_ssf ) {
937                                                 fprintf( stderr,
938                                                         "%s: line %d: ssf attribute already specified.\n",
939                                                         fname, lineno );
940                                                 acl_usage();
941                                         }
942
943                                         if ( right == NULL || *right == '\0' ) {
944                                                 fprintf( stderr,
945                                                         "%s: line %d: no ssf is defined\n",
946                                                         fname, lineno );
947                                                 acl_usage();
948                                         }
949
950                                         b->a_authz.sai_ssf = atoi( right );
951
952                                         if( !b->a_authz.sai_ssf ) {
953                                                 fprintf( stderr,
954                                                         "%s: line %d: invalid ssf value (%s)\n",
955                                                         fname, lineno, right );
956                                                 acl_usage();
957                                         }
958                                         continue;
959                                 }
960
961                                 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
962                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
963                                                 fprintf( stderr,
964                                                         "%s: line %d: inappropriate style \"%s\" in by clause\n",
965                                                     fname, lineno, style );
966                                                 acl_usage();
967                                         }
968
969                                         if( b->a_authz.sai_transport_ssf ) {
970                                                 fprintf( stderr,
971                                                         "%s: line %d: transport_ssf attribute already specified.\n",
972                                                         fname, lineno );
973                                                 acl_usage();
974                                         }
975
976                                         if ( right == NULL || *right == '\0' ) {
977                                                 fprintf( stderr,
978                                                         "%s: line %d: no transport_ssf is defined\n",
979                                                         fname, lineno );
980                                                 acl_usage();
981                                         }
982
983                                         b->a_authz.sai_transport_ssf = atoi( right );
984
985                                         if( !b->a_authz.sai_transport_ssf ) {
986                                                 fprintf( stderr,
987                                                         "%s: line %d: invalid transport_ssf value (%s)\n",
988                                                         fname, lineno, right );
989                                                 acl_usage();
990                                         }
991                                         continue;
992                                 }
993
994                                 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
995                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
996                                                 fprintf( stderr,
997                                                         "%s: line %d: inappropriate style \"%s\" in by clause\n",
998                                                     fname, lineno, style );
999                                                 acl_usage();
1000                                         }
1001
1002                                         if( b->a_authz.sai_tls_ssf ) {
1003                                                 fprintf( stderr,
1004                                                         "%s: line %d: tls_ssf attribute already specified.\n",
1005                                                         fname, lineno );
1006                                                 acl_usage();
1007                                         }
1008
1009                                         if ( right == NULL || *right == '\0' ) {
1010                                                 fprintf( stderr,
1011                                                         "%s: line %d: no tls_ssf is defined\n",
1012                                                         fname, lineno );
1013                                                 acl_usage();
1014                                         }
1015
1016                                         b->a_authz.sai_tls_ssf = atoi( right );
1017
1018                                         if( !b->a_authz.sai_tls_ssf ) {
1019                                                 fprintf( stderr,
1020                                                         "%s: line %d: invalid tls_ssf value (%s)\n",
1021                                                         fname, lineno, right );
1022                                                 acl_usage();
1023                                         }
1024                                         continue;
1025                                 }
1026
1027                                 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
1028                                         if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
1029                                                 fprintf( stderr,
1030                                                         "%s: line %d: inappropriate style \"%s\" in by clause\n",
1031                                                     fname, lineno, style );
1032                                                 acl_usage();
1033                                         }
1034
1035                                         if( b->a_authz.sai_sasl_ssf ) {
1036                                                 fprintf( stderr,
1037                                                         "%s: line %d: sasl_ssf attribute already specified.\n",
1038                                                         fname, lineno );
1039                                                 acl_usage();
1040                                         }
1041
1042                                         if ( right == NULL || *right == '\0' ) {
1043                                                 fprintf( stderr,
1044                                                         "%s: line %d: no sasl_ssf is defined\n",
1045                                                         fname, lineno );
1046                                                 acl_usage();
1047                                         }
1048
1049                                         b->a_authz.sai_sasl_ssf = atoi( right );
1050
1051                                         if( !b->a_authz.sai_sasl_ssf ) {
1052                                                 fprintf( stderr,
1053                                                         "%s: line %d: invalid sasl_ssf value (%s)\n",
1054                                                         fname, lineno, right );
1055                                                 acl_usage();
1056                                         }
1057                                         continue;
1058                                 }
1059
1060                                 if( right != NULL ) {
1061                                         /* unsplit */
1062                                         right[-1] = '=';
1063                                 }
1064                                 break;
1065                         }
1066
1067                         if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) { 
1068                                 /* out of arguments or plain stop */
1069
1070                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1071                                 b->a_type = ACL_STOP;
1072
1073                                 access_append( &a->acl_access, b );
1074                                 continue;
1075                         }
1076
1077                         if( strcasecmp( left, "continue" ) == 0 ) {
1078                                 /* plain continue */
1079
1080                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1081                                 b->a_type = ACL_CONTINUE;
1082
1083                                 access_append( &a->acl_access, b );
1084                                 continue;
1085                         }
1086
1087                         if( strcasecmp( left, "break" ) == 0 ) {
1088                                 /* plain continue */
1089
1090                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1091                                 b->a_type = ACL_BREAK;
1092
1093                                 access_append( &a->acl_access, b );
1094                                 continue;
1095                         }
1096
1097                         if ( strcasecmp( left, "by" ) == 0 ) {
1098                                 /* we've gone too far */
1099                                 --i;
1100                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
1101                                 b->a_type = ACL_STOP;
1102
1103                                 access_append( &a->acl_access, b );
1104                                 continue;
1105                         }
1106
1107                         /* get <access> */
1108                         if( strncasecmp( left, "self", 4 ) == 0 ) {
1109                                 b->a_dn_self = 1;
1110                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[4] ) );
1111
1112                         } else {
1113                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
1114                         }
1115
1116                         if( ACL_IS_INVALID( b->a_access_mask ) ) {
1117                                 fprintf( stderr,
1118                                         "%s: line %d: expecting <access> got \"%s\"\n",
1119                                         fname, lineno, left );
1120                                 acl_usage();
1121                         }
1122
1123                         b->a_type = ACL_STOP;
1124
1125                         if( ++i == argc ) {
1126                                 /* out of arguments or plain stop */
1127                                 access_append( &a->acl_access, b );
1128                                 continue;
1129                         }
1130
1131                         if( strcasecmp( argv[i], "continue" ) == 0 ) {
1132                                 /* plain continue */
1133                                 b->a_type = ACL_CONTINUE;
1134
1135                         } else if( strcasecmp( argv[i], "break" ) == 0 ) {
1136                                 /* plain continue */
1137                                 b->a_type = ACL_BREAK;
1138
1139                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
1140                                 /* gone to far */
1141                                 i--;
1142                         }
1143
1144                         access_append( &a->acl_access, b );
1145
1146                 } else {
1147                         fprintf( stderr,
1148                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
1149                             fname, lineno, argv[i] );
1150                         acl_usage();
1151                 }
1152         }
1153
1154         /* if we have no real access clause, complain and do nothing */
1155         if ( a == NULL ) {
1156                         fprintf( stderr,
1157                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
1158                             fname, lineno );
1159
1160         } else {
1161 #ifdef LDAP_DEBUG
1162                 if (ldap_debug & LDAP_DEBUG_ACL)
1163                         print_acl(be, a);
1164 #endif
1165         
1166                 if ( a->acl_access == NULL ) {
1167                         fprintf( stderr,
1168                         "%s: line %d: warning: no by clause(s) specified in access line\n",
1169                             fname, lineno );
1170                 }
1171
1172                 if ( be != NULL ) {
1173                         acl_append( &be->be_acl, a );
1174                 } else {
1175                         acl_append( &global_acl, a );
1176                 }
1177         }
1178 }
1179
1180 char *
1181 accessmask2str( slap_mask_t mask, char *buf )
1182 {
1183         int none=1;
1184         char *ptr = buf;
1185
1186         assert( buf != NULL );
1187
1188         if ( ACL_IS_INVALID( mask ) ) {
1189                 return "invalid";
1190         }
1191
1192         buf[0] = '\0';
1193
1194         if ( ACL_IS_LEVEL( mask ) ) {
1195                 if ( ACL_LVL_IS_NONE(mask) ) {
1196                         ptr = lutil_strcopy( ptr, "none" );
1197
1198                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
1199                         ptr = lutil_strcopy( ptr, "auth" );
1200
1201                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
1202                         ptr = lutil_strcopy( ptr, "compare" );
1203
1204                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
1205                         ptr = lutil_strcopy( ptr, "search" );
1206
1207                 } else if ( ACL_LVL_IS_READ(mask) ) {
1208                         ptr = lutil_strcopy( ptr, "read" );
1209
1210                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
1211                         ptr = lutil_strcopy( ptr, "write" );
1212                 } else {
1213                         ptr = lutil_strcopy( ptr, "unknown" );
1214                 }
1215                 
1216                 *ptr++ = '(';
1217         }
1218
1219         if( ACL_IS_ADDITIVE( mask ) ) {
1220                 *ptr++ = '+';
1221
1222         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
1223                 *ptr++ = '-';
1224
1225         } else {
1226                 *ptr++ = '=';
1227         }
1228
1229         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
1230                 none = 0;
1231                 *ptr++ = 'w';
1232         } 
1233
1234         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
1235                 none = 0;
1236                 *ptr++ = 'r';
1237         } 
1238
1239         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
1240                 none = 0;
1241                 *ptr++ = 's';
1242         } 
1243
1244         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
1245                 none = 0;
1246                 *ptr++ = 'c';
1247         } 
1248
1249         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
1250                 none = 0;
1251                 *ptr++ = 'x';
1252         } 
1253
1254         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
1255                 none = 0;
1256                 *ptr++ = 'n';
1257         } 
1258
1259         if ( none ) {
1260                 *ptr++ = '0';
1261         }
1262
1263         if ( ACL_IS_LEVEL( mask ) ) {
1264                 *ptr++ = ')';
1265         }
1266
1267         *ptr = '\0';
1268
1269         return buf;
1270 }
1271
1272 slap_mask_t
1273 str2accessmask( const char *str )
1274 {
1275         slap_mask_t     mask;
1276
1277         if( !ASCII_ALPHA(str[0]) ) {
1278                 int i;
1279
1280                 if ( str[0] == '=' ) {
1281                         ACL_INIT(mask);
1282
1283                 } else if( str[0] == '+' ) {
1284                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1285
1286                 } else if( str[0] == '-' ) {
1287                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1288
1289                 } else {
1290                         ACL_INVALIDATE(mask);
1291                         return mask;
1292                 }
1293
1294                 for( i=1; str[i] != '\0'; i++ ) {
1295                         if( TOLOWER((unsigned char) str[i]) == 'w' ) {
1296                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1297
1298                         } else if( TOLOWER((unsigned char) str[i]) == 'r' ) {
1299                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1300
1301                         } else if( TOLOWER((unsigned char) str[i]) == 's' ) {
1302                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1303
1304                         } else if( TOLOWER((unsigned char) str[i]) == 'c' ) {
1305                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1306
1307                         } else if( TOLOWER((unsigned char) str[i]) == 'x' ) {
1308                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1309
1310                         } else if( str[i] != '0' ) {
1311                                 ACL_INVALIDATE(mask);
1312                                 return mask;
1313                         }
1314                 }
1315
1316                 return mask;
1317         }
1318
1319         if ( strcasecmp( str, "none" ) == 0 ) {
1320                 ACL_LVL_ASSIGN_NONE(mask);
1321
1322         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1323                 ACL_LVL_ASSIGN_AUTH(mask);
1324
1325         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1326                 ACL_LVL_ASSIGN_COMPARE(mask);
1327
1328         } else if ( strcasecmp( str, "search" ) == 0 ) {
1329                 ACL_LVL_ASSIGN_SEARCH(mask);
1330
1331         } else if ( strcasecmp( str, "read" ) == 0 ) {
1332                 ACL_LVL_ASSIGN_READ(mask);
1333
1334         } else if ( strcasecmp( str, "write" ) == 0 ) {
1335                 ACL_LVL_ASSIGN_WRITE(mask);
1336
1337         } else {
1338                 ACL_INVALIDATE( mask );
1339         }
1340
1341         return mask;
1342 }
1343
1344 static void
1345 acl_usage( void )
1346 {
1347         fprintf( stderr, "%s%s\n",
1348                 "<access clause> ::= access to <what> "
1349                                 "[ by <who> <access> [ <control> ] ]+ \n"
1350                 "<what> ::= * | [dn[.<dnstyle>]=<DN>] [filter=<filter>] [attrs=<attrlist>]\n"
1351                 "<attrlist> ::= <attr> [val[.<style>]=<value>] | <attr> , <attrlist>\n"
1352                 "<attr> ::= <attrname> | entry | children\n"
1353                 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<DN> ]\n"
1354                         "\t[dnattr=<attrname>]\n"
1355                         "\t[group[/<objectclass>[/<attrname>]][.<style>]=<group>]\n"
1356                         "\t[peername[.<style>]=<peer>] [sockname[.<style>]=<name>]\n",
1357                         "\t[domain[.<style>]=<domain>] [sockurl[.<style>]=<url>]\n"
1358 #ifdef SLAPD_ACI_ENABLED
1359                         "\t[aci=<attrname>]\n"
1360 #endif
1361                         "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
1362                 "<dnstyle> ::= base | exact | one | subtree | children | regex\n"
1363                 "<style> ::= regex | base | exact\n"
1364                 "<access> ::= [self]{<level>|<priv>}\n"
1365                 "<level> ::= none | auth | compare | search | read | write\n"
1366                 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
1367                 "<control> ::= [ stop | continue | break ]\n"
1368         );
1369         exit( EXIT_FAILURE );
1370 }
1371
1372 /*
1373  * Set pattern to a "normalized" DN from src.
1374  * At present it simply eats the (optional) space after 
1375  * a RDN separator (,)
1376  * Eventually will evolve in a more complete normalization
1377  */
1378 static void
1379 acl_regex_normalized_dn(
1380         const char *src,
1381         struct berval *pattern
1382 )
1383 {
1384         char *str, *p;
1385         ber_len_t len;
1386
1387         str = ch_strdup( src );
1388         len = strlen( src );
1389
1390         for ( p = str; p && p[ 0 ]; p++ ) {
1391                 /* escape */
1392                 if ( p[ 0 ] == '\\' && p[ 1 ] ) {
1393                         /* 
1394                          * if escaping a hex pair we should
1395                          * increment p twice; however, in that 
1396                          * case the second hex number does 
1397                          * no harm
1398                          */
1399                         p++;
1400                 }
1401
1402                 if ( p[ 0 ] == ',' ) {
1403                         if ( p[ 1 ] == ' ' ) {
1404                                 char *q;
1405                         
1406                                 /*
1407                                  * too much space should be 
1408                                  * an error if we are pedantic
1409                                  */
1410                                 for ( q = &p[ 2 ]; q[ 0 ] == ' '; q++ ) {
1411                                         /* DO NOTHING */ ;
1412                                 }
1413                                 AC_MEMCPY( p+1, q, len-(q-str)+1);
1414                         }
1415                 }
1416         }
1417         pattern->bv_val = str;
1418         pattern->bv_len = p-str;
1419
1420         return;
1421 }
1422
1423 static void
1424 split(
1425     char        *line,
1426     int         splitchar,
1427     char        **left,
1428     char        **right
1429 )
1430 {
1431         *left = line;
1432         if ( (*right = strchr( line, splitchar )) != NULL ) {
1433                 *((*right)++) = '\0';
1434         }
1435 }
1436
1437 static void
1438 access_append( Access **l, Access *a )
1439 {
1440         for ( ; *l != NULL; l = &(*l)->a_next )
1441                 ;       /* NULL */
1442
1443         *l = a;
1444 }
1445
1446 void
1447 acl_append( AccessControl **l, AccessControl *a )
1448 {
1449         for ( ; *l != NULL; l = &(*l)->acl_next )
1450                 ;       /* NULL */
1451
1452         *l = a;
1453 }
1454
1455 static void
1456 access_free( Access *a )
1457 {
1458         if ( a->a_dn_pat.bv_val )
1459                 free ( a->a_dn_pat.bv_val );
1460         if ( a->a_peername_pat.bv_val )
1461                 free ( a->a_peername_pat.bv_val );
1462         if ( a->a_sockname_pat.bv_val )
1463                 free ( a->a_sockname_pat.bv_val );
1464         if ( a->a_domain_pat.bv_val )
1465                 free ( a->a_domain_pat.bv_val );
1466         if ( a->a_sockurl_pat.bv_val )
1467                 free ( a->a_sockurl_pat.bv_val );
1468         if ( a->a_set_pat.bv_len )
1469                 free ( a->a_set_pat.bv_val );
1470         if ( a->a_group_pat.bv_len )
1471                 free ( a->a_group_pat.bv_val );
1472         free( a );
1473 }
1474
1475 void
1476 acl_free( AccessControl *a )
1477 {
1478         Access *n;
1479         AttributeName *an;
1480
1481         if ( a->acl_filter )
1482                 filter_free( a->acl_filter );
1483         if ( a->acl_dn_pat.bv_len )
1484                 free ( a->acl_dn_pat.bv_val );
1485         if ( a->acl_attrs ) {
1486                 for ( an = a->acl_attrs; an->an_name.bv_val; an++ ) {
1487                         free( an->an_name.bv_val );
1488                 }
1489                 free( a->acl_attrs );
1490         }
1491         for (; a->acl_access; a->acl_access = n) {
1492                 n = a->acl_access->a_next;
1493                 access_free( a->acl_access );
1494         }
1495         free( a );
1496 }
1497
1498 /* Because backend_startup uses acl_append to tack on the global_acl to
1499  * the end of each backend's acl, we cannot just take one argument and
1500  * merrily free our way to the end of the list. backend_destroy calls us
1501  * with the be_acl in arg1, and global_acl in arg2 to give us a stopping
1502  * point. config_destroy calls us with global_acl in arg1 and NULL in
1503  * arg2, so we then proceed to polish off the global_acl.
1504  */
1505 void
1506 acl_destroy( AccessControl *a, AccessControl *end )
1507 {
1508         AccessControl *n;
1509
1510         for (; a && a!= end; a=n) {
1511                 n = a->acl_next;
1512                 acl_free( a );
1513         }
1514 }
1515
1516 char *
1517 access2str( slap_access_t access )
1518 {
1519         if ( access == ACL_NONE ) {
1520                 return "none";
1521
1522         } else if ( access == ACL_AUTH ) {
1523                 return "auth";
1524
1525         } else if ( access == ACL_COMPARE ) {
1526                 return "compare";
1527
1528         } else if ( access == ACL_SEARCH ) {
1529                 return "search";
1530
1531         } else if ( access == ACL_READ ) {
1532                 return "read";
1533
1534         } else if ( access == ACL_WRITE ) {
1535                 return "write";
1536         }
1537
1538         return "unknown";
1539 }
1540
1541 slap_access_t
1542 str2access( const char *str )
1543 {
1544         if ( strcasecmp( str, "none" ) == 0 ) {
1545                 return ACL_NONE;
1546
1547         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1548                 return ACL_AUTH;
1549
1550         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1551                 return ACL_COMPARE;
1552
1553         } else if ( strcasecmp( str, "search" ) == 0 ) {
1554                 return ACL_SEARCH;
1555
1556         } else if ( strcasecmp( str, "read" ) == 0 ) {
1557                 return ACL_READ;
1558
1559         } else if ( strcasecmp( str, "write" ) == 0 ) {
1560                 return ACL_WRITE;
1561         }
1562
1563         return( ACL_INVALID_ACCESS );
1564 }
1565
1566 #ifdef LDAP_DEBUG
1567
1568 static char *style_strings[5] = { "regex",
1569         "base", "one", "subtree", "children" };
1570
1571 static void
1572 print_access( Access *b )
1573 {
1574         char maskbuf[ACCESSMASK_MAXLEN];
1575
1576         fprintf( stderr, "\tby" );
1577
1578         if ( b->a_dn_pat.bv_len != 0 ) {
1579                 if( strcmp(b->a_dn_pat.bv_val, "*") == 0
1580                         || strcmp(b->a_dn_pat.bv_val, "users") == 0 
1581                         || strcmp(b->a_dn_pat.bv_val, "anonymous") == 0 
1582                         || strcmp(b->a_dn_pat.bv_val, "self") == 0 )
1583                 {
1584                         fprintf( stderr, " %s", b->a_dn_pat.bv_val );
1585
1586                 } else {
1587                         fprintf( stderr, " dn.%s=\"%s\"",
1588                                 style_strings[b->a_dn_style], b->a_dn_pat.bv_val );
1589                 }
1590         }
1591
1592         if ( b->a_dn_at != NULL ) {
1593                 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname.bv_val );
1594         }
1595
1596         if ( b->a_group_pat.bv_len ) {
1597                 fprintf( stderr, " group/%s/%s.%s=\"%s\"",
1598                         b->a_group_oc ? b->a_group_oc->soc_cname.bv_val : "groupOfNames",
1599                         b->a_group_at ? b->a_group_at->ad_cname.bv_val : "member",
1600                         style_strings[b->a_group_style],
1601                         b->a_group_pat.bv_val );
1602     }
1603
1604         if ( b->a_peername_pat.bv_len != 0 ) {
1605                 fprintf( stderr, " peername=\"%s\"", b->a_peername_pat.bv_val );
1606         }
1607
1608         if ( b->a_sockname_pat.bv_len != 0 ) {
1609                 fprintf( stderr, " sockname=\"%s\"", b->a_sockname_pat.bv_val );
1610         }
1611
1612         if ( b->a_domain_pat.bv_len != 0 ) {
1613                 fprintf( stderr, " domain=%s", b->a_domain_pat.bv_val );
1614         }
1615
1616         if ( b->a_sockurl_pat.bv_len != 0 ) {
1617                 fprintf( stderr, " sockurl=\"%s\"", b->a_sockurl_pat.bv_val );
1618         }
1619
1620 #ifdef SLAPD_ACI_ENABLED
1621         if ( b->a_aci_at != NULL ) {
1622                 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname.bv_val );
1623         }
1624 #endif
1625
1626         /* Security Strength Factors */
1627         if ( b->a_authz.sai_ssf ) {
1628                 fprintf( stderr, " ssf=%u",
1629                         b->a_authz.sai_ssf );
1630         }
1631         if ( b->a_authz.sai_transport_ssf ) {
1632                 fprintf( stderr, " transport_ssf=%u",
1633                         b->a_authz.sai_transport_ssf );
1634         }
1635         if ( b->a_authz.sai_tls_ssf ) {
1636                 fprintf( stderr, " tls_ssf=%u",
1637                         b->a_authz.sai_tls_ssf );
1638         }
1639         if ( b->a_authz.sai_sasl_ssf ) {
1640                 fprintf( stderr, " sasl_ssf=%u",
1641                         b->a_authz.sai_sasl_ssf );
1642         }
1643
1644         fprintf( stderr, " %s%s",
1645                 b->a_dn_self ? "self" : "",
1646                 accessmask2str( b->a_access_mask, maskbuf ) );
1647
1648         if( b->a_type == ACL_BREAK ) {
1649                 fprintf( stderr, " break" );
1650
1651         } else if( b->a_type == ACL_CONTINUE ) {
1652                 fprintf( stderr, " continue" );
1653
1654         } else if( b->a_type != ACL_STOP ) {
1655                 fprintf( stderr, " unknown-control" );
1656         }
1657
1658         fprintf( stderr, "\n" );
1659 }
1660
1661
1662 static void
1663 print_acl( Backend *be, AccessControl *a )
1664 {
1665         int             to = 0;
1666         Access  *b;
1667
1668         fprintf( stderr, "%s ACL: access to",
1669                 be == NULL ? "Global" : "Backend" );
1670
1671         if ( a->acl_dn_pat.bv_len != 0 ) {
1672                 to++;
1673                 fprintf( stderr, " dn.%s=\"%s\"\n",
1674                         style_strings[a->acl_dn_style], a->acl_dn_pat.bv_val );
1675         }
1676
1677         if ( a->acl_filter != NULL ) {
1678                 struct berval bv = { 0, NULL };
1679                 to++;
1680                 filter2bv( a->acl_filter, &bv );
1681                 fprintf( stderr, " filter=%s\n", bv.bv_val );
1682                 ch_free( bv.bv_val );
1683         }
1684
1685         if ( a->acl_attrs != NULL ) {
1686                 int     first = 1;
1687                 AttributeName *an;
1688                 to++;
1689
1690                 fprintf( stderr, " attrs=" );
1691                 for ( an = a->acl_attrs; an && an->an_name.bv_val; an++ ) {
1692                         if ( ! first ) {
1693                                 fprintf( stderr, "," );
1694                         }
1695                         fputs( an->an_name.bv_val, stderr );
1696                         first = 0;
1697                 }
1698                 fprintf(  stderr, "\n" );
1699         }
1700
1701         if ( a->acl_attrval.bv_len != 0 ) {
1702                 to++;
1703                 fprintf( stderr, " val.%s=\"%s\"\n",
1704                         style_strings[a->acl_attrval_style], a->acl_attrval.bv_val );
1705
1706         }
1707
1708         if( !to ) {
1709                 fprintf( stderr, " *\n" );
1710         }
1711
1712         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1713                 print_access( b );
1714         }
1715
1716         fprintf( stderr, "\n" );
1717 }
1718
1719 #endif /* LDAP_DEBUG */