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