1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/*
* Copyright (C) 2010 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Tool.cpp
#include "mongo/tools/tool.h"
#include <boost/filesystem/operations.hpp>
#include <fstream>
#include <iostream>
#include "pcrecpp.h"
#include "mongo/base/initializer.h"
#include "mongo/client/dbclient_rs.h"
#include "mongo/client/sasl_client_authenticate.h"
#include "mongo/db/auth/authorization_manager.h"
#include "mongo/db/auth/authorization_manager_global.h"
#include "mongo/db/auth/authz_manager_external_state_mock.h"
#include "mongo/db/json.h"
#include "mongo/db/namespace_details.h"
#include "mongo/platform/posix_fadvise.h"
#include "mongo/util/file_allocator.h"
#include "mongo/util/options_parser/option_section.h"
#include "mongo/util/password.h"
#include "mongo/util/net/ssl_options.h"
#include "mongo/util/text.h"
#include "mongo/util/version.h"
using namespace std;
using namespace mongo;
namespace mongo {
CmdLine cmdLine;
moe::OptionSection options("options");
moe::Environment _params;
Tool::Tool( string name , string defaultDB ,
string defaultCollection , bool usesstdout , bool quiet) :
_name( name ) , _db( defaultDB ) , _coll( defaultCollection ) ,
_usesstdout(usesstdout) , _quiet(quiet) , _noconnection(false) ,
_autoreconnect(false) , _conn(0) , _slaveConn(0) , _paired(false) { }
Tool::~Tool() {
if ( _conn )
delete _conn;
}
void Tool::printVersion(ostream &out) {
out << _name << " version " << mongo::versionString;
if (mongo::versionString[strlen(mongo::versionString)-1] == '-')
out << " (commit " << mongo::gitVersion() << ")";
out << endl;
}
int Tool::main( int argc , char ** argv, char ** envp ) {
static StaticObserver staticObserver;
setGlobalAuthorizationManager(new AuthorizationManager(new AuthzManagerExternalStateMock()));
cmdLine.prealloc = false;
// The default value may vary depending on compile options, but for tools
// we want durability to be disabled.
cmdLine.dur = false;
_name = argv[0];
mongo::runGlobalInitializersOrDie(argc, argv, envp);
// Set authentication parameters
if ( _params.count( "authenticationDatabase" ) ) {
_authenticationDatabase = _params["authenticationDatabase"].as<string>();
}
if ( _params.count( "authenticationMechanism" ) ) {
_authenticationMechanism = _params["authenticationMechanism"].as<string>();
}
// 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")) {
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()));
}
}
if ( hasParam("quiet") ) {
_quiet = true;
}
#ifdef MONGO_SSL
if (_params.count("ssl")) {
sslGlobalParams.sslOnNormalPorts = true;
}
#endif
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 = _params["password"].as<string>();
if ( _password.empty() ) {
_password = askPassword();
}
}
if (_params.count("ipv6"))
enableIPv6();
bool useDirectClient = hasParam( "dbpath" );
if ( useDirectClient && _params.count("journal")){
cmdLine.dur = true;
}
preSetup();
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);
}
if (!_quiet) {
(_usesstdout ? cout : cerr ) << "connected to: " << _host << endl;
}
}
}
else {
if ( _params.count( "directoryperdb" ) ) {
directoryperdb = true;
}
verify( lastError.get( 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_FS );
::_exit(EXIT_FAILURE);
}
FileAllocator::get()->start();
dur::startup();
}
int ret = -1;
try {
if (!useDirectClient && !_noconnection)
auth();
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 );
fflush(stdout);
fflush(stderr);
::_exit(ret);
}
DBClientBase& Tool::conn( bool slaveIfPaired ) {
if ( slaveIfPaired && _conn->type() == ConnectionString::SET ) {
if (!_slaveConn) {
DBClientReplicaSet* rs = static_cast<DBClientReplicaSet*>(_conn);
_slaveConn = &rs->slaveConn();
}
return *_slaveConn;
}
return *_conn;
}
bool Tool::isMaster() {
if ( hasParam("dbpath") ) {
return true;
}
BSONObj info;
bool isMaster;
bool ok = conn().isMaster(isMaster, &info);
if (ok && !isMaster) {
cerr << "ERROR: trying to write to non-master " << conn().toString() << endl;
cerr << "isMaster info: " << info << endl;
return false;
}
return true;
}
bool Tool::isMongos() {
// TODO: when mongos supports QueryOption_Exaust add a version check (SERVER-2628)
BSONObj isdbgrid;
conn("true").simpleCommand("admin", &isdbgrid, "isdbgrid");
return isdbgrid["isdbgrid"].trueValue();
}
void Tool::needFields() {
if ( hasParam( "fields" ) ) {
BSONObjBuilder b;
string fields_arg = getParam("fields");
pcrecpp::StringPiece input(fields_arg);
string f;
pcrecpp::RE re("([#\\w\\.\\s\\-]+),?" );
while ( re.Consume( &input, &f ) ) {
_fields.push_back( f );
b.append( f , 1 );
}
_fieldsObj = b.obj();
return;
}
if ( hasParam( "fieldFile" ) ) {
string fn = getParam( "fieldFile" );
if ( ! boost::filesystem::exists( fn ) )
throw UserException( 9999 , ((string)"file: " + fn ) + " doesn't exist" );
const int BUF_SIZE = 1024;
char line[ 1024 + 128];
ifstream file( fn.c_str() );
BSONObjBuilder b;
while ( file.rdstate() == ios_base::goodbit ) {
file.getline( line , BUF_SIZE );
const char * cur = line;
while ( isspace( cur[0] ) ) cur++;
if ( cur[0] == '\0' )
continue;
_fields.push_back( cur );
b.append( cur , 1 );
}
_fieldsObj = b.obj();
return;
}
throw UserException( 9998 , "you need to specify fields" );
}
std::string Tool::getAuthenticationDatabase() {
if (!_authenticationDatabase.empty()) {
return _authenticationDatabase;
}
if (!_db.empty()) {
return _db;
}
return "admin";
}
/**
* Validate authentication on the server for the given dbname.
*/
void Tool::auth() {
if ( _username.empty() ) {
// Make sure that we don't need authentication to connect to this db
// findOne throws an AssertionException if it's not authenticated.
if (_coll.size() > 0) {
// BSONTools don't have a collection
conn().findOne(getNS(), Query("{}"), 0, QueryOption_SlaveOk);
}
return;
}
_conn->auth( BSON( saslCommandUserSourceFieldName << getAuthenticationDatabase() <<
saslCommandUserFieldName << _username <<
saslCommandPasswordFieldName << _password <<
saslCommandMechanismFieldName << _authenticationMechanism ) );
}
BSONTool::BSONTool( const char * name, bool objcheck )
: Tool( name , "" , "" , false ) , _objcheck( objcheck ) { }
int BSONTool::run() {
if ( hasParam( "objcheck" ) )
_objcheck = true;
else if ( hasParam( "noobjcheck" ) )
_objcheck = false;
if ( hasParam( "filter" ) )
_matcher.reset( new Matcher( fromjson( getParam( "filter" ) ) ) );
return doRun();
}
long long BSONTool::processFile( const boost::filesystem::path& root ) {
_fileName = root.string();
unsigned long long fileLength = file_size( root );
if ( fileLength == 0 ) {
if (!_quiet) {
(_usesstdout ? cout : cerr ) << "file " << _fileName << " empty, skipping" << endl;
}
return 0;
}
FILE* file = fopen( _fileName.c_str() , "rb" );
if ( ! file ) {
cerr << "error opening file: " << _fileName << " " << errnoWithDescription() << endl;
return 0;
}
#ifdef POSIX_FADV_SEQUENTIAL
posix_fadvise(fileno(file), 0, fileLength, POSIX_FADV_SEQUENTIAL);
#endif
if (!_quiet && logger::globalLogDomain()->shouldLog(logger::LogSeverity::Debug(1))) {
(_usesstdout ? cout : cerr ) << "\t file size: " << fileLength << endl;
}
unsigned long long read = 0;
unsigned long long num = 0;
unsigned long long processed = 0;
const int BUF_SIZE = BSONObjMaxUserSize + ( 1024 * 1024 );
boost::scoped_array<char> buf_holder(new char[BUF_SIZE]);
char * buf = buf_holder.get();
ProgressMeter m( fileLength );
m.setUnits( "bytes" );
while ( read < fileLength ) {
size_t amt = fread(buf, 1, 4, file);
verify( amt == 4 );
int size = ((int*)buf)[0];
uassert( 10264 , str::stream() << "invalid object size: " << size , size < BUF_SIZE );
amt = fread(buf+4, 1, size-4, file);
verify( amt == (size_t)( size - 4 ) );
BSONObj o( buf );
if ( _objcheck && ! o.valid() ) {
cerr << "INVALID OBJECT - going to try and print out " << endl;
cerr << "size: " << size << endl;
BSONObjIterator i(o);
while ( i.more() ) {
BSONElement e = i.next();
try {
e.validate();
}
catch ( ... ) {
cerr << "\t\t NEXT ONE IS INVALID" << endl;
}
cerr << "\t name : " << e.fieldName() << " " << e.type() << endl;
cerr << "\t " << e << endl;
}
}
if ( _matcher.get() == 0 || _matcher->matches( o ) ) {
gotObject( o );
processed++;
}
read += o.objsize();
num++;
m.hit( o.objsize() );
}
fclose( file );
uassert( 10265 , "counts don't match" , m.done() == fileLength );
if (!_quiet) {
(_usesstdout ? cout : cerr ) << m.hits() << " objects found" << endl;
if ( _matcher.get() )
(_usesstdout ? cout : cerr ) << processed << " objects processed" << endl;
}
return processed;
}
}
#if defined(_WIN32)
// In Windows, wmain() is an alternate entry point for main(), and receives the same parameters
// as main() but encoded in Windows Unicode (UTF-16); "wide" 16-bit wchar_t characters. The
// WindowsCommandLine object converts these wide character strings to a UTF-8 coded equivalent
// and makes them available through the argv() and envp() members. This enables toolMain()
// to process UTF-8 encoded arguments and environment variables without regard to platform.
int wmain(int argc, wchar_t* argvW[], wchar_t* envpW[]) {
mongo::WindowsCommandLine wcl(argc, argvW, envpW);
auto_ptr<Tool> instance = (*Tool::createInstance)();
int exitCode = instance->main(argc, wcl.argv(), wcl.envp());
::_exit(exitCode);
}
#else
int main(int argc, char* argv[], char** envp) {
auto_ptr<Tool> instance = (*Tool::createInstance)();
::_exit(instance->main(argc, argv, envp));
}
#endif
|