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