1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.manager.host;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Serial;
import java.lang.management.ManagementFactory;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.StringTokenizer;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import jakarta.servlet.ServletException;
import jakarta.servlet.UnavailableException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.catalina.Container;
import org.apache.catalina.ContainerServlet;
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.Wrapper;
import org.apache.catalina.core.ContainerBase;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.HostConfig;
import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.buf.StringUtils;
import org.apache.tomcat.util.res.StringManager;
/**
* Servlet that enables remote management of the virtual hosts installed on the server. Normally, this functionality
* will be protected by a security constraint in the web application deployment descriptor. However, this requirement
* can be relaxed during testing.
* <p>
* This servlet examines the value returned by <code>getPathInfo()</code> and related query parameters to determine what
* action is being requested. The following actions and parameters (starting after the servlet path) are supported:
* <ul>
* <li><b>/add?name={host-name}&aliases={host-aliases}&manager={manager}</b> - Create and add a new virtual
* host. The <code>host-name</code> attribute indicates the name of the new host. The <code>host-aliases</code>
* attribute is a comma separated list of the host alias names. The <code>manager</code> attribute is a boolean value
* indicating if the webapp manager will be installed in the newly created host (optional, false by default).</li>
* <li><b>/remove?name={host-name}</b> - Remove a virtual host. The <code>host-name</code> attribute indicates the name
* of the host.</li>
* <li><b>/list</b> - List the virtual hosts installed on the server. Each host will be listed with the following format
* <code>host-name#host-aliases</code>.</li>
* <li><b>/start?name={host-name}</b> - Start the virtual host.</li>
* <li><b>/stop?name={host-name}</b> - Stop the virtual host.</li>
* </ul>
* <p>
* <b>NOTE</b> - Attempting to stop or remove the host containing this servlet itself will not succeed. Therefore, this
* servlet should generally be deployed in a separate virtual host.
* <p>
* The following servlet initialization parameters are recognized:
* <ul>
* <li><b>debug</b> - The debugging detail level that controls the amount of information that is logged by this servlet.
* Default is zero.
* </ul>
*/
public class HostManagerServlet extends HttpServlet implements ContainerServlet {
@Serial
private static final long serialVersionUID = 1L;
// ----------------------------------------------------- Instance Variables
/**
* The Context container associated with our web application.
*/
protected transient Context context = null;
/**
* The debugging detail level for this servlet.
*/
protected int debug = 1;
/**
* The associated host.
*/
protected transient Host installedHost = null;
/**
* The associated engine.
*/
protected transient Engine engine = null;
/**
* The string manager for this package.
*/
protected static final StringManager sm = StringManager.getManager(Constants.Package);
/**
* The Wrapper container associated with this servlet.
*/
protected transient Wrapper wrapper = null;
// ----------------------------------------------- ContainerServlet Methods
@Override
public Wrapper getWrapper() {
return this.wrapper;
}
@Override
public void setWrapper(Wrapper wrapper) {
this.wrapper = wrapper;
if (wrapper == null) {
context = null;
installedHost = null;
engine = null;
} else {
context = (Context) wrapper.getParent();
installedHost = (Host) context.getParent();
engine = (Engine) installedHost.getParent();
}
}
// --------------------------------------------------------- Public Methods
@Override
public void destroy() {
// No actions necessary
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
StringManager smClient = StringManager.getManager(Constants.Package, request.getLocales());
// Identify the request parameters that we need
String command = request.getPathInfo();
if (command == null) {
command = request.getServletPath();
}
String name = request.getParameter("name");
// Prepare our output writer to generate the response message
response.setContentType("text/plain; charset=" + Constants.CHARSET);
// Stop older versions of IE thinking they know best. We set text/plain
// in the line above for a reason. IE's behaviour is unwanted at best
// and dangerous at worst.
response.setHeader("X-Content-Type-Options", "nosniff");
PrintWriter writer = response.getWriter();
// Process the requested command
if (command == null) {
writer.println(smClient.getString("hostManagerServlet.noCommand"));
} else if (command.equals("/add")) {
add(request, writer, name, false, smClient);
} else if (command.equals("/remove")) {
remove(writer, name, smClient);
} else if (command.equals("/list")) {
list(writer, smClient);
} else if (command.equals("/start")) {
start(writer, name, smClient);
} else if (command.equals("/stop")) {
stop(writer, name, smClient);
} else if (command.equals("/persist")) {
persist(writer, smClient);
} else {
writer.println(smClient.getString("hostManagerServlet.unknownCommand", command));
}
// Finish up the response
writer.flush();
writer.close();
}
/**
* Add host with the given parameters.
*
* @param request The request
* @param writer The output writer
* @param name The host name
* @param htmlMode Flag value
* @param smClient StringManager for the client's locale
*/
protected void add(HttpServletRequest request, PrintWriter writer, String name, boolean htmlMode,
StringManager smClient) {
String aliases = request.getParameter("aliases");
String appBase = request.getParameter("appBase");
boolean manager = booleanParameter(request, "manager", false, htmlMode);
boolean autoDeploy = booleanParameter(request, "autoDeploy", true, htmlMode);
boolean deployOnStartup = booleanParameter(request, "deployOnStartup", true, htmlMode);
boolean deployXML = booleanParameter(request, "deployXML", true, htmlMode);
boolean unpackWARs = booleanParameter(request, "unpackWARs", true, htmlMode);
boolean copyXML = booleanParameter(request, "copyXML", false, htmlMode);
add(writer, name, aliases, appBase, manager, autoDeploy, deployOnStartup, deployXML, unpackWARs, copyXML,
smClient);
}
/**
* Extract boolean value from checkbox with default.
*
* @param request The Servlet request
* @param parameter The parameter name
* @param theDefault Default value
* @param htmlMode Flag value
*
* @return the boolean value for the parameter
*/
protected boolean booleanParameter(HttpServletRequest request, String parameter, boolean theDefault,
boolean htmlMode) {
String value = request.getParameter(parameter);
boolean booleanValue = theDefault;
if (value != null) {
if (htmlMode) {
if (value.equals("on")) {
booleanValue = true;
}
} else if (theDefault) {
if (value.equals("false")) {
booleanValue = false;
}
} else if (value.equals("true")) {
booleanValue = true;
}
} else if (htmlMode) {
booleanValue = false;
}
return booleanValue;
}
@Override
public void init() throws ServletException {
// Ensure that our ContainerServlet properties have been set
if (wrapper == null || context == null) {
throw new UnavailableException(sm.getString("hostManagerServlet.noWrapper"));
}
// Set our properties from the initialization parameters
String value;
try {
value = getServletConfig().getInitParameter("debug");
debug = Integer.parseInt(value);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
}
}
// -------------------------------------------------------- Private Methods
/**
* Add a host using the specified parameters.
*
* @param writer Writer to render results to
* @param name host name
* @param aliases comma separated alias list
* @param appBase application base for the host
* @param manager should the manager webapp be deployed to the new host ?
* @param autoDeploy Flag value
* @param deployOnStartup Flag value
* @param deployXML Flag value
* @param unpackWARs Flag value
* @param copyXML Flag value
* @param smClient StringManager for the client's locale
*/
protected synchronized void add(PrintWriter writer, String name, String aliases, String appBase, boolean manager,
boolean autoDeploy, boolean deployOnStartup, boolean deployXML, boolean unpackWARs, boolean copyXML,
StringManager smClient) {
if (debug >= 1) {
log(sm.getString("hostManagerServlet.add", name));
}
// Validate the requested host name
if (name == null || name.isEmpty()) {
writer.println(smClient.getString("hostManagerServlet.invalidHostName", name));
return;
}
// Check if host already exists
if (engine.findChild(name) != null) {
writer.println(smClient.getString("hostManagerServlet.alreadyHost", name));
return;
}
// Validate and create appBase
File appBaseFile;
File file;
String applicationBase = appBase;
if (applicationBase == null || applicationBase.isEmpty()) {
applicationBase = name;
}
file = new File(applicationBase);
if (!file.isAbsolute()) {
file = new File(engine.getCatalinaBase(), file.getPath());
}
try {
appBaseFile = file.getCanonicalFile();
} catch (IOException ioe) {
appBaseFile = file;
}
if (!appBaseFile.mkdirs() && !appBaseFile.isDirectory()) {
writer.println(smClient.getString("hostManagerServlet.appBaseCreateFail", appBaseFile.toString(), name));
return;
}
// Create base for config files
File configBaseFile = getConfigBase(name);
// Copy manager.xml if requested
if (manager) {
if (configBaseFile == null) {
writer.println(smClient.getString("hostManagerServlet.configBaseCreateFail", name));
return;
}
try (InputStream is = getServletContext().getResourceAsStream("/WEB-INF/manager.xml")) {
if (is == null) {
writer.println(smClient.getString("hostManagerServlet.managerXml"));
return;
}
Path dest = new File(configBaseFile, "manager.xml").toPath();
Files.copy(is, dest);
} catch (IOException ioe) {
writer.println(smClient.getString("hostManagerServlet.managerXml"));
return;
}
}
StandardHost host = new StandardHost();
host.setAppBase(applicationBase);
host.setName(name);
host.addLifecycleListener(new HostConfig());
// Add host aliases
if (aliases != null && !aliases.isEmpty()) {
StringTokenizer tok = new StringTokenizer(aliases, ", ");
while (tok.hasMoreTokens()) {
host.addAlias(tok.nextToken());
}
}
host.setAutoDeploy(autoDeploy);
host.setDeployOnStartup(deployOnStartup);
host.setDeployXML(deployXML);
host.setUnpackWARs(unpackWARs);
host.setCopyXML(copyXML);
// Add new host
try {
engine.addChild(host);
} catch (Exception e) {
writer.println(smClient.getString("hostManagerServlet.exception", e.toString()));
return;
}
host = (StandardHost) engine.findChild(name);
if (host != null) {
writer.println(smClient.getString("hostManagerServlet.addSuccess", name));
} else {
// Something failed
writer.println(smClient.getString("hostManagerServlet.addFailed", name));
}
}
/**
* Remove the specified host.
*
* @param writer Writer to render results to
* @param name host name
* @param smClient StringManager for the client's locale
*/
protected synchronized void remove(PrintWriter writer, String name, StringManager smClient) {
if (debug >= 1) {
log(sm.getString("hostManagerServlet.remove", name));
}
// Validate the requested host name
if (name == null || name.isEmpty()) {
writer.println(smClient.getString("hostManagerServlet.invalidHostName", name));
return;
}
// Check if host exists
if (engine.findChild(name) == null) {
writer.println(smClient.getString("hostManagerServlet.noHost", name));
return;
}
// Prevent removing our own host
if (engine.findChild(name) == installedHost) {
writer.println(smClient.getString("hostManagerServlet.cannotRemoveOwnHost", name));
return;
}
// Remove host
// Note that the host will not get physically removed
try {
Container child = engine.findChild(name);
engine.removeChild(child);
if (child instanceof ContainerBase) {
child.destroy();
}
} catch (Exception e) {
writer.println(smClient.getString("hostManagerServlet.exception", e.toString()));
return;
}
Host host = (StandardHost) engine.findChild(name);
if (host == null) {
writer.println(smClient.getString("hostManagerServlet.removeSuccess", name));
} else {
// Something failed
writer.println(smClient.getString("hostManagerServlet.removeFailed", name));
}
}
/**
* Render a list of the currently active Contexts in our virtual host.
*
* @param writer Writer to render to
* @param smClient StringManager for the client's locale
*/
protected void list(PrintWriter writer, StringManager smClient) {
if (debug >= 1) {
log(sm.getString("hostManagerServlet.list", engine.getName()));
}
writer.println(smClient.getString("hostManagerServlet.listed", engine.getName()));
Container[] hosts = engine.findChildren();
for (Container container : hosts) {
Host host = (Host) container;
String name = host.getName();
String[] aliases = host.findAliases();
writer.println(String.format("[%s]:[%s]", name, StringUtils.join(aliases)));
}
}
/**
* Start the host with the specified name.
*
* @param writer Writer to render to
* @param name Host name
* @param smClient StringManager for the client's locale
*/
protected void start(PrintWriter writer, String name, StringManager smClient) {
if (debug >= 1) {
log(sm.getString("hostManagerServlet.start", name));
}
// Validate the requested host name
if (name == null || name.isEmpty()) {
writer.println(smClient.getString("hostManagerServlet.invalidHostName", name));
return;
}
Container host = engine.findChild(name);
// Check if host exists
if (host == null) {
writer.println(smClient.getString("hostManagerServlet.noHost", name));
return;
}
// Prevent starting our own host
if (host == installedHost) {
writer.println(smClient.getString("hostManagerServlet.cannotStartOwnHost", name));
return;
}
// Don't start host if already started
if (host.getState().isAvailable()) {
writer.println(smClient.getString("hostManagerServlet.alreadyStarted", name));
return;
}
// Start host
try {
host.start();
writer.println(smClient.getString("hostManagerServlet.started", name));
} catch (Exception e) {
getServletContext().log(sm.getString("hostManagerServlet.startFailed", name), e);
writer.println(smClient.getString("hostManagerServlet.startFailed", name));
writer.println(smClient.getString("hostManagerServlet.exception", e.toString()));
}
}
/**
* Stop the host with the specified name.
*
* @param writer Writer to render to
* @param name Host name
* @param smClient StringManager for the client's locale
*/
protected void stop(PrintWriter writer, String name, StringManager smClient) {
if (debug >= 1) {
log(sm.getString("hostManagerServlet.stop", name));
}
// Validate the requested host name
if (name == null || name.isEmpty()) {
writer.println(smClient.getString("hostManagerServlet.invalidHostName", name));
return;
}
Container host = engine.findChild(name);
// Check if host exists
if (host == null) {
writer.println(smClient.getString("hostManagerServlet.noHost", name));
return;
}
// Prevent stopping our own host
if (host == installedHost) {
writer.println(smClient.getString("hostManagerServlet.cannotStopOwnHost", name));
return;
}
// Don't stop host if already stopped
if (!host.getState().isAvailable()) {
writer.println(smClient.getString("hostManagerServlet.alreadyStopped", name));
return;
}
// Stop host
try {
host.stop();
writer.println(smClient.getString("hostManagerServlet.stopped", name));
} catch (Exception e) {
getServletContext().log(sm.getString("hostManagerServlet.stopFailed", name), e);
writer.println(smClient.getString("hostManagerServlet.stopFailed", name));
writer.println(smClient.getString("hostManagerServlet.exception", e.toString()));
}
}
/**
* Persist the current configuration to server.xml.
*
* @param writer Writer to render to
* @param smClient i18n resources localized for the client
*/
protected void persist(PrintWriter writer, StringManager smClient) {
if (debug >= 1) {
log(sm.getString("hostManagerServlet.persist"));
}
try {
MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName oname = new ObjectName("Catalina:type=StoreConfig");
platformMBeanServer.invoke(oname, "storeConfig", null, null);
writer.println(smClient.getString("hostManagerServlet.persisted"));
} catch (Exception e) {
getServletContext().log(sm.getString("hostManagerServlet.persistFailed"), e);
writer.println(smClient.getString("hostManagerServlet.persistFailed"));
// catch InstanceNotFoundException when StoreConfig is not enabled instead of printing
// the failure message
if (e instanceof InstanceNotFoundException) {
writer.println(smClient.getString("hostManagerServlet.noStoreConfig"));
} else {
writer.println(smClient.getString("hostManagerServlet.exception", e.toString()));
}
}
}
// -------------------------------------------------------- Support Methods
/**
* Get config base.
*
* @param hostName The host name
*
* @return the config base for the host
*/
protected File getConfigBase(String hostName) {
File configBase = new File(context.getCatalinaBase(), "conf");
if (!configBase.exists()) {
return null;
}
if (engine != null) {
configBase = new File(configBase, engine.getName());
}
if (installedHost != null) {
configBase = new File(configBase, hostName);
}
if (!configBase.mkdirs() && !configBase.isDirectory()) {
return null;
}
return configBase;
}
}
|