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