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 || *val == '(' || strcasecmp( val, "on" ) == 0 ) {
119 slapd_register_slp = 1;
120 slapd_slp_attrs = (val != NULL && *val == '(') ? val : NULL;
122 } else if ( strcasecmp( val, "off" ) == 0 ) {
123 slapd_register_slp = 0;
125 /* NOTE: add support for URL specification? */
128 fprintf(stderr, "unrecognized value \"%s\" for SLP option\n", val );
135 fputs( "slapd: SLP support is not available\n", stderr );
141 * Option helper structure:
143 * oh_nam is left-hand part of <option>[=<value>]
144 * oh_fnc is handler function
145 * oh_arg is an optional arg to oh_fnc
146 * oh_usage is the one-line usage string related to the option,
147 * which is assumed to start with <option>[=<value>]
149 * please leave valid options in the structure, and optionally #ifdef
150 * their processing inside the helper, so that reasonable and helpful
151 * error messages can be generated if a disabled option is requested.
153 struct option_helper {
154 struct berval oh_name;
155 int (*oh_fnc)(const char *val, void *arg);
157 const char *oh_usage;
158 } option_helpers[] = {
159 { BER_BVC("slp"), slapd_opt_slp, NULL, "slp[={on|off|(attrs)}] enable/disable SLP using (attrs)" },
160 { BER_BVNULL, 0, NULL, NULL }
165 parse_syslog_user( const char *arg, int *syslogUser )
167 static slap_verbmasks syslogUsers[] = {
168 { BER_BVC( "LOCAL0" ), LOG_LOCAL0 },
169 { BER_BVC( "LOCAL1" ), LOG_LOCAL1 },
170 { BER_BVC( "LOCAL2" ), LOG_LOCAL2 },
171 { BER_BVC( "LOCAL3" ), LOG_LOCAL3 },
172 { BER_BVC( "LOCAL4" ), LOG_LOCAL4 },
173 { BER_BVC( "LOCAL5" ), LOG_LOCAL5 },
174 { BER_BVC( "LOCAL6" ), LOG_LOCAL6 },
175 { BER_BVC( "LOCAL7" ), LOG_LOCAL7 },
177 { BER_BVC( "USER" ), LOG_USER },
178 #endif /* LOG_USER */
180 { BER_BVC( "DAEMON" ), LOG_DAEMON },
181 #endif /* LOG_DAEMON */
184 int i = verb_to_mask( optarg, syslogUsers );
186 if ( BER_BVISNULL( &syslogUsers[ i ].word ) ) {
187 Debug( LDAP_DEBUG_ANY,
188 "unrecognized syslog user \"%s\".\n",
193 *syslogUser = syslogUsers[ i ].mask;
197 #endif /* LOG_LOCAL4 */
200 parse_syslog_level( const char *arg, int *levelp )
202 static slap_verbmasks str2syslog_level[] = {
203 { BER_BVC( "EMERG" ), LOG_EMERG },
204 { BER_BVC( "ALERT" ), LOG_ALERT },
205 { BER_BVC( "CRIT" ), LOG_CRIT },
206 { BER_BVC( "ERR" ), LOG_ERR },
207 { BER_BVC( "WARNING" ), LOG_WARNING },
208 { BER_BVC( "NOTICE" ), LOG_NOTICE },
209 { BER_BVC( "INFO" ), LOG_INFO },
210 { BER_BVC( "DEBUG" ), LOG_DEBUG },
213 int i = verb_to_mask( arg, str2syslog_level );
214 if ( BER_BVISNULL( &str2syslog_level[ i ].word ) ) {
215 Debug( LDAP_DEBUG_ANY,
216 "unknown syslog level \"%s\".\n",
221 *levelp = str2syslog_level[ i ].mask;
227 parse_debug_unknowns( char **unknowns, int *levelp )
229 int i, level, rc = 0;
231 for ( i = 0; unknowns[ i ] != NULL; i++ ) {
233 if ( str2loglevel( unknowns[ i ], &level )) {
235 "unrecognized log level \"%s\"\n", unknowns[ i ] );
245 parse_debug_level( const char *arg, int *levelp, char ***unknowns )
249 if ( arg && arg[ 0 ] != '-' && !isdigit( (unsigned char) arg[ 0 ] ) )
254 levels = ldap_str2charray( arg, "," );
256 for ( i = 0; levels[ i ] != NULL; i++ ) {
259 if ( str2loglevel( levels[ i ], &level ) ) {
260 /* remember this for later */
261 ldap_charray_add( unknowns, levels[ i ] );
263 "unrecognized log level \"%s\" (deferred)\n",
270 ldap_charray_free( levels );
273 if ( lutil_atoix( &level, arg, 0 ) != 0 ) {
275 "unrecognized log level "
295 "usage: %s options\n", name );
297 "\t-4\t\tIPv4 only\n"
298 "\t-6\t\tIPv6 only\n"
299 "\t-T {acl|add|auth|cat|dn|index|passwd|test}\n"
300 "\t\t\tRun in Tool mode\n"
301 "\t-c cookie\tSync cookie of consumer\n"
302 "\t-d level\tDebug level" "\n"
303 "\t-f filename\tConfiguration file\n"
304 "\t-F dir\tConfiguration directory\n"
305 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
306 "\t-g group\tGroup (id or name) to run as\n"
308 "\t-h URLs\t\tList of URLs to serve\n"
310 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
312 "\t-n serverName\tService name\n"
313 "\t-o <opt>[=val] generic means to specify options" );
314 if ( !BER_BVISNULL( &option_helpers[0].oh_name ) ) {
317 fprintf( stderr, "; supported options:\n" );
318 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++) {
319 fprintf( stderr, "\t\t%s\n", option_helpers[i].oh_usage );
322 fprintf( stderr, "\n" );
326 "\t-r directory\tSandbox directory to chroot to\n"
328 "\t-s level\tSyslog level\n"
329 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
330 "\t-u user\t\tUser (id or name) to run as\n"
332 "\t-V\t\tprint version info (-VV only)\n"
336 #ifdef HAVE_NT_SERVICE_MANAGER
337 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
339 int main( int argc, char **argv )
342 int i, no_detach = 0;
345 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
346 char *username = NULL;
347 char *groupname = NULL;
349 #if defined(HAVE_CHROOT)
350 char *sandbox = NULL;
353 int syslogUser = DEFAULT_SYSLOG_USER;
357 char **g_argv = argv;
359 char *configfile = NULL;
360 char *configdir = NULL;
362 int serverMode = SLAP_SERVER_MODE;
364 struct sync_cookie *scp = NULL;
365 struct sync_cookie *scp_entry = NULL;
367 char **debug_unknowns = NULL;
368 char **syslog_unknowns = NULL;
370 char *serverNamePrefix = "";
373 int slapd_pid_file_unlink = 0, slapd_args_file_unlink = 0;
377 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
384 (void) ldap_pvt_thread_initialize();
386 serverName = lutil_progname( "slapd", argc, argv );
388 if ( strcmp( serverName, "slapd" ) ) {
389 for (i=0; tools[i].name; i++) {
390 if ( !strcmp( serverName, tools[i].name ) ) {
391 rc = tools[i].func(argc, argv);
397 #ifdef HAVE_NT_SERVICE_MANAGER
403 char *regService = NULL;
405 if ( is_NT_Service ) {
406 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
407 if ( strcmp(serverName, SERVICE_NAME) )
408 regService = serverName;
411 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
414 Debug( LDAP_DEBUG_ANY,
415 "new debug level from registry is: %d\n", slap_debug, 0, 0 );
418 newUrls = (char *) lutil_getRegParam(regService, "Urls");
423 urls = ch_strdup(newUrls);
424 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
428 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
429 if ( newConfigFile != NULL ) {
430 configfile = newConfigFile;
431 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
434 newConfigDir = (char*)lutil_getRegParam( regService, "ConfigDir" );
435 if ( newConfigDir != NULL ) {
436 configdir = newConfigDir;
437 Debug ( LDAP_DEBUG_ANY, "new config dir from registry is: %s\n", configdir, 0, 0 );
442 while ( (i = getopt( argc, argv,
443 "c:d:f:F:h:n:o:s:tT:V"
456 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
463 slap_inet4or6 = AF_INET;
466 slap_inet4or6 = AF_INET6;
470 case 'h': /* listen URLs */
471 if ( urls != NULL ) free( urls );
472 urls = ch_strdup( optarg );
475 case 'c': /* provide sync cookie, override if exist in replica */
476 scp = (struct sync_cookie *) ch_calloc( 1,
477 sizeof( struct sync_cookie ));
478 ber_str2bv( optarg, 0, 1, &scp->octet_str );
480 /* This only parses out the rid at this point */
481 slap_parse_sync_cookie( scp, NULL );
483 if ( scp->rid == -1 ) {
484 Debug( LDAP_DEBUG_ANY,
485 "main: invalid cookie \"%s\"\n",
487 slap_sync_cookie_free( scp, 1 );
491 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
492 if ( scp->rid == scp_entry->rid ) {
493 Debug( LDAP_DEBUG_ANY,
494 "main: duplicated replica id in cookies\n",
496 slap_sync_cookie_free( scp, 1 );
500 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
503 case 'd': { /* set debug level and 'do not detach' flag */
507 if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
514 fputs( "must compile with LDAP_DEBUG for debugging\n",
519 case 'f': /* read config file */
520 configfile = ch_strdup( optarg );
523 case 'F': /* use config dir */
524 configdir = ch_strdup( optarg );
528 char *val = strchr( optarg, '=' );
535 opt.bv_len = ( val - optarg );
539 opt.bv_len = strlen( optarg );
542 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++ ) {
543 if ( ber_bvstrcasecmp( &option_helpers[i].oh_name, &opt ) == 0 ) {
544 assert( option_helpers[i].oh_fnc != NULL );
545 if ( (*option_helpers[i].oh_fnc)( val, option_helpers[i].oh_arg ) == -1 ) {
546 /* we assume the option parsing helper
547 * issues appropriate and self-explanatory
548 * error messages... */
555 if ( BER_BVISNULL( &option_helpers[i].oh_name ) ) {
556 goto unhandled_option;
561 case 's': /* set syslog level */
562 if ( parse_debug_level( optarg, &ldap_syslog, &syslog_unknowns ) ) {
567 #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
569 if ( parse_syslog_level( optarg, &ldap_syslog_level ) ) {
573 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
576 case 'l': /* set syslog local user */
577 if ( parse_syslog_user( optarg, &syslogUser ) ) {
585 if( sandbox ) free(sandbox);
586 sandbox = ch_strdup( optarg );
590 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
591 case 'u': /* user name */
592 if( username ) free(username);
593 username = ch_strdup( optarg );
596 case 'g': /* group name */
597 if( groupname ) free(groupname);
598 groupname = ch_strdup( optarg );
600 #endif /* SETUID && GETUID */
602 case 'n': /* NT service name */
603 serverName = ch_strdup( optarg );
607 /* deprecated; use slaptest instead */
608 fprintf( stderr, "option -t deprecated; "
609 "use slaptest command instead\n" );
610 check |= CHECK_CONFIG;
618 /* try full option string first */
619 for ( i = 0; tools[i].name; i++ ) {
620 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
621 rc = tools[i].func( argc, argv );
626 /* try bits of option string (backward compatibility for single char) */
627 l = strlen( optarg );
628 for ( i = 0; tools[i].name; i++ ) {
629 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
630 rc = tools[i].func( argc, argv );
637 serverNamePrefix = "slap";
638 fprintf( stderr, "program name \"%s%s\" unrecognized; "
639 "aborting...\n", serverNamePrefix, serverName );
645 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
650 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
651 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
652 ldif_debug = slap_debug;
655 fprintf( stderr, "%s\n", Versionstr );
656 if ( version > 1 ) goto stop;
662 logName = ch_strdup( serverName );
665 logName = serverName;
669 openlog( logName, OPENLOG_OPTIONS, syslogUser );
671 openlog( logName, OPENLOG_OPTIONS );
678 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
680 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
682 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
686 #if defined(HAVE_CHROOT)
688 if ( chdir( sandbox ) ) {
693 if ( chroot( sandbox ) ) {
701 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
702 if ( username != NULL || groupname != NULL ) {
703 slap_init_user( username, groupname );
712 rc = ldap_create( &slap_tls_ld );
714 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
719 rc = slap_init( serverMode, serverName );
721 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
725 if ( read_config( configfile, configdir ) != 0 ) {
727 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
729 if ( check & CHECK_CONFIG ) {
730 fprintf( stderr, "config check failed\n" );
736 if ( debug_unknowns ) {
737 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
738 ldap_charray_free( debug_unknowns );
739 debug_unknowns = NULL;
743 if ( syslog_unknowns ) {
744 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
745 ldap_charray_free( syslog_unknowns );
746 syslog_unknowns = NULL;
751 if ( check & CHECK_CONFIG ) {
752 fprintf( stderr, "config check succeeded\n" );
754 check &= ~CHECK_CONFIG;
755 if ( check == CHECK_NONE ) {
761 if ( glue_sub_attach( ) != 0 ) {
762 Debug( LDAP_DEBUG_ANY,
763 "subordinate config error\n",
769 if ( slap_schema_check( ) != 0 ) {
770 Debug( LDAP_DEBUG_ANY,
771 "schema prep error\n",
778 rc = ldap_pvt_tls_init();
780 Debug( LDAP_DEBUG_ANY,
781 "main: TLS init failed: %d\n",
784 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
791 /* Force new ctx to be created */
792 rc = ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
794 /* The ctx's refcount is bumped up here */
795 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
796 load_extop( &slap_EXOP_START_TLS, 0, starttls_extop );
797 } else if ( rc != LDAP_NOT_SUPPORTED ) {
798 Debug( LDAP_DEBUG_ANY,
799 "main: TLS init def ctx failed: %d\n",
802 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
808 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
809 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
812 (void) SIGNAL( SIGPIPE, SIG_IGN );
815 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
817 (void) SIGNAL( SIGINT, slap_sig_shutdown );
818 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
820 (void) SIGNAL( SIGTRAP, slap_sig_shutdown );
823 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
826 /* SIGBREAK is generated when Ctrl-Break is pressed. */
827 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
831 lutil_detach( no_detach, 0 );
832 #endif /* HAVE_WINSOCK */
838 if ( slapd_pid_file != NULL ) {
839 FILE *fp = fopen( slapd_pid_file, "w" );
842 int save_errno = errno;
844 Debug( LDAP_DEBUG_ANY, "unable to open pid file "
847 save_errno, strerror( save_errno ) );
849 free( slapd_pid_file );
850 slapd_pid_file = NULL;
855 fprintf( fp, "%d\n", (int) getpid() );
857 slapd_pid_file_unlink = 1;
860 if ( slapd_args_file != NULL ) {
861 FILE *fp = fopen( slapd_args_file, "w" );
864 int save_errno = errno;
866 Debug( LDAP_DEBUG_ANY, "unable to open args file "
869 save_errno, strerror( save_errno ) );
871 free( slapd_args_file );
872 slapd_args_file = NULL;
878 for ( i = 0; i < g_argc; i++ ) {
879 fprintf( fp, "%s ", g_argv[i] );
883 slapd_args_file_unlink = 1;
887 * FIXME: moved here from slapd_daemon_task()
888 * because back-monitor db_open() needs it
892 if ( slap_startup( NULL ) != 0 ) {
894 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
898 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
900 #ifdef HAVE_NT_EVENT_LOG
902 lutil_LogStartedEvent( serverName, slap_debug, configfile ?
903 configfile : SLAPD_DEFAULT_CONFIGFILE , urls );
908 #ifdef HAVE_NT_SERVICE_MANAGER
909 /* Throw away the event that we used during the startup process. */
911 ldap_pvt_thread_cond_destroy( &started_event );
915 /* remember an error during shutdown */
916 rc |= slap_shutdown( NULL );
919 /* remember an error during destroy */
920 rc |= slap_destroy();
922 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
923 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
924 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
936 supported_feature_destroy();
937 entry_info_destroy();
940 #ifdef HAVE_NT_EVENT_LOG
942 lutil_LogStoppedEvent( serverName );
945 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
948 #ifdef HAVE_NT_SERVICE_MANAGER
949 lutil_ReportShutdownComplete();
955 slapd_daemon_destroy();
961 lutil_passwd_destroy();
964 /* Setting it to itself decreases refcount, allowing it to be freed
965 * when the LD is freed.
968 ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, slap_tls_ctx );
969 ldap_unbind( slap_tls_ld );
971 ldap_pvt_tls_destroy();
974 if ( slapd_pid_file_unlink ) {
975 unlink( slapd_pid_file );
977 if ( slapd_args_file_unlink ) {
978 unlink( slapd_args_file );
984 ch_free( configfile );
986 ch_free( configdir );
991 mal_dumpleaktrace( leakfile );
1001 * Catch and discard terminated child processes, to avoid zombies.
1005 wait4child( int sig )
1007 int save_errno = errno;
1012 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
1015 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
1019 (void) wait( NULL );
1021 (void) SIGNAL_REINSTALL( sig, wait4child );
1025 #endif /* LDAP_SIGCHLD */