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