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