📄 remotemanagerhandler.java
字号:
/*********************************************************************** * Copyright (c) 2000-2004 The Apache Software Foundation. * * All rights reserved. * * ------------------------------------------------------------------- * * Licensed 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.james.remotemanager;import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;import org.apache.avalon.excalibur.pool.Poolable;import org.apache.avalon.framework.configuration.Configurable;import org.apache.avalon.framework.configuration.Configuration;import org.apache.avalon.framework.configuration.ConfigurationException;import org.apache.avalon.framework.activity.Disposable;import org.apache.avalon.framework.logger.AbstractLogEnabled;import org.apache.james.Constants;import org.apache.james.services.*;import org.apache.james.userrepository.DefaultUser;import org.apache.james.util.watchdog.Watchdog;import org.apache.james.util.watchdog.WatchdogTarget;import org.apache.mailet.MailAddress;import javax.mail.internet.ParseException;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.Socket;import java.util.HashMap;import java.util.Iterator;import java.util.Locale;import java.util.StringTokenizer;/** * Provides a really rude network interface to administer James. * Allow to add accounts. * TODO: -improve protocol * -much more... * * @version $Revision: 1.21.4.6 $ * */public class RemoteManagerHandler extends AbstractLogEnabled implements ConnectionHandler, Poolable { /** * The text string for the ADDUSER command */ private static final String COMMAND_ADDUSER = "ADDUSER"; /** * The text string for the SETPASSWORD command */ private static final String COMMAND_SETPASSWORD = "SETPASSWORD"; /** * The text string for the DELUSER command */ private static final String COMMAND_DELUSER = "DELUSER"; /** * The text string for the LISTUSERS command */ private static final String COMMAND_LISTUSERS = "LISTUSERS"; /** * The text string for the COUNTUSERS command */ private static final String COMMAND_COUNTUSERS = "COUNTUSERS"; /** * The text string for the VERIFY command */ private static final String COMMAND_VERIFY = "VERIFY"; /** * The text string for the HELP command */ private static final String COMMAND_HELP = "HELP"; /** * The text string for the SETFORWARDING command */ private static final String COMMAND_SETFORWARDING = "SETFORWARDING"; /** * The text string for the SHOWFORWARDING command */ private static final String COMMAND_SHOWFORWARDING = "SHOWFORWARDING"; /** * The text string for the UNSETFORWARDING command */ private static final String COMMAND_UNSETFORWARDING = "UNSETFORWARDING"; /** * The text string for the SETALIAS command */ private static final String COMMAND_SETALIAS = "SETALIAS"; /** * The text string for the SHOWALIAS command */ private static final String COMMAND_SHOWALIAS = "SHOWALIAS"; /** * The text string for the UNSETALIAS command */ private static final String COMMAND_UNSETALIAS = "UNSETALIAS"; /** * The text string for the USER command */ private static final String COMMAND_USER = "USER"; /** * The text string for the QUIT command */ private static final String COMMAND_QUIT = "QUIT"; /** * The text string for the SHUTDOWN command */ private static final String COMMAND_SHUTDOWN = "SHUTDOWN"; /** * The per-service configuration data that applies to all handlers */ private RemoteManagerHandlerConfigurationData theConfigData; /** * The current UsersRepository being managed/viewed/modified */ private UsersRepository users; /** * Whether the local users repository should be used to store new * users. */ private boolean inLocalUsers = true; /** * The reader associated with incoming commands. */ private BufferedReader in; /** * The writer to which outgoing messages are written. */ private PrintWriter out; /** * The thread executing this handler */ private Thread handlerThread; /** * The TCP/IP socket over which the RemoteManager interaction * is occurring */ private Socket socket; /** * The watchdog being used by this handler to deal with idle timeouts. */ private Watchdog theWatchdog; /** * The watchdog target that idles out this handler. */ private WatchdogTarget theWatchdogTarget = new RemoteManagerWatchdogTarget(); /** * Set the configuration data for the handler. * * @param theData the configuration data */ void setConfigurationData(RemoteManagerHandlerConfigurationData theData) { theConfigData = theData; // Reset the users repository to the default. users = theConfigData.getUsersRepository(); inLocalUsers = true; } /** * Set the Watchdog for use by this handler. * * @param theWatchdog the watchdog */ void setWatchdog(Watchdog theWatchdog) { this.theWatchdog = theWatchdog; } /** * Gets the Watchdog Target that should be used by Watchdogs managing * this connection. * * @return the WatchdogTarget */ WatchdogTarget getWatchdogTarget() { return theWatchdogTarget; } /** * Idle out this connection */ void idleClose() { if (getLogger() != null) { getLogger().error("Remote Manager Connection has idled out."); } try { if (socket != null) { socket.close(); } } catch (Exception e) { // ignored } finally { socket = null; } synchronized (this) { // Interrupt the thread to recover from internal hangs if (handlerThread != null) { handlerThread.interrupt(); handlerThread = null; } } } /** * @see org.apache.avalon.cornerstone.services.connection.ConnectionHandler#handleConnection(Socket) */ public void handleConnection( final Socket connection ) throws IOException { socket = connection; String remoteIP = socket.getInetAddress().getHostAddress(); String remoteHost = socket.getInetAddress().getHostName(); synchronized (this) { handlerThread = Thread.currentThread(); } try { in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ASCII"), 512); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()), 512), false); if (getLogger().isInfoEnabled()) { StringBuffer infoBuffer = new StringBuffer(128) .append("Access from ") .append(remoteHost) .append("(") .append(remoteIP) .append(")"); getLogger().info( infoBuffer.toString() ); } writeLoggedResponse("JAMES Remote Administration Tool " + Constants.SOFTWARE_VERSION ); writeLoggedResponse("Please enter your login and password"); String login = null; String password = null; do { if (login != null) { final String message = "Login failed for " + login; writeLoggedFlushedResponse(message); } writeLoggedFlushedResponse("Login id:"); login = in.readLine().trim(); writeLoggedFlushedResponse("Password:"); password = in.readLine().trim(); } while (!password.equals(theConfigData.getAdministrativeAccountData().get(login)) || password.length() == 0); StringBuffer messageBuffer = new StringBuffer(64) .append("Welcome ") .append(login) .append(". HELP for a list of commands"); out.println( messageBuffer.toString() ); out.flush(); if (getLogger().isInfoEnabled()) { StringBuffer infoBuffer = new StringBuffer(128) .append("Login for ") .append(login) .append(" successful"); getLogger().info(infoBuffer.toString()); } try { theWatchdog.start(); while (parseCommand(in.readLine())) { theWatchdog.reset(); } theWatchdog.stop(); } catch (IOException ioe) { //We can cleanly ignore this as it's probably a socket timeout } catch (Throwable thr) { System.out.println("Exception: " + thr.getMessage()); getLogger().error("Encountered exception in handling the remote manager connection.", thr); } StringBuffer infoBuffer = new StringBuffer(64) .append("Logout for ") .append(login) .append("."); getLogger().info(infoBuffer.toString()); } catch ( final IOException e ) { out.println("Error. Closing connection"); out.flush(); if (getLogger().isErrorEnabled()) { StringBuffer exceptionBuffer = new StringBuffer(128) .append("Exception during connection from ") .append(remoteHost) .append(" (") .append(remoteIP) .append("): ") .append(e.getMessage()); getLogger().error(exceptionBuffer.toString()); } } finally { resetHandler(); } } /** * Resets the handler data to a basic state. */ private void resetHandler() { // Clear the Watchdog if (theWatchdog != null) { if (theWatchdog instanceof Disposable) { ((Disposable)theWatchdog).dispose(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -