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