2008-09-09 02:37:59 +02:00
|
|
|
/* commands.cpp
|
|
|
|
db "commands" (sent via db.$cmd.findOne(...))
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-09-09 02:37:59 +02:00
|
|
|
* 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.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-09-09 02:37:59 +02:00
|
|
|
* 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.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-09-09 02:37:59 +02:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "jsobj.h"
|
2008-09-10 21:31:05 +02:00
|
|
|
#include "commands.h"
|
2008-09-09 02:37:59 +02:00
|
|
|
|
2008-10-22 22:56:39 +02:00
|
|
|
map<string,Command*> *commands;
|
2008-09-12 17:26:51 +02:00
|
|
|
|
2008-12-29 02:28:49 +01:00
|
|
|
Command::Command(const char *_name) : name(_name) {
|
2008-09-12 17:26:51 +02:00
|
|
|
// register ourself.
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( commands == 0 )
|
2008-09-12 17:26:51 +02:00
|
|
|
commands = new map<string,Command*>;
|
|
|
|
(*commands)[name] = this;
|
|
|
|
}
|
2008-09-10 21:31:05 +02:00
|
|
|
|
2008-12-29 02:28:49 +01:00
|
|
|
bool runCommandAgainstRegistered(const char *ns, BSONObj& jsobj, BSONObjBuilder& anObjBuilder) {
|
|
|
|
const char *p = strchr(ns, '.');
|
|
|
|
if ( !p ) return false;
|
|
|
|
if ( strcmp(p, ".$cmd") != 0 ) return false;
|
2008-09-09 02:37:59 +02:00
|
|
|
|
2008-12-29 02:28:49 +01:00
|
|
|
bool ok = false;
|
|
|
|
bool valid = false;
|
2008-09-09 02:37:59 +02:00
|
|
|
|
2008-12-29 02:28:49 +01:00
|
|
|
BSONElement e;
|
|
|
|
e = jsobj.firstElement();
|
2008-09-09 02:37:59 +02:00
|
|
|
|
2008-09-10 21:31:05 +02:00
|
|
|
map<string,Command*>::iterator i;
|
|
|
|
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( e.eoo() )
|
2008-09-10 21:31:05 +02:00
|
|
|
;
|
2008-12-29 02:28:49 +01:00
|
|
|
/* check for properly registered command objects. Note that all the commands below should be
|
2008-09-10 21:31:05 +02:00
|
|
|
migrated over to the command object format.
|
|
|
|
*/
|
2008-12-29 02:28:49 +01:00
|
|
|
else if ( (i = commands->find(e.fieldName())) != commands->end() ) {
|
2008-09-10 21:31:05 +02:00
|
|
|
valid = true;
|
|
|
|
string errmsg;
|
|
|
|
Command *c = i->second;
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( c->adminOnly() && strncmp(ns, "admin", p-ns) != 0 ) {
|
2008-09-10 21:31:05 +02:00
|
|
|
ok = false;
|
2008-12-29 02:28:49 +01:00
|
|
|
errmsg = "access denied";
|
2008-09-10 21:31:05 +02:00
|
|
|
}
|
|
|
|
else {
|
2008-12-05 16:09:16 +01:00
|
|
|
ok = c->run(ns, jsobj, errmsg, anObjBuilder, false);
|
2008-09-10 21:31:05 +02:00
|
|
|
}
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( !ok )
|
2008-09-10 21:31:05 +02:00
|
|
|
anObjBuilder.append("errmsg", errmsg);
|
2008-10-22 22:56:39 +02:00
|
|
|
return true;
|
2008-09-10 21:31:05 +02:00
|
|
|
}
|
2008-12-29 02:28:49 +01:00
|
|
|
|
|
|
|
return false;
|
2008-11-17 21:39:03 +01:00
|
|
|
}
|