]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
a67c0b9ad825dcd14a2e8165e008b506ab034dc1
[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( is_object_subclass( b->a_group_oc,
470                                                 slap_schema.si_oc_referral ) )
471                                         {
472                                                 fprintf( stderr,
473                                                         "%s: line %d: group objectclass \"%s\" "
474                                                         "is subclass of referral\n",
475                                                         fname, lineno, value );
476                                                 acl_usage();
477                                         }
478
479                                         if( is_object_subclass( b->a_group_oc,
480                                                 slap_schema.si_oc_alias ) )
481                                         {
482                                                 fprintf( stderr,
483                                                         "%s: line %d: group objectclass \"%s\" "
484                                                         "is subclass of alias\n",
485                                                         fname, lineno, value );
486                                                 acl_usage();
487                                         }
488
489                                         if (name && *name) {
490                                                 rc = slap_str2ad( right, &b->a_group_at, &text );
491
492                                                 if( rc != LDAP_SUCCESS ) {
493                                                         fprintf( stderr,
494                                                                 "%s: line %d: group \"%s\": %s\n",
495                                                                 fname, lineno, right, text );
496                                                         acl_usage();
497                                                 }
498                                                 *--name = '/';
499                                         } else {
500                                                 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
501
502                                                 if( rc != LDAP_SUCCESS ) {
503                                                         fprintf( stderr,
504                                                                 "%s: line %d: group \"%s\": %s\n",
505                                                                 fname, lineno, SLAPD_GROUP_ATTR, text );
506                                                         acl_usage();
507                                                 }
508                                         }
509
510                                         if( !is_at_syntax( b->a_group_at->ad_type,
511                                                 SLAPD_DN_SYNTAX ) )
512                                         {
513                                                 fprintf( stderr,
514                                                         "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
515                                                         fname, lineno, right,
516                                                         b->a_group_at->ad_type->sat_syntax_oid );
517                                                 acl_usage();
518                                         }
519
520
521                                         {
522                                                 int rc;
523                                                 struct berval val;
524                                                 struct berval *vals[2];
525
526                                                 val.bv_val = b->a_group_oc->soc_oid;
527                                                 val.bv_len = strlen(val.bv_val);
528                                                 vals[0] = &val;
529                                                 vals[1] = NULL;
530
531
532                                                 rc = oc_check_allowed( b->a_group_at->ad_type, vals );
533
534                                                 if( rc != 0 ) {
535                                                         fprintf( stderr,
536                                                                 "%s: line %d: group: \"%s\" not allowed by \"%s\"\n",
537                                                                 fname, lineno,
538                                                                 b->a_group_at->ad_cname->bv_val,
539                                                                 b->a_group_oc->soc_oid );
540                                                         acl_usage();
541                                                 }
542                                         }
543                                         continue;
544                                 }
545
546                                 if ( strcasecmp( left, "peername" ) == 0 ) {
547                                         if( b->a_peername_pat != NULL ) {
548                                                 fprintf( stderr,
549                                                         "%s: line %d: peername pattern already specified.\n",
550                                                         fname, lineno );
551                                                 acl_usage();
552                                         }
553
554                                         b->a_peername_style = sty;
555                                         if (sty == ACL_STYLE_REGEX) {
556                                                 regtest(fname, lineno, right);
557                                         }
558                                         b->a_peername_pat = ch_strdup( right );
559                                         continue;
560                                 }
561
562                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
563                                         if( b->a_sockname_pat != NULL ) {
564                                                 fprintf( stderr,
565                                                         "%s: line %d: sockname pattern already specified.\n",
566                                                         fname, lineno );
567                                                 acl_usage();
568                                         }
569
570                                         b->a_sockname_style = sty;
571                                         if (sty == ACL_STYLE_REGEX) {
572                                                 regtest(fname, lineno, right);
573                                         }
574                                         b->a_sockname_pat = ch_strdup( right );
575                                         continue;
576                                 }
577
578                                 if ( strcasecmp( left, "domain" ) == 0 ) {
579                                         if( b->a_domain_pat != NULL ) {
580                                                 fprintf( stderr,
581                                                         "%s: line %d: domain pattern already specified.\n",
582                                                         fname, lineno );
583                                                 acl_usage();
584                                         }
585
586                                         b->a_domain_style = sty;
587                                         if (sty == ACL_STYLE_REGEX) {
588                                                 regtest(fname, lineno, right);
589                                         }
590                                         b->a_domain_pat = ch_strdup( right );
591                                         continue;
592                                 }
593
594                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
595                                         if( b->a_sockurl_pat != NULL ) {
596                                                 fprintf( stderr,
597                                                         "%s: line %d: sockurl pattern already specified.\n",
598                                                         fname, lineno );
599                                                 acl_usage();
600                                         }
601
602                                         b->a_sockurl_style = sty;
603                                         if (sty == ACL_STYLE_REGEX) {
604                                                 regtest(fname, lineno, right);
605                                         }
606                                         b->a_sockurl_pat = ch_strdup( right );
607                                         continue;
608                                 }
609
610 #ifdef SLAPD_ACI_ENABLED
611                                 if ( strcasecmp( left, "aci" ) == 0 ) {
612                                         if( b->a_aci_at != NULL ) {
613                                                 fprintf( stderr,
614                                                         "%s: line %d: aci attribute already specified.\n",
615                                                         fname, lineno );
616                                                 acl_usage();
617                                         }
618
619                                         if ( right != NULL && *right != '\0' ) {
620                                                 rc = slap_str2ad( right, &b->a_aci_at, &text );
621
622                                                 if( rc != LDAP_SUCCESS ) {
623                                                         fprintf( stderr,
624                                                                 "%s: line %d: aci \"%s\": %s\n",
625                                                                 fname, lineno, right, text );
626                                                         acl_usage();
627                                                 }
628
629                                         } else {
630                                                 rc = slap_str2ad( SLAPD_ACI_ATTR, &b->a_aci_at, &text );
631
632                                                 if( rc != LDAP_SUCCESS ) {
633                                                         fprintf( stderr,
634                                                                 "%s: line %d: aci \"%s\": %s\n",
635                                                                 fname, lineno, SLAPD_ACI_ATTR, text );
636                                                         acl_usage();
637                                                 }
638                                         }
639
640                                         if( !is_at_syntax( b->a_aci_at->ad_type,
641                                                 SLAPD_ACI_SYNTAX) )
642                                         {
643                                                 fprintf( stderr,
644                                                         "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
645                                                         fname, lineno, right,
646                                                         b->a_aci_at->ad_type->sat_syntax_oid );
647                                                 acl_usage();
648                                         }
649
650                                         continue;
651                                 }
652 #endif /* SLAPD_ACI_ENABLED */
653
654                                 if( right != NULL ) {
655                                         /* unsplit */
656                                         right[-1] = '=';
657                                 }
658                                 break;
659                         }
660
661                         if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) { 
662                                 /* out of arguments or plain stop */
663
664                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
665                                 b->a_type = ACL_STOP;
666
667                                 access_append( &a->acl_access, b );
668                                 continue;
669                         }
670
671                         if( strcasecmp( left, "continue" ) == 0 ) {
672                                 /* plain continue */
673
674                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
675                                 b->a_type = ACL_CONTINUE;
676
677                                 access_append( &a->acl_access, b );
678                                 continue;
679                         }
680
681                         if( strcasecmp( left, "break" ) == 0 ) {
682                                 /* plain continue */
683
684                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
685                                 b->a_type = ACL_BREAK;
686
687                                 access_append( &a->acl_access, b );
688                                 continue;
689                         }
690
691                         if ( strcasecmp( left, "by" ) == 0 ) {
692                                 /* we've gone too far */
693                                 --i;
694                                 ACL_PRIV_ASSIGN(b->a_mask, ACL_PRIV_ADDITIVE);
695                                 b->a_type = ACL_STOP;
696
697                                 access_append( &a->acl_access, b );
698                                 continue;
699                         }
700
701                         /* get <access> */
702                         if( strncasecmp( left, "self", 4 ) == 0 ) {
703                                 b->a_dn_self = 1;
704                                 ACL_PRIV_ASSIGN( b->a_mask, str2accessmask( &left[4] ) );
705
706                         } else {
707                                 ACL_PRIV_ASSIGN( b->a_mask, str2accessmask( left ) );
708                         }
709
710                         if( ACL_IS_INVALID( b->a_mask ) ) {
711                                 fprintf( stderr,
712                                         "%s: line %d: expecting <access> got \"%s\"\n",
713                                         fname, lineno, left );
714                                 acl_usage();
715                         }
716
717                         b->a_type = ACL_STOP;
718
719                         if( ++i == argc ) {
720                                 /* out of arguments or plain stop */
721                                 access_append( &a->acl_access, b );
722                                 continue;
723                         }
724
725                         if( strcasecmp( argv[i], "continue" ) == 0 ) {
726                                 /* plain continue */
727                                 b->a_type = ACL_CONTINUE;
728
729                         } else if( strcasecmp( argv[i], "break" ) == 0 ) {
730                                 /* plain continue */
731                                 b->a_type = ACL_BREAK;
732
733                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
734                                 /* gone to far */
735                                 i--;
736                         }
737
738                         access_append( &a->acl_access, b );
739
740                 } else {
741                         fprintf( stderr,
742                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
743                             fname, lineno, argv[i] );
744                         acl_usage();
745                 }
746         }
747
748         /* if we have no real access clause, complain and do nothing */
749         if ( a == NULL ) {
750                         fprintf( stderr,
751                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
752                             fname, lineno );
753
754         } else {
755 #ifdef LDAP_DEBUG
756                 if (ldap_debug & LDAP_DEBUG_ACL)
757                         print_acl(be, a);
758 #endif
759         
760                 if ( a->acl_access == NULL ) {
761                         fprintf( stderr,
762                         "%s: line %d: warning: no by clause(s) specified in access line\n",
763                             fname, lineno );
764                 }
765
766                 if ( be != NULL ) {
767                         acl_append( &be->be_acl, a );
768                 } else {
769                         acl_append( &global_acl, a );
770                 }
771         }
772 }
773
774 char *
775 accessmask2str( slap_access_mask_t mask, char *buf )
776 {
777         int none=1;
778
779         assert( buf != NULL );
780
781         if ( ACL_IS_INVALID( mask ) ) {
782                 return "invalid";
783         }
784
785         buf[0] = '\0';
786
787         if ( ACL_IS_LEVEL( mask ) ) {
788                 if ( ACL_LVL_IS_NONE(mask) ) {
789                         strcat( buf, "none" );
790
791                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
792                         strcat( buf, "auth" );
793
794                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
795                         strcat( buf, "compare" );
796
797                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
798                         strcat( buf, "search" );
799
800                 } else if ( ACL_LVL_IS_READ(mask) ) {
801                         strcat( buf, "read" );
802
803                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
804                         strcat( buf, "write" );
805                 } else {
806                         strcat( buf, "unknown" );
807                 }
808                 
809                 strcat(buf, " (");
810         }
811
812         if( ACL_IS_ADDITIVE( mask ) ) {
813                 strcat( buf, "+" );
814
815         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
816                 strcat( buf, "-" );
817
818         } else {
819                 strcat( buf, "=" );
820         }
821
822         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
823                 none = 0;
824                 strcat( buf, "w" );
825         } 
826
827         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
828                 none = 0;
829                 strcat( buf, "r" );
830         } 
831
832         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
833                 none = 0;
834                 strcat( buf, "s" );
835         } 
836
837         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
838                 none = 0;
839                 strcat( buf, "c" );
840         } 
841
842         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
843                 none = 0;
844                 strcat( buf, "x" );
845         } 
846
847         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
848                 none = 0;
849                 strcat( buf, "n" );
850         } 
851
852         if ( none ) {
853                 strcat( buf, "0" );
854         }
855
856         if ( ACL_IS_LEVEL( mask ) ) {
857                 strcat(buf, ")");
858         } 
859         return buf;
860 }
861
862 slap_access_mask_t
863 str2accessmask( const char *str )
864 {
865         slap_access_mask_t      mask;
866
867         if( !ASCII_ALPHA(str[0]) ) {
868                 int i;
869
870                 if ( str[0] == '=' ) {
871                         ACL_INIT(mask);
872
873                 } else if( str[0] == '+' ) {
874                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
875
876                 } else if( str[0] == '-' ) {
877                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
878
879                 } else {
880                         ACL_INVALIDATE(mask);
881                         return mask;
882                 }
883
884                 for( i=1; str[i] != '\0'; i++ ) {
885                         if( TOLOWER(str[i]) == 'w' ) {
886                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
887
888                         } else if( TOLOWER(str[i]) == 'r' ) {
889                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
890
891                         } else if( TOLOWER(str[i]) == 's' ) {
892                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
893
894                         } else if( TOLOWER(str[i]) == 'c' ) {
895                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
896
897                         } else if( TOLOWER(str[i]) == 'x' ) {
898                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
899
900                         } else if( str[i] != '0' ) {
901                                 ACL_INVALIDATE(mask);
902                                 return mask;
903                         }
904                 }
905
906                 return mask;
907         }
908
909         if ( strcasecmp( str, "none" ) == 0 ) {
910                 ACL_LVL_ASSIGN_NONE(mask);
911
912         } else if ( strcasecmp( str, "auth" ) == 0 ) {
913                 ACL_LVL_ASSIGN_AUTH(mask);
914
915         } else if ( strcasecmp( str, "compare" ) == 0 ) {
916                 ACL_LVL_ASSIGN_COMPARE(mask);
917
918         } else if ( strcasecmp( str, "search" ) == 0 ) {
919                 ACL_LVL_ASSIGN_SEARCH(mask);
920
921         } else if ( strcasecmp( str, "read" ) == 0 ) {
922                 ACL_LVL_ASSIGN_READ(mask);
923
924         } else if ( strcasecmp( str, "write" ) == 0 ) {
925                 ACL_LVL_ASSIGN_WRITE(mask);
926
927         } else {
928                 ACL_INVALIDATE( mask );
929         }
930
931         return mask;
932 }
933
934 static void
935 acl_usage( void )
936 {
937         fprintf( stderr, "\n"
938                 "<access clause> ::= access to <what> "
939                                 "[ by <who> <access> <control> ]+ \n"
940                 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
941                 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
942                 "<attr> ::= <attrname> | entry | children\n"
943                 "<who> ::= [ * | anonymous | users | self | dn=<regex> ]\n"
944                         "\t[dnattr=<attrname>]\n"
945                         "\t[group[/<objectclass>[/<attrname>]]=<regex>]\n"
946                         "\t[peername=<regex>] [sockname=<regex>]\n"
947                         "\t[domain=<regex>] [sockurl=<regex>]\n"
948 #ifdef SLAPD_ACI_ENABLED
949                         "\t[aci=<attrname>]\n"
950 #endif
951                 "<access> ::= [self]{<level>|<priv>}\n"
952                 "<level> ::= none | auth | compare | search | read | write\n"
953                 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
954                 "<control> ::= [ stop | continue | break ]\n"
955                 );
956         exit( EXIT_FAILURE );
957 }
958
959 static void
960 split(
961     char        *line,
962     int         splitchar,
963     char        **left,
964     char        **right
965 )
966 {
967         *left = line;
968         if ( (*right = strchr( line, splitchar )) != NULL ) {
969                 *((*right)++) = '\0';
970         }
971 }
972
973 static void
974 access_append( Access **l, Access *a )
975 {
976         for ( ; *l != NULL; l = &(*l)->a_next )
977                 ;       /* NULL */
978
979         *l = a;
980 }
981
982 void
983 acl_append( AccessControl **l, AccessControl *a )
984 {
985         for ( ; *l != NULL; l = &(*l)->acl_next )
986                 ;       /* NULL */
987
988         *l = a;
989 }
990
991 char *
992 access2str( slap_access_t access )
993 {
994         if ( access == ACL_NONE ) {
995                 return "none";
996
997         } else if ( access == ACL_AUTH ) {
998                 return "auth";
999
1000         } else if ( access == ACL_COMPARE ) {
1001                 return "compare";
1002
1003         } else if ( access == ACL_SEARCH ) {
1004                 return "search";
1005
1006         } else if ( access == ACL_READ ) {
1007                 return "read";
1008
1009         } else if ( access == ACL_WRITE ) {
1010                 return "write";
1011         }
1012
1013         return "unknown";
1014 }
1015
1016 slap_access_t
1017 str2access( const char *str )
1018 {
1019         if ( strcasecmp( str, "none" ) == 0 ) {
1020                 return ACL_NONE;
1021
1022         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1023                 return ACL_AUTH;
1024
1025         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1026                 return ACL_COMPARE;
1027
1028         } else if ( strcasecmp( str, "search" ) == 0 ) {
1029                 return ACL_SEARCH;
1030
1031         } else if ( strcasecmp( str, "read" ) == 0 ) {
1032                 return ACL_READ;
1033
1034         } else if ( strcasecmp( str, "write" ) == 0 ) {
1035                 return ACL_WRITE;
1036         }
1037
1038         return( ACL_INVALID_ACCESS );
1039 }
1040
1041 #ifdef LDAP_DEBUG
1042
1043 static char *style_strings[5] = {
1044                         "regex",
1045                         "base",
1046                         "one",
1047                         "subtree",
1048                         "children"
1049                 };
1050
1051
1052 static void
1053 print_access( Access *b )
1054 {
1055         char maskbuf[ACCESSMASK_MAXLEN];
1056
1057         fprintf( stderr, "\tby" );
1058
1059         if ( b->a_dn_pat != NULL ) {
1060                 if( strcmp(b->a_dn_pat, "*") == 0
1061                         || strcmp(b->a_dn_pat, "users") == 0 
1062                         || strcmp(b->a_dn_pat, "anonymous") == 0 
1063                         || strcmp(b->a_dn_pat, "self") == 0 )
1064                 {
1065                         fprintf( stderr, " %s", b->a_dn_pat );
1066
1067                 } else {
1068                         fprintf( stderr, " dn.%s=%s", style_strings[b->a_dn_style], b->a_dn_pat );
1069                 }
1070         }
1071
1072         if ( b->a_dn_at != NULL ) {
1073                 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname->bv_val );
1074         }
1075
1076         if ( b->a_group_pat != NULL ) {
1077                 fprintf( stderr, " group=%s", b->a_group_pat );
1078
1079                 if ( b->a_group_oc ) {
1080                         fprintf( stderr, " objectClass: %s",
1081                                 b->a_group_oc->soc_oclass.oc_oid );
1082
1083                         if ( b->a_group_at ) {
1084                                 fprintf( stderr, " attributeType: %s", b->a_group_at->ad_cname->bv_val );
1085                         }
1086                 }
1087     }
1088
1089         if ( b->a_peername_pat != NULL ) {
1090                 fprintf( stderr, " peername=%s", b->a_peername_pat );
1091         }
1092
1093         if ( b->a_sockname_pat != NULL ) {
1094                 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
1095         }
1096
1097         if ( b->a_domain_pat != NULL ) {
1098                 fprintf( stderr, " domain=%s", b->a_domain_pat );
1099         }
1100
1101         if ( b->a_sockurl_pat != NULL ) {
1102                 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
1103         }
1104
1105 #ifdef SLAPD_ACI_ENABLED
1106         if ( b->a_aci_at != NULL ) {
1107                 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname->bv_val );
1108         }
1109 #endif
1110
1111         fprintf( stderr, " %s%s",
1112                 b->a_dn_self ? "self" : "",
1113                 accessmask2str( b->a_mask, maskbuf ) );
1114
1115         if( b->a_type == ACL_BREAK ) {
1116                 fprintf( stderr, " break" );
1117
1118         } else if( b->a_type == ACL_CONTINUE ) {
1119                 fprintf( stderr, " continue" );
1120
1121         } else if( b->a_type != ACL_STOP ) {
1122                 fprintf( stderr, " unknown-control" );
1123         }
1124
1125         fprintf( stderr, "\n" );
1126 }
1127
1128
1129 static void
1130 print_acl( Backend *be, AccessControl *a )
1131 {
1132         int             to = 0;
1133         Access  *b;
1134
1135         fprintf( stderr, "%s ACL: access to",
1136                 be == NULL ? "Global" : "Backend" );
1137
1138         if ( a->acl_dn_pat != NULL ) {
1139                 to++;
1140                 fprintf( stderr, " dn.%s=%s\n",
1141                         style_strings[a->acl_dn_style], a->acl_dn_pat );
1142         }
1143
1144         if ( a->acl_filter != NULL ) {
1145                 to++;
1146                 fprintf( stderr, " filter=" );
1147                 filter_print( a->acl_filter );
1148                 fprintf( stderr, "\n" );
1149         }
1150
1151         if ( a->acl_attrs != NULL ) {
1152                 int     i, first = 1;
1153                 to++;
1154
1155                 fprintf( stderr, " attrs=" );
1156                 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
1157                         if ( ! first ) {
1158                                 fprintf( stderr, "," );
1159                         }
1160                         fprintf( stderr, a->acl_attrs[i] );
1161                         first = 0;
1162                 }
1163                 fprintf(  stderr, "\n" );
1164         }
1165
1166         if( !to ) {
1167                 fprintf( stderr, " *\n" );
1168         }
1169
1170         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1171                 print_access( b );
1172         }
1173
1174         fprintf( stderr, "\n" );
1175 }
1176
1177 #endif /* LDAP_DEBUG */