2
db "commands" (sent via db.$cmd.findOne(...))
4
* Copyright (C) 2012 10gen Inc.
7
6
* This program is free software: you can redistribute it and/or modify
8
7
* it under the terms of the GNU Affero General Public License, version 3,
62
auto_ptr<Scope> s = globalScriptEngine->getPooledScope( dbName );
60
auto_ptr<Scope> s = globalScriptEngine->getPooledScope( dbName, "dbeval" );
63
61
ScriptingFunction f = s->createFunction(code);
65
63
errmsg = (string)"compile failed: " + s->getError();
93
91
else OCCASIONALLY log() << code << endl;
94
if (res || s->isLastRetNativeCode()) {
97
95
result.append("errno", (double) res);
98
96
errmsg = "invoke failed: ";
99
errmsg += s->getError();
97
if (s->isLastRetNativeCode())
98
errmsg += "cannot return native function";
100
errmsg += s->getError();
103
s->append( result , "retval" , "return" );
104
s->append( result , "retval" , "__returnValue" );
115
116
help << "Evaluate javascript at the server.\n" "http://dochub.mongodb.org/core/serversidecodeexecution";
117
118
virtual LockType locktype() const { return NONE; }
119
virtual void addRequiredPrivileges(const std::string& dbname,
120
const BSONObj& cmdObj,
121
std::vector<Privilege>* out) {
122
// $eval can do pretty much anything, so require all privileges.
123
out->push_back(Privilege(PrivilegeSet::WILDCARD_RESOURCE,
124
AuthorizationManager::getAllUserActions()));
118
126
CmdEval() : Command("eval", false, "$eval") { }
119
127
bool run(const string& dbname , BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
121
AuthenticationInfo *ai = cc().getAuthenticationInfo();
122
uassert( 12598 , "$eval reads unauthorized", ai->isAuthorizedReads(dbname.c_str()) );
124
128
if ( cmdObj["nolock"].trueValue() ) {
125
129
return dbEval(dbname, cmdObj, result, errmsg);
128
// write security will be enforced in DBDirectClient
129
// TODO: should this be a db lock?
130
scoped_ptr<Lock::ScopedLock> lk( ai->isAuthorized( dbname.c_str() ) ?
131
static_cast<Lock::ScopedLock*>( new Lock::GlobalWrite() ) :
132
static_cast<Lock::ScopedLock*>( new Lock::GlobalRead() ) );
132
Lock::GlobalWrite lk;
133
133
Client::Context ctx( dbname );
135
135
return dbEval(dbname, cmdObj, result, errmsg);