Ejemplo n.º 1
0
/**
 * parse the set command
 */
void parseSet(char* input) {
    printf("%s\n", input);
    if (begins(input, "user ")) {
        debug("set user");
        setUser(input + 5);
        return;
    } else if (begins(input, "pass ")) {

        if (strlen(input + 5) < 1) {
            debug("ask for password");
            askPassword();
            return;
        }
        debug("set password");
        setPassword(input + 5);
        return;
    } else if (begins(input, "debug ")) {
        if (begins(input + 6, "true") || begins(input + 6, "on")) {
            setDebug(true);
        } else if (begins(input + 6, "false") || begins(input + 6, "off")) {
            setDebug(false);
        }
    } else if (begins(input, "pgpass ")) {

        if (begins(input + 7, "true") || begins(input + 7, "on")) {
            setPGPass(true);
        } else if (begins(input + 7, "false") || begins(input + 7, "off")) {
            setPGPass(false);
        }
    }
    debug("no match in set");
    printf("Don't know what you want...\n");
}
Ejemplo n.º 2
0
	virtual int16 selectAnswer() {
		int ans = NO_ANSWER_SELECTED;
		if (_askPassword) {
			ans = askPassword();
		} else
		if (_numVisAnswers == 1) {
			ans = selectAnswer1();
		} else {
			ans = selectAnswerN();
		}
		return ans;
	}
Ejemplo n.º 3
0
void UserInterface::askPasswordAsync(const std::string &passwordName,
                                     const std::string &descr,
                                     const ConfigPasswordKey &key,
                                     const boost::function<void (const std::string &)> &success,
                                     const boost::function<void ()> &failureException)
{
    try {
        success(askPassword(passwordName, descr, key));
    } catch (...) {
        failureException();
    }
}
Ejemplo n.º 4
0
	virtual int16 selectAnswer() {
		int ans = -1;
		if (_askPassword) {
			ans = askPassword();
		} else
		if (_numVisAnswers == 1) {
			ans = selectAnswer1();
		} else {
			ans = selectAnswerN();
		}
		return ans;
	}
Ejemplo n.º 5
0
    int Tool::main( int argc , char ** argv ){
        static StaticObserver staticObserver;
        
        cmdLine.prealloc = false;

        boost::filesystem::path::default_name_check( boost::filesystem::no_check );

        _name = argv[0];

        /* using the same style as db.cpp */
        int command_line_style = (((po::command_line_style::unix_style ^
                                    po::command_line_style::allow_guessing) |
                                   po::command_line_style::allow_long_disguise) ^
                                  po::command_line_style::allow_sticky);
        try {
            po::options_description all_options("all options");
            all_options.add(*_options).add(*_hidden_options);

            po::store( po::command_line_parser( argc , argv ).
                       options(all_options).
                       positional( _positonalOptions ).
                       style(command_line_style).run() , _params );

            po::notify( _params );
        } catch (po::error &e) {
            cerr << "ERROR: " << e.what() << endl << endl;
            printHelp(cerr);
            return EXIT_BADOPTIONS;
        }

        // hide password from ps output
        for (int i=0; i < (argc-1); ++i){
            if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--password")){
                char* arg = argv[i+1];
                while (*arg){
                    *arg++ = 'x';
                }
            }
        }

        if ( _params.count( "help" ) ){
            printHelp(cout);
            return 0;
        }

        if ( _params.count( "verbose" ) ) {
            logLevel = 1;
        }

        for (string s = "vv"; s.length() <= 10; s.append("v")) {
            if (_params.count(s)) {
                logLevel = s.length();
            }
        }
        
        preSetup();

        bool useDirectClient = hasParam( "dbpath" );
        
        if ( ! useDirectClient ) {
            _host = "127.0.0.1";
            if ( _params.count( "host" ) )
                _host = _params["host"].as<string>();

            if ( _params.count( "port" ) )
                _host += ':' + _params["port"].as<string>();
            
            if ( _noconnection ){
                // do nothing
            }
            else if ( _host.find( "," ) == string::npos ){
                DBClientConnection * c = new DBClientConnection( _autoreconnect );
                _conn = c;

                string errmsg;
                if ( ! c->connect( _host , errmsg ) ){
                    cerr << "couldn't connect to [" << _host << "] " << errmsg << endl;
                    return -1;
                }
            }
            else {
                log(1) << "using pairing" << endl;
                DBClientPaired * c = new DBClientPaired();
                _paired = true;
                _conn = c;

                if ( ! c->connect( _host ) ){
                    cerr << "couldn't connect to paired server: " << _host << endl;
                    return -1;
                }
            }
            
            (_usesstdout ? cout : cerr ) << "connected to: " << _host << endl;
        }
        else {
            if ( _params.count( "directoryperdb" ) ) {
                directoryperdb = true;
            }
            assert( lastError.get( true ) );
            Client::initThread("tools");
            _conn = new DBDirectClient();
            _host = "DIRECT";
            static string myDbpath = getParam( "dbpath" );
            dbpath = myDbpath.c_str();
            try {
                acquirePathLock();
            }
            catch ( DBException& e ){
                cerr << endl << "If you are running a mongod on the same "
                    "path you should connect to that instead of direct data "
                    "file access" << endl << endl;
                dbexit( EXIT_CLEAN );
                return -1;
            }

            theFileAllocator().start();
        }

        if ( _params.count( "db" ) )
            _db = _params["db"].as<string>();

        if ( _params.count( "collection" ) )
            _coll = _params["collection"].as<string>();

        if ( _params.count( "username" ) )
            _username = _params["username"].as<string>();

        if ( _params.count( "password" )
             && ( _password.empty() ) ) {
            _password = askPassword();
        }

        if (_params.count("ipv6"))
            enableIPv6();

        int ret = -1;
        try {
            ret = run();
        }
        catch ( DBException& e ){
            cerr << "assertion: " << e.toString() << endl;
            ret = -1;
        }
    
        if ( currentClient.get() )
            currentClient->shutdown();

        if ( useDirectClient )
            dbexit( EXIT_CLEAN );
        return ret;
    }
