~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to db/security.h

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// security.h
 
2
 
 
3
/**
 
4
*    Copyright (C) 2009 10gen Inc.
 
5
*
 
6
*    This program is free software: you can redistribute it and/or  modify
 
7
*    it under the terms of the GNU Affero General Public License, version 3,
 
8
*    as published by the Free Software Foundation.
 
9
*
 
10
*    This program is distributed in the hope that it will be useful,
 
11
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
*    GNU Affero General Public License for more details.
 
14
*
 
15
*    You should have received a copy of the GNU Affero General Public License
 
16
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
#pragma once
 
20
 
 
21
#include <boost/thread/tss.hpp>
 
22
#undef assert
 
23
#define assert xassert
 
24
 
 
25
#include "db.h"
 
26
#include "dbhelpers.h"
 
27
#include "nonce.h"
 
28
 
 
29
namespace mongo {
 
30
 
 
31
    // --noauth cmd line option
 
32
    extern bool noauth;
 
33
 
 
34
    /* for a particular db */
 
35
    struct Auth {
 
36
        Auth() { level = 0; }
 
37
        int level;
 
38
    };
 
39
 
 
40
    class AuthenticationInfo : boost::noncopyable {
 
41
        map<string, Auth> m; // dbname -> auth
 
42
                static int warned;
 
43
    public:
 
44
                bool isLocalHost;
 
45
        AuthenticationInfo() { isLocalHost = false; }
 
46
        virtual ~AuthenticationInfo() {
 
47
        }
 
48
        void logout(const char *dbname) { 
 
49
                        assertInWriteLock();
 
50
                        m.erase(dbname); 
 
51
                }
 
52
        void authorize(const char *dbname) { 
 
53
                        assertInWriteLock();
 
54
            m[dbname].level = 2;
 
55
        }
 
56
        virtual bool isAuthorized(const char *dbname) { 
 
57
            if( m[dbname].level == 2 ) return true;
 
58
                        if( noauth ) return true;
 
59
                        if( m["admin"].level == 2 ) return true;
 
60
                        if( m["local"].level == 2 ) return true;
 
61
                        if( isLocalHost ) { 
 
62
                readlock l(""); 
 
63
                Client::Context c("admin.system.users");
 
64
                                BSONObj result;
 
65
                                if( Helpers::getSingleton("admin.system.users", result) )
 
66
                                        return false;
 
67
                                if( warned == 0 ) {
 
68
                                        warned++;
 
69
                                        log() << "warning: no users configured in admin.system.users, allowing localhost access" << endl;
 
70
                                }
 
71
                                return true;
 
72
                        }
 
73
                        return false;
 
74
        }
 
75
    };
 
76
 
 
77
} // namespace mongo