]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
Add referral generator
[openldap] / servers / slapd / aclparse.c
1 /* aclparse.c - routines to parse and check acl's */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/regex.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
17
18 #include "slap.h"
19
20 static void             split(char *line, int splitchar, char **left, char **right);
21 static void             access_append(Access **l, Access *a);
22 static void             acl_usage(void) LDAP_GCCATTR((noreturn));
23
24 #ifdef LDAP_DEBUG
25 static void             print_acl(Backend *be, AccessControl *a);
26 static void             print_access(Access *b);
27 #endif
28
29 static int
30 regtest(const char *fname, int lineno, char *pat) {
31         int e;
32         regex_t re;
33
34         char buf[512];
35         unsigned size;
36
37         char *sp;
38         char *dp;
39         int  flag;
40
41         sp = pat;
42         dp = buf;
43         size = 0;
44         buf[0] = '\0';
45
46         for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
47                 if (flag) {
48                         if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
49                                 *dp++ = *sp;
50                                 size++;
51                         }
52                         flag = 0;
53
54                 } else {
55                         if (*sp == '$') {
56                                 flag = 1;
57                         } else {
58                                 *dp++ = *sp;
59                                 size++;
60                         }
61                 }
62         }
63
64         *dp = '\0';
65         if ( size >= (sizeof(buf)-1) ) {
66                 fprintf( stderr,
67                         "%s: line %d: regular expression \"%s\" too large\n",
68                         fname, lineno, pat );
69                 acl_usage();
70         }
71
72         if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
73                 char error[512];
74                 regerror(e, &re, error, sizeof(error));
75                 fprintf( stderr,
76                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
77                         fname, lineno, pat, error );
78                 acl_usage();
79                 return(0);
80         }
81         regfree(&re);
82         return(1);
83 }
84
85 void
86 parse_acl(
87     Backend     *be,
88     const char  *fname,
89     int         lineno,
90     int         argc,
91     char        **argv
92 )
93 {
94         int             i;
95         char            *left, *right, *style;
96         AccessControl   *a;
97         Access  *b;
98         int rc;
99         const char *text;
100
101         a = NULL;
102         for ( i = 1; i < argc; i++ ) {
103                 /* to clause - select which entries are protected */
104                 if ( strcasecmp( argv[i], "to" ) == 0 ) {
105                         if ( a != NULL ) {
106                                 fprintf( stderr,
107                 "%s: line %d: only one to clause allowed in access line\n",
108                                     fname, lineno );
109                                 acl_usage();
110                         }
111                         a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
112                         a->acl_filter = NULL;
113                         a->acl_dn_pat = NULL;
114                         a->acl_attrs  = NULL;
115                         a->acl_access = NULL;
116                         a->acl_next   = NULL;
117                         for ( ++i; i < argc; i++ ) {
118                                 if ( strcasecmp( argv[i], "by" ) == 0 ) {
119                                         i--;
120                                         break;
121                                 }
122
123                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
124                                         if( a->acl_dn_pat != NULL ) {
125                                                 fprintf( stderr,
126                                                         "%s: line %d: dn pattern"
127                                                         " already specified in to clause.\n",
128                                                         fname, lineno );
129                                                 acl_usage();
130                                         }
131
132                                         a->acl_dn_pat = ch_strdup( "*" );
133                                         continue;
134                                 }
135
136                                 split( argv[i], '=', &left, &right );
137                                 split( left, '.', &left, &style );
138
139                                 if ( right == NULL || *right == '\0' ) {
140                                         fprintf( stderr,
141         "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
142                                             fname, lineno, left );
143                                         acl_usage();
144                                 }
145
146                                 if ( strcasecmp( left, "dn" ) == 0 ) {
147                                         if( a->acl_dn_pat != NULL ) {
148                                                 fprintf( stderr,
149                                                         "%s: line %d: dn pattern"
150                                                         " already specified in to clause.\n",
151                                                         fname, lineno );
152                                                 acl_usage();
153                                         }
154
155                                         if ( style == NULL || *style == '\0'
156                                                 || strcasecmp( style, "regex" ) == 0 )
157                                         {
158                                                 a->acl_dn_style = ACL_STYLE_REGEX;
159                                                 if ( strcmp(right, "*") == 0 
160                                                         || strcmp(right, ".*") == 0 
161                                                         || strcmp(right, ".*$") == 0 
162                                                         || strcmp(right, "^.*") == 0 
163                                                         || strcmp(right, "^.*$$") == 0
164                                                         || strcmp(right, ".*$$") == 0 
165                                                         || strcmp(right, "^.*$$") == 0 )
166                                                 {
167                                                         a->acl_dn_pat = ch_strdup( "*" );
168
169                                                 } else {
170                                                         a->acl_dn_pat = ch_strdup( right );
171                                                 }
172                                         } else if ( strcasecmp( style, "base" ) == 0 ) {
173                                                 a->acl_dn_style = ACL_STYLE_BASE;
174                                                 a->acl_dn_pat = ch_strdup( right );
175
176                                         } else if ( strcasecmp( style, "one" ) == 0 ) {
177                                                 a->acl_dn_style = ACL_STYLE_ONE;
178                                                 a->acl_dn_pat = ch_strdup( right );
179
180                                         } else if ( strcasecmp( style, "subtree" ) == 0 ) {
181                                                 a->acl_dn_style = ACL_STYLE_SUBTREE;
182                                                 a->acl_dn_pat = ch_strdup( right );
183
184                                         } else if ( strcasecmp( style, "children" ) == 0 ) {
185                                                 a->acl_dn_style = ACL_STYLE_CHILDREN;
186                                                 a->acl_dn_pat = ch_strdup( right );
187
188                                         } else {
189                                                 fprintf( stderr,
190         "%s: line %d: unknown dn style \"%s\" in to clause\n",
191                                                     fname, lineno, style );
192                                                 acl_usage();
193                                         }
194
195                                         continue;
196                                 }
197
198                                 if ( strcasecmp( left, "filter" ) == 0 ) {
199                                         if ( (a->acl_filter = str2filter(
200                                             right )) == NULL ) {
201                                                 fprintf( stderr,
202                                 "%s: line %d: bad filter \"%s\" in to clause\n",
203                                                     fname, lineno, right );
204                                                 acl_usage();
205                                         }
206
207                                 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
208                                         char    **alist;
209
210                                         alist = str2charray( right, "," );
211                                         charray_merge( &a->acl_attrs, alist );
212                                         charray_free( alist );
213
214                                 } else {
215                                         fprintf( stderr,
216                                                 "%s: line %d: expecting <what> got \"%s\"\n",
217                                             fname, lineno, left );
218                                         acl_usage();
219                                 }
220                         }
221
222                         if ( a->acl_dn_pat != NULL && strcmp(a->acl_dn_pat, "*") == 0) {
223                                 free( a->acl_dn_pat );
224                                 a->acl_dn_pat = NULL;
225                         }
226                         
227                         if( a->acl_dn_pat != NULL ) {
228                                 if ( a->acl_dn_style != ACL_STYLE_REGEX )
229                                 {
230                                         dn_normalize(a->acl_dn_pat);
231
232                                 } else {
233                                         int e = regcomp( &a->acl_dn_re, a->acl_dn_pat,
234                                                          REG_EXTENDED | REG_ICASE );
235                                         if ( e ) {
236                                                 char buf[512];
237                                                 regerror( e, &a->acl_dn_re, buf, sizeof(buf) );
238                                                 fprintf( stderr,
239                                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
240                                                          fname, lineno, right, buf );
241                                                 acl_usage();
242                                         }
243                                 }
244                         }
245
246                 /* by clause - select who has what access to entries */
247                 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
248                         if ( a == NULL ) {
249                                 fprintf( stderr,
250                                         "%s: line %d: to clause required before by clause in access line\n",
251                                     fname, lineno );
252                                 acl_usage();
253                         }
254
255                         /*
256                          * by clause consists of <who> and <access>
257                          */
258
259                         b = (Access *) ch_calloc( 1, sizeof(Access) );
260
261                         ACL_INVALIDATE( b->a_mask );
262
263                         if ( ++i == argc ) {
264                                 fprintf( stderr,
265                             "%s: line %d: premature eol: expecting <who>\n",
266                                     fname, lineno );
267                                 acl_usage();
268                         }
269
270                         /* get <who> */
271                         for ( ; i < argc; i++ ) {
272                                 char *pat;
273                                 slap_style_t sty = ACL_STYLE_REGEX;
274
275                                 split( argv[i], '=', &left, &right );
276                                 split( left, '.', &left, &style );
277                                 if ( style == NULL || *style == '\0'
278                                         || strcasecmp( style, "regex" ) == 0 )
279                                 {
280                                         sty = ACL_STYLE_REGEX;
281                                 } else if ( strcasecmp( style, "exact" ) == 0 ) {
282                                         sty = ACL_STYLE_BASE;
283                                 } else if ( strcasecmp( style, "base" ) == 0 ) {
284                                         sty = ACL_STYLE_BASE;
285                                 } else if ( strcasecmp( style, "one" ) == 0 ) {
286                                         sty = ACL_STYLE_ONE;
287                                 } else if ( strcasecmp( style, "subtree" ) == 0 ) {
288                                         sty = ACL_STYLE_SUBTREE;
289                                 } else if ( strcasecmp( style, "children" ) == 0 ) {
290                                         sty = ACL_STYLE_CHILDREN;
291                                 } else {
292                                         fprintf( stderr,
293                                                 "%s: line %d: unknown style \"%s\" in by clause\n",
294                                             fname, lineno, style );
295                                         acl_usage();
296                                 }
297
298                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
299                                         pat = ch_strdup( "*" );
300
301                                 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
302                                         pat = ch_strdup( "anonymous" );
303
304                                 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
305                                         pat = ch_strdup( "self" );
306
307                                 } else if ( strcasecmp( argv[i], "users" ) == 0 ) {
308                                         pat = ch_strdup( "users" );
309
310                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
311                                         if ( sty == ACL_STYLE_REGEX ) {
312                                                 b->a_dn_style = ACL_STYLE_REGEX;
313                                                 if( right == NULL ) {
314                                                         /* no '=' */
315                                                         pat = ch_strdup( "users" );
316
317                                                 } else if (*right == '\0' ) {
318                                                         /* dn="" */
319                                                         pat = ch_strdup( "anonymous" );
320
321                                                 } else if ( strcmp( right, "*" ) == 0 ) {
322                                                         /* dn=* */
323                                                         /* any or users?  users for now */
324                                                         pat = ch_strdup( "users" );
325
326                                                 } else if ( strcmp( right, ".+" ) == 0
327                                                         || strcmp( right, "^.+" ) == 0
328                                                         || strcmp( right, ".+$" ) == 0
329                                                         || strcmp( right, "^.+$" ) == 0
330                                                         || strcmp( right, ".+$$" ) == 0
331                                                         || strcmp( right, "^.+$$" ) == 0 )
332                                                 {
333                                                         pat = ch_strdup( "users" );
334
335                                                 } else if ( strcmp( right, ".*" ) == 0
336                                                         || strcmp( right, "^.*" ) == 0
337                                                         || strcmp( right, ".*$" ) == 0
338                                                         || strcmp( right, "^.*$" ) == 0
339                                                         || strcmp( right, ".*$$" ) == 0
340                                                         || strcmp( right, "^.*$$" ) == 0 )
341                                                 {
342                                                         pat = ch_strdup( "*" );
343
344                                                 } else {
345                                                         regtest(fname, lineno, right);
346                                                         pat = ch_strdup( right );
347                                                 }
348                                         } else if ( right == NULL || *right == '\0' ) {
349                                                 fprintf( stderr,
350                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
351                                                     fname, lineno, left );
352                                                 acl_usage();
353
354                                         } else {
355                                                 pat = ch_strdup( right );
356                                         }
357
358                                 } else {
359                                         pat = NULL;
360                                 }
361
362                                 if( pat != NULL ) {
363                                         if( b->a_dn_pat != NULL ) {
364                                                 fprintf( stderr,
365                                                     "%s: line %d: dn pattern already specified.\n",
366                                                     fname, lineno );
367                                                 acl_usage();
368                                         }
369
370                                         b->a_dn_pat = pat;
371                                         b->a_dn_style = sty;
372                                         if ( sty != ACL_STYLE_REGEX )
373                                                 dn_normalize(pat);
374                                         continue;
375                                 }
376
377                                 if ( strcasecmp( left, "dnattr" ) == 0 ) {
378                                         if( b->a_dn_at != NULL ) {
379                                                 fprintf( stderr,
380                                                         "%s: line %d: dnattr already specified.\n",
381                                                         fname, lineno );
382                                                 acl_usage();
383                                         }
384
385                                         rc = slap_str2ad( right, &b->a_dn_at, &text );
386
387                                         if( rc != LDAP_SUCCESS ) {
388                                                 fprintf( stderr,
389                                                         "%s: line %d: dnattr \"%s\": %s\n",
390                                                         fname, lineno, right, text );
391                                                 acl_usage();
392                                         }
393
394
395                                         if( !is_at_syntax( b->a_dn_at->ad_type,
396                                                 SLAPD_DN_SYNTAX ) )
397                                         {
398                                                 fprintf( stderr,
399                                                         "%s: line %d: dnattr \"%s\": "
400                                                         "inappropriate syntax: %s\n",
401                                                         fname, lineno, right,
402                                                         b->a_dn_at->ad_type->sat_syntax_oid );
403                                                 acl_usage();
404                                         }
405
406                                         continue;
407                                 }
408
409                                 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
410                                         fprintf( stderr,
411                                                 "%s: line %d: inappropriate style \"%s\" in by clause\n",
412                                             fname, lineno, style );
413                                         acl_usage();
414                                 }
415
416                                 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
417                                         char *name = NULL;
418                                         char *value = NULL;
419
420                                         if( b->a_group_pat != NULL ) {
421                                                 fprintf( stderr,
422                                                         "%s: line %d: group pattern already specified.\n",
423                                                         fname, lineno );
424                                                 acl_usage();
425                                         }
426
427                                         /* format of string is "group/objectClassValue/groupAttrName" */
428                                         if ((value = strchr(left, '/')) != NULL) {
429                                                 *value++ = '\0';
430                                                 if (*value
431                                                         && (name = strchr(value, '/')) != NULL)
432                                                 {
433                                                         *name++ = '\0';
434                                                 }
435                                         }
436
437                                         b->a_group_style = sty;
438                                         if (sty == ACL_STYLE_REGEX) {
439                                                 regtest(fname, lineno, right);
440                                                 b->a_group_pat = ch_strdup( right );
441                                         } else {
442                                                 b->a_group_pat = ch_strdup( right );
443                                                 dn_normalize(b->a_group_pat);
444                                         }
445
446                                         if (value && *value) {
447                                                 b->a_group_oc = oc_find( value );
448                                                 *--value = '/';
449
450                                                 if( b->a_group_oc == NULL ) {
451                                                         fprintf( stderr,
452                                                                 "%s: line %d: group objectclass "
453                                                                 "\"%s\" unknown\n",
454                                                                 fname, lineno, value );
455                                                         acl_usage();
456                                                 }
457                                         } else {
458                                                 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
459
460                                                 if( b->a_group_oc == NULL ) {
461                                                         fprintf( stderr,
462                                                                 "%s: line %d: group default objectclass "
463                                                                 "\"%s\" unknown\n",
464                                                                 fname, lineno, SLAPD_GROUP_CLASS );
465                                                         acl_usage();
466                                                 }
467                                         }
468
469 #if 0
470                                         if( is_object_subclass( b->a_group_oc,
471                                                 slap_schema.si_oc_referral ) )
472                                         {
473                                                 fprintf( stderr,
474                                                         "%s: line %d: group objectclass \"%s\" "
475                                                         "is subclass of referral\n",
476                                                         fname, lineno, value );
477                                                 acl_usage();
478                                         }
479
480                                         if( is_object_subclass( b->a_group_oc,
481                                                 slap_schema.si_oc_alias ) )
482                                         {
483                                                 fprintf( stderr,
484                                                         "%s: line %d: group objectclass \"%s\" "
485                                                         "is subclass of alias\n",
486                                                         fname, lineno, value );
487                                                 acl_usage();
488                                         }
489 #endif
490
491                                         if (name && *name) {
492                                                 rc = slap_str2ad( right, &b->a_group_at, &text );
493
494                                                 if( rc != LDAP_SUCCESS ) {
495                                                         fprintf( stderr,
496                                                                 "%s: line %d: group \"%s\": %s\n",
497                                                                 fname, lineno, right, text );
498                                                         acl_usage();
499                                                 }
500                                                 *--name = '/';
501                                         } else {
502                                                 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
503
504                                                 if( rc != LDAP_SUCCESS ) {
505                                                         fprintf( stderr,
506                                                                 "%s: line %d: group \"%s\": %s\n",
507                                                                 fname, lineno, SLAPD_GROUP_ATTR, text );
508                                                         acl_usage();
509                                                 }
510                                         }
511
512                                         if( !is_at_syntax( b->a_group_at->ad_type,
513                                                 SLAPD_DN_SYNTAX ) )
514                                         {
515                                                 fprintf( stderr,
516                                                         "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
517                                                         fname, lineno, right,
518                                                         b->a_group_at->ad_type->sat_syntax_oid );
519                                                 acl_usage();
520                                         }
521
522
523                                         {
524                                                 int rc;
525                                                 struct berval val;
526                                                 struct berval *vals[2];
527
528                                                 val.bv_val = b->a_group_oc->soc_oid;
529                                                 val.bv_len = strlen(val.bv_val);
530                                                 vals[0] = &val;
531                                                 vals[1] = NULL;
532
533
534                                                 rc = oc_check_allowed( b->a_group_at->ad_type, vals );
535
536                                                 if( rc != 0 ) {
537                                                         fprintf( stderr,
538                                                                 "%s: line %d: group: \"%s\" not allowed by \"%s\"\n",
539                                                                 fname, lineno,
540                                                                 b->a_group_at->ad_cname->bv_val,
541                                                                 b->a_group_oc->soc_oid );
542                                                         acl_usage();
543                                                 }
544                                         }
545                                         continue;
546                                 }
547
548                                 if ( strcasecmp( left, "peername" ) == 0 ) {
549                                         if( b->a_peername_pat != NULL ) {
550                                                 fprintf( stderr,
551                                                         "%s: line %d: peername pattern already specified.\n",
552                                                         fname, lineno );
553                                                 acl_usage();
554                                         }
555
556                                         b->a_peername_style = sty;
557                                         if (sty == ACL_STYLE_REGEX) {
558                                                 regtest(fname, lineno, right);
559                                         }
560                                         b->a_peername_pat = ch_strdup( right );
561                                         continue;
562                                 }
563
564                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
565                                         if( b->a_sockname_pat != NULL ) {
566                                                 fprintf( stderr,
567                                                         "%s: line %d: sockname pattern already specified.\n",
568                                                         fname, lineno );
569                                                 acl_usage();
570                                         }
571
572                                         b->a_sockname_style = sty;
573                                         if (sty == ACL_STYLE_REGEX) {
574                                                 regtest(fname, lineno, right);
575                                         }
576                                         b->a_sockname_pat = ch_strdup( right );
577                                         continue;
578                                 }
579
580                                 if ( strcasecmp( left, "domain" ) == 0 ) {
581                                         if( b->a_domain_pat != NULL ) {
582                                                 fprintf( stderr,
583                                                         "%s: line %d: domain pattern already specified.\n",
584                                                         fname, lineno );
585                                                 acl_usage();
586                                         }
587
588                                         b->a_domain_style = sty;
589                                         if (sty == ACL_STYLE_REGEX) {
590                                                 regtest(fname, lineno, right);
591                                         }
592                                         b->a_domain_pat = ch_strdup( right );
593                                         continue;
594                                 }
595
596                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
597                                         if( b->a_sockurl_pat != NULL ) {
598                                                 fprintf( stderr,
599                                                         "%s: line %d: sockurl pattern already specified.\n",
600                                                         fname, lineno );
601                                                 acl_usage();
602                                         }
603
604                                         b->a_sockurl_style = sty;
605                                         if (sty == ACL_STYLE_REGEX) {
606                                                 regtest(fname, lineno, right);
607                                         }
608                                         b->a_sockurl_pat = ch_strdup( right );
609                                         continue;
610                                 }
611
612 #ifdef SLAPD_ACI_ENABLED
613                                 if ( strcasecmp( left, "aci" ) == 0 ) {
614                                         if( b->a_aci_at != NULL ) {
615                                                 fprintf( stderr,
616                                                         "%s: line %d: aci attribute already specified.\n",
617                                                         fname, lineno );
618                                                 acl_usage();
619                                         }
620
621                                         if ( right != NULL && *right != '\0' ) {
622                                                 rc = slap_str2ad( right, &b->a_aci_at, &text );
623
624                                                 if( rc != LDAP_SUCCESS ) {
625                                                         fprintf( stderr,
626                                                                 "%s: line %d: aci \"%s\": %s\n",
627                                                                 fname, lineno, right, text );
628                                                         acl_usage();
629                                                 }
630
631                                         } else {
632                                                 rc = slap_str2ad( SLAPD_ACI_ATTR, &b->a_aci_at, &text );
633
634                                                 if( rc != LDAP_SUCCESS ) {
635                                                         fprintf( stderr,
636                                                                 "%s: line %d: aci \"%s\": %s\n",
637                                                                 fname, lineno, SLAPD_ACI_ATTR, text );
638                                                         acl_usage();
639                                                 }
640                                         }
641
642                                         if( !is_at_syntax( b->a_aci_at->ad_type,
643                                                 SLAPD_ACI_SYNTAX) )
644                                         {
645                                                 fprintf( stderr,
646                                                         "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
647                                                         fname, lineno, right,
648                                                         b->a_aci_at->ad_type->sat_syntax_oid );
649                                                 acl_usage();
650                                         }
651
652                                         continue;
653                                 }
654 #endif /* SLAPD_ACI_ENABLED */
655
656                                 if( right != NULL ) {
657                                         /* unsplit */
658                                         right[-1] = '=';
659                                 }
660                                 break;
661                         }
662
663                         if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) { 
664                                 /* out of arguments or plain stop */
665
666                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
667                                 b->a_type = ACL_STOP;
668
669                                 access_append( &a->acl_access, b );
670                                 continue;
671                         }
672
673                         if( strcasecmp( left, "continue" ) == 0 ) {
674                                 /* plain continue */
675
676                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
677                                 b->a_type = ACL_CONTINUE;
678
679                                 access_append( &a->acl_access, b );
680                                 continue;
681                         }
682
683                         if( strcasecmp( left, "break" ) == 0 ) {
684                                 /* plain continue */
685
686                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
687                                 b->a_type = ACL_BREAK;
688
689                                 access_append( &a->acl_access, b );
690                                 continue;
691                         }
692
693                         if ( strcasecmp( left, "by" ) == 0 ) {
694                                 /* we've gone too far */
695                                 --i;
696                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
697                                 b->a_type = ACL_STOP;
698
699                                 access_append( &a->acl_access, b );
700                                 continue;
701                         }
702
703                         /* get <access> */
704                         if( strncasecmp( left, "self", 4 ) == 0 ) {
705                                 b->a_dn_self = 1;
706                                 ACL_PRIV_ASSIGN( b->a_mask, str2accessmask( &left[4] ) );
707
708                         } else {
709                                 ACL_PRIV_ASSIGN( b->a_mask, str2accessmask( left ) );
710                         }
711
712                         if( ACL_IS_INVALID( b->a_mask ) ) {
713                                 fprintf( stderr,
714                                         "%s: line %d: expecting <access> got \"%s\"\n",
715                                         fname, lineno, left );
716                                 acl_usage();
717                         }
718
719                         b->a_type = ACL_STOP;
720
721                         if( ++i == argc ) {
722                                 /* out of arguments or plain stop */
723                                 access_append( &a->acl_access, b );
724                                 continue;
725                         }
726
727                         if( strcasecmp( argv[i], "continue" ) == 0 ) {
728                                 /* plain continue */
729                                 b->a_type = ACL_CONTINUE;
730
731                         } else if( strcasecmp( argv[i], "break" ) == 0 ) {
732                                 /* plain continue */
733                                 b->a_type = ACL_BREAK;
734
735                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
736                                 /* gone to far */
737                                 i--;
738                         }
739
740                         access_append( &a->acl_access, b );
741
742                 } else {
743                         fprintf( stderr,
744                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
745                             fname, lineno, argv[i] );
746                         acl_usage();
747                 }
748         }
749
750         /* if we have no real access clause, complain and do nothing */
751         if ( a == NULL ) {
752                         fprintf( stderr,
753                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
754                             fname, lineno );
755
756         } else {
757 #ifdef LDAP_DEBUG
758                 if (ldap_debug & LDAP_DEBUG_ACL)
759                         print_acl(be, a);
760 #endif
761         
762                 if ( a->acl_access == NULL ) {
763                         fprintf( stderr,
764                         "%s: line %d: warning: no by clause(s) specified in access line\n",
765                             fname, lineno );
766                 }
767
768                 if ( be != NULL ) {
769                         acl_append( &be->be_acl, a );
770                 } else {
771                         acl_append( &global_acl, a );
772                 }
773         }
774 }
775
776 char *
777 accessmask2str( slap_access_mask_t mask, char *buf )
778 {
779         int none=1;
780
781         assert( buf != NULL );
782
783         if ( ACL_IS_INVALID( mask ) ) {
784                 return "invalid";
785         }
786
787         buf[0] = '\0';
788
789         if ( ACL_IS_LEVEL( mask ) ) {
790                 if ( ACL_LVL_IS_NONE(mask) ) {
791                         strcat( buf, "none" );
792
793                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
794                         strcat( buf, "auth" );
795
796                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
797                         strcat( buf, "compare" );
798
799                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
800                         strcat( buf, "search" );
801
802                 } else if ( ACL_LVL_IS_READ(mask) ) {
803                         strcat( buf, "read" );
804
805                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
806                         strcat( buf, "write" );
807                 } else {
808                         strcat( buf, "unknown" );
809                 }
810                 
811                 strcat(buf, " (");
812         }
813
814         if( ACL_IS_ADDITIVE( mask ) ) {
815                 strcat( buf, "+" );
816
817         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
818                 strcat( buf, "-" );
819
820         } else {
821                 strcat( buf, "=" );
822         }
823
824         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
825                 none = 0;
826                 strcat( buf, "w" );
827         } 
828
829         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
830                 none = 0;
831                 strcat( buf, "r" );
832         } 
833
834         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
835                 none = 0;
836                 strcat( buf, "s" );
837         } 
838
839         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
840                 none = 0;
841                 strcat( buf, "c" );
842         } 
843
844         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
845                 none = 0;
846                 strcat( buf, "x" );
847         } 
848
849         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
850                 none = 0;
851                 strcat( buf, "n" );
852         } 
853
854         if ( none ) {
855                 strcat( buf, "0" );
856         }
857
858         if ( ACL_IS_LEVEL( mask ) ) {
859                 strcat(buf, ")");
860         } 
861         return buf;
862 }
863
864 slap_access_mask_t
865 str2accessmask( const char *str )
866 {
867         slap_access_mask_t      mask;
868
869         if( !isalpha(str[0]) ) {
870                 int i;
871
872                 if ( str[0] == '=' ) {
873                         ACL_INIT(mask);
874
875                 } else if( str[0] == '+' ) {
876                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
877
878                 } else if( str[0] == '-' ) {
879                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
880
881                 } else {
882                         ACL_INVALIDATE(mask);
883                         return mask;
884                 }
885
886                 for( i=1; str[i] != '\0'; i++ ) {
887                         if( TOLOWER(str[i]) == 'w' ) {
888                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
889
890                         } else if( TOLOWER(str[i]) == 'r' ) {
891                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
892
893                         } else if( TOLOWER(str[i]) == 's' ) {
894                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
895
896                         } else if( TOLOWER(str[i]) == 'c' ) {
897                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
898
899                         } else if( TOLOWER(str[i]) == 'x' ) {
900                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
901
902                         } else if( str[i] != '0' ) {
903                                 ACL_INVALIDATE(mask);
904                                 return mask;
905                         }
906                 }
907
908                 return mask;
909         }
910
911         if ( strcasecmp( str, "none" ) == 0 ) {
912                 ACL_LVL_ASSIGN_NONE(mask);
913
914         } else if ( strcasecmp( str, "auth" ) == 0 ) {
915                 ACL_LVL_ASSIGN_AUTH(mask);
916
917         } else if ( strcasecmp( str, "compare" ) == 0 ) {
918                 ACL_LVL_ASSIGN_COMPARE(mask);
919
920         } else if ( strcasecmp( str, "search" ) == 0 ) {
921                 ACL_LVL_ASSIGN_SEARCH(mask);
922
923         } else if ( strcasecmp( str, "read" ) == 0 ) {
924                 ACL_LVL_ASSIGN_READ(mask);
925
926         } else if ( strcasecmp( str, "write" ) == 0 ) {
927                 ACL_LVL_ASSIGN_WRITE(mask);
928
929         } else {
930                 ACL_INVALIDATE( mask );
931         }
932
933         return mask;
934 }
935
936 static void
937 acl_usage( void )
938 {
939         fprintf( stderr, "\n"
940                 "<access clause> ::= access to <what> "
941                                 "[ by <who> <access> <control> ]+ \n"
942                 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
943                 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
944                 "<attr> ::= <attrname> | entry | children\n"
945                 "<who> ::= [ * | anonymous | users | self | dn=<regex> ]\n"
946                         "\t[dnattr=<attrname>]\n"
947                         "\t[group[/<objectclass>[/<attrname>]]=<regex>]\n"
948                         "\t[peername=<regex>] [sockname=<regex>]\n"
949                         "\t[domain=<regex>] [sockurl=<regex>]\n"
950 #ifdef SLAPD_ACI_ENABLED
951                         "\t[aci=<attrname>]\n"
952 #endif
953                 "<access> ::= [self]{<level>|<priv>}\n"
954                 "<level> ::= none | auth | compare | search | read | write\n"
955                 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
956                 "<control> ::= [ stop | continue | break ]\n"
957                 );
958         exit( EXIT_FAILURE );
959 }
960
961 static void
962 split(
963     char        *line,
964     int         splitchar,
965     char        **left,
966     char        **right
967 )
968 {
969         *left = line;
970         if ( (*right = strchr( line, splitchar )) != NULL ) {
971                 *((*right)++) = '\0';
972         }
973 }
974
975 static void
976 access_append( Access **l, Access *a )
977 {
978         for ( ; *l != NULL; l = &(*l)->a_next )
979                 ;       /* NULL */
980
981         *l = a;
982 }
983
984 void
985 acl_append( AccessControl **l, AccessControl *a )
986 {
987         for ( ; *l != NULL; l = &(*l)->acl_next )
988                 ;       /* NULL */
989
990         *l = a;
991 }
992
993 char *
994 access2str( slap_access_t access )
995 {
996         if ( access == ACL_NONE ) {
997                 return "none";
998
999         } else if ( access == ACL_AUTH ) {
1000                 return "auth";
1001
1002         } else if ( access == ACL_COMPARE ) {
1003                 return "compare";
1004
1005         } else if ( access == ACL_SEARCH ) {
1006                 return "search";
1007
1008         } else if ( access == ACL_READ ) {
1009                 return "read";
1010
1011         } else if ( access == ACL_WRITE ) {
1012                 return "write";
1013         }
1014
1015         return "unknown";
1016 }
1017
1018 slap_access_t
1019 str2access( const char *str )
1020 {
1021         if ( strcasecmp( str, "none" ) == 0 ) {
1022                 return ACL_NONE;
1023
1024         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1025                 return ACL_AUTH;
1026
1027         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1028                 return ACL_COMPARE;
1029
1030         } else if ( strcasecmp( str, "search" ) == 0 ) {
1031                 return ACL_SEARCH;
1032
1033         } else if ( strcasecmp( str, "read" ) == 0 ) {
1034                 return ACL_READ;
1035
1036         } else if ( strcasecmp( str, "write" ) == 0 ) {
1037                 return ACL_WRITE;
1038         }
1039
1040         return( ACL_INVALID_ACCESS );
1041 }
1042
1043 #ifdef LDAP_DEBUG
1044
1045 static char *style_strings[5] = {
1046                         "regex",
1047                         "base",
1048                         "one",
1049                         "subtree",
1050                         "children"
1051                 };
1052
1053
1054 static void
1055 print_access( Access *b )
1056 {
1057         char maskbuf[ACCESSMASK_MAXLEN];
1058
1059         fprintf( stderr, "\tby" );
1060
1061         if ( b->a_dn_pat != NULL ) {
1062                 if( strcmp(b->a_dn_pat, "*") == 0
1063                         || strcmp(b->a_dn_pat, "users") == 0 
1064                         || strcmp(b->a_dn_pat, "anonymous") == 0 
1065                         || strcmp(b->a_dn_pat, "self") == 0 )
1066                 {
1067                         fprintf( stderr, " %s", b->a_dn_pat );
1068
1069                 } else {
1070                         fprintf( stderr, " dn.%s=%s", style_strings[b->a_dn_style], b->a_dn_pat );
1071                 }
1072         }
1073
1074         if ( b->a_dn_at != NULL ) {
1075                 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname->bv_val );
1076         }
1077
1078         if ( b->a_group_pat != NULL ) {
1079                 fprintf( stderr, " group=%s", b->a_group_pat );
1080
1081                 if ( b->a_group_oc ) {
1082                         fprintf( stderr, " objectClass: %s",
1083                                 b->a_group_oc->soc_oclass.oc_oid );
1084
1085                         if ( b->a_group_at ) {
1086                                 fprintf( stderr, " attributeType: %s", b->a_group_at->ad_cname->bv_val );
1087                         }
1088                 }
1089     }
1090
1091         if ( b->a_peername_pat != NULL ) {
1092                 fprintf( stderr, " peername=%s", b->a_peername_pat );
1093         }
1094
1095         if ( b->a_sockname_pat != NULL ) {
1096                 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
1097         }
1098
1099         if ( b->a_domain_pat != NULL ) {
1100                 fprintf( stderr, " domain=%s", b->a_domain_pat );
1101         }
1102
1103         if ( b->a_sockurl_pat != NULL ) {
1104                 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
1105         }
1106
1107 #ifdef SLAPD_ACI_ENABLED
1108         if ( b->a_aci_at != NULL ) {
1109                 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname->bv_val );
1110         }
1111 #endif
1112
1113         fprintf( stderr, " %s%s",
1114                 b->a_dn_self ? "self" : "",
1115                 accessmask2str( b->a_mask, maskbuf ) );
1116
1117         if( b->a_type == ACL_BREAK ) {
1118                 fprintf( stderr, " break" );
1119
1120         } else if( b->a_type == ACL_CONTINUE ) {
1121                 fprintf( stderr, " continue" );
1122
1123         } else if( b->a_type != ACL_STOP ) {
1124                 fprintf( stderr, " unknown-control" );
1125         }
1126
1127         fprintf( stderr, "\n" );
1128 }
1129
1130
1131 static void
1132 print_acl( Backend *be, AccessControl *a )
1133 {
1134         int             to = 0;
1135         Access  *b;
1136
1137         fprintf( stderr, "%s ACL: access to",
1138                 be == NULL ? "Global" : "Backend" );
1139
1140         if ( a->acl_dn_pat != NULL ) {
1141                 to++;
1142                 fprintf( stderr, " dn.%s=%s\n",
1143                         style_strings[a->acl_dn_style], a->acl_dn_pat );
1144         }
1145
1146         if ( a->acl_filter != NULL ) {
1147                 to++;
1148                 fprintf( stderr, " filter=" );
1149                 filter_print( a->acl_filter );
1150                 fprintf( stderr, "\n" );
1151         }
1152
1153         if ( a->acl_attrs != NULL ) {
1154                 int     i, first = 1;
1155                 to++;
1156
1157                 fprintf( stderr, " attrs=" );
1158                 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
1159                         if ( ! first ) {
1160                                 fprintf( stderr, "," );
1161                         }
1162                         fprintf( stderr, a->acl_attrs[i] );
1163                         first = 0;
1164                 }
1165                 fprintf(  stderr, "\n" );
1166         }
1167
1168         if( !to ) {
1169                 fprintf( stderr, " *\n" );
1170         }
1171
1172         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1173                 print_access( b );
1174         }
1175
1176         fprintf( stderr, "\n" );
1177 }
1178
1179 #endif /* LDAP_DEBUG */