]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
1c08e6bd417b6a5d243f63c9784739fc29a8da26
[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, 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 #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                 case 'd':       /* set debug level and 'do not detach' flag */
237                         no_detach = 1;
238 #ifdef LDAP_DEBUG
239                         slap_debug |= atoi( optarg );
240 #else
241                         if ( atoi( optarg ) != 0 )
242                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
243                                        stderr );
244 #endif
245                         break;
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                 lutil_detach( no_detach, 0 );
393 #endif /* HAVE_WINSOCK */
394
395 #ifdef CSRIMALLOC
396         mal_leaktrace(1);
397 #endif
398
399         if ( slap_startup( NULL )  != 0 ) {
400                 rc = 1;
401                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
402                 goto shutdown;
403         }
404
405         {
406                 FILE *fp;
407
408                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
409
410                 if (( slapd_pid_file != NULL ) &&
411                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
412                 {
413                         fprintf( fp, "%d\n", (int) getpid() );
414                         fclose( fp );
415                 }
416
417                 if (( slapd_args_file != NULL ) &&
418                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
419                 {
420                         for ( i = 0; i < g_argc; i++ ) {
421                                 fprintf( fp, "%s ", g_argv[i] );
422                         }
423                         fprintf( fp, "\n" );
424                         fclose( fp );
425                 }
426         }
427
428 #ifdef HAVE_WINSOCK
429         LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
430 #endif
431
432         rc = slapd_daemon();
433
434 #ifdef HAVE_WINSOCK
435         /* Throw away the event that we used during the startup process. */
436         if ( is_NT_Service )
437                 ldap_pvt_thread_cond_destroy( &started_event );
438 #endif
439
440 shutdown:
441         /* remember an error during shutdown */
442         rc |= slap_shutdown( NULL );
443
444 destroy:
445         /* remember an error during destroy */
446         rc |= slap_destroy();
447
448 stop:
449 #ifdef HAVE_WINSOCK
450         LogSlapdStoppedEvent( NTservice );
451 #endif
452
453         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
454
455 #ifdef HAVE_WINSOCK
456         ReportSlapdShutdownComplete();
457 #endif
458
459     closelog();
460         slapd_daemon_destroy();
461
462 #ifdef CSRIMALLOC
463         mal_dumpleaktrace( leakfile );
464 #endif
465
466         MAIN_RETURN(rc);
467 }
468
469
470 #ifdef LDAP_SIGCHLD
471
472 /*
473  *  Catch and discard terminated child processes, to avoid zombies.
474  */
475
476 static RETSIGTYPE
477 wait4child( int sig )
478 {
479     int save_errno = errno;
480
481 #ifdef WNOHANG
482     errno = 0;
483 #ifdef HAVE_WAITPID
484     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
485         ;       /* NULL */
486 #else
487     while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
488         ;       /* NULL */
489 #endif
490 #else
491     (void) wait( NULL );
492 #endif
493     (void) SIGNAL( sig, wait4child );
494     errno = save_errno;
495 }
496
497 #endif /* SIGCHLD || SIGCLD */
498
499
500 #ifdef LOG_LOCAL4
501
502 /*
503  *  Convert a string to an integer by means of a dispatcher table
504  *  if the string is not in the table return the default
505  */
506
507 static int
508 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
509 {
510     int        retVal = defaultVal;
511     STRDISP_P  disp;
512
513     for (disp = dispatcher; disp->stringVal; disp++) {
514
515         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
516
517             retVal = disp->intVal;
518             break;
519
520         }
521     }
522
523     return (retVal);
524 }
525
526 #endif  /* LOG_LOCAL4 */