1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "ban.h"
22 #include <fstream>
23 #include "threading/mutex_auto_lock.h"
24 #include <sstream>
25 #include <set>
26 #include "util/strfnd.h"
27 #include "util/string.h"
28 #include "log.h"
29 #include "filesys.h"
30
BanManager(const std::string & banfilepath)31 BanManager::BanManager(const std::string &banfilepath):
32 m_banfilepath(banfilepath)
33 {
34 try {
35 load();
36 } catch(SerializationError &e) {
37 infostream << "BanManager: creating "
38 << m_banfilepath << std::endl;
39 }
40 }
41
~BanManager()42 BanManager::~BanManager()
43 {
44 save();
45 }
46
load()47 void BanManager::load()
48 {
49 MutexAutoLock lock(m_mutex);
50 infostream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
51 std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
52 if (!is.good()) {
53 infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
54 throw SerializationError("BanManager::load(): Couldn't open file");
55 }
56
57 while (!is.eof() && is.good()) {
58 std::string line;
59 std::getline(is, line, '\n');
60 Strfnd f(line);
61 std::string ip = trim(f.next("|"));
62 std::string name = trim(f.next("|"));
63 if(!ip.empty()) {
64 m_ips[ip] = name;
65 }
66 }
67 m_modified = false;
68 }
69
save()70 void BanManager::save()
71 {
72 MutexAutoLock lock(m_mutex);
73 infostream << "BanManager: saving to " << m_banfilepath << std::endl;
74 std::ostringstream ss(std::ios_base::binary);
75
76 for (const auto &ip : m_ips)
77 ss << ip.first << "|" << ip.second << "\n";
78
79 if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
80 infostream << "BanManager: failed saving to " << m_banfilepath << std::endl;
81 throw SerializationError("BanManager::save(): Couldn't write file");
82 }
83
84 m_modified = false;
85 }
86
isIpBanned(const std::string & ip)87 bool BanManager::isIpBanned(const std::string &ip)
88 {
89 MutexAutoLock lock(m_mutex);
90 return m_ips.find(ip) != m_ips.end();
91 }
92
getBanDescription(const std::string & ip_or_name)93 std::string BanManager::getBanDescription(const std::string &ip_or_name)
94 {
95 MutexAutoLock lock(m_mutex);
96 std::string s;
97 for (const auto &ip : m_ips) {
98 if (ip.first == ip_or_name || ip.second == ip_or_name
99 || ip_or_name.empty()) {
100 s += ip.first + "|" + ip.second + ", ";
101 }
102 }
103 s = s.substr(0, s.size() - 2);
104 return s;
105 }
106
getBanName(const std::string & ip)107 std::string BanManager::getBanName(const std::string &ip)
108 {
109 MutexAutoLock lock(m_mutex);
110 StringMap::iterator it = m_ips.find(ip);
111 if (it == m_ips.end())
112 return "";
113 return it->second;
114 }
115
add(const std::string & ip,const std::string & name)116 void BanManager::add(const std::string &ip, const std::string &name)
117 {
118 MutexAutoLock lock(m_mutex);
119 m_ips[ip] = name;
120 m_modified = true;
121 }
122
remove(const std::string & ip_or_name)123 void BanManager::remove(const std::string &ip_or_name)
124 {
125 MutexAutoLock lock(m_mutex);
126 for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
127 if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
128 m_ips.erase(it++);
129 m_modified = true;
130 } else {
131 ++it;
132 }
133 }
134 }
135
136
isModified()137 bool BanManager::isModified()
138 {
139 MutexAutoLock lock(m_mutex);
140 return m_modified;
141 }
142
143