Skip to content

Commit

Permalink
FEAT(server): JSON import/export to/from database
Browse files Browse the repository at this point in the history
The dumped JSON format may not be 100% DB backend independent but it
should be somewhat close to it.

Implements mumble-voip#4980
Implements mumble-voip#2960 (must be done manually though)
  • Loading branch information
Krzmbrzl committed Jun 29, 2024
1 parent 1b7b3b4 commit 8077acf
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 27 deletions.
21 changes: 20 additions & 1 deletion src/murmur/DBWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

#include <boost/optional.hpp>

#include <nlohmann/json.hpp>

#include <algorithm>
#include <cassert>
#include <limits>
Expand Down Expand Up @@ -1375,7 +1377,8 @@ unsigned int DBWrapper::getLastChannelID(unsigned int serverID, unsigned int use

assert(inactiveSeconds >= 0);

return maxRememberDuration >= static_cast< unsigned long >(inactiveSeconds) ? userData.lastChannelID : Mumble::ROOT_CHANNEL_ID;
return maxRememberDuration >= static_cast< unsigned long >(inactiveSeconds) ? userData.lastChannelID
: Mumble::ROOT_CHANNEL_ID;

WRAPPER_END
}
Expand Down Expand Up @@ -1576,4 +1579,20 @@ void DBWrapper::setUserData(unsigned int serverID, unsigned int userID, const ::
WRAPPER_END
}

nlohmann::json DBWrapper::exportDBToJSON() {
WRAPPER_BEGIN

return m_serverDB.exportToJSON();

WRAPPER_END
}

void DBWrapper::importFromJSON(const nlohmann::json &json, bool createMissingTables) {
WRAPPER_BEGIN

m_serverDB.importFromJSON(json, createMissingTables);

WRAPPER_END
}

#undef assertValidID
6 changes: 6 additions & 0 deletions src/murmur/DBWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <boost/optional.hpp>

#include <nlohmann/json_fwd.hpp>

#include <exception>
#include <string>
#include <vector>
Expand Down Expand Up @@ -147,6 +149,10 @@ class DBWrapper {

void setUserData(unsigned int serverID, unsigned int userID, const ::mumble::server::db::DBUserData &data);

nlohmann::json exportDBToJSON();

void importFromJSON(const nlohmann::json &json, bool createMissingTables);

protected:
::mumble::server::db::ServerDatabase m_serverDB;
};
Expand Down
92 changes: 66 additions & 26 deletions src/murmur/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# include "DBus.h"
#endif

#include "DBWrapper.h"
#include "EnvUtils.h"
#include "License.h"
#include "LogEmitter.h"
Expand All @@ -20,6 +21,9 @@

#include <cassert>
#include <csignal>
#include <fstream>

#include <nlohmann/json.hpp>

#ifdef Q_OS_WIN
# include "About.h"
Expand Down Expand Up @@ -265,6 +269,8 @@ int main(int argc, char **argv) {

QString inifile;
QString supw;
QString dbDumpPath;
QString dbImportPath;
bool disableSu = false;
bool wipeSsl = false;
bool wipeLogs = false;
Expand Down Expand Up @@ -373,40 +379,49 @@ int main(int argc, char **argv) {
qInfo("%s", qPrintable(License::printableThirdPartyLicenseInfo()));
return 0;
#endif
} else if (arg == "--db-json-dump") {
++i;
dbDumpPath = args.at(i);
} else if (arg == "--db-json-import") {
++i;
dbImportPath = args.at(i);
} else if ((arg == "-h") || (arg == "-help") || (arg == "--help")) {
detach = false;
qInfo("Usage: %s [-ini <inifile>] [-supw <password>]\n"
" --version Print version information and exit\n"
" -ini <inifile> Specify ini file to use.\n"
" -supw <pw> [srv] Set password for 'SuperUser' account on server srv.\n"
qInfo(
"Usage: %s [-ini <inifile>] [-supw <password>]\n"
" --version Print version information and exit\n"
" -ini <inifile> Specify ini file to use.\n"
" -supw <pw> [srv] Set password for 'SuperUser' account on server srv.\n"
#ifdef Q_OS_UNIX
" -readsupw [srv] Reads password for server srv from standard input.\n"
" -readsupw [srv] Reads password for server srv from standard input.\n"
#endif
" -disablesu [srv] Disable password for 'SuperUser' account on server srv.\n"
" -disablesu [srv] Disable password for 'SuperUser' account on server srv.\n"
#ifdef Q_OS_UNIX
" -limits Tests and shows how many file descriptors and threads can be created.\n"
" The purpose of this option is to test how many clients Murmur can handle.\n"
" Murmur will exit after this test.\n"
" -limits Tests and shows how many file descriptors and threads can be created.\n"
" The purpose of this option is to test how many clients Murmur can handle.\n"
" Murmur will exit after this test.\n"
#endif
" -v Use verbose logging (include debug-logs).\n"
" -v Use verbose logging (include debug-logs).\n"
#ifdef Q_OS_UNIX
" -fg Don't detach from console.\n"
" -fg Don't detach from console.\n"
#else
" -fg Don't write to the log file.\n"
#endif
" -wipessl Remove SSL certificates from database.\n"
" -wipelogs Remove all log entries from database.\n"
" -loggroups Turns on logging for group changes for all servers.\n"
" -logacls Turns on logging for ACL changes for all servers.\n"
" -version Show version information.\n"
"\n"
" -license Show Murmur's license.\n"
" -authors Show Murmur's authors.\n"
" -third-party-licenses Show licenses for third-party software used by Murmur.\n"
"\n"
"If no inifile is provided, murmur will search for one in \n"
"default locations.",
qPrintable(args.at(0)));
" -fg Don't write to the log file.\n"
#endif
" -wipessl Remove SSL certificates from database.\n"
" -wipelogs Remove all log entries from database.\n"
" -loggroups Turns on logging for group changes for all servers.\n"
" -logacls Turns on logging for ACL changes for all servers.\n"
" -version Show version information.\n"
" --db-json-dump [file] Requests a JSON dump of the database to be written to the given file\n"
" --db-json-import [file] Reads in the provide JSON file and imports its contents into the database\n"
"\n"
" -license Show Murmur's license.\n"
" -authors Show Murmur's authors.\n"
" -third-party-licenses Show licenses for third-party software used by Murmur.\n"
"\n"
"If no inifile is provided, murmur will search for one in \n"
"default locations.",
qPrintable(args.at(0)));
return 0;
#ifdef Q_OS_UNIX
} else if (arg == "-limits") {
Expand Down Expand Up @@ -442,6 +457,31 @@ int main(int argc, char **argv) {

Meta::mp.read(inifile);

if (!dbDumpPath.isEmpty()) {
DBWrapper wrapper(Meta::getConnectionParameter());

std::ofstream file(dbDumpPath.toStdString());
file << wrapper.exportDBToJSON().dump(2);

qInfo("Dumped JSON representation of database contents to '%s'", qPrintable(dbDumpPath));

return 0;
}

if (!dbImportPath.isEmpty()) {
qInfo("Importing contents of '%s' into database", qPrintable(dbImportPath));
DBWrapper wrapper(Meta::getConnectionParameter());

std::ifstream file(dbImportPath.toStdString());

nlohmann::json json;
file >> json;

wrapper.importFromJSON(json, true);

return 0;
}

// Activating the logging of ACLs and groups via commandLine overwrites whatever is set in the ini file
if (logGroups) {
Meta::mp.bLogGroupChanges = logGroups;
Expand Down

0 comments on commit 8077acf

Please sign in to comment.