]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Fix new URL startup code.
[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         int tls_port = LDAPS_PORT;
153
154         g_argc = argc;
155         g_argv = argv;
156
157 #ifdef HAVE_WINSOCK
158         {
159                 int *i;
160                 char *newConfigFile;
161                 ldap_debug = 0xffff;
162                 if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
163                 i = (int*)getRegParam( NULL, "Port" );
164                 if ( i != NULL )
165                 {
166                         port = *i;
167                         Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
168                 }
169 #ifdef HAVE_TLS
170                 i = (int*)getRegParam( NULL, "TLSPort" );
171                 if ( i != NULL )
172                 {
173                         tls_port = *i;
174                         Debug ( LDAP_DEBUG_ANY, "new TLS port from registry is: %d\n", tls_port, 0, 0 );
175                 }
176 #endif
177                 i = (int*)getRegParam( NULL, "DebugLevel" );
178                 if ( i != NULL ) 
179                 {
180                         slap_debug = *i;
181                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
182                 }
183                 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
184                 if ( newConfigFile != NULL ) 
185                 {
186                         configfile = newConfigFile;
187                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
188                 }
189         }
190 #endif
191
192         while ( (i = getopt( argc, argv,
193                              "d:f:h:p:s:u"
194 #ifdef LOG_LOCAL4
195                              "l:"
196 #endif
197 #ifdef SLAPD_BDB2
198                              "t"
199 #endif
200 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
201                              "u:g:"
202 #endif
203 #ifdef LDAP_CONNECTIONLESS
204                                  "c"
205 #endif
206 #ifdef HAVE_WINSOCK
207                                  "n:"
208 #endif
209 #ifdef HAVE_TLS
210                              "P:"
211 #endif
212                              )) != EOF ) {
213                 switch ( i ) {
214                 case 'h':       /* listen URLs */
215                         urls = ch_strdup( optarg );
216             break;
217
218 #ifdef LDAP_DEBUG
219                 case 'd':       /* turn on debugging */
220                         slap_debug |= atoi( optarg );
221                         break;
222 #else
223                 case 'd':       /* turn on debugging */
224                         fprintf( stderr,
225                             "must compile with LDAP_DEBUG for debugging\n" );
226                         break;
227 #endif
228
229                 case 'f':       /* read config file */
230                         configfile = ch_strdup( optarg );
231                         break;
232
233                 case 'p': {     /* port on which to listen */
234                                 int p = atoi( optarg );
235                                 if(! p ) {
236                                         fprintf(stderr, "-p %s must be numeric\n", optarg);
237                                 } else if( p < 0 || p >= 1<<16) {
238                                         fprintf(stderr, "-p %s invalid\n", optarg);
239                                 } else {
240                                         port = p;
241                                 }
242                         } break;
243
244 #ifdef HAVE_TLS
245                 case 'P': {     /* port on which to listen for TLS */
246                                 int p = atoi( optarg );
247                                 if(! p ) {
248                                         fprintf(stderr, "-P %s must be numeric\n", optarg);
249                                 } else if( p < 0 || p >= 1<<16) {
250                                         fprintf(stderr, "-P %s invalid\n", optarg);
251                                 } else {
252                                         tls_port = p;
253                                 }
254                         } break;
255 #endif
256
257                 case 's':       /* set syslog level */
258                         ldap_syslog = atoi( optarg );
259                         break;
260
261 #ifdef LOG_LOCAL4
262                 case 'l':       /* set syslog local user */
263                         syslogUser = cnvt_str2int( optarg,
264                                 syslog_types, DEFAULT_SYSLOG_USER );
265                         break;
266 #endif
267
268 #ifdef LDAP_CONNECTIONLESS
269                 case 'c':       /* do connectionless (udp) */
270                         udp = 1;
271                         break;
272 #endif
273
274 #ifdef SLAPD_BDB2
275                 case 't':  /* timed server */
276                         serverMode = SLAP_TIMEDSERVER_MODE;
277                         break;
278 #endif
279
280 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
281                 case 'u':       /* user name */
282                         if( username ) free(username);
283                         username = ch_strdup( optarg );
284                         break;
285
286                 case 'g':       /* group name */
287                         if( groupname ) free(groupname);
288                         groupname = ch_strdup( optarg );
289                         break;
290 #endif /* SETUID && GETUID */
291
292 #ifdef HAVE_WINSOCK
293                 case 'n':  /* NT service name */
294                         NTservice = ch_strdup( optarg );
295                         break;
296 #endif
297                 default:
298                         usage( argv[0] );
299                         rc = 1;
300                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
301                         goto stop;
302                 }
303         }
304
305         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
306         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
307         ldif_debug = slap_debug;
308
309         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
310
311         if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
312                 serverName = ch_strdup( argv[0] );
313         } else {
314                 serverName = ch_strdup( serverName + 1 );
315         }
316
317 #ifdef LOG_LOCAL4
318         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
319 #else
320         openlog( serverName, OPENLOG_OPTIONS );
321 #endif
322
323         if( slapd_daemon_init( urls, port, tls_port ) != 0 ) {
324                 rc = 1;
325                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
326                 goto stop;
327         }
328
329 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
330         if ( username != NULL || groupname != NULL ) {
331                 slap_init_user( username, groupname );
332         }
333 #endif
334
335         if ( slap_init( serverMode, serverName ) != 0 ) {
336                 rc = 1;
337                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
338                 goto destroy;
339         }
340
341         if ( read_config( configfile ) != 0 ) {
342                 rc = 1;
343                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
344                 goto destroy;
345         }
346
347 #ifdef HAVE_TLS
348         ldap_pvt_tls_init();
349         ldap_pvt_tls_init_def_ctx();
350 #endif
351
352         (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
353         (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
354 #ifdef SIGPIPE
355         (void) SIGNAL( SIGPIPE, SIG_IGN );
356 #endif
357 #ifdef SIGHUP
358         (void) SIGNAL( SIGHUP, slap_set_shutdown );
359 #endif
360         (void) SIGNAL( SIGINT, slap_set_shutdown );
361         (void) SIGNAL( SIGTERM, slap_set_shutdown );
362 #ifdef LDAP_SIGCHLD
363         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
364 #endif
365 #ifdef SIGBREAK
366         /* SIGBREAK is generated when Ctrl-Break is pressed. */
367         (void) SIGNAL( SIGBREAK, slap_set_shutdown );
368 #endif
369
370 #ifndef HAVE_WINSOCK
371 #ifdef LDAP_DEBUG
372                 lutil_detach( ldap_debug, 0 );
373 #else
374                 lutil_detach( 0, 0 );
375 #endif
376 #endif /* HAVE_WINSOCK */
377
378         if ( slap_startup(-1)  != 0 ) {
379                 rc = 1;
380                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
381                 goto shutdown;
382         }
383
384         {
385                 FILE *fp;
386
387                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
388
389                 if (( slapd_pid_file != NULL ) &&
390                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
391                 {
392                         fprintf( fp, "%d\n", (int) getpid() );
393                         fclose( fp );
394                 }
395
396                 if (( slapd_args_file != NULL ) &&
397                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
398                 {
399                         for ( i = 0; i < g_argc; i++ ) {
400                                 fprintf( fp, "%s ", g_argv[i] );
401                         }
402                         fprintf( fp, "\n" );
403                         fclose( fp );
404                 }
405         }
406
407 #ifdef HAVE_WINSOCK
408         LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
409 #endif
410
411         rc = slapd_daemon();
412
413 #ifdef HAVE_WINSOCK
414         /* Throw away the event that we used during the startup process. */
415         if ( is_NT_Service )
416                 ldap_pvt_thread_cond_destroy( &started_event );
417 #endif
418
419 shutdown:
420         /* remember an error during shutdown */
421         rc |= slap_shutdown(-1);
422 destroy:
423         /* remember an error during destroy */
424         rc |= slap_destroy();
425
426 stop:
427 #ifdef HAVE_WINSOCK
428         LogSlapdStoppedEvent( NTservice );
429 #endif
430
431         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
432
433 #ifdef HAVE_WINSOCK
434         ReportSlapdShutdownComplete();
435 #endif
436
437     closelog();
438         slapd_daemon_destroy();
439
440         MAIN_RETURN(rc);
441 }
442
443
444 #ifdef LDAP_SIGCHLD
445
446 /*
447  *  Catch and discard terminated child processes, to avoid zombies.
448  */
449
450 static RETSIGTYPE
451 wait4child( int sig )
452 {
453     int save_errno = errno;
454
455 #ifdef WNOHANG
456     errno = 0;
457 #ifdef HAVE_WAITPID
458     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
459         ;       /* NULL */
460 #else
461     while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
462         ;       /* NULL */
463 #endif
464 #else
465     (void) wait( NULL );
466 #endif
467     (void) SIGNAL( sig, wait4child );
468     errno = save_errno;
469 }
470
471 #endif /* SIGCHLD || SIGCLD */
472
473
474 #ifdef LOG_LOCAL4
475
476 /*
477  *  Convert a string to an integer by means of a dispatcher table
478  *  if the string is not in the table return the default
479  */
480
481 static int
482 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
483 {
484     int        retVal = defaultVal;
485     STRDISP_P  disp;
486
487     for (disp = dispatcher; disp->stringVal; disp++) {
488
489         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
490
491             retVal = disp->intVal;
492             break;
493
494         }
495     }
496
497     return (retVal);
498 }
499
500 #endif  /* LOG_LOCAL4 */