]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Bind and listen on TLS port too
[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, short port, int udp );
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 short port = LDAP_PORT;
52 #ifdef HAVE_TLS
53 short tls_port = LDAP_TLS_PORT;
54 #endif
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",         6, LOG_LOCAL0 },
79     { "LOCAL1",         6, LOG_LOCAL1 },
80     { "LOCAL2",         6, LOG_LOCAL2 },
81     { "LOCAL3",         6, LOG_LOCAL3 },
82     { "LOCAL4",         6, LOG_LOCAL4 },
83     { "LOCAL5",         6, LOG_LOCAL5 },
84     { "LOCAL6",         6, LOG_LOCAL6 },
85     { "LOCAL7",         6, 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, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
98     fprintf( stderr, "\n        [-a bind-address] [-i] [-u]" );
99 #ifdef HAVE_WINSOCK
100         fprintf( stderr, " [-n NTserviceName]" );
101 #endif
102 #if LDAP_CONNECTIONLESS
103         fprintf( stderr, " [-c]" );
104 #endif
105 #ifdef SLAPD_BDB2
106     fprintf( stderr, " [-t]" );
107 #endif
108 #ifdef LOG_LOCAL4
109     fprintf( stderr, " [-l sysloguser]" );
110 #endif
111 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
112     fprintf( stderr, " [-u user] [-g group]" );
113 #endif
114     fprintf( stderr, "\n" );
115 }
116
117 time_t starttime;
118 struct sockaddr_in      bind_addr;
119 ber_int_t tcps;
120 #ifdef HAVE_TLS
121 struct sockaddr_in      tls_bind_addr;
122 ber_int_t tls_tcps;
123 #endif
124
125 #ifdef HAVE_WINSOCK
126 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
127 #else
128 int main( int argc, char **argv )
129 #endif
130 {
131         int             i;
132         int             inetd = 0;
133         int             rc;
134         struct slapd_args args;
135         int             udp;
136 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
137         char *username = NULL;
138         char *groupname = NULL;
139 #endif
140 #ifdef LOG_LOCAL4
141     int     syslogUser = DEFAULT_SYSLOG_USER;
142 #endif
143 #ifdef HAVE_WINSOCK
144         char        *NTservice  = SERVICE_NAME;
145         char            *configfile = ".\\slapd.conf";
146 #else
147         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
148 #endif
149         char        *serverName;
150         int         serverMode = SLAP_SERVER_MODE;
151
152         (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
153         bind_addr.sin_family = AF_INET;
154         bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
155         bind_addr.sin_port = htons(port);
156 #ifdef HAVE_TLS
157         tls_bind_addr.sin_family = AF_INET;
158         tls_bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
159         tls_bind_addr.sin_port = htons(tls_port);
160 #endif
161
162         g_argc = argc;
163         g_argv = argv;
164
165 #ifdef HAVE_WINSOCK
166         {
167                 int *newPort;
168                 int *newDebugLevel;
169                 char *newConfigFile;
170                 ldap_debug = 0xffff;
171                 if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
172                 newPort = (int*)getRegParam( NULL, "Port" );
173                 if ( newPort != NULL )
174                 {
175                         port = *newPort;
176                         bind_addr.sin_port = htons(port);
177                         Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
178                 }
179                 newDebugLevel = (int*)getRegParam( NULL, "DebugLevel" );
180                 if ( newDebugLevel != NULL ) 
181                 {
182                         slap_debug = *newDebugLevel;
183                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
184                 }
185                 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
186                 if ( newConfigFile != NULL ) 
187                 {
188                         configfile = newConfigFile;
189                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
190                 }
191         }
192 #endif
193
194         while ( (i = getopt( argc, argv,
195                              "d:f:ia:p:s:u"
196 #ifdef LOG_LOCAL4
197                              "l:"
198 #endif
199 #ifdef SLAPD_BDB2
200                              "t"
201 #endif
202 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
203                              "u:g:"
204 #endif
205 #ifdef LDAP_CONNECTIONLESS
206                                  "c"
207 #endif
208 #ifdef HAVE_WINSOCK
209                                  "n:"
210 #endif
211 #ifdef HAVE_TLS
212                              "P:"
213 #endif
214                              )) != EOF ) {
215                 switch ( i ) {
216                 case 'a':       /* bind address */
217 #ifdef HAVE_WINSOCK
218                         if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
219 #else
220                         if(!inet_aton(optarg, &bind_addr.sin_addr))
221 #endif
222                         {
223                                 fprintf(stderr, "invalid address (%s) for -a option", optarg);
224                         }
225 #ifdef HAVE_TLS
226 #ifdef HAVE_WINSOCK
227                         tls_bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg);
228 #else
229                         inet_aton(optarg, &tls_bind_addr.sin_addr);
230 #endif
231 #endif
232             break;
233
234 #ifdef LDAP_DEBUG
235                 case 'd':       /* turn on debugging */
236                         if ( optarg[0] == '?' ) {
237                                 printf( "Debug levels:\n" );
238                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
239                                     LDAP_DEBUG_TRACE );
240                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
241                                     LDAP_DEBUG_PACKETS );
242                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
243                                     LDAP_DEBUG_ARGS );
244                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
245                                     LDAP_DEBUG_CONNS );
246                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
247                                     LDAP_DEBUG_BER );
248                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
249                                     LDAP_DEBUG_FILTER );
250                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
251                                     LDAP_DEBUG_CONFIG );
252                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
253                                     LDAP_DEBUG_ACL );
254                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
255                                     LDAP_DEBUG_STATS );
256                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
257                                     LDAP_DEBUG_STATS2 );
258                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
259                                     LDAP_DEBUG_SHELL );
260                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
261                                     LDAP_DEBUG_PARSE );
262                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
263                                     LDAP_DEBUG_ANY );
264                                 exit( 0 );
265                         } else {
266                                 slap_debug |= atoi( optarg );
267                         }
268                         break;
269 #else
270                 case 'd':       /* turn on debugging */
271                         fprintf( stderr,
272                             "must compile with LDAP_DEBUG for debugging\n" );
273                         break;
274 #endif
275
276                 case 'f':       /* read config file */
277                         configfile = ch_strdup( optarg );
278                         break;
279
280                 case 'i':       /* run from inetd */
281                         inetd = 1;
282                         break;
283
284                 case 'p': {     /* port on which to listen */
285                                 port = (short)atoi( optarg );
286                                 if(! port ) {
287                                         fprintf(stderr, "-p %s must be numeric\n", optarg);
288                                 } else {
289                                         bind_addr.sin_port = htons(port);
290                                 }
291                         } break;
292
293 #ifdef HAVE_TLS
294                 case 'P': {     /* port on which to listen for TLS */
295                                 tls_port = (short)atoi( optarg );
296                                 if(! tls_port ) {
297                                         fprintf(stderr, "-P %s must be numeric\n", optarg);
298                                 } else {
299                                         tls_bind_addr.sin_port = htons(tls_port);
300                                 }
301                         } break;
302 #endif
303
304                 case 's':       /* set syslog level */
305                         ldap_syslog = atoi( optarg );
306                         break;
307
308 #ifdef LOG_LOCAL4
309                 case 'l':       /* set syslog local user */
310                         syslogUser = cnvt_str2int( optarg, syslog_types,
311                                            DEFAULT_SYSLOG_USER );
312                         break;
313 #endif
314
315 #ifdef LDAP_CONNECTIONLESS
316                 case 'c':       /* do connectionless (udp) */
317                         udp = 1;
318                         break;
319 #endif
320
321 #ifdef SLAPD_BDB2
322                 case 't':  /* timed server */
323                         serverMode = SLAP_TIMEDSERVER_MODE;
324                         break;
325 #endif
326
327 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
328                 case 'u':       /* user name */
329                         if( username ) free(username);
330                         username = ch_strdup( optarg );
331                         break;
332
333                 case 'g':       /* group name */
334                         if( groupname ) free(groupname);
335                         groupname = ch_strdup( optarg );
336                         break;
337 #endif /* SETUID && GETUID */
338 #ifdef HAVE_WINSOCK
339                 case 'n':  /* NT service name */
340                         NTservice = ch_strdup( optarg );
341                         break;
342 #endif
343                 default:
344                         usage( argv[0] );
345                         rc = 1;
346                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
347                         goto stop;
348                 }
349         }
350
351         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
352         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
353         ldif_debug = slap_debug;
354
355         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
356
357         if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
358                 serverName = ch_strdup( argv[0] );
359         } else {
360                 serverName = ch_strdup( serverName + 1 );
361         }
362
363 #ifdef LOG_LOCAL4
364         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
365 #else
366         openlog( serverName, OPENLOG_OPTIONS );
367 #endif
368
369 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
370         if ( username != NULL || groupname != NULL )
371                 slap_init_user( username, groupname );
372 #endif
373
374         if ( slap_init( serverMode, serverName ) != 0 ) {
375                 rc = 1;
376                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
377                 goto destroy;
378         }
379
380         if ( read_config( configfile ) != 0 ) {
381                 rc = 1;
382                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
383                 goto destroy;
384         }
385
386         tcps = set_socket( inetd ? NULL : &bind_addr );
387         if ( tcps == -1 )
388                 goto destroy;
389 #ifdef HAVE_TLS
390         tls_tcps = set_socket( inetd ? NULL : &tls_bind_addr );
391         if ( tls_tcps == -1 )
392                 goto destroy;
393 #endif
394
395         (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
396         (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
397 #ifdef SIGPIPE
398         (void) SIGNAL( SIGPIPE, SIG_IGN );
399 #endif
400 #ifdef SIGHUP
401         (void) SIGNAL( SIGHUP, slap_set_shutdown );
402 #endif
403         (void) SIGNAL( SIGINT, slap_set_shutdown );
404         (void) SIGNAL( SIGTERM, slap_set_shutdown );
405 #ifdef LDAP_SIGCHLD
406         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
407 #endif
408 #ifdef SIGBREAK
409         /* SIGBREAK is generated when Ctrl-Break is pressed. */
410         (void) SIGNAL( SIGBREAK, slap_set_shutdown );
411 #endif
412
413 #ifndef HAVE_WINSOCK
414         if(!inetd) {
415 #ifdef LDAP_DEBUG
416                 lutil_detach( ldap_debug, 0 );
417 #else
418                 lutil_detach( 0, 0 );
419 #endif
420         }
421 #endif /* HAVE_WINSOC */
422
423         if ( slap_startup(-1)  != 0 ) {
424                 rc = 1;
425                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
426                 goto shutdown;
427         }
428
429         if(!inetd) {
430                 FILE *fp;
431
432                 args.addr = &bind_addr;
433 #ifdef HAVE_TLS
434                 args.tls_addr = &tls_bind_addr;
435 #endif
436
437                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
438
439                 if (( slapd_pid_file != NULL ) &&
440                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
441                 {
442                         fprintf( fp, "%d\n", (int) getpid() );
443                         fclose( fp );
444                 }
445
446                 if (( slapd_args_file != NULL ) &&
447                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
448                 {
449                         for ( i = 0; i < g_argc; i++ ) {
450                                 fprintf( fp, "%s ", g_argv[i] );
451                         }
452                         fprintf( fp, "\n" );
453                         fclose( fp );
454                 }
455
456         } else {
457                 args.addr = NULL;
458 #ifdef HAVE_TLS
459                 args.tls_addr = NULL;
460 #endif
461         }
462         args.tcps = tcps;
463 #ifdef HAVE_TLS
464         args.tls_tcps = tls_tcps;
465 #endif
466
467         time( &starttime );
468 #ifdef HAVE_WINSOCK
469         LogSlapdStartedEvent( NTservice, slap_debug, configfile, port, udp );
470 #endif
471
472         rc = slapd_daemon( &args );
473
474 #ifdef HAVE_WINSOCK
475         /* Throw away the event that we used during the startup process. */
476         if ( is_NT_Service )
477                 ldap_pvt_thread_cond_destroy( &started_event );
478 #endif
479
480 shutdown:
481         /* remember an error during shutdown */
482         rc |= slap_shutdown(-1);
483 destroy:
484         /* remember an error during destroy */
485         rc |= slap_destroy();
486
487 stop:
488 #ifdef HAVE_WINSOCK
489         LogSlapdStoppedEvent( NTservice );
490 #endif
491         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
492 #ifdef HAVE_WINSOCK
493         ReportSlapdShutdownComplete();
494 #endif
495
496     closelog();
497
498         MAIN_RETURN(rc);
499 }
500
501
502 #ifdef LDAP_SIGCHLD
503
504 /*
505  *  Catch and discard terminated child processes, to avoid zombies.
506  */
507
508 static RETSIGTYPE
509 wait4child( int sig )
510 {
511     int save_errno = errno;
512
513 #ifdef WNOHANG
514     errno = 0;
515 #ifdef HAVE_WAITPID
516     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
517         ;       /* NULL */
518 #else
519     while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
520         ;       /* NULL */
521 #endif
522 #else
523     (void) wait( NULL );
524 #endif
525     (void) SIGNAL( sig, wait4child );
526     errno = save_errno;
527 }
528
529 #endif /* SIGCHLD || SIGCLD */
530
531
532 #ifdef LOG_LOCAL4
533
534 /*
535  *  Convert a string to an integer by means of a dispatcher table
536  *  if the string is not in the table return the default
537  */
538
539 static int
540 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
541 {
542     int        retVal = defaultVal;
543     STRDISP_P  disp;
544
545     for (disp = dispatcher; disp->stringVal; disp++) {
546
547         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
548
549             retVal = disp->intVal;
550             break;
551
552         }
553     }
554
555     return (retVal);
556
557 } /* cnvt_str2int */
558
559 #endif  /* LOG_LOCAL4 */