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" );
306 Debug( LDAP_DEBUG_ANY,
307 "new debug level from registry is: %d\n", slap_debug, 0, 0 );
310 newUrls = (char *) lutil_getRegParam(regService, "Urls");
315 urls = ch_strdup(newUrls);
316 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
320 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
321 if ( newConfigFile != NULL ) {
322 configfile = newConfigFile;
323 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
328 while ( (i = getopt( argc, argv,
329 "c:d:f:h:n:o:s:StT:V"
339 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
346 slap_inet4or6 = AF_INET;
349 slap_inet4or6 = AF_INET6;
353 case 'h': /* listen URLs */
354 if ( urls != NULL ) free( urls );
355 urls = ch_strdup( optarg );
358 case 'c': /* provide sync cookie, override if exist in replica */
359 scp = (struct sync_cookie *) ch_calloc( 1,
360 sizeof( struct sync_cookie ));
361 ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
362 ber_bvarray_add( &scp->octet_str, &cookie );
363 slap_parse_sync_cookie( scp );
365 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
366 if ( scp->rid == scp_entry->rid ) {
367 Debug( LDAP_DEBUG_ANY,
368 "main: duplicated replica id in cookies\n",
370 slap_sync_cookie_free( scp, 1 );
374 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
377 case 'd': /* set debug level and 'do not detach' flag */
380 slap_debug |= atoi( optarg );
382 if ( atoi( optarg ) != 0 )
383 fputs( "must compile with LDAP_DEBUG for debugging\n",
388 case 'f': /* read config file */
389 configfile = ch_strdup( optarg );
393 char *val = strchr( optarg, '=' );
400 opt.bv_len = ( val - optarg );
404 opt.bv_len = strlen( optarg );
407 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++ ) {
408 if ( ber_bvstrcasecmp( &option_helpers[i].oh_name, &opt ) == 0 ) {
409 assert( option_helpers[i].oh_fnc != NULL );
410 if ( (*option_helpers[i].oh_fnc)( val, option_helpers[i].oh_arg ) == -1 ) {
411 /* we assume the option parsing helper
412 * issues appropriate and self-explanatory
413 * error messages... */
420 if ( BER_BVISNULL( &option_helpers[i].oh_name ) ) {
421 goto unhandled_option;
426 case 's': /* set syslog level */
427 ldap_syslog = atoi( optarg );
431 case 'l': /* set syslog local user */
432 syslogUser = cnvt_str2int( optarg,
433 syslog_types, DEFAULT_SYSLOG_USER );
439 if( sandbox ) free(sandbox);
440 sandbox = ch_strdup( optarg );
444 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
445 case 'u': /* user name */
446 if( username ) free(username);
447 username = ch_strdup( optarg );
450 case 'g': /* group name */
451 if( groupname ) free(groupname);
452 groupname = ch_strdup( optarg );
454 #endif /* SETUID && GETUID */
456 case 'n': /* NT service name */
457 serverName = ch_strdup( optarg );
461 /* deprecated; use slaptest instead */
462 fprintf( stderr, "option -t deprecated; "
463 "use slaptest command instead\n" );
464 check |= CHECK_CONFIG;
472 /* try full option string first */
473 for ( i = 0; tools[i].name; i++ ) {
474 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
475 rc = tools[i].func( argc, argv );
480 /* try bits of option string (backward compatibility for single char) */
481 l = strlen( optarg );
482 for ( i = 0; tools[i].name; i++ ) {
483 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
484 rc = tools[i].func( argc, argv );
491 serverNamePrefix = "slap";
492 fprintf( stderr, "program name \"%s%s\" unrecognized; "
493 "aborting...\n", serverNamePrefix, serverName );
499 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
504 lutil_set_debug_level( "slapd", slap_debug );
505 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
506 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
507 ldif_debug = slap_debug;
510 fprintf( stderr, "%s\n", Versionstr );
511 if ( version > 1 ) goto stop;
517 logName = ch_strdup( serverName );
520 logName = serverName;
524 openlog( logName, OPENLOG_OPTIONS, syslogUser );
526 openlog( logName, OPENLOG_OPTIONS );
533 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
535 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
537 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
541 #if defined(HAVE_CHROOT)
543 if ( chdir( sandbox ) ) {
548 if ( chroot( sandbox ) ) {
556 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
557 if ( username != NULL || groupname != NULL ) {
558 slap_init_user( username, groupname );
567 if ( module_init() != 0 ) {
569 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
574 if ( slap_schema_init( ) != 0 ) {
575 Debug( LDAP_DEBUG_ANY,
576 "schema initialization error\n",
582 if ( slap_init( serverMode, serverName ) != 0 ) {
584 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
588 if ( slap_controls_init( ) != 0 ) {
589 Debug( LDAP_DEBUG_ANY,
590 "controls initialization error\n",
597 /* Library defaults to full certificate checking. This is correct when
598 * a client is verifying a server because all servers should have a
599 * valid cert. But few clients have valid certs, so we want our default
600 * to be no checking. The config file can override this as usual.
603 (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
607 if ( slapi_int_initialize() != 0 ) {
608 Debug( LDAP_DEBUG_ANY,
609 "slapi initialization error\n",
614 #endif /* LDAP_SLAPI */
616 if ( frontend_init() ) {
620 if ( overlay_init() ) {
624 if ( read_config( configfile, 0 ) != 0 ) {
626 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
628 if ( check & CHECK_CONFIG ) {
629 fprintf( stderr, "config check failed\n" );
635 if ( check & CHECK_CONFIG ) {
636 fprintf( stderr, "config check succeeded\n" );
638 check &= ~CHECK_CONFIG;
639 if ( check == CHECK_NONE ) {
645 if ( glue_sub_init( ) != 0 ) {
646 Debug( LDAP_DEBUG_ANY,
647 "subordinate config error\n",
652 if ( slap_schema_check( ) != 0 ) {
653 Debug( LDAP_DEBUG_ANY,
654 "schema prep error\n",
661 rc = ldap_pvt_tls_init();
663 Debug( LDAP_DEBUG_ANY,
664 "main: TLS init failed: %d\n",
667 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
672 void *def_ctx = NULL;
674 /* Save existing default ctx, if any */
675 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &def_ctx );
677 /* Force new ctx to be created */
678 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, NULL );
680 rc = ldap_pvt_tls_init_def_ctx();
682 Debug( LDAP_DEBUG_ANY,
683 "main: TLS init def ctx failed: %d\n",
686 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
689 /* Retrieve slapd's own ctx */
690 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
691 /* Restore previous ctx */
692 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, def_ctx );
696 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
697 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
700 (void) SIGNAL( SIGPIPE, SIG_IGN );
703 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
705 (void) SIGNAL( SIGINT, slap_sig_shutdown );
706 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
708 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
711 /* SIGBREAK is generated when Ctrl-Break is pressed. */
712 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
716 lutil_detach( no_detach, 0 );
717 #endif /* HAVE_WINSOCK */
724 * FIXME: moved here from slapd_daemon_task()
725 * because back-monitor db_open() needs it
729 if ( slap_startup( NULL ) != 0 ) {
731 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
735 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
738 if ( slapd_pid_file != NULL ) {
739 FILE *fp = fopen( slapd_pid_file, "w" );
742 fprintf( fp, "%d\n", (int) getpid() );
746 free(slapd_pid_file);
747 slapd_pid_file = NULL;
751 if ( slapd_args_file != NULL ) {
752 FILE *fp = fopen( slapd_args_file, "w" );
755 for ( i = 0; i < g_argc; i++ ) {
756 fprintf( fp, "%s ", g_argv[i] );
761 free(slapd_args_file);
762 slapd_args_file = NULL;
766 #ifdef HAVE_NT_EVENT_LOG
768 lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
773 #ifdef HAVE_NT_SERVICE_MANAGER
774 /* Throw away the event that we used during the startup process. */
776 ldap_pvt_thread_cond_destroy( &started_event );
780 /* remember an error during shutdown */
781 rc |= slap_shutdown( NULL );
784 /* remember an error during destroy */
785 rc |= slap_destroy();
787 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
788 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
789 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
802 #ifdef HAVE_NT_EVENT_LOG
804 lutil_LogStoppedEvent( serverName );
807 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
810 #ifdef HAVE_NT_SERVICE_MANAGER
811 lutil_ReportShutdownComplete();
817 slapd_daemon_destroy();
823 lutil_passwd_destroy();
826 ldap_pvt_tls_destroy();
829 if ( slapd_pid_file != NULL ) {
830 unlink( slapd_pid_file );
832 if ( slapd_args_file != NULL ) {
833 unlink( slapd_args_file );
839 mal_dumpleaktrace( leakfile );
849 * Catch and discard terminated child processes, to avoid zombies.
853 wait4child( int sig )
855 int save_errno = errno;
860 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
863 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
869 (void) SIGNAL_REINSTALL( sig, wait4child );
873 #endif /* LDAP_SIGCHLD */
879 * Convert a string to an integer by means of a dispatcher table
880 * if the string is not in the table return the default
884 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
886 int retVal = defaultVal;
889 for (disp = dispatcher; disp->stringVal; disp++) {
891 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
893 retVal = disp->intVal;
902 #endif /* LOG_LOCAL4 */