]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
5b4f409d9b17b3a9f3c085b2bd2813d809f39eed
[openldap] / servers / slapd / config.c
1 /* config.c - configuration file handling routines */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/ctype.h>
9 #include <ac/socket.h>
10
11 #include "ldapconfig.h"
12 #include "slap.h"
13
14 #define MAXARGS 100
15
16 /*
17  * defaults for various global variables
18  */
19 int             defsize = SLAPD_DEFAULT_SIZELIMIT;
20 int             deftime = SLAPD_DEFAULT_TIMELIMIT;
21 struct acl      *global_acl = NULL;
22 int             global_default_access = ACL_READ;
23 char            *replogfile;
24 int             global_lastmod;
25 char            *ldap_srvtab = "";
26
27 static char     *fp_getline(FILE *fp, int *lineno);
28 static void     fp_getline_init(int *lineno);
29 static void     fp_parse_line(char *line, int *argcp, char **argv);
30
31 static char     *strtok_quote(char *line, char *sep);
32
33 void
34 read_config( char *fname, Backend **bep, FILE *pfp )
35 {
36         FILE    *fp;
37         char    *line, *savefname;
38         int     cargc, savelineno;
39         char    *cargv[MAXARGS];
40         int     lineno, i;
41         Backend *be;
42
43         if ( (fp = pfp) == NULL && (fp = fopen( fname, "r" )) == NULL ) {
44                 ldap_syslog = 1;
45                 Debug( LDAP_DEBUG_ANY,
46                     "could not open config file \"%s\" - absolute path?\n",
47                     fname, 0, 0 );
48                 perror( fname );
49                 exit( 1 );
50         }
51
52         Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
53         be = *bep;
54         fp_getline_init( &lineno );
55         while ( (line = fp_getline( fp, &lineno )) != NULL ) {
56                 /* skip comments and blank lines */
57                 if ( line[0] == '#' || line[0] == '\0' ) {
58                         continue;
59                 }
60
61                 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
62
63                 fp_parse_line( line, &cargc, cargv );
64
65                 if ( cargc < 1 ) {
66                         Debug( LDAP_DEBUG_ANY,
67                             "%s: line %d: bad config line (ignored)\n",
68                             fname, lineno, 0 );
69                         continue;
70                 }
71
72                 /* start of a new database definition */
73                 if ( strcasecmp( cargv[0], "database" ) == 0 ) {
74                         if ( cargc < 2 ) {
75                                 Debug( LDAP_DEBUG_ANY,
76                 "%s: line %d: missing type in \"database <type>\" line\n",
77                                     fname, lineno, 0 );
78                                 exit( 1 );
79                         }
80                         *bep = new_backend( cargv[1] );
81                         be = *bep;
82
83                 /* assign a default depth limit for alias deref */
84                 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
85
86                 /* set size limit */
87                 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
88                         if ( cargc < 2 ) {
89                                 Debug( LDAP_DEBUG_ANY,
90             "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
91                                     fname, lineno, 0 );
92                                 exit( 1 );
93                         }
94                         if ( be == NULL ) {
95                                 defsize = atoi( cargv[1] );
96                         } else {
97                                 be->be_sizelimit = atoi( cargv[1] );
98                         }
99
100                 /* set time limit */
101                 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
102                         if ( cargc < 2 ) {
103                                 Debug( LDAP_DEBUG_ANY,
104             "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
105                                     fname, lineno, 0 );
106                                 exit( 1 );
107                         }
108                         if ( be == NULL ) {
109                                 deftime = atoi( cargv[1] );
110                         } else {
111                                 be->be_timelimit = atoi( cargv[1] );
112                         }
113
114                 /* set database suffix */
115                 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
116                         if ( cargc < 2 ) {
117                                 Debug( LDAP_DEBUG_ANY,
118                     "%s: line %d: missing dn in \"suffix <dn>\" line\n",
119                                     fname, lineno, 0 );
120                                 exit( 1 );
121                         } else if ( cargc > 2 ) {
122                                 Debug( LDAP_DEBUG_ANY,
123     "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
124                                     fname, lineno, cargv[1] );
125                         }
126                         if ( be == NULL ) {
127                                 Debug( LDAP_DEBUG_ANY,
128 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
129                                     fname, lineno, 0 );
130                         } else {
131                                 char *dn = ch_strdup( cargv[1] );
132                                 (void) dn_normalize( dn );
133                                 charray_add( &be->be_suffix, dn );
134                         }
135
136                 /* set database suffixAlias */
137                 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
138                         if ( cargc < 2 ) {
139                                 Debug( LDAP_DEBUG_ANY,
140                     "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
141                                     fname, lineno, 0 );
142                                 exit( 1 );
143                         } else if ( cargc < 3 ) {
144                                 Debug( LDAP_DEBUG_ANY,
145                     "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
146                                     fname, lineno, 0 );
147                                 exit( 1 );
148                         } else if ( cargc > 3 ) {
149                                 Debug( LDAP_DEBUG_ANY,
150     "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
151                                     fname, lineno, 0 );
152                         }
153                         if ( be == NULL ) {
154                                 Debug( LDAP_DEBUG_ANY,
155 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
156                                     fname, lineno, 0 );
157                         } else {
158                                 char *dn = ch_strdup( cargv[1] );
159                                 (void) dn_normalize( dn );
160                                 charray_add( &be->be_suffixAlias, dn );
161
162                                 dn = ch_strdup( cargv[2] );
163                                 (void) dn_normalize( dn );
164                                 charray_add( &be->be_suffixAlias, dn );
165                         }
166
167                /* set max deref depth */
168                } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
169                        if ( cargc < 2 ) {
170                                Debug( LDAP_DEBUG_ANY,
171                    "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
172                                    fname, lineno, 0 );
173                                exit( 1 );
174                        }
175                        if ( be == NULL ) {
176                                Debug( LDAP_DEBUG_ANY,
177 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
178                                    fname, lineno, 0 );
179                        } else {
180                            be->be_maxDerefDepth = atoi (cargv[1]);
181                        }
182
183
184                 /* set magic "root" dn for this database */
185                 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
186                         if ( cargc < 2 ) {
187                                 Debug( LDAP_DEBUG_ANY,
188                     "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
189                                     fname, lineno, 0 );
190                                 exit( 1 );
191                         }
192                         if ( be == NULL ) {
193                                 Debug( LDAP_DEBUG_ANY,
194 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
195                                     fname, lineno, 0 );
196                         } else {
197                                 be->be_rootdn = dn_normalize_case( ch_strdup( cargv[1] ) );
198                         }
199
200                 /* set super-secret magic database password */
201                 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
202                         if ( cargc < 2 ) {
203                                 Debug( LDAP_DEBUG_ANY,
204             "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
205                                     fname, lineno, 0 );
206                                 exit( 1 );
207                         }
208                         if ( be == NULL ) {
209                                 Debug( LDAP_DEBUG_ANY,
210 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
211                                     fname, lineno, 0 );
212                         } else {
213                                 be->be_rootpw = ch_strdup( cargv[1] );
214                         }
215
216                 /* make this database read-only */
217                 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
218                         if ( cargc < 2 ) {
219                                 Debug( LDAP_DEBUG_ANY,
220             "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
221                                     fname, lineno, 0 );
222                                 exit( 1 );
223                         }
224                         if ( be == NULL ) {
225                                 Debug( LDAP_DEBUG_ANY,
226 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
227                                     fname, lineno, 0 );
228                         } else {
229                                 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
230                                         be->be_readonly = 1;
231                                 } else {
232                                         be->be_readonly = 0;
233                                 }
234                         }
235
236                 /* where to send clients when we don't hold it */
237                 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
238                         if ( cargc < 2 ) {
239                                 Debug( LDAP_DEBUG_ANY,
240                     "%s: line %d: missing URL in \"referral <URL>\" line\n",
241                                     fname, lineno, 0 );
242                                 exit( 1 );
243                         }
244                         default_referral = (char *) malloc( strlen( cargv[1] )
245                             + sizeof("Referral:\n") + 1 );
246                         strcpy( default_referral, "Referral:\n" );
247                         strcat( default_referral, cargv[1] );
248
249                 /* specify an objectclass */
250                 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
251                         parse_oc( be, fname, lineno, cargc, cargv );
252
253                 /* specify an attribute */
254                 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
255                         attr_syntax_config( fname, lineno, cargc - 1,
256                             &cargv[1] );
257
258                 /* turn on/off schema checking */
259                 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
260                         if ( cargc < 2 ) {
261                                 Debug( LDAP_DEBUG_ANY,
262     "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
263                                     fname, lineno, 0 );
264                                 exit( 1 );
265                         }
266                         if ( strcasecmp( cargv[1], "on" ) == 0 ) {
267                                 global_schemacheck = 1;
268                         } else {
269                                 global_schemacheck = 0;
270                         }
271
272                 /* specify access control info */
273                 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
274                         parse_acl( be, fname, lineno, cargc, cargv );
275
276                 /* specify default access control info */
277                 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
278                         if ( cargc < 2 ) {
279                                 Debug( LDAP_DEBUG_ANY,
280             "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
281                                     fname, lineno, 0 );
282                                 exit( 1 );
283                         }
284                         if ( be == NULL ) {
285                                 if ( (global_default_access =
286                                     str2access( cargv[1] )) == -1 ) {
287                                         Debug( LDAP_DEBUG_ANY,
288 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
289                                             fname, lineno, cargv[1] );
290                                         exit( 1 );
291                                 }
292                         } else {
293                                 if ( (be->be_dfltaccess =
294                                     str2access( cargv[1] )) == -1 ) {
295                                         Debug( LDAP_DEBUG_ANY,
296 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
297                                             fname, lineno, cargv[1] );
298                                         exit( 1 );
299                                 }
300                         }
301
302                 /* debug level to log things to syslog */
303                 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
304                         if ( cargc < 2 ) {
305                                 Debug( LDAP_DEBUG_ANY,
306                     "%s: line %d: missing level in \"loglevel <level>\" line\n",
307                                     fname, lineno, 0 );
308                                 exit( 1 );
309                         }
310                         ldap_syslog = atoi( cargv[1] );
311
312                 /* list of replicas of the data in this backend (master only) */
313                 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
314                         if ( cargc < 2 ) {
315                                 Debug( LDAP_DEBUG_ANY,
316             "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
317                                     fname, lineno, 0 );
318                                 exit( 1 );
319                         }
320                         if ( be == NULL ) {
321                                 Debug( LDAP_DEBUG_ANY,
322 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
323                                     fname, lineno, 0 );
324                         } else {
325                                 for ( i = 1; i < cargc; i++ ) {
326                                         if ( strncasecmp( cargv[i], "host=", 5 )
327                                             == 0 ) {
328                                                 charray_add( &be->be_replica,
329                                                     ch_strdup( cargv[i] + 5 ) );
330                                                 break;
331                                         }
332                                 }
333                                 if ( i == cargc ) {
334                                         Debug( LDAP_DEBUG_ANY,
335                     "%s: line %d: missing host in \"replica\" line (ignored)\n",
336                                             fname, lineno, 0 );
337                                 }
338                         }
339
340                 /* dn of master entity allowed to write to replica */
341                 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
342                         if ( cargc < 2 ) {
343                                 Debug( LDAP_DEBUG_ANY,
344                     "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
345                                     fname, lineno, 0 );
346                                 exit( 1 );
347                         }
348                         if ( be == NULL ) {
349                                 Debug( LDAP_DEBUG_ANY,
350 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
351                                     fname, lineno, 0 );
352                         } else {
353                                 be->be_updatedn = ch_strdup( cargv[1] );
354                                 (void) dn_normalize( be->be_updatedn );
355                         }
356
357                 /* replication log file to which changes are appended */
358                 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
359                         if ( cargc < 2 ) {
360                                 Debug( LDAP_DEBUG_ANY,
361             "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
362                                     fname, lineno, 0 );
363                                 exit( 1 );
364                         }
365                         if ( be ) {
366                                 be->be_replogfile = ch_strdup( cargv[1] );
367                         } else {
368                                 replogfile = ch_strdup( cargv[1] );
369                         }
370
371                 /* maintain lastmodified{by,time} attributes */
372                 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
373                         if ( cargc < 2 ) {
374                                 Debug( LDAP_DEBUG_ANY,
375             "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
376                                     fname, lineno, 0 );
377                                 exit( 1 );
378                         }
379                         if ( strcasecmp( cargv[1], "on" ) == 0 ) {
380                                 if ( be )
381                                         be->be_lastmod = ON;
382                                 else
383                                         global_lastmod = ON;
384                         } else {
385                                 if ( be )
386                                         be->be_lastmod = OFF;
387                                 else
388                                         global_lastmod = OFF;
389                         }
390
391                 /* include another config file */
392                 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
393                         if ( cargc < 2 ) {
394                                 Debug( LDAP_DEBUG_ANY,
395     "%s: line %d: missing filename in \"include <filename>\" line\n",
396                                     fname, lineno, 0 );
397                                 exit( 1 );
398                         }
399                         savefname = ch_strdup( cargv[1] );
400                         savelineno = lineno;
401                         read_config( savefname, bep, NULL );
402                         be = *bep;
403                         free( savefname );
404                         lineno = savelineno - 1;
405
406                 /* location of kerberos srvtab file */
407                 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
408                         if ( cargc < 2 ) {
409                                 Debug( LDAP_DEBUG_ANY,
410             "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
411                                     fname, lineno, 0 );
412                                 exit( 1 );
413                         }
414                         ldap_srvtab = ch_strdup( cargv[1] );
415
416                 /* pass anything else to the current backend config routine */
417                 } else {
418                         if ( be == NULL ) {
419                                 Debug( LDAP_DEBUG_ANY,
420 "%s: line %d: unknown directive \"%s\" outside database definition (ignored)\n",
421                                     fname, lineno, cargv[0] );
422                         } else if ( be->be_config == NULL ) {
423                                 Debug( LDAP_DEBUG_ANY,
424 "%s: line %d: unknown directive \"%s\" inside database definition (ignored)\n",
425                                     fname, lineno, cargv[0] );
426                         } else {
427                                 (*be->be_config)( be, fname, lineno, cargc,
428                                     cargv );
429                         }
430                 }
431         }
432         fclose( fp );
433 }
434
435 static void
436 fp_parse_line(
437     char        *line,
438     int         *argcp,
439     char        **argv
440 )
441 {
442         char *  token;
443
444         *argcp = 0;
445         for ( token = strtok_quote( line, " \t" ); token != NULL;
446             token = strtok_quote( NULL, " \t" ) ) {
447                 if ( *argcp == MAXARGS ) {
448                         Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
449                             MAXARGS, 0, 0 );
450                         exit( 1 );
451                 }
452                 argv[(*argcp)++] = token;
453         }
454         argv[*argcp] = NULL;
455 }
456
457 static char *
458 strtok_quote( char *line, char *sep )
459 {
460         int             inquote;
461         char            *tmp;
462         static char     *next;
463
464         if ( line != NULL ) {
465                 next = line;
466         }
467         while ( *next && strchr( sep, *next ) ) {
468                 next++;
469         }
470
471         if ( *next == '\0' ) {
472                 next = NULL;
473                 return( NULL );
474         }
475         tmp = next;
476
477         for ( inquote = 0; *next; ) {
478                 switch ( *next ) {
479                 case '"':
480                         if ( inquote ) {
481                                 inquote = 0;
482                         } else {
483                                 inquote = 1;
484                         }
485                         SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
486                         break;
487
488                 case '\\':
489                         SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
490                         break;
491
492                 default:
493                         if ( ! inquote ) {
494                                 if ( strchr( sep, *next ) != NULL ) {
495                                         *next++ = '\0';
496                                         return( tmp );
497                                 }
498                         }
499                         next++;
500                         break;
501                 }
502         }
503
504         return( tmp );
505 }
506
507 static char     buf[BUFSIZ];
508 static char     *line;
509 static int      lmax, lcur;
510
511 #define CATLINE( buf )  { \
512         int     len; \
513         len = strlen( buf ); \
514         while ( lcur + len + 1 > lmax ) { \
515                 lmax += BUFSIZ; \
516                 line = (char *) ch_realloc( line, lmax ); \
517         } \
518         strcpy( line + lcur, buf ); \
519         lcur += len; \
520 }
521
522 static char *
523 fp_getline( FILE *fp, int *lineno )
524 {
525         char            *p;
526
527         lcur = 0;
528         CATLINE( buf );
529         (*lineno)++;
530
531         /* hack attack - keeps us from having to keep a stack of bufs... */
532         if ( strncasecmp( line, "include", 7 ) == 0 ) {
533                 buf[0] = '\0';
534                 return( line );
535         }
536
537         while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
538                 if ( (p = strchr( buf, '\n' )) != NULL ) {
539                         *p = '\0';
540                 }
541                 if ( ! isspace( buf[0] ) ) {
542                         return( line );
543                 }
544
545                 CATLINE( buf );
546                 (*lineno)++;
547         }
548         buf[0] = '\0';
549
550         return( line[0] ? line : NULL );
551 }
552
553 static void
554 fp_getline_init( int *lineno )
555 {
556         *lineno = -1;
557         buf[0] = '\0';
558 }