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