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