]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslinfo.cpp
Use FindNickOnly in a few commands to prevent enumerating users via UID walking
[user/henk/code/inspircd.git] / src / modules / m_sslinfo.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "ssl.h"
16
17 /* $ModDesc: Provides SSL metadata, including /WHOIS information and /SSLINFO command */
18
19 class SSLCertExt : public ExtensionItem {
20  public:
21         SSLCertExt(Module* parent) : ExtensionItem("ssl_cert", parent) {}
22         ssl_cert* get(const Extensible* item) const
23         {
24                 return static_cast<ssl_cert*>(get_raw(item));
25         }
26         void set(Extensible* item, ssl_cert* value)
27         {
28                 value->refcount_inc();
29                 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
30                 if (old && old->refcount_dec())
31                         delete old;
32         }
33
34         std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
35         {
36                 return static_cast<ssl_cert*>(item)->GetMetaLine();
37         }
38
39         void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
40         {
41                 ssl_cert* cert = new ssl_cert;
42                 set(container, cert);
43
44                 std::stringstream s(value);
45                 std::string v;
46                 getline(s,v,' ');
47
48                 cert->invalid = (v.find('v') != std::string::npos);
49                 cert->trusted = (v.find('T') != std::string::npos);
50                 cert->revoked = (v.find('R') != std::string::npos);
51                 cert->unknownsigner = (v.find('s') != std::string::npos);
52                 if (v.find('E') != std::string::npos)
53                 {
54                         getline(s,cert->error,'\n');
55                 }
56                 else
57                 {
58                         getline(s,cert->fingerprint,' ');
59                         getline(s,cert->dn,' ');
60                         getline(s,cert->issuer,'\n');
61                 }
62         }
63
64         void free(void* item)
65         {
66                 ssl_cert* old = static_cast<ssl_cert*>(item);
67                 if (old && old->refcount_dec())
68                         delete old;
69         }
70 };
71
72 /** Handle /SSLINFO
73  */
74 class CommandSSLInfo : public Command
75 {
76  public:
77         SSLCertExt CertExt;
78
79         CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
80         {
81                 this->syntax = "<nick>";
82         }
83
84         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
85         {
86                 User* target = ServerInstance->FindNickOnly(parameters[0]);
87
88                 if (!target)
89                 {
90                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
91                         return CMD_FAILURE;
92                 }
93                 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
94                 if (operonlyfp && !IS_OPER(user) && target != user)
95                 {
96                         user->WriteServ("NOTICE %s :*** You cannot view SSL certificate information for other users", user->nick.c_str());
97                         return CMD_FAILURE;
98                 }
99                 ssl_cert* cert = CertExt.get(target);
100                 if (!cert)
101                 {
102                         user->WriteServ("NOTICE %s :*** No SSL certificate for this user", user->nick.c_str());
103                 }
104                 else if (cert->GetError().length())
105                 {
106                         user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
107                 }
108                 else
109                 {
110                         user->WriteServ("NOTICE %s :*** Distinguished Name: %s", user->nick.c_str(), cert->GetDN().c_str());
111                         user->WriteServ("NOTICE %s :*** Issuer:             %s", user->nick.c_str(), cert->GetIssuer().c_str());
112                         user->WriteServ("NOTICE %s :*** Key Fingerprint:    %s", user->nick.c_str(), cert->GetFingerprint().c_str());
113                 }
114                 return CMD_SUCCESS;
115         }
116 };
117
118 class ModuleSSLInfo : public Module
119 {
120         CommandSSLInfo cmd;
121
122  public:
123         ModuleSSLInfo() : cmd(this)
124         {
125         }
126
127         void init()
128         {
129                 ServerInstance->AddCommand(&cmd);
130
131                 ServerInstance->Extensions.Register(&cmd.CertExt);
132
133                 Implementation eventlist[] = { I_OnWhois, I_OnPreCommand, I_OnSetConnectClass, I_OnUserConnect };
134                 ServerInstance->Modules->Attach(eventlist, this, 4);
135         }
136
137         Version GetVersion()
138         {
139                 return Version("SSL Certificate Utilities", VF_VENDOR);
140         }
141
142         void OnWhois(User* source, User* dest)
143         {
144                 ssl_cert* cert = cmd.CertExt.get(dest);
145                 if (cert)
146                 {
147                         ServerInstance->SendWhoisLine(source, dest, 671, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
148                         bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
149                         if ((!operonlyfp || source == dest || IS_OPER(source)) && !cert->fingerprint.empty())
150                                 ServerInstance->SendWhoisLine(source, dest, 276, "%s %s :has client certificate fingerprint %s",
151                                         source->nick.c_str(), dest->nick.c_str(), cert->fingerprint.c_str());
152                 }
153         }
154
155         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
156         {
157                 std::stringstream hl(hostlist);
158                 std::string xhost;
159                 while (hl >> xhost)
160                 {
161                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
162                         {
163                                 return true;
164                         }
165                 }
166                 return false;
167         }
168
169         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line)
170         {
171                 irc::string pcmd = command.c_str();
172
173                 if ((pcmd == "OPER") && (validated))
174                 {
175                         OperIndex::iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
176                         if (i != ServerInstance->Config->oper_blocks.end())
177                         {
178                                 OperInfo* ifo = i->second;
179                                 ssl_cert* cert = cmd.CertExt.get(user);
180
181                                 if (ifo->oper_block->getBool("sslonly") && !cert)
182                                 {
183                                         user->WriteNumeric(491, "%s :This oper login requires an SSL connection.", user->nick.c_str());
184                                         user->CommandFloodPenalty += 10000;
185                                         return MOD_RES_DENY;
186                                 }
187
188                                 std::string fingerprint;
189                                 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || cert->GetFingerprint() != fingerprint))
190                                 {
191                                         user->WriteNumeric(491, "%s :This oper login requires a matching SSL fingerprint.",user->nick.c_str());
192                                         user->CommandFloodPenalty += 10000;
193                                         return MOD_RES_DENY;
194                                 }
195                         }
196                 }
197
198                 // Let core handle it for extra stuff
199                 return MOD_RES_PASSTHRU;
200         }
201
202         void OnUserConnect(LocalUser* user)
203         {
204                 SocketCertificateRequest req(&user->eh, this);
205                 if (!req.cert)
206                         return;
207                 cmd.CertExt.set(user, req.cert);
208                 if (req.cert->fingerprint.empty())
209                         return;
210                 // find an auto-oper block for this user
211                 for(OperIndex::iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); i++)
212                 {
213                         OperInfo* ifo = i->second;
214                         std::string fp = ifo->oper_block->getString("fingerprint");
215                         if (fp == req.cert->fingerprint && ifo->oper_block->getBool("autologin"))
216                                 user->Oper(ifo);
217                 }
218         }
219
220         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
221         {
222                 SocketCertificateRequest req(&user->eh, this);
223                 bool ok = true;
224                 if (myclass->config->getString("requiressl") == "trusted")
225                 {
226                         ok = (req.cert && req.cert->IsCAVerified());
227                 }
228                 else if (myclass->config->getBool("requiressl"))
229                 {
230                         ok = (req.cert != NULL);
231                 }
232
233                 if (!ok)
234                         return MOD_RES_DENY;
235                 return MOD_RES_PASSTHRU;
236         }
237
238         void OnRequest(Request& request)
239         {
240                 if (strcmp("GET_USER_CERT", request.id) == 0)
241                 {
242                         UserCertificateRequest& req = static_cast<UserCertificateRequest&>(request);
243                         req.cert = cmd.CertExt.get(req.user);
244                 }
245         }
246 };
247
248 MODULE_INIT(ModuleSSLInfo)
249