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)
45 if(privs & PRIV_PASSWORD)
50 // Convert a privileges value into a human-readable string,
51 // with each component separated by a comma.
52 std::string privsToString(u64 privs)
54 std::ostringstream os(std::ios_base::binary);
55 if(privs & PRIV_BUILD)
57 if(privs & PRIV_TELEPORT)
59 if(privs & PRIV_SETTIME)
61 if(privs & PRIV_PRIVS)
63 if(privs & PRIV_SHOUT)
69 if(privs & PRIV_PASSWORD)
73 // Drop the trailing comma. (Why on earth can't
74 // you truncate a C++ stream anyway???)
75 std::string tmp = os.str();
76 return tmp.substr(0, tmp.length() -1);
81 // Converts a comma-seperated list of privilege values into a
82 // privileges value. The reverse of privsToString(). Returns
83 // PRIV_INVALID if there is anything wrong with the input.
84 u64 stringToPrivs(std::string str)
88 while(f.atend() == false)
90 std::string s = trim(f.next(","));
93 else if(s == "teleport")
94 privs |= PRIV_TELEPORT;
95 else if(s == "settime")
96 privs |= PRIV_SETTIME;
105 else if(s == "password")
106 privs |= PRIV_PASSWORD;
113 AuthManager::AuthManager(const std::string &authfilepath):
114 m_authfilepath(authfilepath),
122 catch(SerializationError &e)
124 dstream<<"WARNING: AuthManager: creating "
125 <<m_authfilepath<<std::endl;
129 AuthManager::~AuthManager()
134 void AuthManager::load()
136 JMutexAutoLock lock(m_mutex);
138 dstream<<"AuthManager: loading from "<<m_authfilepath<<std::endl;
139 std::ifstream is(m_authfilepath.c_str(), std::ios::binary);
140 if(is.good() == false)
142 dstream<<"AuthManager: failed loading from "<<m_authfilepath<<std::endl;
143 throw SerializationError("AuthManager::load(): Couldn't open file");
148 if(is.eof() || is.good() == false)
153 std::getline(is, line, '\n');
155 std::istringstream iss(line);
159 std::getline(iss, name, ':');
163 std::getline(iss, pwd, ':');
166 std::string stringprivs;
167 std::getline(iss, stringprivs, ':');
168 u64 privs = stringToPrivs(stringprivs);
174 m_authdata[name] = ad;
180 void AuthManager::save()
182 JMutexAutoLock lock(m_mutex);
184 dstream<<"AuthManager: saving to "<<m_authfilepath<<std::endl;
185 std::ofstream os(m_authfilepath.c_str(), std::ios::binary);
186 if(os.good() == false)
188 dstream<<"AuthManager: failed saving to "<<m_authfilepath<<std::endl;
189 throw SerializationError("AuthManager::save(): Couldn't open file");
192 for(core::map<std::string, AuthData>::Iterator
193 i = m_authdata.getIterator();
194 i.atEnd()==false; i++)
196 std::string name = i.getNode()->getKey();
199 AuthData ad = i.getNode()->getValue();
200 os<<name<<":"<<ad.pwd<<":"<<privsToString(ad.privs)<<"\n";
206 bool AuthManager::exists(const std::string &username)
208 JMutexAutoLock lock(m_mutex);
210 core::map<std::string, AuthData>::Node *n;
211 n = m_authdata.find(username);
217 void AuthManager::set(const std::string &username, AuthData ad)
219 JMutexAutoLock lock(m_mutex);
221 m_authdata[username] = ad;
226 void AuthManager::add(const std::string &username)
228 JMutexAutoLock lock(m_mutex);
230 m_authdata[username] = AuthData();
235 std::string AuthManager::getPassword(const std::string &username)
237 JMutexAutoLock lock(m_mutex);
239 core::map<std::string, AuthData>::Node *n;
240 n = m_authdata.find(username);
242 throw AuthNotFoundException("");
244 return n->getValue().pwd;
247 void AuthManager::setPassword(const std::string &username,
248 const std::string &password)
250 JMutexAutoLock lock(m_mutex);
252 core::map<std::string, AuthData>::Node *n;
253 n = m_authdata.find(username);
255 throw AuthNotFoundException("");
257 AuthData ad = n->getValue();
264 u64 AuthManager::getPrivs(const std::string &username)
266 JMutexAutoLock lock(m_mutex);
268 core::map<std::string, AuthData>::Node *n;
269 n = m_authdata.find(username);
271 throw AuthNotFoundException("");
273 return n->getValue().privs;
276 void AuthManager::setPrivs(const std::string &username, u64 privs)
278 JMutexAutoLock lock(m_mutex);
280 core::map<std::string, AuthData>::Node *n;
281 n = m_authdata.find(username);
283 throw AuthNotFoundException("");
285 AuthData ad = n->getValue();
292 bool AuthManager::isModified()
294 JMutexAutoLock lock(m_mutex);