refactoring of AgentConnection::getList
[public/netxms.git] / src / server / tools / nxget / nxget.cpp
CommitLineData
13438df0 1/*
5039dede 2** nxget - command line tool used to retrieve parameters from NetXMS agent
aa93afcf 3** Copyright (C) 2004-2017 Victor Kirhenshtein
5039dede
AK
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**
4e2debcc 19** File: nxget.cpp
5039dede
AK
20**
21**/
22
23#include <nms_common.h>
24#include <nms_agent.h>
25#include <nms_util.h>
bf6fb6c3 26#include <nxcpapi.h>
5039dede
AK
27#include <nxsrvapi.h>
28
29#ifndef _WIN32
30#include <netdb.h>
31#endif
32
5039dede
AK
33#define MAX_LINE_SIZE 4096
34
c73f92be
VK
35/**
36 * Operations
37 */
38enum Operation
39{
40 CMD_GET = 0,
41 CMD_LIST = 1,
42 CMD_CHECK_SERVICE = 2,
43 CMD_GET_PARAMS = 3,
44 CMD_GET_CONFIG = 4,
45 CMD_TABLE = 5,
46 CMD_GET_SCREENSHOT = 6
47};
5039dede 48
aa93afcf
VK
49/**
50 * Debug writer
51 */
b41dff2a 52static void DebugWriter(const TCHAR *tag, const TCHAR *text)
aa93afcf 53{
b41dff2a 54 if (tag == NULL)
be372638 55 _tprintf(_T("%s\n"), text);
b41dff2a
EJ
56 else
57 _tprintf(_T("<%s> %s\n"), tag, text);
aa93afcf
VK
58}
59
c73f92be
VK
60/**
61 * Get single parameter
62 */
08b214c6 63static int Get(AgentConnection *pConn, const TCHAR *pszParam, BOOL bShowName)
5039dede 64{
967893bb 65 UINT32 dwError;
08b214c6 66 TCHAR szBuffer[1024];
5039dede 67
4687826e 68 dwError = pConn->getParameter(pszParam, 1024, szBuffer);
5039dede
AK
69 if (dwError == ERR_SUCCESS)
70 {
71 if (bShowName)
08b214c6 72 _tprintf(_T("%s = %s\n"), pszParam, szBuffer);
5039dede 73 else
08b214c6 74 _tprintf(_T("%s\n"), szBuffer);
5039dede
AK
75 }
76 else
77 {
08b214c6 78 _tprintf(_T("%d: %s\n"), dwError, AgentErrorCodeToText(dwError));
5039dede
AK
79 }
80 fflush(stdout);
81 return (dwError == ERR_SUCCESS) ? 0 : 1;
82}
83
5944946e
VK
84/**
85 * Get list of values for enum parameters
86 */
08b214c6 87static int List(AgentConnection *pConn, const TCHAR *pszParam)
5039dede 88{
6a777a32
VK
89 StringList *data;
90 UINT32 rcc = pConn->getList(pszParam, &data);
91 if (rcc == ERR_SUCCESS)
5039dede 92 {
6a777a32
VK
93 for(int i = 0; i < data->size(); i++)
94 _tprintf(_T("%s\n"), data->get(i));
95 delete data;
5039dede
AK
96 }
97 else
98 {
6a777a32 99 _tprintf(_T("%d: %s\n"), rcc, AgentErrorCodeToText(rcc));
5039dede 100 }
6a777a32 101 return (rcc == ERR_SUCCESS) ? 0 : 1;
5039dede
AK
102}
103
5944946e
VK
104/**
105 * Get table value
106 */
4687826e
VK
107static int GetTable(AgentConnection *pConn, const TCHAR *pszParam)
108{
109 Table *table;
110
967893bb 111 UINT32 rcc = pConn->getTable(pszParam, &table);
4687826e
VK
112 if (rcc == ERR_SUCCESS)
113 {
c73f92be
VK
114 // calculate column widths and print headers
115 int *widths = (int *)calloc(table->getNumColumns(), sizeof(int));
116 _puttc(_T('|'), stdout);
a8799146
VK
117 for(int c = 0; c < table->getNumColumns(); c++)
118 {
c73f92be
VK
119 widths[c] = (int)_tcslen(table->getColumnName(c));
120 for(int i = 0; i < table->getNumRows(); i++)
121 {
122 int len = (int)_tcslen(table->getAsString(i, c));
123 if (len > widths[c])
124 widths[c] = len;
125 }
126 _tprintf(_T(" %*s |"), -widths[c], table->getColumnName(c));
127 }
128
a8799146 129 _puttc(_T('\n'), stdout);
4687826e
VK
130 for(int i = 0; i < table->getNumRows(); i++)
131 {
c73f92be 132 _puttc(_T('|'), stdout);
4687826e
VK
133 for(int j = 0; j < table->getNumColumns(); j++)
134 {
c73f92be 135 _tprintf(_T(" %*s |"), -widths[j], table->getAsString(i, j));
4687826e
VK
136 }
137 _puttc(_T('\n'), stdout);
138 }
139 delete table;
13438df0 140 free(widths);
4687826e
VK
141 }
142 else
143 {
144 _tprintf(_T("%u: %s\n"), rcc, AgentErrorCodeToText(rcc));
145 }
146 return (rcc == ERR_SUCCESS) ? 0 : 1;
147}
148
5944946e
VK
149/**
150 * Check network service state
151 */
c73f92be 152static int CheckService(AgentConnection *pConn, int serviceType, UINT32 dwServiceAddr,
08b214c6 153 WORD wProto, WORD wPort, const TCHAR *pszRequest, const TCHAR *pszResponse)
5039dede 154{
967893bb 155 UINT32 dwStatus, dwError;
5039dede 156
c73f92be 157 dwError = pConn->checkNetworkService(&dwStatus, dwServiceAddr, serviceType, wPort,
5039dede
AK
158 wProto, pszRequest, pszResponse);
159 if (dwError == ERR_SUCCESS)
160 {
161 printf("Service status: %d\n", dwStatus);
162 }
163 else
164 {
08b214c6 165 _tprintf(_T("%d: %s\n"), dwError, AgentErrorCodeToText(dwError));
5039dede
AK
166 }
167 return (dwError == ERR_SUCCESS) ? 0 : 1;
168}
169
86c126f5
VK
170/**
171 * List supported parameters
172 */
5039dede
AK
173static int ListParameters(AgentConnection *pConn)
174{
08b214c6 175 static const TCHAR *pszDataType[] = { _T("INT"), _T("UINT"), _T("INT64"), _T("UINT64"), _T("STRING"), _T("FLOAT"), _T("UNKNOWN") };
5039dede 176
86c126f5
VK
177 ObjectArray<AgentParameterDefinition> *paramList;
178 ObjectArray<AgentTableDefinition> *tableList;
967893bb 179 UINT32 dwError = pConn->getSupportedParameters(&paramList, &tableList);
5039dede
AK
180 if (dwError == ERR_SUCCESS)
181 {
cc8ce218 182 for(int i = 0; i < paramList->size(); i++)
5039dede 183 {
86c126f5
VK
184 AgentParameterDefinition *p = paramList->get(i);
185 _tprintf(_T("%s %s \"%s\"\n"), p->getName(),
186 pszDataType[(p->getDataType() < 6) && (p->getDataType() >= 0) ? p->getDataType() : 6],
187 p->getDescription());
5039dede 188 }
cc8ce218
VK
189 delete paramList;
190 delete tableList;
5039dede
AK
191 }
192 else
193 {
08b214c6 194 _tprintf(_T("%d: %s\n"), dwError, AgentErrorCodeToText(dwError));
5039dede
AK
195 }
196 return (dwError == ERR_SUCCESS) ? 0 : 1;
197}
198
5944946e
VK
199/**
200 * Get configuration file
201 */
5039dede
AK
202static int GetConfig(AgentConnection *pConn)
203{
967893bb 204 UINT32 dwError, dwSize;
5039dede
AK
205 TCHAR *pszFile;
206
4687826e 207 dwError = pConn->getConfigFile(&pszFile, &dwSize);
5039dede
AK
208 if (dwError == ERR_SUCCESS)
209 {
08b214c6
VK
210 TranslateStr(pszFile, _T("\r\n"), _T("\n"));
211 _fputts(pszFile, stdout);
5039dede
AK
212 if (dwSize > 0)
213 {
08b214c6 214 if (pszFile[dwSize - 1] != _T('\n'))
5039dede
AK
215 fputc('\n', stdout);
216 }
217 else
218 {
219 fputc('\n', stdout);
220 }
221 }
222 else
223 {
08b214c6 224 _tprintf(_T("%d: %s\n"), dwError, AgentErrorCodeToText(dwError));
5039dede
AK
225 }
226 return (dwError == ERR_SUCCESS) ? 0 : 1;
227}
228
5944946e
VK
229/**
230 * Get screenshot
231 */
9c786c0f 232static int GetScreenshot(AgentConnection *pConn, const char *sessionName, const char *fileName)
5944946e
VK
233{
234 BYTE *data;
235 size_t size;
9c786c0f
VK
236#ifdef UNICODE
237 WCHAR *wname = WideStringFromMBString(sessionName);
238 UINT32 dwError = pConn->takeScreenshot(wname, &data, &size);
239 free(wname);
240#else
241 UINT32 dwError = pConn->takeScreenshot(sessionName, &data, &size);
242#endif
5944946e
VK
243 if (dwError == ERR_SUCCESS)
244 {
245 FILE *f = fopen(fileName, "wb");
246 if (f != NULL)
247 {
248 if (data != NULL)
249 fwrite(data, 1, size, f);
250 fclose(f);
251 }
252 safe_free(data);
253 }
254 else
255 {
256 _tprintf(_T("%d: %s\n"), dwError, AgentErrorCodeToText(dwError));
257 }
258 return (dwError == ERR_SUCCESS) ? 0 : 1;
259}
5039dede 260
5944946e
VK
261/**
262 * Startup
263 */
5039dede
AK
264int main(int argc, char *argv[])
265{
266 char *eptr;
c73f92be
VK
267 BOOL start = TRUE, batchMode = FALSE, showNames = FALSE, useProxy = FALSE;
268 int i, ch, iPos, iExitCode = 3, iInterval = 0;
269 int authMethod = AUTH_NONE, proxyAuth = AUTH_NONE, serviceType = NETSRV_SSH;
5039dede
AK
270#ifdef _WITH_ENCRYPTION
271 int iEncryptionPolicy = ENCRYPTION_ALLOWED;
272#else
273 int iEncryptionPolicy = ENCRYPTION_DISABLED;
274#endif
c73f92be
VK
275 Operation operation = CMD_GET;
276 WORD agentPort = AGENT_LISTEN_PORT, proxyPort = AGENT_LISTEN_PORT;
5039dede 277 WORD wServicePort = 0, wServiceProto = 0;
c11eee9b 278 UINT32 dwTimeout = 5000, dwConnTimeout = 30000, dwServiceAddr = 0, dwError;
08b214c6 279 TCHAR szSecret[MAX_SECRET_LENGTH] = _T(""), szRequest[MAX_DB_STRING] = _T("");
1039d7ee
VK
280 TCHAR keyFile[MAX_PATH];
281 TCHAR szResponse[MAX_DB_STRING] = _T("");
08b214c6
VK
282 char szProxy[MAX_OBJECT_NAME] = "";
283 TCHAR szProxySecret[MAX_SECRET_LENGTH] = _T("");
5039dede 284 RSA *pServerKey = NULL;
08b214c6
VK
285#ifdef UNICODE
286 WCHAR *wcValue;
287#endif
5039dede 288
d37e7f10 289 InitNetXMSProcess(true);
5039dede 290#ifdef _WIN32
08b214c6 291 SetExceptionHandler(SEHDefaultConsoleHandler, NULL, NULL, _T("nxget"), 0, FALSE, FALSE);
5039dede 292#endif
aa93afcf 293 nxlog_set_debug_writer(DebugWriter);
5039dede 294
1039d7ee
VK
295 GetNetXMSDirectory(nxDirData, keyFile);
296 _tcscat(keyFile, DFILE_KEYS);
297
5039dede
AK
298 // Parse command line
299 opterr = 1;
aa93afcf 300 while((ch = getopt(argc, argv, "a:A:bCD:e:Ehi:IK:lno:O:p:P:r:R:s:S:t:Tvw:W:X:Z:")) != -1)
5039dede
AK
301 {
302 switch(ch)
303 {
304 case 'h': // Display help and exit
08b214c6
VK
305 _tprintf(_T("Usage: nxget [<options>] <host> [<parameter> [<parameter> ...]]\n")
306 _T("Valid options are:\n")
aa93afcf 307 _T(" -a auth : Authentication method. Valid methods are \"none\",\n")
08b214c6 308 _T(" \"plain\", \"md5\" and \"sha1\". Default is \"none\".\n")
aa93afcf 309 _T(" -A auth : Authentication method for proxy agent.\n")
08b214c6
VK
310 _T(" -b : Batch mode - get all parameters listed on command line.\n")
311 _T(" -C : Get agent's configuration file\n")
aa93afcf 312 _T(" -D level : Set debug level (default is 0).\n")
5039dede 313#ifdef _WITH_ENCRYPTION
aa93afcf 314 _T(" -e policy : Set encryption policy. Possible values are:\n")
08b214c6
VK
315 _T(" 0 = Encryption disabled;\n")
316 _T(" 1 = Encrypt connection only if agent requires encryption;\n")
317 _T(" 2 = Encrypt connection if agent supports encryption;\n")
318 _T(" 3 = Force encrypted connection;\n")
319 _T(" Default value is 1.\n")
5039dede 320#endif
aa93afcf 321 _T(" -E file : Take screenshot. First parameter is file name, second (optional) is session name.\n")
08b214c6 322 _T(" -h : Display help and exit.\n")
aa93afcf 323 _T(" -i seconds : Get specified parameter(s) continously with given interval.\n")
08b214c6 324 _T(" -I : Get list of supported parameters.\n")
5039dede 325#ifdef _WITH_ENCRYPTION
aa93afcf 326 _T(" -K file : Specify server's key file\n")
1039d7ee 327 _T(" (default is %s).\n")
5039dede 328#endif
4687826e 329 _T(" -l : Requested parameter is a list.\n")
08b214c6 330 _T(" -n : Show parameter's name in result.\n")
aa93afcf
VK
331 _T(" -o proto : Protocol number to be used for service check.\n")
332 _T(" -O port : Proxy agent's port number. Default is %d.\n")
333 _T(" -p port : Agent's port number. Default is %d.\n")
334 _T(" -P port : Network service port (to be used wth -S option).\n")
335 _T(" -r string : Service check request string.\n")
336 _T(" -R string : Service check expected response string.\n")
337 _T(" -s secret : Shared secret for authentication.\n")
338 _T(" -S addr : Check state of network service at given address.\n")
339 _T(" -t type : Set type of service to be checked.\n")
ffabff3c 340 _T(" Possible types are: custom, ssh, pop3, smtp, ftp, http, https, telnet.\n")
4687826e 341 _T(" -T : Requested parameter is a table.\n")
08b214c6 342 _T(" -v : Display version and exit.\n")
aa93afcf
VK
343 _T(" -w seconds : Set command timeout (default is 5 seconds).\n")
344 _T(" -W seconds : Set connection timeout (default is 30 seconds).\n")
345 _T(" -X addr : Use proxy agent at given address.\n")
346 _T(" -Z secret : Shared secret for proxy agent authentication.\n")
13438df0 347 _T("\n"),
1039d7ee
VK
348#ifdef _WITH_ENCRYPTION
349 keyFile,
350#endif
351 agentPort, agentPort);
c73f92be 352 start = FALSE;
5039dede
AK
353 break;
354 case 'a': // Auth method
355 case 'A':
356 if (!strcmp(optarg, "none"))
357 i = AUTH_NONE;
358 else if (!strcmp(optarg, "plain"))
359 i = AUTH_PLAINTEXT;
360 else if (!strcmp(optarg, "md5"))
361 i = AUTH_MD5_HASH;
362 else if (!strcmp(optarg, "sha1"))
363 i = AUTH_SHA1_HASH;
364 else
365 {
366 printf("Invalid authentication method \"%s\"\n", optarg);
c73f92be 367 start = FALSE;
5039dede
AK
368 }
369 if (ch == 'a')
c73f92be 370 authMethod = i;
5039dede 371 else
c73f92be 372 proxyAuth = i;
5039dede
AK
373 break;
374 case 'b': // Batch mode
c73f92be 375 batchMode = TRUE;
5039dede 376 break;
aa93afcf
VK
377 case 'D': // debug level
378 nxlog_set_debug_level((int)strtol(optarg, NULL, 0));
379 break;
5039dede
AK
380 case 'i': // Interval
381 i = strtol(optarg, &eptr, 0);
382 if ((*eptr != 0) || (i <= 0))
383 {
384 printf("Invalid interval \"%s\"\n", optarg);
c73f92be 385 start = FALSE;
5039dede
AK
386 }
387 else
388 {
389 iInterval = i;
390 }
391 break;
5944946e 392 case 'E':
c73f92be 393 operation = CMD_GET_SCREENSHOT;
5944946e 394 break;
5039dede 395 case 'I':
c73f92be 396 operation = CMD_GET_PARAMS;
5039dede
AK
397 break;
398 case 'C':
c73f92be 399 operation = CMD_GET_CONFIG;
5039dede
AK
400 break;
401 case 'l':
c73f92be 402 operation = CMD_LIST;
5039dede 403 break;
4687826e 404 case 'T':
c73f92be 405 operation = CMD_TABLE;
4687826e 406 break;
5039dede 407 case 'n': // Show names
c73f92be 408 showNames = TRUE;
5039dede
AK
409 break;
410 case 'p': // Agent's port number
411 case 'P': // Port number for service check
412 case 'O': // Proxy agent's port number
413 i = strtol(optarg, &eptr, 0);
414 if ((*eptr != 0) || (i < 0) || (i > 65535))
415 {
416 printf("Invalid port number \"%s\"\n", optarg);
c73f92be 417 start = FALSE;
5039dede
AK
418 }
419 else
420 {
421 if (ch == 'p')
c73f92be 422 agentPort = (WORD)i;
5039dede 423 else if (ch == 'O')
c73f92be 424 proxyPort = (WORD)i;
5039dede
AK
425 else
426 wServicePort = (WORD)i;
427 }
428 break;
5039dede 429 case 'r': // Service check request string
08b214c6
VK
430#ifdef UNICODE
431 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, szRequest, MAX_DB_STRING);
432 szRequest[MAX_DB_STRING - 1] = 0;
433#else
5039dede 434 nx_strncpy(szRequest, optarg, MAX_DB_STRING);
08b214c6 435#endif
5039dede
AK
436 break;
437 case 'R': // Service check response string
08b214c6
VK
438#ifdef UNICODE
439 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, szResponse, MAX_DB_STRING);
440 szResponse[MAX_DB_STRING - 1] = 0;
441#else
5039dede 442 nx_strncpy(szResponse, optarg, MAX_DB_STRING);
08b214c6 443#endif
5039dede
AK
444 break;
445 case 's': // Shared secret
08b214c6
VK
446#ifdef UNICODE
447 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, szSecret, MAX_SECRET_LENGTH);
448 szSecret[MAX_SECRET_LENGTH - 1] = 0;
449#else
5039dede 450 nx_strncpy(szSecret, optarg, MAX_SECRET_LENGTH);
08b214c6 451#endif
5039dede
AK
452 break;
453 case 'S': // Check service
c73f92be 454 operation = CMD_CHECK_SERVICE;
5039dede
AK
455 dwServiceAddr = ntohl(inet_addr(optarg));
456 if ((dwServiceAddr == INADDR_NONE) || (dwServiceAddr == INADDR_ANY))
457 {
06a93345 458 _tprintf(_T("Invalid IP address \"%hs\"\n"), optarg);
c73f92be 459 start = FALSE;
5039dede
AK
460 }
461 break;
462 case 't': // Service type
c73f92be 463 serviceType = strtol(optarg, &eptr, 0);
5039dede
AK
464 if (*eptr != 0)
465 {
adb3378f
VK
466 if (!stricmp(optarg, "custom"))
467 {
c73f92be 468 serviceType = NETSRV_CUSTOM;
adb3378f
VK
469 }
470 else if (!stricmp(optarg, "ftp"))
471 {
c73f92be 472 serviceType = NETSRV_FTP;
adb3378f
VK
473 }
474 else if (!stricmp(optarg, "http"))
475 {
c73f92be 476 serviceType = NETSRV_HTTP;
adb3378f 477 }
ffabff3c
AK
478 else if (!stricmp(optarg, "https"))
479 {
c73f92be 480 serviceType = NETSRV_HTTPS;
ffabff3c 481 }
adb3378f
VK
482 else if (!stricmp(optarg, "pop3"))
483 {
c73f92be 484 serviceType = NETSRV_POP3;
adb3378f
VK
485 }
486 else if (!stricmp(optarg, "smtp"))
487 {
c73f92be 488 serviceType = NETSRV_SMTP;
adb3378f
VK
489 }
490 else if (!stricmp(optarg, "ssh"))
491 {
c73f92be 492 serviceType = NETSRV_SSH;
adb3378f
VK
493 }
494 else if (!stricmp(optarg, "telnet"))
495 {
c73f92be 496 serviceType = NETSRV_TELNET;
adb3378f
VK
497 }
498 else
499 {
06a93345 500 _tprintf(_T("Invalid service type \"%hs\"\n"), optarg);
c73f92be 501 start = FALSE;
adb3378f 502 }
5039dede
AK
503 }
504 break;
4687826e 505 case 'o': // Protocol number for service check
5039dede
AK
506 i = strtol(optarg, &eptr, 0);
507 if ((*eptr != 0) || (i < 0) || (i > 65535))
508 {
06a93345 509 _tprintf(_T("Invalid protocol number \"%hs\"\n"), optarg);
c73f92be 510 start = FALSE;
5039dede
AK
511 }
512 else
513 {
514 wServiceProto = (WORD)i;
515 }
516 break;
517 case 'v': // Print version and exit
08b214c6 518 _tprintf(_T("NetXMS GET command-line utility Version ") NETXMS_VERSION_STRING _T("\n"));
c73f92be 519 start = FALSE;
5039dede
AK
520 break;
521 case 'w': // Command timeout
522 i = strtol(optarg, &eptr, 0);
523 if ((*eptr != 0) || (i < 1) || (i > 120))
524 {
06a93345 525 _tprintf(_T("Invalid timeout \"%hs\"\n"), optarg);
c73f92be 526 start = FALSE;
5039dede
AK
527 }
528 else
529 {
967893bb 530 dwTimeout = (UINT32)i * 1000; // Convert to milliseconds
5039dede
AK
531 }
532 break;
7c521895
VK
533 case 'W': // Connection timeout
534 i = strtol(optarg, &eptr, 0);
535 if ((*eptr != 0) || (i < 1) || (i > 120))
536 {
537 printf("Invalid timeout \"%s\"\n", optarg);
c73f92be 538 start = FALSE;
7c521895
VK
539 }
540 else
541 {
967893bb 542 dwConnTimeout = (UINT32)i * 1000; // Convert to milliseconds
7c521895
VK
543 }
544 break;
5039dede
AK
545#ifdef _WITH_ENCRYPTION
546 case 'e':
547 iEncryptionPolicy = atoi(optarg);
548 if ((iEncryptionPolicy < 0) ||
549 (iEncryptionPolicy > 3))
550 {
551 printf("Invalid encryption policy %d\n", iEncryptionPolicy);
c73f92be 552 start = FALSE;
5039dede
AK
553 }
554 break;
555 case 'K':
08b214c6 556#ifdef UNICODE
c73f92be
VK
557 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, keyFile, MAX_PATH);
558 keyFile[MAX_PATH - 1] = 0;
08b214c6 559#else
c73f92be 560 nx_strncpy(keyFile, optarg, MAX_PATH);
08b214c6 561#endif
5039dede
AK
562 break;
563#else
564 case 'e':
565 case 'K':
566 printf("ERROR: This tool was compiled without encryption support\n");
c73f92be 567 start = FALSE;
5039dede
AK
568 break;
569#endif
570 case 'X': // Use proxy
571 strncpy(szProxy, optarg, MAX_OBJECT_NAME);
08b214c6 572 szProxy[MAX_OBJECT_NAME - 1] = 0;
c73f92be 573 useProxy = TRUE;
5039dede
AK
574 break;
575 case 'Z': // Shared secret for proxy agent
08b214c6
VK
576#ifdef UNICODE
577 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, szProxySecret, MAX_SECRET_LENGTH);
578 szProxySecret[MAX_SECRET_LENGTH - 1] = 0;
579#else
5039dede 580 nx_strncpy(szProxySecret, optarg, MAX_SECRET_LENGTH);
08b214c6 581#endif
5039dede
AK
582 break;
583 case '?':
c73f92be 584 start = FALSE;
5039dede
AK
585 break;
586 default:
587 break;
588 }
589 }
590
591 // Check parameter correctness
c73f92be 592 if (start)
5039dede 593 {
c73f92be 594 if (argc - optind < (((operation == CMD_CHECK_SERVICE) || (operation == CMD_GET_PARAMS) || (operation == CMD_GET_CONFIG)) ? 1 : 2))
5039dede
AK
595 {
596 printf("Required argument(s) missing.\nUse nxget -h to get complete command line syntax.\n");
c73f92be 597 start = FALSE;
5039dede 598 }
c73f92be 599 else if ((authMethod != AUTH_NONE) && (szSecret[0] == 0))
5039dede
AK
600 {
601 printf("Shared secret not specified or empty\n");
c73f92be 602 start = FALSE;
5039dede
AK
603 }
604
605 // Load server key if requested
606#ifdef _WITH_ENCRYPTION
c73f92be 607 if ((iEncryptionPolicy != ENCRYPTION_DISABLED) && start)
5039dede 608 {
2df047f4 609 if (InitCryptoLib(0xFFFF))
5039dede 610 {
c73f92be 611 pServerKey = LoadRSAKeys(keyFile);
5039dede
AK
612 if (pServerKey == NULL)
613 {
c73f92be 614 _tprintf(_T("Error loading RSA keys from \"%s\"\n"), keyFile);
5039dede 615 if (iEncryptionPolicy == ENCRYPTION_REQUIRED)
c73f92be 616 start = FALSE;
5039dede
AK
617 }
618 }
619 else
620 {
621 printf("Error initializing cryptografy module\n");
622 if (iEncryptionPolicy == ENCRYPTION_REQUIRED)
c73f92be 623 start = FALSE;
5039dede
AK
624 }
625 }
626#endif
627
628 // If everything is ok, start communications
c73f92be 629 if (start)
5039dede
AK
630 {
631 // Initialize WinSock
632#ifdef _WIN32
633 WSADATA wsaData;
634 WSAStartup(2, &wsaData);
635#endif
c11eee9b
VK
636 InetAddress addr = InetAddress::resolveHostName(argv[optind]);
637 InetAddress proxyAddr = useProxy ? InetAddress::resolveHostName(szProxy) : InetAddress();
638 if (!addr.isValid())
5039dede
AK
639 {
640 fprintf(stderr, "Invalid host name or address \"%s\"\n", argv[optind]);
641 }
c11eee9b 642 else if (useProxy && !proxyAddr.isValid())
5039dede
AK
643 {
644 fprintf(stderr, "Invalid host name or address \"%s\"\n", szProxy);
645 }
646 else
647 {
7dd6369a 648 AgentConnection *conn = new AgentConnection(addr, agentPort, authMethod, szSecret);
5039dede 649
7dd6369a
VK
650 conn->setConnectionTimeout(dwConnTimeout);
651 conn->setCommandTimeout(dwTimeout);
652 conn->setEncryptionPolicy(iEncryptionPolicy);
c73f92be 653 if (useProxy)
7dd6369a 654 conn->setProxy(proxyAddr, proxyPort, proxyAuth, szProxySecret);
e3ff8ad1 655 if (conn->connect(pServerKey, &dwError))
5039dede
AK
656 {
657 do
658 {
c73f92be 659 switch(operation)
5039dede
AK
660 {
661 case CMD_GET:
662 iPos = optind + 1;
663 do
664 {
08b214c6
VK
665#ifdef UNICODE
666 wcValue = WideStringFromMBString(argv[iPos++]);
7dd6369a 667 iExitCode = Get(conn, wcValue, showNames);
08b214c6
VK
668 free(wcValue);
669#else
7dd6369a 670 iExitCode = Get(conn, argv[iPos++], showNames);
08b214c6 671#endif
c73f92be 672 } while((iExitCode == 0) && (batchMode) && (iPos < argc));
5039dede
AK
673 break;
674 case CMD_LIST:
08b214c6
VK
675#ifdef UNICODE
676 wcValue = WideStringFromMBString(argv[optind + 1]);
7dd6369a 677 iExitCode = List(conn, wcValue);
08b214c6
VK
678 free(wcValue);
679#else
7dd6369a 680 iExitCode = List(conn, argv[optind + 1]);
4687826e
VK
681#endif
682 break;
683 case CMD_TABLE:
684#ifdef UNICODE
685 wcValue = WideStringFromMBString(argv[optind + 1]);
7dd6369a 686 iExitCode = GetTable(conn, wcValue);
4687826e
VK
687 free(wcValue);
688#else
7dd6369a 689 iExitCode = GetTable(conn, argv[optind + 1]);
08b214c6 690#endif
5039dede
AK
691 break;
692 case CMD_CHECK_SERVICE:
7dd6369a 693 iExitCode = CheckService(conn, serviceType, dwServiceAddr,
5039dede
AK
694 wServiceProto, wServicePort, szRequest, szResponse);
695 break;
696 case CMD_GET_PARAMS:
7dd6369a 697 iExitCode = ListParameters(conn);
5039dede
AK
698 break;
699 case CMD_GET_CONFIG:
7dd6369a 700 iExitCode = GetConfig(conn);
5039dede 701 break;
5944946e 702 case CMD_GET_SCREENSHOT:
7dd6369a 703 iExitCode = GetScreenshot(conn, (argc > optind + 2) ? argv[optind + 2] : "Console", argv[optind + 1]);
5944946e 704 break;
5039dede
AK
705 default:
706 break;
707 }
708 ThreadSleep(iInterval);
709 }
710 while(iInterval > 0);
5039dede
AK
711 }
712 else
713 {
08b214c6 714 _tprintf(_T("%d: %s\n"), dwError, AgentErrorCodeToText(dwError));
5039dede
AK
715 iExitCode = 2;
716 }
7dd6369a 717 conn->decRefCount();
5039dede
AK
718 }
719 }
720 }
721
d87ddcc2 722 MsgWaitQueue::shutdown();
5039dede
AK
723 return iExitCode;
724}