2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2020 Matt Schatz <genius3000@g3k.solutions>
5 * Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2013 Shawn Smith <ShawnSmith0828@gmail.com>
7 * Copyright (C) 2012-2014 Attila Molnar <attilamolnar@hush.com>
8 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
9 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
12 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
13 * Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
15 * This file is part of InspIRCd. InspIRCd is free software: you can
16 * redistribute it and/or modify it under the terms of the GNU General Public
17 * License as published by the Free Software Foundation, version 2.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include "modules/ctctags.h"
31 #include "modules/ssl.h"
36 ERR_SECUREONLYCHAN = 489,
42 /** Handle channel mode +z
44 class SSLMode : public ModeHandler
47 UserCertificateAPI& API;
50 SSLMode(Module* Creator, UserCertificateAPI& api)
51 : ModeHandler(Creator, "sslonly", 'z', PARAM_NONE, MODETYPE_CHANNEL)
56 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
60 if (!channel->IsModeSet(this))
66 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, "Unable to determine whether all members of the channel are connected via TLS (SSL)");
67 return MODEACTION_DENY;
70 unsigned long nonssl = 0;
71 const Channel::MemberMap& userlist = channel->GetUsers();
72 for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
74 ssl_cert* cert = API->GetCertificate(i->first);
75 if (!cert && !i->first->server->IsULine())
81 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, InspIRCd::Format("All members of the channel must be connected via TLS (SSL) (%lu/%lu are non-TLS (SSL))",
82 nonssl, static_cast<unsigned long>(userlist.size())));
83 return MODEACTION_DENY;
86 channel->SetMode(this, true);
87 return MODEACTION_ALLOW;
91 return MODEACTION_DENY;
96 if (channel->IsModeSet(this))
98 channel->SetMode(this, false);
99 return MODEACTION_ALLOW;
102 return MODEACTION_DENY;
107 /** Handle user mode +z
109 class SSLModeUser : public ModeHandler
112 UserCertificateAPI& API;
115 SSLModeUser(Module* Creator, UserCertificateAPI& api)
116 : ModeHandler(Creator, "sslqueries", 'z', PARAM_NONE, MODETYPE_USER)
119 if (!ServerInstance->Config->ConfValue("sslmodes")->getBool("enableumode"))
120 DisableAutoRegister();
123 ModeAction OnModeChange(User* user, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
125 if (adding == dest->IsModeSet(this))
126 return MODEACTION_DENY;
128 if (adding && IS_LOCAL(user) && (!API || !API->GetCertificate(user)))
129 return MODEACTION_DENY;
131 dest->SetMode(this, adding);
132 return MODEACTION_ALLOW;
138 , public CTCTags::EventListener
141 UserCertificateAPI api;
143 SSLModeUser sslquery;
147 : CTCTags::EventListener(this)
150 , sslquery(this, api)
154 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
156 if(chan && chan->IsModeSet(sslm))
160 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; unable to determine if you are a TLS (SSL) user (+z is set)");
164 if (!api->GetCertificate(user))
166 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; TLS (SSL) users only (+z is set)");
171 return MOD_RES_PASSTHRU;
174 ModResult HandleMessage(User* user, const MessageTarget& msgtarget)
176 if (msgtarget.type != MessageTarget::TYPE_USER)
177 return MOD_RES_PASSTHRU;
179 User* target = msgtarget.Get<User>();
181 /* If one or more of the parties involved is a ulined service, we won't stop it. */
182 if (user->server->IsULine() || target->server->IsULine())
183 return MOD_RES_PASSTHRU;
185 /* If the target is +z */
186 if (target->IsModeSet(sslquery))
188 if (!api || !api->GetCertificate(user))
190 /* The sending user is not on an SSL connection */
191 user->WriteNumeric(Numerics::CannotSendTo(target, "messages", &sslquery));
195 /* If the user is +z */
196 else if (user->IsModeSet(sslquery))
198 if (!api || !api->GetCertificate(target))
200 user->WriteNumeric(Numerics::CannotSendTo(target, "messages", &sslquery, true));
205 return MOD_RES_PASSTHRU;
208 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
210 return HandleMessage(user, target);
213 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
215 return HandleMessage(user, target);
218 ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
220 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
222 const std::string fp = api ? api->GetFingerprint(user) : "";
223 if (!fp.empty() && InspIRCd::Match(fp, mask.substr(2)))
226 return MOD_RES_PASSTHRU;
229 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
231 tokens["EXTBAN"].push_back('z');
234 Version GetVersion() CXX11_OVERRIDE
236 return Version("Adds channel mode z (sslonly) which prevents users who are not connecting using TLS (SSL) from joining the channel and user mode z (sslqueries) to prevent messages from non-TLS (SSL) users.", VF_VENDOR);
240 MODULE_INIT(ModuleSSLModes)