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 slapd_opt_slp( const char *val, void *arg )
113 /* NULL is default */
114 if ( val == NULL || *val == '(' || strcasecmp( val, "on" ) == 0 ) {
115 slapd_register_slp = 1;
116 slapd_slp_attrs = (val != NULL && *val == '(') ? val : NULL;
118 } else if ( strcasecmp( val, "off" ) == 0 ) {
119 slapd_register_slp = 0;
121 /* NOTE: add support for URL specification? */
124 fprintf(stderr, "unrecognized value \"%s\" for SLP option\n", val );
131 fputs( "slapd: SLP support is not available\n", stderr );
137 * Option helper structure:
139 * oh_nam is left-hand part of <option>[=<value>]
140 * oh_fnc is handler function
141 * oh_arg is an optional arg to oh_fnc
142 * oh_usage is the one-line usage string related to the option,
143 * which is assumed to start with <option>[=<value>]
145 * please leave valid options in the structure, and optionally #ifdef
146 * their processing inside the helper, so that reasonable and helpful
147 * error messages can be generated if a disabled option is requested.
149 struct option_helper {
150 struct berval oh_name;
151 int (*oh_fnc)(const char *val, void *arg);
153 const char *oh_usage;
154 } option_helpers[] = {
155 { BER_BVC("slp"), slapd_opt_slp, NULL, "slp[={on|off|(attrs)}] enable/disable SLP using (attrs)" },
156 { BER_BVNULL, 0, NULL, NULL }
159 #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
162 parse_syslog_user( const char *arg, int *syslogUser )
164 static slap_verbmasks syslogUsers[] = {
165 { BER_BVC( "LOCAL0" ), LOG_LOCAL0 },
166 { BER_BVC( "LOCAL1" ), LOG_LOCAL1 },
167 { BER_BVC( "LOCAL2" ), LOG_LOCAL2 },
168 { BER_BVC( "LOCAL3" ), LOG_LOCAL3 },
169 { BER_BVC( "LOCAL4" ), LOG_LOCAL4 },
170 { BER_BVC( "LOCAL5" ), LOG_LOCAL5 },
171 { BER_BVC( "LOCAL6" ), LOG_LOCAL6 },
172 { BER_BVC( "LOCAL7" ), LOG_LOCAL7 },
174 { BER_BVC( "USER" ), LOG_USER },
175 #endif /* LOG_USER */
177 { BER_BVC( "DAEMON" ), LOG_DAEMON },
178 #endif /* LOG_DAEMON */
181 int i = verb_to_mask( arg, syslogUsers );
183 if ( BER_BVISNULL( &syslogUsers[ i ].word ) ) {
184 Debug( LDAP_DEBUG_ANY,
185 "unrecognized syslog user \"%s\".\n",
190 *syslogUser = syslogUsers[ i ].mask;
194 #endif /* LOG_LOCAL4 */
197 parse_syslog_level( const char *arg, int *levelp )
199 static slap_verbmasks str2syslog_level[] = {
200 { BER_BVC( "EMERG" ), LOG_EMERG },
201 { BER_BVC( "ALERT" ), LOG_ALERT },
202 { BER_BVC( "CRIT" ), LOG_CRIT },
203 { BER_BVC( "ERR" ), LOG_ERR },
204 { BER_BVC( "WARNING" ), LOG_WARNING },
205 { BER_BVC( "NOTICE" ), LOG_NOTICE },
206 { BER_BVC( "INFO" ), LOG_INFO },
207 { BER_BVC( "DEBUG" ), LOG_DEBUG },
210 int i = verb_to_mask( arg, str2syslog_level );
211 if ( BER_BVISNULL( &str2syslog_level[ i ].word ) ) {
212 Debug( LDAP_DEBUG_ANY,
213 "unknown syslog level \"%s\".\n",
218 *levelp = str2syslog_level[ i ].mask;
222 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
225 parse_debug_unknowns( char **unknowns, int *levelp )
227 int i, level, rc = 0;
229 for ( i = 0; unknowns[ i ] != NULL; i++ ) {
231 if ( str2loglevel( unknowns[ i ], &level )) {
233 "unrecognized log level \"%s\"\n", unknowns[ i ] );
243 parse_debug_level( const char *arg, int *levelp, char ***unknowns )
247 if ( arg && arg[ 0 ] != '-' && !isdigit( (unsigned char) arg[ 0 ] ) )
252 levels = ldap_str2charray( arg, "," );
254 for ( i = 0; levels[ i ] != NULL; i++ ) {
257 if ( str2loglevel( levels[ i ], &level ) ) {
258 /* remember this for later */
259 ldap_charray_add( unknowns, levels[ i ] );
261 "unrecognized log level \"%s\" (deferred)\n",
268 ldap_charray_free( levels );
271 if ( lutil_atoix( &level, arg, 0 ) != 0 ) {
273 "unrecognized log level "
293 "usage: %s options\n", name );
295 "\t-4\t\tIPv4 only\n"
296 "\t-6\t\tIPv6 only\n"
297 "\t-T {acl|add|auth|cat|dn|index|passwd|test}\n"
298 "\t\t\tRun in Tool mode\n"
299 "\t-c cookie\tSync cookie of consumer\n"
300 "\t-d level\tDebug level" "\n"
301 "\t-f filename\tConfiguration file\n"
302 "\t-F dir\tConfiguration directory\n"
303 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
304 "\t-g group\tGroup (id or name) to run as\n"
306 "\t-h URLs\t\tList of URLs to serve\n"
308 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
310 "\t-n serverName\tService name\n"
311 "\t-o <opt>[=val] generic means to specify options" );
312 if ( !BER_BVISNULL( &option_helpers[0].oh_name ) ) {
315 fprintf( stderr, "; supported options:\n" );
316 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++) {
317 fprintf( stderr, "\t\t%s\n", option_helpers[i].oh_usage );
320 fprintf( stderr, "\n" );
324 "\t-r directory\tSandbox directory to chroot to\n"
326 "\t-s level\tSyslog level\n"
327 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
328 "\t-u user\t\tUser (id or name) to run as\n"
330 "\t-V\t\tprint version info (-VV only)\n"
334 #ifdef HAVE_NT_SERVICE_MANAGER
335 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
337 int main( int argc, char **argv )
340 int i, no_detach = 0;
343 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
344 char *username = NULL;
345 char *groupname = NULL;
347 #if defined(HAVE_CHROOT)
348 char *sandbox = NULL;
351 int syslogUser = SLAP_DEFAULT_SYSLOG_USER;
355 char **g_argv = argv;
357 char *configfile = NULL;
358 char *configdir = NULL;
360 int serverMode = SLAP_SERVER_MODE;
362 struct sync_cookie *scp = NULL;
363 struct sync_cookie *scp_entry = NULL;
365 char **debug_unknowns = NULL;
366 char **syslog_unknowns = NULL;
368 char *serverNamePrefix = "";
371 int slapd_pid_file_unlink = 0, slapd_args_file_unlink = 0;
375 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
382 (void) ldap_pvt_thread_initialize();
384 serverName = lutil_progname( "slapd", argc, argv );
386 if ( strcmp( serverName, "slapd" ) ) {
387 for (i=0; tools[i].name; i++) {
388 if ( !strcmp( serverName, tools[i].name ) ) {
389 rc = tools[i].func(argc, argv);
395 #ifdef HAVE_NT_SERVICE_MANAGER
401 char *regService = NULL;
403 if ( is_NT_Service ) {
404 lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
405 if ( strcmp(serverName, SERVICE_NAME) )
406 regService = serverName;
409 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
412 Debug( LDAP_DEBUG_ANY,
413 "new debug level from registry is: %d\n", slap_debug, 0, 0 );
416 newUrls = (char *) lutil_getRegParam(regService, "Urls");
421 urls = ch_strdup(newUrls);
422 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
426 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
427 if ( newConfigFile != NULL ) {
428 configfile = newConfigFile;
429 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
432 newConfigDir = (char*)lutil_getRegParam( regService, "ConfigDir" );
433 if ( newConfigDir != NULL ) {
434 configdir = newConfigDir;
435 Debug ( LDAP_DEBUG_ANY, "new config dir from registry is: %s\n", configdir, 0, 0 );
440 while ( (i = getopt( argc, argv,
441 "c:d:f:F:h:n:o:s:tT:V"
448 #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
454 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
461 slap_inet4or6 = AF_INET;
464 slap_inet4or6 = AF_INET6;
468 case 'h': /* listen URLs */
469 if ( urls != NULL ) free( urls );
470 urls = ch_strdup( optarg );
473 case 'c': /* provide sync cookie, override if exist in replica */
474 scp = (struct sync_cookie *) ch_calloc( 1,
475 sizeof( struct sync_cookie ));
476 ber_str2bv( optarg, 0, 1, &scp->octet_str );
478 /* This only parses out the rid at this point */
479 slap_parse_sync_cookie( scp, NULL );
481 if ( scp->rid == -1 ) {
482 Debug( LDAP_DEBUG_ANY,
483 "main: invalid cookie \"%s\"\n",
485 slap_sync_cookie_free( scp, 1 );
489 LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
490 if ( scp->rid == scp_entry->rid ) {
491 Debug( LDAP_DEBUG_ANY,
492 "main: duplicated replica id in cookies\n",
494 slap_sync_cookie_free( scp, 1 );
498 LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
501 case 'd': { /* set debug level and 'do not detach' flag */
504 if ( strcmp( optarg, "?" ) == 0 ) {
505 rc = loglevel_print( stdout );
510 if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
517 fputs( "must compile with LDAP_DEBUG for debugging\n",
522 case 'f': /* read config file */
523 configfile = ch_strdup( optarg );
526 case 'F': /* use config dir */
527 configdir = ch_strdup( optarg );
531 char *val = strchr( optarg, '=' );
538 opt.bv_len = ( val - optarg );
542 opt.bv_len = strlen( optarg );
545 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++ ) {
546 if ( ber_bvstrcasecmp( &option_helpers[i].oh_name, &opt ) == 0 ) {
547 assert( option_helpers[i].oh_fnc != NULL );
548 if ( (*option_helpers[i].oh_fnc)( val, option_helpers[i].oh_arg ) == -1 ) {
549 /* we assume the option parsing helper
550 * issues appropriate and self-explanatory
551 * error messages... */
558 if ( BER_BVISNULL( &option_helpers[i].oh_name ) ) {
559 goto unhandled_option;
564 case 's': /* set syslog level */
565 if ( strcmp( optarg, "?" ) == 0 ) {
566 rc = loglevel_print( stdout );
570 if ( parse_debug_level( optarg, &ldap_syslog, &syslog_unknowns ) ) {
575 #if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
577 if ( parse_syslog_level( optarg, &ldap_syslog_level ) ) {
583 case 'l': /* set syslog local user */
584 if ( parse_syslog_user( optarg, &syslogUser ) ) {
589 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
593 if( sandbox ) free(sandbox);
594 sandbox = ch_strdup( optarg );
598 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
599 case 'u': /* user name */
600 if( username ) free(username);
601 username = ch_strdup( optarg );
604 case 'g': /* group name */
605 if( groupname ) free(groupname);
606 groupname = ch_strdup( optarg );
608 #endif /* SETUID && GETUID */
610 case 'n': /* NT service name */
611 serverName = ch_strdup( optarg );
615 /* deprecated; use slaptest instead */
616 fprintf( stderr, "option -t deprecated; "
617 "use slaptest command instead\n" );
618 check |= CHECK_CONFIG;
626 /* try full option string first */
627 for ( i = 0; tools[i].name; i++ ) {
628 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
629 rc = tools[i].func( argc, argv );
634 /* try bits of option string (backward compatibility for single char) */
635 l = strlen( optarg );
636 for ( i = 0; tools[i].name; i++ ) {
637 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
638 rc = tools[i].func( argc, argv );
645 serverNamePrefix = "slap";
646 fprintf( stderr, "program name \"%s%s\" unrecognized; "
647 "aborting...\n", serverNamePrefix, serverName );
653 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
658 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
659 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
660 ldif_debug = slap_debug;
663 fprintf( stderr, "%s\n", Versionstr );
664 if ( version > 1 ) goto stop;
670 logName = ch_strdup( serverName );
673 logName = serverName;
677 openlog( logName, OPENLOG_OPTIONS, syslogUser );
679 openlog( logName, OPENLOG_OPTIONS );
686 Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
688 if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
690 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
694 #if defined(HAVE_CHROOT)
696 if ( chdir( sandbox ) ) {
701 if ( chroot( sandbox ) ) {
709 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
710 if ( username != NULL || groupname != NULL ) {
711 slap_init_user( username, groupname );
720 rc = ldap_create( &slap_tls_ld );
722 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
727 rc = slap_init( serverMode, serverName );
729 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
733 if ( read_config( configfile, configdir ) != 0 ) {
735 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
737 if ( check & CHECK_CONFIG ) {
738 fprintf( stderr, "config check failed\n" );
744 if ( debug_unknowns ) {
745 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
746 ldap_charray_free( debug_unknowns );
747 debug_unknowns = NULL;
751 if ( syslog_unknowns ) {
752 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
753 ldap_charray_free( syslog_unknowns );
754 syslog_unknowns = NULL;
759 if ( check & CHECK_CONFIG ) {
760 fprintf( stderr, "config check succeeded\n" );
762 check &= ~CHECK_CONFIG;
763 if ( check == CHECK_NONE ) {
769 if ( glue_sub_attach( ) != 0 ) {
770 Debug( LDAP_DEBUG_ANY,
771 "subordinate config error\n",
777 if ( slap_schema_check( ) != 0 ) {
778 Debug( LDAP_DEBUG_ANY,
779 "schema prep error\n",
786 rc = ldap_pvt_tls_init();
788 Debug( LDAP_DEBUG_ANY,
789 "main: TLS init failed: %d\n",
792 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
799 /* Force new ctx to be created */
800 rc = ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
802 /* The ctx's refcount is bumped up here */
803 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
804 load_extop( &slap_EXOP_START_TLS, 0, starttls_extop );
805 } else if ( rc != LDAP_NOT_SUPPORTED ) {
806 Debug( LDAP_DEBUG_ANY,
807 "main: TLS init def ctx failed: %d\n",
810 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
816 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
817 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
820 (void) SIGNAL( SIGPIPE, SIG_IGN );
823 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
825 (void) SIGNAL( SIGINT, slap_sig_shutdown );
826 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
828 (void) SIGNAL( SIGTRAP, slap_sig_shutdown );
831 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
834 /* SIGBREAK is generated when Ctrl-Break is pressed. */
835 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
839 lutil_detach( no_detach, 0 );
840 #endif /* HAVE_WINSOCK */
846 if ( slapd_pid_file != NULL ) {
847 FILE *fp = fopen( slapd_pid_file, "w" );
850 int save_errno = errno;
852 Debug( LDAP_DEBUG_ANY, "unable to open pid file "
855 save_errno, strerror( save_errno ) );
857 free( slapd_pid_file );
858 slapd_pid_file = NULL;
863 fprintf( fp, "%d\n", (int) getpid() );
865 slapd_pid_file_unlink = 1;
868 if ( slapd_args_file != NULL ) {
869 FILE *fp = fopen( slapd_args_file, "w" );
872 int save_errno = errno;
874 Debug( LDAP_DEBUG_ANY, "unable to open args file "
877 save_errno, strerror( save_errno ) );
879 free( slapd_args_file );
880 slapd_args_file = NULL;
886 for ( i = 0; i < g_argc; i++ ) {
887 fprintf( fp, "%s ", g_argv[i] );
891 slapd_args_file_unlink = 1;
895 * FIXME: moved here from slapd_daemon_task()
896 * because back-monitor db_open() needs it
900 if ( slap_startup( NULL ) != 0 ) {
902 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
906 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
908 #ifdef HAVE_NT_EVENT_LOG
910 lutil_LogStartedEvent( serverName, slap_debug, configfile ?
911 configfile : SLAPD_DEFAULT_CONFIGFILE , urls );
916 #ifdef HAVE_NT_SERVICE_MANAGER
917 /* Throw away the event that we used during the startup process. */
919 ldap_pvt_thread_cond_destroy( &started_event );
923 /* remember an error during shutdown */
924 rc |= slap_shutdown( NULL );
927 /* remember an error during destroy */
928 rc |= slap_destroy();
930 while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
931 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
932 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
944 supported_feature_destroy();
945 entry_info_destroy();
948 #ifdef HAVE_NT_EVENT_LOG
950 lutil_LogStoppedEvent( serverName );
953 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
956 #ifdef HAVE_NT_SERVICE_MANAGER
957 lutil_ReportShutdownComplete();
963 slapd_daemon_destroy();
969 lutil_passwd_destroy();
973 SSL_CTX_free( slap_tls_ctx );
974 ldap_unbind_ext( slap_tls_ld, NULL, NULL );
976 ldap_pvt_tls_destroy();
979 slap_sasl_regexp_destroy();
981 if ( slapd_pid_file_unlink ) {
982 unlink( slapd_pid_file );
984 if ( slapd_args_file_unlink ) {
985 unlink( slapd_args_file );
991 ch_free( configfile );
993 ch_free( configdir );
997 /* kludge, get symbols referenced */
998 tavl_free( NULL, NULL );
1001 mal_dumpleaktrace( leakfile );
1011 * Catch and discard terminated child processes, to avoid zombies.
1015 wait4child( int sig )
1017 int save_errno = errno;
1022 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
1025 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
1029 (void) wait( NULL );
1031 (void) SIGNAL_REINSTALL( sig, wait4child );
1035 #endif /* LDAP_SIGCHLD */