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