788ed9d1768b9b9e3add6425113a92a49ee4820b
[public/netxms.git] / src / client / nxshell / nxshell.cpp
1 /*
2 ** nxshell - launcher for main Java application
3 ** Copyright (C) 2017 Raden Solutions
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 **/
20
21 #include <nms_common.h>
22 #include <nms_util.h>
23 #include <nxjava.h>
24
25 #if HAVE_GETOPT_H
26 #include <getopt.h>
27 #endif
28
29 /**
30 * Options
31 */
32 static const char *s_optHost = "127.0.0.1";
33 static const char *s_optPort = "";
34 static const char *s_optUser = "admin";
35 static const char *s_optPassword = "";
36 static const char *s_optJre = NULL;
37 static const char *s_optClassPath = NULL;
38
39 /**
40 * Start application
41 */
42 static int StartApp(int argc, char *argv[])
43 {
44 TCHAR jre[MAX_PATH];
45 if (s_optJre != NULL)
46 {
47 #ifdef UNICODE
48 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, s_optJre, -1, jre, MAX_PATH);
49 jre[MAX_PATH - 1] = 0;
50 #else
51 nx_strncpy(jre, s_optJre, MAX_PATH);
52 #endif
53 }
54 else
55 {
56 if (FindJavaRuntime(jre, MAX_PATH) == NULL)
57 {
58 _tprintf(_T("Cannot find suitable Java runtime environment\n"));
59 return 2;
60 }
61 }
62 nxlog_debug(1, _T("Using JRE: %s"), jre);
63
64 StringList vmOptions;
65 char buffer[256];
66 snprintf(buffer, 256, "-Dnetxms.server=%s", s_optHost);
67 vmOptions.addMBString(buffer);
68 snprintf(buffer, 256, "-Dnetxms.port=%s", s_optPort);
69 vmOptions.addMBString(buffer);
70 snprintf(buffer, 256, "-Dnetxms.login=%s", s_optUser);
71 vmOptions.addMBString(buffer);
72
73 char clearPassword[128];
74 DecryptPasswordA(s_optUser, s_optPassword, clearPassword, 128);
75 if (clearPassword[0] != 0)
76 {
77 snprintf(buffer, 256, "-Dnetxms.password=%s", clearPassword);
78 vmOptions.addMBString(buffer);
79 }
80
81 bool verboseVM = (nxlog_get_debug_level() > 0);
82 if (verboseVM)
83 {
84 vmOptions.add(_T("-verbose:jni"));
85 vmOptions.add(_T("-verbose:class"));
86 }
87
88 int rc = 0;
89
90 #ifdef UNICODE
91 TCHAR *cp = WideStringFromMBStringSysLocale(s_optClassPath);
92 #else
93 #define cp s_optClassPath
94 #endif
95 JNIEnv *env;
96 JavaBridgeError err = CreateJavaVM(jre, _T("nxshell.jar"), cp, &vmOptions, &env);
97 if (err == NXJAVA_SUCCESS)
98 {
99 nxlog_debug(5, _T("JVM created"));
100 err = StartJavaApplication(env, "org/netxms/Shell", argc, argv);
101 if (err != NXJAVA_SUCCESS)
102 {
103 _tprintf(_T("Cannot start Java application (%s)"), GetJavaBridgeErrorMessage(err));
104 rc = 4;
105 }
106 DestroyJavaVM();
107 }
108 else
109 {
110 _tprintf(_T("Unable to create Java VM (%s)\n"), GetJavaBridgeErrorMessage(err));
111 rc = 3;
112 }
113 return rc;
114 }
115
116 /**
117 * Command line options
118 */
119 #if HAVE_DECL_GETOPT_LONG
120 static struct option longOptions[] =
121 {
122 { (char *)"classpath", required_argument, NULL, 'C' },
123 { (char *)"debug", no_argument, NULL, 'D' },
124 { (char *)"help", no_argument, NULL, 'h' },
125 { (char *)"host", required_argument, NULL, 'H' },
126 { (char *)"jre", required_argument, NULL, 'j' },
127 { (char *)"password", required_argument, NULL, 'P' },
128 { (char *)"port", required_argument, NULL, 'p' },
129 { (char *)"user", required_argument, NULL, 'u' },
130 { (char *)"version", no_argument, NULL, 'v' },
131 { NULL, 0, NULL, 0 }
132 };
133 #endif
134
135 #define SHORT_OPTIONS "C:DhH:j:p:P:u:v"
136
137 /**
138 * Print usage info
139 */
140 static void usage(bool showVersion)
141 {
142 if (showVersion)
143 {
144 _tprintf(
145 _T("NetXMS Interactive Shell Version ") NETXMS_VERSION_STRING _T("\n")
146 _T("Copyright (c) 2006-2017 Raden Solutions\n\n"));
147 }
148
149 _tprintf(
150 _T("Usage: nxshell [OPTIONS] [script]\n")
151 _T(" \n")
152 _T("Options:\n")
153 #if HAVE_GETOPT_LONG
154 _T(" -C, --classpath <path> Additional Java class path.\n")
155 _T(" -D, --debug Show additional debug output.\n")
156 _T(" -h, --help Display this help message.\n")
157 _T(" -H, --host <hostname> Specify host name or IP address. Could be in host:port form.\n")
158 _T(" -j, --jre <path> Specify JRE location.\n")
159 _T(" -p, --port <port> Specify TCP port for connection. Default is 4701.\n")
160 _T(" -P, --password <password> Specify user's password. Default is empty.\n")
161 _T(" -u, --user <user> Login to server as user. Default is \"admin\".\n")
162 _T(" -v, --version Display version information.\n\n")
163 #else
164 _T(" -C <path> Additional Java class path.\n")
165 _T(" -D Show additional debug output.\n")
166 _T(" -h Display this help message.\n")
167 _T(" -H <hostname> Specify host name or IP address. Could be in host:port form.\n")
168 _T(" -j <path> Specify JRE location.\n")
169 _T(" -p <port> Specify TCP port for connection. Default is 4701.\n")
170 _T(" -P <password> Specify user's password. Default is empty.\n")
171 _T(" -u <user> Login to server as user. Default is \"admin\".\n")
172 _T(" -v Display version information.\n\n")
173 #endif
174 );
175 }
176
177 /**
178 * Debug writer
179 */
180 static void DebugWriter(const TCHAR *msg)
181 {
182 _tprintf(_T("DBG: %s\n"), msg);
183 }
184
185 /**
186 * Entry point
187 */
188 int main(int argc, char *argv[])
189 {
190 int ret = 0;
191
192 InitNetXMSProcess(true);
193
194 opterr = 0;
195 int c;
196 #if HAVE_DECL_GETOPT_LONG
197 while ((c = getopt_long(argc, argv, SHORT_OPTIONS, longOptions, NULL)) != -1)
198 #else
199 while ((c = getopt(argc, argv, SHORT_OPTIONS)) != -1)
200 #endif
201 {
202 switch(c)
203 {
204 case 'C': // classpath
205 s_optClassPath = optarg;
206 break;
207 case 'D': // Additional debug
208 nxlog_set_debug_writer(DebugWriter);
209 nxlog_set_debug_level(9);
210 break;
211 case 'h': // help
212 usage(true);
213 exit(0);
214 break;
215 case 'H': // host
216 s_optHost = optarg;
217 break;
218 case 'j': // JRE
219 s_optJre = optarg;
220 break;
221 case 'p': // port
222 s_optPort = optarg;
223 break;
224 case 'P': // password
225 s_optPassword = optarg;
226 break;
227 case 'u': // user
228 s_optUser = optarg;
229 break;
230 case 'v': // version
231 _tprintf(
232 _T("NetXMS Interactive Shell Version ") NETXMS_VERSION_STRING _T("\n")
233 _T("Copyright (c) 2006-2017 Raden Solutions\n\n"));
234 exit(0);
235 break;
236 case '?':
237 exit(1);
238 break;
239 }
240 }
241
242 return StartApp(argc - optind, &argv[optind]);
243 }