Files
mongo/db/security_commands.cpp

144 lines
4.4 KiB
C++
Raw Normal View History

2009-01-18 19:13:12 -05:00
// security_commands.cpp
/*
* Copyright (C) 2010 10gen Inc.
*
* 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.
*
* 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.
*
* 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/>.
*/
2009-01-18 19:13:12 -05:00
// security.cpp links with both dbgrid and db. this file db only -- at least for now.
// security.cpp
2010-04-27 15:27:52 -04:00
#include "pch.h"
2009-01-18 19:13:12 -05:00
#include "security.h"
#include "../util/md5.hpp"
2011-01-04 00:40:41 -05:00
#include "json.h"
2009-01-18 19:13:12 -05:00
#include "pdfile.h"
#include "db.h"
#include "dbhelpers.h"
#include "commands.h"
#include "jsobj.h"
#include "client.h"
2009-01-18 19:13:12 -05:00
namespace mongo {
2011-01-04 00:40:41 -05:00
/* authentication
2009-01-18 19:13:12 -05:00
2011-01-04 00:40:41 -05:00
system.users contains
{ user : <username>, pwd : <pwd_digest>, ... }
2009-01-18 19:13:12 -05:00
2011-01-04 00:40:41 -05:00
getnonce sends nonce to client
2009-01-18 19:13:12 -05:00
2011-06-05 18:13:18 -04:00
client then sends { authenticate:1, nonce64:<nonce_str>, user:<username>, key:<key> }
2009-01-18 19:13:12 -05:00
2011-01-04 00:40:41 -05:00
where <key> is md5(<nonce_str><username><pwd_digest_str>) as a string
*/
2009-01-18 19:13:12 -05:00
2011-06-05 18:13:18 -04:00
boost::thread_specific_ptr<nonce64> lastNonce;
2009-01-18 19:13:12 -05:00
class CmdGetNonce : public Command {
public:
2009-01-18 20:31:33 -05:00
virtual bool requiresAuth() { return false; }
2010-08-16 09:00:58 -04:00
virtual bool logTheOp() { return false; }
2010-04-23 15:50:49 -04:00
virtual bool slaveOk() const {
2009-01-18 19:13:12 -05:00
return true;
}
2010-04-23 16:41:56 -04:00
void help(stringstream& h) const { h << "internal"; }
2010-04-23 15:50:49 -04:00
virtual LockType locktype() const { return NONE; }
2009-01-18 19:13:12 -05:00
CmdGetNonce() : Command("getnonce") {}
bool run(const string&, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
2011-06-05 18:13:18 -04:00
nonce64 *n = new nonce64(Security::getNonce());
2009-01-20 11:37:15 -05:00
stringstream ss;
ss << hex << *n;
2009-01-20 11:37:15 -05:00
result.append("nonce", ss.str() );
lastNonce.reset(n);
2009-01-18 19:13:12 -05:00
return true;
}
} cmdGetNonce;
CmdLogout cmdLogout;
2011-01-04 00:40:41 -05:00
bool CmdAuthenticate::run(const string& dbname , BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
log() << " authenticate: " << cmdObj << endl;
string user = cmdObj.getStringField("user");
string key = cmdObj.getStringField("key");
string received_nonce = cmdObj.getStringField("nonce");
if( user.empty() || key.empty() || received_nonce.empty() ) {
log() << "field missing/wrong type in received authenticate command "
<< dbname
<< endl;
errmsg = "auth fails";
sleepmillis(10);
2009-01-18 19:13:12 -05:00
return false;
}
2011-01-04 00:40:41 -05:00
stringstream digestBuilder;
2010-12-27 16:05:40 -05:00
{
bool reject = false;
nonce64 *ln = lastNonce.release();
if ( ln == 0 ) {
reject = true;
log(1) << "auth: no lastNonce" << endl;
2010-12-27 16:05:40 -05:00
}
else {
digestBuilder << hex << *ln;
reject = digestBuilder.str() != received_nonce;
if ( reject ) log(1) << "auth: different lastNonce" << endl;
2009-01-18 19:13:12 -05:00
}
2010-12-27 16:05:40 -05:00
if ( reject ) {
log() << "auth: bad nonce received or getnonce not called. could be a driver bug or a security attack. db:" << dbname << endl;
2009-01-18 19:13:12 -05:00
errmsg = "auth fails";
sleepmillis(30);
2009-01-18 19:13:12 -05:00
return false;
}
}
2009-01-18 19:13:12 -05:00
BSONObj userObj;
string pwd;
if (!getUserObj(dbname, user, userObj, pwd)) {
errmsg = "auth fails";
return false;
}
2011-01-04 00:40:41 -05:00
md5digest d;
{
digestBuilder << user << pwd;
string done = digestBuilder.str();
md5_state_t st;
md5_init(&st);
md5_append(&st, (const md5_byte_t *) done.c_str(), done.size());
md5_finish(&st, d);
}
string computed = digestToString( d );
if ( key != computed ) {
log() << "auth: key mismatch " << user << ", ns:" << dbname << endl;
errmsg = "auth fails";
return false;
2009-01-18 19:13:12 -05:00
}
authenticate(dbname, user, userObj[ "readOnly" ].isBoolean() && userObj[ "readOnly" ].boolean());
return true;
}
CmdAuthenticate cmdAuthenticate;
2011-01-04 00:40:41 -05:00
2009-01-18 19:13:12 -05:00
} // namespace mongo