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