Files
mongo/db/security.cpp

80 lines
2.0 KiB
C++
Raw Normal View History

2009-01-07 18:47:11 -05:00
// security.cpp
#include "stdafx.h"
2009-01-09 18:15:30 -05:00
#include "security.h"
2009-01-14 17:17:24 -05:00
#include "../util/md5.hpp"
namespace mongo {
extern "C" int do_md5_test(void);
2009-01-07 18:47:11 -05:00
boost::thread_specific_ptr<AuthenticationInfo> authInfo;
2009-01-09 18:15:30 -05:00
typedef unsigned long long nonce;
2009-01-07 18:47:11 -05:00
struct Security {
ifstream *devrandom;
2009-01-07 18:47:11 -05:00
nonce getNonce() {
nonce n;
2009-01-07 18:47:11 -05:00
#if defined(__linux__)
devrandom->read((char*)&n, sizeof(n));
massert("devrandom failed", !devrandom->fail());
2009-01-07 18:47:11 -05:00
#else
n = ((unsigned long long)rand())<<32 | rand();
2009-01-07 18:47:11 -05:00
#endif
return n;
}
2009-01-07 18:47:11 -05:00
Security()
{
2009-01-07 18:47:11 -05:00
#if defined(__linux__)
devrandom = new ifstream("/dev/urandom", ios::binary|ios::in);
massert( "can't open dev/urandom", devrandom->is_open() );
2009-01-07 18:47:11 -05:00
#endif
assert( sizeof(nonce) == 8 );
2009-01-07 18:47:11 -05:00
if ( do_md5_test() )
massert("md5 unit test fails", false);
}
} security;
2009-01-07 18:47:11 -05:00
2009-01-14 17:17:24 -05:00
} // namespace mongo
2009-01-07 18:47:11 -05:00
#include "commands.h"
#include "jsobj.h"
2009-01-14 17:17:24 -05:00
namespace mongo {
class CmdGetNonce : public Command {
public:
virtual bool logTheOp() {
return false;
}
virtual bool slaveOk() {
return true;
}
CmdGetNonce() : Command("getnonce") {}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
result.append("nonce", (double) security.getNonce());
return true;
}
} cmdGetNonce;
class CmdAuthenticate : public Command {
public:
virtual bool logTheOp() {
return false;
}
virtual bool slaveOk() {
return true;
}
CmdAuthenticate() : Command("authenticate") {}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
return false;
}
} cmdAuthenticate;
2009-01-14 17:17:24 -05:00
} // namespace mongo