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_INTERACT)
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_INTERACT)
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(","));
92 privs |= PRIV_INTERACT;
93 else if(s == "interact")
94 privs |= PRIV_INTERACT;
95 else if(s == "teleport")
96 privs |= PRIV_TELEPORT;
97 else if(s == "settime")
98 privs |= PRIV_SETTIME;
101 else if(s == "shout")
107 else if(s == "password")
108 privs |= PRIV_PASSWORD;
115 AuthManager::AuthManager(const std::string &authfilepath):
116 m_authfilepath(authfilepath),
124 catch(SerializationError &e)
126 infostream<<"WARNING: AuthManager: creating "
127 <<m_authfilepath<<std::endl;
131 AuthManager::~AuthManager()
136 void AuthManager::load()
138 JMutexAutoLock lock(m_mutex);
140 infostream<<"AuthManager: loading from "<<m_authfilepath<<std::endl;
141 std::ifstream is(m_authfilepath.c_str(), std::ios::binary);
142 if(is.good() == false)
144 infostream<<"AuthManager: failed loading from "<<m_authfilepath<<std::endl;
145 throw SerializationError("AuthManager::load(): Couldn't open file");
150 if(is.eof() || is.good() == false)
155 std::getline(is, line, '\n');
157 std::istringstream iss(line);
161 std::getline(iss, name, ':');
165 std::getline(iss, pwd, ':');
168 std::string stringprivs;
169 std::getline(iss, stringprivs, ':');
170 u64 privs = stringToPrivs(stringprivs);
176 m_authdata[name] = ad;
182 void AuthManager::save()
184 JMutexAutoLock lock(m_mutex);
186 infostream<<"AuthManager: saving to "<<m_authfilepath<<std::endl;
187 std::ofstream os(m_authfilepath.c_str(), std::ios::binary);
188 if(os.good() == false)
190 infostream<<"AuthManager: failed saving to "<<m_authfilepath<<std::endl;
191 throw SerializationError("AuthManager::save(): Couldn't open file");
194 for(core::map<std::string, AuthData>::Iterator
195 i = m_authdata.getIterator();
196 i.atEnd()==false; i++)
198 std::string name = i.getNode()->getKey();
201 AuthData ad = i.getNode()->getValue();
202 os<<name<<":"<<ad.pwd<<":"<<privsToString(ad.privs)<<"\n";
208 bool AuthManager::exists(const std::string &username)
210 JMutexAutoLock lock(m_mutex);
212 core::map<std::string, AuthData>::Node *n;
213 n = m_authdata.find(username);
219 void AuthManager::set(const std::string &username, AuthData ad)
221 JMutexAutoLock lock(m_mutex);
223 m_authdata[username] = ad;
228 void AuthManager::add(const std::string &username)
230 JMutexAutoLock lock(m_mutex);
232 m_authdata[username] = AuthData();
237 std::string AuthManager::getPassword(const std::string &username)
239 JMutexAutoLock lock(m_mutex);
241 core::map<std::string, AuthData>::Node *n;
242 n = m_authdata.find(username);
244 throw AuthNotFoundException("");
246 return n->getValue().pwd;
249 void AuthManager::setPassword(const std::string &username,
250 const std::string &password)
252 JMutexAutoLock lock(m_mutex);
254 core::map<std::string, AuthData>::Node *n;
255 n = m_authdata.find(username);
257 throw AuthNotFoundException("");
259 AuthData ad = n->getValue();
266 u64 AuthManager::getPrivs(const std::string &username)
268 JMutexAutoLock lock(m_mutex);
270 core::map<std::string, AuthData>::Node *n;
271 n = m_authdata.find(username);
273 throw AuthNotFoundException("");
275 return n->getValue().privs;
278 void AuthManager::setPrivs(const std::string &username, u64 privs)
280 JMutexAutoLock lock(m_mutex);
282 core::map<std::string, AuthData>::Node *n;
283 n = m_authdata.find(username);
285 throw AuthNotFoundException("");
287 AuthData ad = n->getValue();
294 bool AuthManager::isModified()
296 JMutexAutoLock lock(m_mutex);