Files
mongo/db/security.h

75 lines
2.3 KiB
C
Raw Normal View History

2009-01-18 19:13:12 -05:00
// security.h
/**
* Copyright (C) 2009 10gen Inc.
*
2009-01-09 18:15:30 -05:00
* 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-14 17:17:24 -05:00
#pragma once
2009-01-23 11:28:29 -05:00
#include "nonce.h"
#include "concurrency.h"
2009-01-14 17:09:51 -05:00
namespace mongo {
2009-01-18 20:31:33 -05:00
// --noauth cmd line option
extern bool noauth;
/* for a particular db */
struct Auth {
2009-01-18 20:31:33 -05:00
Auth() { level = 0; }
int level;
};
class AuthenticationInfo : boost::noncopyable {
mongo::mutex _lock;
2009-01-18 19:01:51 -05:00
map<string, Auth> m; // dbname -> auth
static int warned;
public:
bool isLocalHost;
2010-05-26 00:46:49 -04:00
AuthenticationInfo() : _lock("AuthenticationInfo") { isLocalHost = false; }
~AuthenticationInfo() {
}
void logout(const string& dbname ) {
scoped_lock lk(_lock);
m.erase(dbname);
}
void authorize(const string& dbname ) {
scoped_lock lk(_lock);
2009-01-18 20:31:33 -05:00
m[dbname].level = 2;
}
void authorizeReadOnly(const string& dbname) {
scoped_lock lk(_lock);
2010-01-26 17:04:09 -08:00
m[dbname].level = 1;
}
bool isAuthorized(const string& dbname) { return _isAuthorized( dbname, 2 ); }
bool isAuthorizedReads(const string& dbname) { return _isAuthorized( dbname, 1 ); }
bool isAuthorizedForLock(const string& dbname, int lockType ) { return _isAuthorized( dbname , lockType > 0 ? 2 : 1 ); }
void print();
2010-01-26 17:04:09 -08:00
protected:
bool _isAuthorized(const string& dbname, int level) {
2010-01-26 17:04:09 -08:00
if( m[dbname].level >= level ) return true;
if( noauth ) return true;
2010-01-26 17:04:09 -08:00
if( m["admin"].level >= level ) return true;
if( m["local"].level >= level ) return true;
return _isAuthorizedSpecialChecks( dbname );
2009-01-18 20:31:33 -05:00
}
bool _isAuthorizedSpecialChecks( const string& dbname );
};
2009-01-14 17:17:24 -05:00
} // namespace mongo