authplugin.h
5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
FlashMQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef AUTHPLUGIN_H
#define AUTHPLUGIN_H
#include <string>
#include <cstring>
#include "enums.h"
#include "logger.h"
#include "configfileparser.h"
#include "acltree.h"
/**
* @brief The MosquittoPasswordFileEntry struct stores the decoded base64 password salt and hash.
*
* The Mosquitto encrypted format looks like that of crypt(2), but it's not. This is an example entry:
*
* one:$6$emTXKCHfxMnZLDWg$gDcJRPojvOX8l7W/DRhSPoxV3CgPfECJVGRzw2Sqjdc2KIQ/CVLS1mNEuZUsp/vLdj7RCuqXCkgG43+XIc8WBA==
*
* $ is the seperator. '6' is hard-coded by the 'mosquitto_passwd' utility.
*/
struct MosquittoPasswordFileEntry
{
std::vector<char> salt;
std::vector<char> cryptedPassword;
MosquittoPasswordFileEntry(const std::vector<char> &&salt, const std::vector<char> &&cryptedPassword);
// The plan was that objects of this type wouldn't be copied, but I can't get emplacing to work without it...?
//MosquittoPasswordFileEntry(const MosquittoPasswordFileEntry &other) = delete;
};
typedef int (*F_auth_plugin_version)(void);
typedef int (*F_auth_plugin_init_v2)(void **, struct mosquitto_auth_opt *, int);
typedef int (*F_auth_plugin_cleanup_v2)(void *, struct mosquitto_auth_opt *, int);
typedef int (*F_auth_plugin_security_init_v2)(void *, struct mosquitto_auth_opt *, int, bool);
typedef int (*F_auth_plugin_security_cleanup_v2)(void *, struct mosquitto_auth_opt *, int, bool);
typedef int (*F_auth_plugin_acl_check_v2)(void *, const char *, const char *, const char *, int);
typedef int (*F_auth_plugin_unpwd_check_v2)(void *, const char *, const char *);
typedef int (*F_auth_plugin_psk_key_get_v2)(void *, const char *, const char *, char *, int);
extern "C"
{
// Gets called by the plugin, so it needs to exist, globally
void mosquitto_log_printf(int level, const char *fmt, ...);
}
std::string AuthResultToString(AuthResult r);
/**
* @brief The Authentication class handles our integrated authentication, but also supports loading Mosquitto auth
* plugin compatible .so files.
*/
class Authentication
{
F_auth_plugin_version version = nullptr;
F_auth_plugin_init_v2 init_v2 = nullptr;
F_auth_plugin_cleanup_v2 cleanup_v2 = nullptr;
F_auth_plugin_security_init_v2 security_init_v2 = nullptr;
F_auth_plugin_security_cleanup_v2 security_cleanup_v2 = nullptr;
F_auth_plugin_acl_check_v2 acl_check_v2 = nullptr;
F_auth_plugin_unpwd_check_v2 unpwd_check_v2 = nullptr;
F_auth_plugin_psk_key_get_v2 psk_key_get_v2 = nullptr;
static std::mutex initMutex;
static std::mutex authChecksMutex;
Settings &settings; // A ref because I want it to always be the same as the thread's settings
void *pluginData = nullptr;
Logger *logger = nullptr;
bool initialized = false;
bool useExternalPlugin = false;
bool quitting = false;
/**
* @brief mosquittoPasswordFile is a once set value based on config. It's not reloaded on reload signal currently, because it
* forces some decisions when you change files or remove the config option. For instance, do you remove all accounts loaded
* from the previous one? Perhaps I'm overthinking it.
*
* Its content is, however, reloaded every two seconds.
*/
const std::string mosquittoPasswordFile;
const std::string mosquittoAclFile;
struct timespec mosquittoPasswordFileLastLoad;
struct timespec mosquittoAclFileLastChange;
std::unique_ptr<std::unordered_map<std::string, MosquittoPasswordFileEntry>> mosquittoPasswordEntries;
EVP_MD_CTX *mosquittoDigestContext = nullptr;
const EVP_MD *sha512 = EVP_sha512();
AclTree aclTree;
void *loadSymbol(void *handle, const char *symbol) const;
public:
Authentication(Settings &settings);
Authentication(const Authentication &other) = delete;
Authentication(Authentication &&other) = delete;
~Authentication();
void loadPlugin(const std::string &pathToSoFile);
void init();
void cleanup();
void securityInit(bool reloading);
void securityCleanup(bool reloading);
AuthResult aclCheck(const std::string &clientid, const std::string &username, const std::string &topic, const std::vector<std::string> &subtopics, AclAccess access);
AuthResult unPwdCheck(const std::string &username, const std::string &password);
void setQuitting();
void loadMosquittoPasswordFile();
void loadMosquittoAclFile();
AuthResult aclCheckFromMosquittoAclFile(const std::string &clientid, const std::string &username, const std::vector<std::string> &subtopics, AclAccess access);
AuthResult unPwdCheckFromMosquittoPasswordFile(const std::string &username, const std::string &password);
};
#endif // AUTHPLUGIN_H