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/ssl.h"
31 ERR_SECUREONLYCHAN = 489,
35 /** Handle channel mode +z
37 class SSLMode : public ModeHandler
40 UserCertificateAPI& API;
43 SSLMode(Module* Creator, UserCertificateAPI& api)
44 : ModeHandler(Creator, "sslonly", 'z', PARAM_NONE, MODETYPE_CHANNEL)
49 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
53 if (!channel->IsModeSet(this))
59 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, "Unable to determine whether all members of the channel are connected via SSL");
60 return MODEACTION_DENY;
63 const Channel::MemberMap& userlist = channel->GetUsers();
64 for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
66 ssl_cert* cert = API->GetCertificate(i->first);
67 if (!cert && !i->first->server->IsULine())
69 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, "all members of the channel must be connected via SSL");
70 return MODEACTION_DENY;
74 channel->SetMode(this, true);
75 return MODEACTION_ALLOW;
79 return MODEACTION_DENY;
84 if (channel->IsModeSet(this))
86 channel->SetMode(this, false);
87 return MODEACTION_ALLOW;
90 return MODEACTION_DENY;
95 /** Handle user mode +z
97 class SSLModeUser : public ModeHandler
100 UserCertificateAPI& API;
103 SSLModeUser(Module* Creator, UserCertificateAPI& api)
104 : ModeHandler(Creator, "sslqueries", 'z', PARAM_NONE, MODETYPE_USER)
107 if (!ServerInstance->Config->ConfValue("sslmodes")->getBool("enableumode"))
108 DisableAutoRegister();
111 ModeAction OnModeChange(User* user, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
115 if (!dest->IsModeSet(this))
117 if (!API || !API->GetCertificate(user))
118 return MODEACTION_DENY;
120 dest->SetMode(this, true);
121 return MODEACTION_ALLOW;
126 if (dest->IsModeSet(this))
128 dest->SetMode(this, false);
129 return MODEACTION_ALLOW;
133 return MODEACTION_DENY;
137 class ModuleSSLModes : public Module
140 UserCertificateAPI api;
142 SSLModeUser sslquery;
148 , sslquery(this, api)
152 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
154 if(chan && chan->IsModeSet(sslm))
158 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; unable to determine if you are a SSL user (+z)");
162 if (!api->GetCertificate(user))
164 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; SSL users only (+z)");
169 return MOD_RES_PASSTHRU;
172 ModResult OnUserPreMessage(User* user, const MessageTarget& msgtarget, MessageDetails& details) CXX11_OVERRIDE
174 if (msgtarget.type != MessageTarget::TYPE_USER)
175 return MOD_RES_PASSTHRU;
177 User* target = msgtarget.Get<User>();
179 /* If one or more of the parties involved is a ulined service, we wont stop it. */
180 if (user->server->IsULine() || target->server->IsULine())
181 return MOD_RES_PASSTHRU;
183 /* If the target is +z */
184 if (target->IsModeSet(sslquery))
186 if (!api || !api->GetCertificate(user))
188 /* The sending user is not on an SSL connection */
189 user->WriteNumeric(ERR_CANTSENDTOUSER, target->nick, "You are not permitted to send private messages to this user (+z set)");
193 /* If the user is +z */
194 else if (user->IsModeSet(sslquery))
196 if (!api || !api->GetCertificate(target))
198 user->WriteNumeric(ERR_CANTSENDTOUSER, target->nick, "You must remove usermode 'z' before you are able to send private messages to a non-ssl user.");
203 return MOD_RES_PASSTHRU;
206 ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
208 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
210 const std::string fp = api ? api->GetFingerprint(user) : "";
211 if (!fp.empty() && InspIRCd::Match(fp, mask.substr(2)))
214 return MOD_RES_PASSTHRU;
217 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
219 tokens["EXTBAN"].push_back('z');
222 Version GetVersion() CXX11_OVERRIDE
224 return Version("Provides user and channel mode +z to allow for SSL-only channels, queries and notices.", VF_VENDOR);
228 MODULE_INIT(ModuleSSLModes)