]> git.lizzy.rs Git - minetest.git/blob - src/auth.h
ObjectRef:get_player_name, ObjectRef:inventory_set_list, ObjectRef:inventory_get_list
[minetest.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_BUILD = 1;            // Can build - i.e. modify the world
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
45 // Default privileges - these can be overriden for new players using the
46 // config option "default_privs" - however, this value still applies for
47 // players that existed before the privileges system was added.
48 const u64 PRIV_DEFAULT = PRIV_BUILD|PRIV_SHOUT;
49 const u64 PRIV_ALL = 0x7FFFFFFFFFFFFFFFULL;
50 const u64 PRIV_INVALID = 0x8000000000000000ULL;
51
52 std::set<std::string> privsToSet(u64 privs);
53
54 // Convert a privileges value into a human-readable string,
55 // with each component separated by a comma.
56 std::string privsToString(u64 privs);
57
58 // Converts a comma-seperated list of privilege values into a
59 // privileges value. The reverse of privsToString(). Returns
60 // PRIV_INVALID if there is anything wrong with the input.
61 u64 stringToPrivs(std::string str);
62
63 struct AuthData
64 {
65         std::string pwd;
66         u64 privs;
67
68         AuthData():
69                 privs(PRIV_DEFAULT)
70         {
71         }
72 };
73
74 class AuthNotFoundException : public BaseException
75 {
76 public:
77         AuthNotFoundException(const char *s):
78                 BaseException(s)
79         {}
80 };
81
82 class AuthManager
83 {
84 public:
85         AuthManager(const std::string &authfilepath);
86         ~AuthManager();
87         void load();
88         void save();
89         bool exists(const std::string &username);
90         void set(const std::string &username, AuthData ad);
91         void add(const std::string &username);
92         std::string getPassword(const std::string &username);
93         void setPassword(const std::string &username,
94                         const std::string &password);
95         u64 getPrivs(const std::string &username);
96         void setPrivs(const std::string &username, u64 privs);
97         bool isModified();
98 private:
99         JMutex m_mutex;
100         std::string m_authfilepath;
101         core::map<std::string, AuthData> m_authdata;
102         bool m_modified;
103 };
104
105 #endif
106