001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.daemon;
027:
028: import com.sshtools.common.configuration.*;
029:
030: import com.sshtools.daemon.configuration.*;
031: import com.sshtools.daemon.forwarding.*;
032: import com.sshtools.daemon.session.*;
033:
034: import com.sshtools.j2ssh.configuration.*;
035: import com.sshtools.j2ssh.connection.*;
036:
037: import org.apache.commons.logging.*;
038:
039: import java.io.*;
040:
041: import java.net.*;
042:
043: /**
044: *
045: *
046: * @author $author$
047: * @version $Revision: 1.17 $
048: */
049: public class SshDaemon {
050: private static Log log = LogFactory.getLog(SshDaemon.class);
051:
052: /**
053: *
054: *
055: * @param args
056: */
057: public static void main(String[] args) {
058: try {
059: XmlServerConfigurationContext context = new XmlServerConfigurationContext();
060: context.setServerConfigurationResource(ConfigurationLoader
061: .checkAndGetProperty("sshtools.server",
062: "server.xml"));
063: context.setPlatformConfigurationResource(System
064: .getProperty("sshtools.platform", "platform.xml"));
065: ConfigurationLoader.initialize(false, context);
066:
067: XmlConfigurationContext context2 = new XmlConfigurationContext();
068: context2.setFailOnError(false);
069: context2.setAPIConfigurationResource(ConfigurationLoader
070: .checkAndGetProperty("sshtools.config",
071: "sshtools.xml"));
072: context2
073: .setAutomationConfigurationResource(ConfigurationLoader
074: .checkAndGetProperty("sshtools.automate",
075: "automation.xml"));
076: ConfigurationLoader.initialize(false, context2);
077:
078: if (args.length > 0) {
079: if (args[0].equals("-start")) {
080: start();
081: } else if (args[0].equals("-stop")) {
082: if (args.length > 1) {
083: stop(args[1]);
084: } else {
085: stop("The framework daemon is shutting down");
086: }
087: } else {
088: System.out
089: .println("Usage: SshDaemon [-start|-stop]");
090: }
091: } else {
092: System.out.println("Usage: SshDaemon [-start|-stop]");
093: }
094: } catch (Exception e) {
095: log.error("The server failed to process the "
096: + ((args.length > 0) ? args[0] : "") + " command",
097: e);
098: }
099: }
100:
101: /**
102: *
103: *
104: * @throws IOException
105: */
106: public static void start() throws IOException {
107: // We need at least one host key
108: SshServer server = new SshServer() {
109: public void configureServices(ConnectionProtocol connection)
110: throws IOException {
111: connection.addChannelFactory(
112: SessionChannelFactory.SESSION_CHANNEL,
113: new SessionChannelFactory());
114:
115: if (ConfigurationLoader
116: .isConfigurationAvailable(ServerConfiguration.class)) {
117: if (((ServerConfiguration) ConfigurationLoader
118: .getConfiguration(ServerConfiguration.class))
119: .getAllowTcpForwarding()) {
120: ForwardingServer forwarding = new ForwardingServer(
121: connection);
122: }
123: }
124: }
125:
126: public void shutdown(String msg) {
127: // Disconnect all sessions
128: }
129: };
130:
131: server.startServer();
132: }
133:
134: /**
135: *
136: *
137: * @param msg
138: *
139: * @throws IOException
140: */
141: public static void stop(String msg) throws IOException {
142: try {
143: Socket socket = new Socket(
144: InetAddress.getLocalHost(),
145: ((ServerConfiguration) ConfigurationLoader
146: .getConfiguration(ServerConfiguration.class))
147: .getCommandPort());
148:
149: // Write the command id
150: socket.getOutputStream().write(0x3a);
151:
152: // Write the length of the message (max 255)
153: int len = (msg.length() <= 255) ? msg.length() : 255;
154: socket.getOutputStream().write(len);
155:
156: // Write the message
157: if (len > 0) {
158: socket.getOutputStream().write(
159: msg.substring(0, len).getBytes());
160: }
161:
162: socket.close();
163: } catch (IOException ioe) {
164: ioe.printStackTrace();
165: }
166: }
167: }
|