player passwords and privileges in world/auth.txt
[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 {
82         m_mutex.Init();
83         
84         try{
85                 load();
86         }
87         catch(SerializationError &e)
88         {
89                 dstream<<"WARNING: AuthManager: creating "
90                                 <<m_authfilepath<<std::endl;
91         }
92 }
93
94 AuthManager::~AuthManager()
95 {
96         save();
97 }
98
99 void AuthManager::load()
100 {
101         JMutexAutoLock lock(m_mutex);
102         
103         dstream<<"AuthManager: loading from "<<m_authfilepath<<std::endl;
104         std::ifstream is(m_authfilepath.c_str(), std::ios::binary);
105         if(is.good() == false)
106         {
107                 dstream<<"AuthManager: failed loading from "<<m_authfilepath<<std::endl;
108                 throw SerializationError("AuthManager::load(): Couldn't open file");
109         }
110
111         for(;;)
112         {
113                 if(is.eof() || is.good() == false)
114                         break;
115
116                 // Read a line
117                 std::string line;
118                 std::getline(is, line, '\n');
119
120                 std::istringstream iss(line);
121                 
122                 // Read name
123                 std::string name;
124                 std::getline(iss, name, ':');
125
126                 // Read password
127                 std::string pwd;
128                 std::getline(iss, pwd, ':');
129
130                 // Read privileges
131                 std::string stringprivs;
132                 std::getline(iss, stringprivs, ':');
133                 u64 privs = stringToPrivs(stringprivs);
134                 
135                 // Store it
136                 AuthData ad;
137                 ad.pwd = pwd;
138                 ad.privs = privs;
139                 m_authdata[name] = ad;
140         }
141 }
142
143 void AuthManager::save()
144 {
145         JMutexAutoLock lock(m_mutex);
146         
147         dstream<<"AuthManager: saving to "<<m_authfilepath<<std::endl;
148         std::ofstream os(m_authfilepath.c_str(), std::ios::binary);
149         if(os.good() == false)
150         {
151                 dstream<<"AuthManager: failed saving to "<<m_authfilepath<<std::endl;
152                 throw SerializationError("AuthManager::save(): Couldn't open file");
153         }
154         
155         for(core::map<std::string, AuthData>::Iterator
156                         i = m_authdata.getIterator();
157                         i.atEnd()==false; i++)
158         {
159                 std::string name = i.getNode()->getKey();
160                 if(name == "")
161                         continue;
162                 AuthData ad = i.getNode()->getValue();
163                 os<<name<<":"<<ad.pwd<<":"<<privsToString(ad.privs)<<"\n";
164         }
165 }
166
167 bool AuthManager::exists(const std::string &username)
168 {
169         JMutexAutoLock lock(m_mutex);
170         
171         core::map<std::string, AuthData>::Node *n;
172         n = m_authdata.find(username);
173         if(n == NULL)
174                 return false;
175         return true;
176 }
177
178 void AuthManager::set(const std::string &username, AuthData ad)
179 {
180         JMutexAutoLock lock(m_mutex);
181         
182         m_authdata[username] = ad;
183 }
184
185 void AuthManager::add(const std::string &username)
186 {
187         JMutexAutoLock lock(m_mutex);
188         
189         m_authdata[username] = AuthData();
190 }
191
192 std::string AuthManager::getPassword(const std::string &username)
193 {
194         JMutexAutoLock lock(m_mutex);
195         
196         core::map<std::string, AuthData>::Node *n;
197         n = m_authdata.find(username);
198         if(n == NULL)
199                 throw AuthNotFoundException("");
200         
201         return n->getValue().pwd;
202 }
203
204 void AuthManager::setPassword(const std::string &username,
205                 const std::string &password)
206 {
207         JMutexAutoLock lock(m_mutex);
208         
209         core::map<std::string, AuthData>::Node *n;
210         n = m_authdata.find(username);
211         if(n == NULL)
212                 throw AuthNotFoundException("");
213         
214         AuthData ad = n->getValue();
215         ad.pwd = password;
216         n->setValue(ad);
217 }
218
219 u64 AuthManager::getPrivs(const std::string &username)
220 {
221         JMutexAutoLock lock(m_mutex);
222         
223         core::map<std::string, AuthData>::Node *n;
224         n = m_authdata.find(username);
225         if(n == NULL)
226                 throw AuthNotFoundException("");
227         
228         return n->getValue().privs;
229 }
230
231 void AuthManager::setPrivs(const std::string &username, u64 privs)
232 {
233         JMutexAutoLock lock(m_mutex);
234         
235         core::map<std::string, AuthData>::Node *n;
236         n = m_authdata.find(username);
237         if(n == NULL)
238                 throw AuthNotFoundException("");
239         
240         AuthData ad = n->getValue();
241         ad.privs = privs;
242         n->setValue(ad);
243 }
244