Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move runAgainstRegistered to sharding SERVER-921

  • Loading branch information...
commit 09d1825d7f599724739ee712c8b91bfb566ea607 1 parent 67945f8
@kchodorow kchodorow authored
Showing with 61 additions and 49 deletions.
  1. +0 −49 db/commands.cpp
  2. +61 −0 s/commands_public.cpp
View
49 db/commands.cpp
@@ -121,55 +121,6 @@ namespace mongo {
help << "no help defined";
}
- 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. Note that all the commands below should be
- migrated over to the command object format.
- */
- else if ( (i = _commands->find(e.fieldName())) != _commands->end() ) {
- string errmsg;
- Command *c = i->second;
- 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;
- }
-
Command* Command::findCommand( const string& name ) {
map<string,Command*>::iterator i = _commands->find( name );
if ( i == _commands->end() )
View
61 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;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.