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