]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
need starttime before backend db_open() is invoked
[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         sl_mem_init();
155
156 #ifdef HAVE_NT_SERVICE_MANAGER
157         {
158                 int *i;
159                 char *newConfigFile;
160                 char *newUrls;
161                 char *regService = NULL;
162
163                 if ( is_NT_Service ) {
164                         serverName = argv[0];
165                         lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
166                         if ( strcmp(serverName, SERVICE_NAME) )
167                             regService = serverName;
168                 }
169
170                 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
171                 if ( i != NULL ) 
172                 {
173                         slap_debug = *i;
174 #ifdef NEW_LOGGING
175                         lutil_log_initialize( argc, argv );
176                         LDAP_LOG( SLAPD, INFO, 
177                                 "main: new debug level from registry is: %d\n", 
178                                 slap_debug, 0, 0 );
179 #else
180                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
181 #endif
182                 }
183
184                 newUrls = (char *) lutil_getRegParam(regService, "Urls");
185                 if (newUrls)
186                 {
187                     if (urls)
188                         ch_free(urls);
189
190                     urls = ch_strdup(newUrls);
191 #ifdef NEW_LOGGING
192                     LDAP_LOG( SLAPD, INFO, 
193                                 "main: new urls from registry: %s\n", urls, 0, 0 );
194 #else
195                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
196                           urls, 0, 0);
197 #endif
198
199                 }
200
201                 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
202                 if ( newConfigFile != NULL ) 
203                 {
204                         configfile = newConfigFile;
205 #ifdef NEW_LOGGING
206                         LDAP_LOG( SLAPD, INFO, 
207                                 "main: new config file from registry is: %s\n", configfile, 0, 0 );
208 #else
209                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
210 #endif
211
212                 }
213         }
214 #endif
215
216         while ( (i = getopt( argc, argv,
217                              "d:f:h:s:n:t"
218 #if LDAP_PF_INET6
219                                 "46"
220 #endif
221 #ifdef HAVE_CHROOT
222                                 "r:"
223 #endif
224 #ifdef LOG_LOCAL4
225                              "l:"
226 #endif
227 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
228                              "u:g:"
229 #endif
230                              )) != EOF ) {
231                 switch ( i ) {
232 #ifdef LDAP_PF_INET6
233                 case '4':
234                         slap_inet4or6 = AF_INET;
235                         break;
236                 case '6':
237                         slap_inet4or6 = AF_INET6;
238                         break;
239 #endif
240
241                 case 'h':       /* listen URLs */
242                         if ( urls != NULL ) free( urls );
243                         urls = ch_strdup( optarg );
244             break;
245
246                 case 'd':       /* set debug level and 'do not detach' flag */
247                         no_detach = 1;
248 #ifdef LDAP_DEBUG
249                         slap_debug |= atoi( optarg );
250 #else
251                         if ( atoi( optarg ) != 0 )
252                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
253                                        stderr );
254 #endif
255                         break;
256
257                 case 'f':       /* read config file */
258                         configfile = ch_strdup( optarg );
259                         break;
260
261                 case 's':       /* set syslog level */
262                         ldap_syslog = atoi( optarg );
263                         break;
264
265 #ifdef LOG_LOCAL4
266                 case 'l':       /* set syslog local user */
267                         syslogUser = cnvt_str2int( optarg,
268                                 syslog_types, DEFAULT_SYSLOG_USER );
269                         break;
270 #endif
271
272 #ifdef HAVE_CHROOT
273                 case 'r':
274                         if( sandbox ) free(sandbox);
275                         sandbox = ch_strdup( optarg );
276                         break;
277 #endif
278
279 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
280                 case 'u':       /* user name */
281                         if( username ) free(username);
282                         username = ch_strdup( optarg );
283                         break;
284
285                 case 'g':       /* group name */
286                         if( groupname ) free(groupname);
287                         groupname = ch_strdup( optarg );
288                         break;
289 #endif /* SETUID && GETUID */
290
291                 case 'n':  /* NT service name */
292                         if( serverName != NULL ) free( serverName );
293                         serverName = ch_strdup( optarg );
294                         break;
295
296                 case 't':
297                         check_config++;
298                         break;
299
300                 default:
301                         usage( argv[0] );
302                         rc = 1;
303                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
304                         goto stop;
305                 }
306         }
307
308 #ifdef NEW_LOGGING
309         lutil_log_initialize( argc, argv );
310 #else
311         lutil_set_debug_level( "slapd", slap_debug );
312         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
313         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
314         ldif_debug = slap_debug;
315 #endif
316
317 #ifdef NEW_LOGGING
318         LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
319 #else
320         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
321 #endif
322
323         if( serverName == NULL ) {
324                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
325                         serverName = argv[0];
326                 } else {
327                         serverName = serverName + 1;
328                 }
329         }
330
331 #ifdef LOG_LOCAL4
332         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
333 #elif LOG_DEBUG
334         openlog( serverName, OPENLOG_OPTIONS );
335 #endif
336
337         if( !check_config && slapd_daemon_init( urls ) != 0 ) {
338                 rc = 1;
339                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
340                 goto stop;
341         }
342
343 #if defined(HAVE_CHROOT)
344         if ( sandbox ) {
345                 if ( chdir( sandbox ) ) {
346                         perror("chdir");
347                         rc = 1;
348                         goto stop;
349                 }
350                 if ( chroot( sandbox ) ) {
351                         perror("chroot");
352                         rc = 1;
353                         goto stop;
354                 }
355         }
356 #endif
357
358 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
359         if ( username != NULL || groupname != NULL ) {
360                 slap_init_user( username, groupname );
361         }
362 #endif
363
364         extops_init();
365         lutil_passwd_init();
366         slap_op_init();
367
368 #ifdef SLAPD_MODULES
369         if ( module_init() != 0 ) {
370                 rc = 1;
371                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
372                 goto destroy;
373         }
374 #endif
375
376         if ( slap_init( serverMode, serverName ) != 0 ) {
377                 rc = 1;
378                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
379                 goto destroy;
380         }
381
382         if ( slap_schema_init( ) != 0 ) {
383 #ifdef NEW_LOGGING
384                 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
385 #else
386                 Debug( LDAP_DEBUG_ANY,
387                     "schema initialization error\n",
388                     0, 0, 0 );
389 #endif
390
391                 goto destroy;
392         }
393
394         if ( slap_controls_init( ) != 0 ) {
395 #ifdef NEW_LOGGING
396                 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
397 #else
398                 Debug( LDAP_DEBUG_ANY,
399                     "controls initialization error\n",
400                     0, 0, 0 );
401 #endif
402
403                 goto destroy;
404         }
405
406 #ifdef HAVE_TLS
407         /* Library defaults to full certificate checking. This is correct when
408          * a client is verifying a server because all servers should have a
409          * valid cert. But few clients have valid certs, so we want our default
410          * to be no checking. The config file can override this as usual.
411          */
412         rc = 0;
413         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
414 #endif
415
416 #ifdef LDAP_SLAPI
417         if ( slapi_init() != 0 ) {
418 #ifdef NEW_LOGGING
419                 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
420 #else
421                 Debug( LDAP_DEBUG_ANY,
422                     "slapi initialization error\n",
423                     0, 0, 0 );
424 #endif
425
426                 goto destroy;
427         }
428 #endif /* LDAP_SLAPI */
429
430         if ( read_config( configfile, 0 ) != 0 ) {
431                 rc = 1;
432                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
433
434                 if ( check_config ) {
435                         fprintf( stderr, "config check failed\n" );
436                 }
437
438                 goto destroy;
439         }
440
441         if ( check_config ) {
442                 rc = 0;
443                 fprintf( stderr, "config check succeeded\n" );
444                 goto destroy;
445         }
446
447         if ( glue_sub_init( ) != 0 ) {
448 #ifdef NEW_LOGGING
449                 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
450 #else
451                 Debug( LDAP_DEBUG_ANY,
452                     "subordinate config error\n",
453                     0, 0, 0 );
454 #endif
455                 goto destroy;
456         }
457
458         if ( slap_schema_check( ) != 0 ) {
459 #ifdef NEW_LOGGING
460                 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
461 #else
462                 Debug( LDAP_DEBUG_ANY,
463                     "schema prep error\n",
464                     0, 0, 0 );
465 #endif
466
467                 goto destroy;
468         }
469
470 #ifdef HAVE_TLS
471         rc = ldap_pvt_tls_init();
472         if( rc != 0) {
473 #ifdef NEW_LOGGING
474                 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
475 #else
476                 Debug( LDAP_DEBUG_ANY,
477                     "main: TLS init failed: %d\n",
478                     0, 0, 0 );
479 #endif
480                 rc = 1;
481                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
482                 goto destroy;
483         }
484
485         rc = ldap_pvt_tls_init_def_ctx();
486         if( rc != 0) {
487 #ifdef NEW_LOGGING
488                 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
489 #else
490                 Debug( LDAP_DEBUG_ANY,
491                     "main: TLS init def ctx failed: %d\n",
492                     rc, 0, 0 );
493 #endif
494                 rc = 1;
495                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
496                 goto destroy;
497         }
498 #endif
499
500         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
501         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
502
503 #ifdef SIGPIPE
504         (void) SIGNAL( SIGPIPE, SIG_IGN );
505 #endif
506 #ifdef SIGHUP
507         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
508 #endif
509         (void) SIGNAL( SIGINT, slap_sig_shutdown );
510         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
511 #ifdef LDAP_SIGCHLD
512         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
513 #endif
514 #ifdef SIGBREAK
515         /* SIGBREAK is generated when Ctrl-Break is pressed. */
516         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
517 #endif
518
519 #ifndef HAVE_WINSOCK
520         lutil_detach( no_detach, 0 );
521 #endif /* HAVE_WINSOCK */
522
523 #ifdef CSRIMALLOC
524         mal_leaktrace(1);
525 #endif
526
527         /*
528          * FIXME: moved here from slapd_daemon_task()
529          * because back-monitor db_open() needs it
530          */
531         time( &starttime );
532
533         if ( slap_startup( NULL )  != 0 ) {
534                 rc = 1;
535                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
536                 goto shutdown;
537         }
538
539 #ifdef NEW_LOGGING
540         LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
541 #else
542         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
543 #endif
544
545
546         if ( slapd_pid_file != NULL ) {
547                 FILE *fp = fopen( slapd_pid_file, "w" );
548
549                 if( fp != NULL ) {
550                         fprintf( fp, "%d\n", (int) getpid() );
551                         fclose( fp );
552
553                 } else {
554                         free(slapd_pid_file);
555                         slapd_pid_file = NULL;
556                 }
557         }
558
559         if ( slapd_args_file != NULL ) {
560                 FILE *fp = fopen( slapd_args_file, "w" );
561
562                 if( fp != NULL ) {
563                         for ( i = 0; i < g_argc; i++ ) {
564                                 fprintf( fp, "%s ", g_argv[i] );
565                         }
566                         fprintf( fp, "\n" );
567                         fclose( fp );
568                 } else {
569                         free(slapd_args_file);
570                         slapd_args_file = NULL;
571                 }
572         }
573
574 #ifdef HAVE_NT_EVENT_LOG
575         if (is_NT_Service)
576         lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
577 #endif
578
579         rc = slapd_daemon();
580
581 #ifdef HAVE_NT_SERVICE_MANAGER
582         /* Throw away the event that we used during the startup process. */
583         if ( is_NT_Service )
584                 ldap_pvt_thread_cond_destroy( &started_event );
585 #endif
586
587 shutdown:
588         /* remember an error during shutdown */
589         rc |= slap_shutdown( NULL );
590
591 destroy:
592         /* remember an error during destroy */
593         rc |= slap_destroy();
594
595 #ifdef SLAPD_MODULES
596         module_kill();
597 #endif
598
599         slap_op_destroy();
600
601         extops_kill();
602
603 stop:
604 #ifdef HAVE_NT_EVENT_LOG
605         if (is_NT_Service)
606         lutil_LogStoppedEvent( serverName );
607 #endif
608
609 #ifdef NEW_LOGGING
610         LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
611 #else
612         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
613 #endif
614
615
616 #ifdef HAVE_NT_SERVICE_MANAGER
617         lutil_ReportShutdownComplete();
618 #endif
619
620 #ifdef LOG_DEBUG
621     closelog();
622 #endif
623         slapd_daemon_destroy();
624
625         controls_destroy();
626
627         schema_destroy();
628
629         lutil_passwd_destroy();
630
631 #ifdef HAVE_TLS
632         ldap_pvt_tls_destroy();
633 #endif
634
635         if ( slapd_pid_file != NULL ) {
636                 unlink( slapd_pid_file );
637         }
638         if ( slapd_args_file != NULL ) {
639                 unlink( slapd_args_file );
640         }
641
642         config_destroy();
643
644 #ifdef CSRIMALLOC
645         mal_dumpleaktrace( leakfile );
646 #endif
647
648         MAIN_RETURN(rc);
649 }
650
651
652 #ifdef LDAP_SIGCHLD
653
654 /*
655  *  Catch and discard terminated child processes, to avoid zombies.
656  */
657
658 static RETSIGTYPE
659 wait4child( int sig )
660 {
661     int save_errno = errno;
662
663 #ifdef WNOHANG
664     errno = 0;
665 #ifdef HAVE_WAITPID
666     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
667         ;       /* NULL */
668 #else
669     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
670         ;       /* NULL */
671 #endif
672 #else
673     (void) wait( NULL );
674 #endif
675     (void) SIGNAL_REINSTALL( sig, wait4child );
676     errno = save_errno;
677 }
678
679 #endif /* LDAP_SIGCHLD */
680
681
682 #ifdef LOG_LOCAL4
683
684 /*
685  *  Convert a string to an integer by means of a dispatcher table
686  *  if the string is not in the table return the default
687  */
688
689 static int
690 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
691 {
692     int        retVal = defaultVal;
693     STRDISP_P  disp;
694
695     for (disp = dispatcher; disp->stringVal; disp++) {
696
697         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
698
699             retVal = disp->intVal;
700             break;
701
702         }
703     }
704
705     return (retVal);
706 }
707
708 #endif  /* LOG_LOCAL4 */