]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslmodes.cpp
Show how many users are non-SSL in ERR_ALLMUSTSSL.
[user/henk/code/inspircd.git] / src / modules / m_sslmodes.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
10  *
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.
14  *
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
18  * details.
19  *
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/>.
22  */
23
24
25 #include "inspircd.h"
26 #include "modules/ctctags.h"
27 #include "modules/ssl.h"
28
29 enum
30 {
31         // From UnrealIRCd.
32         ERR_SECUREONLYCHAN = 489,
33         ERR_ALLMUSTSSL = 490
34 };
35
36 /** Handle channel mode +z
37  */
38 class SSLMode : public ModeHandler
39 {
40  private:
41         UserCertificateAPI& API;
42
43  public:
44         SSLMode(Module* Creator, UserCertificateAPI& api)
45                 : ModeHandler(Creator, "sslonly", 'z', PARAM_NONE, MODETYPE_CHANNEL)
46                 , API(api)
47         {
48         }
49
50         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
51         {
52                 if (adding)
53                 {
54                         if (!channel->IsModeSet(this))
55                         {
56                                 if (IS_LOCAL(source))
57                                 {
58                                         if (!API)
59                                         {
60                                                 source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, "Unable to determine whether all members of the channel are connected via SSL");
61                                                 return MODEACTION_DENY;
62                                         }
63
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)
67                                         {
68                                                 ssl_cert* cert = API->GetCertificate(i->first);
69                                                 if (!cert && !i->first->server->IsULine())
70                                                         nonssl++;
71                                         }
72
73                                         if (nonssl)
74                                         {
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;
78                                         }
79                                 }
80                                 channel->SetMode(this, true);
81                                 return MODEACTION_ALLOW;
82                         }
83                         else
84                         {
85                                 return MODEACTION_DENY;
86                         }
87                 }
88                 else
89                 {
90                         if (channel->IsModeSet(this))
91                         {
92                                 channel->SetMode(this, false);
93                                 return MODEACTION_ALLOW;
94                         }
95
96                         return MODEACTION_DENY;
97                 }
98         }
99 };
100
101 /** Handle user mode +z
102 */
103 class SSLModeUser : public ModeHandler
104 {
105  private:
106         UserCertificateAPI& API;
107
108  public:
109         SSLModeUser(Module* Creator, UserCertificateAPI& api)
110                 : ModeHandler(Creator, "sslqueries", 'z', PARAM_NONE, MODETYPE_USER)
111                 , API(api)
112         {
113                 if (!ServerInstance->Config->ConfValue("sslmodes")->getBool("enableumode"))
114                         DisableAutoRegister();
115         }
116
117         ModeAction OnModeChange(User* user, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
118         {
119                 if (adding)
120                 {
121                         if (!dest->IsModeSet(this))
122                         {
123                                 if (!API || !API->GetCertificate(user))
124                                         return MODEACTION_DENY;
125
126                                 dest->SetMode(this, true);
127                                 return MODEACTION_ALLOW;
128                         }
129                 }
130                 else
131                 {
132                         if (dest->IsModeSet(this))
133                         {
134                                 dest->SetMode(this, false);
135                                 return MODEACTION_ALLOW;
136                         }
137                 }
138
139                 return MODEACTION_DENY;
140         }
141 };
142
143 class ModuleSSLModes
144         : public Module
145         , public CTCTags::EventListener
146 {
147  private:
148         UserCertificateAPI api;
149         SSLMode sslm;
150         SSLModeUser sslquery;
151
152  public:
153         ModuleSSLModes()
154                 : CTCTags::EventListener(this)
155                 , api(this)
156                 , sslm(this, api)
157                 , sslquery(this, api)
158         {
159         }
160
161         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
162         {
163                 if(chan && chan->IsModeSet(sslm))
164                 {
165                         if (!api)
166                         {
167                                 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; unable to determine if you are an SSL user (+z is set)");
168                                 return MOD_RES_DENY;
169                         }
170
171                         if (!api->GetCertificate(user))
172                         {
173                                 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; SSL users only (+z is set)");
174                                 return MOD_RES_DENY;
175                         }
176                 }
177
178                 return MOD_RES_PASSTHRU;
179         }
180
181         ModResult HandleMessage(User* user, const MessageTarget& msgtarget)
182         {
183                 if (msgtarget.type != MessageTarget::TYPE_USER)
184                         return MOD_RES_PASSTHRU;
185
186                 User* target = msgtarget.Get<User>();
187
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;
191
192                 /* If the target is +z */
193                 if (target->IsModeSet(sslquery))
194                 {
195                         if (!api || !api->GetCertificate(user))
196                         {
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)");
199                                 return MOD_RES_DENY;
200                         }
201                 }
202                 /* If the user is +z */
203                 else if (user->IsModeSet(sslquery))
204                 {
205                         if (!api || !api->GetCertificate(target))
206                         {
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.");
208                                 return MOD_RES_DENY;
209                         }
210                 }
211
212                 return MOD_RES_PASSTHRU;
213         }
214
215         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
216         {
217                 return HandleMessage(user, target);
218         }
219
220         ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
221         {
222                 return HandleMessage(user, target);
223         }
224
225         ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
226         {
227                 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
228                 {
229                         const std::string fp = api ? api->GetFingerprint(user) : "";
230                         if (!fp.empty() && InspIRCd::Match(fp, mask.substr(2)))
231                                 return MOD_RES_DENY;
232                 }
233                 return MOD_RES_PASSTHRU;
234         }
235
236         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
237         {
238                 tokens["EXTBAN"].push_back('z');
239         }
240
241         Version GetVersion() CXX11_OVERRIDE
242         {
243                 return Version("Provides user and channel mode +z to allow for SSL-only channels, queries and notices", VF_VENDOR);
244         }
245 };
246
247 MODULE_INIT(ModuleSSLModes)