]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslinfo.cpp
7457ce29652f7481e241eb49885c597f8a902791
[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) 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->FindNick(parameters[0]);
87
88                 if (target)
89                 {
90                         ssl_cert* cert = CertExt.get(target);
91                         if (cert)
92                         {
93                                 if (cert->GetError().length())
94                                 {
95                                         user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
96                                 }
97                                 else
98                                 {
99                                         user->WriteServ("NOTICE %s :*** Distinguised Name: %s", user->nick.c_str(), cert->GetDN().c_str());
100                                         user->WriteServ("NOTICE %s :*** Issuer:            %s", user->nick.c_str(), cert->GetIssuer().c_str());
101                                         user->WriteServ("NOTICE %s :*** Key Fingerprint:   %s", user->nick.c_str(), cert->GetFingerprint().c_str());
102                                 }
103                                 return CMD_SUCCESS;
104                         }
105                         else
106                         {
107                                 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user.", user->nick.c_str());
108                                 return CMD_FAILURE;
109                         }
110                 }
111                 else
112                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
113
114                 return CMD_FAILURE;
115         }
116 };
117
118 class ModuleSSLInfo : public Module
119 {
120         CommandSSLInfo cmd;
121
122  public:
123         ModuleSSLInfo()
124                 : cmd(this)
125         {
126                 ServerInstance->AddCommand(&cmd);
127
128                 ServerInstance->Extensions.Register(&cmd.CertExt);
129
130                 Implementation eventlist[] = { I_OnWhois, I_OnPreCommand };
131                 ServerInstance->Modules->Attach(eventlist, this, 2);
132                 ServerInstance->Modules->PublishInterface("SSLCertInfo", this);
133         }
134
135         ~ModuleSSLInfo()
136         {
137                 ServerInstance->Modules->UnpublishInterface("SSLCertInfo", this);
138         }
139
140         Version GetVersion()
141         {
142                 return Version("SSL Certificate Utilities", VF_VENDOR);
143         }
144
145         void OnWhois(User* source, User* dest)
146         {
147                 if (cmd.CertExt.get(dest))
148                 {
149                         ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
150                 }
151         }
152
153         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
154         {
155                 std::stringstream hl(hostlist);
156                 std::string xhost;
157                 while (hl >> xhost)
158                 {
159                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
160                         {
161                                 return true;
162                         }
163                 }
164                 return false;
165         }
166
167         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
168         {
169                 irc::string pcmd = command.c_str();
170
171                 if ((pcmd == "OPER") && (validated))
172                 {
173                         ConfigReader cf;
174                         char TheHost[MAXBUF];
175                         char TheIP[MAXBUF];
176                         std::string LoginName;
177                         std::string Password;
178                         std::string OperType;
179                         std::string HostName;
180                         std::string HashType;
181                         std::string FingerPrint;
182                         bool SSLOnly;
183                         ssl_cert* cert = cmd.CertExt.get(user);
184
185                         snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
186                         snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
187
188                         for (int i = 0; i < cf.Enumerate("oper"); i++)
189                         {
190                                 LoginName = cf.ReadValue("oper", "name", i);
191                                 Password = cf.ReadValue("oper", "password", i);
192                                 OperType = cf.ReadValue("oper", "type", i);
193                                 HostName = cf.ReadValue("oper", "host", i);
194                                 HashType = cf.ReadValue("oper", "hash", i);
195                                 FingerPrint = cf.ReadValue("oper", "fingerprint", i);
196                                 SSLOnly = cf.ReadFlag("oper", "sslonly", i);
197
198                                 if (FingerPrint.empty() && !SSLOnly)
199                                         continue;
200
201                                 if (LoginName != parameters[0])
202                                         continue;
203
204                                 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
205                                         continue;
206
207                                 if (Password.length() && ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
208                                         continue;
209
210                                 if (SSLOnly && !cert)
211                                 {
212                                         user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
213                                         return MOD_RES_DENY;
214                                 }
215
216                                 /*
217                                  * No cert found or the fingerprint doesn't match
218                                  */
219                                 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
220                                 {
221                                         user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
222                                         ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
223                                         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());
224                                         return MOD_RES_DENY;
225                                 }
226                         }
227                 }
228
229                 // Let core handle it for extra stuff
230                 return MOD_RES_PASSTHRU;
231         }
232
233         void OnRequest(Request& request)
234         {
235                 if (strcmp("GET_USER_CERT", request.id) == 0)
236                 {
237                         UserCertificateRequest& req = static_cast<UserCertificateRequest&>(request);
238                         req.cert = cmd.CertExt.get(req.user);
239                 }
240                 else if (strcmp("SET_CERT", request.id) == 0)
241                 {
242                         SSLCertSubmission& req = static_cast<SSLCertSubmission&>(request);
243                         cmd.CertExt.set(req.item, req.cert);
244                 }
245         }
246 };
247
248 MODULE_INIT(ModuleSSLInfo)
249