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
|
package com.c2kernel.scripting;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.omg.CORBA.ORB;
import org.tanukisoftware.wrapper.WrapperManager;
import com.c2kernel.entity.proxy.AgentProxy;
import com.c2kernel.entity.proxy.EntityProxyManager;
import com.c2kernel.lookup.LDAPLookup;
import com.c2kernel.persistency.TransactionManager;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Logger;
import com.c2kernel.utils.Resource;
import com.c2kernel.utils.server.SocketHandler;
import com.ibm.bsf.BSFEngine;
import com.ibm.bsf.BSFException;
import com.ibm.bsf.BSFManager;
/**************************************************************************
*
* $Revision: 1.16 $
* $Date: 2005/08/31 07:20:40 $
*
* Copyright (C) 2003 CERN - European Organization for Nuclear Research
* All rights reserved.
**************************************************************************/
public class ScriptConsole implements SocketHandler {
BufferedReader input;
PrintStream output;
Socket socket = null;
BSFManager manager;
BSFEngine context;
static ArrayList securityHosts = new ArrayList();
public static final short NONE = 0;
public static final short ALLOW = 1;
public static final short DENY = 2;
static short securityMode;
static {
securityMode = ALLOW;
String hosts = Gateway.getProperty("ItemServer.Console.allow");
if (hosts == null || hosts.equals("")) {
securityMode = DENY;
hosts = Gateway.getProperty("ItemServer.Console.deny");
}
if (hosts == null || hosts.equals("")) { // by default only allow localhost
securityMode = ALLOW;
securityHosts.add("127.0.0.1");
}
else {
StringTokenizer tok = new StringTokenizer(hosts, ",");
while(tok.hasMoreTokens()) {
try {
securityHosts.add(InetAddress.getByName(tok.nextToken()).getHostAddress());
} catch (UnknownHostException ex) {
Logger.error("Host not found "+ex.getMessage());
}
}
}
}
public ScriptConsole() {
}
public String getName() {
return "Script Console";
}
public boolean isBusy() {
return (socket != null);
}
public void setSocket(Socket newSocket) {
try {
input = new BufferedReader(new InputStreamReader(newSocket.getInputStream()));
OutputStreamWriter ansi = new OutputStreamWriter(newSocket.getOutputStream(), "US-ASCII");
output = new PrintStream(newSocket.getOutputStream());
newSocket.setSoTimeout(0);
socket = newSocket;
} catch (IOException ex) {
try {
newSocket.close();
} catch (IOException ex2) {
}
socket = null;
return;
}
}
public void shutdown() {
Socket closingSocket = socket;
socket = null;
if (closingSocket == null)
return;
try {
Logger.removeLogStream(output);
closingSocket.shutdownInput();
closingSocket.shutdownOutput();
closingSocket.close();
Logger.msg("Script console to "+closingSocket.getInetAddress()+" closed");
} catch (IOException e) {
Logger.error("Script Console to " + closingSocket.getInetAddress() + " - Error closing.");
Logger.error(e);
}
}
public void run() {
// check permission
boolean allowed = true;
if (securityMode!=NONE) {
if (securityHosts.contains(socket.getInetAddress().getHostAddress())) {
if (securityMode==DENY)
allowed = false;
}
else if (securityMode==ALLOW)
allowed = false;
}
if (!allowed) {
Logger.error("Host "+socket.getInetAddress()+" access denied");
output.println("Host "+socket.getInetAddress()+" access denied");
shutdown();
return;
}
// get system objects
try {
Logger.addLogStream(output, 0);
try {
manager = new BSFManager();
manager.declareBean("storage", Gateway.getStorage(), TransactionManager.class);
manager.declareBean("db", Gateway.getStorage().getDb(), TransactionManager.class);
manager.declareBean("proxy", Gateway.getProxyManager(), EntityProxyManager.class);
manager.declareBean("lookup", Gateway.getLDAPLookup(), LDAPLookup.class);
manager.declareBean("orb", Gateway.getORB(), ORB.class);
try {
manager.declareBean("system", Gateway.getProxyManager().getProxy(
Gateway.getLDAPLookup().getRoleManager().getAgentPath("system")), AgentProxy.class);
} catch (Exception ex) {
output.println("System agent unavailable");
}
context = manager.loadScriptingEngine("javascript");
} catch (BSFException ex) {
output.println("Error initialising environment objects");
output.flush();
shutdown();
}
StringBuffer commandBuffer = new StringBuffer();
while (socket != null) {
output.println();
output.print('>');
String command = null;
boolean gotCommand = false;
while (!gotCommand) {
try {
command = input.readLine();
gotCommand = true;
} catch (InterruptedIOException ex) {
}
}
if (command == null) // disconnected
shutdown();
else {
if (command.equals("exit")) {
shutdown();
continue;
}
else if(command.startsWith("log")) {
try {
int newLogLevel = Integer.parseInt(command.substring(4));
Logger.removeLogStream(output);
Logger.addLogStream(output, newLogLevel);
Logger.msg("Log level for "+socket.getInetAddress()+" set to "+newLogLevel);
continue;
} catch (NumberFormatException ex) { }
}
else if(command.equals("help")) {
output.println(Resource.getTextResource("textFiles/consoleHelp.txt"));
continue;
}
else if(command.equals("version")) {
output.println("Domain version: "+Resource.getDomainVersion());
output.println("Kernel version: "+Resource.getKernelVersion());
continue;
}
try {
if (command.endsWith("\\")) {
commandBuffer.append(command.substring(0,command.length()-1));
continue;
}
commandBuffer.append(command);
command = commandBuffer.toString();
commandBuffer = new StringBuffer();
Logger.msg("Console command from "+socket.getInetAddress()+": "+command);
// process control
if (command.equals("shutdown")) {
WrapperManager.stop(0);
}
else {
Object response = context.eval("Command", 0, 0, command);
if (response instanceof org.mozilla.javascript.Undefined)
output.println("Ok");
else
output.println(response);
}
} catch (Throwable ex) {
ex.printStackTrace(output);
}
output.flush();
}
}
} catch (IOException ex) {
Logger.error("IO Exception reading from script console socket");
shutdown();
}
}
}
|