]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Fix typos in last commit
[openldap] / servers / slapd / main.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/socket.h>
11 #include <ac/string.h>
12 #include <ac/time.h>
13 #include <ac/unistd.h>
14 #include <ac/wait.h>
15 #include <ac/errno.h>
16
17 #include "ldap_pvt.h"
18
19 #include "slap.h"
20 #include "lutil.h"
21 #include "ldif.h"
22
23 #ifdef LDAP_SLAPI
24 #include "slapi.h"
25 #endif
26
27 #ifdef LDAP_SIGCHLD
28 static RETSIGTYPE wait4child( int sig );
29 #endif
30
31 #ifdef HAVE_NT_SERVICE_MANAGER
32 #define MAIN_RETURN(x) return
33 static struct sockaddr_in       bind_addr;
34
35 #define SERVICE_EXIT( e, n )    do { \
36         if ( is_NT_Service ) { \
37                 lutil_ServiceStatus.dwWin32ExitCode                             = (e); \
38                 lutil_ServiceStatus.dwServiceSpecificExitCode   = (n); \
39         } \
40 } while ( 0 )
41
42 #else
43 #define SERVICE_EXIT( e, n )
44 #define MAIN_RETURN(x) return(x)
45 #endif
46
47 /*
48  * when more than one slapd is running on one machine, each one might have
49  * it's own LOCAL for syslogging and must have its own pid/args files
50  */
51
52 #ifndef HAVE_MKVERSION
53 const char Versionstr[] =
54         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
55 #endif
56
57 #ifdef LOG_LOCAL4
58
59 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
60
61 typedef struct _str2intDispatch {
62         char    *stringVal;
63         int      abbr;
64         int      intVal;
65 } STRDISP, *STRDISP_P;
66
67
68 /* table to compute syslog-options to integer */
69 static STRDISP  syslog_types[] = {
70         { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
71         { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
72         { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
73         { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
74         { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
75         { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
76         { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
77         { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
78         { NULL, 0, 0 }
79 };
80
81 static int   cnvt_str2int( char *, STRDISP_P, int );
82
83 #endif  /* LOG_LOCAL4 */
84
85 static int check_config = 0;
86
87 static void
88 usage( char *name )
89 {
90         fprintf( stderr,
91                 "usage: %s options\n", name );
92         fprintf( stderr,
93                 "\t-4\t\tIPv4 only\n"
94                 "\t-6\t\tIPv6 only\n"
95                 "\t-d level\tDebug level" "\n"
96                 "\t-f filename\tConfiguration file\n"
97 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
98                 "\t-g group\tGroup (id or name) to run as\n"
99 #endif
100                 "\t-h URLs\t\tList of URLs to serve\n"
101 #ifdef LOG_LOCAL4
102                 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
103 #endif
104                 "\t-n serverName\tService name\n"
105 #ifdef HAVE_CHROOT
106                 "\t-r directory\tSandbox directory to chroot to\n"
107 #endif
108                 "\t-s level\tSyslog level\n"
109                 "\t-t\t\tCheck configuration file and exit\n"
110 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
111                 "\t-u user\t\tUser (id or name) to run as\n"
112 #endif
113     );
114 }
115
116 #ifdef HAVE_NT_SERVICE_MANAGER
117 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
118 #else
119 int main( int argc, char **argv )
120 #endif
121 {
122         int             i, no_detach = 0;
123         int             rc = 1;
124         char *urls = NULL;
125 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
126         char *username = NULL;
127         char *groupname = NULL;
128 #endif
129 #if defined(HAVE_CHROOT)
130         char *sandbox = NULL;
131 #endif
132 #ifdef LOG_LOCAL4
133     int     syslogUser = DEFAULT_SYSLOG_USER;
134 #endif
135         
136         int g_argc = argc;
137         char **g_argv = argv;
138
139 #ifdef HAVE_NT_SERVICE_MANAGER
140         char            *configfile = ".\\slapd.conf";
141 #else
142         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
143 #endif
144         char        *serverName = NULL;
145         int         serverMode = SLAP_SERVER_MODE;
146
147 #ifdef CSRIMALLOC
148         FILE *leakfile;
149         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
150                 leakfile = stderr;
151         }
152 #endif
153
154 #ifdef HAVE_NT_SERVICE_MANAGER
155         {
156                 int *i;
157                 char *newConfigFile;
158                 char *newUrls;
159                 char *regService = NULL;
160
161                 if ( is_NT_Service ) {
162                         serverName = argv[0];
163                         lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
164                         if ( strcmp(serverName, SERVICE_NAME) )
165                             regService = serverName;
166                 }
167
168                 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
169                 if ( i != NULL ) 
170                 {
171                         slap_debug = *i;
172 #ifdef NEW_LOGGING
173                         lutil_log_initialize( argc, argv );
174                         LDAP_LOG( SLAPD, INFO, 
175                                 "main: new debug level from registry is: %d\n", 
176                                 slap_debug, 0, 0 );
177 #else
178                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
179 #endif
180                 }
181
182                 newUrls = (char *) lutil_getRegParam(regService, "Urls");
183                 if (newUrls)
184                 {
185                     if (urls)
186                         ch_free(urls);
187
188                     urls = ch_strdup(newUrls);
189 #ifdef NEW_LOGGING
190                     LDAP_LOG( SLAPD, INFO, 
191                                 "main: new urls from registry: %s\n", urls, 0, 0 );
192 #else
193                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
194                           urls, 0, 0);
195 #endif
196
197                 }
198
199                 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
200                 if ( newConfigFile != NULL ) 
201                 {
202                         configfile = newConfigFile;
203 #ifdef NEW_LOGGING
204                         LDAP_LOG( SLAPD, INFO, 
205                                 "main: new config file from registry is: %s\n", configfile, 0, 0 );
206 #else
207                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
208 #endif
209
210                 }
211         }
212 #endif
213
214         while ( (i = getopt( argc, argv,
215                              "d:f:h:s:n:t"
216 #if LDAP_PF_INET6
217                                 "46"
218 #endif
219 #ifdef HAVE_CHROOT
220                                 "r:"
221 #endif
222 #ifdef LOG_LOCAL4
223                              "l:"
224 #endif
225 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
226                              "u:g:"
227 #endif
228                              )) != EOF ) {
229                 switch ( i ) {
230 #ifdef LDAP_PF_INET6
231                 case '4':
232                         slap_inet4or6 = AF_INET;
233                         break;
234                 case '6':
235                         slap_inet4or6 = AF_INET6;
236                         break;
237 #endif
238
239                 case 'h':       /* listen URLs */
240                         if ( urls != NULL ) free( urls );
241                         urls = ch_strdup( optarg );
242             break;
243
244                 case 'd':       /* set debug level and 'do not detach' flag */
245                         no_detach = 1;
246 #ifdef LDAP_DEBUG
247                         slap_debug |= atoi( optarg );
248 #else
249                         if ( atoi( optarg ) != 0 )
250                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
251                                        stderr );
252 #endif
253                         break;
254
255                 case 'f':       /* read config file */
256                         configfile = ch_strdup( optarg );
257                         break;
258
259                 case 's':       /* set syslog level */
260                         ldap_syslog = atoi( optarg );
261                         break;
262
263 #ifdef LOG_LOCAL4
264                 case 'l':       /* set syslog local user */
265                         syslogUser = cnvt_str2int( optarg,
266                                 syslog_types, DEFAULT_SYSLOG_USER );
267                         break;
268 #endif
269
270 #ifdef HAVE_CHROOT
271                 case 'r':
272                         if( sandbox ) free(sandbox);
273                         sandbox = ch_strdup( optarg );
274                         break;
275 #endif
276
277 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
278                 case 'u':       /* user name */
279                         if( username ) free(username);
280                         username = ch_strdup( optarg );
281                         break;
282
283                 case 'g':       /* group name */
284                         if( groupname ) free(groupname);
285                         groupname = ch_strdup( optarg );
286                         break;
287 #endif /* SETUID && GETUID */
288
289                 case 'n':  /* NT service name */
290                         if( serverName != NULL ) free( serverName );
291                         serverName = ch_strdup( optarg );
292                         break;
293
294                 case 't':
295                         check_config++;
296                         break;
297
298                 default:
299                         usage( argv[0] );
300                         rc = 1;
301                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
302                         goto stop;
303                 }
304         }
305
306 #ifdef NEW_LOGGING
307         lutil_log_initialize( argc, argv );
308 #else
309         lutil_set_debug_level( "slapd", slap_debug );
310         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
311         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
312         ldif_debug = slap_debug;
313 #endif
314
315 #ifdef NEW_LOGGING
316         LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
317 #else
318         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
319 #endif
320
321         if( serverName == NULL ) {
322                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
323                         serverName = argv[0];
324                 } else {
325                         serverName = serverName + 1;
326                 }
327         }
328
329 #ifdef LOG_LOCAL4
330         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
331 #elif LOG_DEBUG
332         openlog( serverName, OPENLOG_OPTIONS );
333 #endif
334
335         if( !check_config && slapd_daemon_init( urls ) != 0 ) {
336                 rc = 1;
337                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
338                 goto stop;
339         }
340
341 #if defined(HAVE_CHROOT)
342         if ( sandbox ) {
343                 if ( chdir( sandbox ) ) {
344                         perror("chdir");
345                         rc = 1;
346                         goto stop;
347                 }
348                 if ( chroot( sandbox ) ) {
349                         perror("chroot");
350                         rc = 1;
351                         goto stop;
352                 }
353         }
354 #endif
355
356 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
357         if ( username != NULL || groupname != NULL ) {
358                 slap_init_user( username, groupname );
359         }
360 #endif
361
362         extops_init();
363         lutil_passwd_init();
364
365 #ifdef SLAPD_MODULES
366         if ( module_init() != 0 ) {
367                 rc = 1;
368                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
369                 goto destroy;
370         }
371 #endif
372
373         if ( slap_init( serverMode, serverName ) != 0 ) {
374                 rc = 1;
375                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
376                 goto destroy;
377         }
378
379         if ( slap_schema_init( ) != 0 ) {
380 #ifdef NEW_LOGGING
381                 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
382 #else
383                 Debug( LDAP_DEBUG_ANY,
384                     "schema initialization error\n",
385                     0, 0, 0 );
386 #endif
387
388                 goto destroy;
389         }
390
391         if ( slap_controls_init( ) != 0 ) {
392 #ifdef NEW_LOGGING
393                 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
394 #else
395                 Debug( LDAP_DEBUG_ANY,
396                     "controls initialization error\n",
397                     0, 0, 0 );
398 #endif
399
400                 goto destroy;
401         }
402
403 #ifdef HAVE_TLS
404         /* Library defaults to full certificate checking. This is correct when
405          * a client is verifying a server because all servers should have a
406          * valid cert. But few clients have valid certs, so we want our default
407          * to be no checking. The config file can override this as usual.
408          */
409         rc = 0;
410         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
411 #endif
412
413 #ifdef LDAP_SLAPI
414         if ( slapi_init() != 0 ) {
415 #ifdef NEW_LOGGING
416                 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
417 #else
418                 Debug( LDAP_DEBUG_ANY,
419                     "slapi initialization error\n",
420                     0, 0, 0 );
421 #endif
422
423                 goto destroy;
424         }
425 #endif /* LDAP_SLAPI */
426
427         if ( read_config( configfile, 0 ) != 0 ) {
428                 rc = 1;
429                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
430
431                 if ( check_config ) {
432                         fprintf( stderr, "config check failed\n" );
433                 }
434
435                 goto destroy;
436         }
437
438         if ( check_config ) {
439                 rc = 0;
440                 fprintf( stderr, "config check succeeded\n" );
441                 goto destroy;
442         }
443
444         if ( glue_sub_init( ) != 0 ) {
445 #ifdef NEW_LOGGING
446                 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
447 #else
448                 Debug( LDAP_DEBUG_ANY,
449                     "subordinate config error\n",
450                     0, 0, 0 );
451 #endif
452                 goto destroy;
453         }
454
455         if ( slap_schema_check( ) != 0 ) {
456 #ifdef NEW_LOGGING
457                 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
458 #else
459                 Debug( LDAP_DEBUG_ANY,
460                     "schema prep error\n",
461                     0, 0, 0 );
462 #endif
463
464                 goto destroy;
465         }
466
467 #ifdef HAVE_TLS
468         rc = ldap_pvt_tls_init();
469         if( rc != 0) {
470 #ifdef NEW_LOGGING
471                 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
472 #else
473                 Debug( LDAP_DEBUG_ANY,
474                     "main: TLS init failed: %d\n",
475                     0, 0, 0 );
476 #endif
477                 rc = 1;
478                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
479                 goto destroy;
480         }
481
482         rc = ldap_pvt_tls_init_def_ctx();
483         if( rc != 0) {
484 #ifdef NEW_LOGGING
485                 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
486 #else
487                 Debug( LDAP_DEBUG_ANY,
488                     "main: TLS init def ctx failed: %d\n",
489                     rc, 0, 0 );
490 #endif
491                 rc = 1;
492                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
493                 goto destroy;
494         }
495 #endif
496
497         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
498         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
499
500 #ifdef SIGPIPE
501         (void) SIGNAL( SIGPIPE, SIG_IGN );
502 #endif
503 #ifdef SIGHUP
504         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
505 #endif
506         (void) SIGNAL( SIGINT, slap_sig_shutdown );
507         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
508 #ifdef LDAP_SIGCHLD
509         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
510 #endif
511 #ifdef SIGBREAK
512         /* SIGBREAK is generated when Ctrl-Break is pressed. */
513         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
514 #endif
515
516 #ifndef HAVE_WINSOCK
517         lutil_detach( no_detach, 0 );
518 #endif /* HAVE_WINSOCK */
519
520 #ifdef CSRIMALLOC
521         mal_leaktrace(1);
522 #endif
523
524         if ( slap_startup( NULL )  != 0 ) {
525                 rc = 1;
526                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
527                 goto shutdown;
528         }
529
530 #ifdef NEW_LOGGING
531         LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
532 #else
533         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
534 #endif
535
536
537         if ( slapd_pid_file != NULL ) {
538                 FILE *fp = fopen( slapd_pid_file, "w" );
539
540                 if( fp != NULL ) {
541                         fprintf( fp, "%d\n", (int) getpid() );
542                         fclose( fp );
543
544                 } else {
545                         free(slapd_pid_file);
546                         slapd_pid_file = NULL;
547                 }
548         }
549
550         if ( slapd_args_file != NULL ) {
551                 FILE *fp = fopen( slapd_args_file, "w" );
552
553                 if( fp != NULL ) {
554                         for ( i = 0; i < g_argc; i++ ) {
555                                 fprintf( fp, "%s ", g_argv[i] );
556                         }
557                         fprintf( fp, "\n" );
558                         fclose( fp );
559                 } else {
560                         free(slapd_args_file);
561                         slapd_args_file = NULL;
562                 }
563         }
564
565 #ifdef HAVE_NT_EVENT_LOG
566         if (is_NT_Service)
567         lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
568 #endif
569
570         rc = slapd_daemon();
571
572 #ifdef HAVE_NT_SERVICE_MANAGER
573         /* Throw away the event that we used during the startup process. */
574         if ( is_NT_Service )
575                 ldap_pvt_thread_cond_destroy( &started_event );
576 #endif
577
578 shutdown:
579         /* remember an error during shutdown */
580         rc |= slap_shutdown( NULL );
581
582 destroy:
583         /* remember an error during destroy */
584         rc |= slap_destroy();
585
586 #ifdef SLAPD_MODULES
587         module_kill();
588 #endif
589
590         extops_kill();
591
592 stop:
593 #ifdef HAVE_NT_EVENT_LOG
594         if (is_NT_Service)
595         lutil_LogStoppedEvent( serverName );
596 #endif
597
598 #ifdef NEW_LOGGING
599         LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
600 #else
601         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
602 #endif
603
604
605 #ifdef HAVE_NT_SERVICE_MANAGER
606         lutil_ReportShutdownComplete();
607 #endif
608
609 #ifdef LOG_DEBUG
610     closelog();
611 #endif
612         slapd_daemon_destroy();
613
614         controls_destroy();
615
616         schema_destroy();
617
618         lutil_passwd_destroy();
619
620 #ifdef HAVE_TLS
621         ldap_pvt_tls_destroy();
622 #endif
623
624         if ( slapd_pid_file != NULL ) {
625                 unlink( slapd_pid_file );
626         }
627         if ( slapd_args_file != NULL ) {
628                 unlink( slapd_args_file );
629         }
630
631         config_destroy();
632
633 #ifdef CSRIMALLOC
634         mal_dumpleaktrace( leakfile );
635 #endif
636
637         MAIN_RETURN(rc);
638 }
639
640
641 #ifdef LDAP_SIGCHLD
642
643 /*
644  *  Catch and discard terminated child processes, to avoid zombies.
645  */
646
647 static RETSIGTYPE
648 wait4child( int sig )
649 {
650     int save_errno = errno;
651
652 #ifdef WNOHANG
653     errno = 0;
654 #ifdef HAVE_WAITPID
655     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
656         ;       /* NULL */
657 #else
658     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
659         ;       /* NULL */
660 #endif
661 #else
662     (void) wait( NULL );
663 #endif
664     (void) SIGNAL_REINSTALL( sig, wait4child );
665     errno = save_errno;
666 }
667
668 #endif /* LDAP_SIGCHLD */
669
670
671 #ifdef LOG_LOCAL4
672
673 /*
674  *  Convert a string to an integer by means of a dispatcher table
675  *  if the string is not in the table return the default
676  */
677
678 static int
679 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
680 {
681     int        retVal = defaultVal;
682     STRDISP_P  disp;
683
684     for (disp = dispatcher; disp->stringVal; disp++) {
685
686         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
687
688             retVal = disp->intVal;
689             break;
690
691         }
692     }
693
694     return (retVal);
695 }
696
697 #endif  /* LOG_LOCAL4 */