]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslinfo.cpp
MetaData rework
[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_OnSyncUser, 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 OnSyncUser(User* user, Module* proto, void* opaque)
96         {
97                 if (user->GetExt("ssl"))
98                         proto->ProtoSendMetaData(opaque, user, "ssl", "ON");
99                 
100                 ssl_cert* cert;
101                 if (user->GetExt("ssl_cert", cert))
102                         proto->ProtoSendMetaData(opaque, user, "ssl_cert", cert->GetMetaLine().c_str());
103         }
104
105         virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
106         {
107                 User* dest = dynamic_cast<User*>(target);
108                 // check if its our metadata key, and its associated with a user
109                 if (dest && (extname == "ssl"))
110                 {
111                         // if they dont already have an ssl flag, accept the remote server's
112                         if (!dest->GetExt(extname))
113                         {
114                                 dest->Extend(extname);
115                         }
116                 }
117                 else if (dest && (extname == "ssl_cert"))
118                 {
119                         if (dest->GetExt(extname))
120                                 return;
121
122                         ssl_cert* cert = new ssl_cert;
123                         dest->Extend(extname, cert);
124
125                         std::stringstream s(extdata);
126                         std::string v;
127                         getline(s,v,' ');
128
129                         cert->invalid = (v.find('v') != std::string::npos);
130                         cert->trusted = (v.find('T') != std::string::npos);
131                         cert->revoked = (v.find('R') != std::string::npos);
132                         cert->unknownsigner = (v.find('s') != std::string::npos);
133                         if (v.find('E') != std::string::npos)
134                         {
135                                 getline(s,cert->error,'\n');
136                         }
137                         else
138                         {
139                                 getline(s,cert->fingerprint,' ');
140                                 getline(s,cert->dn,' ');
141                                 getline(s,cert->issuer,'\n');
142                         }
143                 }
144         }
145
146         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
147         {
148                 std::stringstream hl(hostlist);
149                 std::string xhost;
150                 while (hl >> xhost)
151                 {
152                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
153                         {
154                                 return true;
155                         }
156                 }
157                 return false;
158         }
159
160         virtual int OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
161         {
162                 irc::string pcmd = command.c_str();
163
164                 if ((pcmd == "OPER") && (validated))
165                 {
166                         ConfigReader cf(ServerInstance);
167                         char TheHost[MAXBUF];
168                         char TheIP[MAXBUF];
169                         std::string LoginName;
170                         std::string Password;
171                         std::string OperType;
172                         std::string HostName;
173                         std::string HashType;
174                         std::string FingerPrint;
175                         bool SSLOnly;
176                         ssl_cert* cert = NULL;
177
178                         snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
179                         snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
180
181                         user->GetExt("ssl_cert",cert);
182
183                         for (int i = 0; i < cf.Enumerate("oper"); i++)
184                         {
185                                 LoginName = cf.ReadValue("oper", "name", i);
186                                 Password = cf.ReadValue("oper", "password", i);
187                                 OperType = cf.ReadValue("oper", "type", i);
188                                 HostName = cf.ReadValue("oper", "host", i);
189                                 HashType = cf.ReadValue("oper", "hash", i);
190                                 FingerPrint = cf.ReadValue("oper", "fingerprint", i);
191                                 SSLOnly = cf.ReadFlag("oper", "sslonly", i);
192
193                                 if (FingerPrint.empty() && !SSLOnly)
194                                         continue;
195
196                                 if (LoginName != parameters[0])
197                                         continue;
198
199                                 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
200                                         continue;
201
202                                 if (Password.length() && ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
203                                         continue;
204
205                                 if (SSLOnly && !user->GetExt("ssl"))
206                                 {
207                                         user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
208                                         return 1;
209                                 }
210
211                                 /*
212                                  * No cert found or the fingerprint doesn't match
213                                  */
214                                 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
215                                 {
216                                         user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
217                                         ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
218                                         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());
219                                         return 1;
220                                 }
221                         }
222                 }
223
224                 // Let core handle it for extra stuff
225                 return 0;
226         }
227
228
229 };
230
231 MODULE_INIT(ModuleSSLInfo)
232