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