]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
db833db299995e9d9b9e914d41863f6332b3ce6f
[openldap] / servers / slapd / main.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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 extern SERVICE_STATUS                   SLAPDServiceStatus;
32 extern 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-p port\tLDAP Port\n"
120 #ifdef HAVE_TLS
121                 "\t-P port\tLDAP over TLS Port\n"
122 #endif
123                 "\t-s level\tSyslog Level\n"
124 #ifdef SLAPD_BDB2
125                 "\t-t\t\tEnable BDB2 timing\n"
126 #endif
127 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
128                 "\t-u user\tUser (id or name) to ran as\n"
129 #endif
130     );
131 }
132
133 #ifdef HAVE_NT_SERVICE_MANAGER
134 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
135 #else
136 int main( int argc, char **argv )
137 #endif
138 {
139         int             i, no_detach = 0;
140         int             rc;
141         char *urls = NULL;
142 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
143         char *username = NULL;
144         char *groupname = NULL;
145 #endif
146 #ifdef LOG_LOCAL4
147     int     syslogUser = DEFAULT_SYSLOG_USER;
148 #endif
149
150 #ifdef HAVE_NT_EVENT_LOG
151         char        *NTservice  = SERVICE_NAME;
152 #endif
153 #ifdef HAVE_NT_SERVICE_MANAGER
154         char            *configfile = ".\\slapd.conf";
155 #else
156         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
157 #endif
158         char        *serverName;
159         int         serverMode = SLAP_SERVER_MODE;
160
161         int port = LDAP_PORT;
162 #ifdef HAVE_TLS
163         int tls_port = LDAPS_PORT;
164 #else
165         int tls_port = 0;
166 #endif
167
168 #ifdef CSRIMALLOC
169         FILE *leakfile;
170         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
171                 leakfile = stderr;
172         }
173 #endif
174
175         g_argc = argc;
176         g_argv = argv;
177
178 #ifdef HAVE_NT_SERVICE_MANAGER
179         {
180                 int *i;
181                 char *newConfigFile;
182
183                 if ( is_NT_Service ) {
184                         CommenceStartupProcessing( NTservice, slap_sig_shutdown );
185                 }
186
187                 i = (int*)getRegParam( NULL, "Port" );
188                 if ( i != NULL )
189                 {
190                         port = *i;
191                         Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
192                 }
193 #ifdef HAVE_TLS
194                 i = (int*)getRegParam( NULL, "TLSPort" );
195                 if ( i != NULL )
196                 {
197                         tls_port = *i;
198                         Debug ( LDAP_DEBUG_ANY, "new TLS port from registry is: %d\n", tls_port, 0, 0 );
199                 }
200 #endif
201                 i = (int*)getRegParam( NULL, "DebugLevel" );
202                 if ( i != NULL ) 
203                 {
204                         slap_debug = *i;
205                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
206                 }
207                 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
208                 if ( newConfigFile != NULL ) 
209                 {
210                         configfile = newConfigFile;
211                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
212                 }
213         }
214 #endif
215
216         while ( (i = getopt( argc, argv,
217                              "d:f:h:p:s:"
218 #ifdef LOG_LOCAL4
219                              "l:"
220 #endif
221 #ifdef SLAPD_BDB2
222                              "t"
223 #endif
224 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
225                              "u:g:"
226 #endif
227 #ifdef LDAP_CONNECTIONLESS
228                                  "c"
229 #endif
230 #ifdef HAVE_NT_EVENT_LOG
231                                  "n:"
232 #endif
233 #ifdef HAVE_TLS
234                              "P:"
235 #endif
236                              )) != EOF ) {
237                 switch ( i ) {
238                 case 'h':       /* listen URLs */
239                         if ( urls != NULL ) free( urls );
240                         urls = ch_strdup( optarg );
241             break;
242
243                 case 'd':       /* set debug level and 'do not detach' flag */
244                         no_detach = 1;
245 #ifdef LDAP_DEBUG
246                         slap_debug |= atoi( optarg );
247 #else
248                         if ( atoi( optarg ) != 0 )
249                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
250                                        stderr );
251 #endif
252                         break;
253
254                 case 'f':       /* read config file */
255                         configfile = ch_strdup( optarg );
256                         break;
257
258                 case 'p': {     /* port on which to listen */
259                                 int p = atoi( optarg );
260                                 if(! p ) {
261                                         fprintf(stderr, "-p %s must be numeric\n", optarg);
262                                 } else if( p < 0 || p >= 1<<16) {
263                                         fprintf(stderr, "-p %s invalid\n", optarg);
264                                 } else {
265                                         port = p;
266                                 }
267                         } break;
268
269 #ifdef HAVE_TLS
270                 case 'P': {     /* port on which to listen for TLS */
271                                 int p = atoi( optarg );
272                                 if(! p ) {
273                                         fprintf(stderr, "-P %s must be numeric\n", optarg);
274                                 } else if( p < 0 || p >= 1<<16) {
275                                         fprintf(stderr, "-P %s invalid\n", optarg);
276                                 } else {
277                                         tls_port = p;
278                                 }
279                         } break;
280 #endif
281
282                 case 's':       /* set syslog level */
283                         ldap_syslog = atoi( optarg );
284                         break;
285
286 #ifdef LOG_LOCAL4
287                 case 'l':       /* set syslog local user */
288                         syslogUser = cnvt_str2int( optarg,
289                                 syslog_types, DEFAULT_SYSLOG_USER );
290                         break;
291 #endif
292
293 #ifdef LDAP_CONNECTIONLESS
294                 case 'c':       /* do connectionless (udp) */
295                         /* udp = 1; */
296                         fprintf( stderr, "connectionless support not supported");
297                         exit( EXIT_FAILURE );
298                         break;
299 #endif
300
301 #ifdef SLAPD_BDB2
302                 case 't':  /* timed server */
303                         serverMode |= SLAP_TIMED_MODE;
304                         break;
305 #endif
306
307 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
308                 case 'u':       /* user name */
309                         if( username ) free(username);
310                         username = ch_strdup( optarg );
311                         break;
312
313                 case 'g':       /* group name */
314                         if( groupname ) free(groupname);
315                         groupname = ch_strdup( optarg );
316                         break;
317 #endif /* SETUID && GETUID */
318
319 #ifdef HAVE_NT_EVENT_LOG
320                 case 'n':  /* NT service name */
321                         NTservice = ch_strdup( optarg );
322                         break;
323 #endif
324                 default:
325                         usage( argv[0] );
326                         rc = 1;
327                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
328                         goto stop;
329                 }
330         }
331
332         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
333         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
334         ldif_debug = slap_debug;
335
336         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
337
338         if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
339                 serverName = ch_strdup( argv[0] );
340         } else {
341                 serverName = ch_strdup( serverName + 1 );
342         }
343
344 #ifdef LOG_LOCAL4
345         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
346 #elif LOG_DEBUG
347         openlog( serverName, OPENLOG_OPTIONS );
348 #endif
349
350         if( slapd_daemon_init( urls, port, tls_port ) != 0 ) {
351                 rc = 1;
352                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
353                 goto stop;
354         }
355
356 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
357         if ( username != NULL || groupname != NULL ) {
358                 slap_init_user( username, groupname );
359         }
360 #endif
361
362         if ( slap_init( serverMode, serverName ) != 0 ) {
363                 rc = 1;
364                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
365                 goto destroy;
366         }
367
368         if ( read_config( configfile ) != 0 ) {
369                 rc = 1;
370                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
371                 goto destroy;
372         }
373
374 #ifdef HAVE_TLS
375         ldap_pvt_tls_init();
376         ldap_pvt_tls_init_def_ctx();
377 #endif
378
379         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
380         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
381
382 #ifdef SIGPIPE
383         (void) SIGNAL( SIGPIPE, SIG_IGN );
384 #endif
385 #ifdef SIGHUP
386         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
387 #endif
388         (void) SIGNAL( SIGINT, slap_sig_shutdown );
389         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
390 #ifdef LDAP_SIGCHLD
391         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
392 #endif
393 #ifdef SIGBREAK
394         /* SIGBREAK is generated when Ctrl-Break is pressed. */
395         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
396 #endif
397
398 #ifndef HAVE_WINSOCK
399         lutil_detach( no_detach, 0 );
400 #endif /* HAVE_WINSOCK */
401
402 #ifdef CSRIMALLOC
403         mal_leaktrace(1);
404 #endif
405
406         if ( slap_startup( NULL )  != 0 ) {
407                 rc = 1;
408                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
409                 goto shutdown;
410         }
411
412         {
413                 FILE *fp;
414
415                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
416
417                 if (( slapd_pid_file != NULL ) &&
418                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
419                 {
420                         fprintf( fp, "%d\n", (int) getpid() );
421                         fclose( fp );
422                 }
423
424                 if (( slapd_args_file != NULL ) &&
425                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
426                 {
427                         for ( i = 0; i < g_argc; i++ ) {
428                                 fprintf( fp, "%s ", g_argv[i] );
429                         }
430                         fprintf( fp, "\n" );
431                         fclose( fp );
432                 }
433         }
434
435 #ifdef HAVE_NT_EVENT_LOG
436         LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
437 #endif
438
439         rc = slapd_daemon();
440
441 #ifdef HAVE_NT_SERVICE_MANAGER
442         /* Throw away the event that we used during the startup process. */
443         if ( is_NT_Service )
444                 ldap_pvt_thread_cond_destroy( &started_event );
445 #endif
446
447 shutdown:
448         /* remember an error during shutdown */
449         rc |= slap_shutdown( NULL );
450
451 destroy:
452         /* remember an error during destroy */
453         rc |= slap_destroy();
454
455 stop:
456 #ifdef HAVE_NT_EVENT_LOG
457         LogSlapdStoppedEvent( NTservice );
458 #endif
459
460         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
461
462 #ifdef HAVE_NT_SERVICE_MANAGER
463         ReportSlapdShutdownComplete();
464 #endif
465
466 #ifdef LOG_DEBUG
467     closelog();
468 #endif
469         slapd_daemon_destroy();
470
471 #ifdef CSRIMALLOC
472         mal_dumpleaktrace( leakfile );
473 #endif
474
475         MAIN_RETURN(rc);
476 }
477
478
479 #ifdef LDAP_SIGCHLD
480
481 /*
482  *  Catch and discard terminated child processes, to avoid zombies.
483  */
484
485 static RETSIGTYPE
486 wait4child( int sig )
487 {
488     int save_errno = errno;
489
490 #ifdef WNOHANG
491     errno = 0;
492 #ifdef HAVE_WAITPID
493     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
494         ;       /* NULL */
495 #else
496     while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
497         ;       /* NULL */
498 #endif
499 #else
500     (void) wait( NULL );
501 #endif
502     (void) SIGNAL( sig, wait4child );
503     errno = save_errno;
504 }
505
506 #endif /* SIGCHLD || SIGCLD */
507
508
509 #ifdef LOG_LOCAL4
510
511 /*
512  *  Convert a string to an integer by means of a dispatcher table
513  *  if the string is not in the table return the default
514  */
515
516 static int
517 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
518 {
519     int        retVal = defaultVal;
520     STRDISP_P  disp;
521
522     for (disp = dispatcher; disp->stringVal; disp++) {
523
524         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
525
526             retVal = disp->intVal;
527             break;
528
529         }
530     }
531
532     return (retVal);
533 }
534
535 #endif  /* LOG_LOCAL4 */