~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to src/mongo/tools/tool.cpp

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#include "pcrecpp.h"
25
25
 
 
26
#include "mongo/client/sasl_client_authenticate.h"
26
27
#include "mongo/db/namespace_details.h"
27
28
#include "mongo/util/file_allocator.h"
28
29
#include "mongo/util/password.h"
64
65
 
65
66
            ("username,u",po::value<string>(), "username" )
66
67
            ("password,p", new PasswordValue( &_password ), "password" )
 
68
            ("authenticationDatabase",
 
69
             po::value<string>(&_authenticationDatabase)->default_value(""),
 
70
             "user source (defaults to dbname)" )
 
71
            ("authenticationMechanism",
 
72
             po::value<string>(&_authenticationMechanism)->default_value("MONGODB-CR"),
 
73
             "authentication mechanism")
67
74
            ;
68
75
 
69
76
        if ( access & LOCAL_SERVER )
72
79
             "files in the given path, instead of connecting to a mongod  "
73
80
             "server - needs to lock the data directory, so cannot be "
74
81
             "used if a mongod is currently accessing the same path" )
75
 
            ("directoryperdb", "if dbpath specified, each db is in a separate directory" )
76
 
            ("journal", "enable journaling" )
 
82
            ("directoryperdb", "each db is in a separate directly (relevant only if dbpath specified)" )
 
83
            ("journal", "enable journaling (relevant only if dbpath specified)" )
77
84
            ;
78
85
 
79
86
        if ( access & SPECIFY_DBCOL )
118
125
        // we want durability to be disabled.
119
126
        cmdLine.dur = false;
120
127
 
121
 
#if( BOOST_VERSION >= 104500 )
122
 
    boost::filesystem::path::default_name_check( boost::filesystem2::no_check );
123
 
#else
124
 
    boost::filesystem::path::default_name_check( boost::filesystem::no_check );
125
 
#endif
126
 
 
127
128
        _name = argv[0];
128
129
 
129
130
        /* using the same style as db.cpp */
241
242
                cerr << endl << "If you are running a mongod on the same "
242
243
                     "path you should connect to that instead of direct data "
243
244
                     "file access" << endl << endl;
244
 
                dbexit( EXIT_CLEAN );
245
 
                ::_exit(-1);
 
245
                dbexit( EXIT_FS );
 
246
                ::_exit(EXIT_FAILURE);
246
247
            }
247
248
 
248
249
            FileAllocator::get()->start();
269
270
 
270
271
        int ret = -1;
271
272
        try {
 
273
            if (!useDirectClient && !_noconnection)
 
274
                auth();
272
275
            ret = run();
273
276
        }
274
277
        catch ( DBException& e ) {
275
278
            cerr << "assertion: " << e.toString() << endl;
276
279
            ret = -1;
277
280
        }
278
 
        catch(const boost::filesystem::filesystem_error &fse) {
279
 
            /*
280
 
              https://jira.mongodb.org/browse/SERVER-2904
281
 
 
282
 
              Simple tools that don't access the database, such as
283
 
              bsondump, aren't throwing DBExceptions, but are throwing
284
 
              boost exceptions.
285
 
 
286
 
              The currently available set of error codes don't seem to match
287
 
              boost documentation.  boost::filesystem::not_found_error
288
 
              (from http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/exception.htm)
289
 
              doesn't seem to exist in our headers.  Also, fse.code() isn't
290
 
              boost::system::errc::no_such_file_or_directory when this
291
 
              happens, as you would expect.  And, determined from
292
 
              experimentation that the command-line argument gets turned into
293
 
              "\\?" instead of "/?" !!!
294
 
             */
 
281
        catch(const boost::filesystem::filesystem_error &fse) {
 
282
            /*
 
283
              https://jira.mongodb.org/browse/SERVER-2904
 
284
 
 
285
              Simple tools that don't access the database, such as
 
286
              bsondump, aren't throwing DBExceptions, but are throwing
 
287
              boost exceptions.
 
288
 
 
289
              The currently available set of error codes don't seem to match
 
290
              boost documentation.  boost::filesystem::not_found_error
 
291
              (from http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/exception.htm)
 
292
              doesn't seem to exist in our headers.  Also, fse.code() isn't
 
293
              boost::system::errc::no_such_file_or_directory when this
 
294
              happens, as you would expect.  And, determined from
 
295
              experimentation that the command-line argument gets turned into
 
296
              "\\?" instead of "/?" !!!
 
297
             */
295
298
#if defined(_WIN32)
296
 
            if (/*(fse.code() == boost::system::errc::no_such_file_or_directory) &&*/
297
 
                (fse.path1() == "\\?"))
298
 
                printHelp(cerr);
299
 
            else
 
299
            if (/*(fse.code() == boost::system::errc::no_such_file_or_directory) &&*/
 
300
                (fse.path1() == "\\?"))
 
301
                printHelp(cerr);
 
302
            else
300
303
#endif // _WIN32
301
 
                cerr << "error: " << fse.what() << endl;
 
304
                cerr << "error: " << fse.what() << endl;
302
305
 
303
 
            ret = -1;
304
 
        }
 
306
            ret = -1;
 
307
        }
