]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
ccb6c0e12c9fd6b928f380840f06dd1c03bad54b
[openldap] / servers / slapd / main.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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/signal.h>
11 #include <ac/socket.h>
12 #include <ac/string.h>
13 #include <ac/time.h>
14 #include <ac/unistd.h>
15 #include <ac/wait.h>
16 #include <ac/errno.h>
17
18 #include "ldap_pvt.h"
19
20 #include "slap.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 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 }
86 };
87
88 static int   cnvt_str2int( char *, STRDISP_P, int );
89
90 #endif  /* LOG_LOCAL4 */
91
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 ran as\n"
103 #endif
104                 "\t-h URLs\tList of URLs to serve\n"
105 #ifdef LOG_LOCAL4
106                 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
107 #endif
108                 "\t-n serverName\tservice name\n"
109 #ifdef HAVE_CHROOT
110                 "\t-r directory\n"
111 #endif
112                 "\t-s level\tSyslog Level\n"
113 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
114                 "\t-u user\tUser (id or name) to ran as\n"
115 #endif
116     );
117 }
118
119 #ifdef HAVE_NT_SERVICE_MANAGER
120 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
121 #else
122 int main( int argc, char **argv )
123 #endif
124 {
125         int             i, no_detach = 0;
126         int             rc = 1;
127         char *urls = NULL;
128 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
129         char *username = NULL;
130         char *groupname = NULL;
131 #endif
132 #if defined(HAVE_CHROOT)
133         char *sandbox = NULL;
134 #endif
135 #ifdef LOG_LOCAL4
136     int     syslogUser = DEFAULT_SYSLOG_USER;
137 #endif
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         g_argc = argc;
155         g_argv = argv;
156
157 #ifdef HAVE_NT_SERVICE_MANAGER
158         {
159                 int *i;
160                 char *newConfigFile;
161                 char *newUrls;
162                 char *regService = NULL;
163
164                 if ( is_NT_Service ) {
165                         serverName = argv[0];
166                         CommenceStartupProcessing( serverName, slap_sig_shutdown );
167                         if ( strcmp(serverName, SERVICE_NAME) )
168                             regService = serverName;
169                 }
170
171                 i = (int*)getRegParam( regService, "DebugLevel" );
172                 if ( i != NULL ) 
173                 {
174                         slap_debug = *i;
175 #ifdef NEW_LOGGING
176                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
177                                    "main: new debug level from registry is: %d\n", slap_debug ));
178 #else
179                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
180 #endif
181                 }
182
183                 newUrls = (char *) getRegParam(regService, "Urls");
184                 if (newUrls)
185                 {
186                     if (urls)
187                         ch_free(urls);
188
189                     urls = ch_strdup(newUrls);
190 #ifdef NEW_LOGGING
191                     LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
192                                "main: new urls from registry: %s\n", urls ));
193 #else
194                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
195                           urls, 0, 0);
196 #endif
197
198                 }
199
200                 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
201                 if ( newConfigFile != NULL ) 
202                 {
203                         configfile = newConfigFile;
204 #ifdef NEW_LOGGING
205                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
206                                    "main: new config file from registry is: %s\n", configfile ));
207 #else
208                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
209 #endif
210
211                 }
212         }
213 #endif
214
215         while ( (i = getopt( argc, argv,
216                              "d:f:h:s:n:"
217 #ifdef HAVE_CHROOT
218                                 "r:"
219 #endif
220 #ifdef LOG_LOCAL4
221                              "l:"
222 #endif
223 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
224                              "u:g:"
225 #endif
226                              )) != EOF ) {
227                 switch ( i ) {
228                 case 'h':       /* listen URLs */
229                         if ( urls != NULL ) free( urls );
230                         urls = ch_strdup( optarg );
231             break;
232
233                 case 'd':       /* set debug level and 'do not detach' flag */
234                         no_detach = 1;
235 #ifdef LDAP_DEBUG
236                         slap_debug |= atoi( optarg );
237 #else
238                         if ( atoi( optarg ) != 0 )
239                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
240                                        stderr );
241 #endif
242                         break;
243
244                 case 'f':       /* read config file */
245                         configfile = ch_strdup( optarg );
246                         break;
247
248                 case 's':       /* set syslog level */
249                         ldap_syslog = atoi( optarg );
250                         break;
251
252 #ifdef LOG_LOCAL4
253                 case 'l':       /* set syslog local user */
254                         syslogUser = cnvt_str2int( optarg,
255                                 syslog_types, DEFAULT_SYSLOG_USER );
256                         break;
257 #endif
258
259 #ifdef HAVE_CHROOT
260                 case 'r':
261                         if( sandbox ) free(sandbox);
262                         sandbox = ch_strdup( optarg );
263                         break;
264 #endif
265
266 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
267                 case 'u':       /* user name */
268                         if( username ) free(username);
269                         username = ch_strdup( optarg );
270                         break;
271
272                 case 'g':       /* group name */
273                         if( groupname ) free(groupname);
274                         groupname = ch_strdup( optarg );
275                         break;
276 #endif /* SETUID && GETUID */
277
278                 case 'n':  /* NT service name */
279                         if( serverName != NULL ) free( serverName );
280                         serverName = ch_strdup( optarg );
281                         break;
282
283                 default:
284                         usage( argv[0] );
285                         rc = 1;
286                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
287                         goto stop;
288                 }
289         }
290
291 #ifdef NEW_LOGGING
292         lutil_log_initialize( argc, argv );
293 #endif
294
295         lutil_set_debug_level( "slapd", slap_debug );
296         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
297         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
298         ldif_debug = slap_debug;
299
300 #ifdef NEW_LOGGING
301         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
302                    "%s", Versionstr ));
303 #else
304         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
305 #endif
306
307
308         if( serverName == NULL ) {
309                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
310                         serverName = ch_strdup( argv[0] );
311                 } else {
312                         serverName = ch_strdup( serverName + 1 );
313                 }
314         }
315
316 #ifdef LOG_LOCAL4
317         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
318 #elif LOG_DEBUG
319         openlog( serverName, OPENLOG_OPTIONS );
320 #endif
321
322         if( slapd_daemon_init( urls ) != 0 ) {
323                 rc = 1;
324                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
325                 goto stop;
326         }
327
328 #if defined(HAVE_CHROOT)
329         if ( sandbox ) {
330                 if ( chdir( sandbox ) ) {
331                         perror("chdir");
332                         rc = 1;
333                         goto stop;
334                 }
335                 if ( chroot( sandbox ) ) {
336                         perror("chroot");
337                         rc = 1;
338                         goto stop;
339                 }
340         }
341 #endif
342
343 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
344         if ( username != NULL || groupname != NULL ) {
345                 slap_init_user( username, groupname );
346         }
347 #endif
348
349         extops_init();
350
351 #ifdef SLAPD_MODULES
352         if ( module_init() != 0 ) {
353                 rc = 1;
354                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
355                 goto destroy;
356         }
357 #endif
358
359         if ( slap_init( serverMode, serverName ) != 0 ) {
360                 rc = 1;
361                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
362                 goto destroy;
363         }
364
365         if ( schema_init( ) != 0 ) {
366 #ifdef NEW_LOGGING
367                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
368                            "main: schema initialization error\n" ));
369 #else
370                 Debug( LDAP_DEBUG_ANY,
371                     "schema initialization error\n",
372                     0, 0, 0 );
373 #endif
374
375                 goto destroy;
376         }
377
378         if ( read_config( configfile ) != 0 ) {
379                 rc = 1;
380                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
381                 goto destroy;
382         }
383
384         if ( schema_prep( ) != 0 ) {
385 #ifdef NEW_LOGGING
386                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
387                            "main: schema prep error\n"));
388 #else
389                 Debug( LDAP_DEBUG_ANY,
390                     "schema prep error\n",
391                     0, 0, 0 );
392 #endif
393
394                 goto destroy;
395         }
396
397 #ifdef HAVE_TLS
398         rc = ldap_pvt_tls_init();
399         if( rc != 0) {
400 #ifdef NEW_LOGGING
401                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
402                            "main: tls init failed: %d\n", rc ));
403 #else
404                 Debug( LDAP_DEBUG_ANY,
405                     "main: TLS init failed: %d\n",
406                     0, 0, 0 );
407 #endif
408                 rc = 1;
409                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
410                 goto destroy;
411         }
412
413         rc = ldap_pvt_tls_init_def_ctx();
414         if( rc != 0) {
415 #ifdef NEW_LOGGING
416                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
417                            "main: tls init def ctx failed: %d\n", rc ));
418 #else
419                 Debug( LDAP_DEBUG_ANY,
420                     "main: TLS init def ctx failed: %d\n",
421                     0, 0, 0 );
422 #endif
423                 rc = 1;
424                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
425                 goto destroy;
426         }
427 #endif
428
429         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
430         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
431
432 #ifdef SIGPIPE
433         (void) SIGNAL( SIGPIPE, SIG_IGN );
434 #endif
435 #ifdef SIGHUP
436         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
437 #endif
438         (void) SIGNAL( SIGINT, slap_sig_shutdown );
439         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
440 #ifdef LDAP_SIGCHLD
441         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
442 #endif
443 #ifdef SIGBREAK
444         /* SIGBREAK is generated when Ctrl-Break is pressed. */
445         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
446 #endif
447
448 #ifndef HAVE_WINSOCK
449         lutil_detach( no_detach, 0 );
450 #endif /* HAVE_WINSOCK */
451
452 #ifdef CSRIMALLOC
453         mal_leaktrace(1);
454 #endif
455
456         if ( slap_startup( NULL )  != 0 ) {
457                 rc = 1;
458                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
459                 goto shutdown;
460         }
461
462         {
463                 FILE *fp;
464
465 #ifdef NEW_LOGGING
466                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
467                            "main: slapd starting.\n" ));
468 #else
469                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
470 #endif
471
472
473                 if (( slapd_pid_file != NULL ) &&
474                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
475                 {
476                         fprintf( fp, "%d\n", (int) getpid() );
477                         fclose( fp );
478                 }
479
480                 if (( slapd_args_file != NULL ) &&
481                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
482                 {
483                         for ( i = 0; i < g_argc; i++ ) {
484                                 fprintf( fp, "%s ", g_argv[i] );
485                         }
486                         fprintf( fp, "\n" );
487                         fclose( fp );
488                 }
489         }
490
491 #ifdef HAVE_NT_EVENT_LOG
492         if (is_NT_Service)
493         LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
494 #endif
495
496         rc = slapd_daemon();
497
498 #ifdef HAVE_NT_SERVICE_MANAGER
499         /* Throw away the event that we used during the startup process. */
500         if ( is_NT_Service )
501                 ldap_pvt_thread_cond_destroy( &started_event );
502 #endif
503
504 shutdown:
505         /* remember an error during shutdown */
506         rc |= slap_shutdown( NULL );
507
508 destroy:
509         /* remember an error during destroy */
510         rc |= slap_destroy();
511
512 #ifdef SLAPD_MODULES
513         module_kill();
514 #endif
515
516         extops_kill();
517
518 stop:
519 #ifdef HAVE_NT_EVENT_LOG
520         if (is_NT_Service)
521         LogSlapdStoppedEvent( serverName );
522 #endif
523
524 #ifdef NEW_LOGGING
525         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
526                    "main: slapd stopped.\n" ));
527 #else
528         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
529 #endif
530
531
532 #ifdef HAVE_NT_SERVICE_MANAGER
533         ReportSlapdShutdownComplete();
534 #endif
535
536 #ifdef LOG_DEBUG
537     closelog();
538 #endif
539         slapd_daemon_destroy();
540
541         schema_destroy();
542
543 #ifdef HAVE_TLS
544         ldap_pvt_tls_destroy();
545 #endif
546
547 #ifdef CSRIMALLOC
548         mal_dumpleaktrace( leakfile );
549 #endif
550
551         MAIN_RETURN(rc);
552 }
553
554
555 #ifdef LDAP_SIGCHLD
556
557 /*
558  *  Catch and discard terminated child processes, to avoid zombies.
559  */
560
561 static RETSIGTYPE
562 wait4child( int sig )
563 {
564     int save_errno = errno;
565
566 #ifdef WNOHANG
567     errno = 0;
568 #ifdef HAVE_WAITPID
569     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
570         ;       /* NULL */
571 #else
572     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
573         ;       /* NULL */
574 #endif
575 #else
576     (void) wait( NULL );
577 #endif
578     (void) SIGNAL_REINSTALL( sig, wait4child );
579     errno = save_errno;
580 }
581
582 #endif /* SIGCHLD || SIGCLD */
583
584
585 #ifdef LOG_LOCAL4
586
587 /*
588  *  Convert a string to an integer by means of a dispatcher table
589  *  if the string is not in the table return the default
590  */
591
592 static int
593 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
594 {
595     int        retVal = defaultVal;
596     STRDISP_P  disp;
597
598     for (disp = dispatcher; disp->stringVal; disp++) {
599
600         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
601
602             retVal = disp->intVal;
603             break;
604
605         }
606     }
607
608     return (retVal);
609 }
610
611 #endif  /* LOG_LOCAL4 */