2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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.
31 #include <ac/socket.h>
32 #include <ac/string.h>
34 #include <ac/unistd.h>
43 #include "slapi/slapi.h"
47 static RETSIGTYPE wait4child( int sig );
50 #ifdef HAVE_NT_SERVICE_MANAGER
51 #define MAIN_RETURN(x) return
52 static struct sockaddr_in bind_addr;
54 #define SERVICE_EXIT( e, n ) do { \
55 if ( is_NT_Service ) { \
56 lutil_ServiceStatus.dwWin32ExitCode = (e); \
57 lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
62 #define SERVICE_EXIT( e, n )
63 #define MAIN_RETURN(x) return(x)
66 typedef int (MainFunc) LDAP_P(( int argc, char *argv[] ));
67 extern MainFunc slapadd, slapcat, slapdn, slapindex, slappasswd,
68 slaptest, slapauth, slapacl;
77 {"slapindex", slapindex},
78 {"slappasswd", slappasswd},
79 {"slaptest", slaptest},
80 {"slapauth", slapauth},
82 /* NOTE: new tools must be added in chronological order,
83 * not in alphabetical order, because for backwards
84 * compatibility name[4] is used to identify the
85 * tools; so name[4]=='a' must refer to "slapadd" and
86 * not to "slapauth". Alphabetical order can be used
87 * for tools whose name[4] is not used yet */
92 * when more than one slapd is running on one machine, each one might have
93 * it's own LOCAL for syslogging and must have its own pid/args files
96 #ifndef HAVE_MKVERSION
97 const char Versionstr[] =
98 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
101 #define CHECK_NONE 0x00
102 #define CHECK_CONFIG 0x01
103 static int check = CHECK_NONE;
104 static int version = 0;
110 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
111 #endif /* LOG_LOCAL4 */
114 slapd_opt_slp( const char *val, void *arg )
117 /* NULL is default */
118 if ( val == NULL || strcasecmp( val, "on" ) == 0 ) {
119 slapd_register_slp = 1;
121 } else if ( strcasecmp( val, "off" ) == 0 ) {
122 slapd_register_slp = 0;
124 /* NOTE: add support for URL specification? */
127 fprintf(stderr, "unrecognized value \"%s\" for SLP option\n", val );
134 fputs( "slapd: SLP support is not available\n", stderr );
140 * Option helper structure:
142 * oh_nam is left-hand part of <option>[=<value>]
143 * oh_fnc is handler function
144 * oh_arg is an optional arg to oh_fnc
145 * oh_usage is the one-line usage string related to the option,
146 * which is assumed to start with <option>[=<value>]
148 * please leave valid options in the structure, and optionally #ifdef
149 * their processing inside the helper, so that reasonable and helpful
150 * error messages can be generated if a disabled option is requested.
152 struct option_helper {
153 struct berval oh_name;
154 int (*oh_fnc)(const char *val, void *arg);
156 const char *oh_usage;
157 } option_helpers[] = {
158 { BER_BVC("slp"), slapd_opt_slp, NULL, "slp[={on|off}] enable/disable SLP" },
159 { BER_BVNULL, 0, NULL, NULL }
164 parse_syslog_user( const char *arg, int *syslogUser )
166 static slap_verbmasks syslogUsers[] = {
167 { BER_BVC( "LOCAL0" ), LOG_LOCAL0 },
168 { BER_BVC( "LOCAL1" ), LOG_LOCAL1 },
169 { BER_BVC( "LOCAL2" ), LOG_LOCAL2 },
170 { BER_BVC( "LOCAL3" ), LOG_LOCAL3 },
171 { BER_BVC( "LOCAL4" ), LOG_LOCAL4 },
172 { BER_BVC( "LOCAL5" ), LOG_LOCAL5 },
173 { BER_BVC( "LOCAL6" ), LOG_LOCAL6 },
174 { BER_BVC( "LOCAL7" ), LOG_LOCAL7 },
176 { BER_BVC( "USER" ), LOG_USER },
177 #endif /* LOG_USER */
179 { BER_BVC( "DAEMON" ), LOG_DAEMON },
180 #endif /* LOG_DAEMON */
183 int i = verb_to_mask( optarg, syslogUsers );
185 if ( BER_BVISNULL( &syslogUsers[ i ].word ) ) {
186 Debug( LDAP_DEBUG_ANY,
187 "unrecognized syslog user \"%s\".\n",
192 *syslogUser = syslogUsers[ i ].mask;
196 #endif /* LOG_LOCAL4 */
199 parse_syslog_level( const char *arg, int *levelp )
201 static slap_verbmasks str2syslog_level[] = {
202 { BER_BVC( "EMERG" ), LOG_EMERG },
203 { BER_BVC( "ALERT" ), LOG_ALERT },
204 { BER_BVC( "CRIT" ), LOG_CRIT },
205 { BER_BVC( "ERR" ), LOG_ERR },
206 { BER_BVC( "WARNING" ), LOG_WARNING },
207 { BER_BVC( "NOTICE" ), LOG_NOTICE },
208 { BER_BVC( "INFO" ), LOG_INFO },
209 { BER_BVC( "DEBUG" ), LOG_DEBUG },
212 int i = verb_to_mask( arg, str2syslog_level );
213 if ( BER_BVISNULL( &str2syslog_level[ i ].word ) ) {
214 Debug( LDAP_DEBUG_ANY,
215 "unknown syslog level \"%s\".\n",
220 *levelp = str2syslog_level[ i ].mask;
226 parse_debug_unknowns( char **unknowns, int *levelp )
228 int i, level, rc = 0;
230 for ( i = 0; unknowns[ i ] != NULL; i++ ) {
232 if ( str2loglevel( unknowns[ i ], &level )) {
234 "unrecognized log level \"%s\"\n", unknowns[ i ] );
244 parse_debug_level( const char *arg, int *levelp, char ***unknowns )
248 if ( arg && arg[ 0 ] != '-' && !isdigit( (unsigned char) arg[ 0 ] ) )
253 levels = ldap_str2charray( arg, "," );
255 for ( i = 0; levels[ i ] != NULL; i++ ) {
258 if ( str2loglevel( levels[ i ], &level ) ) {
259 /* remember this for later */
260 ldap_charray_add( unknowns, levels[ i ] );
262 "unrecognized log level \"%s\" (deferred)\n",
269 ldap_charray_free( levels );
272 if ( lutil_atoix( &level, arg, 0 ) != 0 ) {
274 "unrecognized log level "
294 "usage: %s options\n", name );
296 "\t-4\t\tIPv4 only\n"
297 "\t-6\t\tIPv6 only\n"
298 "\t-T {acl|add|auth|cat|dn|index|passwd|test}\n"
299 "\t\t\tRun in Tool mode\n"
300 "\t-c cookie\tSync cookie of consumer\n"
301 "\t-d level\tDebug level" "\n"
302 "\t-f filename\tConfiguration file\n"
303 "\t-F dir\tConfiguration directory\n"
304 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
305 "\t-g group\tGroup (id or name) to run as\n"
307 "\t-h URLs\t\tList of URLs to serve\n"
309 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
311 "\t-n serverName\tService name\n"
312 "\t-o <opt>[=val] generic means to specify options" );
313 if ( !BER_BVISNULL( &option_helpers[0].oh_name ) ) {
316 fprintf( stderr, "; supported options:\n" );
317 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++) {
318 fprintf( stderr, "\t\t%s\n", option_helpers[i].oh_usage );
321 fprintf( stderr, "\n" );
325 "\t-r directory\tSandbox directory to chroot to\n"
327 "\t-s level\tSyslog level\n"
328 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
329 "\t-u user\t\tUser (id or name) to run as\n"
331 "\t-V\t\tprint version info (-VV only)\n"
335 #ifdef HAVE_NT_SERVICE_MANAGER
336 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
338 int main( int argc, char **argv )
341 int i, no_detach = 0;
344 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
345 char *username = NULL;
346 char *groupname = NULL;
348 #if defined(HAVE_CHROOT)
349 char *sandbox = NULL;
352 int syslogUser = DEFAULT_SYSLOG_USER;
356 char **g_argv = argv;
358 char *configfile = NULL;
359 char *configdir = NULL;
361 int serverMode = SLAP_SERVER_MODE;
363 struct sync_cookie *scp = NULL;
364 struct sync_cookie *scp_entry = NULL;
366 char **debug_unknowns = NULL;
367 char **syslog_unknowns = NULL;
369 char *serverNamePrefix = "";
372 int slapd_pid_file_unlink = 0, slapd_args_file_unlink = 0;
376 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
383 (void) ldap_pvt_thread_initialize();
385 serverName = lutil_progname( "slapd", argc, argv );
387 if ( strcmp( serverName, "slapd" ) ) {
388 for (i=0; tools[i].name; i++) {
389 if ( !strcmp( serverName, tools[i].name ) ) {
390 rc = tools[i].func(argc, argv);
396 #ifdef HAVE_NT_SERVICE_MANAGER
402 char *regService = NULL;
404 if ( is_NT_Service ) {
405 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
406 if ( strcmp(serverName, SERVICE_NAME) )
407 regService = serverName;
410 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
413 Debug( LDAP_DEBUG_ANY,
414 "new debug level from registry is: %d\n", slap_debug, 0, 0 );
417 newUrls = (char *) lutil_getRegParam(regService, "Urls");
422 urls = ch_strdup(newUrls);
423 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
427 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
428 if ( newConfigFile != NULL ) {
429 configfile = newConfigFile;
430 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
433 newConfigDir = (char*)lutil_getRegParam( regService, "ConfigDir" );
434 if ( newConfigDir != NULL ) {
435 configdir = newConfigDir;
436 Debug ( LDAP_DEBUG_ANY, "new config dir from registry is: %s\n", configdir, 0, 0 );
441 while ( (i = getopt( argc, argv,
442 "c:d:f:F:h:n:o:s:tT:V"
455 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
462 slap_inet4or6 = AF_INET;
465 slap_inet4or6 = AF_INET6;
469 case 'h': /* listen URLs */
470 if ( urls != NULL ) free( urls );
471 urls = ch_strdup( optarg );
474 case 'c': /* provide sync cookie, override if exist in replica */
475 scp = (struct sync_cookie *) ch_calloc( 1,
476 sizeof( struct sync_cookie ));
477 ber_str2bv( optarg, 0, 1, &scp->octet_str );
479 /* This only parses out the rid at this point */
480 slap_parse_sync_cookie( scp, NULL );
482 if ( scp->rid == -1 ) {
483 Debug( LDAP_DEBUG_ANY,
484 "main: invalid cookie \"%s\"\n",
486 slap_sync_cookie_free( scp, 1 );
490 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
491 if ( scp->rid == scp_entry->rid ) {
492 Debug( LDAP_DEBUG_ANY,
493 "main: duplicated replica id in cookies\n",
495 slap_sync_cookie_free( scp, 1 );
499 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
502 case 'd': { /* set debug level and 'do not detach' flag */
506 if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
513 fputs( "must compile with LDAP_DEBUG for debugging\n",
518 case 'f': /* read config file */
519 configfile = ch_strdup( optarg );
522 case 'F': /* use config dir */
523 configdir = ch_strdup( optarg );
527 char *val = strchr( optarg, '=' );
534 opt.bv_len = ( val - optarg );
538 opt.bv_len = strlen( optarg );
541 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++ ) {
542 if ( ber_bvstrcasecmp( &option_helpers[i].oh_name, &opt ) == 0 ) {
543 assert( option_helpers[i].oh_fnc != NULL );
544 if ( (*option_helpers[i].oh_fnc)( val, option_helpers[i].oh_arg ) == -1 ) {
545 /* we assume the option parsing helper
546 * issues appropriate and self-explanatory
547 * error messages... */
554 if ( BER_BVISNULL( &option_helpers[i].oh_name ) ) {
555 goto unhandled_option;
560 case 's': /* set syslog level */
561 if ( parse_debug_level( optarg, &ldap_syslog, &syslog_unknowns ) ) {
566 #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
568 if ( parse_syslog_level( optarg, &ldap_syslog_level ) ) {
572 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
575 case 'l': /* set syslog local user */
576 if ( parse_syslog_user( optarg, &syslogUser ) ) {
584 if( sandbox ) free(sandbox);
585 sandbox = ch_strdup( optarg );
589 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
590 case 'u': /* user name */
591 if( username ) free(username);
592 username = ch_strdup( optarg );
595 case 'g': /* group name */
596 if( groupname ) free(groupname);
597 groupname = ch_strdup( optarg );
599 #endif /* SETUID && GETUID */
601 case 'n': /* NT service name */
602 serverName = ch_strdup( optarg );
606 /* deprecated; use slaptest instead */
607 fprintf( stderr, "option -t deprecated; "
608 "use slaptest command instead\n" );
609 check |= CHECK_CONFIG;
617 /* try full option string first */
618 for ( i = 0; tools[i].name; i++ ) {
619 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
620 rc = tools[i].func( argc, argv );
625 /* try bits of option string (backward compatibility for single char) */
626 l = strlen( optarg );
627 for ( i = 0; tools[i].name; i++ ) {
628 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
629 rc = tools[i].func( argc, argv );
636 serverNamePrefix = "slap";
637 fprintf( stderr, "program name \"%s%s\" unrecognized; "
638 "aborting...\n", serverNamePrefix, serverName );
644 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
649 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
650 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
651 ldif_debug = slap_debug;
654 fprintf( stderr, "%s\n", Versionstr );
655 if ( version > 1 ) goto stop;
661 logName = ch_strdup( serverName );
664 logName = serverName;
668 openlog( logName, OPENLOG_OPTIONS, syslogUser );
670 openlog( logName, OPENLOG_OPTIONS );
677 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
679 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
681 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
685 #if defined(HAVE_CHROOT)
687 if ( chdir( sandbox ) ) {
692 if ( chroot( sandbox ) ) {
700 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
701 if ( username != NULL || groupname != NULL ) {
702 slap_init_user( username, groupname );
711 rc = ldap_create( &slap_tls_ld );
713 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
718 rc = slap_init( serverMode, serverName );
720 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
724 if ( read_config( configfile, configdir ) != 0 ) {
726 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
728 if ( check & CHECK_CONFIG ) {
729 fprintf( stderr, "config check failed\n" );
735 if ( debug_unknowns ) {
736 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
737 ldap_charray_free( debug_unknowns );
738 debug_unknowns = NULL;
742 if ( syslog_unknowns ) {
743 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
744 ldap_charray_free( syslog_unknowns );
745 syslog_unknowns = NULL;
750 if ( check & CHECK_CONFIG ) {
751 fprintf( stderr, "config check succeeded\n" );
753 check &= ~CHECK_CONFIG;
754 if ( check == CHECK_NONE ) {
760 if ( glue_sub_attach( ) != 0 ) {
761 Debug( LDAP_DEBUG_ANY,
762 "subordinate config error\n",
768 if ( slap_schema_check( ) != 0 ) {
769 Debug( LDAP_DEBUG_ANY,
770 "schema prep error\n",
777 rc = ldap_pvt_tls_init();
779 Debug( LDAP_DEBUG_ANY,
780 "main: TLS init failed: %d\n",
783 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
790 /* Force new ctx to be created */
791 rc = ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
793 /* The ctx's refcount is bumped up here */
794 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
795 load_extop( &slap_EXOP_START_TLS, 0, starttls_extop );
796 } else if ( rc != LDAP_NOT_SUPPORTED ) {
797 Debug( LDAP_DEBUG_ANY,
798 "main: TLS init def ctx failed: %d\n",
801 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
807 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
808 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
811 (void) SIGNAL( SIGPIPE, SIG_IGN );
814 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
816 (void) SIGNAL( SIGINT, slap_sig_shutdown );
817 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
819 (void) SIGNAL( SIGTRAP, slap_sig_shutdown );
822 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
825 /* SIGBREAK is generated when Ctrl-Break is pressed. */
826 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
830 lutil_detach( no_detach, 0 );
831 #endif /* HAVE_WINSOCK */
837 if ( slapd_pid_file != NULL ) {
838 FILE *fp = fopen( slapd_pid_file, "w" );
841 int save_errno = errno;
843 Debug( LDAP_DEBUG_ANY, "unable to open pid file "
846 save_errno, strerror( save_errno ) );
848 free( slapd_pid_file );
849 slapd_pid_file = NULL;
854 fprintf( fp, "%d\n", (int) getpid() );
856 slapd_pid_file_unlink = 1;
859 if ( slapd_args_file != NULL ) {
860 FILE *fp = fopen( slapd_args_file, "w" );
863 int save_errno = errno;
865 Debug( LDAP_DEBUG_ANY, "unable to open args file "
868 save_errno, strerror( save_errno ) );
870 free( slapd_args_file );
871 slapd_args_file = NULL;
877 for ( i = 0; i < g_argc; i++ ) {
878 fprintf( fp, "%s ", g_argv[i] );
882 slapd_args_file_unlink = 1;
886 * FIXME: moved here from slapd_daemon_task()
887 * because back-monitor db_open() needs it
891 if ( slap_startup( NULL ) != 0 ) {
893 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
897 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
899 #ifdef HAVE_NT_EVENT_LOG
901 lutil_LogStartedEvent( serverName, slap_debug, configfile ?
902 configfile : SLAPD_DEFAULT_CONFIGFILE , urls );
907 #ifdef HAVE_NT_SERVICE_MANAGER
908 /* Throw away the event that we used during the startup process. */
910 ldap_pvt_thread_cond_destroy( &started_event );
914 /* remember an error during shutdown */
915 rc |= slap_shutdown( NULL );
918 /* remember an error during destroy */
919 rc |= slap_destroy();
921 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
922 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
923 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
935 supported_feature_destroy();
936 entry_info_destroy();
939 #ifdef HAVE_NT_EVENT_LOG
941 lutil_LogStoppedEvent( serverName );
944 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
947 #ifdef HAVE_NT_SERVICE_MANAGER
948 lutil_ReportShutdownComplete();
954 slapd_daemon_destroy();
960 lutil_passwd_destroy();
963 /* Setting it to itself decreases refcount, allowing it to be freed
964 * when the LD is freed.
967 ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, slap_tls_ctx );
968 ldap_unbind( slap_tls_ld );
970 ldap_pvt_tls_destroy();
973 if ( slapd_pid_file_unlink ) {
974 unlink( slapd_pid_file );
976 if ( slapd_args_file_unlink ) {
977 unlink( slapd_args_file );
983 ch_free( configfile );
985 ch_free( configdir );
990 mal_dumpleaktrace( leakfile );
1000 * Catch and discard terminated child processes, to avoid zombies.
1004 wait4child( int sig )
1006 int save_errno = errno;
1011 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
1014 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
1018 (void) wait( NULL );
1020 (void) SIGNAL_REINSTALL( sig, wait4child );
1024 #endif /* LDAP_SIGCHLD */