void onConnect( DBClientWithCommands &c ) { if ( _nokillop ) { return; } c.setClientRPCProtocols(shellGlobalParams.rpcProtocols); connectionRegistry.registerConnection( c ); }
void onConnect(DBClientWithCommands& c) { if (_nokillop) { return; } // Only override the default rpcProtocols if they were set on the command line. if (shellGlobalParams.rpcProtocols) { c.setClientRPCProtocols(*shellGlobalParams.rpcProtocols); } connectionRegistry.registerConnection(c); }
void onConnect( DBClientWithCommands &c ) { if ( _nokillop ) { return; } connectionRegistry.registerConnection( c ); }
namespace shell_utils { std::string _dbConnect; std::string _dbAuth; const char* argv0 = 0; void RecordMyLocation(const char* _argv0) { argv0 = _argv0; } // helpers BSONObj makeUndefined() { BSONObjBuilder b; b.appendUndefined(""); return b.obj(); } const BSONObj undefinedReturn = makeUndefined(); BSONElement singleArg(const BSONObj& args) { uassert(12597, "need to specify 1 argument", args.nFields() == 1); return args.firstElement(); } const char* getUserDir() { #ifdef _WIN32 return getenv("USERPROFILE"); #else return getenv("HOME"); #endif } // real methods BSONObj JSGetMemInfo(const BSONObj& args, void* data) { ProcessInfo pi; uassert(10258, "processinfo not supported", pi.supported()); BSONObjBuilder e; e.append("virtual", pi.getVirtualMemorySize()); e.append("resident", pi.getResidentSize()); BSONObjBuilder b; b.append("ret", e.obj()); return b.obj(); } #if !defined(_WIN32) ThreadLocalValue<unsigned int> _randomSeed; #endif BSONObj JSSrand(const BSONObj& a, void* data) { unsigned int seed; // grab the least significant bits of either the supplied argument or // a random number from SecureRandom. if (a.nFields() == 1 && a.firstElement().isNumber()) seed = static_cast<unsigned int>(a.firstElement().numberLong()); else { std::unique_ptr<SecureRandom> rand(SecureRandom::create()); seed = static_cast<unsigned int>(rand->nextInt64()); } #if !defined(_WIN32) _randomSeed.set(seed); #else srand(seed); #endif return BSON("" << static_cast<double>(seed)); } BSONObj JSRand(const BSONObj& a, void* data) { uassert(12519, "rand accepts no arguments", a.nFields() == 0); unsigned r; #if !defined(_WIN32) r = rand_r(&_randomSeed.getRef()); #else r = rand(); #endif return BSON("" << double(r) / (double(RAND_MAX) + 1)); } BSONObj isWindows(const BSONObj& a, void* data) { uassert(13006, "isWindows accepts no arguments", a.nFields() == 0); #ifdef _WIN32 return BSON("" << true); #else return BSON("" << false); #endif } BSONObj getBuildInfo(const BSONObj& a, void* data) { uassert(16822, "getBuildInfo accepts no arguments", a.nFields() == 0); BSONObjBuilder b; VersionInfoInterface::instance().appendBuildInfo(&b); return BSON("" << b.done()); } BSONObj isKeyTooLarge(const BSONObj& a, void* data) { uassert(17428, "keyTooLarge takes exactly 2 arguments", a.nFields() == 2); BSONObjIterator i(a); BSONObj index = i.next().Obj(); BSONObj doc = i.next().Obj(); return BSON("" << isAnyIndexKeyTooLarge(index, doc)); } BSONObj validateIndexKey(const BSONObj& a, void* data) { BSONObj key = a[0].Obj(); Status indexValid = validateKeyPattern(key); if (!indexValid.isOK()) { return BSON("" << BSON("ok" << false << "type" << indexValid.codeString() << "errmsg" << indexValid.reason())); } return BSON("" << BSON("ok" << true)); } BSONObj replMonitorStats(const BSONObj& a, void* data) { uassert(17134, "replMonitorStats requires a single string argument (the ReplSet name)", a.nFields() == 1 && a.firstElement().type() == String); ReplicaSetMonitorPtr rsm = ReplicaSetMonitor::get(a.firstElement().valuestrsafe()); if (!rsm) { return BSON("" << "no ReplSetMonitor exists by that name"); } BSONObjBuilder result; rsm->appendInfo(result); return result.obj(); } BSONObj useWriteCommandsDefault(const BSONObj& a, void* data) { return BSON("" << shellGlobalParams.useWriteCommandsDefault); } BSONObj writeMode(const BSONObj&, void*) { return BSON("" << shellGlobalParams.writeMode); } BSONObj readMode(const BSONObj&, void*) { return BSON("" << shellGlobalParams.readMode); } BSONObj interpreterVersion(const BSONObj& a, void* data) { uassert(16453, "interpreterVersion accepts no arguments", a.nFields() == 0); return BSON("" << getGlobalScriptEngine()->getInterpreterVersionString()); } BSONObj createCertificateRequest(const BSONObj& a, void* data) { #ifndef MONGO_CONFIG_SSL return BSON( "" << BSON("ok" << false << "errmsg" << "Cannot create a certificate signing request without SSL support")); #else if (a.nFields() != 1 || a.firstElement().type() != Object) { return BSON( "" << BSON("ok" << false << "errmsg" << "createCertificateRequest requires a single object argument")); } // args can optionally contain some to be determined fields... BSONObj args = a.firstElement().embeddedObject(); if (!args.hasField("CN")) { return BSON( "" << BSON("ok" << false << "errmsg" << "createCertificateRequest requires a Common Name (\"CN\") field")); } // Generate key pair and certificate signing request RSA* rsa; EVP_PKEY* pkey; X509_REQ* x509req; X509_NAME* name; BIO* out; char client_key[2048]; char client_csr[2048]; pkey = EVP_PKEY_new(); if (!pkey) { return BSON("" << BSON("ok" << false)); // fail("couldn't generate key"); } rsa = RSA_generate_key(2048, RSA_F4, NULL, NULL); if (!EVP_PKEY_assign_RSA(pkey, rsa)) { return BSON("" << BSON("ok" << false)); // fail("couldn't assign the key"); } x509req = X509_REQ_new(); X509_REQ_set_pubkey(x509req, pkey); name = X509_NAME_new(); X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (const unsigned char*)"IS", -1, -1, 0); X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (const unsigned char*)"MongoDB", -1, -1, 0); X509_NAME_add_entry_by_txt( name, "OU", MBSTRING_ASC, (const unsigned char*)"SkunkWorks client", -1, -1, 0); X509_NAME_add_entry_by_txt( name, "CN", MBSTRING_ASC, (const unsigned char*)args.getStringField("CN"), -1, -1, 0); X509_REQ_set_subject_name(x509req, name); X509_REQ_set_version(x509req, 2); if (!X509_REQ_sign(x509req, pkey, EVP_sha1())) { return BSON("" << BSON("ok" << false)); } // out = BIO_new_file("client.key.pem", "wb"); out = BIO_new(BIO_s_mem()); if (!PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL)) { return BSON("" << BSON("ok" << false)); // fail("can't write private key"); } int i = BIO_read(out, &client_key, sizeof client_key); client_key[i] = '\0'; BIO_free_all(out); out = BIO_new(BIO_s_mem()); if (!PEM_write_bio_X509_REQ_NEW(out, x509req)) { return BSON("" << BSON("ok" << false)); // fail("coudln't write csr"); } i = BIO_read(out, &client_csr, sizeof client_csr); client_csr[i] = '\0'; BIO_free_all(out); EVP_PKEY_free(pkey); X509_REQ_free(x509req); return BSON("" << BSON("ok" << true << "certificateRequest" << client_csr << "privateKey" << client_key)); #endif } void installShellUtils(Scope& scope) { scope.injectNative("getMemInfo", JSGetMemInfo); scope.injectNative("_replMonitorStats", replMonitorStats); scope.injectNative("_srand", JSSrand); scope.injectNative("_rand", JSRand); scope.injectNative("_isWindows", isWindows); scope.injectNative("interpreterVersion", interpreterVersion); scope.injectNative("getBuildInfo", getBuildInfo); scope.injectNative("isKeyTooLarge", isKeyTooLarge); scope.injectNative("validateIndexKey", validateIndexKey); scope.injectNative("createCertificateRequest", createCertificateRequest); #ifndef MONGO_SAFE_SHELL // can't launch programs installShellUtilsLauncher(scope); installShellUtilsExtended(scope); #endif } void initScope(Scope& scope) { // Need to define this method before JSFiles::utils is executed. scope.injectNative("_useWriteCommandsDefault", useWriteCommandsDefault); scope.injectNative("_writeMode", writeMode); scope.injectNative("_readMode", readMode); scope.externalSetup(); mongo::shell_utils::installShellUtils(scope); scope.execSetup(JSFiles::servers); scope.execSetup(JSFiles::shardingtest); scope.execSetup(JSFiles::servers_misc); scope.execSetup(JSFiles::replsettest); scope.execSetup(JSFiles::bridge); scope.injectNative("benchRun", BenchRunner::benchRunSync); scope.injectNative("benchRunSync", BenchRunner::benchRunSync); scope.injectNative("benchStart", BenchRunner::benchStart); scope.injectNative("benchFinish", BenchRunner::benchFinish); if (!_dbConnect.empty()) { uassert(12513, "connect failed", scope.exec(_dbConnect, "(connect)", false, true, false)); } if (!_dbAuth.empty()) { uassert(12514, "login failed", scope.exec(_dbAuth, "(auth)", true, true, false)); } } Prompter::Prompter(const string& prompt) : _prompt(prompt), _confirmed() {} bool Prompter::confirm() { if (_confirmed) { return true; } // The printf and scanf functions provide thread safe i/o. printf("\n%s (y/n): ", _prompt.c_str()); char yn = '\0'; int nScanMatches = scanf("%c", &yn); bool matchedY = (nScanMatches == 1 && (yn == 'y' || yn == 'Y')); return _confirmed = matchedY; } ConnectionRegistry::ConnectionRegistry() = default; void ConnectionRegistry::registerConnection(DBClientWithCommands& client) { BSONObj info; if (client.runCommand("admin", BSON("whatsmyuri" << 1), info)) { string connstr = dynamic_cast<DBClientBase&>(client).getServerAddress(); stdx::lock_guard<stdx::mutex> lk(_mutex); _connectionUris[connstr].insert(info["you"].str()); } } void ConnectionRegistry::killOperationsOnAllConnections(bool withPrompt) const { Prompter prompter("do you want to kill the current op(s) on the server?"); stdx::lock_guard<stdx::mutex> lk(_mutex); for (map<string, set<string>>::const_iterator i = _connectionUris.begin(); i != _connectionUris.end(); ++i) { auto status = ConnectionString::parse(i->first); if (!status.isOK()) { continue; } const ConnectionString cs(status.getValue()); string errmsg; std::unique_ptr<DBClientWithCommands> conn(cs.connect("MongoDB Shell", errmsg)); if (!conn) { continue; } const set<string>& uris = i->second; BSONObj currentOpRes; conn->runPseudoCommand("admin", "currentOp", "$cmd.sys.inprog", {}, currentOpRes); if (!currentOpRes["inprog"].isABSONObj()) { // We don't have permissions (or the call didn't succeed) - go to the next connection. continue; } auto inprog = currentOpRes["inprog"].embeddedObject(); for (const auto op : inprog) { // For sharded clusters, `client_s` is used instead and `client` is not present. string client; if (auto elem = op["client"]) { // mongod currentOp client if (elem.type() != String) { warning() << "Ignoring operation " << op["opid"].toString(false) << "; expected 'client' field in currentOp response to have type " "string, but found " << typeName(elem.type()); continue; } client = elem.str(); } else if (auto elem = op["client_s"]) { // mongos currentOp client if (elem.type() != String) { warning() << "Ignoring operation " << op["opid"].toString(false) << "; expected 'client_s' field in currentOp response to have type " "string, but found " << typeName(elem.type()); continue; } client = elem.str(); } else { // Internal operation, like TTL index. continue; } if (uris.count(client)) { if (!withPrompt || prompter.confirm()) { BSONObjBuilder cmdBob; BSONObj info; cmdBob.appendAs(op["opid"], "op"); auto cmdArgs = cmdBob.done(); conn->runPseudoCommand("admin", "killOp", "$cmd.sys.killop", cmdArgs, info); } else { return; } } } } } ConnectionRegistry connectionRegistry; bool _nokillop = false; void onConnect(DBClientWithCommands& c) { if (_nokillop) { return; } // Only override the default rpcProtocols if they were set on the command line. if (shellGlobalParams.rpcProtocols) { c.setClientRPCProtocols(*shellGlobalParams.rpcProtocols); } connectionRegistry.registerConnection(c); } bool fileExists(const std::string& file) { try { #ifdef _WIN32 boost::filesystem::path p(toWideString(file.c_str())); #else boost::filesystem::path p(file); #endif return boost::filesystem::exists(p); } catch (...) { return false; } } stdx::mutex& mongoProgramOutputMutex(*(new stdx::mutex())); }