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