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