]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_starttls.cpp
Update copyright headers.
[user/henk/code/inspircd.git] / src / modules / m_starttls.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Robby <robby@chatbelgie.be>
5  *   Copyright (C) 2018 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2014 Adam <Adam@anope.org>
7  *   Copyright (C) 2013, 2015-2016 Attila Molnar <attilamolnar@hush.com>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "modules/ssl.h"
25 #include "modules/cap.h"
26
27 // From IRCv3 tls-3.1
28 enum
29 {
30         RPL_STARTTLS = 670,
31         ERR_STARTTLS = 691
32 };
33
34 class CommandStartTLS : public SplitCommand
35 {
36         dynamic_reference_nocheck<IOHookProvider>& ssl;
37
38  public:
39         CommandStartTLS(Module* mod, dynamic_reference_nocheck<IOHookProvider>& s)
40                 : SplitCommand(mod, "STARTTLS")
41                 , ssl(s)
42         {
43                 works_before_reg = true;
44         }
45
46         CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
47         {
48                 if (!ssl)
49                 {
50                         user->WriteNumeric(ERR_STARTTLS, "STARTTLS is not enabled");
51                         return CMD_FAILURE;
52                 }
53
54                 if (user->registered == REG_ALL)
55                 {
56                         user->WriteNumeric(ERR_STARTTLS, "STARTTLS is not permitted after client registration is complete");
57                         return CMD_FAILURE;
58                 }
59
60                 if (user->eh.GetIOHook())
61                 {
62                         user->WriteNumeric(ERR_STARTTLS, "STARTTLS failure");
63                         return CMD_FAILURE;
64                 }
65
66                 user->WriteNumeric(RPL_STARTTLS, "STARTTLS successful, go ahead with TLS handshake");
67                 /* We need to flush the write buffer prior to adding the IOHook,
68                  * otherwise we'll be sending this line inside the SSL session - which
69                  * won't start its handshake until the client gets this line. Currently,
70                  * we assume the write will not block here; this is usually safe, as
71                  * STARTTLS is sent very early on in the registration phase, where the
72                  * user hasn't built up much sendq. Handling a blocked write here would
73                  * be very annoying.
74                  */
75                 user->eh.DoWrite();
76
77                 ssl->OnAccept(&user->eh, NULL, NULL);
78
79                 return CMD_SUCCESS;
80         }
81 };
82
83 class ModuleStartTLS : public Module
84 {
85         CommandStartTLS starttls;
86         Cap::Capability tls;
87         dynamic_reference_nocheck<IOHookProvider> ssl;
88
89  public:
90         ModuleStartTLS()
91                 : starttls(this, ssl)
92                 , tls(this, "tls")
93                 , ssl(this, "ssl")
94         {
95         }
96
97         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
98         {
99                 ConfigTag* conf = ServerInstance->Config->ConfValue("starttls");
100
101                 std::string newprovider = conf->getString("provider");
102                 if (newprovider.empty())
103                         ssl.SetProvider("ssl");
104                 else
105                         ssl.SetProvider("ssl/" + newprovider);
106         }
107
108         Version GetVersion() CXX11_OVERRIDE
109         {
110                 return Version("Provides the STARTTLS command", VF_VENDOR);
111         }
112 };
113
114 MODULE_INIT(ModuleStartTLS)