"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/server/tools/nxwsget/nxwsget.cpp" (23 Feb 2021, 8104 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "nxwsget.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.6.300_vs_3.7.95.

    1 /*
    2 ** nxwsget - command line tool used to retrieve web service parameters
    3 **           from NetXMS agent
    4 ** Copyright (C) 2004-2020 Raden Solutions
    5 **
    6 ** This program is free software; you can redistribute it and/or modify
    7 ** it under the terms of the GNU General Public License as published by
    8 ** the Free Software Foundation; either version 2 of the License, or
    9 ** (at your option) any later version.
   10 **
   11 ** This program is distributed in the hope that it will be useful,
   12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 ** GNU General Public License for more details.
   15 **
   16 ** You should have received a copy of the GNU General Public License
   17 ** along with this program; if not, write to the Free Software
   18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   19 **
   20 ** File: nxwsget.cpp
   21 **
   22 **/
   23 
   24 #include <nms_util.h>
   25 #include <nms_agent.h>
   26 #include <nxsrvapi.h>
   27 #include <netxms-version.h>
   28 
   29 NETXMS_EXECUTABLE_HEADER(nxwsget)
   30 
   31 #define MAX_LINE_SIZE      4096
   32 #define MAX_CRED_LEN       128
   33 
   34 /**
   35  * Static fields
   36  */
   37 static uint32_t s_interval = 0;
   38 static uint32_t s_retentionTime = 60;
   39 static TCHAR s_login[MAX_CRED_LEN] = _T("");
   40 static TCHAR s_password[MAX_CRED_LEN] = _T("");
   41 static WebServiceAuthType s_authType = WebServiceAuthType::NONE;
   42 static StringMap s_headers;
   43 static bool s_verifyCert = true;
   44 static bool s_verifyHost = true;
   45 static bool s_parseAsPlainText = false;
   46 static WebServiceRequestType s_requestType = WebServiceRequestType::PARAMETER;
   47 
   48 /**
   49  * Callback for printing results
   50  */
   51 static EnumerationCallbackResult PrintParameterResults(const TCHAR *key, const void *value, void *data)
   52 {
   53    WriteToTerminalEx(_T("%s = %s\n"), key, value);
   54    return _CONTINUE;
   55 }
   56 
   57 /**
   58  * Get service parameter
   59  */
   60 static int QueryWebService(AgentConnection *pConn, const TCHAR *url, const StringList& parameters)
   61 {
   62    uint32_t rcc = ERR_SUCCESS;
   63    switch (s_requestType)
   64    {
   65       case WebServiceRequestType::PARAMETER:
   66       {
   67          StringMap results;
   68          rcc = pConn->queryWebServiceParameters(url, (pConn->getCommandTimeout() - 500) / 1000, s_retentionTime,
   69                   (s_login[0] == 0) ? NULL: s_login, (s_password[0] == 0) ? NULL: s_password,
   70                   s_authType, s_headers, parameters, s_verifyCert, s_verifyHost, s_parseAsPlainText, &results);
   71          if (rcc == ERR_SUCCESS)
   72          {
   73             results.forEach(PrintParameterResults, NULL);
   74          }
   75          break;
   76       }
   77       case WebServiceRequestType::LIST:
   78       {
   79          StringList results;
   80          rcc = pConn->queryWebServiceList(url, (pConn->getCommandTimeout() - 500) / 1000, s_retentionTime,
   81                   (s_login[0] == 0) ? NULL: s_login, (s_password[0] == 0) ? NULL: s_password,
   82                   s_authType, s_headers, parameters.get(0), s_verifyCert, s_verifyHost, s_parseAsPlainText, &results);
   83          if (rcc == ERR_SUCCESS)
   84          {
   85             for (int i = 0; i < results.size(); i++)
   86                WriteToTerminalEx(_T("%s\n"), results.get(i));
   87          }
   88          break;
   89       }
   90    }
   91 
   92    if (rcc != ERR_SUCCESS)
   93    {
   94       WriteToTerminalEx(_T("%d: %s\n"), rcc, AgentErrorCodeToText(rcc));
   95    }
   96    fflush(stdout);
   97    return (rcc == ERR_SUCCESS) ? 0 : 1;
   98 }
   99 
  100 /**
  101  * Parse nxwsget specific parameters
  102  */
  103 static bool ParseAdditionalOptionCb(const char ch, const TCHAR *optarg)
  104 {
  105    int i;
  106    bool start = true;
  107    TCHAR *eptr;
  108    TCHAR header[1024];
  109    TCHAR *s;
  110 
  111    switch(ch)
  112    {
  113       case 'c':   // disable certificate check
  114          s_verifyCert = false;
  115          break;
  116       case 'C':   // disable certificate's host check
  117          s_verifyHost = false;
  118          break;
  119       case 'd':   // debug level
  120          s_parseAsPlainText = true;
  121          break;
  122       case 'i':   // Interval
  123          i = _tcstol(optarg, &eptr, 0);
  124          if ((*eptr != 0) || (i <= 0))
  125          {
  126             _tprintf(_T("Invalid interval \"%s\"\n"), optarg);
  127             start = false;
  128          }
  129          else
  130          {
  131             s_interval = i;
  132          }
  133          break;
  134       case 'H':   // Header
  135          _tcslcpy(header, optarg, 1024);
  136          s = _tcschr(header, _T(':'));
  137          if (s != nullptr)
  138          {
  139             *s++ = 0;
  140             while(_istspace(*s))
  141                s++;
  142             s_headers.set(header, s);
  143          }
  144          else
  145          {
  146             s_headers.set(header, _T(""));
  147          }
  148          break;
  149       case 'l':
  150          s_requestType = WebServiceRequestType::LIST;
  151          break;
  152       case 'L':   // Login
  153          _tcslcpy(s_login, optarg, MAX_CRED_LEN);
  154          break;
  155       case 'P':   // Password
  156          _tcslcpy(s_password, optarg, MAX_CRED_LEN);
  157          break;
  158       case 'r':   // Retention time
  159          s_retentionTime = _tcstol(optarg, &eptr, 0);
  160          if ((*eptr != 0) || (s_retentionTime < 1))
  161          {
  162             _tprintf(_T("Invalid retention time \"%s\"\n"), optarg);
  163             start = false;
  164          }
  165          break;
  166       case 't':
  167          if (!_tcscmp(optarg, _T("any")))
  168             s_authType = WebServiceAuthType::ANY;
  169          else if (!_tcscmp(optarg, _T("anysafe")))
  170             s_authType = WebServiceAuthType::ANYSAFE;
  171          else if (!_tcscmp(optarg, _T("basic")))
  172             s_authType = WebServiceAuthType::BASIC;
  173          else if (!_tcscmp(optarg, _T("bearer")))
  174             s_authType = WebServiceAuthType::BEARER;
  175          else if (!_tcscmp(optarg, _T("digest")))
  176             s_authType = WebServiceAuthType::DIGEST;
  177          else if (!_tcscmp(optarg, _T("none")))
  178             s_authType = WebServiceAuthType::NONE;
  179          else if (!_tcscmp(optarg, _T("ntlm")))
  180             s_authType = WebServiceAuthType::NTLM;
  181          else
  182          {
  183             _tprintf(_T("Invalid authentication method \"%s\"\n"), optarg);
  184             start = false;
  185          }
  186          break;
  187       default:
  188          break;
  189    }
  190    return start;
  191 }
  192 
  193 /**
  194  * Validate parameter count
  195  */
  196 static bool IsArgMissingCb(int currentCount)
  197 {
  198    return currentCount < 3;
  199 }
  200 
  201 /**
  202  * Execute command callback
  203  */
  204 static int ExecuteCommandCb(AgentConnection *conn, int argc, TCHAR **argv, int optind, RSA *serverKey)
  205 {
  206    int exitCode = 3, pos;
  207 
  208    do
  209    {
  210       TCHAR *url;
  211       StringList parameters;
  212       pos = optind + 1;
  213       url = MemCopyString(argv[pos++]);
  214       while (pos < argc)
  215          parameters.add(argv[pos++]);
  216       exitCode = QueryWebService(conn, url, parameters);
  217       ThreadSleep(s_interval);
  218       MemFree(url);
  219    }
  220    while(s_interval > 0);
  221    return exitCode;
  222 }
  223 
  224 /**
  225  * Startup
  226  */
  227 #ifdef _WIN32
  228 int _tmain(int argc, TCHAR *argv[])
  229 #else
  230 int main(int argc, char *argv[])
  231 #endif
  232 {
  233    ServerCommandLineTool tool;
  234    tool.argc = argc;
  235    tool.argv = argv;
  236    tool.mainHelpText = _T("Usage: nxwsget [<options>] <host> <URL> <path> [<path> ...]\n\n")
  237                        _T("Tool specific options:\n")
  238                        _T("   -c           : Do not verify service certificate.\n")
  239                        _T("   -C           : Do not verify certificate's name against host.\n")
  240                        _T("   -d           : Use text parsing.\n")
  241                        _T("   -H header    : HTTP header (can be used multiple times).\n")
  242                        _T("   -i seconds   : Query service continuously with given interval.\n")
  243                        _T("   -l           : Requested parameter is a list.\n")
  244                        _T("   -L login     : Web service login name.\n")
  245                        _T("   -P passwod   : Web service password.\n")
  246                        _T("   -r seconds   : Cache retention time.\n")
  247                        _T("   -t auth      : HTTP authentication type. Valid methods are \"none\", \"basic\", \"digest\",\n")
  248                        _T("                  \"ntlm\", \"bearer\", \"any\", or \"anysafe\". Default is \"none\".\n");
  249    tool.additionalOptions = "cCdH:i:lL:P:r:t:";
  250    tool.executeCommandCb = &ExecuteCommandCb;
  251    tool.parseAdditionalOptionCb = &ParseAdditionalOptionCb;
  252    tool.isArgMissingCb = &IsArgMissingCb;
  253 
  254    return ExecuteServerCommandLineTool(&tool);
  255 }