]> git.sur5r.net Git - openldap/blob - servers/slapd/aclparse.c
65d0450317980efcab82f4b89e44336de339b599
[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(struct acl **l, struct acl *a);
17 static void             access_append(struct access **l, struct access *a);
18 static void             acl_usage(void);
19 #ifdef LDAP_DEBUG
20 static void             print_acl(struct acl *a);
21 static void             print_access(struct 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         struct acl      *a;
92         struct 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 = (struct acl *) ch_calloc( 1, sizeof(struct acl) );
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                                         int e;
113                                         if ((e = regcomp( &a->acl_dnre, ".*",
114                                                 REG_EXTENDED|REG_ICASE)))
115                                         {
116                                                 char buf[512];
117                                                 regerror(e, &a->acl_dnre, buf, sizeof(buf));
118                                                 fprintf( stderr,
119                                                         "%s: line %d: regular expression \"%s\" bad because of %s\n",
120                                                         fname, lineno, right, buf );
121                                                 acl_usage();
122                                         }
123                                         a->acl_dnpat = ch_strdup( ".*" );
124                                         continue;
125                                 }
126
127                                 split( argv[i], '=', &left, &right );
128                                 if ( right == NULL || *right == '\0' ) {
129                                         fprintf( stderr,
130         "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
131                                             fname, lineno, left );
132                                         acl_usage();
133                                 }
134
135                                 if ( strcasecmp( left, "filter" ) == 0 ) {
136                                         if ( (a->acl_filter = str2filter(
137                                             right )) == NULL ) {
138                                                 fprintf( stderr,
139                                 "%s: line %d: bad filter \"%s\" in to clause\n",
140                                                     fname, lineno, right );
141                                                 acl_usage();
142                                         }
143                                 } else if ( strcasecmp( left, "dn" ) == 0 ) {
144                                         int e;
145                                         if ((e = regcomp(&a->acl_dnre, right,
146                                                 REG_EXTENDED|REG_ICASE))) {
147                                                 char buf[512];
148                                                 regerror(e, &a->acl_dnre, buf, sizeof(buf));
149                                                 fprintf( stderr,
150                                 "%s: line %d: regular expression \"%s\" bad because of %s\n",
151                                                         fname, lineno, right, buf );
152                                                 acl_usage();
153
154                                         } else {
155                                                 a->acl_dnpat = dn_upcase(ch_strdup( right ));
156                                         }
157                                 } else if ( strncasecmp( left, "attr", 4 )
158                                     == 0 ) {
159                                         char    **alist;
160
161                                         alist = str2charray( right, "," );
162                                         charray_merge( &a->acl_attrs, alist );
163                                         charray_free( alist );
164                                 } else {
165                                         fprintf( stderr,
166                                                 "%s: line %d: expecting <what> got \"%s\"\n",
167                                             fname, lineno, left );
168                                         acl_usage();
169                                 }
170                         }
171
172                 /* by clause - select who has what access to entries */
173                 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
174                         if ( a == NULL ) {
175                                 fprintf( stderr,
176                                         "%s: line %d: to clause required before by clause in access line\n",
177                                     fname, lineno );
178                                 acl_usage();
179                         }
180                         /*
181                          * by clause consists of <who> and <access>
182                          */
183
184                         b = (struct access *) ch_calloc( 1, sizeof(struct access) );
185
186                         if ( ++i == argc ) {
187                                 fprintf( stderr,
188                             "%s: line %d: premature eol: expecting <who>\n",
189                                     fname, lineno );
190                                 acl_usage();
191                         }
192
193                         /* get <who> */
194                         split( argv[i], '=', &left, &right );
195                         if ( strcasecmp( argv[i], "*" ) == 0 ) {
196                                 b->a_dnpat = ch_strdup( ".*" );
197                         } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
198                                 b->a_dnpat = ch_strdup( "self" );
199                         } else if ( strcasecmp( left, "dn" ) == 0 ) {
200                                 regtest(fname, lineno, right);
201                                 b->a_dnpat = dn_upcase( ch_strdup( right ) );
202                         } else if ( strcasecmp( left, "dnattr" ) == 0 ) {
203                                 b->a_dnattr = ch_strdup( right );
204
205 #ifdef SLAPD_ACLGROUPS
206                         } else if ( strncasecmp( left, "group", sizeof("group")-1 ) == 0 ) {
207                                 char *name = NULL;
208                                 char *value = NULL;
209
210                                 /* format of string is "group/objectClassValue/groupAttrName"
211                                  */
212                                 if ((value = strchr(left, '/')) != NULL) {
213                                         *value++ = '\0';
214                                         if (value && *value && (name = strchr(value, '/')) != NULL) 
215                                             *name++ = '\0';
216                                 }
217
218                                 regtest(fname, lineno, right);
219                                 b->a_group = dn_upcase(ch_strdup( right ));
220
221                                 if (value && *value) {
222                                         b->a_group_oc = ch_strdup(value);
223                                         *--value = '/';
224                                 }
225                                 else
226                                         b->a_group_oc = ch_strdup("groupOfNames");
227
228                                 if (name && *name) {
229                                         b->a_group_at = ch_strdup(name);
230                                         *--name = '/';
231                                 }
232                                 else
233                                         b->a_group_at = ch_strdup("member");
234
235
236
237 #endif /* SLAPD_ACLGROUPS */
238                         } else if ( strcasecmp( left, "domain" ) == 0 ) {
239                                 char    *s;
240                                 regtest(fname, lineno, right);
241                                 b->a_domainpat = ch_strdup( right );
242
243                                 /* normalize the domain */
244                                 for ( s = b->a_domainpat; *s; s++ ) {
245                                         *s = TOLOWER( (unsigned char) *s );
246                                 }
247                         } else if ( strcasecmp( left, "addr" ) == 0 ) {
248                                 regtest(fname, lineno, right);
249                                 b->a_addrpat = ch_strdup( right );
250                         } else {
251                                 fprintf( stderr,
252                                     "%s: line %d: expecting <who> got \"%s\"\n",
253                                     fname, lineno, left );
254                                 acl_usage();
255                         }
256
257                         if ( ++i == argc ) {
258                                 fprintf( stderr,
259                             "%s: line %d: premature eol: expecting <access>\n",
260                                     fname, lineno );
261                                 acl_usage();
262                         }
263
264                         /* get <access> */
265                         split( argv[i], '=', &left, &right );
266                         if ( ACL_IS_INVALID(ACL_SET(str2access( left ),b->a_access)) ) {
267                                 fprintf( stderr,
268                             "%s: line %d: expecting <access> got \"%s\"\n",
269                                     fname, lineno, left );
270                                 acl_usage();
271                         }
272                         access_append( &a->acl_access, b );
273
274                 } else {
275                         fprintf( stderr,
276                     "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
277                             fname, lineno, argv[i] );
278                         acl_usage();
279                 }
280         }
281
282         /* if we have no real access clause, complain and do nothing */
283         if ( a == NULL ) {
284                         fprintf( stderr,
285                                 "%s: line %d: warning: no access clause(s) specified in access line\n",
286                             fname, lineno );
287
288         } else {
289
290 #ifdef LDAP_DEBUG
291                 if (ldap_debug & LDAP_DEBUG_ACL)
292                     print_acl(a);
293 #endif
294         
295                 if ( a->acl_access == NULL ) {
296                         fprintf( stderr,
297                         "%s: line %d: warning: no by clause(s) specified in access line\n",
298                             fname, lineno );
299                 }
300
301                 if ( be != NULL ) {
302                         acl_append( &be->be_acl, a );
303                 } else {
304                         acl_append( &global_acl, a );
305                 }
306         }
307 }
308
309 char *
310 access2str( int access )
311 {
312         static char     buf[12];
313
314         if ( ACL_IS_SELF( access ) ) {
315                 strcpy( buf, "self" );
316         } else {
317                 buf[0] = '\0';
318         }
319
320         if ( ACL_IS_NONE(access) ) {
321                 strcat( buf, "none" );
322 #ifdef SLAPD_ACLAUTH
323         } else if ( ACL_IS_AUTH(access) ) {
324                 strcat( buf, "auth" );
325 #endif
326         } else if ( ACL_IS_COMPARE(access) ) {
327                 strcat( buf, "compare" );
328         } else if ( ACL_IS_SEARCH(access) ) {
329                 strcat( buf, "search" );
330         } else if ( ACL_IS_READ(access) ) {
331                 strcat( buf, "read" );
332         } else if ( ACL_IS_WRITE(access) ) {
333                 strcat( buf, "write" );
334         } else {
335                 strcat( buf, "unknown" );
336         }
337
338         return( buf );
339 }
340
341 int
342 str2access( char *str )
343 {
344         int     access;
345
346         ACL_CLR(access);
347
348         if ( strncasecmp( str, "self", 4 ) == 0 ) {
349                 ACL_SET_SELF(access);
350                 str += 4;
351         }
352
353         if ( strcasecmp( str, "none" ) == 0 ) {
354                 ACL_SET_NONE(access);
355 #ifdef SLAPD_ACLAUTH
356         } else if ( strcasecmp( str, "auth" ) == 0 ) {
357                 ACL_SET_AUTH(access);
358 #endif
359         } else if ( strcasecmp( str, "compare" ) == 0 ) {
360                 ACL_SET_COMPARE(access);
361         } else if ( strcasecmp( str, "search" ) == 0 ) {
362                 ACL_SET_SEARCH(access);
363         } else if ( strcasecmp( str, "read" ) == 0 ) {
364                 ACL_SET_READ(access);
365         } else if ( strcasecmp( str, "write" ) == 0 ) {
366                 ACL_SET_WRITE(access);
367         } else {
368                 ACL_SET_INVALID(access);
369         }
370
371         return( access );
372 }
373
374 static void
375 acl_usage( void )
376 {
377         fprintf( stderr, "\n"
378                 "<access clause> ::= access to <what> [ by <who> <access> ]+ \n"
379                 "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n"
380                 "<attrlist> ::= <attr> | <attr> , <attrlist>\n"
381                 "<attr> ::= <attrname> | entry | children\n"
382                 "<who> ::= * | self | dn=<regex> | addr=<regex>\n"
383                         "\t| domain=<regex> | dnattr=<dnattrname>\n"
384 #ifdef SLAPD_ACLGROUPS
385                         "\t| group[/<objectclass>[/<attrname>]]=<regex>\n"
386 #endif
387 #ifdef SLAPD_ACLAUTH
388                 "<access> ::= [self]{none|auth|compare|search|read|write}\n"
389 #else
390                 "<access> ::= [self]{none|auth|compare|search|read|write}\n"
391 #endif
392                 );
393         exit( 1 );
394 }
395
396 static void
397 split(
398     char        *line,
399     int         splitchar,
400     char        **left,
401     char        **right
402 )
403 {
404         *left = line;
405         if ( (*right = strchr( line, splitchar )) != NULL ) {
406                 *((*right)++) = '\0';
407         }
408 }
409
410 static void
411 access_append( struct access **l, struct access *a )
412 {
413         for ( ; *l != NULL; l = &(*l)->a_next )
414                 ;       /* NULL */
415
416         *l = a;
417 }
418
419 static void
420 acl_append( struct acl **l, struct acl *a )
421 {
422         for ( ; *l != NULL; l = &(*l)->acl_next )
423                 ;       /* NULL */
424
425         *l = a;
426 }
427
428 #ifdef LDAP_DEBUG
429
430 static void
431 print_access( struct access *b )
432 {
433         fprintf( stderr, "\tby" );
434
435         if ( b->a_dnpat != NULL ) {
436                 fprintf( stderr, " dn=%s", b->a_dnpat );
437         } else if ( b->a_addrpat != NULL ) {
438                 fprintf( stderr, " addr=%s", b->a_addrpat );
439         } else if ( b->a_domainpat != NULL ) {
440                 fprintf( stderr, " domain=%s", b->a_domainpat );
441         } else if ( b->a_dnattr != NULL ) {
442                 fprintf( stderr, " dnattr=%s", b->a_dnattr );
443         }
444 #ifdef SLAPD_ACLGROUPS
445         else if ( b->a_group != NULL ) {
446                 fprintf( stderr, " group: %s", b->a_group );
447                 if ( b->a_group_oc )
448                         fprintf( stderr, " objectClass: %s", b->a_group_oc );
449                 if ( b->a_group_at )
450                         fprintf( stderr, " attributeType: %s", b->a_group_at );
451         }
452 #endif
453         fprintf( stderr, "\n" );
454 }
455
456 static void
457 print_acl( struct acl *a )
458 {
459         int             i;
460         struct access   *b;
461
462         if ( a == NULL ) {
463                 fprintf( stderr, "NULL\n" );
464         }
465         fprintf( stderr, "ACL: access to" );
466         if ( a->acl_filter != NULL ) {
467                 fprintf(  stderr," filter=" );
468                 filter_print( a->acl_filter );
469         }
470         if ( a->acl_dnpat != NULL ) {
471                 fprintf( stderr, " dn=" );
472                 fprintf( stderr, a->acl_dnpat );
473         }
474         if ( a->acl_attrs != NULL ) {
475                 int     first = 1;
476
477                 fprintf( stderr, "\n attrs=" );
478                 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
479                         if ( ! first ) {
480                                 fprintf( stderr, "," );
481                         }
482                         fprintf( stderr, a->acl_attrs[i] );
483                         first = 0;
484                 }
485         }
486         fprintf( stderr, "\n" );
487         for ( b = a->acl_access; b != NULL; b = b->a_next ) {
488                 print_access( b );
489         }
490         fprintf( stderr, "\n" );
491 }
492
493 #endif /* LDAP_DEBUG */