2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013 Shawn Smith <shawn@inspircd.org>
5 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8 * Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
9 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "modules/ctctags.h"
27 #include "modules/ssl.h"
32 ERR_SECUREONLYCHAN = 489,
36 /** Handle channel mode +z
38 class SSLMode : public ModeHandler
41 UserCertificateAPI& API;
44 SSLMode(Module* Creator, UserCertificateAPI& api)
45 : ModeHandler(Creator, "sslonly", 'z', PARAM_NONE, MODETYPE_CHANNEL)
50 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
54 if (!channel->IsModeSet(this))
60 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, "Unable to determine whether all members of the channel are connected via SSL");
61 return MODEACTION_DENY;
64 unsigned long nonssl = 0;
65 const Channel::MemberMap& userlist = channel->GetUsers();
66 for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
68 ssl_cert* cert = API->GetCertificate(i->first);
69 if (!cert && !i->first->server->IsULine())
75 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, InspIRCd::Format("All members of the channel must be connected via SSL (%lu/%lu are non-SSL)",
76 nonssl, static_cast<unsigned long>(userlist.size())));
77 return MODEACTION_DENY;
80 channel->SetMode(this, true);
81 return MODEACTION_ALLOW;
85 return MODEACTION_DENY;
90 if (channel->IsModeSet(this))
92 channel->SetMode(this, false);
93 return MODEACTION_ALLOW;
96 return MODEACTION_DENY;
101 /** Handle user mode +z
103 class SSLModeUser : public ModeHandler
106 UserCertificateAPI& API;
109 SSLModeUser(Module* Creator, UserCertificateAPI& api)
110 : ModeHandler(Creator, "sslqueries", 'z', PARAM_NONE, MODETYPE_USER)
113 if (!ServerInstance->Config->ConfValue("sslmodes")->getBool("enableumode"))
114 DisableAutoRegister();
117 ModeAction OnModeChange(User* user, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
121 if (!dest->IsModeSet(this))
123 if (!API || !API->GetCertificate(user))
124 return MODEACTION_DENY;
126 dest->SetMode(this, true);
127 return MODEACTION_ALLOW;
132 if (dest->IsModeSet(this))
134 dest->SetMode(this, false);
135 return MODEACTION_ALLOW;
139 return MODEACTION_DENY;
145 , public CTCTags::EventListener
148 UserCertificateAPI api;
150 SSLModeUser sslquery;
154 : CTCTags::EventListener(this)
157 , sslquery(this, api)
161 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
163 if(chan && chan->IsModeSet(sslm))
167 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; unable to determine if you are an SSL user (+z is set)");
171 if (!api->GetCertificate(user))
173 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; SSL users only (+z is set)");
178 return MOD_RES_PASSTHRU;
181 ModResult HandleMessage(User* user, const MessageTarget& msgtarget)
183 if (msgtarget.type != MessageTarget::TYPE_USER)
184 return MOD_RES_PASSTHRU;
186 User* target = msgtarget.Get<User>();
188 /* If one or more of the parties involved is a ulined service, we wont stop it. */
189 if (user->server->IsULine() || target->server->IsULine())
190 return MOD_RES_PASSTHRU;
192 /* If the target is +z */
193 if (target->IsModeSet(sslquery))
195 if (!api || !api->GetCertificate(user))
197 /* The sending user is not on an SSL connection */
198 user->WriteNumeric(ERR_CANTSENDTOUSER, target->nick, "You are not permitted to send private messages to this user (+z is set)");
202 /* If the user is +z */
203 else if (user->IsModeSet(sslquery))
205 if (!api || !api->GetCertificate(target))
207 user->WriteNumeric(ERR_CANTSENDTOUSER, target->nick, "You must remove user mode 'z' before you are able to send private messages to a non-SSL user.");
212 return MOD_RES_PASSTHRU;
215 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
217 return HandleMessage(user, target);
220 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
222 return HandleMessage(user, target);
225 ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
227 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
229 const std::string fp = api ? api->GetFingerprint(user) : "";
230 if (!fp.empty() && InspIRCd::Match(fp, mask.substr(2)))
233 return MOD_RES_PASSTHRU;
236 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
238 tokens["EXTBAN"].push_back('z');
241 Version GetVersion() CXX11_OVERRIDE
243 return Version("Provides user and channel mode +z to allow for SSL-only channels, queries and notices", VF_VENDOR);
247 MODULE_INIT(ModuleSSLModes)