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