]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslmodes.cpp
5c436b116e7d695af1812c3f4d90dd772a2b703d
[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                                         const Channel::MemberMap& userlist = channel->GetUsers();
65                                         for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
66                                         {
67                                                 ssl_cert* cert = API->GetCertificate(i->first);
68                                                 if (!cert && !i->first->server->IsULine())
69                                                 {
70                                                         source->WriteNumeric(ERR_ALLMUSTSSL, channel->name, "all members of the channel must be connected via SSL");
71                                                         return MODEACTION_DENY;
72                                                 }
73                                         }
74                                 }
75                                 channel->SetMode(this, true);
76                                 return MODEACTION_ALLOW;
77                         }
78                         else
79                         {
80                                 return MODEACTION_DENY;
81                         }
82                 }
83                 else
84                 {
85                         if (channel->IsModeSet(this))
86                         {
87                                 channel->SetMode(this, false);
88                                 return MODEACTION_ALLOW;
89                         }
90
91                         return MODEACTION_DENY;
92                 }
93         }
94 };
95
96 /** Handle user mode +z
97 */
98 class SSLModeUser : public ModeHandler
99 {
100  private:
101         UserCertificateAPI& API;
102
103  public:
104         SSLModeUser(Module* Creator, UserCertificateAPI& api)
105                 : ModeHandler(Creator, "sslqueries", 'z', PARAM_NONE, MODETYPE_USER)
106                 , API(api)
107         {
108                 if (!ServerInstance->Config->ConfValue("sslmodes")->getBool("enableumode"))
109                         DisableAutoRegister();
110         }
111
112         ModeAction OnModeChange(User* user, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
113         {
114                 if (adding)
115                 {
116                         if (!dest->IsModeSet(this))
117                         {
118                                 if (!API || !API->GetCertificate(user))
119                                         return MODEACTION_DENY;
120
121                                 dest->SetMode(this, true);
122                                 return MODEACTION_ALLOW;
123                         }
124                 }
125                 else
126                 {
127                         if (dest->IsModeSet(this))
128                         {
129                                 dest->SetMode(this, false);
130                                 return MODEACTION_ALLOW;
131                         }
132                 }
133
134                 return MODEACTION_DENY;
135         }
136 };
137
138 class ModuleSSLModes
139         : public Module
140         , public CTCTags::EventListener
141 {
142  private:
143         UserCertificateAPI api;
144         SSLMode sslm;
145         SSLModeUser sslquery;
146
147  public:
148         ModuleSSLModes()
149                 : CTCTags::EventListener(this)
150                 , api(this)
151                 , sslm(this, api)
152                 , sslquery(this, api)
153         {
154         }
155
156         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
157         {
158                 if(chan && chan->IsModeSet(sslm))
159                 {
160                         if (!api)
161                         {
162                                 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; unable to determine if you are a SSL user (+z)");
163                                 return MOD_RES_DENY;
164                         }
165
166                         if (!api->GetCertificate(user))
167                         {
168                                 user->WriteNumeric(ERR_SECUREONLYCHAN, cname, "Cannot join channel; SSL users only (+z)");
169                                 return MOD_RES_DENY;
170                         }
171                 }
172
173                 return MOD_RES_PASSTHRU;
174         }
175
176         ModResult HandleMessage(User* user, const MessageTarget& msgtarget)
177         {
178                 if (msgtarget.type != MessageTarget::TYPE_USER)
179                         return MOD_RES_PASSTHRU;
180
181                 User* target = msgtarget.Get<User>();
182
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;
186
187                 /* If the target is +z */
188                 if (target->IsModeSet(sslquery))
189                 {
190                         if (!api || !api->GetCertificate(user))
191                         {
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)");
194                                 return MOD_RES_DENY;
195                         }
196                 }
197                 /* If the user is +z */
198                 else if (user->IsModeSet(sslquery))
199                 {
200                         if (!api || !api->GetCertificate(target))
201                         {
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.");
203                                 return MOD_RES_DENY;
204                         }
205                 }
206
207                 return MOD_RES_PASSTHRU;
208         }
209
210         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
211         {
212                 return HandleMessage(user, target);
213         }
214
215         ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
216         {
217                 return HandleMessage(user, target);
218         }
219
220         ModResult OnCheckBan(User *user, Channel *c, const std::string& mask) CXX11_OVERRIDE
221         {
222                 if ((mask.length() > 2) && (mask[0] == 'z') && (mask[1] == ':'))
223                 {
224                         const std::string fp = api ? api->GetFingerprint(user) : "";
225                         if (!fp.empty() && InspIRCd::Match(fp, mask.substr(2)))
226                                 return MOD_RES_DENY;
227                 }
228                 return MOD_RES_PASSTHRU;
229         }
230
231         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
232         {
233                 tokens["EXTBAN"].push_back('z');
234         }
235
236         Version GetVersion() CXX11_OVERRIDE
237         {
238                 return Version("Provides user and channel mode +z to allow for SSL-only channels, queries and notices.", VF_VENDOR);
239         }
240 };
241
242 MODULE_INIT(ModuleSSLModes)