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