diff options
author | Kristina <kristina@10gen.com> | 2011-06-22 13:24:43 -0400 |
---|---|---|
committer | Kristina <kristina@10gen.com> | 2011-06-22 14:20:53 -0400 |
commit | 09d1825d7f599724739ee712c8b91bfb566ea607 (patch) | |
tree | 5bfb67cf5ae81a84ecb1b57f76d944ce1fc428d7 /s/commands_public.cpp | |
parent | 67945f8b00d6e3cad4eaace5ee8f3cd9a6a71dde (diff) | |
download | mongo-09d1825d7f599724739ee712c8b91bfb566ea607.tar.gz |
move runAgainstRegistered to sharding SERVER-921
Diffstat (limited to 's/commands_public.cpp')
-rw-r--r-- | s/commands_public.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/s/commands_public.cpp b/s/commands_public.cpp index c549c68b14c..875a3aad31d 100644 --- a/s/commands_public.cpp +++ b/s/commands_public.cpp @@ -31,6 +31,7 @@ #include "strategy.h" #include "grid.h" #include "mr_shard.h" +#include "client.h" namespace mongo { @@ -1239,4 +1240,64 @@ namespace mongo { } + bool Command::runAgainstRegistered(const char *ns, BSONObj& jsobj, BSONObjBuilder& anObjBuilder) { + const char *p = strchr(ns, '.'); + if ( !p ) return false; + if ( strcmp(p, ".$cmd") != 0 ) return false; + + bool ok = false; + + BSONElement e = jsobj.firstElement(); + map<string,Command*>::iterator i; + + if ( e.eoo() ) + ; + // check for properly registered command objects. + else if ( (i = _commands->find(e.fieldName())) != _commands->end() ) { + string errmsg; + Command *c = i->second; + ClientInfo *client = ClientInfo::get(); + AuthenticationInfo *ai = client->getAuthenticationInfo(); + + char cl[256]; + nsToDatabase(ns, cl); + if( c->requiresAuth() && !ai->isAuthorized(cl)) { + ok = false; + errmsg = "unauthorized"; + } + else if( c->adminOnly() && c->localHostOnlyIfNoAuth( jsobj ) && noauth && !ai->isLocalHost ) { + ok = false; + errmsg = "unauthorized: this command must run from localhost when running db without auth"; + log() << "command denied: " << jsobj.toString() << endl; + } + else if ( c->adminOnly() && !startsWith(ns, "admin.") ) { + ok = false; + errmsg = "access denied - use admin db"; + } + else if ( jsobj.getBoolField( "help" ) ) { + stringstream help; + help << "help for: " << e.fieldName() << " "; + c->help( help ); + anObjBuilder.append( "help" , help.str() ); + } + else { + ok = c->run( nsToDatabase( ns ) , jsobj, errmsg, anObjBuilder, false); + } + + BSONObj tmp = anObjBuilder.asTempObj(); + bool have_ok = tmp.hasField("ok"); + bool have_errmsg = tmp.hasField("errmsg"); + + if (!have_ok) + anObjBuilder.append( "ok" , ok ? 1.0 : 0.0 ); + + if ( !ok && !have_errmsg) { + anObjBuilder.append("errmsg", errmsg); + uassert_nothrow(errmsg.c_str()); + } + return true; + } + + return false; + } } |