]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
f627ad1ad958261407b507895b78abd6486d304d
[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         if ( slap_controls_init( ) != 0 ) {
401 #ifdef NEW_LOGGING
402                 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
403 #else
404                 Debug( LDAP_DEBUG_ANY,
405                     "controls initialization error\n",
406                     0, 0, 0 );
407 #endif
408
409                 goto destroy;
410         }
411
412 #ifdef HAVE_TLS
413         /* Library defaults to full certificate checking. This is correct when
414          * a client is verifying a server because all servers should have a
415          * valid cert. But few clients have valid certs, so we want our default
416          * to be no checking. The config file can override this as usual.
417          */
418         rc = 0;
419         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
420 #endif
421
422 #ifdef LDAP_SLAPI
423         if ( slapi_init() != 0 ) {
424 #ifdef NEW_LOGGING
425                 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
426 #else
427                 Debug( LDAP_DEBUG_ANY,
428                     "slapi initialization error\n",
429                     0, 0, 0 );
430 #endif
431
432                 goto destroy;
433         }
434 #endif /* LDAP_SLAPI */
435
436         if ( read_config( configfile, 0 ) != 0 ) {
437                 rc = 1;
438                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
439
440                 if ( check_config ) {
441                         fprintf( stderr, "config check failed\n" );
442                 }
443
444                 goto destroy;
445         }
446
447         if ( check_config ) {
448                 rc = 0;
449                 fprintf( stderr, "config check succeeded\n" );
450                 goto destroy;
451         }
452
453         if ( glue_sub_init( ) != 0 ) {
454 #ifdef NEW_LOGGING
455                 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
456 #else
457                 Debug( LDAP_DEBUG_ANY,
458                     "subordinate config error\n",
459                     0, 0, 0 );
460 #endif
461                 goto destroy;
462         }
463
464         if ( slap_schema_check( ) != 0 ) {
465 #ifdef NEW_LOGGING
466                 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
467 #else
468                 Debug( LDAP_DEBUG_ANY,
469                     "schema prep error\n",
470                     0, 0, 0 );
471 #endif
472
473                 goto destroy;
474         }
475
476 #ifdef HAVE_TLS
477         rc = ldap_pvt_tls_init();
478         if( rc != 0) {
479 #ifdef NEW_LOGGING
480                 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
481 #else
482                 Debug( LDAP_DEBUG_ANY,
483                     "main: TLS init failed: %d\n",
484                     0, 0, 0 );
485 #endif
486                 rc = 1;
487                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
488                 goto destroy;
489         }
490
491         rc = ldap_pvt_tls_init_def_ctx();
492         if( rc != 0) {
493 #ifdef NEW_LOGGING
494                 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
495 #else
496                 Debug( LDAP_DEBUG_ANY,
497                     "main: TLS init def ctx failed: %d\n",
498                     rc, 0, 0 );
499 #endif
500                 rc = 1;
501                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
502                 goto destroy;
503         }
504 #endif
505
506         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
507         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
508
509 #ifdef SIGPIPE
510         (void) SIGNAL( SIGPIPE, SIG_IGN );
511 #endif
512 #ifdef SIGHUP
513         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
514 #endif
515         (void) SIGNAL( SIGINT, slap_sig_shutdown );
516         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
517 #ifdef LDAP_SIGCHLD
518         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
519 #endif
520 #ifdef SIGBREAK
521         /* SIGBREAK is generated when Ctrl-Break is pressed. */
522         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
523 #endif
524
525 #ifndef HAVE_WINSOCK
526         lutil_detach( no_detach, 0 );
527 #endif /* HAVE_WINSOCK */
528
529 #ifdef CSRIMALLOC
530         mal_leaktrace(1);
531 #endif
532
533         if ( slap_startup( NULL )  != 0 ) {
534                 rc = 1;
535                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
536                 goto shutdown;
537         }
538
539 #ifdef NEW_LOGGING
540         LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
541 #else
542         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
543 #endif
544
545
546         if ( slapd_pid_file != NULL ) {
547                 FILE *fp = fopen( slapd_pid_file, "w" );
548
549                 if( fp != NULL ) {
550                         fprintf( fp, "%d\n", (int) getpid() );
551                         fclose( fp );
552
553                 } else {
554                         free(slapd_pid_file);
555                         slapd_pid_file = NULL;
556                 }
557         }
558
559         if ( slapd_args_file != NULL ) {
560                 FILE *fp = fopen( slapd_args_file, "w" );
561
562                 if( fp != NULL ) {
563                         for ( i = 0; i < g_argc; i++ ) {
564                                 fprintf( fp, "%s ", g_argv[i] );
565                         }
566                         fprintf( fp, "\n" );
567                         fclose( fp );
568                 } else {
569                         free(slapd_args_file);
570                         slapd_args_file = NULL;
571                 }
572         }
573
574 #ifdef HAVE_NT_EVENT_LOG
575         if (is_NT_Service)
576         LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
577 #endif
578
579         rc = slapd_daemon();
580
581 #ifdef HAVE_NT_SERVICE_MANAGER
582         /* Throw away the event that we used during the startup process. */
583         if ( is_NT_Service )
584                 ldap_pvt_thread_cond_destroy( &started_event );
585 #endif
586
587 shutdown:
588         /* remember an error during shutdown */
589         rc |= slap_shutdown( NULL );
590
591 destroy:
592         /* remember an error during destroy */
593         rc |= slap_destroy();
594
595 #ifdef SLAPD_MODULES
596         module_kill();
597 #endif
598
599         extops_kill();
600
601 stop:
602 #ifdef HAVE_NT_EVENT_LOG
603         if (is_NT_Service)
604         LogSlapdStoppedEvent( serverName );
605 #endif
606
607 #ifdef NEW_LOGGING
608         LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
609 #else
610         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
611 #endif
612
613
614 #ifdef HAVE_NT_SERVICE_MANAGER
615         ReportSlapdShutdownComplete();
616 #endif
617
618 #ifdef LOG_DEBUG
619     closelog();
620 #endif
621         slapd_daemon_destroy();
622
623         controls_destroy();
624
625         schema_destroy();
626
627 #ifdef HAVE_TLS
628         ldap_pvt_tls_destroy();
629 #endif
630
631         if ( slapd_pid_file != NULL ) {
632                 unlink( slapd_pid_file );
633         }
634         if ( slapd_args_file != NULL ) {
635                 unlink( slapd_args_file );
636         }
637
638         config_destroy();
639
640 #ifdef CSRIMALLOC
641         mal_dumpleaktrace( leakfile );
642 #endif
643
644         MAIN_RETURN(rc);
645 }
646
647
648 #ifdef LDAP_SIGCHLD
649
650 /*
651  *  Catch and discard terminated child processes, to avoid zombies.
652  */
653
654 static RETSIGTYPE
655 wait4child( int sig )
656 {
657     int save_errno = errno;
658
659 #ifdef WNOHANG
660     errno = 0;
661 #ifdef HAVE_WAITPID
662     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
663         ;       /* NULL */
664 #else
665     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
666         ;       /* NULL */
667 #endif
668 #else
669     (void) wait( NULL );
670 #endif
671     (void) SIGNAL_REINSTALL( sig, wait4child );
672     errno = save_errno;
673 }
674
675 #endif /* LDAP_SIGCHLD */
676
677
678 #ifdef LOG_LOCAL4
679
680 /*
681  *  Convert a string to an integer by means of a dispatcher table
682  *  if the string is not in the table return the default
683  */
684
685 static int
686 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
687 {
688     int        retVal = defaultVal;
689     STRDISP_P  disp;
690
691     for (disp = dispatcher; disp->stringVal; disp++) {
692
693         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
694
695             retVal = disp->intVal;
696             break;
697
698         }
699     }
700
701     return (retVal);
702 }
703
704 #endif  /* LOG_LOCAL4 */