]> git.lizzy.rs Git - minetest-m13.git/blob - src/auth.h
Update to 4.6 base
[minetest-m13.git] / src / auth.h
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 #ifndef AUTH_HEADER
21 #define AUTH_HEADER
22
23 #include <set>
24 #include <string>
25 #include <jthread.h>
26 #include <jmutex.h>
27 #include "irrlichttypes.h"
28 #include "exceptions.h"
29
30 // Player privileges. These form a bitmask stored in the privs field
31 // of the player, and define things they're allowed to do. See also
32 // the static methods Player::privsToString and stringToPrivs that
33 // convert these to human-readable form.
34 const u64 PRIV_INTERACT = 1;            // Can interact
35 const u64 PRIV_TELEPORT = 2;         // Can teleport
36 const u64 PRIV_SETTIME = 4;          // Can set the time
37 const u64 PRIV_PRIVS = 8;            // Can grant and revoke privileges
38 const u64 PRIV_SERVER = 16;          // Can manage the server (e.g. shutodwn
39                                      // ,settings)
40 const u64 PRIV_SHOUT = 32;           // Can broadcast chat messages to all
41                                      // players
42 const u64 PRIV_BAN = 64;             // Can ban players
43 const u64 PRIV_GIVE = 128;             // Can give stuff
44 const u64 PRIV_PASSWORD = 256;       // Can set other players' passwords
45
46 // Default privileges - these can be overriden for new players using the
47 // config option "default_privs" - however, this value still applies for
48 // players that existed before the privileges system was added.
49 const u64 PRIV_DEFAULT = PRIV_INTERACT|PRIV_SHOUT;
50 const u64 PRIV_ALL = 0x7FFFFFFFFFFFFFFFULL;
51 const u64 PRIV_INVALID = 0x8000000000000000ULL;
52
53 std::set<std::string> privsToSet(u64 privs);
54
55 // Convert a privileges value into a human-readable string,
56 // with each component separated by a comma.
57 std::string privsToString(u64 privs);
58
59 // Converts a comma-seperated list of privilege values into a
60 // privileges value. The reverse of privsToString(). Returns
61 // PRIV_INVALID if there is anything wrong with the input.
62 u64 stringToPrivs(std::string str);
63
64 struct AuthData
65 {
66         std::string pwd;
67         u64 privs;
68
69         AuthData():
70                 privs(PRIV_DEFAULT)
71         {
72         }
73 };
74
75 class AuthNotFoundException : public BaseException
76 {
77 public:
78         AuthNotFoundException(const char *s):
79                 BaseException(s)
80         {}
81 };
82
83 class AuthManager
84 {
85 public:
86         AuthManager(const std::string &authfilepath);
87         ~AuthManager();
88         void load();
89         void save();
90         bool exists(const std::string &username);
91         void set(const std::string &username, AuthData ad);
92         void add(const std::string &username);
93         std::string getPassword(const std::string &username);
94         void setPassword(const std::string &username,
95                         const std::string &password);
96         u64 getPrivs(const std::string &username);
97         void setPrivs(const std::string &username, u64 privs);
98         bool isModified();
99 private:
100         JMutex m_mutex;
101         std::string m_authfilepath;
102         core::map<std::string, AuthData> m_authdata;
103         bool m_modified;
104 };
105
106 #endif
107