]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/main.c
reject registrations when back-monitor is not configured
[openldap] / servers / slapd / main.c
index 0270b05bfa77055f493346cb219eb3d90313e8b0..81fbed49f46f3908b573d304eee901a0d1160e66 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2004 The OpenLDAP Foundation.
+ * Copyright 1998-2005 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,6 +27,7 @@
 
 #include <stdio.h>
 
+#include <ac/ctype.h>
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
@@ -133,16 +134,18 @@ static int cnvt_str2int( char *, STRDISP_P, int );
 static int check = CHECK_NONE;
 static int version = 0;
 
+void *slap_tls_ctx;
+
 static int
 slapd_opt_slp( const char *val, void *arg )
 {
 #ifdef HAVE_SLP
        /* NULL is default */
        if ( val == NULL || strcasecmp( val, "on" ) == 0 ) {
-               SLAPD_GLOBAL(register_slp) = 1;
+               slapd_register_slp = 1;
 
        } else if ( strcasecmp( val, "off" ) == 0 ) {
-               SLAPD_GLOBAL(register_slp) = 0;
+               slapd_register_slp = 0;
 
        /* NOTE: add support for URL specification? */
 
@@ -179,7 +182,7 @@ struct option_helper {
        const char      *oh_usage;
 } option_helpers[] = {
        { BER_BVC("slp"),       slapd_opt_slp,  NULL, "slp[={on|off}] enable/disable SLP" },
-       { BER_BVNULL }
+       { BER_BVNULL, 0, NULL, NULL }
 };
 
 static void
@@ -190,11 +193,12 @@ usage( char *name )
        fprintf( stderr,
                "\t-4\t\tIPv4 only\n"
                "\t-6\t\tIPv6 only\n"
-               "\t-T {add|auth|cat|dn|index|passwd|test}\n"
+               "\t-T {acl|add|auth|cat|dn|index|passwd|test}\n"
                "\t\t\tRun in Tool mode\n"
                "\t-c cookie\tSync cookie of consumer\n"
                "\t-d level\tDebug level" "\n"
                "\t-f filename\tConfiguration file\n"
+               "\t-F dir\tConfiguration directory\n"
 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
                "\t-g group\tGroup (id or name) to run as\n"
 #endif
@@ -249,15 +253,11 @@ int main( int argc, char **argv )
        int g_argc = argc;
        char **g_argv = argv;
 
-#ifdef HAVE_NT_SERVICE_MANAGER
-       char            *configfile = ".\\slapd.conf";
-#else
-       char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
-#endif
+       char            *configfile = NULL;
+       char            *configdir = NULL;
        char        *serverName;
        int         serverMode = SLAP_SERVER_MODE;
 
-       struct berval cookie = BER_BVNULL;
        struct sync_cookie *scp = NULL;
        struct sync_cookie *scp_entry = NULL;
 
@@ -272,7 +272,7 @@ int main( int argc, char **argv )
 
        slap_sl_mem_init();
 
-       config_init();  /* sets frontendDB */
+       (void) ldap_pvt_thread_initialize();
 
        serverName = lutil_progname( "slapd", argc, argv );
 
@@ -289,6 +289,7 @@ int main( int argc, char **argv )
        {
                int *i;
                char *newConfigFile;
+               char *newConfigDir;
                char *newUrls;
                char *regService = NULL;
 
@@ -320,11 +321,17 @@ int main( int argc, char **argv )
                        configfile = newConfigFile;
                        Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
                }
+
+               newConfigDir = (char*)lutil_getRegParam( regService, "ConfigDir" );
+               if ( newConfigDir != NULL ) {
+                       configdir = newConfigDir;
+                       Debug ( LDAP_DEBUG_ANY, "new config dir from registry is: %s\n", configdir, 0, 0 );
+               }
        }
 #endif
 
        while ( (i = getopt( argc, argv,
-                            "c:d:f:h:n:o:s:StT:V"
+                            "c:d:f:F:h:n:o:s:StT:V"
 #if LDAP_PF_INET6
                                "46"
 #endif
@@ -354,12 +361,22 @@ int main( int argc, char **argv )
                        break;
 
                case 'c':       /* provide sync cookie, override if exist in replica */
-                       scp = (struct sync_cookie *) ch_calloc( 1, sizeof( struct sync_cookie ));
-                       ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
-                       ber_bvarray_add( &scp->octet_str, &cookie );
-                       slap_parse_sync_cookie( scp );
+                       scp = (struct sync_cookie *) ch_calloc( 1,
+                                                                               sizeof( struct sync_cookie ));
+                       ber_str2bv( optarg, 0, 1, &scp->octet_str );
+                       
+                       /* This only parses out the rid at this point */
+                       slap_parse_sync_cookie( scp, NULL );
+
+                       if ( scp->rid == -1 ) {
+                               Debug( LDAP_DEBUG_ANY,
+                                               "main: invalid cookie \"%s\"\n",
+                                               optarg, 0, 0 );
+                               slap_sync_cookie_free( scp, 1 );
+                               goto destroy;
+                       }
 
-                       LDAP_STAILQ_FOREACH( scp_entry, &SLAPD_GLOBAL(sync_cookie), sc_next ) {
+                       LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
                                if ( scp->rid == scp_entry->rid ) {
                                        Debug( LDAP_DEBUG_ANY,
                                                    "main: duplicated replica id in cookies\n",
@@ -368,13 +385,37 @@ int main( int argc, char **argv )
                                        goto destroy;
                                }
                        }
-                       LDAP_STAILQ_INSERT_TAIL( &SLAPD_GLOBAL(sync_cookie), scp, sc_next );
+                       LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
                        break;
 
                case 'd':       /* set debug level and 'do not detach' flag */
                        no_detach = 1;
 #ifdef LDAP_DEBUG
-                       slap_debug |= atoi( optarg );
+                       if ( optarg != NULL && optarg[ 0 ] != '-' && !isdigit( optarg[ 0 ] ) )
+                       {
+                               int     level;
+
+                               if ( str2loglevel( optarg, &level ) ) {
+                                       fprintf( stderr,
+                                               "unrecognized log level "
+                                               "\"%s\"\n", optarg );
+                                       goto destroy;
+                               }
+
+                               slap_debug |= level;
+                       } else {
+                               int     level;
+                               char    *next = NULL;
+
+                               level = strtol( optarg, &next, 0 );
+                               if ( next == NULL || next[ 0 ] != '\0' ) {
+                                       fprintf( stderr,
+                                               "unrecognized log level "
+                                               "\"%s\"\n", optarg );
+                                       goto destroy;
+                               }
+                               slap_debug |= level;
+                       }
 #else
                        if ( atoi( optarg ) != 0 )
                                fputs( "must compile with LDAP_DEBUG for debugging\n",
@@ -386,6 +427,10 @@ int main( int argc, char **argv )
                        configfile = ch_strdup( optarg );
                        break;
 
+               case 'F':       /* use config dir */
+                       configdir = ch_strdup( optarg );
+                       break;
+
                case 'o': {
                        char            *val = strchr( optarg, '=' );
                        struct berval   opt;
@@ -498,9 +543,6 @@ unhandled_option:;
                }
        }
 
-       (void) ldap_pvt_thread_initialize();
-
-       lutil_set_debug_level( "slapd", slap_debug );
        ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
        ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
        ldif_debug = slap_debug;
@@ -562,65 +604,13 @@ unhandled_option:;
        lutil_passwd_init();
        slap_op_init();
 
-#ifdef SLAPD_MODULES
-       if ( module_init() != 0 ) {
-               rc = 1;
-               SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
-               goto destroy;
-       }
-#endif
-
-       if ( slap_schema_init( ) != 0 ) {
-               Debug( LDAP_DEBUG_ANY,
-                   "schema initialization error\n",
-                   0, 0, 0 );
-
-               goto destroy;
-       }
-
-       if ( slap_init( serverMode, serverName ) != 0 ) {
-               rc = 1;
+       rc = slap_init( serverMode, serverName );
+       if ( rc ) {
                SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
                goto destroy;
        }
 
-       if ( slap_controls_init( ) != 0 ) {
-               Debug( LDAP_DEBUG_ANY,
-                   "controls initialization error\n",
-                   0, 0, 0 );
-
-               goto destroy;
-       }
-
-#ifdef HAVE_TLS
-       /* Library defaults to full certificate checking. This is correct when
-        * a client is verifying a server because all servers should have a
-        * valid cert. But few clients have valid certs, so we want our default
-        * to be no checking. The config file can override this as usual.
-        */
-       rc = 0;
-       (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
-#endif
-
-#ifdef LDAP_SLAPI
-       if ( slapi_int_initialize() != 0 ) {
-               Debug( LDAP_DEBUG_ANY,
-                   "slapi initialization error\n",
-                   0, 0, 0 );
-
-               goto destroy;
-       }
-#endif /* LDAP_SLAPI */
-
-       if ( frontend_init() ) {
-               goto destroy;
-       }
-
-       if ( overlay_init() ) {
-               goto destroy;
-       }
-
-       if ( read_config( configfile, 0 ) != 0 ) {
+       if ( read_config( configfile, configdir ) != 0 ) {
                rc = 1;
                SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
 
@@ -645,6 +635,7 @@ unhandled_option:;
                Debug( LDAP_DEBUG_ANY,
                    "subordinate config error\n",
                    0, 0, 0 );
+
                goto destroy;
        }
 
@@ -686,7 +677,7 @@ unhandled_option:;
                        goto destroy;
                }
                /* Retrieve slapd's own ctx */
-               ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &SLAPD_GLOBAL(tls_ctx) );
+               ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
                /* Restore previous ctx */
                ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, def_ctx );
        }
@@ -703,6 +694,9 @@ unhandled_option:;
 #endif
        (void) SIGNAL( SIGINT, slap_sig_shutdown );
        (void) SIGNAL( SIGTERM, slap_sig_shutdown );
+#ifdef SIGTRAP
+       (void) SIGNAL( SIGTRAP, slap_sig_shutdown );
+#endif
 #ifdef LDAP_SIGCHLD
        (void) SIGNAL( LDAP_SIGCHLD, wait4child );
 #endif
@@ -723,7 +717,7 @@ unhandled_option:;
         * FIXME: moved here from slapd_daemon_task()
         * because back-monitor db_open() needs it
         */
-       time( &SLAPD_GLOBAL(starttime) );
+       time( &starttime );
 
        if ( slap_startup( NULL )  != 0 ) {
                rc = 1;
@@ -734,21 +728,21 @@ unhandled_option:;
        Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
 
 
-       if ( SLAPD_GLOBAL(pid_file) != NULL ) {
-               FILE *fp = fopen( SLAPD_GLOBAL(pid_file), "w" );
+       if ( slapd_pid_file != NULL ) {
+               FILE *fp = fopen( slapd_pid_file, "w" );
 
                if( fp != NULL ) {
                        fprintf( fp, "%d\n", (int) getpid() );
                        fclose( fp );
 
                } else {
-                       free(SLAPD_GLOBAL(pid_file));
-                       SLAPD_GLOBAL(pid_file) = NULL;
+                       free(slapd_pid_file);
+                       slapd_pid_file = NULL;
                }
        }
 
-       if ( SLAPD_GLOBAL(args_file) != NULL ) {
-               FILE *fp = fopen( SLAPD_GLOBAL(args_file), "w" );
+       if ( slapd_args_file != NULL ) {
+               FILE *fp = fopen( slapd_args_file, "w" );
 
                if( fp != NULL ) {
                        for ( i = 0; i < g_argc; i++ ) {
@@ -757,14 +751,15 @@ unhandled_option:;
                        fprintf( fp, "\n" );
                        fclose( fp );
                } else {
-                       free(SLAPD_GLOBAL(args_file));
-                       SLAPD_GLOBAL(args_file) = NULL;
+                       free(slapd_args_file);
+                       slapd_args_file = NULL;
                }
        }
 
 #ifdef HAVE_NT_EVENT_LOG
        if (is_NT_Service)
-       lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
+       lutil_LogStartedEvent( serverName, slap_debug, configfile ?
+               configfile : SLAPD_DEFAULT_CONFIGFILE , urls );
 #endif
 
        rc = slapd_daemon();
@@ -783,9 +778,9 @@ destroy:
        /* remember an error during destroy */
        rc |= slap_destroy();
 
-       while ( !LDAP_STAILQ_EMPTY( &SLAPD_GLOBAL(sync_cookie) )) {
-               scp = LDAP_STAILQ_FIRST( &SLAPD_GLOBAL(sync_cookie) );
-               LDAP_STAILQ_REMOVE_HEAD( &SLAPD_GLOBAL(sync_cookie), sc_next );
+       while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
+               scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
+               LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
                ch_free( scp );
        }
 
@@ -825,15 +820,22 @@ stop:
        ldap_pvt_tls_destroy();
 #endif
 
-       if ( SLAPD_GLOBAL(pid_file) != NULL ) {
-               unlink( SLAPD_GLOBAL(pid_file) );
+       if ( slapd_pid_file != NULL ) {
+               unlink( slapd_pid_file );
        }
-       if ( SLAPD_GLOBAL(args_file) != NULL ) {
-               unlink( SLAPD_GLOBAL(args_file) );
+       if ( slapd_args_file != NULL ) {
+               unlink( slapd_args_file );
        }
 
        config_destroy();
 
+       if ( configfile )
+               ch_free( configfile );
+       if ( configdir )
+               ch_free( configdir );
+       if ( urls )
+               ch_free( urls );
+
 #ifdef CSRIMALLOC
        mal_dumpleaktrace( leakfile );
 #endif