]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslinfo.cpp
19b6160cc8b20cd903979a8b38e15ea197e18535
[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->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, I_OnSetConnectClass };
131                 ServerInstance->Modules->Attach(eventlist, this, 3);
132         }
133
134         Version GetVersion()
135         {
136                 return Version("SSL Certificate Utilities", VF_VENDOR);
137         }
138
139         void OnWhois(User* source, User* dest)
140         {
141                 if (cmd.CertExt.get(dest))
142                 {
143                         ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
144                 }
145         }
146
147         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
148         {
149                 std::stringstream hl(hostlist);
150                 std::string xhost;
151                 while (hl >> xhost)
152                 {
153                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
154                         {
155                                 return true;
156                         }
157                 }
158                 return false;
159         }
160
161         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line)
162         {
163                 irc::string pcmd = command.c_str();
164
165                 if ((pcmd == "OPER") && (validated))
166                 {
167                         OperIndex::iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
168                         if (i != ServerInstance->Config->oper_blocks.end())
169                         {
170                                 OperInfo* ifo = i->second;
171                                 ssl_cert* cert = cmd.CertExt.get(user);
172
173                                 if (ifo->oper_block->getBool("sslonly") && !cert)
174                                 {
175                                         user->WriteNumeric(491, "%s :This oper login requires an SSL connection.", user->nick.c_str());
176                                         user->CommandFloodPenalty += 10000;
177                                         return MOD_RES_DENY;
178                                 }
179
180                                 std::string fingerprint;
181                                 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || cert->GetFingerprint() != fingerprint))
182                                 {
183                                         user->WriteNumeric(491, "%s :This oper login requires a matching SSL fingerprint.",user->nick.c_str());
184                                         user->CommandFloodPenalty += 10000;
185                                         return MOD_RES_DENY;
186                                 }
187                         }
188                 }
189
190                 // Let core handle it for extra stuff
191                 return MOD_RES_PASSTHRU;
192         }
193
194         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
195         {
196                 SocketCertificateRequest req(&user->eh, this);
197                 req.Send();
198                 bool ok = true;
199                 if (myclass->config->getBool("requiressl"))
200                 {
201                         ok = (req.cert != NULL);
202                 }
203                 else if (myclass->config->getString("requiressl") == "trusted")
204                 {
205                         ok = (req.cert && req.cert->IsCAVerified());
206                 }
207
208                 if (!ok)
209                         return MOD_RES_DENY;
210                 return MOD_RES_PASSTHRU;
211         }
212
213         void OnRequest(Request& request)
214         {
215                 if (strcmp("GET_USER_CERT", request.id) == 0)
216                 {
217                         UserCertificateRequest& req = static_cast<UserCertificateRequest&>(request);
218                         req.cert = cmd.CertExt.get(req.user);
219                 }
220                 else if (strcmp("SET_CERT", request.id) == 0)
221                 {
222                         SSLCertSubmission& req = static_cast<SSLCertSubmission&>(request);
223                         cmd.CertExt.set(req.item, req.cert);
224                 }
225         }
226 };
227
228 MODULE_INIT(ModuleSSLInfo)
229