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 const Channel::MemberMap& userlist = channel->GetUsers();
65 for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
67 ssl_cert* cert = API->GetCertificate(i->first);
68 if (!cert && !i->first->server->IsULine())
70 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, "all members of the channel must be connected via SSL");
71 return MODEACTION_DENY;
75 channel->SetMode(this, true);
76 return MODEACTION_ALLOW;
80 return MODEACTION_DENY;
85 if (channel->IsModeSet(this))
87 channel->SetMode(this, false);
88 return MODEACTION_ALLOW;
91 return MODEACTION_DENY;
96 /** Handle user mode +z
98 class SSLModeUser : public ModeHandler
101 UserCertificateAPI& API;
104 SSLModeUser(Module* Creator, UserCertificateAPI& api)
105 : ModeHandler(Creator, "sslqueries", 'z', PARAM_NONE, MODETYPE_USER)
108 if (!ServerInstance->Config->ConfValue("sslmodes")->getBool("enableumode"))
109 DisableAutoRegister();
112 ModeAction OnModeChange(User* user, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
116 if (!dest->IsModeSet(this))
118 if (!API || !API->GetCertificate(user))
119 return MODEACTION_DENY;
121 dest->SetMode(this, true);
122 return MODEACTION_ALLOW;
127 if (dest->IsModeSet(this))
129 dest->SetMode(this, false);
130 return MODEACTION_ALLOW;
134 return MODEACTION_DENY;
140 , public CTCTags::EventListener
143 UserCertificateAPI api;
145 SSLModeUser sslquery;
149 : CTCTags::EventListener(this)
152 , sslquery(this, api)
156 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
158 if(chan && chan->IsModeSet(sslm))
162 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; unable to determine if you are a SSL user (+z)");
166 if (!api->GetCertificate(user))
168 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; SSL users only (+z)");
173 return MOD_RES_PASSTHRU;
176 ModResult HandleMessage(User* user, const MessageTarget& msgtarget)
178 if (msgtarget.type != MessageTarget::TYPE_USER)
179 return MOD_RES_PASSTHRU;
181 User* target = msgtarget.Get<User>();
183 /* If one or more of the parties involved is a ulined service, we wont stop it. */
184 if (user->server->IsULine() || target->server->IsULine())
185 return MOD_RES_PASSTHRU;
187 /* If the target is +z */
188 if (target->IsModeSet(sslquery))
190 if (!api || !api->GetCertificate(user))
192 /* The sending user is not on an SSL connection */
193 user->WriteNumeric(ERR_CANTSENDTOUSER, target->nick, "You are not permitted to send private messages to this user (+z set)");
197 /* If the user is +z */
198 else if (user->IsModeSet(sslquery))
200 if (!api || !api->GetCertificate(target))
202 user->WriteNumeric(ERR_CANTSENDTOUSER, target->nick, "You must remove usermode 'z' before you are able to send private messages to a non-ssl user.");
207 return MOD_RES_PASSTHRU;
210 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
212 return HandleMessage(user, target);
215 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
217 return HandleMessage(user, target);
220 ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
222 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
224 const std::string fp = api ? api->GetFingerprint(user) : "";
225 if (!fp.empty() && InspIRCd::Match(fp, mask.substr(2)))
228 return MOD_RES_PASSTHRU;
231 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
233 tokens["EXTBAN"].push_back('z');
236 Version GetVersion() CXX11_OVERRIDE
238 return Version("Provides user and channel mode +z to allow for SSL-only channels, queries and notices.", VF_VENDOR);
242 MODULE_INIT(ModuleSSLModes)