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