Changeset 978bd31 in nscp


Ignore:
Timestamp:
11/28/07 20:33:21 (5 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
013a71f
Parents:
3f569d3
Message:

2007-11-28 MickeM

  • Fixed some UNICODE issues with process-listings + Added an error message if the "detected" process enumeration method is not available. + Fixed some more unicode issues Password encrypt/decrypt: #107
  • Fixed unicode issues with "external programs" #109
  • Fixed so default string for check_nt:s FILEAGE command is "delta" is 5 minutes ago (and not absolute ie. 1970...), Issue #39 + added support for <date strings> to check_nt:s FILEAGE command, Issue #39

append: .<date string> if you want to use a "custom date" like so: ... -v FILEAGE -l c:
windows,Date: %Y-%m-%d %H:%M:%S" -w 5 -c 6 ...
Only the above listed %<char> works, and default to 0 so might not be to pretty but works...

Files:
19 edited

Legend:

Unmodified
Added
Removed
  • NSClient++.cpp

    r7e33d82 r978bd31  
    7676      std::wstring xor_pwd = Encrypt(password); 
    7777      std::wcout << _T("obfuscated_password=") << xor_pwd << std::endl; 
    78       if (password != Decrypt(xor_pwd))  
    79         std::wcout << _T("ERROR: Password did not match!") << std::endl; 
     78      std::wstring outPasswd = Decrypt(xor_pwd); 
     79      if (password != outPasswd)  
     80        std::wcout << _T("ERROR: Password did not match: ") << outPasswd<< std::endl; 
    8081      Settings::destroyInstance(); 
    8182      return 0; 
     
    508509    ReadLock readLock(&m_mutexRW, true, 5000); 
    509510    if (!readLock.IsLocked()) { 
    510       std::cout << _T("Message was lost as the core was locked...") << std::endl; 
     511      std::wcout << _T("Message was lost as the core was locked...") << std::endl; 
    511512      return; 
    512513    } 
     
    652653    return NSCAPI::hasFailed; 
    653654  } 
    654   std::wstring s = inBuffer; 
    655655  std::wstring key = Settings::getInstance()->getString(MAIN_SECTION_TITLE, MAIN_MASTERKEY, MAIN_MASTERKEY_DEFAULT); 
    656   TCHAR *c = new TCHAR[inBufLen+1]; 
    657   wcsncpy_s(c, inBufLen+1, inBuffer, inBufLen); 
    658   for (unsigned int i=0,j=0;i<inBufLen;i++,j++) { 
     656  int tcharInBufLen = 0; 
     657  char *c = charEx::tchar_to_char(inBuffer, inBufLen, tcharInBufLen); 
     658  std::wstring::size_type j=0; 
     659  for (int i=0;i<tcharInBufLen;i++,j++) { 
    659660    if (j > key.size()) 
    660661      j = 0; 
    661662    c[i] ^= key[j]; 
    662663  } 
    663   LOG_ERROR(_T("DISABLED FOR NOW!!!! REPORT THIS!!!")); 
    664   size_t len = 0; //b64::b64_encode(reinterpret_cast<void*>(c), inBufLen, outBuf, *outBufLen); 
     664  size_t cOutBufLen = b64::b64_encode(reinterpret_cast<void*>(c), tcharInBufLen, NULL, NULL); 
     665  if (!outBuf) { 
     666    *outBufLen = static_cast<unsigned int>(cOutBufLen*2); // TODO: Guessing wildly here but no proper way to tell without a lot of extra work 
     667    return NSCAPI::isSuccess; 
     668  } 
     669  char *cOutBuf = new char[cOutBufLen+1]; 
     670  size_t len = b64::b64_encode(reinterpret_cast<void*>(c), tcharInBufLen, cOutBuf, cOutBufLen); 
    665671  delete [] c; 
    666   if (outBuf) { 
    667     if ((len == 0)||(len >= *outBufLen)) { 
    668       LOG_ERROR(_T("Invalid out buffer length.")); 
    669       return NSCAPI::isInvalidBufferLen; 
    670     } 
    671     outBuf[len] = 0; 
    672     *outBufLen = static_cast<unsigned int>(len); 
    673   } else { 
    674     *outBufLen = static_cast<unsigned int>(len); 
    675   } 
     672  if (len == 0) { 
     673    LOG_ERROR(_T("Invalid out buffer length.")); 
     674    return NSCAPI::isInvalidBufferLen; 
     675  } 
     676  int realOutLen; 
     677  TCHAR *realOut = charEx::char_to_tchar(cOutBuf, cOutBufLen, realOutLen); 
     678  if (static_cast<unsigned int>(realOutLen) >= *outBufLen) { 
     679    LOG_ERROR_STD(_T("Invalid out buffer length: ") + strEx::itos(realOutLen) + _T(" was needed but only ") + strEx::itos(*outBufLen) + _T(" was allocated.")); 
     680    return NSCAPI::isInvalidBufferLen; 
     681  } 
     682  wcsncpy_s(outBuf, *outBufLen, realOut, realOutLen); 
     683  delete [] realOut; 
     684  outBuf[realOutLen] = 0; 
     685  *outBufLen = static_cast<unsigned int>(realOutLen); 
    676686  return NSCAPI::isSuccess; 
    677687} 
     
    682692    return NSCAPI::hasFailed; 
    683693  } 
    684   int inBufLenC = WideCharToMultiByte(CP_ACP, 0, inBuffer, inBufLen, NULL, 0, NULL, NULL);  
    685   if (inBufLenC == 0) { 
    686     LOG_ERROR_STD(_T("Could not convert string: ") + error::lookup::last_error()); 
    687     return NSCAPI::hasFailed; 
    688   } 
    689   char *inBufferC = new char[inBufLenC+1]; 
    690   inBufLenC = WideCharToMultiByte(CP_ACP, 0, inBuffer, inBufLen, inBufferC, inBufLenC, NULL, NULL);  
    691   if (inBufLenC == 0) { 
    692     LOG_ERROR_STD(_T("Could not convert string: ") + error::lookup::last_error()); 
    693     delete [] inBufferC; 
    694     return NSCAPI::hasFailed; 
    695   } 
    696   char *outBufferC = new char[*outBufLen]; 
    697   size_t len =  b64::b64_decode(inBufferC, inBufLenC, reinterpret_cast<void*>(outBufferC), *outBufLen); 
    698   if (outBufferC) { 
    699     if (len == 0) { 
    700       delete [] inBufferC; 
    701       LOG_ERROR_STD(_T("Invalid out buffer length.")); 
    702       return NSCAPI::isInvalidBufferLen; 
    703     } 
    704     std::string key = strEx::wstring_to_string(Settings::getInstance()->getString(MAIN_SECTION_TITLE, MAIN_MASTERKEY, MAIN_MASTERKEY_DEFAULT)); 
    705     for (unsigned int i=0,j=0;i<len;i++,j++) { 
    706       if (j > key.size()) 
    707         j = 0; 
    708       outBufferC[i] ^= key[j]; 
    709     } 
    710     outBufferC[len] = 0; 
    711  
    712     int neededLen = MultiByteToWideChar(CP_ACP, 0, outBufferC, static_cast<int>(len), NULL, 0 ); 
    713     if (neededLen == 0 || neededLen < 0) { 
    714       LOG_ERROR_STD(_T("Could not convert string: ") + error::lookup::last_error()); 
    715       delete [] inBufferC; 
    716       return NSCAPI::hasFailed; 
    717     } 
    718     if (static_cast<unsigned int>(neededLen) > *outBufLen) { 
    719       LOG_ERROR_STD(_T("Invalid out buffer length.")); 
    720       return NSCAPI::isInvalidBufferLen; 
    721     } 
    722     *outBufLen = static_cast<unsigned int>(MultiByteToWideChar(CP_ACP, 0, outBufferC, static_cast<int>(len), outBuf, neededLen )); 
    723     delete [] inBufferC; 
    724     if (*outBufLen == 0) { 
    725       LOG_ERROR_STD(_T("Could not convert string: ") + error::lookup::last_error()); 
    726       return NSCAPI::hasFailed; 
    727     } 
    728   } else { 
    729     *outBufLen = static_cast<unsigned int>(len); 
    730   } 
     694  int inBufLenC = 0; 
     695  char *inBufferC = charEx::tchar_to_char(inBuffer, inBufLen, inBufLenC); 
     696  size_t cOutLen =  b64::b64_decode(inBufferC, inBufLenC, NULL, NULL); 
     697  if (!outBuf) { 
     698    *outBufLen = static_cast<unsigned int>(cOutLen*2); // TODO: Guessing wildly here but no proper way to tell without a lot of extra work 
     699    return NSCAPI::isSuccess; 
     700  } 
     701  char *cOutBuf = new char[cOutLen+1]; 
     702  size_t len = b64::b64_decode(inBufferC, inBufLenC, reinterpret_cast<void*>(cOutBuf), cOutLen); 
     703  delete [] inBufferC; 
     704  if (len == 0) { 
     705    LOG_ERROR(_T("Invalid out buffer length.")); 
     706    return NSCAPI::isInvalidBufferLen; 
     707  } 
     708  int realOutLen; 
     709 
     710  std::wstring key = Settings::getInstance()->getString(MAIN_SECTION_TITLE, MAIN_MASTERKEY, MAIN_MASTERKEY_DEFAULT); 
     711  std::wstring::size_type j=0; 
     712  for (int i=0;i<cOutLen;i++,j++) { 
     713    if (j > key.size()) 
     714      j = 0; 
     715    cOutBuf[i] ^= key[j]; 
     716  } 
     717 
     718  TCHAR *realOut = charEx::char_to_tchar(cOutBuf, cOutLen, realOutLen); 
     719  if (static_cast<unsigned int>(realOutLen) >= *outBufLen) { 
     720    LOG_ERROR_STD(_T("Invalid out buffer length: ") + strEx::itos(realOutLen) + _T(" was needed but only ") + strEx::itos(*outBufLen) + _T(" was allocated.")); 
     721    return NSCAPI::isInvalidBufferLen; 
     722  } 
     723  wcsncpy_s(outBuf, *outBufLen, realOut, realOutLen); 
     724  delete [] realOut; 
     725  outBuf[realOutLen] = 0; 
     726  *outBufLen = static_cast<unsigned int>(realOutLen); 
    731727  return NSCAPI::isSuccess; 
    732728} 
  • changelog

    r3f569d3 r978bd31  
    55 * Add module for relaying events 
    66 * Add API for rehashing the daemon (or implement it the API is there but does nothing) 
     7 
     82007-11-28 MickeM 
     9 * Fixed some UNICODE issues with process-listings 
     10 + Added an error message if the "detected" process enumeration method is not available. 
     11 + Fixed some more unicode issues Password encrypt/decrypt: #107 
     12 * Fixed unicode issues with "external programs" #109 
     13 * Fixed so default string for check_nt:s FILEAGE command is "delta" is 5 minutes ago (and not absolute ie. 1970...), Issue #39 
     14 + added support for  <date strings> to check_nt:s FILEAGE command, Issue #39 
     15    append: .<date string> if you want to use a "custom date" like so: ... -v FILEAGE -l c:\\windows,Date: %Y-%m-%d %H:%M:%S" -w 5 -c 6 ... 
     16    Only the above listed %<char> works, and default to 0 so might not be to pretty but works... 
    717 
    8182007-11-26 MickeM 
  • include/EnumNtSrv.cpp

    r99e4d8f r978bd31  
    191191    DWORD bufLen = SC_BUF_LEN; 
    192192    TCHAR *buf = new TCHAR[bufLen+1]; 
    193     //std::cout << "name: '" << name << "'" << std::endl; 
    194193    if (GetServiceKeyName(scman, name.c_str(), buf, &bufLen) == 0) { 
    195194      ::CloseServiceHandle(scman); 
  • include/EnumProcess.cpp

    r99e4d8f r978bd31  
    2222#include <windows.h> 
    2323#include <tchar.h> 
     24#include <iostream> 
    2425 
    2526#include "EnumProcess.h" 
     
    3031 
    3132 
    32 CEnumProcess::CEnumProcess() : m_pProcesses(NULL), m_pModules(NULL), m_pCurrentP(NULL), m_pCurrentM(NULL), lpString(NULL) 
     33CEnumProcess::CEnumProcess() : m_pProcesses(NULL), m_pModules(NULL), m_pCurrentP(NULL), m_pCurrentM(NULL), lpString(NULL), PSAPI(NULL) 
    3334{ 
    3435  lpString = new TCHAR[MAX_FILENAME+1]; 
     
    6162    m_me.dwSize = sizeof(m_me); 
    6263    // Find ToolHelp functions 
    63  
     64#ifdef UNICODE 
    6465    FCreateToolhelp32Snapshot = (PFCreateToolhelp32Snapshot)::GetProcAddress(TOOLHELP, "CreateToolhelp32Snapshot"); 
    65     FProcess32First = (PFProcess32First)::GetProcAddress(TOOLHELP, "Process32First"); 
    66     FProcess32Next = (PFProcess32Next)::GetProcAddress(TOOLHELP, "Process32Next"); 
    67     FModule32First = (PFModule32First)::GetProcAddress(TOOLHELP, "Module32First"); 
    68     FModule32Next = (PFModule32Next)::GetProcAddress(TOOLHELP, "Module32Next"); 
     66    FProcess32First = (PFProcess32First)::GetProcAddress(TOOLHELP, "Process32FirstW"); 
     67    FProcess32Next = (PFProcess32Next)::GetProcAddress(TOOLHELP, "Process32NextW"); 
     68    FModule32First = (PFModule32First)::GetProcAddress(TOOLHELP, "Module32FirstW"); 
     69    FModule32Next = (PFModule32Next)::GetProcAddress(TOOLHELP, "Module32NextW"); 
     70#else 
     71    FCreateToolhelp32Snapshot = (PFCreateToolhelp32Snapshot)::GetProcAddress(TOOLHELP, "CreateToolhelp32SnapshotA"); 
     72    FProcess32First = (PFProcess32First)::GetProcAddress(TOOLHELP, "Process32FirstA"); 
     73    FProcess32Next = (PFProcess32Next)::GetProcAddress(TOOLHELP, "Process32NextA"); 
     74    FModule32First = (PFModule32First)::GetProcAddress(TOOLHELP, "Module32FirstA"); 
     75    FModule32Next = (PFModule32Next)::GetProcAddress(TOOLHELP, "Module32NextA"); 
     76#endif 
    6977  } 
    7078 
     
    124132{ 
    125133  if (ENUM_METHOD::NONE == m_method) return FALSE;  
    126  
    127134 
    128135  if ((ENUM_METHOD::TOOLHELP|m_method) == m_method) 
     
    282289        pEntry->sFilename = _T("N/A (error)"); 
    283290      } else { 
    284         pEntry->sFilename = lpString; 
     291        std::wstring path = lpString; 
     292        std::wstring::size_type pos = path.find_last_of(_T("\\")); 
     293        if (pos != std::wstring::npos) { 
     294          path = path.substr(++pos); 
     295        } 
     296        pEntry->sFilename = path; 
    285297      } 
    286298    } 
  • include/EnumProcess.h

    r99e4d8f r978bd31  
    3636const int MAX_FILENAME = 256; 
    3737 
     38#ifdef UNICODE 
     39// Functions loaded from PSAPI 
     40typedef BOOL (WINAPI *PFEnumProcesses)(DWORD * lpidProcess, DWORD cb, DWORD * cbNeeded); 
     41typedef BOOL (WINAPI *PFEnumProcessModules)(HANDLE hProcess, HMODULE * lphModule, DWORD cb, LPDWORD lpcbNeeded); 
     42typedef DWORD (WINAPI *PFGetModuleFileNameEx)(HANDLE hProcess, HMODULE hModule, LPTSTR lpFilename, DWORD nSize); 
     43 
     44//Functions loaded from Kernel32 
     45typedef HANDLE (WINAPI *PFCreateToolhelp32Snapshot)(DWORD dwFlags, DWORD th32ProcessID); 
     46typedef BOOL (WINAPI *PFProcess32First)(HANDLE hSnapshot, LPPROCESSENTRY32W lppe); 
     47typedef BOOL (WINAPI *PFProcess32Next)(HANDLE hSnapshot, LPPROCESSENTRY32W lppe); 
     48typedef BOOL (WINAPI *PFModule32First)(HANDLE hSnapshot, LPMODULEENTRY32W lpme); 
     49typedef BOOL (WINAPI *PFModule32Next)(HANDLE hSnapshot, LPMODULEENTRY32W lpme); 
     50#else 
    3851// Functions loaded from PSAPI 
    3952typedef BOOL (WINAPI *PFEnumProcesses)(DWORD * lpidProcess, DWORD cb, DWORD * cbNeeded); 
     
    4760typedef BOOL (WINAPI *PFModule32First)(HANDLE hSnapshot, LPMODULEENTRY32 lpme); 
    4861typedef BOOL (WINAPI *PFModule32Next)(HANDLE hSnapshot, LPMODULEENTRY32 lpme); 
    49  
     62#endif 
    5063 
    5164class CEnumProcess   
     
    111124  HANDLE m_hProcessSnap, m_hModuleSnap; 
    112125  HMODULE TOOLHELP;   //Handle to the module (Kernel32) 
     126#ifdef UNICODE 
     127  PROCESSENTRY32W m_pe; 
     128  MODULEENTRY32W  m_me; 
     129#else 
    113130  PROCESSENTRY32 m_pe; 
    114131  MODULEENTRY32  m_me; 
     132#endif 
    115133  // ToolHelp related functions 
    116134  PFCreateToolhelp32Snapshot FCreateToolhelp32Snapshot; 
  • include/Mutex.h

    r478588b r978bd31  
    124124  MutexLock(HANDLE hMutex, DWORD timeout = 5000L) : bHasMutex(false), hMutex_(hMutex) { 
    125125    if (hMutex_ == NULL) 
    126       std::cout << "Whops..." << std::endl; 
     126      std::wcout << "Whops..." << std::endl; 
    127127    assert(hMutex_ != NULL); 
    128128    dwWaitResult = WaitForSingleObject(hMutex_, timeout); 
  • include/MutexRW.h

    rdc65e35 r978bd31  
    5858        NULL  
    5959        ); 
    60       std::cout << "***** ERROR: CreateSemaphore: %s\n" << (LPCTSTR)lpMsgBuf; 
     60      std::wcout << "***** ERROR: CreateSemaphore: %s\n" << (LPCTSTR)lpMsgBuf; 
    6161      //        TRACE( "***** ERROR: CreateSemaphore: %s\n", (LPCTSTR)lpMsgBuf ); 
    6262      LocalFree( lpMsgBuf );       
  • include/NTService.h

    r99e4d8f r978bd31  
    7272    BOOL ret = ::StartServiceCtrlDispatcher(dispatchTable); 
    7373    if (ret == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) { 
    74       std::cout << "We are running in console mode, terminating..." << std::endl; 
     74      std::wcout << "We are running in console mode, terminating..." << std::endl; 
    7575      return false; 
    7676    } 
  • include/PDHCounter.h

    r99e4d8f r978bd31  
    206206 
    207207    PDHCounter* addCounter(std::wstring name, PDHCounterListener *listener) { 
    208       //std::cout << "Adding counter: " << name << std::endl; 
    209208      PDHCounter *counter = new PDHCounter(name, listener); 
    210209      counters_.push_back(counter); 
  • include/REGSettings.h

    r99e4d8f r978bd31  
    119119          ret = reinterpret_cast<LPCTSTR>(bData); 
    120120        } else { 
    121           std::cout << _T("getString_::Buffersize to small: ") << lpszPath << "." << lpszKey << ": " << type << std::endl; 
     121          std::wcout << _T("getString_::Buffersize to small: ") << lpszPath << "." << lpszKey << ": " << type << std::endl; 
    122122        } 
    123123      } else if (type == REG_DWORD) { 
     
    125125        ret = strEx::itos(dw); 
    126126      } else { 
    127         std::cout << _T("getString_::Unsupported type: ") << lpszPath << "." << lpszKey << ": " << type << std::endl; 
     127        std::wcout << _T("getString_::Unsupported type: ") << lpszPath << "." << lpszKey << ": " << type << std::endl; 
    128128      } 
    129129    } else if (lRet == ERROR_FILE_NOT_FOUND) { 
     
    177177          ret.push_back(std::wstring(lpValueName)); 
    178178        } else { 
    179           std::cout << _T("getValues_::Error: ") << bRet << ": " << lpszPath << _T("[") << i << _T("]") << std::endl; 
     179          std::wcout << _T("getValues_::Error: ") << bRet << ": " << lpszPath << _T("[") << i << _T("]") << std::endl; 
    180180 
    181181        } 
     
    205205          ret.push_back(std::wstring(lpValueName)); 
    206206        } else { 
    207           std::cout << _T("getSubKeys_::Error: ") << bRet << _T(": ") << lpszPath << _T("[") << i << _T("]") << std::endl; 
     207          std::wcout << _T("getSubKeys_::Error: ") << bRet << _T(": ") << lpszPath << _T("[") << i << _T("]") << std::endl; 
    208208        } 
    209209      } 
  • include/Settings.h

    r99e4d8f r978bd31  
    133133 
    134134/* 
    135         std::cout << "  Key: " << (*it2) << std::endl; 
     135        std::wcout << "  Key: " << (*it2) << std::endl; 
    136136        int i = sM->getInt((*it), (*it2), UNLIKELY_VALUE_1); 
    137         std::cout << "Int vaöl: " << i << std::endl; 
     137        std::wcout << "Int vaöl: " << i << std::endl; 
    138138        if (i == UNLIKELY_VALUE_1) { 
    139139          if (sM->getInt((*it), (*it2), UNLIKELY_VALUE_2)==UNLIKELY_VALUE_2) { 
    140             std::cout << "Writing: " << (*it) << " - " << (*it2) << " - " << sM->getString((*it), (*it2)) << std::endl; 
     140            std::wcout << "Writing: " << (*it) << " - " << (*it2) << " - " << sM->getString((*it), (*it2)) << std::endl; 
    141141            setString((*it), (*it2), sM->getString((*it), (*it2))); 
    142142          } else 
     
    144144        } else if (i == 0) { 
    145145          std::wstring s = sM->getString((*it), (*it2)); 
    146           std::cout << "Size: " << s.size() << " |" << s << "| " << std::endl; 
     146          std::wcout << "Size: " << s.size() << " |" << s << "| " << std::endl; 
    147147          if (s.size() == 0) 
    148148            setString((*it), (*it2), s); 
  • include/charEx.h

    rdc65e35 r978bd31  
    2121#pragma once 
    2222#include <assert.h> 
     23#include <windows.h> 
     24#include <tchar.h> 
    2325 
    2426namespace charEx { 
     
    2931   * @return a list with strings 
    3032   */ 
    31   inline std::list<std::string> split(const char* buffer, char split) { 
    32     std::list<std::string> ret; 
    33     const char *start = buffer; 
    34     for (const char *p = buffer;*p!='\0';p++) { 
     33  inline std::list<std::wstring> split(const TCHAR* buffer, TCHAR split) { 
     34    std::list<std::wstring> ret; 
     35    const TCHAR *start = buffer; 
     36    for (const TCHAR *p = buffer;*p!='\0';p++) { 
    3537      if (*p==split) { 
    36         std::string str(start, p-start); 
     38        std::wstring str(start, p-start); 
    3739        ret.push_back(str); 
    3840        start = p+1; 
    3941      } 
    4042    } 
    41     ret.push_back(std::string(start)); 
     43    ret.push_back(std::wstring(start)); 
     44    return ret; 
     45  } 
     46 
     47 
     48  inline char* tchar_to_char( const wchar_t* pStr, int len, int &nChars) { 
     49    assert(pStr != NULL); 
     50    assert(len >= 0 || len == -1); 
     51 
     52    // figure out how many narrow characters we are going to get  
     53    nChars = WideCharToMultiByte(CP_ACP, 0, pStr, len, NULL, 0, NULL, NULL); 
     54    if (len == -1) 
     55      --nChars; 
     56    if (nChars==0) { 
     57      char *ret = new char[1]; 
     58      ret[0] = 0; 
     59      return ret; 
     60    } 
     61 
     62    // convert the wide string to a narrow string 
     63    char *ret = new char[nChars+1]; 
     64    WideCharToMultiByte(CP_ACP, 0, pStr, len, ret, nChars, NULL, NULL); 
     65    return ret; 
     66  } 
     67 
     68  inline wchar_t* char_to_tchar(const char* pStr, int len, int &nChars) { 
     69    assert( pStr != NULL); 
     70    assert( len >= 0 || len == -1); 
     71 
     72    // figure out how many wide characters we are going to get 
     73    nChars = MultiByteToWideChar( CP_ACP , 0 , pStr , len , NULL , 0 ); 
     74    if (len == -1) 
     75      --nChars; 
     76    if (nChars == 0) { 
     77      TCHAR *ret = new TCHAR[1]; 
     78      ret[0] = 0; 
     79      return ret; 
     80    } 
     81 
     82    // convert the narrow string to a wide string  
     83    TCHAR *ret = new TCHAR[nChars+1]; 
     84    MultiByteToWideChar(CP_ACP, 0 ,pStr ,len, const_cast<wchar_t*>(ret), nChars); 
    4285    return ret; 
    4386  } 
     
    4588 
    4689 
    47   typedef std::pair<std::string,char*> token; 
    48   inline token getToken(char *buffer, char split) { 
     90  typedef std::pair<std::wstring,TCHAR*> token; 
     91  inline token getToken(TCHAR *buffer, TCHAR split) { 
    4992    assert(buffer != NULL); 
    50     char *p = strchr(buffer, split); 
     93    TCHAR *p = wcschr(buffer, split); 
    5194    if (!p) 
    5295      return token(buffer, NULL); 
    5396    if (!p[1]) 
    54       return token(std::string(buffer, p-buffer), NULL); 
     97      return token(std::wstring(buffer, p-buffer), NULL); 
    5598    p++; 
    56     return token(std::string(buffer, p-buffer-1), p); 
     99    return token(std::wstring(buffer, p-buffer-1), p); 
    57100  } 
    58101#ifdef _DEBUG 
    59   inline void test_getToken(char* in1, char in2, std::string out1, char * out2) { 
     102  inline void test_getToken(TCHAR* in1, TCHAR in2, std::wstring out1, TCHAR * out2) { 
    60103    token t = getToken(in1, in2); 
    61     std::cout << "charEx::test_getToken(" << in1 << ", " << in2 << ") : "; 
     104    std::wcout << _T("charEx::test_getToken(") << in1 << _T(", ") << in2 << _T(") : "); 
    62105    if (t.first == out1)  { 
    63106      if ((t.second == NULL) && (out2 == NULL)) 
    64         std::cout << "Succeeded" << std::endl; 
     107        std::wcout << _T("Succeeded") << std::endl; 
    65108      else if (t.second == NULL) 
    66         std::cout << "Failed [NULL=" << out2 << "]" << std::endl; 
     109        std::wcout << _T("Failed [NULL=") << out2 << _T("]") << std::endl; 
    67110      else if (out2 == NULL) 
    68         std::cout << "Failed [" << t.second << "=NULL]" << std::endl; 
    69       else if (strcmp(t.second, out2) == 0) 
    70         std::cout << "Succeeded" << std::endl; 
     111        std::wcout << _T("Failed [") << t.second << _T("=NULL]") << std::endl; 
     112      else if (wcscmp(t.second, out2) == 0) 
     113        std::wcout << _T("Succeeded") << std::endl; 
    71114      else 
    72         std::cout << "Failed" << std::endl; 
     115        std::wcout << _T("Failed") << std::endl; 
    73116    } else 
    74       std::cout << "Failed [" << out1 << "=" << t.first << "]" << std::endl; 
     117      std::wcout << _T("Failed [") << out1 << _T("=") << t.first << _T("]") << std::endl; 
    75118  } 
    76119  inline void run_test_getToken() { 
    77     test_getToken("", '&', "", NULL); 
    78     test_getToken("&", '&', "", NULL); 
    79     test_getToken("&&", '&', "", "&"); 
    80     test_getToken("foo", '&', "foo", NULL); 
    81     test_getToken("foo&", '&', "foo", NULL); 
    82     test_getToken("foo&bar", '&', "foo", "bar"); 
    83     test_getToken("foo&bar&test", '&', "foo", "bar&test"); 
     120    test_getToken(_T(""), '&', _T(""), NULL); 
     121    test_getToken(_T("&"), '&', _T(""), NULL); 
     122    test_getToken(_T("&&"), '&', _T(""), _T("&")); 
     123    test_getToken(_T("foo"), '&', _T("foo"), NULL); 
     124    test_getToken(_T("foo&"), '&', _T("foo"), NULL); 
     125    test_getToken(_T("foo&bar"), '&', _T("foo"), _T("bar")); 
     126    test_getToken(_T("foo&bar&test"), '&', _T("foo"), _T("bar&test")); 
    84127  } 
    85128#endif 
  • include/strEx.h

    r7e33d82 r978bd31  
    208208  } 
    209209 
     210#define MK_FORMAT_FTD(min, key, val) \ 
     211  if (mtm->tm_year > min) \ 
     212    strEx::replace(format, key, strEx::itos(val));  \ 
     213  else  \ 
     214    strEx::replace(format, key, _T("0")); 
     215 
     216  inline std::wstring format_time_delta(std::wstring format, struct tm *mtm) { 
     217    // "Date: %Y-%m-%d %H:%M:%S" 
     218    MK_FORMAT_FTD(70, _T("%Y"), mtm->tm_year); 
     219    MK_FORMAT_FTD(0, _T("%m"), mtm->tm_mon); 
     220    MK_FORMAT_FTD(0, _T("%d"), mtm->tm_mday); 
     221    MK_FORMAT_FTD(0, _T("%H"), mtm->tm_hour); 
     222    MK_FORMAT_FTD(0, _T("%M"), mtm->tm_min); 
     223    MK_FORMAT_FTD(0, _T("%S"), mtm->tm_sec); 
     224    MK_FORMAT_FTD(0, _T("%Y"), mtm->tm_year); 
     225    return format; 
     226  } 
     227 
    210228#define WEEK  (7 * 24 * 60 * 60 * 1000) 
    211229#define DAY   (24 * 60 * 60 * 1000) 
  • modules/CheckDisk/CheckDisk.cpp

    r99e4d8f r978bd31  
    472472    return NSCAPI::returnUNKNOWN; 
    473473  } 
    474   std::wstring dstr = _T("%#c"); 
     474  std::wstring format = _T("%Y years %m mon %d days %H hours %M min %S sec"); 
    475475  std::wstring path; 
    476476  find_first_file_info finder; 
    477477  MAP_OPTIONS_BEGIN(stl_args) 
    478478    MAP_OPTIONS_STR(_T("path"), path) 
    479     MAP_OPTIONS_FALLBACK(dstr) 
     479    MAP_OPTIONS_STR(_T("date"), format) 
     480    MAP_OPTIONS_FALLBACK(format) 
    480481  MAP_OPTIONS_END() 
    481482 
     
    494495  unsigned long long now = ((now_.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)now_.dwLowDateTime); 
    495496  time_t value = (now-finder.info.ullLastWriteTime)/10000000; 
    496  
    497   TCHAR buf[51]; 
    498   size_t l = wcsftime(buf, 50, dstr.c_str(), gmtime(&value)); 
    499   if (l <= 0 || l >= 50) { 
    500     message = _T("ERROR: could format time."); 
    501     return NSCAPI::returnUNKNOWN; 
    502   } 
    503   buf[l] = 0; 
    504   message = strEx::itos(value/60) + _T("&") + buf; 
     497  message = strEx::itos(value/60) + _T("&") + strEx::format_time_delta(format, gmtime(&value)); 
    505498  return NSCAPI::returnOK; 
    506499} 
  • modules/CheckSystem/CheckSystem.cpp

    r99e4d8f r978bd31  
    7070    if (systemInfo::isBelowNT4(osVer)) { 
    7171      NSC_DEBUG_MSG_STD(_T("Autodetected NT4<, using PSAPI process enumeration.")); 
    72       processMethod_ = ENUM_METHOD::PSAPI; 
     72      if (method == (method|ENUM_METHOD::PSAPI)) { 
     73        processMethod_ = ENUM_METHOD::PSAPI; 
     74      } else { 
     75        NSC_LOG_ERROR_STD(_T("PSAPI method not available, since you are on NT4 you need to install \"Platform SDK Redistributable: PSAPI for Windows NT\" from Microsoft.")); 
     76        NSC_LOG_ERROR_STD(_T("Try this URL: http://www.microsoft.com/downloads/details.aspx?FamilyID=3d1fbaed-d122-45cf-9d46-1cae384097ac")); 
     77      } 
    7378    } else if (systemInfo::isAboveW2K(osVer)) { 
    7479      NSC_DEBUG_MSG_STD(_T("Autodetected W2K>, using TOOLHELP process enumeration.")); 
    75       processMethod_ = ENUM_METHOD::TOOLHELP; 
     80      if (method == (method|ENUM_METHOD::TOOLHELP)) { 
     81        processMethod_ = ENUM_METHOD::TOOLHELP; 
     82      } else { 
     83        NSC_LOG_ERROR_STD(_T("TOOLHELP was not available, since you are on > W2K you need top manually override the ") C_SYSTEM_ENUMPROC_METHOD _T("option in NSC:ini.")); 
     84      } 
    7685    } else { 
    7786      NSC_DEBUG_MSG_STD(_T("Autodetected failed, using PSAPI process enumeration.")); 
    7887      processMethod_ = ENUM_METHOD::PSAPI; 
     88      if (method == (method|ENUM_METHOD::PSAPI)) { 
     89        processMethod_ = ENUM_METHOD::PSAPI; 
     90      } else { 
     91        NSC_LOG_ERROR_STD(_T("PSAPI method not availabletry installing \"Platform SDK Redistributable: PSAPI for Windows NT\" from Microsoft.")); 
     92        NSC_LOG_ERROR_STD(_T("Try this URL: http://www.microsoft.com/downloads/details.aspx?FamilyID=3d1fbaed-d122-45cf-9d46-1cae384097ac")); 
     93      } 
    7994    } 
    8095  } else if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_PSAPI) { 
     
    721736  } 
    722737  CEnumProcess enumeration; 
    723   enumeration.SetMethod(processMethod); 
     738  if (enumeration.SetMethod(processMethod) != processMethod) { 
     739    NSC_LOG_ERROR_STD(_T("Failed to set process enumeration method")); 
     740    return ret; 
     741  } 
    724742  CEnumProcess::CProcessEntry entry; 
    725743  for (BOOL OK = enumeration.GetProcessFirst(&entry); OK; OK = enumeration.GetProcessNext(&entry) ) { 
     
    797815  for (std::list<StateConatiner>::iterator it = list.begin(); it != list.end(); ++it) { 
    798816    NSPROCLST::iterator proc = runningProcs.find((*it).data); 
    799     bool bFound = proc != runningProcs.end(); 
     817    bool bFound = (proc != runningProcs.end()); 
    800818    std::wstring tmp; 
    801819    TNtServiceInfo info; 
     
    904922      pdh.close(); 
    905923      double value = cDouble.getValue(); 
    906       //std::wcout << "Collected double data: " << value << std::endl; 
    907924      if (bNSClient) { 
    908925        msg += strEx::itos(static_cast<float>(value)); 
  • modules/NRPEListener/NRPEListener.cpp

    r3f569d3 r978bd31  
    301301    } else { 
    302302      DWORD dwread; 
    303       TCHAR *buf = new TCHAR[MAX_INPUT_BUFFER+1]; 
    304       retval = ReadFile(hChildOutR, buf, MAX_INPUT_BUFFER, &dwread, NULL); 
     303      //TCHAR *buf = new TCHAR[MAX_INPUT_BUFFER+1]; 
     304      char *buf = new char[MAX_INPUT_BUFFER+1]; 
     305      //retval = ReadFile(hChildOutR, buf, MAX_INPUT_BUFFER*sizeof(WCHAR), &dwread, NULL); 
     306      retval = ReadFile(hChildOutR, buf, MAX_INPUT_BUFFER*sizeof(char), &dwread, NULL); 
    305307      if (!retval || dwread == 0) { 
    306308        msg = _T("No output available from command..."); 
    307309      } else { 
    308310        buf[dwread] = 0; 
    309         msg = buf; 
     311        msg = strEx::string_to_wstring(buf); 
     312        //msg = buf; 
    310313        //strEx::token t = strEx::getToken(msg, '\n'); 
    311314        strEx::token t = strEx::getToken(msg, '|'); 
    312315        msg = t.first; 
     316        std::wstring::size_type pos = msg.find_last_not_of(_T("\n\r ")); 
     317        if (pos != std::wstring::npos) 
     318          msg = msg.substr(0,pos); 
     319        //if (msg[msg.size()-1] == '\n') 
    313320        perf = t.second; 
    314321      } 
  • modules/RemoteConfiguration/RemoteConfiguration.cpp

    r99e4d8f r978bd31  
    145145    setVariable(argLen, args, str); 
    146146  } else if (_wcsicmp(command, _T("ini2reg")) == 0) { 
    147     std::cout << _T("Migrating to registry settings...")<< std::endl; 
     147    std::wcout << _T("Migrating to registry settings...")<< std::endl; 
    148148    NSCModuleHelper::ReadSettings(NSCAPI::settings_inifile); 
    149149    NSCModuleHelper::SetSettingsInt(MAIN_SECTION_TITLE, MAIN_USEFILE, 0); 
     
    152152    NSCModuleHelper::WriteSettings(NSCAPI::settings_registry); 
    153153  } else if (_wcsicmp(command, _T("reg2ini")) == 0) { 
    154     std::cout << _T("Migrating to INI file settings...")<< std::endl; 
     154    std::wcout << _T("Migrating to INI file settings...")<< std::endl; 
    155155    NSCModuleHelper::ReadSettings(NSCAPI::settings_registry); 
    156156    NSCModuleHelper::SetSettingsInt(MAIN_SECTION_TITLE, MAIN_USEREG, 0); 
  • modules/SysTray/SysTray.cpp

    r99e4d8f r978bd31  
    5050bool SysTray::unloadModule() { 
    5151  if (!icon.exitThread(20000)) { 
    52     std::cout << _T("MAJOR ERROR: Could not unload thread...") << std::endl; 
     52    std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl; 
    5353    NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result...")); 
    5454    return false; 
  • modules/SysTray/TrayIcon.cpp

    r7e33d82 r978bd31  
    146146  ndata.uCallbackMessage=WM_ICON_NOTIFY; 
    147147  ndata.hIcon=::LoadIcon(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDI_STANDBY)); 
    148   wcsncpy_s(ndata.szTip, 128, (NSCModuleHelper::getApplicationName() + _T(" - ") + NSCModuleHelper::getApplicationVersionString()).c_str(), 63); 
     148  std::wstring title = NSCModuleHelper::getApplicationName() + _T(" - ") + NSCModuleHelper::getApplicationVersionString(); 
     149  wcsncpy_s(ndata.szTip, 64, title.c_str(), min(64, title.size())); 
    149150  Shell_NotifyIcon(NIM_ADD,&ndata); 
    150151} 
Note: See TracChangeset for help on using the changeset viewer.