]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslinfo.cpp
a1178f9176c0c7b74680bb233be79ba395b26724
[user/henk/code/inspircd.git] / src / modules / m_sslinfo.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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)
23         {
24                 return static_cast<ssl_cert*>(get_raw(item));
25         }
26         void set(Extensible* item, ssl_cert* value)
27         {
28                 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
29                 delete old;
30         }
31
32         std::string serialize(SerializeFormat format, const Extensible* container, void* item)
33         {
34                 return static_cast<ssl_cert*>(item)->GetMetaLine();
35         }
36
37         void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
38         {
39                 ssl_cert* cert = new ssl_cert;
40                 set(container, cert);
41
42                 std::stringstream s(value);
43                 std::string v;
44                 getline(s,v,' ');
45
46                 cert->invalid = (v.find('v') != std::string::npos);
47                 cert->trusted = (v.find('T') != std::string::npos);
48                 cert->revoked = (v.find('R') != std::string::npos);
49                 cert->unknownsigner = (v.find('s') != std::string::npos);
50                 if (v.find('E') != std::string::npos)
51                 {
52                         getline(s,cert->error,'\n');
53                 }
54                 else
55                 {
56                         getline(s,cert->fingerprint,' ');
57                         getline(s,cert->dn,' ');
58                         getline(s,cert->issuer,'\n');
59                 }
60         }
61
62         void free(void* item)
63         {
64                 delete static_cast<ssl_cert*>(item);
65         }
66 };
67
68 /** Handle /SSLINFO
69  */
70 class CommandSSLInfo : public Command
71 {
72  public:
73         SSLCertExt CertExt;
74
75         CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
76         {
77                 this->syntax = "<nick>";
78         }
79
80         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
81         {
82                 User* target = ServerInstance->FindNick(parameters[0]);
83
84                 if (target)
85                 {
86                         ssl_cert* cert = CertExt.get(target);
87                         if (cert)
88                         {
89                                 if (cert->GetError().length())
90                                 {
91                                         user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
92                                 }
93                                 else
94                                 {
95                                         user->WriteServ("NOTICE %s :*** Distinguised Name: %s", user->nick.c_str(), cert->GetDN().c_str());
96                                         user->WriteServ("NOTICE %s :*** Issuer:            %s", user->nick.c_str(), cert->GetIssuer().c_str());
97                                         user->WriteServ("NOTICE %s :*** Key Fingerprint:   %s", user->nick.c_str(), cert->GetFingerprint().c_str());
98                                 }
99                                 return CMD_SUCCESS;
100                         }
101                         else
102                         {
103                                 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user.", user->nick.c_str());
104                                 return CMD_FAILURE;
105                         }
106                 }
107                 else
108                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
109
110                 return CMD_FAILURE;
111         }
112 };
113
114 class ModuleSSLInfo : public Module
115 {
116         CommandSSLInfo cmd;
117
118  public:
119         ModuleSSLInfo()
120                 : cmd(this)
121         {
122                 ServerInstance->AddCommand(&cmd);
123
124                 Extensible::Register(&cmd.CertExt);
125
126                 Implementation eventlist[] = { I_OnWhois, I_OnPreCommand };
127                 ServerInstance->Modules->Attach(eventlist, this, 2);
128                 ServerInstance->Modules->PublishInterface("SSLCertInfo", this);
129         }
130
131         ~ModuleSSLInfo()
132         {
133                 ServerInstance->Modules->UnpublishInterface("SSLCertInfo", this);
134         }
135
136         Version GetVersion()
137         {
138                 return Version("SSL Certificate Utilities", VF_VENDOR | VF_SERVICEPROVIDER);
139         }
140
141         void OnWhois(User* source, User* dest)
142         {
143                 if (cmd.CertExt.get(dest))
144                 {
145                         ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
146                 }
147         }
148
149         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
150         {
151                 std::stringstream hl(hostlist);
152                 std::string xhost;
153                 while (hl >> xhost)
154                 {
155                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
156                         {
157                                 return true;
158                         }
159                 }
160                 return false;
161         }
162
163         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
164         {
165                 irc::string pcmd = command.c_str();
166
167                 if ((pcmd == "OPER") && (validated))
168                 {
169                         ConfigReader cf;
170                         char TheHost[MAXBUF];
171                         char TheIP[MAXBUF];
172                         std::string LoginName;
173                         std::string Password;
174                         std::string OperType;
175                         std::string HostName;
176                         std::string HashType;
177                         std::string FingerPrint;
178                         bool SSLOnly;
179                         ssl_cert* cert = cmd.CertExt.get(user);
180
181                         snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
182                         snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
183
184                         for (int i = 0; i < cf.Enumerate("oper"); i++)
185                         {
186                                 LoginName = cf.ReadValue("oper", "name", i);
187                                 Password = cf.ReadValue("oper", "password", i);
188                                 OperType = cf.ReadValue("oper", "type", i);
189                                 HostName = cf.ReadValue("oper", "host", i);
190                                 HashType = cf.ReadValue("oper", "hash", i);
191                                 FingerPrint = cf.ReadValue("oper", "fingerprint", i);
192                                 SSLOnly = cf.ReadFlag("oper", "sslonly", i);
193
194                                 if (FingerPrint.empty() && !SSLOnly)
195                                         continue;
196
197                                 if (LoginName != parameters[0])
198                                         continue;
199
200                                 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
201                                         continue;
202
203                                 if (Password.length() && ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
204                                         continue;
205
206                                 if (SSLOnly && !cert)
207                                 {
208                                         user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
209                                         return MOD_RES_DENY;
210                                 }
211
212                                 /*
213                                  * No cert found or the fingerprint doesn't match
214                                  */
215                                 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
216                                 {
217                                         user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
218                                         ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
219                                         ServerInstance->Logs->Log("m_ssl_oper_cert",DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but wrong fingerprint.", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
220                                         return MOD_RES_DENY;
221                                 }
222                         }
223                 }
224
225                 // Let core handle it for extra stuff
226                 return MOD_RES_PASSTHRU;
227         }
228
229         void OnRequest(Request& request)
230         {
231                 if (strcmp("GET_CERT", request.id) == 0)
232                 {
233                         SSLCertificateRequest& req = static_cast<SSLCertificateRequest&>(request);
234                         req.cert = cmd.CertExt.get(req.item);
235                 }
236                 else if (strcmp("SET_CERT", request.id) == 0)
237                 {
238                         SSLCertSubmission& req = static_cast<SSLCertSubmission&>(request);
239                         cmd.CertExt.set(req.item, req.cert);
240                 }
241         }
242 };
243
244 MODULE_INIT(ModuleSSLInfo)
245