summaryrefslogtreecommitdiff
path: root/db/security.cpp
blob: 3d78bfe6921780f342c4fd45cea3d70bc72c5c0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// security.cpp

#include "stdafx.h"
#include "security.h"
#include "../util/md5.hpp"

namespace mongo {

extern "C" int do_md5_test(void);

boost::thread_specific_ptr<AuthenticationInfo> authInfo;

typedef unsigned long long nonce;

struct Security { 
    ifstream *devrandom;

    nonce getNonce() { 
        nonce n;
#if defined(__linux__)
        devrandom->read((char*)&n, sizeof(n));
        massert("devrandom failed", !devrandom->fail());
#else
        n = ((unsigned long long)rand())<<32 | rand();
#endif
        return n;
    }

    Security()
    { 
#if defined(__linux__)
        devrandom = new ifstream("/dev/urandom", ios::binary|ios::in);
        massert( "can't open dev/urandom", devrandom->is_open() );
#endif
        assert( sizeof(nonce) == 8 );

        if( do_md5_test() )
            massert("md5 unit test fails", false);
    }
} security;

} // namespace mongo

#include "commands.h"
#include "jsobj.h"

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;


} // namespace mongo