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
36 bool IsSSLUser(UserCertificateAPI& api, User* user)
41 ssl_cert* cert = api->GetCertificate(user);
42 return (cert != NULL);
46 /** Handle channel mode +z
48 class SSLMode : public ModeHandler
51 UserCertificateAPI& API;
54 SSLMode(Module* Creator, UserCertificateAPI& api)
55 : ModeHandler(Creator, "sslonly", 'z', PARAM_NONE, MODETYPE_CHANNEL)
60 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
64 if (!channel->IsModeSet(this))
69 return MODEACTION_DENY;
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())
77 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, "all members of the channel must be connected via SSL");
78 return MODEACTION_DENY;
82 channel->SetMode(this, true);
83 return MODEACTION_ALLOW;
87 return MODEACTION_DENY;
92 if (channel->IsModeSet(this))
94 channel->SetMode(this, false);
95 return MODEACTION_ALLOW;
98 return MODEACTION_DENY;
103 /** Handle user mode +z
105 class SSLModeUser : public ModeHandler
108 UserCertificateAPI& API;
111 SSLModeUser(Module* Creator, UserCertificateAPI& api)
112 : ModeHandler(Creator, "sslqueries", 'z', PARAM_NONE, MODETYPE_USER)
115 if (!ServerInstance->Config->ConfValue("sslmodes")->getBool("enableumode"))
116 DisableAutoRegister();
119 ModeAction OnModeChange(User* user, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
123 if (!dest->IsModeSet(this))
125 if (!IsSSLUser(API, user))
126 return MODEACTION_DENY;
128 dest->SetMode(this, true);
129 return MODEACTION_ALLOW;
134 if (dest->IsModeSet(this))
136 dest->SetMode(this, false);
137 return MODEACTION_ALLOW;
141 return MODEACTION_DENY;
145 class ModuleSSLModes : public Module
148 UserCertificateAPI api;
150 SSLModeUser sslquery;
156 , sslquery(this, api)
160 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
162 if(chan && chan->IsModeSet(sslm))
167 ssl_cert* cert = api->GetCertificate(user);
171 return MOD_RES_PASSTHRU;
176 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; SSL users only (+z)");
181 return MOD_RES_PASSTHRU;
184 ModResult OnUserPreMessage(User* user, const MessageTarget& msgtarget, MessageDetails& details) CXX11_OVERRIDE
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 (!IsSSLUser(api, user))
200 /* The sending user is not on an SSL connection */
201 user->WriteNumeric(ERR_CANTSENDTOUSER, target->nick, "You are not permitted to send private messages to this user (+z set)");
205 /* If the user is +z */
206 else if (user->IsModeSet(sslquery))
208 if (!IsSSLUser(api, target))
210 user->WriteNumeric(ERR_CANTSENDTOUSER, target->nick, "You must remove usermode 'z' before you are able to send private messages to a non-ssl user.");
215 return MOD_RES_PASSTHRU;
218 ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
220 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
225 ssl_cert* cert = api->GetCertificate(user);
226 if (cert && InspIRCd::Match(cert->GetFingerprint(), mask.substr(2)))
229 return MOD_RES_PASSTHRU;
232 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
234 tokens["EXTBAN"].push_back('z');
237 Version GetVersion() CXX11_OVERRIDE
239 return Version("Provides user and channel mode +z to allow for SSL-only channels, queries and notices.", VF_VENDOR);
243 MODULE_INIT(ModuleSSLModes)