Ejemplo n.º 6
0
    Status storeGeneralToolOptions(const moe::Environment& params,
                                   const std::vector<std::string>& args) {

        toolGlobalParams.name = args[0];

        storageGlobalParams.prealloc = false;

        // The default value may vary depending on compile options, but for tools
        // we want durability to be disabled.
        storageGlobalParams.dur = false;

        // Set authentication parameters
        if (params.count("authenticationDatabase")) {
            toolGlobalParams.authenticationDatabase =
                params["authenticationDatabase"].as<string>();
        }

        if (params.count("authenticationMechanism")) {
            toolGlobalParams.authenticationMechanism =
                params["authenticationMechanism"].as<string>();
        }

        if (params.count("verbose")) {
            logger::globalLogDomain()->setMinimumLoggedSeverity(logger::LogSeverity::Debug(1));
        }

        for (string s = "vv"; s.length() <= 12; s.append("v")) {
            if (params.count(s)) {
                logger::globalLogDomain()->setMinimumLoggedSeverity(
                        logger::LogSeverity::Debug(s.length()));
            }
        }

        toolGlobalParams.quiet = params.count("quiet");

#ifdef MONGO_SSL
        Status ret = storeSSLClientOptions(params);
        if (!ret.isOK()) {
            return ret;
        }
#endif

        if (args.empty()) {
            return Status(ErrorCodes::InternalError, "Cannot get binary name: argv array is empty");
        }

        // setup binary name
        toolGlobalParams.name = args[0];
        size_t i = toolGlobalParams.name.rfind('/');
        if (i != string::npos) {
            toolGlobalParams.name = toolGlobalParams.name.substr(i + 1);
        }
        toolGlobalParams.db = "test";
        toolGlobalParams.coll = "";
        toolGlobalParams.noconnection = false;

        if (params.count("db"))
            toolGlobalParams.db = params["db"].as<string>();

        if (params.count("collection"))
            toolGlobalParams.coll = params["collection"].as<string>();

        if (params.count("username"))
            toolGlobalParams.username = params["username"].as<string>();

        if (params.count("password")) {
            toolGlobalParams.password = params["password"].as<string>();
            if (toolGlobalParams.password.empty()) {
                toolGlobalParams.password = askPassword();
            }
        }

        if (params.count("ipv6")) {
            enableIPv6();
        }

        toolGlobalParams.dbpath = getParam("dbpath");
        toolGlobalParams.useDirectClient = hasParam("dbpath");
        if (toolGlobalParams.useDirectClient && params.count("journal")) {
            storageGlobalParams.dur = true;
        }

        if (!toolGlobalParams.useDirectClient) {
            toolGlobalParams.connectionString = "127.0.0.1";
            if (params.count("host")) {
                toolGlobalParams.hostSet = true;
                toolGlobalParams.host = params["host"].as<string>();
                toolGlobalParams.connectionString = params["host"].as<string>();
            }

            if (params.count("port")) {
                toolGlobalParams.portSet = true;
                toolGlobalParams.port = params["port"].as<string>();
                toolGlobalParams.connectionString += ':' + params["port"].as<string>();
            }
        }
        else {
            if (params.count("directoryperdb")) {
                storageGlobalParams.directoryperdb = true;
            }

            toolGlobalParams.connectionString = "DIRECT";
        }

        return Status::OK();
    }
