]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
0aaf463d1f0a34de96574a955e55270ffe9e45a7
[openldap] / servers / slapd / aclparse.c
1 /* acl.c - routines to parse and check acl's */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/ctype.h>
8 #include <ac/regex.h>
9 #include <ac/socket.h>
10 #include <ac/string.h>
11 #include <ac/unistd.h>
12
13 #include "slap.h"
14
15 static void             split(char *line, int splitchar, char **left, char **right);
16 static void             acl_append(AccessControl **l, AccessControl *a);
17 static void             access_append(Access **l, Access *a);
18 static void             acl_usage(void);
19 #ifdef LDAP_DEBUG
20 static void             print_acl(AccessControl *a);
21 static void             print_access(Access *b);
22 #endif
23
24 static int
25 regtest(char *fname, int lineno, char *pat) {
26         int e;
27         regex_t re;
28
29         char buf[512];
30         unsigned size;
31
32         char *sp;
33         char *dp;
34         int  flag;
35
36         sp = pat;
37         dp = buf;
38         size = 0;
39         buf[0] = '\0';
40
41         for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
42                 if (flag) {
43                         if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
44                                 *dp++ = *sp;
45                                 size++;
46                         }
47                         flag = 0;
48
49                 } else {
50                         if (*sp == '$') {
51                                 flag = 1;
52                         } else {
53                                 *dp++ = *sp;
54                                 size++;
55                         }
56                 }
57         }
58
59         *dp = '\0';
60         if ( size >= (sizeof(buf)-1) ) {
61                 fprintf( stderr,
62                         "%s: line %d: regular expression \"%s\" too large\n",
63                         fname, lineno, pat, 0 );
64                 acl_usage();
65         }
66
67         if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
68                 char error[512];
69                 regerror(e, &re, error, sizeof(error));
70                 fprintf( stderr,
71                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
72                         fname, lineno, pat, error );
73                 acl_usage();
74                 return(0);
75         }
76         regfree(&re);
77         return(1);
78 }
79
80 void
81 parse_acl(
82     Backend     *be,
83     char        *fname,
84     int         lineno,
85     int         argc,
86     char        **argv
87 )
88 {
89         int             i;
90         char            *left, *right;
91         AccessControl   *a;
92         Access  *b;
93
94         a = NULL;
95         for ( i = 1; i < argc; i++ ) {
96                 /* to clause - select which entries are protected */
97                 if ( strcasecmp( argv[i], "to" ) == 0 ) {
98                         if ( a != NULL ) {
99                                 fprintf( stderr,
100                 "%s: line %d: only one to clause allowed in access line\n",
101                                     fname, lineno );
102                                 acl_usage();
103                         }
104                         a = (AccessControl *) ch_calloc( 1, sizeof(AccessControl) );
105                         for ( ++i; i < argc; i++ ) {
106                                 if ( strcasecmp( argv[i], "by" ) == 0 ) {
107                                         i--;
108                                         break;
109                                 }
110
111                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
112                                         a->acl_dn_pat = ch_strdup( ".*" );
113                                         continue;
114                                 }
115
116                                 split( argv[i], '=', &left, &right );
117                                 if ( right == NULL || *right == '\0' ) {
118                                         fprintf( stderr,
119         "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
120                                             fname, lineno, left );
121                                         acl_usage();
122                                 }
123
124                                 if ( strcasecmp( left, "filter" ) == 0 ) {
125                                         if ( (a->acl_filter = str2filter(
126                                             right )) == NULL ) {
127                                                 fprintf( stderr,
128                                 "%s: line %d: bad filter \"%s\" in to clause\n",
129                                                     fname, lineno, right );
130                                                 acl_usage();
131                                         }
132
133                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
134                                         int e;
135
136                                         if ((e = regcomp(&a->acl_dn_re, right,
137                                                 REG_EXTENDED|REG_ICASE))) {
138                                                 char buf[512];
139                                                 regerror(e, &a->acl_dn_re, buf, sizeof(buf));
140                                                 fprintf( stderr,
141                                 "%s: line %d: regular expression \"%s\" bad because of %s\n",
142                                                         fname, lineno, right, buf );
143                                                 acl_usage();
144
145                                         } else {
146                                                 a->acl_dn_pat = ch_strdup( right );
147                                         }
148
149                                 } else if ( strncasecmp( left, "attr", 4 ) == 0 ) {
150                                         char    **alist;
151
152                                         alist = str2charray( right, "," );
153                                         charray_merge( &a->acl_attrs, alist );
154                                         charray_free( alist );
155
156                                 } else {
157                                         fprintf( stderr,
158                                                 "%s: line %d: expecting <what> got \"%s\"\n",
159                                             fname, lineno, left );
160                                         acl_usage();
161                                 }
162                         }
163
164                 /* by clause - select who has what access to entries */
165                 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
166                         if ( a == NULL ) {
167                                 fprintf( stderr,
168                                         "%s: line %d: to clause required before by clause in access line\n",
169                                     fname, lineno );
170                                 acl_usage();
171                         }
172                         /*
173                          * by clause consists of <who> and <access>
174                          */
175
176                         b = (Access *) ch_calloc( 1, sizeof(Access) );
177
178                         if ( ++i == argc ) {
179                                 fprintf( stderr,
180                             "%s: line %d: premature eol: expecting <who>\n",
181                                     fname, lineno );
182                                 acl_usage();
183                         }
184
185                         /* get <who> */
186                         for ( ; i < argc; i++ ) {
187                                 char* pat;
188                                 split( argv[i], '=', &left, &right );
189
190                                 if ( strcasecmp( argv[i], "*" ) == 0 ) {
191                                         pat = ch_strdup( ".*" );
192                                 } else if ( strcasecmp( argv[i], "anonymous" ) == 0 ) {
193                                         pat = ch_strdup( "anonymous" );
194                                 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
195                                         pat = ch_strdup( "self" );
196                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
197                                         regtest(fname, lineno, right);
198                                         pat = ch_strdup( right );
199                                 }
200
201                                 if( pat != NULL ) {
202                                         if( b->a_dn_pat != NULL ) {
203                                                 fprintf( stderr,
204                                                     "%s: line %d: dn pattern already specified.\n",
205                                                     fname, lineno );
206                                                 acl_usage();
207                                         }
208
209                                         b->a_dn_pat = pat;
210                                         continue;
211                                 }
212
213                                 if ( strcasecmp( left, "dnattr" ) == 0 ) {
214                                         if( b->a_dn_pat != NULL ) {
215                                                 fprintf( stderr,
216                                                         "%s: line %d: dnaddr already specified.\n",
217                                                         fname, lineno );
218                                                 acl_usage();
219                                         }
220
221                                         b->a_dn_at = ch_strdup( right );
222                                         continue;
223                                 }
224
225                                 if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
226                                         char *name = NULL;
227                                         char *value = NULL;
228
229                                         if( b->a_group_pat != NULL ) {
230                                                 fprintf( stderr,
231                                                         "%s: line %d: group pattern already specified.\n",
232                                                         fname, lineno );
233                                                 acl_usage();
234                                         }
235
236                                         /* format of string is "group/objectClassValue/groupAttrName" */
237                                         if ((value = strchr(left, '/')) != NULL) {
238                                                 *value++ = '\0';
239                                                 if (value && *value
240                                                         && (name = strchr(value, '/')) != NULL)
241                                                 {
242                                                         *name++ = '\0';
243                                                 }
244                                         }
245
246                                         regtest(fname, lineno, right);
247                                         b->a_group_pat = ch_strdup( right );
248
249                                         if (value && *value) {
250                                                 b->a_group_oc = ch_strdup(value);
251                                                 *--value = '/';
252                                         } else {
253                                                 b->a_group_oc = ch_strdup("groupOfNames");
254
255                                                 if (name && *name) {
256                                                         b->a_group_at = ch_strdup(name);
257                                                         *--name = '/';
258
259                                                 } else {
260                                                         b->a_group_at = ch_strdup("member");
261                                                 }
262                                         }
263                                         continue;
264                                 }
265
266                                 if ( strcasecmp( left, "peername" ) == 0 ) {
267                                         if( b->a_peername_pat != NULL ) {
268                                                 fprintf( stderr,
269                                                         "%s: line %d: peername pattern already specified.\n",
270                                                         fname, lineno );
271                                                 acl_usage();
272                                         }
273
274                                         regtest(fname, lineno, right);
275                                         b->a_peername_pat = ch_strdup( right );
276                                         continue;
277                                 }
278
279                                 if ( strcasecmp( left, "sockname" ) == 0 ) {
280                                         if( b->a_sockname_pat != NULL ) {
281                                                 fprintf( stderr,
282                                                         "%s: line %d: sockname pattern already specified.\n",
283                                                         fname, lineno );
284                                                 acl_usage();
285                                         }
286
287                                         regtest(fname, lineno, right);
288                                         b->a_sockname_pat = ch_strdup( right );
289                                         continue;
290                                 }
291
292                                 if ( strcasecmp( left, "domain" ) == 0 ) {
293                                         if( b->a_domain_pat != NULL ) {
294                                                 fprintf( stderr,
295                                                         "%s: line %d: domain pattern already specified.\n",
296                                                         fname, lineno );
297                                                 acl_usage();
298                                         }
299
300                                         regtest(fname, lineno, right);
301                                         b->a_domain_pat = ch_strdup( right );
302                                         continue;
303                                 }
304
305                                 if ( strcasecmp( left, "url" ) == 0 ) {
306                                         if( b->a_url_pat != NULL ) {
307                                                 fprintf( stderr,
308                                                         "%s: line %d: url pattern already specified.\n",
309                                                         fname, lineno );
310                                                 acl_usage();
311                                         }
312
313                                         regtest(fname, lineno, right);
314                                         b->a_url_pat = ch_strdup( right );
315                                         continue;
316                                 }
317
318                                 /* get <access> */
319                                 if ( ACL_IS_INVALID(ACL_SET(b->a_access, str2access( left ))) ) {
320                                         fprintf( stderr,
321                                         "%s: line %d: expecting <access> got \"%s\"\n",
322                                                 fname, lineno, left );
323                                         acl_usage();
324                                 }
325                                 access_append( &a->acl_access, b );
326                                 break;
327                         }
328                 } else {
329                         fprintf( stderr,
330                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
331                             fname, lineno, argv[i] );
332                         acl_usage();
333                 }
334         }
335
336         /* if we have no real access clause, complain and do nothing */
337         if ( a == NULL ) {
338                         fprintf( stderr,
339                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
340                             fname, lineno );
341
342         } else {
343
344 #ifdef LDAP_DEBUG
345                 if (ldap_debug & LDAP_DEBUG_ACL)
346                     print_acl(a);
347 #endif
348         
349                 if ( a->acl_access == NULL ) {
350                         fprintf( stderr,
351                         "%s: line %d: warning: no by clause(s) specified in access line\n",
352                             fname, lineno );
353                 }
354
355                 if ( be != NULL ) {
356                         acl_append( &be->be_acl, a );
357                 } else {
358                         acl_append( &global_acl, a );
359                 }
360         }
361 }
362
363 char *
364 access2str( int access )
365 {
366         static char     buf[12];
367
368         if ( ACL_IS_SELF( access ) ) {
369                 strcpy( buf, "self" );
370         } else {
371                 buf[0] = '\0';
372         }
373
374         if ( ACL_IS_NONE(access) ) {
375                 strcat( buf, "none" );
376         } else if ( ACL_IS_AUTH(access) ) {
377                 strcat( buf, "auth" );
378         } else if ( ACL_IS_COMPARE(access) ) {
379                 strcat( buf, "compare" );
380         } else if ( ACL_IS_SEARCH(access) ) {
381                 strcat( buf, "search" );
382         } else if ( ACL_IS_READ(access) ) {
383                 strcat( buf, "read" );
384         } else if ( ACL_IS_WRITE(access) ) {
385                 strcat( buf, "write" );
386
387         } else {
388                 strcat( buf, "unknown" );
389         }
390
391         return( buf );
392 }
393
394 int
395 str2access( char *str )
396 {
397         int     access;
398
399         ACL_CLR(access);
400
401         if ( strncasecmp( str, "self", 4 ) == 0 ) {
402                 ACL_SET_SELF(access);
403                 str += 4;
404         }
405
406         if ( strcasecmp( str, "none" ) == 0 ) {
407                 ACL_SET_NONE(access);
408         } else if ( strcasecmp( str, "auth" ) == 0 ) {
409                 ACL_SET_AUTH(access);
410         } else if ( strcasecmp( str, "compare" ) == 0 ) {
411                 ACL_SET_COMPARE(access);
412         } else if ( strcasecmp( str, "search" ) == 0 ) {
413                 ACL_SET_SEARCH(access);
414         } else if ( strcasecmp( str, "read" ) == 0 ) {
415                 ACL_SET_READ(access);
416         } else if ( strcasecmp( str, "write" ) == 0 ) {
417                 ACL_SET_WRITE(access);
418         } else {
419                 ACL_SET_INVALID(access);
420         }
421
422         return( access );
423 }
424
425 static void
426 acl_usage( void )
427 {
428         fprintf( stderr, "\n"
429                 "<access clause> ::= access to <what> [ by <who> <access> ]+ \n"
430                 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
431                 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
432                 "<attr> ::= <attrname> | entry | children\n"
433                 "<who> ::= * | anonymous | self | dn=<regex>\n"
434                         "\t| dnattr=<attrname> | group[/<objectclass>[/<attrname>]]=<regex>\n"
435                         "\t| peername=<regex> | sockname=<regex>\n"
436                         "\t| domain=<regex> | sockurl=<regex>\n"
437                 "<access> ::= [self]{none|auth|compare|search|read|write}\n"
438                 );
439         exit( 1 );
440 }
441
442 static void
443 split(
444     char        *line,
445     int         splitchar,
446     char        **left,
447     char        **right
448 )
449 {
450         *left = line;
451         if ( (*right = strchr( line, splitchar )) != NULL ) {
452                 *((*right)++) = '\0';
453         }
454 }
455
456 static void
457 access_append( Access **l, Access *a )
458 {
459         for ( ; *l != NULL; l = &(*l)->a_next )
460                 ;       /* NULL */
461
462         *l = a;
463 }
464
465 static void
466 acl_append( AccessControl **l, AccessControl *a )
467 {
468         for ( ; *l != NULL; l = &(*l)->acl_next )
469                 ;       /* NULL */
470
471         *l = a;
472 }
473
474 #ifdef LDAP_DEBUG
475
476 static void
477 print_access( Access *b )
478 {
479         fprintf( stderr, "\tby" );
480
481         if ( b->a_dn_pat != NULL ) {
482                 if( strcmp(b->a_dn_pat, "anonymous") == 0 ) {
483                         fprintf( stderr, " anonymous" );
484
485                 } else if( strcmp(b->a_dn_pat, "self") == 0 ) {
486                         fprintf( stderr, " self" );
487
488                 } else {
489                         fprintf( stderr, " dn=%s", b->a_dn_pat );
490                 }
491         }
492
493         if ( b->a_dn_at != NULL ) {
494                 fprintf( stderr, " dnattr=%s", b->a_dn_at );
495         }
496
497         if ( b->a_group_pat != NULL ) {
498                 fprintf( stderr, " group: %s", b->a_group_pat );
499
500                 if ( b->a_group_oc ) {
501                         fprintf( stderr, " objectClass: %s", b->a_group_oc );
502
503                         if ( b->a_group_at ) {
504                                 fprintf( stderr, " attributeType: %s", b->a_group_at );
505                         }
506                 }
507     }
508
509         if ( b->a_peername_pat != NULL ) {
510                 fprintf( stderr, " peername=%s", b->a_peername_pat );
511         }
512         if ( b->a_sockname_pat != NULL ) {
513                 fprintf( stderr, " sockname=%s", b->a_sockname_pat );
514         }
515
516         if ( b->a_domain_pat != NULL ) {
517                 fprintf( stderr, " domain=%s", b->a_domain_pat );
518         }
519
520         if ( b->a_url_pat != NULL ) {
521                 fprintf( stderr, " url=%s", b->a_url_pat );
522         }
523
524         fprintf( stderr, "\n" );
525 }
526
527 static void
528 print_acl( AccessControl *a )
529 {
530         int             i;
531         Access  *b;
532
533         if ( a == NULL ) {
534                 fprintf( stderr, "NULL\n" );
535         }
536         fprintf( stderr, "ACL: access to" );
537         if ( a->acl_filter != NULL ) {
538                 fprintf(  stderr," filter=" );
539                 filter_print( a->acl_filter );
540         }
541         if ( a->acl_dn_pat != NULL ) {
542                 fprintf( stderr, " dn=" );
543                 fprintf( stderr, a->acl_dn_pat );
544         }
545         if ( a->acl_attrs != NULL ) {
546                 int     first = 1;
547
548                 fprintf( stderr, "\n attrs=" );
549                 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
550                         if ( ! first ) {
551                                 fprintf( stderr, "," );
552                         }
553                         fprintf( stderr, a->acl_attrs[i] );
554                         first = 0;
555                 }
556         }
557         fprintf( stderr, "\n" );
558         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
559                 print_access( b );
560         }
561         fprintf( stderr, "\n" );
562 }
563
564 #endif /* LDAP_DEBUG */