]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslinfo.cpp
Remove CMD_LOCALONLY, enforce use of GetRouting for routed commands
[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, Module* Creator) : Command(Instance, Creator, "SSLINFO", 0, 1)
25         {
26                 this->syntax = "<nick>";
27         }
28
29         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
30         {
31                 User* target = ServerInstance->FindNick(parameters[0]);
32                 ssl_cert* cert;
33
34                 if (target)
35                 {
36                         if (target->GetExt("ssl_cert", cert))
37                         {
38                                 if (cert->GetError().length())
39                                 {
40                                         user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
41                                 }
42                                 else
43                                 {
44                                         user->WriteServ("NOTICE %s :*** Distinguised Name: %s", user->nick.c_str(), cert->GetDN().c_str());
45                                         user->WriteServ("NOTICE %s :*** Issuer:            %s", user->nick.c_str(), cert->GetIssuer().c_str());
46                                         user->WriteServ("NOTICE %s :*** Key Fingerprint:   %s", user->nick.c_str(), cert->GetFingerprint().c_str());
47                                 }
48                                 return CMD_SUCCESS;
49                         }
50                         else
51                         {
52                                 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user.", user->nick.c_str());
53                                 return CMD_FAILURE;
54                         }
55                 }
56                 else
57                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
58
59                 return CMD_FAILURE;
60         }
61 };
62
63 class ModuleSSLInfo : public Module
64 {
65         CommandSSLInfo cmd;
66  public:
67         ModuleSSLInfo(InspIRCd* Me)
68                 : Module(Me), cmd(Me, this)
69         {
70                 ServerInstance->AddCommand(&cmd);
71
72                 Implementation eventlist[] = { I_OnSyncUser, I_OnDecodeMetaData, I_OnWhois, I_OnPreCommand };
73                 ServerInstance->Modules->Attach(eventlist, this, 4);
74         }
75
76
77         virtual ~ModuleSSLInfo()
78         {
79         }
80
81         virtual Version GetVersion()
82         {
83                 return Version("$Id$", VF_VENDOR, API_VERSION);
84         }
85
86         virtual void OnCleanup(int target_type, void* item)
87         {
88                 if (target_type != TYPE_USER)
89                         return;
90                 User* user = static_cast<User*>(item);
91                 user->Shrink("ssl_cert");
92         }
93
94         virtual void OnWhois(User* source, User* dest)
95         {
96                 if(dest->GetExt("ssl"))
97                 {
98                         ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
99                 }
100         }
101
102         virtual void OnSyncUser(User* user, Module* proto, void* opaque)
103         {
104                 if (user->GetExt("ssl"))
105                         proto->ProtoSendMetaData(opaque, user, "ssl", "ON");
106                 
107                 ssl_cert* cert;
108                 if (user->GetExt("ssl_cert", cert))
109                         proto->ProtoSendMetaData(opaque, user, "ssl_cert", cert->GetMetaLine().c_str());
110         }
111
112         virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
113         {
114                 User* dest = dynamic_cast<User*>(target);
115                 // check if its our metadata key, and its associated with a user
116                 if (dest && (extname == "ssl"))
117                 {
118                         // if they dont already have an ssl flag, accept the remote server's
119                         if (!dest->GetExt(extname))
120                         {
121                                 dest->Extend(extname);
122                         }
123                 }
124                 else if (dest && (extname == "ssl_cert"))
125                 {
126                         if (dest->GetExt(extname))
127                                 return;
128
129                         ssl_cert* cert = new ssl_cert;
130                         dest->Extend(extname, cert);
131
132                         std::stringstream s(extdata);
133                         std::string v;
134                         getline(s,v,' ');
135
136                         cert->invalid = (v.find('v') != std::string::npos);
137                         cert->trusted = (v.find('T') != std::string::npos);
138                         cert->revoked = (v.find('R') != std::string::npos);
139                         cert->unknownsigner = (v.find('s') != std::string::npos);
140                         if (v.find('E') != std::string::npos)
141                         {
142                                 getline(s,cert->error,'\n');
143                         }
144                         else
145                         {
146                                 getline(s,cert->fingerprint,' ');
147                                 getline(s,cert->dn,' ');
148                                 getline(s,cert->issuer,'\n');
149                         }
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         virtual 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(ServerInstance);
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 = NULL;
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                         user->GetExt("ssl_cert",cert);
189
190                         for (int i = 0; i < cf.Enumerate("oper"); i++)
191                         {
192                                 LoginName = cf.ReadValue("oper", "name", i);
193                                 Password = cf.ReadValue("oper", "password", i);
194                                 OperType = cf.ReadValue("oper", "type", i);
195                                 HostName = cf.ReadValue("oper", "host", i);
196                                 HashType = cf.ReadValue("oper", "hash", i);
197                                 FingerPrint = cf.ReadValue("oper", "fingerprint", i);
198                                 SSLOnly = cf.ReadFlag("oper", "sslonly", i);
199
200                                 if (FingerPrint.empty() && !SSLOnly)
201                                         continue;
202
203                                 if (LoginName != parameters[0])
204                                         continue;
205
206                                 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
207                                         continue;
208
209                                 if (Password.length() && ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
210                                         continue;
211
212                                 if (SSLOnly && !user->GetExt("ssl"))
213                                 {
214                                         user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
215                                         return MOD_RES_DENY;
216                                 }
217
218                                 /*
219                                  * No cert found or the fingerprint doesn't match
220                                  */
221                                 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
222                                 {
223                                         user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
224                                         ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
225                                         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());
226                                         return MOD_RES_DENY;
227                                 }
228                         }
229                 }
230
231                 // Let core handle it for extra stuff
232                 return MOD_RES_PASSTHRU;
233         }
234
235
236 };
237
238 MODULE_INIT(ModuleSSLInfo)
239