2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
30 #include <ac/socket.h>
31 #include <ac/string.h>
33 #include <ac/unistd.h>
44 #include "slapi/slapi.h"
48 static RETSIGTYPE wait4child( int sig );
51 #ifdef HAVE_NT_SERVICE_MANAGER
52 #define MAIN_RETURN(x) return
53 static struct sockaddr_in bind_addr;
55 #define SERVICE_EXIT( e, n ) do { \
56 if ( is_NT_Service ) { \
57 lutil_ServiceStatus.dwWin32ExitCode = (e); \
58 lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
63 #define SERVICE_EXIT( e, n )
64 #define MAIN_RETURN(x) return(x)
67 typedef int (MainFunc) LDAP_P(( int argc, char *argv[] ));
68 extern MainFunc slapadd, slapcat, slapdn, slapindex, slappasswd,
69 slaptest, slapauth, slapacl;
78 {"slapindex", slapindex},
79 {"slappasswd", slappasswd},
80 {"slaptest", slaptest},
81 {"slapauth", slapauth},
83 /* NOTE: new tools must be added in chronological order,
84 * not in alphabetical order, because for backwards
85 * compatibility name[4] is used to identify the
86 * tools; so name[4]=='a' must refer to "slapadd" and
87 * not to "slapauth". Alphabetical order can be used
88 * for tools whose name[4] is not used yet */
93 * when more than one slapd is running on one machine, each one might have
94 * it's own LOCAL for syslogging and must have its own pid/args files
97 #ifndef HAVE_MKVERSION
98 const char Versionstr[] =
99 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
103 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
105 typedef struct _str2intDispatch {
109 } STRDISP, *STRDISP_P;
111 /* table to compute syslog-options to integer */
112 static STRDISP syslog_types[] = {
113 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
114 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
115 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
116 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
117 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
118 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
119 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
120 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
122 { "USER", sizeof("USER"), LOG_USER },
125 { "DAEMON", sizeof("DAEMON"), LOG_DAEMON },
130 static int cnvt_str2int( char *, STRDISP_P, int );
131 #endif /* LOG_LOCAL4 */
133 #define CHECK_NONE 0x00
134 #define CHECK_CONFIG 0x01
135 static int check = CHECK_NONE;
136 static int version = 0;
141 slapd_opt_slp( const char *val, void *arg )
144 /* NULL is default */
145 if ( val == NULL || strcasecmp( val, "on" ) == 0 ) {
146 slapd_register_slp = 1;
148 } else if ( strcasecmp( val, "off" ) == 0 ) {
149 slapd_register_slp = 0;
151 /* NOTE: add support for URL specification? */
154 fprintf(stderr, "unrecognized value \"%s\" for SLP option\n", val );
161 fputs( "slapd: SLP support is not available\n", stderr );
167 * Option helper structure:
169 * oh_nam is left-hand part of <option>[=<value>]
170 * oh_fnc is handler function
171 * oh_arg is an optional arg to oh_fnc
172 * oh_usage is the one-line usage string related to the option,
173 * which is assumed to start with <option>[=<value>]
175 * please leave valid options in the structure, and optionally #ifdef
176 * their processing inside the helper, so that reasonable and helpful
177 * error messages can be generated if a disabled option is requested.
179 struct option_helper {
180 struct berval oh_name;
181 int (*oh_fnc)(const char *val, void *arg);
183 const char *oh_usage;
184 } option_helpers[] = {
185 { BER_BVC("slp"), slapd_opt_slp, NULL, "slp[={on|off}] enable/disable SLP" },
193 "usage: %s options\n", name );
195 "\t-4\t\tIPv4 only\n"
196 "\t-6\t\tIPv6 only\n"
197 "\t-T {add|auth|cat|dn|index|passwd|test}\n"
198 "\t\t\tRun in Tool mode\n"
199 "\t-c cookie\tSync cookie of consumer\n"
200 "\t-d level\tDebug level" "\n"
201 "\t-f filename\tConfiguration file\n"
202 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
203 "\t-g group\tGroup (id or name) to run as\n"
205 "\t-h URLs\t\tList of URLs to serve\n"
207 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
209 "\t-n serverName\tService name\n"
210 "\t-o <opt>[=val] generic means to specify options" );
211 if ( !BER_BVISNULL( &option_helpers[0].oh_name ) ) {
214 fprintf( stderr, "; supported options:\n" );
215 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++) {
216 fprintf( stderr, "\t\t%s\n", option_helpers[i].oh_usage );
219 fprintf( stderr, "\n" );
223 "\t-r directory\tSandbox directory to chroot to\n"
225 "\t-s level\tSyslog level\n"
226 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
227 "\t-u user\t\tUser (id or name) to run as\n"
229 "\t-V\t\tprint version info (-VV only)\n"
233 #ifdef HAVE_NT_SERVICE_MANAGER
234 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
236 int main( int argc, char **argv )
239 int i, no_detach = 0;
242 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
243 char *username = NULL;
244 char *groupname = NULL;
246 #if defined(HAVE_CHROOT)
247 char *sandbox = NULL;
250 int syslogUser = DEFAULT_SYSLOG_USER;
254 char **g_argv = argv;
256 #ifdef HAVE_NT_SERVICE_MANAGER
257 char *configfile = ".\\slapd.conf";
259 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
262 int serverMode = SLAP_SERVER_MODE;
264 struct berval cookie = BER_BVNULL;
265 struct sync_cookie *scp = NULL;
266 struct sync_cookie *scp_entry = NULL;
270 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
274 char *serverNamePrefix = "";
279 serverName = lutil_progname( "slapd", argc, argv );
281 if ( strcmp( serverName, "slapd" ) ) {
282 for (i=0; tools[i].name; i++) {
283 if ( !strcmp( serverName, tools[i].name ) ) {
284 rc = tools[i].func(argc, argv);
290 #ifdef HAVE_NT_SERVICE_MANAGER
295 char *regService = NULL;
297 if ( is_NT_Service ) {
298 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
299 if ( strcmp(serverName, SERVICE_NAME) )
300 regService = serverName;
303 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
307 lutil_log_initialize( argc, argv );
308 LDAP_LOG( SLAPD, INFO,
309 "main: new debug level from registry is: %d\n",
312 Debug( LDAP_DEBUG_ANY,
313 "new debug level from registry is: %d\n", slap_debug, 0, 0 );
317 newUrls = (char *) lutil_getRegParam(regService, "Urls");
322 urls = ch_strdup(newUrls);
324 LDAP_LOG( SLAPD, INFO,
325 "main: new urls from registry: %s\n", urls, 0, 0 );
327 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
332 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
333 if ( newConfigFile != NULL ) {
334 configfile = newConfigFile;
336 LDAP_LOG( SLAPD, INFO,
337 "main: new config file from registry is: %s\n", configfile, 0, 0 );
339 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
345 while ( (i = getopt( argc, argv,
346 "c:d:f:h:n:o:s:StT:V"
356 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
363 slap_inet4or6 = AF_INET;
366 slap_inet4or6 = AF_INET6;
370 case 'h': /* listen URLs */
371 if ( urls != NULL ) free( urls );
372 urls = ch_strdup( optarg );
375 case 'c': /* provide sync cookie, override if exist in replica */
376 scp = (struct sync_cookie *) ch_calloc( 1,
377 sizeof( struct sync_cookie ));
378 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
379 ber_bvarray_add( &scp->octet_str, &cookie );
380 slap_parse_sync_cookie( scp );
382 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
383 if ( scp->rid == scp_entry->rid ) {
385 LDAP_LOG( OPERATION, CRIT,
386 "main: duplicated replica id in cookies\n",
389 Debug( LDAP_DEBUG_ANY,
390 "main: duplicated replica id in cookies\n",
393 slap_sync_cookie_free( scp, 1 );
397 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
400 case 'd': /* set debug level and 'do not detach' flag */
403 slap_debug |= atoi( optarg );
405 if ( atoi( optarg ) != 0 )
406 fputs( "must compile with LDAP_DEBUG for debugging\n",
411 case 'f': /* read config file */
412 configfile = ch_strdup( optarg );
416 char *val = strchr( optarg, '=' );
423 opt.bv_len = ( val - optarg );
427 opt.bv_len = strlen( optarg );
430 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++ ) {
431 if ( ber_bvstrcasecmp( &option_helpers[i].oh_name, &opt ) == 0 ) {
432 assert( option_helpers[i].oh_fnc != NULL );
433 if ( (*option_helpers[i].oh_fnc)( val, option_helpers[i].oh_arg ) == -1 ) {
434 /* we assume the option parsing helper
435 * issues appropriate and self-explanatory
436 * error messages... */
443 if ( BER_BVISNULL( &option_helpers[i].oh_name ) ) {
444 goto unhandled_option;
449 case 's': /* set syslog level */
450 ldap_syslog = atoi( optarg );
454 case 'l': /* set syslog local user */
455 syslogUser = cnvt_str2int( optarg,
456 syslog_types, DEFAULT_SYSLOG_USER );
462 if( sandbox ) free(sandbox);
463 sandbox = ch_strdup( optarg );
467 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
468 case 'u': /* user name */
469 if( username ) free(username);
470 username = ch_strdup( optarg );
473 case 'g': /* group name */
474 if( groupname ) free(groupname);
475 groupname = ch_strdup( optarg );
477 #endif /* SETUID && GETUID */
479 case 'n': /* NT service name */
480 serverName = ch_strdup( optarg );
484 /* deprecated; use slaptest instead */
485 fprintf( stderr, "option -t deprecated; "
486 "use slaptest command instead\n" );
487 check |= CHECK_CONFIG;
495 /* try full option string first */
496 for ( i = 0; tools[i].name; i++ ) {
497 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
498 rc = tools[i].func( argc, argv );
503 /* try bits of option string (backward compatibility for single char) */
504 l = strlen( optarg );
505 for ( i = 0; tools[i].name; i++ ) {
506 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
507 rc = tools[i].func( argc, argv );
514 serverNamePrefix = "slap";
515 fprintf( stderr, "program name \"%s%s\" unrecognized; "
516 "aborting...\n", serverNamePrefix, serverName );
522 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
528 lutil_log_initialize( argc, argv );
530 lutil_set_debug_level( "slapd", slap_debug );
531 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
532 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
533 ldif_debug = slap_debug;
537 fprintf( stderr, "%s\n", Versionstr );
538 if ( version > 1 ) goto stop;
544 logName = ch_strdup( serverName );
547 logName = serverName;
551 openlog( logName, OPENLOG_OPTIONS, syslogUser );
553 openlog( logName, OPENLOG_OPTIONS );
561 LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
563 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
566 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
568 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
572 #if defined(HAVE_CHROOT)
574 if ( chdir( sandbox ) ) {
579 if ( chroot( sandbox ) ) {
587 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
588 if ( username != NULL || groupname != NULL ) {
589 slap_init_user( username, groupname );
598 if ( module_init() != 0 ) {
600 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
605 if ( slap_schema_init( ) != 0 ) {
607 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
609 Debug( LDAP_DEBUG_ANY,
610 "schema initialization error\n",
617 if ( slap_init( serverMode, serverName ) != 0 ) {
619 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
623 if ( slap_controls_init( ) != 0 ) {
625 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
627 Debug( LDAP_DEBUG_ANY,
628 "controls initialization error\n",
636 /* Library defaults to full certificate checking. This is correct when
637 * a client is verifying a server because all servers should have a
638 * valid cert. But few clients have valid certs, so we want our default
639 * to be no checking. The config file can override this as usual.
642 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
646 if ( slapi_int_initialize() != 0 ) {
648 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
650 Debug( LDAP_DEBUG_ANY,
651 "slapi initialization error\n",
657 #endif /* LDAP_SLAPI */
659 if ( frontend_init() ) {
663 if ( overlay_init() ) {
667 if ( read_config( configfile, 0 ) != 0 ) {
669 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
671 if ( check & CHECK_CONFIG ) {
672 fprintf( stderr, "config check failed\n" );
678 if ( check & CHECK_CONFIG ) {
679 fprintf( stderr, "config check succeeded\n" );
681 check &= ~CHECK_CONFIG;
682 if ( check == CHECK_NONE ) {
688 if ( glue_sub_init( ) != 0 ) {
690 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
692 Debug( LDAP_DEBUG_ANY,
693 "subordinate config error\n",
699 if ( slap_schema_check( ) != 0 ) {
701 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
703 Debug( LDAP_DEBUG_ANY,
704 "schema prep error\n",
712 rc = ldap_pvt_tls_init();
715 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
717 Debug( LDAP_DEBUG_ANY,
718 "main: TLS init failed: %d\n",
722 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
727 void *def_ctx = NULL;
729 /* Save existing default ctx, if any */
730 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &def_ctx );
732 /* Force new ctx to be created */
733 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, NULL );
735 rc = ldap_pvt_tls_init_def_ctx();
738 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
740 Debug( LDAP_DEBUG_ANY,
741 "main: TLS init def ctx failed: %d\n",
745 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
748 /* Retrieve slapd's own ctx */
749 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
750 /* Restore previous ctx */
751 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, def_ctx );
755 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
756 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
759 (void) SIGNAL( SIGPIPE, SIG_IGN );
762 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
764 (void) SIGNAL( SIGINT, slap_sig_shutdown );
765 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
767 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
770 /* SIGBREAK is generated when Ctrl-Break is pressed. */
771 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
775 lutil_detach( no_detach, 0 );
776 #endif /* HAVE_WINSOCK */
783 * FIXME: moved here from slapd_daemon_task()
784 * because back-monitor db_open() needs it
788 if ( slap_startup( NULL ) != 0 ) {
790 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
795 LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
797 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
801 if ( slapd_pid_file != NULL ) {
802 FILE *fp = fopen( slapd_pid_file, "w" );
805 fprintf( fp, "%d\n", (int) getpid() );
809 free(slapd_pid_file);
810 slapd_pid_file = NULL;
814 if ( slapd_args_file != NULL ) {
815 FILE *fp = fopen( slapd_args_file, "w" );
818 for ( i = 0; i < g_argc; i++ ) {
819 fprintf( fp, "%s ", g_argv[i] );
824 free(slapd_args_file);
825 slapd_args_file = NULL;
829 #ifdef HAVE_NT_EVENT_LOG
831 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
836 #ifdef HAVE_NT_SERVICE_MANAGER
837 /* Throw away the event that we used during the startup process. */
839 ldap_pvt_thread_cond_destroy( &started_event );
843 /* remember an error during shutdown */
844 rc |= slap_shutdown( NULL );
847 /* remember an error during destroy */
848 rc |= slap_destroy();
850 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
851 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
852 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
865 #ifdef HAVE_NT_EVENT_LOG
867 lutil_LogStoppedEvent( serverName );
871 LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
873 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
877 #ifdef HAVE_NT_SERVICE_MANAGER
878 lutil_ReportShutdownComplete();
884 slapd_daemon_destroy();
890 lutil_passwd_destroy();
893 ldap_pvt_tls_destroy();
896 if ( slapd_pid_file != NULL ) {
897 unlink( slapd_pid_file );
899 if ( slapd_args_file != NULL ) {
900 unlink( slapd_args_file );
906 mal_dumpleaktrace( leakfile );
916 * Catch and discard terminated child processes, to avoid zombies.
920 wait4child( int sig )
922 int save_errno = errno;
927 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
930 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
936 (void) SIGNAL_REINSTALL( sig, wait4child );
940 #endif /* LDAP_SIGCHLD */
946 * Convert a string to an integer by means of a dispatcher table
947 * if the string is not in the table return the default
951 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
953 int retVal = defaultVal;
956 for (disp = dispatcher; disp->stringVal; disp++) {
958 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
960 retVal = disp->intVal;
969 #endif /* LOG_LOCAL4 */