]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ircv3_sts.cpp
Add the m_ircv3_sts module which implements the IRCv3 STS spec.
[user/henk/code/inspircd.git] / src / modules / m_ircv3_sts.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2017 Peter Powell <petpow@saberuk.com>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "modules/cap.h"
23 #include "modules/ssl.h"
24
25 class STSCap : public Cap::Capability
26 {
27  private:
28         std::string host;
29         std::string plaintextpolicy;
30         std::string securepolicy;
31
32         bool OnList(LocalUser* user) CXX11_OVERRIDE
33         {
34                 // Don't send the cap to clients that only support cap-3.1.
35                 if (GetProtocol(user) == Cap::CAP_LEGACY)
36                         return false;
37
38                 // Plaintext listeners have their own policy.
39                 SSLIOHook* sslhook = SSLIOHook::IsSSL(&user->eh);
40                 if (!sslhook)
41                         return true;
42
43                 // If no hostname has been provided for the connection, an STS persistence policy SHOULD NOT be advertised.
44                 std::string snihost;
45                 if (!sslhook->GetServerName(snihost))
46                         return false;
47
48                 // Before advertising an STS persistence policy over a secure connection, servers SHOULD verify whether the
49                 // hostname provided by clients, for example, via TLS Server Name Indication (SNI), has been whitelisted by
50                 // administrators in the server configuration.
51                 return InspIRCd::Match(snihost, host, ascii_case_insensitive_map);
52         }
53
54         bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
55         {
56                 // Clients MUST NOT request this capability with CAP REQ. Servers MAY reply with a CAP NAK message if a
57                 // client requests this capability.
58                 return false;
59         }
60
61         const std::string* GetValue(LocalUser* user) const CXX11_OVERRIDE
62         {
63                 return SSLIOHook::IsSSL(&user->eh) ? &securepolicy : &plaintextpolicy;
64         }
65
66  public:
67         STSCap(Module* mod)
68                 : Cap::Capability(mod, "sts")
69         {
70         }
71
72         ~STSCap()
73         {
74                 // TODO: Send duration=0 when STS vanishes.
75         }
76
77         void SetPolicy(const std::string& newhost, unsigned long duration, unsigned int port, bool preload)
78         {
79                 // To enforce an STS upgrade policy, servers MUST send this key to insecurely connected clients. Servers
80                 // MAY send this key to securely connected clients, but it will be ignored.
81                 std::string newplaintextpolicy("port=");
82                 newplaintextpolicy.append(ConvToStr(port));
83
84                 // To enforce an STS persistence policy, servers MUST send this key to securely connected clients. Servers
85                 // MAY send this key to all clients, but insecurely connected clients MUST ignore it.
86                 std::string newsecurepolicy("duration=");
87                 newsecurepolicy.append(ConvToStr(duration));
88
89                 // Servers MAY send this key to all clients, but insecurely connected clients MUST ignore it.
90                 if (preload)
91                         newsecurepolicy.append(",preload");
92
93                 // Apply the new policy.
94                 bool changed = false;
95                 if (!irc::equals(host, newhost))
96                 {
97                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Changing STS SNI hostname from \"%s\" to \"%s\"", host.c_str(), newhost.c_str());
98                         host = newhost;
99                         changed = true;
100                 }
101
102                 if (plaintextpolicy != newplaintextpolicy)
103                 {
104                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Changing plaintext STS policy from \"%s\" to \"%s\"", plaintextpolicy.c_str(), newplaintextpolicy.c_str());
105                         plaintextpolicy.swap(newplaintextpolicy);
106                         changed = true;
107                 }
108
109                 if (securepolicy != newsecurepolicy)
110                 {
111                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Changing secure STS policy from \"%s\" to \"%s\"", securepolicy.c_str(), newsecurepolicy.c_str());
112                         securepolicy.swap(newsecurepolicy);
113                         changed = true;
114                 }
115
116                 // If the policy has changed then notify all clients via cap-notify.
117                 if (changed)
118                         NotifyValueChange();
119         }
120 };
121
122 class ModuleIRCv3STS : public Module
123 {
124  private:
125         STSCap cap;
126
127         // The IRCv3 STS specification requires that the server is listening using SSL using a valid certificate.
128         bool HasValidSSLPort(unsigned int port)
129         {
130                 for (std::vector<ListenSocket*>::const_iterator iter = ServerInstance->ports.begin(); iter != ServerInstance->ports.end(); ++iter)
131                 {
132                         ListenSocket* ls = *iter;
133                         
134                         // Is this listener on the right port?
135                         unsigned int saport = ls->bind_sa.port();
136                         if (saport != port)
137                                 continue;
138
139                         // Is this listener using SSL?
140                         if (ls->bind_tag->getString("ssl").empty())
141                                 continue;
142
143                         // TODO: Add a way to check if a listener's TLS cert is CA-verified.
144                         return true;
145                 }
146                 return false;
147         }
148
149  public:
150         ModuleIRCv3STS()
151                 : cap(this)
152         {
153         }
154
155         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
156         {
157                 // TODO: Multiple SNI profiles
158                 ConfigTag* tag = ServerInstance->Config->ConfValue("sts");
159                 if (tag == ServerInstance->Config->EmptyTag)
160                         throw ModuleException("You must define a STS policy!");
161
162                 const std::string host = tag->getString("host");
163                 if (host.empty())
164                         throw ModuleException("<sts:host> must contain a hostname, at " + tag->getTagLocation());
165
166                 int port = tag->getInt("port");
167                 if (!HasValidSSLPort(port))
168                         throw ModuleException("<sts:port> must be a TLS port, at " + tag->getTagLocation());
169
170                 unsigned long duration = tag->getDuration("duration", 60*60*24*30*2, 0, LONG_MAX);
171                 bool preload = tag->getBool("preload");
172                 cap.SetPolicy(host, duration, port, preload);
173         }
174
175         Version GetVersion() CXX11_OVERRIDE
176         {
177                 return Version("Provides IRCv3 Strict Transport Security policy advertisement", VF_OPTCOMMON);
178         }
179 };
180
181 MODULE_INIT(ModuleIRCv3STS)