Merge branch 'master' of https://github.com/erlehmann/minetest-delta.git into upstrea...
[oweals/minetest.git] / src / auth.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "auth.h"
21 #include <fstream>
22 #include <jmutexautolock.h>
23 //#include "main.h" // for g_settings
24 #include <sstream>
25 #include "strfnd.h"
26 #include "debug.h"
27
28 // Convert a privileges value into a human-readable string,
29 // with each component separated by a comma.
30 std::string privsToString(u64 privs)
31 {
32         std::ostringstream os(std::ios_base::binary);
33         if(privs & PRIV_BUILD)
34                 os<<"build,";
35         if(privs & PRIV_TELEPORT)
36                 os<<"teleport,";
37         if(privs & PRIV_SETTIME)
38                 os<<"settime,";
39         if(privs & PRIV_PRIVS)
40                 os<<"privs,";
41         if(privs & PRIV_SHOUT)
42                 os<<"shout,";
43         if(os.tellp())
44         {
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);
49         }
50         return os.str();
51 }
52
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)
57 {
58         u64 privs=0;
59         Strfnd f(str);
60         while(f.atend() == false)
61         {
62                 std::string s = trim(f.next(","));
63                 if(s == "build")
64                         privs |= PRIV_BUILD;
65                 else if(s == "teleport")
66                         privs |= PRIV_TELEPORT;
67                 else if(s == "settime")
68                         privs |= PRIV_SETTIME;
69                 else if(s == "privs")
70                         privs |= PRIV_PRIVS;
71                 else if(s == "shout")
72                         privs |= PRIV_SHOUT;
73                 else
74                         return PRIV_INVALID;
75         }
76         return privs;
77 }
78
79 AuthManager::AuthManager(const std::string &authfilepath):
80                 m_authfilepath(authfilepath),
81                 m_modified(false)
82 {
83         m_mutex.Init();
84         
85         try{
86                 load();
87         }
88         catch(SerializationError &e)
89         {
90                 dstream<<"WARNING: AuthManager: creating "
91                                 <<m_authfilepath<<std::endl;
92         }
93 }
94
95 AuthManager::~AuthManager()
96 {
97         save();
98 }
99
100 void AuthManager::load()
101 {
102         JMutexAutoLock lock(m_mutex);
103         
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)
107         {
108                 dstream<<"AuthManager: failed loading from "<<m_authfilepath<<std::endl;
109                 throw SerializationError("AuthManager::load(): Couldn't open file");
110         }
111
112         for(;;)
113         {
114                 if(is.eof() || is.good() == false)
115                         break;
116
117                 // Read a line
118                 std::string line;
119                 std::getline(is, line, '\n');
120
121                 std::istringstream iss(line);
122                 
123                 // Read name
124                 std::string name;
125                 std::getline(iss, name, ':');
126
127                 // Read password
128                 std::string pwd;
129                 std::getline(iss, pwd, ':');
130
131                 // Read privileges
132                 std::string stringprivs;
133                 std::getline(iss, stringprivs, ':');
134                 u64 privs = stringToPrivs(stringprivs);
135                 
136                 // Store it
137                 AuthData ad;
138                 ad.pwd = pwd;
139                 ad.privs = privs;
140                 m_authdata[name] = ad;
141         }
142
143         m_modified = false;
144 }
145
146 void AuthManager::save()
147 {
148         JMutexAutoLock lock(m_mutex);
149         
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)
153         {
154                 dstream<<"AuthManager: failed saving to "<<m_authfilepath<<std::endl;
155                 throw SerializationError("AuthManager::save(): Couldn't open file");
156         }
157         
158         for(core::map<std::string, AuthData>::Iterator
159                         i = m_authdata.getIterator();
160                         i.atEnd()==false; i++)
161         {
162                 std::string name = i.getNode()->getKey();
163                 if(name == "")
164                         continue;
165                 AuthData ad = i.getNode()->getValue();
166                 os<<name<<":"<<ad.pwd<<":"<<privsToString(ad.privs)<<"\n";
167         }
168
169         m_modified = false;
170 }
171
172 bool AuthManager::exists(const std::string &username)
173 {
174         JMutexAutoLock lock(m_mutex);
175         
176         core::map<std::string, AuthData>::Node *n;
177         n = m_authdata.find(username);
178         if(n == NULL)
179                 return false;
180         return true;
181 }
182
183 void AuthManager::set(const std::string &username, AuthData ad)
184 {
185         JMutexAutoLock lock(m_mutex);
186         
187         m_authdata[username] = ad;
188
189         m_modified = true;
190 }
191
192 void AuthManager::add(const std::string &username)
193 {
194         JMutexAutoLock lock(m_mutex);
195         
196         m_authdata[username] = AuthData();
197
198         m_modified = true;
199 }
200
201 std::string AuthManager::getPassword(const std::string &username)
202 {
203         JMutexAutoLock lock(m_mutex);
204         
205         core::map<std::string, AuthData>::Node *n;
206         n = m_authdata.find(username);
207         if(n == NULL)
208                 throw AuthNotFoundException("");
209         
210         return n->getValue().pwd;
211 }
212
213 void AuthManager::setPassword(const std::string &username,
214                 const std::string &password)
215 {
216         JMutexAutoLock lock(m_mutex);
217         
218         core::map<std::string, AuthData>::Node *n;
219         n = m_authdata.find(username);
220         if(n == NULL)
221                 throw AuthNotFoundException("");
222         
223         AuthData ad = n->getValue();
224         ad.pwd = password;
225         n->setValue(ad);
226
227         m_modified = true;
228 }
229
230 u64 AuthManager::getPrivs(const std::string &username)
231 {
232         JMutexAutoLock lock(m_mutex);
233         
234         core::map<std::string, AuthData>::Node *n;
235         n = m_authdata.find(username);
236         if(n == NULL)
237                 throw AuthNotFoundException("");
238         
239         return n->getValue().privs;
240 }
241
242 void AuthManager::setPrivs(const std::string &username, u64 privs)
243 {
244         JMutexAutoLock lock(m_mutex);
245         
246         core::map<std::string, AuthData>::Node *n;
247         n = m_authdata.find(username);
248         if(n == NULL)
249                 throw AuthNotFoundException("");
250         
251         AuthData ad = n->getValue();
252         ad.privs = privs;
253         n->setValue(ad);
254
255         m_modified = true;
256 }
257
258 bool AuthManager::isModified()
259 {
260         JMutexAutoLock lock(m_mutex);
261         return m_modified;
262 }
263
264