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 }
163 #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
166 parse_syslog_user( const char *arg, int *syslogUser )
168 static slap_verbmasks syslogUsers[] = {
169 { BER_BVC( "LOCAL0" ), LOG_LOCAL0 },
170 { BER_BVC( "LOCAL1" ), LOG_LOCAL1 },
171 { BER_BVC( "LOCAL2" ), LOG_LOCAL2 },
172 { BER_BVC( "LOCAL3" ), LOG_LOCAL3 },
173 { BER_BVC( "LOCAL4" ), LOG_LOCAL4 },
174 { BER_BVC( "LOCAL5" ), LOG_LOCAL5 },
175 { BER_BVC( "LOCAL6" ), LOG_LOCAL6 },
176 { BER_BVC( "LOCAL7" ), LOG_LOCAL7 },
178 { BER_BVC( "USER" ), LOG_USER },
179 #endif /* LOG_USER */
181 { BER_BVC( "DAEMON" ), LOG_DAEMON },
182 #endif /* LOG_DAEMON */
185 int i = verb_to_mask( optarg, syslogUsers );
187 if ( BER_BVISNULL( &syslogUsers[ i ].word ) ) {
188 Debug( LDAP_DEBUG_ANY,
189 "unrecognized syslog user \"%s\".\n",
194 *syslogUser = syslogUsers[ i ].mask;
198 #endif /* LOG_LOCAL4 */
201 parse_syslog_level( const char *arg, int *levelp )
203 static slap_verbmasks str2syslog_level[] = {
204 { BER_BVC( "EMERG" ), LOG_EMERG },
205 { BER_BVC( "ALERT" ), LOG_ALERT },
206 { BER_BVC( "CRIT" ), LOG_CRIT },
207 { BER_BVC( "ERR" ), LOG_ERR },
208 { BER_BVC( "WARNING" ), LOG_WARNING },
209 { BER_BVC( "NOTICE" ), LOG_NOTICE },
210 { BER_BVC( "INFO" ), LOG_INFO },
211 { BER_BVC( "DEBUG" ), LOG_DEBUG },
214 int i = verb_to_mask( arg, str2syslog_level );
215 if ( BER_BVISNULL( &str2syslog_level[ i ].word ) ) {
216 Debug( LDAP_DEBUG_ANY,
217 "unknown syslog level \"%s\".\n",
222 *levelp = str2syslog_level[ i ].mask;
226 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
229 parse_debug_unknowns( char **unknowns, int *levelp )
231 int i, level, rc = 0;
233 for ( i = 0; unknowns[ i ] != NULL; i++ ) {
235 if ( str2loglevel( unknowns[ i ], &level )) {
237 "unrecognized log level \"%s\"\n", unknowns[ i ] );
247 parse_debug_level( const char *arg, int *levelp, char ***unknowns )
251 if ( arg && arg[ 0 ] != '-' && !isdigit( (unsigned char) arg[ 0 ] ) )
256 levels = ldap_str2charray( arg, "," );
258 for ( i = 0; levels[ i ] != NULL; i++ ) {
261 if ( str2loglevel( levels[ i ], &level ) ) {
262 /* remember this for later */
263 ldap_charray_add( unknowns, levels[ i ] );
265 "unrecognized log level \"%s\" (deferred)\n",
272 ldap_charray_free( levels );
275 if ( lutil_atoix( &level, arg, 0 ) != 0 ) {
277 "unrecognized log level "
297 "usage: %s options\n", name );
299 "\t-4\t\tIPv4 only\n"
300 "\t-6\t\tIPv6 only\n"
301 "\t-T {acl|add|auth|cat|dn|index|passwd|test}\n"
302 "\t\t\tRun in Tool mode\n"
303 "\t-c cookie\tSync cookie of consumer\n"
304 "\t-d level\tDebug level" "\n"
305 "\t-f filename\tConfiguration file\n"
306 "\t-F dir\tConfiguration directory\n"
307 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
308 "\t-g group\tGroup (id or name) to run as\n"
310 "\t-h URLs\t\tList of URLs to serve\n"
312 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
314 "\t-n serverName\tService name\n"
315 "\t-o <opt>[=val] generic means to specify options" );
316 if ( !BER_BVISNULL( &option_helpers[0].oh_name ) ) {
319 fprintf( stderr, "; supported options:\n" );
320 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++) {
321 fprintf( stderr, "\t\t%s\n", option_helpers[i].oh_usage );
324 fprintf( stderr, "\n" );
328 "\t-r directory\tSandbox directory to chroot to\n"
330 "\t-s level\tSyslog level\n"
331 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
332 "\t-u user\t\tUser (id or name) to run as\n"
334 "\t-V\t\tprint version info (-VV only)\n"
338 #ifdef HAVE_NT_SERVICE_MANAGER
339 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
341 int main( int argc, char **argv )
344 int i, no_detach = 0;
347 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
348 char *username = NULL;
349 char *groupname = NULL;
351 #if defined(HAVE_CHROOT)
352 char *sandbox = NULL;
355 int syslogUser = DEFAULT_SYSLOG_USER;
359 char **g_argv = argv;
361 char *configfile = NULL;
362 char *configdir = NULL;
364 int serverMode = SLAP_SERVER_MODE;
366 struct sync_cookie *scp = NULL;
367 struct sync_cookie *scp_entry = NULL;
369 char **debug_unknowns = NULL;
370 char **syslog_unknowns = NULL;
372 char *serverNamePrefix = "";
375 int slapd_pid_file_unlink = 0, slapd_args_file_unlink = 0;
379 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
386 (void) ldap_pvt_thread_initialize();
388 serverName = lutil_progname( "slapd", argc, argv );
390 if ( strcmp( serverName, "slapd" ) ) {
391 for (i=0; tools[i].name; i++) {
392 if ( !strcmp( serverName, tools[i].name ) ) {
393 rc = tools[i].func(argc, argv);
399 #ifdef HAVE_NT_SERVICE_MANAGER
405 char *regService = NULL;
407 if ( is_NT_Service ) {
408 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
409 if ( strcmp(serverName, SERVICE_NAME) )
410 regService = serverName;
413 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
416 Debug( LDAP_DEBUG_ANY,
417 "new debug level from registry is: %d\n", slap_debug, 0, 0 );
420 newUrls = (char *) lutil_getRegParam(regService, "Urls");
425 urls = ch_strdup(newUrls);
426 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
430 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
431 if ( newConfigFile != NULL ) {
432 configfile = newConfigFile;
433 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
436 newConfigDir = (char*)lutil_getRegParam( regService, "ConfigDir" );
437 if ( newConfigDir != NULL ) {
438 configdir = newConfigDir;
439 Debug ( LDAP_DEBUG_ANY, "new config dir from registry is: %s\n", configdir, 0, 0 );
444 while ( (i = getopt( argc, argv,
445 "c:d:f:F:h:n:o:s:tT:V"
452 #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
458 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
465 slap_inet4or6 = AF_INET;
468 slap_inet4or6 = AF_INET6;
472 case 'h': /* listen URLs */
473 if ( urls != NULL ) free( urls );
474 urls = ch_strdup( optarg );
477 case 'c': /* provide sync cookie, override if exist in replica */
478 scp = (struct sync_cookie *) ch_calloc( 1,
479 sizeof( struct sync_cookie ));
480 ber_str2bv( optarg, 0, 1, &scp->octet_str );
482 /* This only parses out the rid at this point */
483 slap_parse_sync_cookie( scp, NULL );
485 if ( scp->rid == -1 ) {
486 Debug( LDAP_DEBUG_ANY,
487 "main: invalid cookie \"%s\"\n",
489 slap_sync_cookie_free( scp, 1 );
493 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
494 if ( scp->rid == scp_entry->rid ) {
495 Debug( LDAP_DEBUG_ANY,
496 "main: duplicated replica id in cookies\n",
498 slap_sync_cookie_free( scp, 1 );
502 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
505 case 'd': { /* set debug level and 'do not detach' flag */
509 if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
516 fputs( "must compile with LDAP_DEBUG for debugging\n",
521 case 'f': /* read config file */
522 configfile = ch_strdup( optarg );
525 case 'F': /* use config dir */
526 configdir = ch_strdup( optarg );
530 char *val = strchr( optarg, '=' );
537 opt.bv_len = ( val - optarg );
541 opt.bv_len = strlen( optarg );
544 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++ ) {
545 if ( ber_bvstrcasecmp( &option_helpers[i].oh_name, &opt ) == 0 ) {
546 assert( option_helpers[i].oh_fnc != NULL );
547 if ( (*option_helpers[i].oh_fnc)( val, option_helpers[i].oh_arg ) == -1 ) {
548 /* we assume the option parsing helper
549 * issues appropriate and self-explanatory
550 * error messages... */
557 if ( BER_BVISNULL( &option_helpers[i].oh_name ) ) {
558 goto unhandled_option;
563 case 's': /* set syslog level */
564 if ( parse_debug_level( optarg, &ldap_syslog, &syslog_unknowns ) ) {
569 #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
571 if ( parse_syslog_level( optarg, &ldap_syslog_level ) ) {
577 case 'l': /* set syslog local user */
578 if ( parse_syslog_user( optarg, &syslogUser ) ) {
583 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
587 if( sandbox ) free(sandbox);
588 sandbox = ch_strdup( optarg );
592 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
593 case 'u': /* user name */
594 if( username ) free(username);
595 username = ch_strdup( optarg );
598 case 'g': /* group name */
599 if( groupname ) free(groupname);
600 groupname = ch_strdup( optarg );
602 #endif /* SETUID && GETUID */
604 case 'n': /* NT service name */
605 serverName = ch_strdup( optarg );
609 /* deprecated; use slaptest instead */
610 fprintf( stderr, "option -t deprecated; "
611 "use slaptest command instead\n" );
612 check |= CHECK_CONFIG;
620 /* try full option string first */
621 for ( i = 0; tools[i].name; i++ ) {
622 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
623 rc = tools[i].func( argc, argv );
628 /* try bits of option string (backward compatibility for single char) */
629 l = strlen( optarg );
630 for ( i = 0; tools[i].name; i++ ) {
631 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
632 rc = tools[i].func( argc, argv );
639 serverNamePrefix = "slap";
640 fprintf( stderr, "program name \"%s%s\" unrecognized; "
641 "aborting...\n", serverNamePrefix, serverName );
647 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
652 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
653 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
654 ldif_debug = slap_debug;
657 fprintf( stderr, "%s\n", Versionstr );
658 if ( version > 1 ) goto stop;
664 logName = ch_strdup( serverName );
667 logName = serverName;
671 openlog( logName, OPENLOG_OPTIONS, syslogUser );
673 openlog( logName, OPENLOG_OPTIONS );
680 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
682 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
684 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
688 #if defined(HAVE_CHROOT)
690 if ( chdir( sandbox ) ) {
695 if ( chroot( sandbox ) ) {
703 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
704 if ( username != NULL || groupname != NULL ) {
705 slap_init_user( username, groupname );
714 rc = ldap_create( &slap_tls_ld );
716 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
721 rc = slap_init( serverMode, serverName );
723 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
727 if ( read_config( configfile, configdir ) != 0 ) {
729 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
731 if ( check & CHECK_CONFIG ) {
732 fprintf( stderr, "config check failed\n" );
738 if ( debug_unknowns ) {
739 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
740 ldap_charray_free( debug_unknowns );
741 debug_unknowns = NULL;
745 if ( syslog_unknowns ) {
746 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
747 ldap_charray_free( syslog_unknowns );
748 syslog_unknowns = NULL;
753 if ( check & CHECK_CONFIG ) {
754 fprintf( stderr, "config check succeeded\n" );
756 check &= ~CHECK_CONFIG;
757 if ( check == CHECK_NONE ) {
763 if ( glue_sub_attach( ) != 0 ) {
764 Debug( LDAP_DEBUG_ANY,
765 "subordinate config error\n",
771 if ( slap_schema_check( ) != 0 ) {
772 Debug( LDAP_DEBUG_ANY,
773 "schema prep error\n",
780 rc = ldap_pvt_tls_init();
782 Debug( LDAP_DEBUG_ANY,
783 "main: TLS init failed: %d\n",
786 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
793 /* Force new ctx to be created */
794 rc = ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
796 /* The ctx's refcount is bumped up here */
797 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
798 load_extop( &slap_EXOP_START_TLS, 0, starttls_extop );
799 } else if ( rc != LDAP_NOT_SUPPORTED ) {
800 Debug( LDAP_DEBUG_ANY,
801 "main: TLS init def ctx failed: %d\n",
804 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
810 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
811 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
814 (void) SIGNAL( SIGPIPE, SIG_IGN );
817 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
819 (void) SIGNAL( SIGINT, slap_sig_shutdown );
820 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
822 (void) SIGNAL( SIGTRAP, slap_sig_shutdown );
825 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
828 /* SIGBREAK is generated when Ctrl-Break is pressed. */
829 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
833 lutil_detach( no_detach, 0 );
834 #endif /* HAVE_WINSOCK */
840 if ( slapd_pid_file != NULL ) {
841 FILE *fp = fopen( slapd_pid_file, "w" );
844 int save_errno = errno;
846 Debug( LDAP_DEBUG_ANY, "unable to open pid file "
849 save_errno, strerror( save_errno ) );
851 free( slapd_pid_file );
852 slapd_pid_file = NULL;
857 fprintf( fp, "%d\n", (int) getpid() );
859 slapd_pid_file_unlink = 1;
862 if ( slapd_args_file != NULL ) {
863 FILE *fp = fopen( slapd_args_file, "w" );
866 int save_errno = errno;
868 Debug( LDAP_DEBUG_ANY, "unable to open args file "
871 save_errno, strerror( save_errno ) );
873 free( slapd_args_file );
874 slapd_args_file = NULL;
880 for ( i = 0; i < g_argc; i++ ) {
881 fprintf( fp, "%s ", g_argv[i] );
885 slapd_args_file_unlink = 1;
889 * FIXME: moved here from slapd_daemon_task()
890 * because back-monitor db_open() needs it
894 if ( slap_startup( NULL ) != 0 ) {
896 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
900 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
902 #ifdef HAVE_NT_EVENT_LOG
904 lutil_LogStartedEvent( serverName, slap_debug, configfile ?
905 configfile : SLAPD_DEFAULT_CONFIGFILE , urls );
910 #ifdef HAVE_NT_SERVICE_MANAGER
911 /* Throw away the event that we used during the startup process. */
913 ldap_pvt_thread_cond_destroy( &started_event );
917 /* remember an error during shutdown */
918 rc |= slap_shutdown( NULL );
921 /* remember an error during destroy */
922 rc |= slap_destroy();
924 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
925 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
926 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
938 supported_feature_destroy();
939 entry_info_destroy();
942 #ifdef HAVE_NT_EVENT_LOG
944 lutil_LogStoppedEvent( serverName );
947 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
950 #ifdef HAVE_NT_SERVICE_MANAGER
951 lutil_ReportShutdownComplete();
957 slapd_daemon_destroy();
963 lutil_passwd_destroy();
966 /* Setting it to itself decreases refcount, allowing it to be freed
967 * when the LD is freed.
970 ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, slap_tls_ctx );
971 ldap_unbind( slap_tls_ld );
973 ldap_pvt_tls_destroy();
976 if ( slapd_pid_file_unlink ) {
977 unlink( slapd_pid_file );
979 if ( slapd_args_file_unlink ) {
980 unlink( slapd_args_file );
986 ch_free( configfile );
988 ch_free( configdir );
993 mal_dumpleaktrace( leakfile );
1003 * Catch and discard terminated child processes, to avoid zombies.
1007 wait4child( int sig )
1009 int save_errno = errno;
1014 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
1017 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
1021 (void) wait( NULL );
1023 (void) SIGNAL_REINSTALL( sig, wait4child );
1027 #endif /* LDAP_SIGCHLD */