305
308
 
306
309
        if ( currentClient.get() )
307
310
            currentClient.get()->shutdown();
316
319
 
317
320
    DBClientBase& Tool::conn( bool slaveIfPaired ) {
318
321
        if ( slaveIfPaired && _conn->type() == ConnectionString::SET ) {
319
 
            if (!_slaveConn)
320
 
                _slaveConn = &((DBClientReplicaSet*)_conn)->slaveConn();
 
322
            if (!_slaveConn) {
 
323
                DBClientReplicaSet* rs = static_cast<DBClientReplicaSet*>(_conn);
 
324
                _slaveConn = &rs->slaveConn();
 
325
            }
321
326
            return *_slaveConn;
322
327
        }
323
328
        return *_conn;
401
406
        throw UserException( 9998 , "you need to specify fields" );
402
407
    }
403
408
 
 
409
    std::string Tool::getAuthenticationDatabase() {
 
410
        if (!_authenticationDatabase.empty()) {
 
411
            return _authenticationDatabase;
 
412
        }
 
413
 
 
414
        if (!_db.empty()) {
 
415
            return _db;
 
416
        }
 
417
 
 
418
        return "admin";
 
419
    }
 
420
 
404
421
    /**
405
 
     * Validate authentication on the server for the given dbname.  populates
406
 
     * level (if supplied) with the user's credentials.
 
422
     * Validate authentication on the server for the given dbname.
407
423
     */
408
 
    void Tool::auth( string dbname, Auth::Level * level ) {
409
 
 
410
 
        if ( ! dbname.size() )
411
 
            dbname = _db;
412
 
 
413
 
        if ( ! ( _username.size() || _password.size() ) ) {
 
424
    void Tool::auth() {
 
425
 
 
426
        if ( _username.empty() ) {
414
427
            // Make sure that we don't need authentication to connect to this db
415
428
            // findOne throws an AssertionException if it's not authenticated.
416
429
            if (_coll.size() > 0) {
418
431
                conn().findOne(getNS(), Query("{}"), 0, QueryOption_SlaveOk);
419
432
            }
420
433
 
421
 
            // set write-level access if authentication is disabled
422
 
            if ( level != NULL )
423
 
                *level = Auth::WRITE;
424
 
 
425
 
            return;
426
 
        }
427
 
 
428
 
        string errmsg;
429
 
        if (dbname.size()) {
430
 
            if ( _conn->auth( dbname , _username , _password , errmsg, true, level ) ) {
431
 
                return;
432
 
            }
433
 
        }
434
 
 
435
 
        // try against the admin db
436
 
        if ( _conn->auth( "admin" , _username , _password , errmsg, true, level ) ) {
437
 
            return;
438
 
        }
439
 
 
440
 
        throw UserException( 9997 , (string)"authentication failed: " + errmsg );
 
434
            return;
 
435
        }
 
436
 
 
437
        _conn->auth( BSON( saslCommandPrincipalSourceFieldName << getAuthenticationDatabase() <<
 
438
                           saslCommandPrincipalFieldName << _username <<
 
439
                           saslCommandPasswordFieldName << _password  <<
 
440
                           saslCommandMechanismFieldName << _authenticationMechanism ) );
441
441
    }
442
442
 
443
443
    BSONTool::BSONTool( const char * name, DBAccess access , bool objcheck )
444
444
        : Tool( name , access , "" , "" , false ) , _objcheck( objcheck ) {
445
445
 
446
446
        add_options()
447
 
        ("objcheck" , "validate object before inserting" )
 
447
        ("objcheck" , "validate object before inserting (default)" )
 
448
        ("noobjcheck" , "don't validate object before inserting" )
448
449
        ("filter" , po::value<string>() , "filter to apply before inserting" )
449
450
        ;
450
451
    }
451
452
 
452
453
 
453
454
    int BSONTool::run() {
454
 
        _objcheck = hasParam( "objcheck" );
 
455
        if ( hasParam( "objcheck" ) )
 
456
            _objcheck = true;
 
457
        else if ( hasParam( "noobjcheck" ) )
 
458
            _objcheck = false;
455
459
 
456
460
        if ( hasParam( "filter" ) )
457
461
            _matcher.reset( new Matcher( fromjson( getParam( "filter" ) ) ) );
541
545
        return processed;
542
546
    }
543
547
 
544
 
 
545
 
 
546
 
    void setupSignals( bool inFork ) {}
547
548
}