2009-01-19 01:13:12 +01:00
|
|
|
// security_commands.cpp
|
2010-02-09 22:48:21 +01:00
|
|
|
/*
|
|
|
|
* 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-19 01:13:12 +01:00
|
|
|
// security.cpp links with both dbgrid and db. this file db only -- at least for now.
|
|
|
|
|
|
|
|
// security.cpp
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "../util/md5.hpp"
|
|
|
|
#include "json.h"
|
|
|
|
#include "pdfile.h"
|
|
|
|
#include "db.h"
|
|
|
|
#include "dbhelpers.h"
|
|
|
|
#include "commands.h"
|
|
|
|
#include "jsobj.h"
|
2009-10-12 21:12:16 +02:00
|
|
|
#include "client.h"
|
2009-01-19 01:13:12 +01:00
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
|
|
|
/* authentication
|
|
|
|
|
|
|
|
system.users contains
|
2009-01-21 21:06:13 +01:00
|
|
|
{ user : <username>, pwd : <pwd_digest>, ... }
|
2009-01-19 01:13:12 +01:00
|
|
|
|
|
|
|
getnonce sends nonce to client
|
|
|
|
|
2009-01-21 21:06:13 +01:00
|
|
|
client then sends { authenticate:1, nonce:<nonce_str>, user:<username>, key:<key> }
|
2009-01-19 01:13:12 +01:00
|
|
|
|
2009-01-21 21:06:13 +01:00
|
|
|
where <key> is md5(<nonce_str><username><pwd_digest_str>) as a string
|
2009-01-19 01:13:12 +01:00
|
|
|
*/
|
|
|
|
|
2009-01-21 21:06:13 +01:00
|
|
|
boost::thread_specific_ptr<nonce> lastNonce;
|
2009-01-19 01:13:12 +01:00
|
|
|
|
|
|
|
class CmdGetNonce : public Command {
|
|
|
|
public:
|
2009-01-19 02:31:33 +01:00
|
|
|
virtual bool requiresAuth() { return false; }
|
2009-01-19 01:13:12 +01:00
|
|
|
virtual bool logTheOp() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
virtual bool slaveOk() {
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-26 20:38:51 +01:00
|
|
|
virtual LockType locktype(){ return NONE; }
|
2009-01-19 01:13:12 +01:00
|
|
|
CmdGetNonce() : Command("getnonce") {}
|
|
|
|
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
|
2009-01-21 21:06:13 +01:00
|
|
|
nonce *n = new nonce(security.getNonce());
|
2009-01-20 17:37:15 +01:00
|
|
|
stringstream ss;
|
2009-01-21 21:06:13 +01:00
|
|
|
ss << hex << *n;
|
2009-01-20 17:37:15 +01:00
|
|
|
result.append("nonce", ss.str() );
|
2009-01-21 21:06:13 +01:00
|
|
|
lastNonce.reset(n);
|
2009-01-19 01:13:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} cmdGetNonce;
|
|
|
|
|
|
|
|
class CmdLogout : public Command {
|
|
|
|
public:
|
|
|
|
virtual bool logTheOp() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
virtual bool slaveOk() {
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-26 20:38:51 +01:00
|
|
|
virtual LockType locktype(){ return NONE; }
|
2009-01-19 01:13:12 +01:00
|
|
|
CmdLogout() : Command("logout") {}
|
|
|
|
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
|
|
|
|
// database->name is the one we are logging out...
|
2009-10-14 20:34:38 +02:00
|
|
|
Client& client = cc();
|
2010-02-04 16:49:19 +01:00
|
|
|
AuthenticationInfo *ai = client.getAuthenticationInfo();
|
2009-10-14 20:34:38 +02:00
|
|
|
ai->logout(client.database()->name.c_str());
|
2009-01-19 01:13:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} cmdLogout;
|
2009-01-20 20:30:59 +01:00
|
|
|
|
2009-01-19 01:13:12 +01:00
|
|
|
class CmdAuthenticate : public Command {
|
|
|
|
public:
|
2009-01-19 02:31:33 +01:00
|
|
|
virtual bool requiresAuth() { return false; }
|
2009-01-19 01:13:12 +01:00
|
|
|
virtual bool logTheOp() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
virtual bool slaveOk() {
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-26 20:38:51 +01:00
|
|
|
virtual LockType locktype(){ return WRITE; } // TODO: make this READ
|
2009-01-19 01:13:12 +01:00
|
|
|
CmdAuthenticate() : Command("authenticate") {}
|
|
|
|
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
|
2009-01-20 20:30:59 +01:00
|
|
|
log(1) << " authenticate: " << cmdObj << endl;
|
|
|
|
|
2009-01-19 01:13:12 +01:00
|
|
|
string user = cmdObj.getStringField("user");
|
2009-01-20 17:37:15 +01:00
|
|
|
string key = cmdObj.getStringField("key");
|
2009-01-21 21:06:13 +01:00
|
|
|
string received_nonce = cmdObj.getStringField("nonce");
|
2009-01-20 17:37:15 +01:00
|
|
|
|
2009-01-21 21:06:13 +01:00
|
|
|
if( user.empty() || key.empty() || received_nonce.empty() ) {
|
2009-10-14 20:34:38 +02:00
|
|
|
log() << "field missing/wrong type in received authenticate command "
|
|
|
|
<< cc().database()->name
|
|
|
|
<< '\n';
|
2009-01-19 01:13:12 +01:00
|
|
|
errmsg = "auth fails";
|
|
|
|
sleepmillis(10);
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-20 17:37:15 +01:00
|
|
|
|
|
|
|
stringstream digestBuilder;
|
2009-01-19 01:13:12 +01:00
|
|
|
|
|
|
|
{
|
2009-12-09 23:00:14 +01:00
|
|
|
bool reject = false;
|
2009-01-21 21:06:13 +01:00
|
|
|
nonce *ln = lastNonce.release();
|
2009-12-09 23:00:14 +01:00
|
|
|
if ( ln == 0 ) {
|
|
|
|
reject = true;
|
|
|
|
} else {
|
|
|
|
digestBuilder << hex << *ln;
|
|
|
|
reject = digestBuilder.str() != received_nonce;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( reject ) {
|
|
|
|
log() << "auth: bad nonce received or getnonce not called. could be a driver bug or a security attack. db:" << cc().database()->name << '\n';
|
2009-01-19 01:13:12 +01:00
|
|
|
errmsg = "auth fails";
|
|
|
|
sleepmillis(30);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static BSONObj userPattern = fromjson("{\"user\":1}");
|
2009-10-14 20:34:38 +02:00
|
|
|
string systemUsers = cc().database()->name + ".system.users";
|
2009-04-24 17:36:42 +02:00
|
|
|
OCCASIONALLY Helpers::ensureIndex(systemUsers.c_str(), userPattern, false, "user_1");
|
2009-01-19 01:13:12 +01:00
|
|
|
|
|
|
|
BSONObj userObj;
|
|
|
|
{
|
|
|
|
BSONObjBuilder b;
|
|
|
|
b << "user" << user;
|
|
|
|
BSONObj query = b.done();
|
|
|
|
if( !Helpers::findOne(systemUsers.c_str(), query, userObj) ) {
|
|
|
|
log() << "auth: couldn't find user " << user << ", " << systemUsers << '\n';
|
|
|
|
errmsg = "auth fails";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-01-20 17:37:15 +01:00
|
|
|
|
2009-01-19 01:13:12 +01:00
|
|
|
md5digest d;
|
|
|
|
{
|
2009-01-20 17:37:15 +01:00
|
|
|
|
|
|
|
string pwd = userObj.getStringField("pwd");
|
|
|
|
digestBuilder << user << pwd;
|
|
|
|
string done = digestBuilder.str();
|
|
|
|
|
2009-01-19 01:13:12 +01:00
|
|
|
md5_state_t st;
|
|
|
|
md5_init(&st);
|
2009-01-20 17:37:15 +01:00
|
|
|
md5_append(&st, (const md5_byte_t *) done.c_str(), done.size());
|
2009-01-19 01:13:12 +01:00
|
|
|
md5_finish(&st, d);
|
|
|
|
}
|
2009-01-20 17:37:15 +01:00
|
|
|
|
|
|
|
string computed = digestToString( d );
|
|
|
|
|
|
|
|
if ( key != computed ){
|
2009-01-19 01:13:12 +01:00
|
|
|
log() << "auth: key mismatch " << user << ", ns:" << ns << '\n';
|
|
|
|
errmsg = "auth fails";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-04 16:49:19 +01:00
|
|
|
AuthenticationInfo *ai = cc().getAuthenticationInfo();
|
2010-01-27 02:04:09 +01:00
|
|
|
|
|
|
|
if ( userObj[ "readOnly" ].isBoolean() && userObj[ "readOnly" ].boolean() ) {
|
2010-02-05 18:44:08 +01:00
|
|
|
if ( readLockSupported() ){
|
|
|
|
ai->authorizeReadOnly( cc().database()->name.c_str() );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log() << "warning: old version of boost, read-only users not supported" << endl;
|
|
|
|
ai->authorize( cc().database()->name.c_str() );
|
|
|
|
}
|
2010-01-27 02:04:09 +01:00
|
|
|
} else {
|
|
|
|
ai->authorize( cc().database()->name.c_str() );
|
|
|
|
}
|
2009-01-19 01:13:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} cmdAuthenticate;
|
2009-01-20 17:37:15 +01:00
|
|
|
|
2009-01-19 01:13:12 +01:00
|
|
|
} // namespace mongo
|