]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslinfo.cpp
01509653c20f41c8245f2eeea84ca5773c89c354
[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 "transport.h"
16
17 /* $ModDesc: Provides SSL metadata, including /WHOIS information and /SSLINFO command */
18
19 /** Handle /SSLINFO
20  */
21 class CommandSSLInfo : public Command
22 {
23  public:
24         CommandSSLInfo (InspIRCd* Instance) : Command(Instance,"SSLINFO", 0, 1)
25         {
26                 this->source = "m_sslinfo.so";
27                 this->syntax = "<nick>";
28         }
29
30         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
31         {
32                 User* target = ServerInstance->FindNick(parameters[0]);
33                 ssl_cert* cert;
34
35                 if (target)
36                 {
37                         if (target->GetExt("ssl_cert", cert))
38                         {
39                                 if (cert->GetError().length())
40                                 {
41                                         user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
42                                 }
43                                 else
44                                 {
45                                         user->WriteServ("NOTICE %s :*** Distinguised Name: %s", user->nick.c_str(), cert->GetDN().c_str());
46                                         user->WriteServ("NOTICE %s :*** Issuer:            %s", user->nick.c_str(), cert->GetIssuer().c_str());
47                                         user->WriteServ("NOTICE %s :*** Key Fingerprint:   %s", user->nick.c_str(), cert->GetFingerprint().c_str());
48                                 }
49                                 return CMD_LOCALONLY;
50                         }
51                         else
52                         {
53                                 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user.", user->nick.c_str());
54                                 return CMD_FAILURE;
55                         }
56                 }
57                 else
58                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
59
60                 return CMD_FAILURE;
61         }
62 };
63
64 class ModuleSSLInfo : public Module
65 {
66         CommandSSLInfo cmd;
67  public:
68         ModuleSSLInfo(InspIRCd* Me)
69                 : Module(Me), cmd(Me)
70         {
71                 ServerInstance->AddCommand(&cmd);
72
73                 Implementation eventlist[] = { I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnWhois, I_OnPreCommand };
74                 ServerInstance->Modules->Attach(eventlist, this, 4);
75         }
76
77
78         virtual ~ModuleSSLInfo()
79         {
80         }
81
82         virtual Version GetVersion()
83         {
84                 return Version("$Id$", VF_VENDOR, API_VERSION);
85         }
86
87         virtual void OnWhois(User* source, User* dest)
88         {
89                 if(dest->GetExt("ssl"))
90                 {
91                         ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
92                 }
93         }
94
95         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
96         {
97                 // check if the linking module wants to know about OUR metadata
98                 if (extname == "ssl")
99                 {
100                         // check if this user has an ssl field to send
101                         if (!user->GetExt(extname))
102                                 return;
103
104                         // call this function in the linking module, let it format the data how it
105                         // sees fit, and send it on its way. We dont need or want to know how.
106                         proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, displayable ? "Enabled" : "ON");
107                 }
108                 else if (extname == "ssl_cert")
109                 {
110                         ssl_cert* cert;
111                         if (!user->GetExt("ssl_cert", cert))
112                                 return;
113
114                         proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, cert->GetMetaLine().c_str());
115                 }
116         }
117
118         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
119         {
120                 // check if its our metadata key, and its associated with a user
121                 if ((target_type == TYPE_USER) && (extname == "ssl"))
122                 {
123                         User* dest = static_cast<User*>(target);
124                         // if they dont already have an ssl flag, accept the remote server's
125                         if (!dest->GetExt(extname))
126                         {
127                                 dest->Extend(extname);
128                         }
129                 }
130                 else if ((target_type == TYPE_USER) && (extname == "ssl_cert"))
131                 {
132                         User* dest = static_cast<User*>(target);
133                         if (dest->GetExt(extname))
134                                 return;
135
136                         ssl_cert* cert = new ssl_cert;
137                         dest->Extend(extname, cert);
138
139                         std::stringstream s(extdata);
140                         std::string v;
141                         getline(s,v,' ');
142
143                         cert->invalid = (v.find('v') != std::string::npos);
144                         cert->trusted = (v.find('T') != std::string::npos);
145                         cert->revoked = (v.find('R') != std::string::npos);
146                         cert->unknownsigner = (v.find('s') != std::string::npos);
147                         if (v.find('E') != std::string::npos)
148                         {
149                                 getline(s,cert->error,'\n');
150                         }
151                         else
152                         {
153                                 getline(s,cert->fingerprint,' ');
154                                 getline(s,cert->dn,' ');
155                                 getline(s,cert->issuer,'\n');
156                         }
157                 }
158         }
159
160         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
161         {
162                 std::stringstream hl(hostlist);
163                 std::string xhost;
164                 while (hl >> xhost)
165                 {
166                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
167                         {
168                                 return true;
169                         }
170                 }
171                 return false;
172         }
173
174         virtual int OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
175         {
176                 irc::string cmd = command.c_str();
177
178                 if ((cmd == "OPER") && (validated))
179                 {
180                         ConfigReader cf(ServerInstance);
181                         char TheHost[MAXBUF];
182                         char TheIP[MAXBUF];
183                         std::string LoginName;
184                         std::string Password;
185                         std::string OperType;
186                         std::string HostName;
187                         std::string HashType;
188                         std::string FingerPrint;
189                         bool SSLOnly;
190                         ssl_cert* cert = NULL;
191
192                         snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
193                         snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
194
195                         user->GetExt("ssl_cert",cert);
196
197                         for (int i = 0; i < cf.Enumerate("oper"); i++)
198                         {
199                                 LoginName = cf.ReadValue("oper", "name", i);
200                                 Password = cf.ReadValue("oper", "password", i);
201                                 OperType = cf.ReadValue("oper", "type", i);
202                                 HostName = cf.ReadValue("oper", "host", i);
203                                 HashType = cf.ReadValue("oper", "hash", i);
204                                 FingerPrint = cf.ReadValue("oper", "fingerprint", i);
205                                 SSLOnly = cf.ReadFlag("oper", "sslonly", i);
206
207                                 if (FingerPrint.empty() && !SSLOnly)
208                                         continue;
209
210                                 if (LoginName != parameters[0])
211                                         continue;
212
213                                 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
214                                         continue;
215
216                                 if (Password.length() && ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
217                                         continue;
218
219                                 if (SSLOnly && !user->GetExt("ssl"))
220                                 {
221                                         user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
222                                         return 1;
223                                 }
224
225                                 /*
226                                  * No cert found or the fingerprint doesn't match
227                                  */
228                                 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
229                                 {
230                                         user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
231                                         ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
232                                         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());
233                                         return 1;
234                                 }
235                         }
236                 }
237
238                 // Let core handle it for extra stuff
239                 return 0;
240         }
241
242
243 };
244
245 MODULE_INIT(ModuleSSLInfo)
246