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 std::set<std::string> privsToSet(u64 privs)
30 std::set<std::string> s;
31 if(privs & PRIV_BUILD)
33 if(privs & PRIV_TELEPORT)
35 if(privs & PRIV_SETTIME)
37 if(privs & PRIV_PRIVS)
39 if(privs & PRIV_SHOUT)
48 // Convert a privileges value into a human-readable string,
49 // with each component separated by a comma.
50 std::string privsToString(u64 privs)
52 std::ostringstream os(std::ios_base::binary);
53 if(privs & PRIV_BUILD)
55 if(privs & PRIV_TELEPORT)
57 if(privs & PRIV_SETTIME)
59 if(privs & PRIV_PRIVS)
61 if(privs & PRIV_SHOUT)
69 // Drop the trailing comma. (Why on earth can't
70 // you truncate a C++ stream anyway???)
71 std::string tmp = os.str();
72 return tmp.substr(0, tmp.length() -1);
77 // Converts a comma-seperated list of privilege values into a
78 // privileges value. The reverse of privsToString(). Returns
79 // PRIV_INVALID if there is anything wrong with the input.
80 u64 stringToPrivs(std::string str)
84 while(f.atend() == false)
86 std::string s = trim(f.next(","));
89 else if(s == "teleport")
90 privs |= PRIV_TELEPORT;
91 else if(s == "settime")
92 privs |= PRIV_SETTIME;
107 AuthManager::AuthManager(const std::string &authfilepath):
108 m_authfilepath(authfilepath),
116 catch(SerializationError &e)
118 dstream<<"WARNING: AuthManager: creating "
119 <<m_authfilepath<<std::endl;
123 AuthManager::~AuthManager()
128 void AuthManager::load()
130 JMutexAutoLock lock(m_mutex);
132 dstream<<"AuthManager: loading from "<<m_authfilepath<<std::endl;
133 std::ifstream is(m_authfilepath.c_str(), std::ios::binary);
134 if(is.good() == false)
136 dstream<<"AuthManager: failed loading from "<<m_authfilepath<<std::endl;
137 throw SerializationError("AuthManager::load(): Couldn't open file");
142 if(is.eof() || is.good() == false)
147 std::getline(is, line, '\n');
149 std::istringstream iss(line);
153 std::getline(iss, name, ':');
157 std::getline(iss, pwd, ':');
160 std::string stringprivs;
161 std::getline(iss, stringprivs, ':');
162 u64 privs = stringToPrivs(stringprivs);
168 m_authdata[name] = ad;
174 void AuthManager::save()
176 JMutexAutoLock lock(m_mutex);
178 dstream<<"AuthManager: saving to "<<m_authfilepath<<std::endl;
179 std::ofstream os(m_authfilepath.c_str(), std::ios::binary);
180 if(os.good() == false)
182 dstream<<"AuthManager: failed saving to "<<m_authfilepath<<std::endl;
183 throw SerializationError("AuthManager::save(): Couldn't open file");
186 for(core::map<std::string, AuthData>::Iterator
187 i = m_authdata.getIterator();
188 i.atEnd()==false; i++)
190 std::string name = i.getNode()->getKey();
193 AuthData ad = i.getNode()->getValue();
194 os<<name<<":"<<ad.pwd<<":"<<privsToString(ad.privs)<<"\n";
200 bool AuthManager::exists(const std::string &username)
202 JMutexAutoLock lock(m_mutex);
204 core::map<std::string, AuthData>::Node *n;
205 n = m_authdata.find(username);
211 void AuthManager::set(const std::string &username, AuthData ad)
213 JMutexAutoLock lock(m_mutex);
215 m_authdata[username] = ad;
220 void AuthManager::add(const std::string &username)
222 JMutexAutoLock lock(m_mutex);
224 m_authdata[username] = AuthData();
229 std::string AuthManager::getPassword(const std::string &username)
231 JMutexAutoLock lock(m_mutex);
233 core::map<std::string, AuthData>::Node *n;
234 n = m_authdata.find(username);
236 throw AuthNotFoundException("");
238 return n->getValue().pwd;
241 void AuthManager::setPassword(const std::string &username,
242 const std::string &password)
244 JMutexAutoLock lock(m_mutex);
246 core::map<std::string, AuthData>::Node *n;
247 n = m_authdata.find(username);
249 throw AuthNotFoundException("");
251 AuthData ad = n->getValue();
258 u64 AuthManager::getPrivs(const std::string &username)
260 JMutexAutoLock lock(m_mutex);
262 core::map<std::string, AuthData>::Node *n;
263 n = m_authdata.find(username);
265 throw AuthNotFoundException("");
267 return n->getValue().privs;
270 void AuthManager::setPrivs(const std::string &username, u64 privs)
272 JMutexAutoLock lock(m_mutex);
274 core::map<std::string, AuthData>::Node *n;
275 n = m_authdata.find(username);
277 throw AuthNotFoundException("");
279 AuthData ad = n->getValue();
286 bool AuthManager::isModified()
288 JMutexAutoLock lock(m_mutex);