2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2013 Shawn Smith <ShawnSmith0828@gmail.com>
6 * Copyright (C) 2012-2014 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
11 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12 * Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
14 * This file is part of InspIRCd. InspIRCd is free software: you can
15 * redistribute it and/or modify it under the terms of the GNU General Public
16 * License as published by the Free Software Foundation, version 2.
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "modules/ctctags.h"
30 #include "modules/ssl.h"
35 ERR_SECUREONLYCHAN = 489,
39 /** Handle channel mode +z
41 class SSLMode : public ModeHandler
44 UserCertificateAPI& API;
47 SSLMode(Module* Creator, UserCertificateAPI& api)
48 : ModeHandler(Creator, "sslonly", 'z', PARAM_NONE, MODETYPE_CHANNEL)
53 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
57 if (!channel->IsModeSet(this))
63 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, "Unable to determine whether all members of the channel are connected via SSL");
64 return MODEACTION_DENY;
67 unsigned long nonssl = 0;
68 const Channel::MemberMap& userlist = channel->GetUsers();
69 for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
71 ssl_cert* cert = API->GetCertificate(i->first);
72 if (!cert && !i->first->server->IsULine())
78 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, InspIRCd::Format("All members of the channel must be connected via SSL (%lu/%lu are non-SSL)",
79 nonssl, static_cast<unsigned long>(userlist.size())));
80 return MODEACTION_DENY;
83 channel->SetMode(this, true);
84 return MODEACTION_ALLOW;
88 return MODEACTION_DENY;
93 if (channel->IsModeSet(this))
95 channel->SetMode(this, false);
96 return MODEACTION_ALLOW;
99 return MODEACTION_DENY;
104 /** Handle user mode +z
106 class SSLModeUser : public ModeHandler
109 UserCertificateAPI& API;
112 SSLModeUser(Module* Creator, UserCertificateAPI& api)
113 : ModeHandler(Creator, "sslqueries", 'z', PARAM_NONE, MODETYPE_USER)
116 if (!ServerInstance->Config->ConfValue("sslmodes")->getBool("enableumode"))
117 DisableAutoRegister();
120 ModeAction OnModeChange(User* user, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
124 if (!dest->IsModeSet(this))
126 if (!API || !API->GetCertificate(user))
127 return MODEACTION_DENY;
129 dest->SetMode(this, true);
130 return MODEACTION_ALLOW;
135 if (dest->IsModeSet(this))
137 dest->SetMode(this, false);
138 return MODEACTION_ALLOW;
142 return MODEACTION_DENY;
148 , public CTCTags::EventListener
151 UserCertificateAPI api;
153 SSLModeUser sslquery;
157 : CTCTags::EventListener(this)
160 , sslquery(this, api)
164 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
166 if(chan && chan->IsModeSet(sslm))
170 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; unable to determine if you are an SSL user (+z is set)");
174 if (!api->GetCertificate(user))
176 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; SSL users only (+z is set)");
181 return MOD_RES_PASSTHRU;
184 ModResult HandleMessage(User* user, const MessageTarget& msgtarget)
186 if (msgtarget.type != MessageTarget::TYPE_USER)
187 return MOD_RES_PASSTHRU;
189 User* target = msgtarget.Get<User>();
191 /* If one or more of the parties involved is a ulined service, we wont stop it. */
192 if (user->server->IsULine() || target->server->IsULine())
193 return MOD_RES_PASSTHRU;
195 /* If the target is +z */
196 if (target->IsModeSet(sslquery))
198 if (!api || !api->GetCertificate(user))
200 /* The sending user is not on an SSL connection */
201 user->WriteNumeric(Numerics::CannotSendTo(target, "messages", &sslquery));
205 /* If the user is +z */
206 else if (user->IsModeSet(sslquery))
208 if (!api || !api->GetCertificate(target))
210 user->WriteNumeric(Numerics::CannotSendTo(target, "messages", &sslquery, true));
215 return MOD_RES_PASSTHRU;
218 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
220 return HandleMessage(user, target);
223 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
225 return HandleMessage(user, target);
228 ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
230 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
232 const std::string fp = api ? api->GetFingerprint(user) : "";
233 if (!fp.empty() && InspIRCd::Match(fp, mask.substr(2)))
236 return MOD_RES_PASSTHRU;
239 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
241 tokens["EXTBAN"].push_back('z');
244 Version GetVersion() CXX11_OVERRIDE
246 return Version("Provides user and channel mode +z to allow for SSL-only channels, queries and notices", VF_VENDOR);
250 MODULE_INIT(ModuleSSLModes)