added quote character escape in function AgentGetParameterArg
authorVictor Kirhenshtein <victor@netxms.org>
Mon, 1 Aug 2016 13:59:27 +0000 (16:59 +0300)
committerVictor Kirhenshtein <victor@netxms.org>
Mon, 1 Aug 2016 13:59:27 +0000 (16:59 +0300)
include/nms_util.h
src/agent/libnxagent/tools.cpp
src/agent/subagents/ssh/main.cpp
src/libnetxms/tools.cpp
src/server/core/datacoll.cpp

index daae81d..30aad2d 100644 (file)
@@ -1997,6 +1997,7 @@ UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *nam
 bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
 
 String LIBNETXMS_EXPORTABLE EscapeStringForJSON(const TCHAR *s);
+String LIBNETXMS_EXPORTABLE EscapeStringForAgent(const TCHAR *s);
 
 #if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
 void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
index c71fca2..2ed4f79 100644 (file)
@@ -53,7 +53,7 @@ static bool AgentGetParameterArgInternal(const TCHAR *param, int index, TCHAR *a
                case _T('"'):
                   state = 1;     // String
                   break;
-               case _T('\''):        // String, type 2
+               case _T('\''):    // String, type 2
                   state = 2;
                   break;
                case _T(','):
@@ -69,7 +69,7 @@ static bool AgentGetParameterArgInternal(const TCHAR *param, int index, TCHAR *a
                   break;
                case 0:
                   state = -1;       // Finish processing
-                  if(!inBrackets) //No error flag if parameters were given without braces
+                  if (!inBrackets) //No error flag if parameters were given without braces
                      success = false;  // Set error flag
                   break;
                default:
@@ -81,7 +81,16 @@ static bool AgentGetParameterArgInternal(const TCHAR *param, int index, TCHAR *a
             switch(*ptr2)
             {
                case _T('"'):
-                  state = 0;     // Normal
+                  if (*(ptr2 + 1) != _T('"'))
+                  {
+                     state = 0;     // Normal
+                  }
+                  else
+                  {
+                     ptr2++;
+                     if ((currIndex == index) && (pos < maxSize - 1))
+                        arg[pos++] = *ptr2;
+                  }
                   break;
                case 0:
                   state = -1;       // Finish processing
@@ -96,7 +105,16 @@ static bool AgentGetParameterArgInternal(const TCHAR *param, int index, TCHAR *a
             switch(*ptr2)
             {
                case _T('\''):
-                  state = 0;     // Normal
+                  if (*(ptr2 + 1) != _T('\''))
+                  {
+                     state = 0;     // Normal
+                  }
+                  else
+                  {
+                     ptr2++;
+                     if ((currIndex == index) && (pos < maxSize - 1))
+                        arg[pos++] = *ptr2;
+                  }
                   break;
                case 0:
                   state = -1;       // Finish processing
index 6530214..fae2d1c 100644 (file)
@@ -69,7 +69,7 @@ static NETXMS_SUBAGENT_LIST m_lists[] =
 static NETXMS_SUBAGENT_INFO m_info =
 {
        NETXMS_SUBAGENT_INFO_MAGIC,
-       _T("PING"), NETXMS_VERSION_STRING,
+       _T("SSH"), NETXMS_VERSION_STRING,
        SubagentInit, SubagentShutdown, NULL,
        sizeof(m_parameters) / sizeof(NETXMS_SUBAGENT_PARAM),
        m_parameters,
index 441b623..4a027a9 100644 (file)
@@ -2751,3 +2751,20 @@ String LIBNETXMS_EXPORTABLE EscapeStringForJSON(const TCHAR *s)
    }
    return js;
 }
+
+/**
+ * Escape string for agent parameter
+ */
+String LIBNETXMS_EXPORTABLE EscapeStringForAgent(const TCHAR *s)
+{
+   String out;
+   if (s == NULL)
+      return out;
+   for(const TCHAR *p = s; *p != 0; p++)
+   {
+      if (*p == _T('"'))
+         out.append(_T('"'));
+      out.append(*p);
+   }
+   return out;
+}
index 154f322..8043e1d 100644 (file)
@@ -93,7 +93,7 @@ static void *GetItemData(DataCollectionTarget *dcTarget, DCItem *pItem, TCHAR *p
                           if (dcTarget->getObjectClass() == OBJECT_NODE)
                           {
                                   TCHAR name[MAX_PARAM_NAME];
-                                  _sntprintf(name, MAX_PARAM_NAME, _T("PDH.CounterValue(\"%s\",%d)"), pItem->getName(), pItem->getSampleCount());
+                                  _sntprintf(name, MAX_PARAM_NAME, _T("PDH.CounterValue(\"%s\",%d)"), (const TCHAR *)EscapeStringForAgent(pItem->getName()), pItem->getSampleCount());
                    *error = ((Node *)dcTarget)->getItemFromAgent(name, MAX_LINE_SIZE, pBuffer);
                           }
                           else