]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_vhost.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / modules / m_vhost.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
5  *   Copyright (C) 2013, 2018, 2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2012 Attila Molnar <attilamolnar@hush.com>
8  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
10  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11  *   Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28
29 struct CustomVhost
30 {
31         const std::string name;
32         const std::string password;
33         const std::string hash;
34         const std::string vhost;
35
36         CustomVhost(const std::string& n, const std::string& p, const std::string& h, const std::string& v)
37                 : name(n)
38                 , password(p)
39                 , hash(h)
40                 , vhost(v)
41         {
42         }
43
44         bool CheckPass(User* user, const std::string& pass) const
45         {
46                 return ServerInstance->PassCompare(user, password, pass, hash);
47         }
48 };
49
50 typedef std::multimap<std::string, CustomVhost> CustomVhostMap;
51 typedef std::pair<CustomVhostMap::iterator, CustomVhostMap::iterator> MatchingConfigs;
52
53 /** Handle /VHOST
54  */
55 class CommandVhost : public Command
56 {
57  public:
58         CustomVhostMap vhosts;
59
60         CommandVhost(Module* Creator)
61                 : Command(Creator, "VHOST", 2)
62         {
63                 syntax = "<username> <password>";
64         }
65
66         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
67         {
68                 MatchingConfigs matching = vhosts.equal_range(parameters[0]);
69
70                 for (MatchingConfigs::first_type i = matching.first; i != matching.second; ++i)
71                 {
72                         CustomVhost config = i->second;
73                         if (config.CheckPass(user, parameters[1]))
74                         {
75                                 user->WriteNotice("Setting your VHost: " + config.vhost);
76                                 user->ChangeDisplayedHost(config.vhost);
77                                 return CMD_SUCCESS;
78                         }
79                 }
80
81                 user->WriteNotice("Invalid username or password.");
82                 return CMD_FAILURE;
83         }
84 };
85
86 class ModuleVHost : public Module
87 {
88         CommandVhost cmd;
89
90  public:
91         ModuleVHost()
92                 : cmd(this)
93         {
94         }
95
96         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
97         {
98                 CustomVhostMap newhosts;
99                 ConfigTagList tags = ServerInstance->Config->ConfTags("vhost");
100                 for (ConfigIter i = tags.first; i != tags.second; ++i)
101                 {
102                         ConfigTag* tag = i->second;
103                         std::string mask = tag->getString("host");
104                         if (mask.empty())
105                                 throw ModuleException("<vhost:host> is empty! at " + tag->getTagLocation());
106
107                         std::string username = tag->getString("user");
108                         if (username.empty())
109                                 throw ModuleException("<vhost:user> is empty! at " + tag->getTagLocation());
110
111                         std::string pass = tag->getString("pass");
112                         if (pass.empty())
113                                 throw ModuleException("<vhost:pass> is empty! at " + tag->getTagLocation());
114
115                         const std::string hash = tag->getString("hash", "plaintext", 1);
116                         if (stdalgo::string::equalsci(hash, "plaintext"))
117                         {
118                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "<vhost> tag for %s at %s contains an plain text password, this is insecure!",
119                                         username.c_str(), tag->getTagLocation().c_str());
120                         }
121
122                         CustomVhost vhost(username, pass, hash, mask);
123                         newhosts.insert(std::make_pair(username, vhost));
124                 }
125
126                 cmd.vhosts.swap(newhosts);
127         }
128
129         Version GetVersion() CXX11_OVERRIDE
130         {
131                 return Version("Allows the server administrator to define accounts which can grant a custom virtual host.", VF_VENDOR);
132         }
133 };
134
135 MODULE_INIT(ModuleVHost)