2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
5 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "modules/ssl.h"
31 /** Handle /JUMPSERVER
33 class CommandJumpserver : public Command
36 bool redirect_new_users;
37 std::string redirect_to;
41 UserCertificateAPI sslapi;
43 CommandJumpserver(Module* Creator)
44 : Command(Creator, "JUMPSERVER", 0, 4)
48 syntax = "[<server> <port>[:<sslport>] <+/-an> <reason>]";
51 redirect_new_users = false;
54 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
57 reason = (parameters.size() < 4) ? "Please use this server/port instead" : parameters[3];
58 bool redirect_all_immediately = false;
59 redirect_new_users = true;
60 bool direction = true;
63 /* No parameters: jumpserver disabled */
64 if (parameters.empty())
67 user->WriteNotice("*** Disabled jumpserver (previously set to '" + redirect_to + ":" + ConvToStr(port) + "')");
69 user->WriteNotice("*** Jumpserver was not enabled.");
80 if (parameters.size() >= 3)
82 for (std::string::const_iterator n = parameters[2].begin(); n != parameters[2].end(); ++n)
93 redirect_all_immediately = direction;
96 redirect_new_users = direction;
99 user->WriteNotice("*** Invalid JUMPSERVER flag: " + ConvToStr(*n));
105 size_t delimpos = parameters[1].find(':');
106 port = ConvToInt(parameters[1].substr(0, delimpos ? delimpos : std::string::npos));
107 sslport = (delimpos == std::string::npos ? 0 : ConvToInt(parameters[1].substr(delimpos + 1)));
109 if (parameters[1].find_first_not_of("0123456789:") != std::string::npos
110 || parameters[1].rfind(':') != delimpos
111 || port > 65535 || sslport > 65535)
113 user->WriteNotice("*** Invalid port number");
117 if (redirect_all_immediately)
119 /* Redirect everyone but the oper sending the command */
120 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
121 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); )
123 // Quitting the user removes it from the list
128 t->WriteNumeric(RPL_REDIR, parameters[0], GetPort(t), "Please use this Server/Port instead");
129 ServerInstance->Users->QuitUser(t, reason);
135 n_done_s = ConvToStr(n_done);
139 if (redirect_new_users)
140 redirect_to = parameters[0];
142 user->WriteNotice("*** Set jumpserver to server '" + parameters[0] + "' port '" + (port ? ConvToStr(port) : "Auto") + ", SSL " + (sslport ? ConvToStr(sslport) : "Auto") + "', flags '+" +
143 (redirect_all_immediately ? "a" : "") + (redirect_new_users ? "n'" : "'") +
144 (n_done ? " (" + n_done_s + "user(s) redirected): " : ": ") + reason);
150 int GetPort(LocalUser* user)
152 int p = (sslapi && sslapi->GetCertificate(user) ? sslport : port);
154 p = user->GetServerPort();
159 class ModuleJumpServer : public Module
161 CommandJumpserver js;
163 ModuleJumpServer() : js(this)
167 void OnModuleRehash(User* user, const std::string& param) CXX11_OVERRIDE
169 if (irc::equals(param, "jumpserver") && js.redirect_new_users)
170 js.redirect_new_users = false;
173 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
175 if (js.redirect_new_users)
177 int port = js.GetPort(user);
178 user->WriteNumeric(RPL_REDIR, js.redirect_to, port, "Please use this Server/Port instead");
179 ServerInstance->Users->QuitUser(user, js.reason);
182 return MOD_RES_PASSTHRU;
185 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
187 // Emergency way to unlock
189 js.redirect_new_users = false;
192 Version GetVersion() CXX11_OVERRIDE
194 return Version("Provides support for the RPL_REDIR numeric and the /JUMPSERVER command.", VF_VENDOR);
198 MODULE_INIT(ModuleJumpServer)