]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslinfo.cpp
523d52abbc9cf21a3d2cde83c3f13deace7cc028
[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 class SSLCertExt : public ExtensionItem {
24  public:
25         SSLCertExt(Module* parent)
26                 : ExtensionItem("ssl_cert", ExtensionItem::EXT_USER, parent)
27         {
28         }
29
30         ssl_cert* get(const Extensible* item) const
31         {
32                 return static_cast<ssl_cert*>(get_raw(item));
33         }
34         void set(Extensible* item, ssl_cert* value)
35         {
36                 value->refcount_inc();
37                 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
38                 if (old && old->refcount_dec())
39                         delete old;
40         }
41
42         std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
43         {
44                 return static_cast<ssl_cert*>(item)->GetMetaLine();
45         }
46
47         void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
48         {
49                 ssl_cert* cert = new ssl_cert;
50                 set(container, cert);
51
52                 std::stringstream s(value);
53                 std::string v;
54                 getline(s,v,' ');
55
56                 cert->invalid = (v.find('v') != std::string::npos);
57                 cert->trusted = (v.find('T') != std::string::npos);
58                 cert->revoked = (v.find('R') != std::string::npos);
59                 cert->unknownsigner = (v.find('s') != std::string::npos);
60                 if (v.find('E') != std::string::npos)
61                 {
62                         getline(s,cert->error,'\n');
63                 }
64                 else
65                 {
66                         getline(s,cert->fingerprint,' ');
67                         getline(s,cert->dn,' ');
68                         getline(s,cert->issuer,'\n');
69                 }
70         }
71
72         void free(void* item)
73         {
74                 ssl_cert* old = static_cast<ssl_cert*>(item);
75                 if (old && old->refcount_dec())
76                         delete old;
77         }
78 };
79
80 /** Handle /SSLINFO
81  */
82 class CommandSSLInfo : public Command
83 {
84  public:
85         SSLCertExt CertExt;
86
87         CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
88         {
89                 this->syntax = "<nick>";
90         }
91
92         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
93         {
94                 User* target = ServerInstance->FindNickOnly(parameters[0]);
95
96                 if ((!target) || (target->registered != REG_ALL))
97                 {
98                         user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nickname", parameters[0].c_str());
99                         return CMD_FAILURE;
100                 }
101                 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
102                 if (operonlyfp && !user->IsOper() && target != user)
103                 {
104                         user->WriteNotice("*** You cannot view SSL certificate information for other users");
105                         return CMD_FAILURE;
106                 }
107                 ssl_cert* cert = CertExt.get(target);
108                 if (!cert)
109                 {
110                         user->WriteNotice("*** No SSL certificate for this user");
111                 }
112                 else if (cert->GetError().length())
113                 {
114                         user->WriteNotice("*** No SSL certificate information for this user (" + cert->GetError() + ").");
115                 }
116                 else
117                 {
118                         user->WriteNotice("*** Distinguished Name: " + cert->GetDN());
119                         user->WriteNotice("*** Issuer:             " + cert->GetIssuer());
120                         user->WriteNotice("*** Key Fingerprint:    " + cert->GetFingerprint());
121                 }
122                 return CMD_SUCCESS;
123         }
124 };
125
126 class UserCertificateAPIImpl : public UserCertificateAPIBase
127 {
128         SSLCertExt& ext;
129
130  public:
131         UserCertificateAPIImpl(Module* mod, SSLCertExt& certext)
132                 : UserCertificateAPIBase(mod), ext(certext)
133         {
134         }
135
136         ssl_cert* GetCertificate(User* user) CXX11_OVERRIDE
137         {
138                 return ext.get(user);
139         }
140 };
141
142 class ModuleSSLInfo : public Module, public Whois::EventListener
143 {
144         CommandSSLInfo cmd;
145         UserCertificateAPIImpl APIImpl;
146
147  public:
148         ModuleSSLInfo()
149                 : Whois::EventListener(this)
150                 , cmd(this)
151                 , APIImpl(this, cmd.CertExt)
152         {
153         }
154
155         Version GetVersion() CXX11_OVERRIDE
156         {
157                 return Version("SSL Certificate Utilities", VF_VENDOR);
158         }
159
160         void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
161         {
162                 ssl_cert* cert = cmd.CertExt.get(whois.GetTarget());
163                 if (cert)
164                 {
165                         whois.SendLine(671, ":is using a secure connection");
166                         bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
167                         if ((!operonlyfp || whois.IsSelfWhois() || whois.GetSource()->IsOper()) && !cert->fingerprint.empty())
168                                 whois.SendLine(276, ":has client certificate fingerprint %s", cert->fingerprint.c_str());
169                 }
170         }
171
172         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
173         {
174                 if ((command == "OPER") && (validated))
175                 {
176                         ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
177                         if (i != ServerInstance->Config->oper_blocks.end())
178                         {
179                                 OperInfo* ifo = i->second;
180                                 ssl_cert* cert = cmd.CertExt.get(user);
181
182                                 if (ifo->oper_block->getBool("sslonly") && !cert)
183                                 {
184                                         user->WriteNumeric(491, ":This oper login requires an SSL connection.");
185                                         user->CommandFloodPenalty += 10000;
186                                         return MOD_RES_DENY;
187                                 }
188
189                                 std::string fingerprint;
190                                 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || cert->GetFingerprint() != fingerprint))
191                                 {
192                                         user->WriteNumeric(491, ":This oper login requires a matching SSL certificate fingerprint.");
193                                         user->CommandFloodPenalty += 10000;
194                                         return MOD_RES_DENY;
195                                 }
196                         }
197                 }
198
199                 // Let core handle it for extra stuff
200                 return MOD_RES_PASSTHRU;
201         }
202
203         void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
204         {
205                 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
206                 if (cert)
207                         cmd.CertExt.set(user, cert);
208         }
209
210         void OnPostConnect(User* user) CXX11_OVERRIDE
211         {
212                 ssl_cert *cert = cmd.CertExt.get(user);
213                 if (!cert || cert->fingerprint.empty())
214                         return;
215                 // find an auto-oper block for this user
216                 for (ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); ++i)
217                 {
218                         OperInfo* ifo = i->second;
219                         std::string fp = ifo->oper_block->getString("fingerprint");
220                         if (fp == cert->fingerprint && ifo->oper_block->getBool("autologin"))
221                                 user->Oper(ifo);
222                 }
223         }
224
225         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
226         {
227                 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
228                 bool ok = true;
229                 if (myclass->config->getString("requiressl") == "trusted")
230                 {
231                         ok = (cert && cert->IsCAVerified());
232                 }
233                 else if (myclass->config->getBool("requiressl"))
234                 {
235                         ok = (cert != NULL);
236                 }
237
238                 if (!ok)
239                         return MOD_RES_DENY;
240                 return MOD_RES_PASSTHRU;
241         }
242 };
243
244 MODULE_INIT(ModuleSSLInfo)