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