Files
mongo/s/s_only.cpp

100 lines
2.9 KiB
C++
Raw Normal View History

2009-12-03 20:49:49 -05:00
// s_only.cpp
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2010-04-27 15:27:52 -04:00
#include "pch.h"
2010-12-16 23:05:25 -05:00
#include "request.h"
2010-12-27 16:27:32 -05:00
#include "client.h"
2009-12-03 20:49:49 -05:00
#include "../client/dbclient.h"
#include "../db/dbhelpers.h"
2010-01-28 15:26:26 -05:00
#include "../db/matcher.h"
2010-07-28 11:09:38 -04:00
#include "../db/commands.h"
2009-12-03 20:49:49 -05:00
/*
most a pile of hacks to make linking nicer
*/
2009-12-03 20:49:49 -05:00
namespace mongo {
2011-09-14 12:00:54 -04:00
TSP_DEFINE(Client,currentClient)
2011-04-05 02:24:16 -04:00
Client::Client(const char *desc , AbstractMessagingPort *p) :
2011-01-04 00:40:41 -05:00
_context(0),
_shutdown(false),
_desc(desc),
_god(0),
_lastOp(0),
_mp(p) {
}
2011-01-04 00:40:41 -05:00
Client::~Client() {}
bool Client::shutdown() { return true; }
2011-04-05 02:24:16 -04:00
Client& Client::initThread(const char *desc, AbstractMessagingPort *mp) {
2010-12-11 16:42:31 -05:00
setThreadName(desc);
assert( currentClient.get() == 0 );
Client *c = new Client(desc, mp);
currentClient.reset(c);
mongo::lastError.initThread();
return *c;
}
2010-12-16 23:05:25 -05:00
string Client::clientAddress(bool includePort) const {
ClientInfo * ci = ClientInfo::get();
if ( ci )
return ci->getRemote();
return "";
}
2010-07-28 11:09:38 -04:00
bool execCommand( Command * c ,
2011-01-04 00:40:41 -05:00
Client& client , int queryOptions ,
const char *ns, BSONObj& cmdObj ,
BSONObjBuilder& result,
bool fromRepl ) {
2010-07-28 11:09:38 -04:00
assert(c);
2011-01-04 00:40:41 -05:00
2010-07-28 11:09:38 -04:00
string dbname = nsToDatabase( ns );
2011-01-04 00:40:41 -05:00
if ( cmdObj["help"].trueValue() ) {
2010-07-28 11:09:38 -04:00
stringstream ss;
ss << "help for: " << c->name << " ";
c->help( ss );
result.append( "help" , ss.str() );
result.append( "lockType" , c->locktype() );
return true;
2011-01-04 00:40:41 -05:00
}
2010-07-28 11:09:38 -04:00
2011-01-04 00:40:41 -05:00
if ( c->adminOnly() ) {
2010-07-28 11:09:38 -04:00
if ( dbname != "admin" ) {
result.append( "errmsg" , "access denied- use admin db" );
log() << "command denied: " << cmdObj.toString() << endl;
return false;
}
log( 2 ) << "command: " << cmdObj << endl;
}
2011-06-22 14:22:09 -04:00
if (!client.getAuthenticationInfo()->isAuthorized(dbname)) {
result.append("errmsg" , "unauthorized");
return false;
}
2010-07-28 11:09:38 -04:00
string errmsg;
int ok = c->run( dbname , cmdObj , queryOptions, errmsg , result , fromRepl );
2010-07-28 11:09:38 -04:00
if ( ! ok )
result.append( "errmsg" , errmsg );
return ok;
}
2009-12-03 20:49:49 -05:00
}