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,
37 bool IsSSLUser(UserCertificateAPI& api, User* user)
42 ssl_cert* cert = api->GetCertificate(user);
43 return (cert != NULL);
47 /** Handle channel mode +z
49 class SSLMode : public ModeHandler
52 UserCertificateAPI& API;
55 SSLMode(Module* Creator, UserCertificateAPI& api)
56 : ModeHandler(Creator, "sslonly", 'z', PARAM_NONE, MODETYPE_CHANNEL)
61 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
65 if (!channel->IsModeSet(this))
70 return MODEACTION_DENY;
72 const Channel::MemberMap& userlist = channel->GetUsers();
73 for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
75 ssl_cert* cert = API->GetCertificate(i->first);
76 if (!cert && !i->first->server->IsULine())
78 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, "all members of the channel must be connected via SSL");
79 return MODEACTION_DENY;
83 channel->SetMode(this, true);
84 return MODEACTION_ALLOW;
88 return MODEACTION_DENY;
93 if (channel->IsModeSet(this))
95 channel->SetMode(this, false);
96 return MODEACTION_ALLOW;
99 return MODEACTION_DENY;
104 /** Handle user mode +z
106 class SSLModeUser : public ModeHandler
109 UserCertificateAPI& API;
112 SSLModeUser(Module* Creator, UserCertificateAPI& api)
113 : ModeHandler(Creator, "sslqueries", 'z', PARAM_NONE, MODETYPE_USER)
116 if (!ServerInstance->Config->ConfValue("sslmodes")->getBool("enableumode"))
117 DisableAutoRegister();
120 ModeAction OnModeChange(User* user, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
124 if (!dest->IsModeSet(this))
126 if (!IsSSLUser(API, user))
127 return MODEACTION_DENY;
129 dest->SetMode(this, true);
130 return MODEACTION_ALLOW;
135 if (dest->IsModeSet(this))
137 dest->SetMode(this, false);
138 return MODEACTION_ALLOW;
142 return MODEACTION_DENY;
146 class ModuleSSLModes : public Module
149 UserCertificateAPI api;
151 SSLModeUser sslquery;
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))
168 ssl_cert* cert = api->GetCertificate(user);
172 return MOD_RES_PASSTHRU;
177 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; SSL users only (+z)");
182 return MOD_RES_PASSTHRU;
185 ModResult OnUserPreMessage(User* user, const MessageTarget& msgtarget, MessageDetails& details) CXX11_OVERRIDE
187 if (msgtarget.type != MessageTarget::TYPE_USER)
188 return MOD_RES_PASSTHRU;
190 User* target = msgtarget.Get<User>();
192 /* If one or more of the parties involved is a ulined service, we wont stop it. */
193 if (user->server->IsULine() || target->server->IsULine())
194 return MOD_RES_PASSTHRU;
196 /* If the target is +z */
197 if (target->IsModeSet(sslquery))
199 if (!IsSSLUser(api, user))
201 /* The sending user is not on an SSL connection */
202 user->WriteNumeric(ERR_CANTSENDTOUSER, target->nick, "You are not permitted to send private messages to this user (+z set)");
206 /* If the user is +z */
207 else if (user->IsModeSet(sslquery))
209 if (!IsSSLUser(api, target))
211 user->WriteNumeric(ERR_CANTSENDTOUSER, target->nick, "You must remove usermode 'z' before you are able to send private messages to a non-ssl user.");
216 return MOD_RES_PASSTHRU;
219 ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
221 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
226 ssl_cert* cert = api->GetCertificate(user);
227 if (cert && InspIRCd::Match(cert->GetFingerprint(), mask.substr(2)))
230 return MOD_RES_PASSTHRU;
233 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
235 tokens["EXTBAN"].push_back('z');
238 Version GetVersion() CXX11_OVERRIDE
240 return Version("Provides user and channel mode +z to allow for SSL-only channels, queries and notices.", VF_VENDOR);
244 MODULE_INIT(ModuleSSLModes)