]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Add multimaster replication support (ITS#170) based upon
[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                         fprintf( stderr, "connectionless support not supported");
276                         exit( EXIT_FAILURE );
277                         break;
278 #endif
279
280 #ifdef SLAPD_BDB2
281                 case 't':  /* timed server */
282                         serverMode = SLAP_TIMEDSERVER_MODE;
283                         break;
284 #endif
285
286 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
287                 case 'u':       /* user name */
288                         if( username ) free(username);
289                         username = ch_strdup( optarg );
290                         break;
291
292                 case 'g':       /* group name */
293                         if( groupname ) free(groupname);
294                         groupname = ch_strdup( optarg );
295                         break;
296 #endif /* SETUID && GETUID */
297
298 #ifdef HAVE_WINSOCK
299                 case 'n':  /* NT service name */
300                         NTservice = ch_strdup( optarg );
301                         break;
302 #endif
303                 default:
304                         usage( argv[0] );
305                         rc = 1;
306                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
307                         goto stop;
308                 }
309         }
310
311         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
312         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
313         ldif_debug = slap_debug;
314
315         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
316
317         if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
318                 serverName = ch_strdup( argv[0] );
319         } else {
320                 serverName = ch_strdup( serverName + 1 );
321         }
322
323 #ifdef LOG_LOCAL4
324         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
325 #else
326         openlog( serverName, OPENLOG_OPTIONS );
327 #endif
328
329         if( slapd_daemon_init( urls, port, tls_port ) != 0 ) {
330                 rc = 1;
331                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
332                 goto stop;
333         }
334
335 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
336         if ( username != NULL || groupname != NULL ) {
337                 slap_init_user( username, groupname );
338         }
339 #endif
340
341         if ( slap_init( serverMode, serverName ) != 0 ) {
342                 rc = 1;
343                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
344                 goto destroy;
345         }
346
347         if ( read_config( configfile ) != 0 ) {
348                 rc = 1;
349                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
350                 goto destroy;
351         }
352
353 #ifdef HAVE_TLS
354         ldap_pvt_tls_init();
355         ldap_pvt_tls_init_def_ctx();
356 #endif
357
358         (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
359         (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
360 #ifdef SIGPIPE
361         (void) SIGNAL( SIGPIPE, SIG_IGN );
362 #endif
363 #ifdef SIGHUP
364         (void) SIGNAL( SIGHUP, slap_set_shutdown );
365 #endif
366         (void) SIGNAL( SIGINT, slap_set_shutdown );
367         (void) SIGNAL( SIGTERM, slap_set_shutdown );
368 #ifdef LDAP_SIGCHLD
369         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
370 #endif
371 #ifdef SIGBREAK
372         /* SIGBREAK is generated when Ctrl-Break is pressed. */
373         (void) SIGNAL( SIGBREAK, slap_set_shutdown );
374 #endif
375
376 #ifndef HAVE_WINSOCK
377 #ifdef LDAP_DEBUG
378                 lutil_detach( ldap_debug, 0 );
379 #else
380                 lutil_detach( 0, 0 );
381 #endif
382 #endif /* HAVE_WINSOCK */
383
384         if ( slap_startup(-1)  != 0 ) {
385                 rc = 1;
386                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
387                 goto shutdown;
388         }
389
390         {
391                 FILE *fp;
392
393                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
394
395                 if (( slapd_pid_file != NULL ) &&
396                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
397                 {
398                         fprintf( fp, "%d\n", (int) getpid() );
399                         fclose( fp );
400                 }
401
402                 if (( slapd_args_file != NULL ) &&
403                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
404                 {
405                         for ( i = 0; i < g_argc; i++ ) {
406                                 fprintf( fp, "%s ", g_argv[i] );
407                         }
408                         fprintf( fp, "\n" );
409                         fclose( fp );
410                 }
411         }
412
413 #ifdef HAVE_WINSOCK
414         LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
415 #endif
416
417         rc = slapd_daemon();
418
419 #ifdef HAVE_WINSOCK
420         /* Throw away the event that we used during the startup process. */
421         if ( is_NT_Service )
422                 ldap_pvt_thread_cond_destroy( &started_event );
423 #endif
424
425 shutdown:
426         /* remember an error during shutdown */
427         rc |= slap_shutdown(-1);
428 destroy:
429         /* remember an error during destroy */
430         rc |= slap_destroy();
431
432 stop:
433 #ifdef HAVE_WINSOCK
434         LogSlapdStoppedEvent( NTservice );
435 #endif
436
437         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
438
439 #ifdef HAVE_WINSOCK
440         ReportSlapdShutdownComplete();
441 #endif
442
443     closelog();
444         slapd_daemon_destroy();
445
446         MAIN_RETURN(rc);
447 }
448
449
450 #ifdef LDAP_SIGCHLD
451
452 /*
453  *  Catch and discard terminated child processes, to avoid zombies.
454  */
455
456 static RETSIGTYPE
457 wait4child( int sig )
458 {
459     int save_errno = errno;
460
461 #ifdef WNOHANG
462     errno = 0;
463 #ifdef HAVE_WAITPID
464     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
465         ;       /* NULL */
466 #else
467     while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
468         ;       /* NULL */
469 #endif
470 #else
471     (void) wait( NULL );
472 #endif
473     (void) SIGNAL( sig, wait4child );
474     errno = save_errno;
475 }
476
477 #endif /* SIGCHLD || SIGCLD */
478
479
480 #ifdef LOG_LOCAL4
481
482 /*
483  *  Convert a string to an integer by means of a dispatcher table
484  *  if the string is not in the table return the default
485  */
486
487 static int
488 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
489 {
490     int        retVal = defaultVal;
491     STRDISP_P  disp;
492
493     for (disp = dispatcher; disp->stringVal; disp++) {
494
495         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
496
497             retVal = disp->intVal;
498             break;
499
500         }
501     }
502
503     return (retVal);
504 }
505
506 #endif  /* LOG_LOCAL4 */