]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
Remove lint
[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 ) {
140                                         fprintf( stderr,
141         "%s: line %d: missing \"=\" in \"%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_access_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_EXACT;
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 ( right == NULL || right[ 0 ] == '\0' ) {
379                                                 fprintf( stderr,
380                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
381                                                         fname, lineno, left );
382                                                 acl_usage();
383                                         }
384
385                                         if( b->a_dn_at != NULL ) {
386                                                 fprintf( stderr,
387                                                         "%s: line %d: dnattr already specified.\n",
388                                                         fname, lineno );
389                                                 acl_usage();
390                                         }
391
392                                         rc = slap_str2ad( right, &b->a_dn_at, &text );
393
394                                         if( rc != LDAP_SUCCESS ) {
395                                                 fprintf( stderr,
396                                                         "%s: line %d: dnattr \"%s\": %s\n",
397                                                         fname, lineno, right, text );
398                                                 acl_usage();
399                                         }
400
401
402                                         if( !is_at_syntax( b->a_dn_at->ad_type,
403                                                 SLAPD_DN_SYNTAX ) &&
404                                                 !is_at_syntax( b->a_dn_at->ad_type,
405                                                 SLAPD_NAMEUID_SYNTAX ))
406                                         {
407                                                 fprintf( stderr,
408                                                         "%s: line %d: dnattr \"%s\": "
409                                                         "inappropriate syntax: %s\n",
410                                                         fname, lineno, right,
411                                                         b->a_dn_at->ad_type->sat_syntax_oid );
412                                                 acl_usage();
413                                         }
414
415                                         continue;
416                                 }
417
418                                 if (sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE) {
419                                         fprintf( stderr,
420                                                 "%s: line %d: inappropriate style \"%s\" in by clause\n",
421                                             fname, lineno, style );
422                                         acl_usage();
423                                 }
424
425                                 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
426                                         char *name = NULL;
427                                         char *value = NULL;
428
429                                         if ( right == NULL || right[ 0 ] == '\0' ) {
430                                                 fprintf( stderr,
431                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
432                                                         fname, lineno, left );
433                                                 acl_usage();
434                                         }
435
436                                         if( b->a_group_pat != NULL ) {
437                                                 fprintf( stderr,
438                                                         "%s: line %d: group pattern already specified.\n",
439                                                         fname, lineno );
440                                                 acl_usage();
441                                         }
442
443                                         /* format of string is "group/objectClassValue/groupAttrName" */
444                                         if ((value = strchr(left, '/')) != NULL) {
445                                                 *value++ = '\0';
446                                                 if (*value
447                                                         && (name = strchr(value, '/')) != NULL)
448                                                 {
449                                                         *name++ = '\0';
450                                                 }
451                                         }
452
453                                         b->a_group_style = sty;
454                                         if (sty == ACL_STYLE_REGEX) {
455                                                 regtest(fname, lineno, right);
456                                                 b->a_group_pat = ch_strdup( right );
457                                         } else {
458                                                 b->a_group_pat = ch_strdup( right );
459                                                 dn_normalize(b->a_group_pat);
460                                         }
461
462                                         if (value && *value) {
463                                                 b->a_group_oc = oc_find( value );
464                                                 *--value = '/';
465
466                                                 if( b->a_group_oc == NULL ) {
467                                                         fprintf( stderr,
468                                                                 "%s: line %d: group objectclass "
469                                                                 "\"%s\" unknown\n",
470                                                                 fname, lineno, value );
471                                                         acl_usage();
472                                                 }
473                                         } else {
474                                                 b->a_group_oc = oc_find(SLAPD_GROUP_CLASS);
475
476                                                 if( b->a_group_oc == NULL ) {
477                                                         fprintf( stderr,
478                                                                 "%s: line %d: group default objectclass "
479                                                                 "\"%s\" unknown\n",
480                                                                 fname, lineno, SLAPD_GROUP_CLASS );
481                                                         acl_usage();
482                                                 }
483                                         }
484
485                                         if( is_object_subclass( b->a_group_oc,
486                                                 slap_schema.si_oc_referral ) )
487                                         {
488                                                 fprintf( stderr,
489                                                         "%s: line %d: group objectclass \"%s\" "
490                                                         "is subclass of referral\n",
491                                                         fname, lineno, value );
492                                                 acl_usage();
493                                         }
494
495                                         if( is_object_subclass( b->a_group_oc,
496                                                 slap_schema.si_oc_alias ) )
497                                         {
498                                                 fprintf( stderr,
499                                                         "%s: line %d: group objectclass \"%s\" "
500                                                         "is subclass of alias\n",
501                                                         fname, lineno, value );
502                                                 acl_usage();
503                                         }
504
505                                         if (name && *name) {
506                                                 rc = slap_str2ad( name, &b->a_group_at, &text );
507
508                                                 if( rc != LDAP_SUCCESS ) {
509                                                         fprintf( stderr,
510                                                                 "%s: line %d: group \"%s\": %s\n",
511                                                                 fname, lineno, right, text );
512                                                         acl_usage();
513                                                 }
514                                                 *--name = '/';
515                                         } else {
516                                                 rc = slap_str2ad( SLAPD_GROUP_ATTR, &b->a_group_at, &text );
517
518                                                 if( rc != LDAP_SUCCESS ) {
519                                                         fprintf( stderr,
520                                                                 "%s: line %d: group \"%s\": %s\n",
521                                                                 fname, lineno, SLAPD_GROUP_ATTR, text );
522                                                         acl_usage();
523                                                 }
524                                         }
525
526                                         if( !is_at_syntax( b->a_group_at->ad_type,
527                                                 SLAPD_DN_SYNTAX ) &&
528                                             !is_at_syntax( b->a_group_at->ad_type,
529                                                 SLAPD_NAMEUID_SYNTAX ) )
530                                         {
531                                                 fprintf( stderr,
532                                                         "%s: line %d: group \"%s\": inappropriate syntax: %s\n",
533                                                         fname, lineno, right,
534                                                         b->a_group_at->ad_type->sat_syntax_oid );
535                                                 acl_usage();
536                                         }
537
538
539                                         {
540                                                 int rc;
541                                                 struct berval val;
542                                                 struct berval *vals[2];
543
544                                                 val.bv_val = b->a_group_oc->soc_oid;
545                                                 val.bv_len = strlen(val.bv_val);
546                                                 vals[0] = &val;
547                                                 vals[1] = NULL;
548
549
550                                                 rc = oc_check_allowed( b->a_group_at->ad_type, vals );
551
552                                                 if( rc != 0 ) {
553                                                         fprintf( stderr,
554                                                                 "%s: line %d: group: \"%s\" not allowed by \"%s\"\n",
555                                                                 fname, lineno,
556                                                                 b->a_group_at->ad_cname.bv_val,
557                                                                 b->a_group_oc->soc_oid );
558                                                         acl_usage();
559                                                 }
560                                         }
561                                         continue;
562                                 }
563
564                                 if ( strcasecmp( left, "peername" ) == 0 ) {
565                                         if ( right == NULL || right[ 0 ] == '\0' ) {
566                                                 fprintf( stderr,
567                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
568                                                         fname, lineno, left );
569                                                 acl_usage();
570                                         }
571
572                                         if( b->a_peername_pat != NULL ) {
573                                                 fprintf( stderr,
574                                                         "%s: line %d: peername pattern already specified.\n",
575                                                         fname, lineno );
576                                                 acl_usage();
577                                         }
578
579                                         b->a_peername_style = sty;
580                                         if (sty == ACL_STYLE_REGEX) {
581                                                 regtest(fname, lineno, right);
582                                         }
583                                         b->a_peername_pat = ch_strdup( right );
584                                         continue;
585                                 }
586
587                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
588                                         if ( right == NULL || right[ 0 ] == '\0' ) {
589                                                 fprintf( stderr,
590                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
591                                                         fname, lineno, left );
592                                                 acl_usage();
593                                         }
594
595                                         if( b->a_sockname_pat != NULL ) {
596                                                 fprintf( stderr,
597                                                         "%s: line %d: sockname pattern already specified.\n",
598                                                         fname, lineno );
599                                                 acl_usage();
600                                         }
601
602                                         b->a_sockname_style = sty;
603                                         if (sty == ACL_STYLE_REGEX) {
604                                                 regtest(fname, lineno, right);
605                                         }
606                                         b->a_sockname_pat = ch_strdup( right );
607                                         continue;
608                                 }
609
610                                 if ( strcasecmp( left, "domain" ) == 0 ) {
611                                         if ( right == NULL || right[ 0 ] == '\0' ) {
612                                                 fprintf( stderr,
613                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
614                                                         fname, lineno, left );
615                                                 acl_usage();
616                                         }
617
618                                         if( b->a_domain_pat != NULL ) {
619                                                 fprintf( stderr,
620                                                         "%s: line %d: domain pattern already specified.\n",
621                                                         fname, lineno );
622                                                 acl_usage();
623                                         }
624
625                                         b->a_domain_style = sty;
626                                         if (sty == ACL_STYLE_REGEX) {
627                                                 regtest(fname, lineno, right);
628                                         }
629                                         b->a_domain_pat = ch_strdup( right );
630                                         continue;
631                                 }
632
633                                 if ( strcasecmp( left, "sockurl" ) == 0 ) {
634                                         if ( right == NULL || right[ 0 ] == '\0' ) {
635                                                 fprintf( stderr,
636                                                         "%s: line %d: missing \"=\" in (or value after) \"%s\" in by clause\n",
637                                                         fname, lineno, left );
638                                                 acl_usage();
639                                         }
640
641                                         if( b->a_sockurl_pat != NULL ) {
642                                                 fprintf( stderr,
643                                                         "%s: line %d: sockurl pattern already specified.\n",
644                                                         fname, lineno );
645                                                 acl_usage();
646                                         }
647
648                                         b->a_sockurl_style = sty;
649                                         if (sty == ACL_STYLE_REGEX) {
650                                                 regtest(fname, lineno, right);
651                                         }
652                                         b->a_sockurl_pat = ch_strdup( right );
653                                         continue;
654                                 }
655
656                                 if ( strcasecmp( left, "set" ) == 0 ) {
657                                         if( b->a_set_pat != NULL ) {
658                                                 fprintf( stderr,
659                                                         "%s: line %d: set attribute already specified.\n",
660                                                         fname, lineno );
661                                                 acl_usage();
662                                         }
663
664                                         if ( right == NULL || *right == '\0' ) {
665                                                 fprintf( stderr,
666                                                         "%s: line %d: no set is defined\n",
667                                                         fname, lineno );
668                                                 acl_usage();
669                                         }
670
671                                         b->a_set_style = sty;
672                                         b->a_set_pat = ch_strdup(right);
673
674                                         continue;
675                                 }
676
677 #ifdef SLAPD_ACI_ENABLED
678                                 if ( strcasecmp( left, "aci" ) == 0 ) {
679                                         if( b->a_aci_at != NULL ) {
680                                                 fprintf( stderr,
681                                                         "%s: line %d: aci attribute already specified.\n",
682                                                         fname, lineno );
683                                                 acl_usage();
684                                         }
685
686                                         if ( right != NULL && *right != '\0' ) {
687                                                 rc = slap_str2ad( right, &b->a_aci_at, &text );
688
689                                                 if( rc != LDAP_SUCCESS ) {
690                                                         fprintf( stderr,
691                                                                 "%s: line %d: aci \"%s\": %s\n",
692                                                                 fname, lineno, right, text );
693                                                         acl_usage();
694                                                 }
695
696                                         } else {
697                                                 rc = slap_str2ad( SLAPD_ACI_ATTR, &b->a_aci_at, &text );
698
699                                                 if( rc != LDAP_SUCCESS ) {
700                                                         fprintf( stderr,
701                                                                 "%s: line %d: aci \"%s\": %s\n",
702                                                                 fname, lineno, SLAPD_ACI_ATTR, text );
703                                                         acl_usage();
704                                                 }
705                                         }
706
707                                         if( !is_at_syntax( b->a_aci_at->ad_type,
708                                                 SLAPD_ACI_SYNTAX) )
709                                         {
710                                                 fprintf( stderr,
711                                                         "%s: line %d: aci \"%s\": inappropriate syntax: %s\n",
712                                                         fname, lineno, right,
713                                                         b->a_aci_at->ad_type->sat_syntax_oid );
714                                                 acl_usage();
715                                         }
716
717                                         continue;
718                                 }
719 #endif /* SLAPD_ACI_ENABLED */
720
721                                 if ( strcasecmp( left, "ssf" ) == 0 ) {
722                                         if( b->a_authz.sai_ssf ) {
723                                                 fprintf( stderr,
724                                                         "%s: line %d: ssf attribute already specified.\n",
725                                                         fname, lineno );
726                                                 acl_usage();
727                                         }
728
729                                         if ( right == NULL || *right == '\0' ) {
730                                                 fprintf( stderr,
731                                                         "%s: line %d: no ssf is defined\n",
732                                                         fname, lineno );
733                                                 acl_usage();
734                                         }
735
736                                         b->a_authz.sai_ssf = atoi( right );
737
738                                         if( !b->a_authz.sai_ssf ) {
739                                                 fprintf( stderr,
740                                                         "%s: line %d: invalid ssf value (%s)\n",
741                                                         fname, lineno, right );
742                                                 acl_usage();
743                                         }
744                                         continue;
745                                 }
746
747                                 if ( strcasecmp( left, "transport_ssf" ) == 0 ) {
748                                         if( b->a_authz.sai_transport_ssf ) {
749                                                 fprintf( stderr,
750                                                         "%s: line %d: transport_ssf attribute already specified.\n",
751                                                         fname, lineno );
752                                                 acl_usage();
753                                         }
754
755                                         if ( right == NULL || *right == '\0' ) {
756                                                 fprintf( stderr,
757                                                         "%s: line %d: no transport_ssf is defined\n",
758                                                         fname, lineno );
759                                                 acl_usage();
760                                         }
761
762                                         b->a_authz.sai_transport_ssf = atoi( right );
763
764                                         if( !b->a_authz.sai_transport_ssf ) {
765                                                 fprintf( stderr,
766                                                         "%s: line %d: invalid transport_ssf value (%s)\n",
767                                                         fname, lineno, right );
768                                                 acl_usage();
769                                         }
770                                         continue;
771                                 }
772
773                                 if ( strcasecmp( left, "tls_ssf" ) == 0 ) {
774                                         if( b->a_authz.sai_tls_ssf ) {
775                                                 fprintf( stderr,
776                                                         "%s: line %d: tls_ssf attribute already specified.\n",
777                                                         fname, lineno );
778                                                 acl_usage();
779                                         }
780
781                                         if ( right == NULL || *right == '\0' ) {
782                                                 fprintf( stderr,
783                                                         "%s: line %d: no tls_ssf is defined\n",
784                                                         fname, lineno );
785                                                 acl_usage();
786                                         }
787
788                                         b->a_authz.sai_tls_ssf = atoi( right );
789
790                                         if( !b->a_authz.sai_tls_ssf ) {
791                                                 fprintf( stderr,
792                                                         "%s: line %d: invalid tls_ssf value (%s)\n",
793                                                         fname, lineno, right );
794                                                 acl_usage();
795                                         }
796                                         continue;
797                                 }
798
799                                 if ( strcasecmp( left, "sasl_ssf" ) == 0 ) {
800                                         if( b->a_authz.sai_sasl_ssf ) {
801                                                 fprintf( stderr,
802                                                         "%s: line %d: sasl_ssf attribute already specified.\n",
803                                                         fname, lineno );
804                                                 acl_usage();
805                                         }
806
807                                         if ( right == NULL || *right == '\0' ) {
808                                                 fprintf( stderr,
809                                                         "%s: line %d: no sasl_ssf is defined\n",
810                                                         fname, lineno );
811                                                 acl_usage();
812                                         }
813
814                                         b->a_authz.sai_sasl_ssf = atoi( right );
815
816                                         if( !b->a_authz.sai_sasl_ssf ) {
817                                                 fprintf( stderr,
818                                                         "%s: line %d: invalid sasl_ssf value (%s)\n",
819                                                         fname, lineno, right );
820                                                 acl_usage();
821                                         }
822                                         continue;
823                                 }
824
825                                 if( right != NULL ) {
826                                         /* unsplit */
827                                         right[-1] = '=';
828                                 }
829                                 break;
830                         }
831
832                         if( i == argc || ( strcasecmp( left, "stop" ) == 0 )) { 
833                                 /* out of arguments or plain stop */
834
835                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
836                                 b->a_type = ACL_STOP;
837
838                                 access_append( &a->acl_access, b );
839                                 continue;
840                         }
841
842                         if( strcasecmp( left, "continue" ) == 0 ) {
843                                 /* plain continue */
844
845                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
846                                 b->a_type = ACL_CONTINUE;
847
848                                 access_append( &a->acl_access, b );
849                                 continue;
850                         }
851
852                         if( strcasecmp( left, "break" ) == 0 ) {
853                                 /* plain continue */
854
855                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
856                                 b->a_type = ACL_BREAK;
857
858                                 access_append( &a->acl_access, b );
859                                 continue;
860                         }
861
862                         if ( strcasecmp( left, "by" ) == 0 ) {
863                                 /* we've gone too far */
864                                 --i;
865                                 ACL_PRIV_ASSIGN(b->a_access_mask, ACL_PRIV_ADDITIVE);
866                                 b->a_type = ACL_STOP;
867
868                                 access_append( &a->acl_access, b );
869                                 continue;
870                         }
871
872                         /* get <access> */
873                         if( strncasecmp( left, "self", 4 ) == 0 ) {
874                                 b->a_dn_self = 1;
875                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( &left[4] ) );
876
877                         } else {
878                                 ACL_PRIV_ASSIGN( b->a_access_mask, str2accessmask( left ) );
879                         }
880
881                         if( ACL_IS_INVALID( b->a_access_mask ) ) {
882                                 fprintf( stderr,
883                                         "%s: line %d: expecting <access> got \"%s\"\n",
884                                         fname, lineno, left );
885                                 acl_usage();
886                         }
887
888                         b->a_type = ACL_STOP;
889
890                         if( ++i == argc ) {
891                                 /* out of arguments or plain stop */
892                                 access_append( &a->acl_access, b );
893                                 continue;
894                         }
895
896                         if( strcasecmp( argv[i], "continue" ) == 0 ) {
897                                 /* plain continue */
898                                 b->a_type = ACL_CONTINUE;
899
900                         } else if( strcasecmp( argv[i], "break" ) == 0 ) {
901                                 /* plain continue */
902                                 b->a_type = ACL_BREAK;
903
904                         } else if ( strcasecmp( argv[i], "stop" ) != 0 ) {
905                                 /* gone to far */
906                                 i--;
907                         }
908
909                         access_append( &a->acl_access, b );
910
911                 } else {
912                         fprintf( stderr,
913                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
914                             fname, lineno, argv[i] );
915                         acl_usage();
916                 }
917         }
918
919         /* if we have no real access clause, complain and do nothing */
920         if ( a == NULL ) {
921                         fprintf( stderr,
922                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
923                             fname, lineno );
924
925         } else {
926 #ifdef LDAP_DEBUG
927                 if (ldap_debug & LDAP_DEBUG_ACL)
928                         print_acl(be, a);
929 #endif
930         
931                 if ( a->acl_access == NULL ) {
932                         fprintf( stderr,
933                         "%s: line %d: warning: no by clause(s) specified in access line\n",
934                             fname, lineno );
935                 }
936
937                 if ( be != NULL ) {
938                         acl_append( &be->be_acl, a );
939                 } else {
940                         acl_append( &global_acl, a );
941                 }
942         }
943 }
944
945 char *
946 accessmask2str( slap_mask_t mask, char *buf )
947 {
948         int none=1;
949
950         assert( buf != NULL );
951
952         if ( ACL_IS_INVALID( mask ) ) {
953                 return "invalid";
954         }
955
956         buf[0] = '\0';
957
958         if ( ACL_IS_LEVEL( mask ) ) {
959                 if ( ACL_LVL_IS_NONE(mask) ) {
960                         strcat( buf, "none" );
961
962                 } else if ( ACL_LVL_IS_AUTH(mask) ) {
963                         strcat( buf, "auth" );
964
965                 } else if ( ACL_LVL_IS_COMPARE(mask) ) {
966                         strcat( buf, "compare" );
967
968                 } else if ( ACL_LVL_IS_SEARCH(mask) ) {
969                         strcat( buf, "search" );
970
971                 } else if ( ACL_LVL_IS_READ(mask) ) {
972                         strcat( buf, "read" );
973
974                 } else if ( ACL_LVL_IS_WRITE(mask) ) {
975                         strcat( buf, "write" );
976                 } else {
977                         strcat( buf, "unknown" );
978                 }
979                 
980                 strcat(buf, " (");
981         }
982
983         if( ACL_IS_ADDITIVE( mask ) ) {
984                 strcat( buf, "+" );
985
986         } else if( ACL_IS_SUBTRACTIVE( mask ) ) {
987                 strcat( buf, "-" );
988
989         } else {
990                 strcat( buf, "=" );
991         }
992
993         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_WRITE) ) {
994                 none = 0;
995                 strcat( buf, "w" );
996         } 
997
998         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_READ) ) {
999                 none = 0;
1000                 strcat( buf, "r" );
1001         } 
1002
1003         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_SEARCH) ) {
1004                 none = 0;
1005                 strcat( buf, "s" );
1006         } 
1007
1008         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_COMPARE) ) {
1009                 none = 0;
1010                 strcat( buf, "c" );
1011         } 
1012
1013         if ( ACL_PRIV_ISSET(mask, ACL_PRIV_AUTH) ) {
1014                 none = 0;
1015                 strcat( buf, "x" );
1016         } 
1017
1018         if ( none && ACL_PRIV_ISSET(mask, ACL_PRIV_NONE) ) {
1019                 none = 0;
1020                 strcat( buf, "n" );
1021         } 
1022
1023         if ( none ) {
1024                 strcat( buf, "0" );
1025         }
1026
1027         if ( ACL_IS_LEVEL( mask ) ) {
1028                 strcat(buf, ")");
1029         } 
1030         return buf;
1031 }
1032
1033 slap_mask_t
1034 str2accessmask( const char *str )
1035 {
1036         slap_mask_t     mask;
1037
1038         if( !ASCII_ALPHA(str[0]) ) {
1039                 int i;
1040
1041                 if ( str[0] == '=' ) {
1042                         ACL_INIT(mask);
1043
1044                 } else if( str[0] == '+' ) {
1045                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_ADDITIVE);
1046
1047                 } else if( str[0] == '-' ) {
1048                         ACL_PRIV_ASSIGN(mask, ACL_PRIV_SUBSTRACTIVE);
1049
1050                 } else {
1051                         ACL_INVALIDATE(mask);
1052                         return mask;
1053                 }
1054
1055                 for( i=1; str[i] != '\0'; i++ ) {
1056                         if( TOLOWER(str[i]) == 'w' ) {
1057                                 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
1058
1059                         } else if( TOLOWER(str[i]) == 'r' ) {
1060                                 ACL_PRIV_SET(mask, ACL_PRIV_READ);
1061
1062                         } else if( TOLOWER(str[i]) == 's' ) {
1063                                 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
1064
1065                         } else if( TOLOWER(str[i]) == 'c' ) {
1066                                 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
1067
1068                         } else if( TOLOWER(str[i]) == 'x' ) {
1069                                 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
1070
1071                         } else if( str[i] != '0' ) {
1072                                 ACL_INVALIDATE(mask);
1073                                 return mask;
1074                         }
1075                 }
1076
1077                 return mask;
1078         }
1079
1080         if ( strcasecmp( str, "none" ) == 0 ) {
1081                 ACL_LVL_ASSIGN_NONE(mask);
1082
1083         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1084                 ACL_LVL_ASSIGN_AUTH(mask);
1085
1086         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1087                 ACL_LVL_ASSIGN_COMPARE(mask);
1088
1089         } else if ( strcasecmp( str, "search" ) == 0 ) {
1090                 ACL_LVL_ASSIGN_SEARCH(mask);
1091
1092         } else if ( strcasecmp( str, "read" ) == 0 ) {
1093                 ACL_LVL_ASSIGN_READ(mask);
1094
1095         } else if ( strcasecmp( str, "write" ) == 0 ) {
1096                 ACL_LVL_ASSIGN_WRITE(mask);
1097
1098         } else {
1099                 ACL_INVALIDATE( mask );
1100         }
1101
1102         return mask;
1103 }
1104
1105 static void
1106 acl_usage( void )
1107 {
1108         fprintf( stderr, "\n"
1109                 "<access clause> ::= access to <what> "
1110                                 "[ by <who> <access> [ <control> ] ]+ \n"
1111                 "<what> ::= * | [dn[.<dnstyle>]=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
1112                 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
1113                 "<attr> ::= <attrname> | entry | children\n"
1114                 "<who> ::= [ * | anonymous | users | self | dn[.<dnstyle>]=<regex> ]\n"
1115                         "\t[dnattr=<attrname>]\n"
1116                         "\t[group[/<objectclass>[/<attrname>]][.<style>]=<regex>]\n"
1117                         "\t[peername[.<style>]=<regex>] [sockname[.<style>]=<regex>]\n"
1118                         "\t[domain[.<style>]=<regex>] [sockurl[.<style>]=<regex>]\n"
1119 #ifdef SLAPD_ACI_ENABLED
1120                         "\t[aci=<attrname>]\n"
1121 #endif
1122                         "\t[ssf=<n>] [transport_ssf=<n>] [tls_ssf=<n>] [sasl_ssf=<n>]\n"
1123                 "<dnstyle> ::= regex | base | exact (alias of base) | one | sub | children\n"
1124                 "<style> ::= regex | base | exact (alias of base)\n"
1125                 "<groupflags> ::= R\n"
1126                 "<access> ::= [self]{<level>|<priv>}\n"
1127                 "<level> ::= none | auth | compare | search | read | write\n"
1128                 "<priv> ::= {=|+|-}{w|r|s|c|x}+\n"
1129                 "<control> ::= [ stop | continue | break ]\n"
1130                 );
1131         exit( EXIT_FAILURE );
1132 }
1133
1134 static void
1135 split(
1136     char        *line,
1137     int         splitchar,
1138     char        **left,
1139     char        **right
1140 )
1141 {
1142         *left = line;
1143         if ( (*right = strchr( line, splitchar )) != NULL ) {
1144                 *((*right)++) = '\0';
1145         }
1146 }
1147
1148 static void
1149 access_append( Access **l, Access *a )
1150 {
1151         for ( ; *l != NULL; l = &(*l)->a_next )
1152                 ;       /* NULL */
1153
1154         *l = a;
1155 }
1156
1157 void
1158 acl_append( AccessControl **l, AccessControl *a )
1159 {
1160         for ( ; *l != NULL; l = &(*l)->acl_next )
1161                 ;       /* NULL */
1162
1163         *l = a;
1164 }
1165
1166 char *
1167 access2str( slap_access_t access )
1168 {
1169         if ( access == ACL_NONE ) {
1170                 return "none";
1171
1172         } else if ( access == ACL_AUTH ) {
1173                 return "auth";
1174
1175         } else if ( access == ACL_COMPARE ) {
1176                 return "compare";
1177
1178         } else if ( access == ACL_SEARCH ) {
1179                 return "search";
1180
1181         } else if ( access == ACL_READ ) {
1182                 return "read";
1183
1184         } else if ( access == ACL_WRITE ) {
1185                 return "write";
1186         }
1187
1188         return "unknown";
1189 }
1190
1191 slap_access_t
1192 str2access( const char *str )
1193 {
1194         if ( strcasecmp( str, "none" ) == 0 ) {
1195                 return ACL_NONE;
1196
1197         } else if ( strcasecmp( str, "auth" ) == 0 ) {
1198                 return ACL_AUTH;
1199
1200         } else if ( strcasecmp( str, "compare" ) == 0 ) {
1201                 return ACL_COMPARE;
1202
1203         } else if ( strcasecmp( str, "search" ) == 0 ) {
1204                 return ACL_SEARCH;
1205
1206         } else if ( strcasecmp( str, "read" ) == 0 ) {
1207                 return ACL_READ;
1208
1209         } else if ( strcasecmp( str, "write" ) == 0 ) {
1210                 return ACL_WRITE;
1211         }
1212
1213         return( ACL_INVALID_ACCESS );
1214 }
1215
1216 #ifdef LDAP_DEBUG
1217
1218 static char *style_strings[5] = {
1219                         "regex",
1220                         "base",
1221                         "one",
1222                         "subtree",
1223                         "children"
1224                 };
1225
1226
1227 static void
1228 print_access( Access *b )
1229 {
1230         char maskbuf[ACCESSMASK_MAXLEN];
1231
1232         fprintf( stderr, "\tby" );
1233
1234         if ( b->a_dn_pat != NULL ) {
1235                 if( strcmp(b->a_dn_pat, "*") == 0
1236                         || strcmp(b->a_dn_pat, "users") == 0 
1237                         || strcmp(b->a_dn_pat, "anonymous") == 0 
1238                         || strcmp(b->a_dn_pat, "self") == 0 )
1239                 {
1240                         fprintf( stderr, " %s", b->a_dn_pat );
1241
1242                 } else {
1243                         fprintf( stderr, " dn.%s=%s", style_strings[b->a_dn_style], b->a_dn_pat );
1244                 }
1245         }
1246
1247         if ( b->a_dn_at != NULL ) {
1248                 fprintf( stderr, " dnattr=%s", b->a_dn_at->ad_cname.bv_val );
1249         }
1250
1251         if ( b->a_group_pat != NULL ) {
1252                 fprintf( stderr, " group=%s", b->a_group_pat );
1253
1254                 if ( b->a_group_oc ) {
1255                         fprintf( stderr, " objectClass: %s",
1256                                 b->a_group_oc->soc_oclass.oc_oid );
1257
1258                         if ( b->a_group_at ) {
1259                                 fprintf( stderr, " attributeType: %s", b->a_group_at->ad_cname.bv_val );
1260                         }
1261                 }
1262     }
1263
1264         if ( b->a_peername_pat != NULL ) {
1265                 fprintf( stderr, " peername=%s", b->a_peername_pat );
1266         }
1267
1268         if ( b->a_sockname_pat != NULL ) {
1269                 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
1270         }
1271
1272         if ( b->a_domain_pat != NULL ) {
1273                 fprintf( stderr, " domain=%s", b->a_domain_pat );
1274         }
1275
1276         if ( b->a_sockurl_pat != NULL ) {
1277                 fprintf( stderr, " sockurl=%s", b->a_sockurl_pat );
1278         }
1279
1280 #ifdef SLAPD_ACI_ENABLED
1281         if ( b->a_aci_at != NULL ) {
1282                 fprintf( stderr, " aci=%s", b->a_aci_at->ad_cname.bv_val );
1283         }
1284 #endif
1285
1286         /* Security Strength Factors */
1287         if ( b->a_authz.sai_ssf ) {
1288                 fprintf( stderr, " ssf=%u",
1289                         b->a_authz.sai_ssf );
1290         }
1291         if ( b->a_authz.sai_transport_ssf ) {
1292                 fprintf( stderr, " transport_ssf=%u",
1293                         b->a_authz.sai_transport_ssf );
1294         }
1295         if ( b->a_authz.sai_tls_ssf ) {
1296                 fprintf( stderr, " tls_ssf=%u",
1297                         b->a_authz.sai_tls_ssf );
1298         }
1299         if ( b->a_authz.sai_sasl_ssf ) {
1300                 fprintf( stderr, " sasl_ssf=%u",
1301                         b->a_authz.sai_sasl_ssf );
1302         }
1303
1304         fprintf( stderr, " %s%s",
1305                 b->a_dn_self ? "self" : "",
1306                 accessmask2str( b->a_access_mask, maskbuf ) );
1307
1308         if( b->a_type == ACL_BREAK ) {
1309                 fprintf( stderr, " break" );
1310
1311         } else if( b->a_type == ACL_CONTINUE ) {
1312                 fprintf( stderr, " continue" );
1313
1314         } else if( b->a_type != ACL_STOP ) {
1315                 fprintf( stderr, " unknown-control" );
1316         }
1317
1318         fprintf( stderr, "\n" );
1319 }
1320
1321
1322 static void
1323 print_acl( Backend *be, AccessControl *a )
1324 {
1325         int             to = 0;
1326         Access  *b;
1327
1328         fprintf( stderr, "%s ACL: access to",
1329                 be == NULL ? "Global" : "Backend" );
1330
1331         if ( a->acl_dn_pat != NULL ) {
1332                 to++;
1333                 fprintf( stderr, " dn.%s=%s\n",
1334                         style_strings[a->acl_dn_style], a->acl_dn_pat );
1335         }
1336
1337         if ( a->acl_filter != NULL ) {
1338                 to++;
1339                 fprintf( stderr, " filter=" );
1340                 filter_print( a->acl_filter );
1341                 fprintf( stderr, "\n" );
1342         }
1343
1344         if ( a->acl_attrs != NULL ) {
1345                 int     i, first = 1;
1346                 to++;
1347
1348                 fprintf( stderr, " attrs=" );
1349                 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
1350                         if ( ! first ) {
1351                                 fprintf( stderr, "," );
1352                         }
1353                         fprintf( stderr, a->acl_attrs[i] );
1354                         first = 0;
1355                 }
1356                 fprintf(  stderr, "\n" );
1357         }
1358
1359         if( !to ) {
1360                 fprintf( stderr, " *\n" );
1361         }
1362
1363         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
1364                 print_access( b );
1365         }
1366
1367         fprintf( stderr, "\n" );
1368 }
1369
1370 #endif /* LDAP_DEBUG */