Changeset bc97cd8 in nscp


Ignore:
Timestamp:
09/06/08 20:51:13 (5 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
dc59b0e
Parents:
c0522cd
Message:

2008-09-06 MickeM

  • Improved error reporting in the eventlogchecker
  • *BREAKING CHANGE* filter=new is now the default so unless you use the new filtering you need to specify filter=old instead! I Recommend everyone to stop using the "old" filtering.

2008-09-04 MickeM

  • Fixed issues with new service stuff on NT4 and W2K (should work fine now) + Added some DebugOutput? to service handling features so if you experience tropubel try using sysinternals DebugView? too se logging before the agent startes.
  • Fixed a memory leak in the error formating code

2008-08-24 MickeM

  • Changed NSCA "general problem" error message to be more descriptive.
  • Fixed issue with CheckCPU not returning a valid performance unit (%) see issue #219 for details.
Files:
26 edited

Legend:

Unmodified
Added
Removed
  • AutoBuild.h

    rc0522cd rbc97cd8  
    33// change the FALSE to TRUE for autoincrement of build number 
    44#define INCREMENT_VERSION TRUE 
    5 #define FILEVER        0,3,3,37 
    6 #define PRODUCTVER     0,3,3,37 
    7 #define STRFILEVER     _T("0.3.3.37") 
    8 #define STRPRODUCTVER  _T("0.3.3.37") 
    9 #define STRPRODUCTDATE  _T("2008-08-24") 
     5#define FILEVER        0,3,3,39 
     6#define PRODUCTVER     0,3,3,39 
     7#define STRFILEVER     _T("0.3.3.39") 
     8#define STRPRODUCTVER  _T("0.3.3.39") 
     9#define STRPRODUCTDATE  _T("2008-09-05") 
    1010#endif // AUTOBUILD_H 
  • NSClient++-2005.sln

    rc0522cd rbc97cd8  
    964964    {A3CF4E23-1D1B-4D93-A16A-48C52D118560}.Release|Mixed Platforms.Build.0 = Release 
    965965    {A3CF4E23-1D1B-4D93-A16A-48C52D118560}.Release|Win32.ActiveCfg = Release 
    966     {A3CF4E23-1D1B-4D93-A16A-48C52D118560}.Release|Win32.Build.0 = Release 
    967966    {A3CF4E23-1D1B-4D93-A16A-48C52D118560}.Release|x64.ActiveCfg = Release 
    968967    {A3CF4E23-1D1B-4D93-A16A-48C52D118560}.Release|x64.Build.0 = Release 
     
    10011000    {6206F046-3D36-4258-BB03-3291A7070117}.Release|Mixed Platforms.Build.0 = Release|Win32 
    10021001    {6206F046-3D36-4258-BB03-3291A7070117}.Release|Win32.ActiveCfg = Release|Win32 
    1003     {6206F046-3D36-4258-BB03-3291A7070117}.Release|Win32.Build.0 = Release|Win32 
    10041002    {6206F046-3D36-4258-BB03-3291A7070117}.Release|x64.ActiveCfg = Release|Win32 
    10051003    {D96F7075-F6CD-4921-B5D8-8488E2D24BDB}.Debug|Any CPU.ActiveCfg = Debug|x64 
     
    10381036    {D96F7075-F6CD-4921-B5D8-8488E2D24BDB}.Release|Mixed Platforms.Build.0 = Release|x64 
    10391037    {D96F7075-F6CD-4921-B5D8-8488E2D24BDB}.Release|Win32.ActiveCfg = Release|Win32 
    1040     {D96F7075-F6CD-4921-B5D8-8488E2D24BDB}.Release|Win32.Build.0 = Release|Win32 
    10411038    {D96F7075-F6CD-4921-B5D8-8488E2D24BDB}.Release|x64.ActiveCfg = Release|x64 
    10421039    {19BF0AD8-0013-46C7-9E91-F60FE3B0A63D}.Debug|Any CPU.ActiveCfg = Debug|Win32 
     
    10721069    {19BF0AD8-0013-46C7-9E91-F60FE3B0A63D}.Release|Mixed Platforms.Build.0 = Release|Win32 
    10731070    {19BF0AD8-0013-46C7-9E91-F60FE3B0A63D}.Release|Win32.ActiveCfg = Release|Win32 
    1074     {19BF0AD8-0013-46C7-9E91-F60FE3B0A63D}.Release|Win32.Build.0 = Release|Win32 
    10751071    {19BF0AD8-0013-46C7-9E91-F60FE3B0A63D}.Release|x64.ActiveCfg = Release|Win32 
    10761072    {F3401E75-60FB-4A0E-A18C-6505587D5B1A}.Debug|Any CPU.ActiveCfg = Debug|x64 
  • NSClient++.cpp

    rc0522cd rbc97cd8  
    2626#include <Lmcons.h> 
    2727 
    28 NSClient mainClient;  // Global core instance. 
     28NSClient mainClient(SZSERVICENAME); // Global core instance. 
    2929bool g_bConsoleLog = false; 
    3030 
     
    380380 
    381381  if (enable_shared_session_) { 
     382    LOG_MESSAGE_STD(_T("Enabling shared session...")); 
    382383    if (boot) { 
     384      LOG_MESSAGE_STD(_T("Starting shared session...")); 
    383385      try { 
    384386        shared_server_.reset(new nsclient_session::shared_server_session(this)); 
     
    397399      } 
    398400    } else { 
     401      LOG_MESSAGE_STD(_T("Attaching to shared session...")); 
    399402      try { 
    400403        std::wstring id = _T("_attached_") + strEx::itos(GetCurrentProcessId()) + _T("_"); 
     
    594597 */ 
    595598void WINAPI NSClientT::service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv) { 
    596   mainClient.service_main(dwArgc, lpszArgv); 
     599  try { 
     600    mainClient.service_main(dwArgc, lpszArgv); 
     601  } catch (service_helper::service_exception e) { 
     602    LOG_ERROR_STD(_T("Unknown service error: ") + e.what()); 
     603  } catch (...) { 
     604    LOG_ERROR_STD(_T("Unknown service error!")); 
     605  } 
    597606} 
    598607DWORD WINAPI NSClientT::service_ctrl_dispatch_ex(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext) { 
     
    965974    ReadLock readLock(&m_mutexRW, true, 5000); 
    966975    if (!readLock.IsLocked()) { 
     976      OutputDebugString(_T("Message was lost as the core was locked...")); 
    967977      std::wcout << _T("Message was lost as the core was locked...") << std::endl; 
    968978      return; 
     
    970980    MutexLock lock(messageMutex); 
    971981    if (!lock.hasMutex()) { 
     982      OutputDebugString(_T("Message was lost as the core was locked...")); 
     983      OutputDebugString(message.c_str()); 
    972984      std::wcout << _T("Message was lost as the core was locked...") << std::endl; 
    973985      std::wcout << message << std::endl; 
     
    9961008    } 
    9971009    if (messageHandlers_.size() == 0 || !plugins_loaded_) { 
     1010      OutputDebugString(message.c_str()); 
    9981011      log_cache_.push_back(cached_log_entry(msgType, file, line, message)); 
    9991012    } else { 
  • NSClient++.h

    rc0522cd rbc97cd8  
    152152    reportMessage(msgType, file, line, message); 
    153153  } 
    154   int session_inject(std::wstring command, std::wstring arguments, std::wstring splitter, bool escape, std::wstring &msg, std::wstring & perf) { 
    155     return inject(command, arguments, splitter[0], escape, msg, perf); 
     154  int session_inject(std::wstring command, std::wstring arguments, TCHAR splitter, bool escape, std::wstring &msg, std::wstring & perf) { 
     155    return inject(command, arguments, splitter, escape, msg, perf); 
    156156  } 
    157157 
     
    163163}; 
    164164 
    165 typedef NTService<NSClientT> NSClient; 
     165typedef service_helper::NTService<NSClientT> NSClient; 
    166166 
    167167 
  • changelog

    rc0522cd rbc97cd8  
    44 * Add API for rehashing the daemon (or implement it the API is there but does nothing) 
    55 * Improved socket performance (would be nice if we could be used as a "hub") 
     6 
     72008-09-06 MickeM 
     8 * Improved error reporting in the eventlogchecker 
     9 * *BREAKING CHANGE* filter=new is now the default so unless you use the new filtering you need to specify filter=old instead! 
     10   I Recomend everyone to stop using the "old" filtering. 
     11 
     122008-09-04 MickeM 
     13 * Fixed issues with new service stuff on NT4 and W2K (should work fine now) 
     14 + Added some DebugOutput to service handling features so if you experience tropubel try using sysinternals DebugView too se logging before the agent startes. 
     15 * Fixed a memory leak in the error formating code 
     16 
     172008-08-24 MickeM 
     18 * Changed NSCA "general problem" error message to be more descriptive. 
     19 * Fixed issue with CheckCPU not returning a valid performance unit (%) see issue #219 for details. 
    620 
    7212008-08-16 MickeM 
  • helpers/installer/Product.wxs

    rda1c7e1 rbc97cd8  
    3535  <?endif?> 
    3636 
    37   <Product Id="$(var.Product.GUID)" Name="NSClient++ ($(var.PlatForm))" Language="1033" Version="0.3.1.0" Manufacturer="MySolutions NORDIC"> 
     37  <Product Id="$(var.Product.GUID)" Name="NSClient++ ($(var.PlatForm))" Language="1033" Version="0.3.4.0" Manufacturer="MySolutions NORDIC"> 
    3838    <Package Id="$(var.Package.GUID)" Description="A simple windows monitor agent for (amongst others) nagios." 
    3939             Comments="This will appear in the file summary stream." InstallerVersion="200" Compressed="yes"/> 
     
    6060                          LongName="Stop NSClient++ ($(var.PlatForm))" WorkingDirectory='INSTALLDIR' Icon="nsclient.exe" 
    6161                          IconIndex="0" Arguments="/stop" Description="Stop NSClient++ as a service"/> 
     62              </File> 
     63              <File Id="NSClientTRAY" Name="systray.exe" DiskId="1" 
     64                    Source="$(var.Source)/systray.exe" Vital="yes"> 
     65                <Shortcut Id="StartmenuNSClientTray" Directory="ProgramMenuDir" Name="NSCPTray" 
     66                          LongName="NSClient++ ($(var.PlatForm) system tray)" WorkingDirectory="INSTALLDIR" Icon="nsclient.exe" 
     67                          IconIndex="0" Description="Run NSClient++ in test mode"/> 
    6268              </File> 
    6369              <File Id="NSClientINI" Name="NSC.ini" LongName="NSC.ini" DiskId="1" Source="$(var.Source)/NSC.ini" Vital="yes"/> 
  • helpers/systray_helper/TrayWidget.cpp

    rc0522cd rbc97cd8  
    7373} 
    7474HINSTANCE ghInstance = NULL; 
    75 IconWidget *gTrayInstance = NULL; 
    76 IconWidget::IconWidget(std::wstring cmdLine) { 
     75TrayWidget *gTrayInstance = NULL; 
     76TrayWidget::TrayWidget(std::wstring cmdLine) { 
    7777  strEx::splitList list = strEx::splitEx(cmdLine, _T(" ")); 
    7878  channel_id_ = getArgumentValue(_T("-channel"), list); 
     
    9090  } 
    9191} 
    92 IconWidget::~IconWidget() { 
     92TrayWidget::~TrayWidget() { 
    9393  gTrayInstance = NULL; 
    9494  try { 
     
    104104  shared_client_.reset(); 
    105105} 
    106 void IconWidget::connectService() { 
     106void TrayWidget::connectService() { 
    107107  LOG_MESSAGE_TRAY(_T("Reconnecting to the service...")); 
    108108  try { 
     
    114114  } 
    115115} 
    116 int IconWidget::inject(std::wstring command, std::wstring arguments, std::wstring splitter, bool escape, std::wstring &msg, std::wstring & perf) { 
     116int TrayWidget::inject(std::wstring command, std::wstring arguments, TCHAR splitter, bool escape, std::wstring &msg, std::wstring & perf) { 
    117117  if (shared_client_.get() == NULL) { 
    118118    LOG_ERROR_TRAY(_T("No active shared instance!")); 
    119119    return -1; 
    120120  } 
    121   return shared_client_->inject(command, arguments, splitter[0], escape, msg, perf); 
    122 } 
    123  
    124  
    125  
    126 void IconWidget::createDialog(HINSTANCE hInstance) { 
     121  return shared_client_->inject(command, arguments, splitter, escape, msg, perf); 
     122} 
     123 
     124 
     125 
     126void TrayWidget::createDialog(HINSTANCE hInstance) { 
    127127  ghInstance = hInstance; 
    128128  hDlgWnd = ::CreateDialog(hInstance,MAKEINTRESOURCE(IDD_NSTRAYDLG),NULL,TrayIcon::DialogProc); 
     
    160160} 
    161161 
    162 void IconWidget::session_error(std::wstring file, unsigned int line, std::wstring msg) { 
     162void TrayWidget::session_error(std::wstring file, unsigned int line, std::wstring msg) { 
    163163  log(_T("error"), file.c_str(), line, msg); 
    164164} 
    165165 
    166 void IconWidget::session_log_message(int msgType, const TCHAR* file, const int line, std::wstring message) { 
     166void TrayWidget::session_log_message(int msgType, const TCHAR* file, const int line, std::wstring message) { 
    167167  log(_T("message"), file, line, message); 
    168168} 
    169 void IconWidget::log(std::wstring category, const TCHAR* file, const int line, std::wstring message) { 
     169void TrayWidget::log(std::wstring category, const TCHAR* file, const int line, std::wstring message) { 
    170170  log_entry record(category, file, line, message); 
    171171  g_log_instance.log(_T("error"), record.file.c_str(), record.line, record.message.c_str()); 
     
    193193} 
    194194 
    195 IconWidget::log_type IconWidget::getLog() { 
     195TrayWidget::log_type TrayWidget::getLog() { 
    196196  log_type ret; 
    197197  for (log_type::const_iterator cit = log_.begin(); cit != log_.end(); ++cit) 
     
    286286      std::wstring perf; 
    287287      try { 
    288         int ret = gTrayInstance->inject(cmd, args, _T(" "), true, msg, perf); 
     288        int ret = gTrayInstance->inject(cmd, args, L' ', true, msg, perf); 
    289289        //NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectSplitAndCommand(cmd, args, ' ', msg, perf); 
    290290        /* 
     
    450450 
    451451 
    452 void insert_logrecord(HWND hwndLV, const IconWidget::log_entry &entry) { 
     452void insert_logrecord(HWND hwndLV, const TrayWidget::log_entry &entry) { 
    453453  LVITEM item; 
    454454  item.mask = LVIF_TEXT; 
     
    490490      col.pszText = _T("Message"); 
    491491      ListView_InsertColumn(hwndLV, 4, &col); 
    492       IconWidget::log_type log = gTrayInstance->getLog(); 
    493       for (IconWidget::log_type::const_iterator cit = log.begin(); cit != log.end(); ++cit) { 
     492      TrayWidget::log_type log = gTrayInstance->getLog(); 
     493      for (TrayWidget::log_type::const_iterator cit = log.begin(); cit != log.end(); ++cit) { 
    494494        insert_logrecord(hwndLV, *cit); 
    495495      } 
     
    505505    { 
    506506      HWND hwndLV = GetDlgItem(hwndDlg, IDC_LOG); 
    507       const IconWidget::log_entry* record = reinterpret_cast<const IconWidget::log_entry*>(wParam); 
     507      const TrayWidget::log_entry* record = reinterpret_cast<const TrayWidget::log_entry*>(wParam); 
    508508      insert_logrecord(hwndLV, *record); 
    509509 
  • helpers/systray_helper/TrayWidget.h

    rc0522cd rbc97cd8  
    2828#define WM_MY_CLOSE WM_USER+2 
    2929 
    30 class IconWidget : public nsclient_session::session_handler_interface { 
     30class TrayWidget : public nsclient_session::session_handler_interface { 
    3131public: 
    3232  struct log_entry { 
     
    5050 
    5151public: 
    52   IconWidget(std::wstring cmdLine); 
    53   ~IconWidget(); 
     52  TrayWidget(std::wstring cmdLine); 
     53  ~TrayWidget(); 
    5454  void createDialog(HINSTANCE hInstance); 
    5555  log_type getLog(); 
     
    6161  void session_error(std::wstring file, unsigned int line, std::wstring msg); 
    6262  void session_log_message(int msgType, const TCHAR* file, const int line, std::wstring message); 
    63   int session_inject(std::wstring command, std::wstring arguments, std::wstring splitter, bool escape, std::wstring &msg, std::wstring & perf) { 
     63  int session_inject(std::wstring command, std::wstring arguments, TCHAR splitter, bool escape, std::wstring &msg, std::wstring & perf) { 
    6464    return -1; 
    6565  } 
     
    6868  void setLogWindow(HWND hWnd) { hLogWnd = hWnd; } 
    6969  HWND getLogWindow() const { return hLogWnd; } 
    70   int inject(std::wstring command, std::wstring arguments, std::wstring splitter, bool escape, std::wstring &msg, std::wstring & perf); 
     70  int inject(std::wstring command, std::wstring arguments, TCHAR splitter, bool escape, std::wstring &msg, std::wstring & perf); 
    7171 
    7272 
  • helpers/systray_helper/systray_helper.cpp

    rc0522cd rbc97cd8  
    2626 
    2727 
    28   IconWidget widget(lpCmdLine); 
     28  TrayWidget widget(lpCmdLine); 
    2929  widget.createDialog(hInstance); 
    3030  return 0; 
  • helpers/systray_helper/systray_helper.vcproj

    rc0522cd rbc97cd8  
    218218      <Tool 
    219219        Name="VCLinkerTool" 
    220         OutputFile="$(OutDir)\systray.exe" 
     220        OutputFile="..\..\Release/systray.exe" 
    221221        LinkIncremental="1" 
    222222        GenerateDebugInformation="true" 
  • include/NSCHelper.cpp

    rc0522cd rbc97cd8  
    249249      break; 
    250250    default: 
    251       throw NSCMHExcpetion(_T("Unknown inject error.")); 
     251      delete [] msgBuffer; 
     252      delete [] perfBuffer; 
     253      throw NSCMHExcpetion(_T("Unknown return code when injecting: ") + std::wstring(command)); 
    252254  } 
    253255  delete [] msgBuffer; 
  • include/NTService.h

    rc0522cd rbc97cd8  
    2424#include <sysinfo.h> 
    2525 
    26  
     26namespace service_helper { 
     27  class service_exception { 
     28    std::wstring what_; 
     29  public: 
     30    service_exception(std::wstring what) : what_(what) { 
     31      OutputDebugString((std::wstring(_T("ERROR:")) + what).c_str()); 
     32    } 
     33    std::wstring what() { 
     34      return what_; 
     35    } 
     36  }; 
    2737/** 
    2838 * @ingroup NSClient++ 
     
    5161class NTService : public TBase 
    5262{ 
     63public: 
    5364private: 
    5465  HANDLE  hServerStopEvent; 
    5566  SERVICE_STATUS          ssStatus; 
    5667  SERVICE_STATUS_HANDLE   sshStatusHandle; 
    57   DWORD                   dwErr; 
    5868  SERVICE_TABLE_ENTRY *dispatchTable; 
     69  DWORD dwControlsAccepted; 
     70  std::wstring name_; 
     71  wchar_t *serviceName_; 
    5972public: 
    60   NTService() : dispatchTable(NULL), hServerStopEvent(NULL), dwErr(0) { 
    61     // TODO This ought to be made dynamic somehow... 
     73  NTService(std::wstring name) : dispatchTable(NULL), hServerStopEvent(NULL), name_(name), dwControlsAccepted(SERVICE_ACCEPT_STOP) { 
     74    serviceName_ = new wchar_t[name.length()+2]; 
     75    wcsncpy(serviceName_, name.c_str(), name.length()); 
    6276    dispatchTable = new SERVICE_TABLE_ENTRY[2]; 
    63     dispatchTable[0].lpServiceName = SZSERVICENAME; 
     77    dispatchTable[0].lpServiceName = serviceName_; 
    6478    dispatchTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)TBase::service_main_dispatch; 
    6579    dispatchTable[1].lpServiceName = NULL; 
     
    6882  virtual ~NTService() { 
    6983    delete [] dispatchTable; 
     84    delete [] serviceName_; 
    7085  } 
    7186 
     
    7388    BOOL ret = ::StartServiceCtrlDispatcher(dispatchTable); 
    7489    if (ret == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) { 
    75       std::wcout << "We are running in console mode, terminating..." << std::endl; 
     90      OutputDebugString(_T("We are running in console mode, terminating...")); 
    7691      return false; 
    7792    } 
    7893    return ret != 0; 
    7994  } 
     95 
     96 
     97 
     98  typedef DWORD (WINAPI *LPHANDLER_FUNCTION_EX) ( 
     99    DWORD dwControl,     // requested control code 
     100    DWORD dwEventType,   // event type 
     101    LPVOID lpEventData,  // event data 
     102    LPVOID lpContext     // user-defined context data 
     103    ); 
     104  typedef SERVICE_STATUS_HANDLE (WINAPI *REGISTER_SERVICE_CTRL_HANDLER_EX) ( 
     105    LPCTSTR lpServiceName,                // name of service 
     106    LPHANDLER_FUNCTION_EX lpHandlerProc,  // handler function 
     107    LPVOID lpContext                      // user data 
     108    ); 
     109 
     110  SERVICE_STATUS_HANDLE RegisterServiceCtrlHandlerEx_(LPCTSTR lpServiceName, LPHANDLER_FUNCTION_EX lpHandlerProc, LPVOID lpContext) { 
     111    HMODULE hAdvapi32 = NULL; 
     112    REGISTER_SERVICE_CTRL_HANDLER_EX fRegisterServiceCtrlHandlerEx = NULL; 
     113 
     114    if ((hAdvapi32 = GetModuleHandle(TEXT("Advapi32.dll"))) == NULL) 
     115      return 0; 
     116#ifdef _UNICODE 
     117    fRegisterServiceCtrlHandlerEx = (REGISTER_SERVICE_CTRL_HANDLER_EX)GetProcAddress(hAdvapi32, "RegisterServiceCtrlHandlerExW"); 
     118#else 
     119    fRegisterServiceCtrlHandlerEx = (REGISTER_SERVICE_CTRL_HANDLER_EX)GetProcAddress(hAdvapi32, "RegisterServiceCtrlHandlerExA"); 
     120#endif // _UNICODE 
     121    if (!fRegisterServiceCtrlHandlerEx) 
     122      return 0; 
     123    return fRegisterServiceCtrlHandlerEx(lpServiceName, lpHandlerProc, lpContext); 
     124  } 
     125 
     126 
    80127 
    81128  void service_main(DWORD dwArgc, LPTSTR *lpszArgv) 
    82129  { 
    83     if (systemInfo::isAboveW2K(systemInfo::getOSVersion())) { 
    84       ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SESSIONCHANGE; 
    85       sshStatusHandle = RegisterServiceCtrlHandlerEx( SZSERVICENAME, TBase::service_ctrl_dispatch_ex, NULL); 
     130    OutputDebugString(_T("service_main launcing...")); 
     131    sshStatusHandle = RegisterServiceCtrlHandlerEx_(name_.c_str(), TBase::service_ctrl_dispatch_ex, NULL); 
     132    if (sshStatusHandle == 0) { 
     133      OutputDebugString(_T("Failed to register RegisterServiceCtrlHandlerEx_ (attempting to use normal one)...")); 
     134      sshStatusHandle = RegisterServiceCtrlHandler(name_.c_str(), TBase::service_ctrl_dispatch); 
    86135    } else { 
    87       // register our service control handler: 
    88       sshStatusHandle = RegisterServiceCtrlHandler( SZSERVICENAME, TBase::service_ctrl_dispatch); 
    89     } 
    90  
    91     // SERVICE_STATUS members that don't change in example 
     136      if (systemInfo::isAboveXP(systemInfo::getOSVersion())) 
     137        dwControlsAccepted |= SERVICE_ACCEPT_SESSIONCHANGE; 
     138      else 
     139        OutputDebugString(_T("Not >w2k so sessiong messages disabled...")); 
     140    } 
     141    ssStatus.dwControlsAccepted = dwControlsAccepted; 
     142    if (sshStatusHandle == 0) 
     143      throw service_exception(_T("Failed to register service: ") + error::lookup::last_error()); 
     144 
     145 
    92146    ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; 
    93147    ssStatus.dwServiceSpecificExitCode = 0; 
    94148 
    95149    // report the status to the service control manager. 
    96     if (!ReportStatusToSCMgr(SERVICE_START_PENDING,NO_ERROR,3000)) { 
    97       if (sshStatusHandle) 
    98         ReportStatusToSCMgr(SERVICE_STOPPED,dwErr,0); 
    99     } 
    100  
    101     ServiceStart( dwArgc, lpszArgv ); 
    102  
    103     // try to report the stopped status to the service control manager. 
    104     if (sshStatusHandle) 
    105       ReportStatusToSCMgr(SERVICE_STOPPED,dwErr,0); 
     150    if (!ReportStatusToSCMgr(SERVICE_START_PENDING,3000)) { 
     151      ReportStatusToSCMgr(SERVICE_STOPPED,0); 
     152      throw service_exception(_T("Failed to report service status: ") + error::lookup::last_error()); 
     153    } 
     154    try { 
     155      OutputDebugString(_T("Attempting to start service...")); 
     156      ServiceStart(dwArgc, lpszArgv); 
     157    } catch (...) { 
     158      throw service_exception(_T("Uncaught exception in service... terminating: ") + error::lookup::last_error()); 
     159    } 
     160    ReportStatusToSCMgr(SERVICE_STOPPED,0); 
    106161  } 
    107162 
     
    119174    { 
    120175    case SERVICE_CONTROL_STOP: 
    121       ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0); 
     176      ReportStatusToSCMgr(SERVICE_STOP_PENDING, 0); 
    122177      ServiceStop(); 
    123178      return 0; 
     
    140195 
    141196    } 
    142     ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0); 
     197    ReportStatusToSCMgr(ssStatus.dwCurrentState, 0); 
    143198    return 0; 
    144199  } 
     
    159214  * 
    160215  */ 
    161   BOOL ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint) { 
     216  BOOL ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWaitHint) { 
    162217    static DWORD dwCheckPoint = 1; 
    163218    BOOL fResult = TRUE; 
     
    166221      ssStatus.dwControlsAccepted = 0; 
    167222    else 
    168       ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP| SERVICE_ACCEPT_SESSIONCHANGE; 
     223      ssStatus.dwControlsAccepted = dwControlsAccepted; 
    169224 
    170225    ssStatus.dwCurrentState = dwCurrentState; 
    171     ssStatus.dwWin32ExitCode = dwWin32ExitCode; 
     226    ssStatus.dwWin32ExitCode = 0; 
    172227    ssStatus.dwWaitHint = dwWaitHint; 
    173228 
     
    196251  void ServiceStart(DWORD dwArgc, LPTSTR *lpszArgv) { 
    197252    hServerStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 
    198     if (!ReportStatusToSCMgr(SERVICE_RUNNING,NO_ERROR,0)) { 
     253    if (!ReportStatusToSCMgr(SERVICE_RUNNING,0)) { 
    199254      if (hServerStopEvent) 
    200255        CloseHandle(hServerStopEvent); 
     
    228283  } 
    229284}; 
     285} 
  • include/error.hpp

    rb7ed6ac rbc97cd8  
    99  public: 
    1010    static std::wstring from_system(unsigned long dwError) { 
    11       LPVOID lpMsgBuf; 
     11      LPVOID lpMsgBuf = NULL; 
    1212      unsigned long dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,NULL,dwError,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0,NULL); 
    1313      if (dwRet == 0) { 
     
    1717      wsprintf(szBuf, _T("%d: %s"), dwError, lpMsgBuf);  
    1818      std::wstring str = szBuf; 
     19      delete [] szBuf; 
    1920      LocalFree(lpMsgBuf); 
    2021      return str; 
     
    2930      wsprintf(szBuf, _T("%d: %s"), dwError, lpMsgBuf);  
    3031      std::wstring str = szBuf; 
     32      delete [] szBuf; 
    3133      LocalFree(lpMsgBuf); 
    3234      return str; 
     
    4749      wsprintf(szBuf, _T("%d: %s"), dwError, lpMsgBuf);  
    4850      std::wstring str = szBuf; 
     51      delete [] szBuf; 
    4952      LocalFree(lpMsgBuf); 
    5053      FreeLibrary(hevt); 
  • include/execute_process.hpp

    rdff5db9 rbc97cd8  
    146146      DWORD error = GetLastError(); 
    147147      if (error == ERROR_BAD_EXE_FORMAT) { 
    148         NSC_LOG_ERROR_STD(command + _T(" is not an .exe file or a valid image (if you run a script you usually need to prefix the command with the interpreter like so: \"command=c:\perl.exe <script>\"")); 
     148        NSC_LOG_ERROR_STD(command + _T(" is not an .exe file or a valid image (if you run a script you usually need to prefix the command with the interpreter like so: \"command=c:\\perl.exe <script>\"")); 
    149149        msg = _T("ExternalCommands: failed to create process (") + command + _T("): it is not an exe file (check NSC.log for more info) - ") + error::lookup::last_error(error); 
    150150      } else { 
  • include/nsclient_session.hpp

    rc0522cd rbc97cd8  
    2525    virtual void session_error(std::wstring file, unsigned int line, std::wstring msg) = 0; 
    2626    virtual void session_log_message(int msgType, const TCHAR* file, const int line, std::wstring message) = 0; 
    27     virtual int session_inject(std::wstring command, std::wstring arguments, std::wstring splitter, bool escape, std::wstring &msg, std::wstring & perf) = 0; 
     27    virtual int session_inject(std::wstring command, std::wstring arguments, TCHAR splitter, bool escape, std::wstring &msg, std::wstring & perf) = 0; 
    2828  }; 
    2929 
     
    650650      remote_channel::local_message_type msg; 
    651651      msg.command = message_inject; 
     652      error(__FILEW__, __LINE__, _T("splitter") + strEx::ctos(splitter)); 
     653      error(__FILEW__, __LINE__, _T("splitter") + splitter); 
     654       
    652655      msg.arguments.push_back(command); 
    653656      msg.arguments.push_back(arguments); 
    654657      msg.arguments.push_back(strEx::ctos(splitter)); 
    655658      msg.arguments.push_back(escape?_T("1"):_T("0")); 
     659 
     660      error(__FILEW__, __LINE__, _T("arg1") + msg.arguments[1]); 
     661      error(__FILEW__, __LINE__, _T("arg2") + msg.arguments[2]); 
     662      error(__FILEW__, __LINE__, _T("arg3") + msg.arguments[3]); 
    656663 
    657664      unsigned int msg_id = send_server(msg); 
     
    772779        if (msg.arguments.size() == 4) { 
    773780          try { 
    774             ret = handler_->session_inject(msg.arguments[0], msg.arguments[1], msg.arguments[2], msg.arguments[2]==_T("1"), message, perf); 
     781            error(__FILEW__, __LINE__, _T("arg1") + msg.arguments[1]); 
     782            error(__FILEW__, __LINE__, _T("arg2") + msg.arguments[2]); 
     783            error(__FILEW__, __LINE__, _T("arg3") + msg.arguments[3]); 
     784            ret = handler_->session_inject(msg.arguments[0], msg.arguments[1], strEx::stoc(msg.arguments[2]), msg.arguments[2]==_T("1"), message, perf); 
    775785          } catch (...) { 
    776786            message = _T("Unknown exception!"); 
  • include/strEx.h

    rc0522cd rbc97cd8  
    214214  } 
    215215  inline std::wstring ctos(TCHAR c) { 
    216     return std::wstring(c,1); 
     216    return std::wstring(1, c); 
     217  } 
     218  inline TCHAR stoc(std::wstring str) { 
     219    if (str.length() == 0) 
     220      return L' '; 
     221    return str[0]; 
    217222  } 
    218223  inline std::wstring itos(unsigned int i) { 
  • include/sysinfo.h

    r7e33d82 rbc97cd8  
    5858    return ((osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT)&&(osVersion.dwMajorVersion>4)); 
    5959  } 
     60  inline bool isAboveXP(const OSVERSIONINFO &osVersion) { 
     61    if ((osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT) && (osVersion.dwMajorVersion>=4)&&(osVersion.dwMinorVersion>=1)) 
     62      return true; 
     63    return false; 
     64  } 
     65  inline bool isBelowXP(const OSVERSIONINFO &osVersion) { 
     66    if ((osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT) && (osVersion.dwMajorVersion<4)) 
     67      return true; 
     68    if ((osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT) && (osVersion.dwMajorVersion==4)&&(osVersion.dwMinorVersion<1)) 
     69      return true; 
     70    return false; 
     71  } 
    6072 
    6173} 
  • modules/CheckDisk/CheckDisk.cpp

    rc165d1d rbc97cd8  
    530530  CheckFileContainer query; 
    531531  std::wstring syntax = _T("%filename%"); 
     532  std::wstring alias; 
    532533  bool bPerfData = true; 
    533534 
     
    539540      MAP_OPTIONS_STR(_T("syntax"), syntax) 
    540541      MAP_OPTIONS_PUSH(_T("path"), paths) 
     542      MAP_OPTIONS_STR(_T("alias"), alias) 
    541543      MAP_OPTIONS_PUSH(_T("file"), paths) 
    542544      MAP_OPTIONS_BOOL_EX(_T("filter"), finder.bFilterIn, _T("in"), _T("out")) 
     
    569571  if (finder.error) 
    570572    return NSCAPI::returnUNKNOWN; 
    571   query.alias = finder.alias; 
     573  if (!alias.empty()) 
     574    query.alias = alias; 
     575  else 
     576    query.alias = finder.alias; 
     577  if (query.alias.empty()) 
     578    query.alias = _T("no files found"); 
    572579  query.runCheck(finder.hit_count, returnCode, message, perf); 
    573580  if ((truncate > 0) && (message.length() > (truncate-4))) 
  • modules/CheckEventLog/CheckEventLog.cpp

    r34e7428 rbc97cd8  
    405405  bool bFilterIn = true; 
    406406  bool bFilterAll = false; 
    407   bool bFilterNew = false; 
     407  bool bFilterNew = true; 
    408408  bool bShowDescriptions = false; 
    409409  bool unique = false; 
     
    471471 
    472472  unsigned long int hit_count = 0; 
     473  if (files.empty()) { 
     474    message = _T("No file specified try adding: file=Application"); 
     475    return NSCAPI::returnUNKNOWN; 
     476  } 
    473477 
    474478  for (std::list<std::wstring>::const_iterator cit2 = files.begin(); cit2 != files.end(); ++cit2) { 
     
    502506 
    503507        if (filter_chain.empty()) { 
    504           message = _T("No filters specified."); 
     508          message = _T("No filters specified try adding: filter+generated=>2d"); 
    505509          return NSCAPI::returnUNKNOWN; 
    506510        } 
  • modules/CheckSystem/CheckSystem.cpp

    r846bbe4 rbc97cd8  
    341341 
    342342  static std::wstring get_perf_unit(__int64 value) { 
    343     return _T(""); 
     343    return _T("%"); 
    344344  } 
    345345  static std::wstring print_perf(__int64 value, std::wstring unit) { 
  • modules/FileLogger/FileLogger.cpp

    rfebff5f rbc97cd8  
    4040} 
    4141 
    42 std::wstring FileLogger::getFileName() 
    43 { 
     42#ifndef CSIDL_COMMON_APPDATA  
     43#define CSIDL_COMMON_APPDATA 0x0023  
     44#endif 
     45typedef BOOL (WINAPI *fnSHGetSpecialFolderPath)(HWND hwndOwner, LPTSTR lpszPath, int nFolder, BOOL fCreate); 
     46 
     47__inline BOOL WINAPI _SHGetSpecialFolderPath(HWND hwndOwner, LPTSTR lpszPath, int nFolder, BOOL fCreate) { 
     48  static fnSHGetSpecialFolderPath __SHGetSpecialFolderPath = NULL; 
     49  if (!__SHGetSpecialFolderPath) { 
     50    HMODULE hDLL = LoadLibrary(_T("shfolder.dll")); 
     51    if (hDLL != NULL) 
     52      __SHGetSpecialFolderPath = (fnSHGetSpecialFolderPath)GetProcAddress(hDLL,"SHGetSpecialFolderPathW"); 
     53  } 
     54  if(__SHGetSpecialFolderPath) 
     55    return __SHGetSpecialFolderPath(hwndOwner, lpszPath, nFolder, fCreate); 
     56  return FALSE; 
     57} 
     58 
     59std::wstring getFolder(std::wstring key) { 
     60  if (key == _T("exe")) { 
     61    return NSCModuleHelper::getBasePath(); 
     62  } else { 
     63    if (key == _T("local-app-data")) { 
     64      TCHAR buf[MAX_PATH+1]; 
     65      _SHGetSpecialFolderPath(NULL, buf, CSIDL_COMMON_APPDATA, FALSE); 
     66      return buf; 
     67    } 
     68  } 
     69  return NSCModuleHelper::getBasePath(); 
     70} 
     71std::wstring FileLogger::getFileName() { 
    4472  if (file_.empty()) { 
    4573    file_ = NSCModuleHelper::getSettingsString(LOG_SECTION_TITLE, LOG_FILENAME, LOG_FILENAME_DEFAULT); 
    46     if (file_.find(_T("\\")) == std::wstring::npos) 
    47       file_ = NSCModuleHelper::getBasePath() + _T("\\") + file_; 
     74    if (file_.find(_T("\\")) == std::wstring::npos) { 
     75      std::wstring root = getFolder(NSCModuleHelper::getSettingsString(LOG_SECTION_TITLE, LOG_ROOT, LOG_ROOT_DEFAULT)); 
     76      std::wstring::size_type pos = root.find_last_not_of(L'\\'); 
     77      if (pos != std::wstring::npos) { 
     78        //root = root.substr(0, pos); 
     79      } 
     80      file_ = root + _T("\\") + file_; 
     81    } 
    4882  } 
    4983  return file_; 
     
    5791  std::wstring hello = _T("Starting to log for: ") + NSCModuleHelper::getApplicationName() + _T(" - ") + NSCModuleHelper::getApplicationVersionString(); 
    5892  handleMessage(NSCAPI::log, __FILEW__, __LINE__, hello.c_str()); 
     93  NSC_LOG_MESSAGE_STD(_T("Log path is: ") + file_ ); 
    5994  return true; 
    6095} 
     
    68103  return true; 
    69104} 
     105HANDLE openAppendOrNew(std::wstring file) { 
     106  DWORD numberOfBytesWritten = 0; 
     107  HANDLE hFile = ::CreateFile(file.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
     108  if (hFile == INVALID_HANDLE_VALUE) { 
     109    hFile = ::CreateFile(file.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
     110    if (hFile != INVALID_HANDLE_VALUE) { 
     111      WORD wBOM = 0xFEFF; 
     112      ::WriteFile(hFile, &wBOM, sizeof(WORD), &numberOfBytesWritten, NULL); 
     113    } 
     114  } 
     115  return hFile; 
     116} 
    70117void FileLogger::writeEntry(std::wstring line) { 
    71118  DWORD numberOfBytesWritten; 
    72   HANDLE hFile = ::CreateFile(file_.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
     119  HANDLE hFile = openAppendOrNew(file_); 
    73120  if (hFile == INVALID_HANDLE_VALUE) { 
    74     hFile = ::CreateFile(file_.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
    75     if (hFile == INVALID_HANDLE_VALUE) { 
    76       std::wcout << _T("Failed to write to log file: ") << file_ << std::endl; 
    77       return; 
    78     } 
    79     WORD wBOM = 0xFEFF; 
    80     ::WriteFile(hFile, &wBOM, sizeof(WORD), &numberOfBytesWritten, NULL); 
     121    if (line.find(_T("can not log to file")) != std::wstring::npos) 
     122      NSC_LOG_MESSAGE_STD(_T("Failed to create log file and temporary log! (can not log to file): ") + file_ ); 
     123    return; 
    81124  } 
    82   //::WriteFile(hFile, &wBOM, sizeof(WORD), &NumberOfBytesWritten, NULL); 
    83125  if (::SetFilePointer(hFile, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER) { 
    84126    std::wcout << _T("Failed to move pointer to end of file...") << std::endl; 
     
    89131 
    90132void FileLogger::handleMessage(int msgType, TCHAR* file, int line, const TCHAR* message) { 
    91   if (!init_) 
     133  if (!init_) { 
     134    std::wcout << _T("Discarding message (not initzialized yet") << std::endl; 
    92135    return; 
     136  } 
    93137  TCHAR buffer[65]; 
    94138  __time64_t ltime; 
  • modules/LUAScript/script_wrapper.hpp

    r367bf20 rbc97cd8  
    355355 
    356356      lua_createtable(L, 0, argLen); 
    357       for (int i=0;i<argLen;i++) { 
     357      for (unsigned int i=0;i<argLen;i++) { 
    358358        lua_pushnumber(L,i+1); 
    359359        lua_pushstring(L,strEx::wstring_to_string(char_args[i]).c_str()); 
  • modules/NSCAAgent/NSCAAgent-2005.vcproj

    rc0522cd rbc97cd8  
    234234        Name="VCLinkerTool" 
    235235        AdditionalDependencies="ws2_32.lib Crypto++.lib" 
    236         OutputFile="$(OutDir)\$(ProjectName).dll" 
     236        OutputFile="../../Release/modules/$(ProjectName).dll" 
    237237        LinkIncremental="1" 
    238238        AdditionalLibraryDirectories="$(SolutionDir)lib\$(ConfigurationName)\$(PlatformName)" 
  • modules/NSCAAgent/NSCAThread.cpp

    r5aebda1 rbc97cd8  
    182182      crypt_inst.encrypt_init(password_.c_str(),encryption_method_,reinterpret_cast<unsigned char*>(packet_in->iv)); 
    183183    } catch (nsca_encrypt::encryption_exception &e) { 
    184       NSC_LOG_ERROR_STD(_T("<<< Failed to initalize encryption header: ") + e.getMessage()); 
     184      NSC_LOG_ERROR_STD(_T("<<< NSCA Encryption header missmatch (hint: if you dont use NSCA dot use the NSCA module): ") + e.getMessage()); 
    185185      return; 
    186186    } catch (...) { 
    187       NSC_LOG_ERROR_STD(_T("<<< Failed to initalize encryption header!")); 
     187      NSC_LOG_ERROR_STD(_T("<<< NSCA Encryption header missmatch (hint: if you dont use NSCA dot use the NSCA module)!")); 
    188188      return; 
    189189    } 
     
    206206    socket.close(); 
    207207  } catch (...) { 
    208     NSC_LOG_ERROR_STD(_T("<<< Failed to initalize encryption header!")); 
     208    NSC_LOG_ERROR_STD(_T("<<< NSCA Configuration missmatch (hint: if you dont use NSCA dot use the NSCA module)!")); 
    209209    return; 
    210210  } 
  • modules/SysTray/SysTray.cpp

    rc0522cd rbc97cd8  
    4343} 
    4444bool SysTray::loadModule() { 
    45   if (systemInfo::isBelowXP(systemInfo::getOSVersion())) { 
    46     try { 
    47       if ((serviceControll::GetServiceType(SZSERVICENAME)&SERVICE_INTERACTIVE_PROCESS)!=SERVICE_INTERACTIVE_PROCESS) { 
    48         NSC_LOG_ERROR(_T("SysTray is not installed (or it cannot interact with the desktop) SysTray won't be loaded. Run ") SZAPPNAME _T(" SysTray install to change this.")); 
    49         return true; 
    50       } 
    51     } catch (serviceControll::SCException e) { 
     45  if (NSCModuleHelper::getSettingsInt(MAIN_SECTION_TITLE, MAIN_SHARED_SESSION, MAIN_SHARED_SESSION_DEFAULT) == 1) { 
     46    NSC_LOG_ERROR(_T("You have enabled shared session, systray module will not load...")); 
     47    return true; 
     48  } 
     49  try { 
     50    if ((serviceControll::GetServiceType(SZSERVICENAME)&SERVICE_INTERACTIVE_PROCESS)!=SERVICE_INTERACTIVE_PROCESS) { 
    5251      NSC_LOG_ERROR(_T("SysTray is not installed (or it cannot interact with the desktop) SysTray won't be loaded. Run ") SZAPPNAME _T(" SysTray install to change this.")); 
    5352      return true; 
    5453    } 
    55     show(); 
    56   } else { 
    57     NSC_LOG_ERROR(_T("SysTray module is not used on windows XP and above (so you can remove it).")); 
     54  } catch (serviceControll::SCException e) { 
     55    NSC_LOG_ERROR(_T("SysTray is not installed (or it cannot interact with the desktop) SysTray won't be loaded. Run ") SZAPPNAME _T(" SysTray install to change this.")); 
     56    return true; 
    5857  } 
     58  show(); 
    5959  return true; 
    6060} 
  • scripts/test.lua

    r367bf20 rbc97cd8  
    1 --require("luacom") 
    2 --require("luacom") 
    3 --require("luacom-lua5-13") 
    4  
    51nscp.print('Loading test script...') 
    6 -- win = loadlib("win32.dll","luaopen_w32") 
    7 -- print(win) -- nil 
    8 require( 'w32' ) 
    9 nscp.print(w32) 
    10 nscp.print(w32.FindWindow) 
    112 
    123nscp.execute('version') 
     
    2213function something (command) 
    2314  nscp.print(command) 
    24   --code, msg, perf = inject('CheckCPU','time=5','MaxCrit=5') 
    25   msg = 'hello' 
    26   perf = 'hello' 
    27   code = 'ok' 
     15  code, msg, perf = nscp.execute('CheckCPU','time=5','MaxCrit=5') 
    2816  print(code .. ': ' .. msg .. ', ' .. perf) 
    2917  collectgarbage () 
Note: See TracChangeset for help on using the changeset viewer.