]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
remove dbenv->lock_put() call from transaction-protected operations
[openldap] / servers / slapd / main.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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/socket.h>
11 #include <ac/string.h>
12 #include <ac/time.h>
13 #include <ac/unistd.h>
14 #include <ac/wait.h>
15 #include <ac/errno.h>
16
17 #include "ldap_pvt.h"
18
19 #include "slap.h"
20 #include "lutil.h"
21 #include "ldif.h"
22
23 #ifdef LDAP_SIGCHLD
24 static RETSIGTYPE wait4child( int sig );
25 #endif
26
27 #ifdef HAVE_NT_SERVICE_MANAGER
28 #define MAIN_RETURN(x) return
29 static struct sockaddr_in       bind_addr;
30
31 void CommenceStartupProcessing( LPCTSTR serverName,
32                                                            void(*stopper)(int));
33 void ReportSlapdShutdownComplete( void );
34 void *getRegParam( char *svc, char *value );
35
36 #define SERVICE_EXIT( e, n )    do { \
37         if ( is_NT_Service ) { \
38                 SLAPDServiceStatus.dwWin32ExitCode                              = (e); \
39                 SLAPDServiceStatus.dwServiceSpecificExitCode    = (n); \
40         } \
41 } while ( 0 )
42
43 #else
44 #define SERVICE_EXIT( e, n )
45 #define MAIN_RETURN(x) return(x)
46 #endif
47
48 #ifdef HAVE_NT_EVENT_LOG
49 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
50 void LogSlapdStoppedEvent( char *svc );
51 #endif
52
53 /*
54  * when more than one slapd is running on one machine, each one might have
55  * it's own LOCAL for syslogging and must have its own pid/args files
56  */
57
58 #ifndef HAVE_MKVERSION
59 const char Versionstr[] =
60         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
61 #endif
62
63 #ifdef LOG_LOCAL4
64
65 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
66
67 typedef struct _str2intDispatch {
68         char    *stringVal;
69         int      abbr;
70         int      intVal;
71 } STRDISP, *STRDISP_P;
72
73
74 /* table to compute syslog-options to integer */
75 static STRDISP  syslog_types[] = {
76         { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
77         { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
78         { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
79         { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
80         { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
81         { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
82         { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
83         { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
84         { NULL }
85 };
86
87 static int   cnvt_str2int( char *, STRDISP_P, int );
88
89 #endif  /* LOG_LOCAL4 */
90
91 static int check_config = 0;
92
93 static void
94 usage( char *name )
95 {
96         fprintf( stderr,
97                 "usage: %s options\n", name );
98         fprintf( stderr,
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 run as\n"
103 #endif
104                 "\t-h URLs\t\tList of URLs to serve\n"
105 #ifdef LOG_LOCAL4
106                 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
107 #endif
108                 "\t-n serverName\tService name\n"
109 #ifdef HAVE_CHROOT
110                 "\t-r directory\tSandbox directory to chroot to\n"
111 #endif
112                 "\t-s level\tSyslog level\n"
113                 "\t-t\t\tCheck configuration file and exit\n"
114 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
115                 "\t-u user\t\tUser (id or name) to run as\n"
116 #endif
117     );
118 }
119
120 #ifdef HAVE_NT_SERVICE_MANAGER
121 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
122 #else
123 int main( int argc, char **argv )
124 #endif
125 {
126         int             i, no_detach = 0;
127         int             rc = 1;
128         char *urls = NULL;
129 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
130         char *username = NULL;
131         char *groupname = NULL;
132 #endif
133 #if defined(HAVE_CHROOT)
134         char *sandbox = NULL;
135 #endif
136 #ifdef LOG_LOCAL4
137     int     syslogUser = DEFAULT_SYSLOG_USER;
138 #endif
139         
140         int g_argc = argc;
141         char **g_argv = argv;
142
143 #ifdef HAVE_NT_SERVICE_MANAGER
144         char            *configfile = ".\\slapd.conf";
145 #else
146         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
147 #endif
148         char        *serverName = NULL;
149         int         serverMode = SLAP_SERVER_MODE;
150
151 #ifdef CSRIMALLOC
152         FILE *leakfile;
153         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
154                 leakfile = stderr;
155         }
156 #endif
157
158
159 #ifdef HAVE_NT_SERVICE_MANAGER
160         {
161                 int *i;
162                 char *newConfigFile;
163                 char *newUrls;
164                 char *regService = NULL;
165
166                 if ( is_NT_Service ) {
167                         serverName = argv[0];
168                         CommenceStartupProcessing( serverName, slap_sig_shutdown );
169                         if ( strcmp(serverName, SERVICE_NAME) )
170                             regService = serverName;
171                 }
172
173                 i = (int*)getRegParam( regService, "DebugLevel" );
174                 if ( i != NULL ) 
175                 {
176                         slap_debug = *i;
177 #ifdef NEW_LOGGING
178                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
179                                    "main: new debug level from registry is: %d\n", slap_debug ));
180 #else
181                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
182 #endif
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 #ifdef NEW_LOGGING
193                     LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
194                                "main: new urls from registry: %s\n", urls ));
195 #else
196                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
197                           urls, 0, 0);
198 #endif
199
200                 }
201
202                 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
203                 if ( newConfigFile != NULL ) 
204                 {
205                         configfile = newConfigFile;
206 #ifdef NEW_LOGGING
207                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
208                                    "main: new config file from registry is: %s\n", configfile ));
209 #else
210                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
211 #endif
212
213                 }
214         }
215 #endif
216
217         while ( (i = getopt( argc, argv,
218                              "d:f:h:s:n:t"
219 #ifdef HAVE_CHROOT
220                                 "r:"
221 #endif
222 #ifdef LOG_LOCAL4
223                              "l:"
224 #endif
225 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
226                              "u:g:"
227 #endif
228                              )) != EOF ) {
229                 switch ( i ) {
230                 case 'h':       /* listen URLs */
231                         if ( urls != NULL ) free( urls );
232                         urls = ch_strdup( optarg );
233             break;
234
235                 case 'd':       /* set debug level and 'do not detach' flag */
236                         no_detach = 1;
237 #ifdef LDAP_DEBUG
238                         slap_debug |= atoi( optarg );
239 #else
240                         if ( atoi( optarg ) != 0 )
241                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
242                                        stderr );
243 #endif
244                         break;
245
246                 case 'f':       /* read config file */
247                         configfile = ch_strdup( optarg );
248                         break;
249
250                 case 's':       /* set syslog level */
251                         ldap_syslog = atoi( optarg );
252                         break;
253
254 #ifdef LOG_LOCAL4
255                 case 'l':       /* set syslog local user */
256                         syslogUser = cnvt_str2int( optarg,
257                                 syslog_types, DEFAULT_SYSLOG_USER );
258                         break;
259 #endif
260
261 #ifdef HAVE_CHROOT
262                 case 'r':
263                         if( sandbox ) free(sandbox);
264                         sandbox = ch_strdup( optarg );
265                         break;
266 #endif
267
268 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
269                 case 'u':       /* user name */
270                         if( username ) free(username);
271                         username = ch_strdup( optarg );
272                         break;
273
274                 case 'g':       /* group name */
275                         if( groupname ) free(groupname);
276                         groupname = ch_strdup( optarg );
277                         break;
278 #endif /* SETUID && GETUID */
279
280                 case 'n':  /* NT service name */
281                         if( serverName != NULL ) free( serverName );
282                         serverName = ch_strdup( optarg );
283                         break;
284
285                 case 't':
286                         check_config++;
287                         break;
288
289                 default:
290                         usage( argv[0] );
291                         rc = 1;
292                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
293                         goto stop;
294                 }
295         }
296
297 #ifdef NEW_LOGGING
298         lutil_log_initialize( argc, argv );
299 #endif
300
301         lutil_set_debug_level( "slapd", slap_debug );
302         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
303         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
304         ldif_debug = slap_debug;
305
306 #ifdef NEW_LOGGING
307         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
308                    "%s", Versionstr ));
309 #else
310         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
311 #endif
312
313
314         if( serverName == NULL ) {
315                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
316                         serverName = argv[0];
317                 } else {
318                         serverName = serverName + 1;
319                 }
320         }
321
322 #ifdef LOG_LOCAL4
323         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
324 #elif LOG_DEBUG
325         openlog( serverName, OPENLOG_OPTIONS );
326 #endif
327
328         if( !check_config && slapd_daemon_init( urls ) != 0 ) {
329                 rc = 1;
330                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
331                 goto stop;
332         }
333
334 #if defined(HAVE_CHROOT)
335         if ( sandbox ) {
336                 if ( chdir( sandbox ) ) {
337                         perror("chdir");
338                         rc = 1;
339                         goto stop;
340                 }
341                 if ( chroot( sandbox ) ) {
342                         perror("chroot");
343                         rc = 1;
344                         goto stop;
345                 }
346         }
347 #endif
348
349 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
350         if ( username != NULL || groupname != NULL ) {
351                 slap_init_user( username, groupname );
352         }
353 #endif
354
355         extops_init();
356
357 #ifdef SLAPD_MODULES
358         if ( module_init() != 0 ) {
359                 rc = 1;
360                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
361                 goto destroy;
362         }
363 #endif
364
365         if ( slap_init( serverMode, serverName ) != 0 ) {
366                 rc = 1;
367                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
368                 goto destroy;
369         }
370
371         if ( slap_schema_init( ) != 0 ) {
372 #ifdef NEW_LOGGING
373                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
374                            "main: schema initialization error\n" ));
375 #else
376                 Debug( LDAP_DEBUG_ANY,
377                     "schema initialization error\n",
378                     0, 0, 0 );
379 #endif
380
381                 goto destroy;
382         }
383
384 #ifdef HAVE_TLS
385         /* Library defaults to full certificate checking. This is correct when
386          * a client is verifying a server because all servers should have a
387          * valid cert. But few clients have valid certs, so we want our default
388          * to be no checking. The config file can override this as usual.
389          */
390         rc = 0;
391         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
392 #endif
393
394         if ( read_config( configfile ) != 0 ) {
395                 rc = 1;
396                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
397
398                 if ( check_config ) {
399                         fprintf( stderr, "config check failed\n" );
400                 }
401
402                 goto destroy;
403         }
404
405         if ( check_config ) {
406                 rc = 0;
407                 fprintf( stderr, "config check succeeded\n" );
408                 goto destroy;
409         }
410
411         if ( glue_sub_init( ) != 0 ) {
412 #ifdef NEW_LOGGING
413                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
414                            "main: subordinate config error\n"));
415 #else
416                 Debug( LDAP_DEBUG_ANY,
417                     "subordinate config error\n",
418                     0, 0, 0 );
419 #endif
420                 goto destroy;
421         }
422
423         if ( slap_schema_check( ) != 0 ) {
424 #ifdef NEW_LOGGING
425                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
426                            "main: schema prep error\n"));
427 #else
428                 Debug( LDAP_DEBUG_ANY,
429                     "schema prep error\n",
430                     0, 0, 0 );
431 #endif
432
433                 goto destroy;
434         }
435
436 #ifdef HAVE_TLS
437         rc = ldap_pvt_tls_init();
438         if( rc != 0) {
439 #ifdef NEW_LOGGING
440                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
441                            "main: tls init failed: %d\n", rc ));
442 #else
443                 Debug( LDAP_DEBUG_ANY,
444                     "main: TLS init failed: %d\n",
445                     0, 0, 0 );
446 #endif
447                 rc = 1;
448                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
449                 goto destroy;
450         }
451
452         rc = ldap_pvt_tls_init_def_ctx();
453         if( rc != 0) {
454 #ifdef NEW_LOGGING
455                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
456                            "main: tls init def ctx failed: %d\n", rc ));
457 #else
458                 Debug( LDAP_DEBUG_ANY,
459                     "main: TLS init def ctx failed: %d\n",
460                     0, 0, 0 );
461 #endif
462                 rc = 1;
463                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
464                 goto destroy;
465         }
466 #endif
467
468         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
469         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
470
471 #ifdef SIGPIPE
472         (void) SIGNAL( SIGPIPE, SIG_IGN );
473 #endif
474 #ifdef SIGHUP
475         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
476 #endif
477         (void) SIGNAL( SIGINT, slap_sig_shutdown );
478         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
479 #ifdef LDAP_SIGCHLD
480         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
481 #endif
482 #ifdef SIGBREAK
483         /* SIGBREAK is generated when Ctrl-Break is pressed. */
484         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
485 #endif
486
487 #ifndef HAVE_WINSOCK
488         lutil_detach( no_detach, 0 );
489 #endif /* HAVE_WINSOCK */
490
491 #ifdef CSRIMALLOC
492         mal_leaktrace(1);
493 #endif
494
495         if ( slap_startup( NULL )  != 0 ) {
496                 rc = 1;
497                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
498                 goto shutdown;
499         }
500
501         {
502                 FILE *fp;
503
504 #ifdef NEW_LOGGING
505                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
506                            "main: slapd starting.\n" ));
507 #else
508                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
509 #endif
510
511
512                 if (( slapd_pid_file != NULL ) &&
513                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
514                 {
515                         fprintf( fp, "%d\n", (int) getpid() );
516                         fclose( fp );
517                 }
518
519                 if (( slapd_args_file != NULL ) &&
520                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
521                 {
522                         for ( i = 0; i < g_argc; i++ ) {
523                                 fprintf( fp, "%s ", g_argv[i] );
524                         }
525                         fprintf( fp, "\n" );
526                         fclose( fp );
527                 }
528         }
529
530 #ifdef HAVE_NT_EVENT_LOG
531         if (is_NT_Service)
532         LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
533 #endif
534
535         rc = slapd_daemon();
536
537 #ifdef HAVE_NT_SERVICE_MANAGER
538         /* Throw away the event that we used during the startup process. */
539         if ( is_NT_Service )
540                 ldap_pvt_thread_cond_destroy( &started_event );
541 #endif
542
543 shutdown:
544         /* remember an error during shutdown */
545         rc |= slap_shutdown( NULL );
546
547 destroy:
548         /* remember an error during destroy */
549         rc |= slap_destroy();
550
551 #ifdef SLAPD_MODULES
552         module_kill();
553 #endif
554
555         extops_kill();
556
557 stop:
558 #ifdef HAVE_NT_EVENT_LOG
559         if (is_NT_Service)
560         LogSlapdStoppedEvent( serverName );
561 #endif
562
563 #ifdef NEW_LOGGING
564         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
565                    "main: slapd stopped.\n" ));
566 #else
567         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
568 #endif
569
570
571 #ifdef HAVE_NT_SERVICE_MANAGER
572         ReportSlapdShutdownComplete();
573 #endif
574
575 #ifdef LOG_DEBUG
576     closelog();
577 #endif
578         slapd_daemon_destroy();
579
580         schema_destroy();
581
582 #ifdef HAVE_TLS
583         ldap_pvt_tls_destroy();
584 #endif
585
586         config_destroy();
587
588 #ifdef CSRIMALLOC
589         mal_dumpleaktrace( leakfile );
590 #endif
591
592         MAIN_RETURN(rc);
593 }
594
595
596 #ifdef LDAP_SIGCHLD
597
598 /*
599  *  Catch and discard terminated child processes, to avoid zombies.
600  */
601
602 static RETSIGTYPE
603 wait4child( int sig )
604 {
605     int save_errno = errno;
606
607 #ifdef WNOHANG
608     errno = 0;
609 #ifdef HAVE_WAITPID
610     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
611         ;       /* NULL */
612 #else
613     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
614         ;       /* NULL */
615 #endif
616 #else
617     (void) wait( NULL );
618 #endif
619     (void) SIGNAL_REINSTALL( sig, wait4child );
620     errno = save_errno;
621 }
622
623 #endif /* SIGCHLD || SIGCLD */
624
625
626 #ifdef LOG_LOCAL4
627
628 /*
629  *  Convert a string to an integer by means of a dispatcher table
630  *  if the string is not in the table return the default
631  */
632
633 static int
634 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
635 {
636     int        retVal = defaultVal;
637     STRDISP_P  disp;
638
639     for (disp = dispatcher; disp->stringVal; disp++) {
640
641         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
642
643             retVal = disp->intVal;
644             break;
645
646         }
647     }
648
649     return (retVal);
650 }
651
652 #endif  /* LOG_LOCAL4 */