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