]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ssl_oper_cert.cpp
Revert automated conversion by Special, as it (unfortunately) neglects some details...
[user/henk/code/inspircd.git] / src / modules / extra / m_ssl_oper_cert.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $ModDesc: Allows for MD5 encrypted oper passwords */
15 /* $ModDep: transport.h */
16
17 #include "inspircd.h"
18 #include "inspircd_config.h"
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22 #include "transport.h"
23 #include "wildcard.h"
24
25 /** Handle /FINGERPRINT
26  */
27 class cmd_fingerprint : public Command
28 {
29  public:
30         cmd_fingerprint (InspIRCd* Instance) : Command(Instance,"FINGERPRINT", 0, 1)
31         {
32                 this->source = "m_ssl_oper_cert.so";
33                 syntax = "<nickname>";
34         }
35
36         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
37         {
38                 User* target = ServerInstance->FindNick(parameters[0]);
39                 if (target)
40                 {
41                         ssl_cert* cert;
42                         if (target->GetExt("ssl_cert",cert))
43                         {
44                                 if (cert->GetFingerprint().length())
45                                 {
46                                         user->WriteServ("NOTICE %s :Certificate fingerprint for %s is %s",user->nick.c_str(),target->nick.c_str(),cert->GetFingerprint().c_str());
47                                         return CMD_SUCCESS;
48                                 }
49                                 else
50                                 {
51                                         user->WriteServ("NOTICE %s :Certificate fingerprint for %s does not exist!", user->nick.c_str(),target->nick.c_str());
52                                         return CMD_FAILURE;
53                                 }
54                         }
55                         else
56                         {
57                                 user->WriteServ("NOTICE %s :Certificate fingerprint for %s does not exist!", user->nick.c_str(), target->nick.c_str());
58                                 return CMD_FAILURE;
59                         }
60                 }
61                 else
62                 {
63                         user->WriteNumeric(401, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
64                         return CMD_FAILURE;
65                 }
66         }
67 };
68
69
70
71 class ModuleOperSSLCert : public Module
72 {
73         ssl_cert* cert;
74         bool HasCert;
75         cmd_fingerprint* mycommand;
76         ConfigReader* cf;
77  public:
78
79         ModuleOperSSLCert(InspIRCd* Me)
80                 : Module(Me)
81         {
82                 mycommand = new cmd_fingerprint(ServerInstance);
83                 ServerInstance->AddCommand(mycommand);
84                 cf = new ConfigReader(ServerInstance);
85                 Implementation eventlist[] = { I_OnPreCommand, I_OnRehash };
86                 ServerInstance->Modules->Attach(eventlist, this, 2);
87         }
88
89         virtual ~ModuleOperSSLCert()
90         {
91                 delete cf;
92         }
93
94
95         virtual void OnRehash(User* user, const std::string &parameter)
96         {
97                 delete cf;
98                 cf = new ConfigReader(ServerInstance);
99         }
100
101         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
102         {
103                 std::stringstream hl(hostlist);
104                 std::string xhost;
105                 while (hl >> xhost)
106                 {
107                         if (match(host, xhost) || match(ip, xhost, true))
108                         {
109                                 return true;
110                         }
111                 }
112                 return false;
113         }
114
115         virtual int OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
116         {
117                 irc::string cmd = command.c_str();
118
119                 if ((cmd == "OPER") && (validated))
120                 {
121                         char TheHost[MAXBUF];
122                         char TheIP[MAXBUF];
123                         std::string LoginName;
124                         std::string Password;
125                         std::string OperType;
126                         std::string HostName;
127                         std::string HashType;
128                         std::string FingerPrint;
129                         bool SSLOnly;
130                         char* dummy;
131
132                         snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
133                         snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
134
135                         HasCert = user->GetExt("ssl_cert",cert);
136
137                         for (int i = 0; i < cf->Enumerate("oper"); i++)
138                         {
139                                 LoginName = cf->ReadValue("oper", "name", i);
140                                 Password = cf->ReadValue("oper", "password", i);
141                                 OperType = cf->ReadValue("oper", "type", i);
142                                 HostName = cf->ReadValue("oper", "host", i);
143                                 HashType = cf->ReadValue("oper", "hash", i);
144                                 FingerPrint = cf->ReadValue("oper", "fingerprint", i);
145                                 SSLOnly = cf->ReadFlag("oper", "sslonly", i);
146
147                                 if (FingerPrint.empty() && !SSLOnly)
148                                         continue;
149
150                                 if (LoginName != parameters[0])
151                                         continue;
152
153                                 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
154                                         continue;
155
156                                 if (Password.length() && !ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
157                                         continue;
158
159                                 if (SSLOnly && !user->GetExt("ssl", dummy))
160                                 {
161                                         user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
162                                         return 1;
163                                 }
164
165                                 /*
166                                  * No cert found or the fingerprint doesn't match
167                                  */
168                                 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
169                                 {
170                                         user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
171                                         ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
172                                         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());
173                                         return 1;
174                                 }
175                         }
176                 }
177
178                 // Let core handle it for extra stuff
179                 return 0;
180         }
181
182         virtual Version GetVersion()
183         {
184                 return Version(1,2,0,0,VF_VENDOR,API_VERSION);
185         }
186 };
187
188 MODULE_INIT(ModuleOperSSLCert)
189