Add PostgreSQL authentication backend (#9756)
[oweals/minetest.git] / src / database / database-postgresql.h
1 /*
2 Minetest
3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 #pragma once
21
22 #include <string>
23 #include <libpq-fe.h>
24 #include "database.h"
25 #include "util/basic_macros.h"
26
27 class Settings;
28
29 class Database_PostgreSQL: public Database
30 {
31 public:
32         Database_PostgreSQL(const std::string &connect_string);
33         ~Database_PostgreSQL();
34
35         virtual void pingDatabase();
36
37         void beginSave();
38         void endSave();
39         void rollback();
40
41         bool initialized() const;
42
43 protected:
44         // Conversion helpers
45         inline int pg_to_int(PGresult *res, int row, int col)
46         {
47                 return atoi(PQgetvalue(res, row, col));
48         }
49
50         inline u32 pg_to_uint(PGresult *res, int row, int col)
51         {
52                 return (u32) atoi(PQgetvalue(res, row, col));
53         }
54
55         inline float pg_to_float(PGresult *res, int row, int col)
56         {
57                 return (float) atof(PQgetvalue(res, row, col));
58         }
59
60         inline v3s16 pg_to_v3s16(PGresult *res, int row, int col)
61         {
62                 return v3s16(
63                         pg_to_int(res, row, col),
64                         pg_to_int(res, row, col + 1),
65                         pg_to_int(res, row, col + 2)
66                 );
67         }
68
69         inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
70                 const void **params,
71                 const int *paramsLengths = NULL, const int *paramsFormats = NULL,
72                 bool clear = true, bool nobinary = true)
73         {
74                 return checkResults(PQexecPrepared(m_conn, stmtName, paramsNumber,
75                         (const char* const*) params, paramsLengths, paramsFormats,
76                         nobinary ? 1 : 0), clear);
77         }
78
79         inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
80                 const char **params, bool clear = true, bool nobinary = true)
81         {
82                 return execPrepared(stmtName, paramsNumber,
83                         (const void **)params, NULL, NULL, clear, nobinary);
84         }
85
86         void createTableIfNotExists(const std::string &table_name, const std::string &definition);
87
88         // Database initialization
89         void connectToDatabase();
90         virtual void createDatabase() = 0;
91         virtual void initStatements() = 0;
92         inline void prepareStatement(const std::string &name, const std::string &sql)
93         {
94                 checkResults(PQprepare(m_conn, name.c_str(), sql.c_str(), 0, NULL));
95         }
96
97         const int getPGVersion() const { return m_pgversion; }
98 private:
99         // Database connectivity checks
100         void ping();
101
102         // Database usage
103         PGresult *checkResults(PGresult *res, bool clear = true);
104
105         // Attributes
106         std::string m_connect_string;
107         PGconn *m_conn = nullptr;
108         int m_pgversion = 0;
109 };
110
111 class MapDatabasePostgreSQL : private Database_PostgreSQL, public MapDatabase
112 {
113 public:
114         MapDatabasePostgreSQL(const std::string &connect_string);
115         virtual ~MapDatabasePostgreSQL() = default;
116
117         virtual void pingDatabase() { Database_PostgreSQL::pingDatabase(); }
118
119         bool saveBlock(const v3s16 &pos, const std::string &data);
120         void loadBlock(const v3s16 &pos, std::string *block);
121         bool deleteBlock(const v3s16 &pos);
122         void listAllLoadableBlocks(std::vector<v3s16> &dst);
123
124         void beginSave() { Database_PostgreSQL::beginSave(); }
125         void endSave() { Database_PostgreSQL::endSave(); }
126
127 protected:
128         virtual void createDatabase();
129         virtual void initStatements();
130 };
131
132 class PlayerDatabasePostgreSQL : private Database_PostgreSQL, public PlayerDatabase
133 {
134 public:
135         PlayerDatabasePostgreSQL(const std::string &connect_string);
136         virtual ~PlayerDatabasePostgreSQL() = default;
137
138         virtual void pingDatabase() { Database_PostgreSQL::pingDatabase(); }
139
140         void savePlayer(RemotePlayer *player);
141         bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
142         bool removePlayer(const std::string &name);
143         void listPlayers(std::vector<std::string> &res);
144
145 protected:
146         virtual void createDatabase();
147         virtual void initStatements();
148
149 private:
150         bool playerDataExists(const std::string &playername);
151 };
152
153 class AuthDatabasePostgreSQL : private Database_PostgreSQL, public AuthDatabase
154 {
155 public:
156         AuthDatabasePostgreSQL(const std::string &connect_string);
157         virtual ~AuthDatabasePostgreSQL() = default;
158
159         virtual void pingDatabase() { Database_PostgreSQL::pingDatabase(); }
160
161         virtual bool getAuth(const std::string &name, AuthEntry &res);
162         virtual bool saveAuth(const AuthEntry &authEntry);
163         virtual bool createAuth(AuthEntry &authEntry);
164         virtual bool deleteAuth(const std::string &name);
165         virtual void listNames(std::vector<std::string> &res);
166         virtual void reload();
167
168 protected:
169         virtual void createDatabase();
170         virtual void initStatements();
171
172 private:
173         virtual void writePrivileges(const AuthEntry &authEntry);
174 };