Ejemplo n.º 7
0
    int Tool::main( int argc , char ** argv ) {
        static StaticObserver staticObserver;

        cmdLine.prealloc = false;

        // The default value may vary depending on compile options, but for tools
        // we want durability to be disabled.
        cmdLine.dur = false;

#if( BOOST_VERSION >= 104500 )
    boost::filesystem::path::default_name_check( boost::filesystem2::no_check );
#else
    boost::filesystem::path::default_name_check( boost::filesystem::no_check );
#endif

        _name = argv[0];

        /* using the same style as db.cpp */
        int command_line_style = (((po::command_line_style::unix_style ^
                                    po::command_line_style::allow_guessing) |
                                   po::command_line_style::allow_long_disguise) ^
                                  po::command_line_style::allow_sticky);
        try {
            po::options_description all_options("all options");
            all_options.add(*_options).add(*_hidden_options);

            po::store( po::command_line_parser( argc , argv ).
                       options(all_options).
                       positional( _positonalOptions ).
                       style(command_line_style).run() , _params );

            po::notify( _params );
        }
        catch (po::error &e) {
            cerr << "ERROR: " << e.what() << endl << endl;
            printHelp(cerr);
            ::_exit(EXIT_BADOPTIONS);
        }

        // hide password from ps output
        for (int i=0; i < (argc-1); ++i) {
            if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--password")) {
                char* arg = argv[i+1];
                while (*arg) {
                    *arg++ = 'x';
                }
            }
        }

        if ( _params.count( "help" ) ) {
            printHelp(cout);
            ::_exit(0);
        }

        if ( _params.count( "version" ) ) {
            printVersion(cout);
            ::_exit(0);
        }

        if ( _params.count( "verbose" ) ) {
            logLevel = 1;
        }

        for (string s = "vv"; s.length() <= 10; s.append("v")) {
            if (_params.count(s)) {
                logLevel = s.length();
            }
        }


#ifdef MONGO_SSL
        if (_params.count("ssl")) {
            mongo::cmdLine.sslOnNormalPorts = true;
        }
#endif

        preSetup();

        bool useDirectClient = hasParam( "dbpath" );

        if ( ! useDirectClient ) {
            _host = "127.0.0.1";
            if ( _params.count( "host" ) )
                _host = _params["host"].as<string>();

            if ( _params.count( "port" ) )
                _host += ':' + _params["port"].as<string>();

            if ( _noconnection ) {
                // do nothing
            }
            else {
                string errmsg;

                ConnectionString cs = ConnectionString::parse( _host , errmsg );
                if ( ! cs.isValid() ) {
                    cerr << "invalid hostname [" << _host << "] " << errmsg << endl;
                    ::_exit(-1);
                }

                _conn = cs.connect( errmsg );
                if ( ! _conn ) {
                    cerr << "couldn't connect to [" << _host << "] " << errmsg << endl;
                    ::_exit(-1);
                }

                (_usesstdout ? cout : cerr ) << "connected to: " << _host << endl;
            }

        }
        else {
            if ( _params.count( "directoryperdb" ) ) {
                directoryperdb = true;
            }
            verify( lastError.get( true ) );

            if (_params.count("journal")){
                cmdLine.dur = true;
            }

            Client::initThread("tools");
            _conn = new DBDirectClient();
            _host = "DIRECT";
            static string myDbpath = getParam( "dbpath" );
            dbpath = myDbpath.c_str();
            try {
                acquirePathLock();
            }
            catch ( DBException& ) {
                cerr << endl << "If you are running a mongod on the same "
                     "path you should connect to that instead of direct data "
                     "file access" << endl << endl;
                dbexit( EXIT_CLEAN );
                ::_exit(-1);
            }

            FileAllocator::get()->start();

            dur::startup();
        }

        if ( _params.count( "db" ) )
            _db = _params["db"].as<string>();

        if ( _params.count( "collection" ) )
            _coll = _params["collection"].as<string>();

        if ( _params.count( "username" ) )
            _username = _params["username"].as<string>();

        if ( _params.count( "password" )
                && ( _password.empty() ) ) {
            _password = askPassword();
        }

        if (_params.count("ipv6"))
            enableIPv6();

        int ret = -1;
        try {
            ret = run();
        }
        catch ( DBException& e ) {
            cerr << "assertion: " << e.toString() << endl;
            ret = -1;
        }
	catch(const boost::filesystem::filesystem_error &fse) {
	    /*
	      https://jira.mongodb.org/browse/SERVER-2904

	      Simple tools that don't access the database, such as
	      bsondump, aren't throwing DBExceptions, but are throwing
	      boost exceptions.

	      The currently available set of error codes don't seem to match
	      boost documentation.  boost::filesystem::not_found_error
	      (from http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/exception.htm)
	      doesn't seem to exist in our headers.  Also, fse.code() isn't
	      boost::system::errc::no_such_file_or_directory when this
	      happens, as you would expect.  And, determined from
	      experimentation that the command-line argument gets turned into
	      "\\?" instead of "/?" !!!
	     */
#if defined(_WIN32)
	    if (/*(fse.code() == boost::system::errc::no_such_file_or_directory) &&*/
		(fse.path1() == "\\?"))
		printHelp(cerr);
	    else
#endif // _WIN32
		cerr << "error: " << fse.what() << endl;

	    ret = -1;
	}

        if ( currentClient.get() )
            currentClient.get()->shutdown();

        if ( useDirectClient )
            dbexit( EXIT_CLEAN );
        ::_exit(ret);
    }