]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
10ab52ae2c9117e27d9d7afdffe0079647580952
[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 void CommenceStartupProcessing( LPCTSTR serverName,
36                                                            void(*stopper)(int));
37 void ReportSlapdShutdownComplete( void );
38 void *getRegParam( char *svc, char *value );
39
40 #define SERVICE_EXIT( e, n )    do { \
41         if ( is_NT_Service ) { \
42                 SLAPDServiceStatus.dwWin32ExitCode                              = (e); \
43                 SLAPDServiceStatus.dwServiceSpecificExitCode    = (n); \
44         } \
45 } while ( 0 )
46
47 #else
48 #define SERVICE_EXIT( e, n )
49 #define MAIN_RETURN(x) return(x)
50 #endif
51
52 #ifdef HAVE_NT_EVENT_LOG
53 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
54 void LogSlapdStoppedEvent( char *svc );
55 #endif
56
57 /*
58  * when more than one slapd is running on one machine, each one might have
59  * it's own LOCAL for syslogging and must have its own pid/args files
60  */
61
62 #ifndef HAVE_MKVERSION
63 const char Versionstr[] =
64         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
65 #endif
66
67 #ifdef LOG_LOCAL4
68
69 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
70
71 typedef struct _str2intDispatch {
72         char    *stringVal;
73         int      abbr;
74         int      intVal;
75 } STRDISP, *STRDISP_P;
76
77
78 /* table to compute syslog-options to integer */
79 static STRDISP  syslog_types[] = {
80         { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
81         { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
82         { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
83         { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
84         { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
85         { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
86         { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
87         { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
88         { NULL, 0, 0 }
89 };
90
91 static int   cnvt_str2int( char *, STRDISP_P, int );
92
93 #endif  /* LOG_LOCAL4 */
94
95 static int check_config = 0;
96
97 static void
98 usage( char *name )
99 {
100         fprintf( stderr,
101                 "usage: %s options\n", name );
102         fprintf( stderr,
103                 "\t-4\t\tIPv4 only\n"
104                 "\t-6\t\tIPv6 only\n"
105                 "\t-d level\tDebug level" "\n"
106                 "\t-f filename\tConfiguration file\n"
107 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
108                 "\t-g group\tGroup (id or name) to run as\n"
109 #endif
110                 "\t-h URLs\t\tList of URLs to serve\n"
111 #ifdef LOG_LOCAL4
112                 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
113 #endif
114                 "\t-n serverName\tService name\n"
115 #ifdef HAVE_CHROOT
116                 "\t-r directory\tSandbox directory to chroot to\n"
117 #endif
118                 "\t-s level\tSyslog level\n"
119                 "\t-t\t\tCheck configuration file and exit\n"
120 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
121                 "\t-u user\t\tUser (id or name) to run as\n"
122 #endif
123     );
124 }
125
126 #ifdef HAVE_NT_SERVICE_MANAGER
127 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
128 #else
129 int main( int argc, char **argv )
130 #endif
131 {
132         int             i, no_detach = 0;
133         int             rc = 1;
134         char *urls = NULL;
135 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
136         char *username = NULL;
137         char *groupname = NULL;
138 #endif
139 #if defined(HAVE_CHROOT)
140         char *sandbox = NULL;
141 #endif
142 #ifdef LOG_LOCAL4
143     int     syslogUser = DEFAULT_SYSLOG_USER;
144 #endif
145         
146         int g_argc = argc;
147         char **g_argv = argv;
148
149 #ifdef HAVE_NT_SERVICE_MANAGER
150         char            *configfile = ".\\slapd.conf";
151 #else
152         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
153 #endif
154         char        *serverName = NULL;
155         int         serverMode = SLAP_SERVER_MODE;
156
157 #ifdef CSRIMALLOC
158         FILE *leakfile;
159         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
160                 leakfile = stderr;
161         }
162 #endif
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         if( serverName == NULL ) {
332                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
333                         serverName = argv[0];
334                 } else {
335                         serverName = serverName + 1;
336                 }
337         }
338
339 #ifdef LOG_LOCAL4
340         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
341 #elif LOG_DEBUG
342         openlog( serverName, OPENLOG_OPTIONS );
343 #endif
344
345         if( !check_config && slapd_daemon_init( urls ) != 0 ) {
346                 rc = 1;
347                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
348                 goto stop;
349         }
350
351 #if defined(HAVE_CHROOT)
352         if ( sandbox ) {
353                 if ( chdir( sandbox ) ) {
354                         perror("chdir");
355                         rc = 1;
356                         goto stop;
357                 }
358                 if ( chroot( sandbox ) ) {
359                         perror("chroot");
360                         rc = 1;
361                         goto stop;
362                 }
363         }
364 #endif
365
366 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
367         if ( username != NULL || groupname != NULL ) {
368                 slap_init_user( username, groupname );
369         }
370 #endif
371
372         extops_init();
373
374 #ifdef SLAPD_MODULES
375         if ( module_init() != 0 ) {
376                 rc = 1;
377                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
378                 goto destroy;
379         }
380 #endif
381
382         if ( slap_init( serverMode, serverName ) != 0 ) {
383                 rc = 1;
384                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
385                 goto destroy;
386         }
387
388         if ( slap_schema_init( ) != 0 ) {
389 #ifdef NEW_LOGGING
390                 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
391 #else
392                 Debug( LDAP_DEBUG_ANY,
393                     "schema initialization error\n",
394                     0, 0, 0 );
395 #endif
396
397                 goto destroy;
398         }
399
400 #ifdef HAVE_TLS
401         /* Library defaults to full certificate checking. This is correct when
402          * a client is verifying a server because all servers should have a
403          * valid cert. But few clients have valid certs, so we want our default
404          * to be no checking. The config file can override this as usual.
405          */
406         rc = 0;
407         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
408 #endif
409
410 #ifdef LDAP_SLAPI
411         if ( slapi_init() != 0 ) {
412 #ifdef NEW_LOGGING
413                 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
414 #else
415                 Debug( LDAP_DEBUG_ANY,
416                     "slapi initialization error\n",
417                     0, 0, 0 );
418 #endif
419
420                 goto destroy;
421         }
422 #endif /* LDAP_SLAPI */
423
424         if ( read_config( configfile, 0 ) != 0 ) {
425                 rc = 1;
426                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
427
428                 if ( check_config ) {
429                         fprintf( stderr, "config check failed\n" );
430                 }
431
432                 goto destroy;
433         }
434
435         if ( check_config ) {
436                 rc = 0;
437                 fprintf( stderr, "config check succeeded\n" );
438                 goto destroy;
439         }
440
441         if ( glue_sub_init( ) != 0 ) {
442 #ifdef NEW_LOGGING
443                 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
444 #else
445                 Debug( LDAP_DEBUG_ANY,
446                     "subordinate config error\n",
447                     0, 0, 0 );
448 #endif
449                 goto destroy;
450         }
451
452         if ( slap_schema_check( ) != 0 ) {
453 #ifdef NEW_LOGGING
454                 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
455 #else
456                 Debug( LDAP_DEBUG_ANY,
457                     "schema prep error\n",
458                     0, 0, 0 );
459 #endif
460
461                 goto destroy;
462         }
463
464 #ifdef HAVE_TLS
465         rc = ldap_pvt_tls_init();
466         if( rc != 0) {
467 #ifdef NEW_LOGGING
468                 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
469 #else
470                 Debug( LDAP_DEBUG_ANY,
471                     "main: TLS init failed: %d\n",
472                     0, 0, 0 );
473 #endif
474                 rc = 1;
475                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
476                 goto destroy;
477         }
478
479         rc = ldap_pvt_tls_init_def_ctx();
480         if( rc != 0) {
481 #ifdef NEW_LOGGING
482                 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
483 #else
484                 Debug( LDAP_DEBUG_ANY,
485                     "main: TLS init def ctx failed: %d\n",
486                     rc, 0, 0 );
487 #endif
488                 rc = 1;
489                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
490                 goto destroy;
491         }
492 #endif
493
494         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
495         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
496
497 #ifdef SIGPIPE
498         (void) SIGNAL( SIGPIPE, SIG_IGN );
499 #endif
500 #ifdef SIGHUP
501         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
502 #endif
503         (void) SIGNAL( SIGINT, slap_sig_shutdown );
504         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
505 #ifdef LDAP_SIGCHLD
506         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
507 #endif
508 #ifdef SIGBREAK
509         /* SIGBREAK is generated when Ctrl-Break is pressed. */
510         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
511 #endif
512
513 #ifndef HAVE_WINSOCK
514         lutil_detach( no_detach, 0 );
515 #endif /* HAVE_WINSOCK */
516
517 #ifdef CSRIMALLOC
518         mal_leaktrace(1);
519 #endif
520
521         if ( slap_startup( NULL )  != 0 ) {
522                 rc = 1;
523                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
524                 goto shutdown;
525         }
526
527         {
528                 FILE *fp;
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                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
539                 {
540                         fprintf( fp, "%d\n", (int) getpid() );
541                         fclose( fp );
542                 }
543
544                 if (( slapd_args_file != NULL ) &&
545                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
546                 {
547                         for ( i = 0; i < g_argc; i++ ) {
548                                 fprintf( fp, "%s ", g_argv[i] );
549                         }
550                         fprintf( fp, "\n" );
551                         fclose( fp );
552                 }
553         }
554
555 #ifdef HAVE_NT_EVENT_LOG
556         if (is_NT_Service)
557         LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
558 #endif
559
560         rc = slapd_daemon();
561
562 #ifdef HAVE_NT_SERVICE_MANAGER
563         /* Throw away the event that we used during the startup process. */
564         if ( is_NT_Service )
565                 ldap_pvt_thread_cond_destroy( &started_event );
566 #endif
567
568 shutdown:
569         /* remember an error during shutdown */
570         rc |= slap_shutdown( NULL );
571
572 destroy:
573         /* remember an error during destroy */
574         rc |= slap_destroy();
575
576 #ifdef SLAPD_MODULES
577         module_kill();
578 #endif
579
580         extops_kill();
581
582 stop:
583 #ifdef HAVE_NT_EVENT_LOG
584         if (is_NT_Service)
585         LogSlapdStoppedEvent( serverName );
586 #endif
587
588 #ifdef NEW_LOGGING
589         LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
590 #else
591         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
592 #endif
593
594
595 #ifdef HAVE_NT_SERVICE_MANAGER
596         ReportSlapdShutdownComplete();
597 #endif
598
599 #ifdef LOG_DEBUG
600     closelog();
601 #endif
602         slapd_daemon_destroy();
603
604         schema_destroy();
605
606 #ifdef HAVE_TLS
607         ldap_pvt_tls_destroy();
608 #endif
609
610         config_destroy();
611
612 #ifdef CSRIMALLOC
613         mal_dumpleaktrace( leakfile );
614 #endif
615
616         MAIN_RETURN(rc);
617 }
618
619
620 #ifdef LDAP_SIGCHLD
621
622 /*
623  *  Catch and discard terminated child processes, to avoid zombies.
624  */
625
626 static RETSIGTYPE
627 wait4child( int sig )
628 {
629     int save_errno = errno;
630
631 #ifdef WNOHANG
632     errno = 0;
633 #ifdef HAVE_WAITPID
634     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
635         ;       /* NULL */
636 #else
637     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
638         ;       /* NULL */
639 #endif
640 #else
641     (void) wait( NULL );
642 #endif
643     (void) SIGNAL_REINSTALL( sig, wait4child );
644     errno = save_errno;
645 }
646
647 #endif /* LDAP_SIGCHLD */
648
649
650 #ifdef LOG_LOCAL4
651
652 /*
653  *  Convert a string to an integer by means of a dispatcher table
654  *  if the string is not in the table return the default
655  */
656
657 static int
658 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
659 {
660     int        retVal = defaultVal;
661     STRDISP_P  disp;
662
663     for (disp = dispatcher; disp->stringVal; disp++) {
664
665         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
666
667             retVal = disp->intVal;
668             break;
669
670         }
671     }
672
673     return (retVal);
674 }
675
676 #endif  /* LOG_LOCAL4 */