3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <jmutexautolock.h>
23 //#include "main.h" // for g_settings
28 // Convert a privileges value into a human-readable string,
29 // with each component separated by a comma.
30 std::string privsToString(u64 privs)
32 std::ostringstream os(std::ios_base::binary);
33 if(privs & PRIV_BUILD)
35 if(privs & PRIV_TELEPORT)
37 if(privs & PRIV_SETTIME)
39 if(privs & PRIV_PRIVS)
41 if(privs & PRIV_SHOUT)
47 // Drop the trailing comma. (Why on earth can't
48 // you truncate a C++ stream anyway???)
49 std::string tmp = os.str();
50 return tmp.substr(0, tmp.length() -1);
55 // Converts a comma-seperated list of privilege values into a
56 // privileges value. The reverse of privsToString(). Returns
57 // PRIV_INVALID if there is anything wrong with the input.
58 u64 stringToPrivs(std::string str)
62 while(f.atend() == false)
64 std::string s = trim(f.next(","));
67 else if(s == "teleport")
68 privs |= PRIV_TELEPORT;
69 else if(s == "settime")
70 privs |= PRIV_SETTIME;
83 AuthManager::AuthManager(const std::string &authfilepath):
84 m_authfilepath(authfilepath),
92 catch(SerializationError &e)
94 dstream<<"WARNING: AuthManager: creating "
95 <<m_authfilepath<<std::endl;
99 AuthManager::~AuthManager()
104 void AuthManager::load()
106 JMutexAutoLock lock(m_mutex);
108 dstream<<"AuthManager: loading from "<<m_authfilepath<<std::endl;
109 std::ifstream is(m_authfilepath.c_str(), std::ios::binary);
110 if(is.good() == false)
112 dstream<<"AuthManager: failed loading from "<<m_authfilepath<<std::endl;
113 throw SerializationError("AuthManager::load(): Couldn't open file");
118 if(is.eof() || is.good() == false)
123 std::getline(is, line, '\n');
125 std::istringstream iss(line);
129 std::getline(iss, name, ':');
133 std::getline(iss, pwd, ':');
136 std::string stringprivs;
137 std::getline(iss, stringprivs, ':');
138 u64 privs = stringToPrivs(stringprivs);
144 m_authdata[name] = ad;
150 void AuthManager::save()
152 JMutexAutoLock lock(m_mutex);
154 dstream<<"AuthManager: saving to "<<m_authfilepath<<std::endl;
155 std::ofstream os(m_authfilepath.c_str(), std::ios::binary);
156 if(os.good() == false)
158 dstream<<"AuthManager: failed saving to "<<m_authfilepath<<std::endl;
159 throw SerializationError("AuthManager::save(): Couldn't open file");
162 for(core::map<std::string, AuthData>::Iterator
163 i = m_authdata.getIterator();
164 i.atEnd()==false; i++)
166 std::string name = i.getNode()->getKey();
169 AuthData ad = i.getNode()->getValue();
170 os<<name<<":"<<ad.pwd<<":"<<privsToString(ad.privs)<<"\n";
176 bool AuthManager::exists(const std::string &username)
178 JMutexAutoLock lock(m_mutex);
180 core::map<std::string, AuthData>::Node *n;
181 n = m_authdata.find(username);
187 void AuthManager::set(const std::string &username, AuthData ad)
189 JMutexAutoLock lock(m_mutex);
191 m_authdata[username] = ad;
196 void AuthManager::add(const std::string &username)
198 JMutexAutoLock lock(m_mutex);
200 m_authdata[username] = AuthData();
205 std::string AuthManager::getPassword(const std::string &username)
207 JMutexAutoLock lock(m_mutex);
209 core::map<std::string, AuthData>::Node *n;
210 n = m_authdata.find(username);
212 throw AuthNotFoundException("");
214 return n->getValue().pwd;
217 void AuthManager::setPassword(const std::string &username,
218 const std::string &password)
220 JMutexAutoLock lock(m_mutex);
222 core::map<std::string, AuthData>::Node *n;
223 n = m_authdata.find(username);
225 throw AuthNotFoundException("");
227 AuthData ad = n->getValue();
234 u64 AuthManager::getPrivs(const std::string &username)
236 JMutexAutoLock lock(m_mutex);
238 core::map<std::string, AuthData>::Node *n;
239 n = m_authdata.find(username);
241 throw AuthNotFoundException("");
243 return n->getValue().privs;
246 void AuthManager::setPrivs(const std::string &username, u64 privs)
248 JMutexAutoLock lock(m_mutex);
250 core::map<std::string, AuthData>::Node *n;
251 n = m_authdata.find(username);
253 throw AuthNotFoundException("");
255 AuthData ad = n->getValue();
262 bool AuthManager::isModified()
264 JMutexAutoLock lock(m_mutex);