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