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