]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslinfo.cpp
Add names for a bunch more numerics.
[user/henk/code/inspircd.git] / src / modules / m_sslinfo.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "modules/ssl.h"
22
23 enum
24 {
25         // From oftc-hybrid.
26         RPL_WHOISCERTFP = 276,
27
28         // From UnrealIRCd.
29         RPL_WHOISSECURE = 671
30 };
31
32 class SSLCertExt : public ExtensionItem {
33  public:
34         SSLCertExt(Module* parent)
35                 : ExtensionItem("ssl_cert", ExtensionItem::EXT_USER, parent)
36         {
37         }
38
39         ssl_cert* get(const Extensible* item) const
40         {
41                 return static_cast<ssl_cert*>(get_raw(item));
42         }
43         void set(Extensible* item, ssl_cert* value)
44         {
45                 value->refcount_inc();
46                 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
47                 if (old && old->refcount_dec())
48                         delete old;
49         }
50
51         std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE
52         {
53                 return static_cast<ssl_cert*>(item)->GetMetaLine();
54         }
55
56         void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
57         {
58                 ssl_cert* cert = new ssl_cert;
59                 set(container, cert);
60
61                 std::stringstream s(value);
62                 std::string v;
63                 getline(s,v,' ');
64
65                 cert->invalid = (v.find('v') != std::string::npos);
66                 cert->trusted = (v.find('T') != std::string::npos);
67                 cert->revoked = (v.find('R') != std::string::npos);
68                 cert->unknownsigner = (v.find('s') != std::string::npos);
69                 if (v.find('E') != std::string::npos)
70                 {
71                         getline(s,cert->error,'\n');
72                 }
73                 else
74                 {
75                         getline(s,cert->fingerprint,' ');
76                         getline(s,cert->dn,' ');
77                         getline(s,cert->issuer,'\n');
78                 }
79         }
80
81         void free(void* item) CXX11_OVERRIDE
82         {
83                 ssl_cert* old = static_cast<ssl_cert*>(item);
84                 if (old && old->refcount_dec())
85                         delete old;
86         }
87 };
88
89 /** Handle /SSLINFO
90  */
91 class CommandSSLInfo : public Command
92 {
93  public:
94         SSLCertExt CertExt;
95
96         CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
97         {
98                 this->syntax = "<nick>";
99         }
100
101         CmdResult Handle(const std::vector<std::string>& parameters, User* user) CXX11_OVERRIDE
102         {
103                 User* target = ServerInstance->FindNickOnly(parameters[0]);
104
105                 if ((!target) || (target->registered != REG_ALL))
106                 {
107                         user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
108                         return CMD_FAILURE;
109                 }
110                 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
111                 if (operonlyfp && !user->IsOper() && target != user)
112                 {
113                         user->WriteNotice("*** You cannot view SSL certificate information for other users");
114                         return CMD_FAILURE;
115                 }
116                 ssl_cert* cert = CertExt.get(target);
117                 if (!cert)
118                 {
119                         user->WriteNotice("*** No SSL certificate for this user");
120                 }
121                 else if (cert->GetError().length())
122                 {
123                         user->WriteNotice("*** No SSL certificate information for this user (" + cert->GetError() + ").");
124                 }
125                 else
126                 {
127                         user->WriteNotice("*** Distinguished Name: " + cert->GetDN());
128                         user->WriteNotice("*** Issuer:             " + cert->GetIssuer());
129                         user->WriteNotice("*** Key Fingerprint:    " + cert->GetFingerprint());
130                 }
131                 return CMD_SUCCESS;
132         }
133 };
134
135 class UserCertificateAPIImpl : public UserCertificateAPIBase
136 {
137         SSLCertExt& ext;
138
139  public:
140         UserCertificateAPIImpl(Module* mod, SSLCertExt& certext)
141                 : UserCertificateAPIBase(mod), ext(certext)
142         {
143         }
144
145         ssl_cert* GetCertificate(User* user) CXX11_OVERRIDE
146         {
147                 return ext.get(user);
148         }
149 };
150
151 class ModuleSSLInfo : public Module, public Whois::EventListener
152 {
153         CommandSSLInfo cmd;
154         UserCertificateAPIImpl APIImpl;
155
156  public:
157         ModuleSSLInfo()
158                 : Whois::EventListener(this)
159                 , cmd(this)
160                 , APIImpl(this, cmd.CertExt)
161         {
162         }
163
164         Version GetVersion() CXX11_OVERRIDE
165         {
166                 return Version("SSL Certificate Utilities", VF_VENDOR);
167         }
168
169         void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
170         {
171                 ssl_cert* cert = cmd.CertExt.get(whois.GetTarget());
172                 if (cert)
173                 {
174                         whois.SendLine(RPL_WHOISSECURE, "is using a secure connection");
175                         bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
176                         if ((!operonlyfp || whois.IsSelfWhois() || whois.GetSource()->IsOper()) && !cert->fingerprint.empty())
177                                 whois.SendLine(RPL_WHOISCERTFP, InspIRCd::Format("has client certificate fingerprint %s", cert->fingerprint.c_str()));
178                 }
179         }
180
181         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
182         {
183                 if ((command == "OPER") && (validated))
184                 {
185                         ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
186                         if (i != ServerInstance->Config->oper_blocks.end())
187                         {
188                                 OperInfo* ifo = i->second;
189                                 ssl_cert* cert = cmd.CertExt.get(user);
190
191                                 if (ifo->oper_block->getBool("sslonly") && !cert)
192                                 {
193                                         user->WriteNumeric(491, "This oper login requires an SSL connection.");
194                                         user->CommandFloodPenalty += 10000;
195                                         return MOD_RES_DENY;
196                                 }
197
198                                 std::string fingerprint;
199                                 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || cert->GetFingerprint() != fingerprint))
200                                 {
201                                         user->WriteNumeric(491, "This oper login requires a matching SSL certificate fingerprint.");
202                                         user->CommandFloodPenalty += 10000;
203                                         return MOD_RES_DENY;
204                                 }
205                         }
206                 }
207
208                 // Let core handle it for extra stuff
209                 return MOD_RES_PASSTHRU;
210         }
211
212         void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
213         {
214                 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
215                 if (cert)
216                         cmd.CertExt.set(user, cert);
217         }
218
219         void OnPostConnect(User* user) CXX11_OVERRIDE
220         {
221                 LocalUser* const localuser = IS_LOCAL(user);
222                 if (!localuser)
223                         return;
224
225                 const SSLIOHook* const ssliohook = SSLIOHook::IsSSL(&localuser->eh);
226                 if (!ssliohook)
227                         return;
228
229                 ssl_cert* const cert = ssliohook->GetCertificate();
230
231                 {
232                         std::string text = "*** You are connected to ";
233                         if (!ssliohook->GetServerName(text))
234                                 text.append(ServerInstance->Config->ServerName);
235                         text.append(" using SSL cipher '");
236                         ssliohook->GetCiphersuite(text);
237                         text.push_back('\'');
238                         if ((cert) && (!cert->GetFingerprint().empty()))
239                                 text.append(" and your SSL certificate fingerprint is ").append(cert->GetFingerprint());
240                         user->WriteNotice(text);
241                 }
242
243                 if (!cert)
244                         return;
245                 // find an auto-oper block for this user
246                 for (ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); ++i)
247                 {
248                         OperInfo* ifo = i->second;
249                         std::string fp = ifo->oper_block->getString("fingerprint");
250                         if (fp == cert->fingerprint && ifo->oper_block->getBool("autologin"))
251                                 user->Oper(ifo);
252                 }
253         }
254
255         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
256         {
257                 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
258                 bool ok = true;
259                 if (myclass->config->getString("requiressl") == "trusted")
260                 {
261                         ok = (cert && cert->IsCAVerified());
262                         ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires a trusted SSL cert. Client %s one.", (ok ? "has" : "does not have"));
263                 }
264                 else if (myclass->config->getBool("requiressl"))
265                 {
266                         ok = (cert != NULL);
267                         ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires any SSL cert. Client %s one.", (ok ? "has" : "does not have"));
268                 }
269
270                 if (!ok)
271                         return MOD_RES_DENY;
272                 return MOD_RES_PASSTHRU;
273         }
274 };
275
276 MODULE_INIT(ModuleSSLInfo)