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