]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
24c56fb6478d4184e91ab1a0668dcb49b9105ba1
[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/signal.h>
17 #include <ac/errno.h>
18
19 #include "slap.h"
20 #include "lutil.h"
21
22 #ifdef LDAP_SIGCHLD
23 static RETSIGTYPE wait4child( int sig );
24 #endif
25
26 #ifdef HAVE_NT_SERVICE_MANAGER
27 #define MAIN_RETURN(x) return
28 struct sockaddr_in      bind_addr;
29
30 /* in nt_main.c */
31 LIBLUTIL_F (SERVICE_STATUS)             SLAPDServiceStatus;
32 LIBLUTIL_F (SERVICE_STATUS_HANDLE)      hSLAPDServiceStatus;
33 extern ldap_pvt_thread_cond_t   started_event,          stopped_event;
34 extern int        is_NT_Service;
35
36 void CommenceStartupProcessing( LPCTSTR serviceName,
37                                                            void(*stopper)(int));
38 void ReportSlapdShutdownComplete( void );
39 void *getRegParam( char *svc, char *value );
40
41 #define SERVICE_EXIT( e, n )    do { \
42         if ( is_NT_Service ) { \
43                 SLAPDServiceStatus.dwWin32ExitCode                              = (e); \
44                 SLAPDServiceStatus.dwServiceSpecificExitCode    = (n); \
45         } \
46 } while ( 0 )
47
48 #else
49 #define SERVICE_EXIT( e, n )
50 #define MAIN_RETURN(x) return(x)
51 #endif
52
53 #ifdef HAVE_NT_EVENT_LOG
54 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
55 void LogSlapdStoppedEvent( char *svc );
56 #endif
57
58 /*
59  * when more than one slapd is running on one machine, each one might have
60  * it's own LOCAL for syslogging and must have its own pid/args files
61  */
62
63 #ifndef HAVE_MKVERSION
64 const char Versionstr[] =
65         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
66 #endif
67
68 #ifdef LOG_LOCAL4
69
70 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
71
72 typedef struct _str2intDispatch {
73         char    *stringVal;
74         int      abbr;
75         int      intVal;
76 } STRDISP, *STRDISP_P;
77
78
79 /* table to compute syslog-options to integer */
80 static STRDISP  syslog_types[] = {
81         { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
82         { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
83         { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
84         { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
85         { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
86         { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
87         { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
88         { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
89         { NULL }
90 };
91
92 static int   cnvt_str2int( char *, STRDISP_P, int );
93
94 #endif  /* LOG_LOCAL4 */
95
96
97 static void
98 usage( char *name )
99 {
100         fprintf( stderr,
101                 "usage: %s options\n", name );
102         fprintf( stderr,
103 #if LDAP_CONNECTIONLESS
104                 "\t-c\t\tEnable (experimental) Connectionless LDAP\n"
105 #endif
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 #ifdef HAVE_NT_EVENT_LOG
116                 "\t-n NTserviceName\tNT service name\n"
117 #endif
118
119                 "\t-s level\tSyslog Level\n"
120 #ifdef SLAPD_BDB2
121                 "\t-t\t\tEnable BDB2 timing\n"
122 #endif
123 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
124                 "\t-u user\tUser (id or name) to ran as\n"
125 #endif
126     );
127 }
128
129 #ifdef HAVE_NT_SERVICE_MANAGER
130 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
131 #else
132 int main( int argc, char **argv )
133 #endif
134 {
135         int             i, no_detach = 0;
136         int             rc;
137         char *urls = NULL;
138 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
139         char *username = NULL;
140         char *groupname = NULL;
141 #endif
142 #if defined(HAVE_CHROOT)
143         char *sandbox = NULL;
144 #endif
145 #ifdef LOG_LOCAL4
146     int     syslogUser = DEFAULT_SYSLOG_USER;
147 #endif
148
149 #ifdef HAVE_NT_EVENT_LOG
150         char        *NTservice  = SERVICE_NAME;
151 #endif
152 #ifdef HAVE_NT_SERVICE_MANAGER
153         char            *configfile = ".\\slapd.conf";
154 #else
155         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
156 #endif
157         char        *serverName;
158         int         serverMode = SLAP_SERVER_MODE;
159
160 #ifdef CSRIMALLOC
161         FILE *leakfile;
162         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
163                 leakfile = stderr;
164         }
165 #endif
166
167         g_argc = argc;
168         g_argv = argv;
169
170 #ifdef HAVE_NT_SERVICE_MANAGER
171         {
172                 int *i;
173                 char *newConfigFile;
174                 char *newUrls;
175                 char *regService = NULL;
176
177                 if ( is_NT_Service ) {
178                         NTservice = argv[0];
179                         CommenceStartupProcessing( NTservice, slap_sig_shutdown );
180                         if ( strcmp(NTservice, SERVICE_NAME) )
181                             regService = NTservice;
182                 }
183
184                 i = (int*)getRegParam( regService, "DebugLevel" );
185                 if ( i != NULL ) 
186                 {
187                         slap_debug = *i;
188                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
189                 }
190
191                 newUrls = (char *) getRegParam(regService, "Urls");
192                 if (newUrls)
193                 {
194                     if (urls)
195                         ch_free(urls);
196
197                     urls = ch_strdup(newUrls);
198                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
199                           urls, 0, 0);
200                 }
201
202                 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
203                 if ( newConfigFile != NULL ) 
204                 {
205                         configfile = newConfigFile;
206                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
207                 }
208         }
209 #endif
210
211         while ( (i = getopt( argc, argv,
212                              "d:f:h:s:"
213 #ifdef HAVE_CHROOT
214                                 "r:"
215 #endif
216 #ifdef LOG_LOCAL4
217                              "l:"
218 #endif
219 #ifdef SLAPD_BDB2
220                              "t"
221 #endif
222 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
223                              "u:g:"
224 #endif
225 #ifdef LDAP_CONNECTIONLESS
226                                  "c"
227 #endif
228 #ifdef HAVE_NT_EVENT_LOG
229                                  "n:"
230 #endif
231                              )) != EOF ) {
232                 switch ( i ) {
233                 case 'h':       /* listen URLs */
234                         if ( urls != NULL ) free( urls );
235                         urls = ch_strdup( optarg );
236             break;
237
238                 case 'd':       /* set debug level and 'do not detach' flag */
239                         no_detach = 1;
240 #ifdef LDAP_DEBUG
241                         slap_debug |= atoi( optarg );
242 #else
243                         if ( atoi( optarg ) != 0 )
244                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
245                                        stderr );
246 #endif
247                         break;
248
249                 case 'f':       /* read config file */
250                         configfile = ch_strdup( optarg );
251                         break;
252
253                 case 's':       /* set syslog level */
254                         ldap_syslog = atoi( optarg );
255                         break;
256
257 #ifdef LOG_LOCAL4
258                 case 'l':       /* set syslog local user */
259                         syslogUser = cnvt_str2int( optarg,
260                                 syslog_types, DEFAULT_SYSLOG_USER );
261                         break;
262 #endif
263
264 #ifdef LDAP_CONNECTIONLESS
265                 case 'c':       /* do connectionless (udp) */
266                         /* udp = 1; */
267                         fprintf( stderr, "connectionless support not supported");
268                         exit( EXIT_FAILURE );
269                         break;
270 #endif
271
272 #ifdef SLAPD_BDB2
273                 case 't':  /* timed server */
274                         serverMode |= SLAP_TIMED_MODE;
275                         break;
276 #endif
277
278 #ifdef HAVE_CHROOT
279                 case 'r':
280                         if( sandbox ) free(sandbox);
281                         sandbox = ch_strdup( optarg );
282                         break;
283 #endif
284
285 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
286                 case 'u':       /* user name */
287                         if( username ) free(username);
288                         username = ch_strdup( optarg );
289                         break;
290
291                 case 'g':       /* group name */
292                         if( groupname ) free(groupname);
293                         groupname = ch_strdup( optarg );
294                         break;
295 #endif /* SETUID && GETUID */
296
297 #ifdef HAVE_NT_EVENT_LOG
298                 case 'n':  /* NT service name */
299                         NTservice = ch_strdup( optarg );
300                         break;
301 #endif
302                 default:
303                         usage( argv[0] );
304                         rc = 1;
305                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
306                         goto stop;
307                 }
308         }
309
310         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
311         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
312         ldif_debug = slap_debug;
313
314         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
315
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 #ifdef LOG_LOCAL4
323         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
324 #elif LOG_DEBUG
325         openlog( serverName, OPENLOG_OPTIONS );
326 #endif
327
328         if( 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 && chroot( sandbox ) ) {
336                 perror("chroot");
337                 rc = 1;
338                 goto stop;
339         }
340 #endif
341
342 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
343         if ( username != NULL || groupname != NULL ) {
344                 slap_init_user( username, groupname );
345         }
346 #endif
347
348         extops_init();
349
350 #ifdef SLAPD_MODULES
351         if ( module_init() != 0 ) {
352                 rc = 1;
353                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
354                 goto destroy;
355         }
356 #endif
357
358         if ( slap_init( serverMode, serverName ) != 0 ) {
359                 rc = 1;
360                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
361                 goto destroy;
362         }
363
364         if ( read_config( configfile ) != 0 ) {
365                 rc = 1;
366                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
367                 goto destroy;
368         }
369
370 #ifdef HAVE_TLS
371         ldap_pvt_tls_init();
372
373         if (ldap_pvt_tls_init_def_ctx() != 0)
374         {
375                 rc = 1;
376                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
377                 goto destroy;
378         }
379 #endif
380
381         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
382         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
383
384 #ifdef SIGPIPE
385         (void) SIGNAL( SIGPIPE, SIG_IGN );
386 #endif
387 #ifdef SIGHUP
388         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
389 #endif
390         (void) SIGNAL( SIGINT, slap_sig_shutdown );
391         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
392 #ifdef LDAP_SIGCHLD
393         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
394 #endif
395 #ifdef SIGBREAK
396         /* SIGBREAK is generated when Ctrl-Break is pressed. */
397         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
398 #endif
399
400 #ifndef HAVE_WINSOCK
401         lutil_detach( no_detach, 0 );
402 #endif /* HAVE_WINSOCK */
403
404 #ifdef CSRIMALLOC
405         mal_leaktrace(1);
406 #endif
407
408         if ( slap_startup( NULL )  != 0 ) {
409                 rc = 1;
410                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
411                 goto shutdown;
412         }
413
414         {
415                 FILE *fp;
416
417                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
418
419                 if (( slapd_pid_file != NULL ) &&
420                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
421                 {
422                         fprintf( fp, "%d\n", (int) getpid() );
423                         fclose( fp );
424                 }
425
426                 if (( slapd_args_file != NULL ) &&
427                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
428                 {
429                         for ( i = 0; i < g_argc; i++ ) {
430                                 fprintf( fp, "%s ", g_argv[i] );
431                         }
432                         fprintf( fp, "\n" );
433                         fclose( fp );
434                 }
435         }
436
437 #ifdef HAVE_NT_EVENT_LOG
438         if (is_NT_Service)
439         LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
440 #endif
441
442         rc = slapd_daemon();
443
444 #ifdef HAVE_NT_SERVICE_MANAGER
445         /* Throw away the event that we used during the startup process. */
446         if ( is_NT_Service )
447                 ldap_pvt_thread_cond_destroy( &started_event );
448 #endif
449
450 shutdown:
451         /* remember an error during shutdown */
452         rc |= slap_shutdown( NULL );
453
454 destroy:
455         /* remember an error during destroy */
456         rc |= slap_destroy();
457
458 #ifdef SLAPD_MODULES
459         module_kill();
460 #endif
461
462         extops_kill();
463
464 stop:
465 #ifdef HAVE_NT_EVENT_LOG
466         if (is_NT_Service)
467         LogSlapdStoppedEvent( NTservice );
468 #endif
469
470         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
471
472 #ifdef HAVE_NT_SERVICE_MANAGER
473         ReportSlapdShutdownComplete();
474 #endif
475
476 #ifdef LOG_DEBUG
477     closelog();
478 #endif
479         slapd_daemon_destroy();
480
481 #ifdef CSRIMALLOC
482         mal_dumpleaktrace( leakfile );
483 #endif
484
485         MAIN_RETURN(rc);
486 }
487
488
489 #ifdef LDAP_SIGCHLD
490
491 /*
492  *  Catch and discard terminated child processes, to avoid zombies.
493  */
494
495 static RETSIGTYPE
496 wait4child( int sig )
497 {
498     int save_errno = errno;
499
500 #ifdef WNOHANG
501     errno = 0;
502 #ifdef HAVE_WAITPID
503     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
504         ;       /* NULL */
505 #else
506     while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
507         ;       /* NULL */
508 #endif
509 #else
510     (void) wait( NULL );
511 #endif
512     (void) SIGNAL( sig, wait4child );
513     errno = save_errno;
514 }
515
516 #endif /* SIGCHLD || SIGCLD */
517
518
519 #ifdef LOG_LOCAL4
520
521 /*
522  *  Convert a string to an integer by means of a dispatcher table
523  *  if the string is not in the table return the default
524  */
525
526 static int
527 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
528 {
529     int        retVal = defaultVal;
530     STRDISP_P  disp;
531
532     for (disp = dispatcher; disp->stringVal; disp++) {
533
534         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
535
536             retVal = disp->intVal;
537             break;
538
539         }
540     }
541
542     return (retVal);
543 }
544
545 #endif  /* LOG_LOCAL4 */