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