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)
45 // Drop the trailing comma. (Why on earth can't
46 // you truncate a C++ stream anyway???)
47 std::string tmp = os.str();
48 return tmp.substr(0, tmp.length() -1);
53 // Converts a comma-seperated list of privilege values into a
54 // privileges value. The reverse of privsToString(). Returns
55 // PRIV_INVALID if there is anything wrong with the input.
56 u64 stringToPrivs(std::string str)
60 while(f.atend() == false)
62 std::string s = trim(f.next(","));
65 else if(s == "teleport")
66 privs |= PRIV_TELEPORT;
67 else if(s == "settime")
68 privs |= PRIV_SETTIME;
79 AuthManager::AuthManager(const std::string &authfilepath):
80 m_authfilepath(authfilepath),
88 catch(SerializationError &e)
90 dstream<<"WARNING: AuthManager: creating "
91 <<m_authfilepath<<std::endl;
95 AuthManager::~AuthManager()
100 void AuthManager::load()
102 JMutexAutoLock lock(m_mutex);
104 dstream<<"AuthManager: loading from "<<m_authfilepath<<std::endl;
105 std::ifstream is(m_authfilepath.c_str(), std::ios::binary);
106 if(is.good() == false)
108 dstream<<"AuthManager: failed loading from "<<m_authfilepath<<std::endl;
109 throw SerializationError("AuthManager::load(): Couldn't open file");
114 if(is.eof() || is.good() == false)
119 std::getline(is, line, '\n');
121 std::istringstream iss(line);
125 std::getline(iss, name, ':');
129 std::getline(iss, pwd, ':');
132 std::string stringprivs;
133 std::getline(iss, stringprivs, ':');
134 u64 privs = stringToPrivs(stringprivs);
140 m_authdata[name] = ad;
146 void AuthManager::save()
148 JMutexAutoLock lock(m_mutex);
150 dstream<<"AuthManager: saving to "<<m_authfilepath<<std::endl;
151 std::ofstream os(m_authfilepath.c_str(), std::ios::binary);
152 if(os.good() == false)
154 dstream<<"AuthManager: failed saving to "<<m_authfilepath<<std::endl;
155 throw SerializationError("AuthManager::save(): Couldn't open file");
158 for(core::map<std::string, AuthData>::Iterator
159 i = m_authdata.getIterator();
160 i.atEnd()==false; i++)
162 std::string name = i.getNode()->getKey();
165 AuthData ad = i.getNode()->getValue();
166 os<<name<<":"<<ad.pwd<<":"<<privsToString(ad.privs)<<"\n";
172 bool AuthManager::exists(const std::string &username)
174 JMutexAutoLock lock(m_mutex);
176 core::map<std::string, AuthData>::Node *n;
177 n = m_authdata.find(username);
183 void AuthManager::set(const std::string &username, AuthData ad)
185 JMutexAutoLock lock(m_mutex);
187 m_authdata[username] = ad;
192 void AuthManager::add(const std::string &username)
194 JMutexAutoLock lock(m_mutex);
196 m_authdata[username] = AuthData();
201 std::string AuthManager::getPassword(const std::string &username)
203 JMutexAutoLock lock(m_mutex);
205 core::map<std::string, AuthData>::Node *n;
206 n = m_authdata.find(username);
208 throw AuthNotFoundException("");
210 return n->getValue().pwd;
213 void AuthManager::setPassword(const std::string &username,
214 const std::string &password)
216 JMutexAutoLock lock(m_mutex);
218 core::map<std::string, AuthData>::Node *n;
219 n = m_authdata.find(username);
221 throw AuthNotFoundException("");
223 AuthData ad = n->getValue();
230 u64 AuthManager::getPrivs(const std::string &username)
232 JMutexAutoLock lock(m_mutex);
234 core::map<std::string, AuthData>::Node *n;
235 n = m_authdata.find(username);
237 throw AuthNotFoundException("");
239 return n->getValue().privs;
242 void AuthManager::setPrivs(const std::string &username, u64 privs)
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 bool AuthManager::isModified()
260 JMutexAutoLock lock(m_mutex);