0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/db/security.h

77 lines
2.1 KiB
C
Raw Normal View History

2009-01-19 01:13:12 +01:00
// security.h
/**
* Copyright (C) 2009 10gen Inc.
*
2009-01-10 00:15:30 +01: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 23:17:24 +01:00
#pragma once
#include <boost/thread/tss.hpp>
#undef assert
#define assert xassert
#include "db.h"
#include "dbhelpers.h"
2009-01-23 17:28:29 +01:00
#include "nonce.h"
2009-01-14 23:09:51 +01:00
namespace mongo {
2009-01-19 02:31:33 +01:00
// --noauth cmd line option
extern bool noauth;
/* for a particular db */
struct Auth {
2009-01-19 02:31:33 +01:00
Auth() { level = 0; }
int level;
};
class AuthenticationInfo : boost::noncopyable {
2009-01-19 01:01:51 +01:00
map<string, Auth> m; // dbname -> auth
static int warned;
public:
bool isLocalHost;
AuthenticationInfo() { isLocalHost = false; }
2009-02-18 19:42:32 +01:00
virtual ~AuthenticationInfo() {
}
void logout(const char *dbname) {
assert( dbMutexInfo.isLocked() );
m.erase(dbname);
}
2009-01-19 02:31:33 +01:00
void authorize(const char *dbname) {
assert( dbMutexInfo.isLocked() );
2009-01-19 02:31:33 +01:00
m[dbname].level = 2;
}
2009-01-23 16:16:36 +01:00
virtual bool isAuthorized(const char *dbname) {
if( m[dbname].level == 2 ) return true;
if( noauth ) return true;
if( m["admin"].level == 2 ) return true;
if( m["local"].level == 2 ) return true;
if( isLocalHost ) {
DBContext c("admin.system.users");
BSONObj result;
if( Helpers::getSingleton("admin.system.users", result) )
return false;
if( warned == 0 ) {
warned++;
log() << "warning: no users configured in admin.system.users, allowing localhost access" << endl;
}
return true;
}
return false;
2009-01-19 02:31:33 +01:00
}
};
2009-01-14 23:17:24 +01:00
extern boost::thread_specific_ptr<AuthenticationInfo> authInfo;
2009-01-14 23:17:24 +01:00
} // namespace mongo