The Battle for Wesnoth  1.19.0-dev
blacklist.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
17 
18 #include "log.hpp"
21 
22 static lg::log_domain log_campaignd_bl("campaignd/blacklist");
23 #define LOG_BL LOG_STREAM(err, log_campaignd_bl)
24 
25 namespace campaignd
26 {
27 
29  : names_()
30  , titles_()
31  , descriptions_()
32  , authors_()
33  , ips_()
34  , emails_()
35 {
36 }
37 
39  : names_()
40  , titles_()
41  , descriptions_()
42  , authors_()
43  , ips_()
44  , emails_()
45 {
46  this->read(cfg);
47 }
48 
50 {
51  names_.clear();
52  titles_.clear();
53  descriptions_.clear();
54 
55  authors_.clear();
56  ips_.clear();
57  emails_.clear();
58 }
59 
60 void blacklist::read(const config& cfg)
61 {
62  parse_str_to_globlist(cfg["name"], names_);
63  parse_str_to_globlist(cfg["title"], titles_);
64  parse_str_to_globlist(cfg["description"], descriptions_);
65 
66  parse_str_to_globlist(cfg["author"], authors_);
67  parse_str_to_globlist(cfg["ip"], ips_);
68  parse_str_to_globlist(cfg["email"], emails_);
69 }
70 
71 void blacklist::parse_str_to_globlist(const std::string& str, blacklist::globlist& glist)
72 {
73  glist = utils::split(str);
74 }
75 
76 bool blacklist::is_blacklisted(const std::string& name,
77  const std::string& title,
78  const std::string& description,
79  const std::string& author,
80  const std::string& ip,
81  const std::string& email) const
82 {
83  // Checks done in increasing order of performance impact and decreasing
84  // order of relevance.
85  return is_in_ip_masklist(ip, ips_) ||
86  is_in_globlist(email, emails_) ||
87  is_in_globlist(name, names_) ||
88  is_in_globlist(title, titles_) ||
89  is_in_globlist(author, authors_) ||
90  is_in_globlist(description, descriptions_);
91 }
92 
93 bool blacklist::is_in_globlist(const std::string& str, const blacklist::globlist& glist) const
94 {
95  if (!str.empty())
96  {
97  const std::string& lc_str = utf8::lowercase(str);
98  for(const std::string& glob : glist)
99  {
100  const std::string& lc_glob = utf8::lowercase(glob);
101  if (utils::wildcard_string_match(lc_str, lc_glob)) {
102  LOG_BL << "Blacklisted field found: " << str << " (" << glob << ")";
103  return true;
104  }
105  }
106  }
107 
108  return false;
109 }
110 
111 bool blacklist::is_in_ip_masklist(const std::string& ip, const blacklist::globlist& mlist) const
112 {
113  if (!ip.empty())
114  {
115  for(const std::string& ip_mask : mlist)
116  {
117  if (ip_matches(ip, ip_mask)) {
118  LOG_BL << "Blacklisted IP found: " << ip << " (" << ip_mask << ")";
119  return true;
120  }
121  }
122  }
123 
124  return false;
125 }
126 
127 bool blacklist::ip_matches(const std::string& ip, const std::string& ip_mask) const
128 {
129  // TODO: we want CIDR subnet mask matching here, not glob matching!
131 }
132 
133 }
#define LOG_BL
Definition: blacklist.cpp:23
static lg::log_domain log_campaignd_bl("campaignd/blacklist")
bool ip_matches(const std::string &ip, const std::string &ip_mask) const
Definition: blacklist.cpp:127
globlist descriptions_
Definition: blacklist.hpp:85
void read(const config &cfg)
Initializes the blacklist from WML.
Definition: blacklist.cpp:60
void parse_str_to_globlist(const std::string &str, globlist &glist)
Definition: blacklist.cpp:71
std::vector< std::string > globlist
Definition: blacklist.hpp:45
bool is_in_globlist(const std::string &str, const globlist &glist) const
Definition: blacklist.cpp:93
bool is_in_ip_masklist(const std::string &ip, const globlist &mlist) const
Definition: blacklist.cpp:111
bool is_blacklisted(const std::string &name, const std::string &title, const std::string &description, const std::string &author, const std::string &ip, const std::string &email) const
Whether an add-on described by these fields is blacklisted.
Definition: blacklist.cpp:76
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Standard logging facilities (interface).
std::string lowercase(const std::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:50
bool wildcard_string_match(const std::string &str, const std::string &match)
Match using '*' as any number of characters (including none), '+' as one or more characters,...
std::vector< std::string > split(const config_attribute_value &val)
std::pair< unsigned int, unsigned int > ip_mask
Definition: ban.hpp:56