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