Changeset cea178b in nscp for modules


Ignore:
Timestamp:
04/19/05 00:50:16 (8 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
d656933
Parents:
aaa9a22
Message:

MAJOR CHANGES
2004-04-19 MickeM

+ Added SSL support
+ Added alot of new options
+ Added documentation

  • Renamed and restructured NSCLientCompat to CheckSystem
  • *ALOT* of fixes all over : We are now starting to get to something that is "stable" : This means that soon you might actually be able to use this.
Location:
modules
Files:
10 added
17 edited

Legend:

Unmodified
Added
Removed
  • modules/CheckDisk/CheckDisk.cpp

    r1a5449e rcea178b  
    66#include <strEx.h> 
    77#include <time.h> 
     8#include <utils.h> 
    89 
    910CheckDisk gCheckDisk; 
     
    8283} 
    8384 
    84 NSCAPI::nagiosReturn CheckDisk::CheckFileSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { 
     85NSCAPI::nagiosReturn CheckDisk::CheckDriveSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { 
    8586  // CheckFileSize 
    86   // request: CheckFileSize&<option>&<option>... 
     87  // request: CheckFileSize <option> <option>... 
    8788  // <option>     MaxWarn=<size gmkb> 
    8889  //          MaxCrit=<size gmkb> 
     
    100101  // Examples: 
    101102  // <return string>  <directory> <size gmkb> ... |<shortname>=<size>:<warn>:<crit> 
    102   // test: CheckFileSize&ShowAll&MaxWarn=1024M&MaxCrit=4096M&File:WIN=c:\WINDOWS\*.* 
     103  // test: CheckFileSize ShowAll MaxWarn=1024M MaxCrit=4096M File:WIN=c:\WINDOWS\*.* 
     104  //       CheckFileSize 
     105  // 
     106  // check_nscp -H <ip> -p <port> -s <passwd> -c <commandstring> 
     107  // 
     108  // ./check_nscp -H 192.168.0.167 -p 1234 -s pwd -c 'CheckFileSize&ShowAll&MaxWarn=1024M&MaxCrit=4096M&File:WIN=c:\WINDOWS\*.*' 
     109  // WIN: 1G (2110962363B)|WIN:2110962363:1073741824:4294967296 
     110  NSC_DEBUG_MSG("CheckDriveSize"); 
     111  NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 
     112  std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); 
     113  if (args.empty()) { 
     114    message = "Missing argument(s)."; 
     115    return NSCAPI::returnCRIT; 
     116  } 
     117 
     118  checkHolders::SizeMaxMin warn; 
     119  checkHolders::SizeMaxMin crit; 
     120  bool bShowAll = false; 
     121  bool bNSClient = false; 
     122  std::list<std::string> drives; 
     123 
     124  std::list<std::string>::const_iterator cit; 
     125  for (cit=args.begin();cit!=args.end();++cit) { 
     126    std::string arg = *cit; 
     127    std::pair<std::string,std::string> p = strEx::split(arg,"="); 
     128    if (p.first == "Drive") { 
     129      drives.push_back(p.second); 
     130    } else if (p.first == "MaxWarn") { 
     131      warn.max.set(p.second); 
     132    } else if (p.first == "MinWarn") { 
     133      warn.min.set(p.second); 
     134    } else if (p.first == "MaxCrit") { 
     135      crit.max.set(p.second); 
     136    } else if (p.first == "MinCrit") { 
     137      crit.min.set(p.second); 
     138    } else if (p.first == "ShowAll") { 
     139      bShowAll = true; 
     140    } else if (p.first == "nsclient") { 
     141      bNSClient = true; 
     142    } else { 
     143      drives.push_back(p.first); 
     144    } 
     145  } 
     146 
     147  NSC_DEBUG_MSG_STD("Bounds: critical " + crit.min.toString() + " > size > " + crit.max.toString()); 
     148  NSC_DEBUG_MSG_STD("Bounds: warning " + warn.min.toString() + " > size > " + warn.max.toString()); 
     149  NSC_DEBUG_MSG_STD("Showall: " + ((bShowAll)?"yeap":"noop")); 
     150  NSC_DEBUG_MSG_STD("nsclient: " + ((bNSClient)?"yeap":"noop")); 
     151 
     152  for (std::list<std::string>::iterator it = drives.begin();it!=drives.end();it++) { 
     153    std::string drive = (*it); 
     154    if (drive.length() == 1) 
     155      drive += ":"; 
     156    if (GetDriveType(drive.c_str()) != DRIVE_FIXED){ 
     157      message = "ERROR: Drive is not a fixed drive: " + drive; 
     158      return NSCAPI::returnUNKNOWN; 
     159    } 
     160    ULARGE_INTEGER freeBytesAvailableToCaller; 
     161    ULARGE_INTEGER totalNumberOfBytes; 
     162    ULARGE_INTEGER totalNumberOfFreeBytes; 
     163    if (!GetDiskFreeSpaceEx(drive.c_str(), &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes)) { 
     164      message = "ERROR: Could not get free space for" + drive; 
     165      return NSCAPI::returnUNKNOWN; 
     166    } 
     167 
     168    if (bNSClient) { 
     169      message += strEx::itos(totalNumberOfFreeBytes.QuadPart) + "&"; 
     170      message += strEx::itos(totalNumberOfBytes.QuadPart) + "&"; 
     171    } else { 
     172      std::string tStr; 
     173      long long usedSpace = totalNumberOfBytes.QuadPart-totalNumberOfFreeBytes.QuadPart; 
     174      long long totalSpace = totalNumberOfBytes.QuadPart; 
     175      if (crit.max.hasBounds() && crit.max.checkMAX(usedSpace, totalSpace)) { 
     176        message += crit.max.prettyPrint(drive, usedSpace, totalSpace); 
     177        NSCHelper::escalteReturnCodeToCRIT(returnCode); 
     178      } else if (crit.min.hasBounds() && crit.min.checkMIN(usedSpace, totalSpace)) { 
     179        tStr = crit.min.prettyPrint(drive, usedSpace, totalSpace); 
     180        NSCHelper::escalteReturnCodeToCRIT(returnCode); 
     181      } else if (warn.max.hasBounds() && warn.max.checkMAX(usedSpace, totalSpace)) { 
     182        tStr = warn.max.prettyPrint(drive, usedSpace, totalSpace); 
     183        NSCHelper::escalteReturnCodeToWARN(returnCode); 
     184      } else if (warn.min.hasBounds() && warn.min.checkMIN(usedSpace, totalSpace)) { 
     185        tStr = warn.min.prettyPrint(drive, usedSpace, totalSpace); 
     186        NSCHelper::escalteReturnCodeToWARN(returnCode); 
     187      } else if (bShowAll) { 
     188        tStr = drive + ": " + strEx::itos_as_BKMG(usedSpace); 
     189      } 
     190      perf += checkHolders::SizeMaxMin::printPerf(drive, usedSpace, totalSpace, warn, crit); 
     191      if (!message.empty() && !tStr.empty()) 
     192        message += ", "; 
     193      if (!tStr.empty()) 
     194        message += tStr; 
     195    } 
     196  } 
     197  if (message.empty()) 
     198    message = "All drive sizes are within bounds."; 
     199  return returnCode; 
     200} 
     201 
     202NSCAPI::nagiosReturn CheckDisk::CheckFileSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { 
     203  // CheckFileSize 
     204  // request: CheckFileSize <option> <option>... 
     205  // <option>     MaxWarn=<size gmkb> 
     206  //          MaxCrit=<size gmkb> 
     207  //          MinWarn=<size gmkb> 
     208  //          MinCrit=<size gmkb> 
     209  //          ShowAll 
     210  //          File=<path> 
     211  //          File:<shortname>=<path> 
     212  // 
     213  // Return: <return state>&<return string>... 
     214  // <return state> 0 - No errors 
     215  //          1 - Unknown 
     216  //          2 - Errors 
     217  // <size gmkb> is a size with a possible modifier letter (such as G for gigabyte, M for Megabyte, K for kilobyte etc) 
     218  // Examples: 
     219  // <return string>  <directory> <size gmkb> ... |<shortname>=<size>:<warn>:<crit> 
     220  // test: CheckFileSize ShowAll MaxWarn=1024M MaxCrit=4096M File:WIN=c:\WINDOWS\*.* 
    103221  //       CheckFileSize 
    104222  // 
     
    196314  if (command == "CheckFileSize") { 
    197315    return CheckFileSize(argLen, char_args, msg, perf); 
     316  } else if (command == "CheckDriveSize") { 
     317    return CheckDriveSize(argLen, char_args, msg, perf); 
     318 
    198319//  } else if (command == "CheckFileDate") { 
    199320  }  
  • modules/CheckDisk/CheckDisk.h

    r1eef1ee rcea178b  
    1818  // Check commands 
    1919  NSCAPI::nagiosReturn CheckFileSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf); 
     20  NSCAPI::nagiosReturn CheckDriveSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf); 
    2021}; 
  • modules/CheckDisk/CheckDisk.vcproj

    r452fd41 rcea178b  
    2121        Optimization="0" 
    2222        AdditionalIncludeDirectories="../include;../../include" 
    23         PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_STLP_USE_STATIC_LIB;_STLP_USE_NEWALLOC" 
     23        PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_STLP_USE_NEWALLOC;_STLP_DEBUG=1" 
    2424        MinimalRebuild="TRUE" 
    2525        BasicRuntimeChecks="3" 
    26         RuntimeLibrary="1" 
     26        RuntimeLibrary="3" 
    2727        UsePrecompiledHeader="3" 
    2828        WarningLevel="3" 
     
    7171        Name="VCCLCompilerTool" 
    7272        AdditionalIncludeDirectories="../include;../../include" 
    73         PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_STLP_USE_STATIC_LIB;SYSTRAY_EXPORTS" 
    74         RuntimeLibrary="0" 
     73        PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS" 
     74        RuntimeLibrary="2" 
    7575        UsePrecompiledHeader="3" 
    7676        WarningLevel="3" 
     
    112112    </Configuration> 
    113113    <Configuration 
    114       Name="Dynamic Linkage|Win32" 
    115       OutputDirectory="$(ConfigurationName)" 
    116       IntermediateDirectory="$(ConfigurationName)" 
    117       ConfigurationType="2" 
    118       CharacterSet="2"> 
    119       <Tool 
    120         Name="VCCLCompilerTool" 
    121         AdditionalIncludeDirectories="../include;../../include" 
    122         PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS" 
    123         RuntimeLibrary="2" 
    124         UsePrecompiledHeader="3" 
    125         WarningLevel="3" 
    126         Detect64BitPortabilityProblems="TRUE" 
    127         DebugInformationFormat="3"/> 
    128       <Tool 
    129         Name="VCCustomBuildTool"/> 
    130       <Tool 
    131         Name="VCLinkerTool" 
    132         OutputFile="../../Release/modules/$(ProjectName).dll" 
    133         LinkIncremental="1" 
    134         ModuleDefinitionFile="CheckDisk.def" 
    135         GenerateDebugInformation="TRUE" 
    136         SubSystem="2" 
    137         OptimizeReferences="2" 
    138         EnableCOMDATFolding="2" 
    139         ImportLibrary="$(OutDir)/CheckDisk.lib" 
    140         TargetMachine="1"/> 
    141       <Tool 
    142         Name="VCMIDLTool"/> 
    143       <Tool 
    144         Name="VCPostBuildEventTool"/> 
    145       <Tool 
    146         Name="VCPreBuildEventTool"/> 
    147       <Tool 
    148         Name="VCPreLinkEventTool"/> 
    149       <Tool 
    150         Name="VCResourceCompilerTool"/> 
    151       <Tool 
    152         Name="VCWebServiceProxyGeneratorTool"/> 
    153       <Tool 
    154         Name="VCXMLDataGeneratorTool"/> 
    155       <Tool 
    156         Name="VCWebDeploymentTool"/> 
    157       <Tool 
    158         Name="VCManagedWrapperGeneratorTool"/> 
    159       <Tool 
    160         Name="VCAuxiliaryManagedWrapperGeneratorTool"/> 
    161     </Configuration> 
    162     <Configuration 
    163       Name="Debug Dynamic Linkage|Win32" 
    164       OutputDirectory="$(ConfigurationName)" 
    165       IntermediateDirectory="$(ConfigurationName)" 
    166       ConfigurationType="2" 
    167       CharacterSet="2"> 
    168       <Tool 
    169         Name="VCCLCompilerTool" 
    170         Optimization="0" 
    171         AdditionalIncludeDirectories="../include;../../include" 
    172         PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_STLP_USE_NEWALLOC;_STLP_DEBUG=1" 
    173         MinimalRebuild="TRUE" 
    174         BasicRuntimeChecks="3" 
    175         RuntimeLibrary="3" 
    176         UsePrecompiledHeader="3" 
    177         WarningLevel="3" 
    178         Detect64BitPortabilityProblems="TRUE" 
    179         DebugInformationFormat="4"/> 
    180       <Tool 
    181         Name="VCCustomBuildTool"/> 
    182       <Tool 
    183         Name="VCLinkerTool" 
    184         OutputFile="../../Debug/modules/$(ProjectName).dll" 
    185         LinkIncremental="2" 
    186         ModuleDefinitionFile="CheckDisk.def" 
    187         GenerateDebugInformation="TRUE" 
    188         ProgramDatabaseFile="$(OutDir)/CheckDisk.pdb" 
    189         SubSystem="2" 
    190         ImportLibrary="$(OutDir)/CheckDisk.lib" 
    191         TargetMachine="1"/> 
    192       <Tool 
    193         Name="VCMIDLTool"/> 
    194       <Tool 
    195         Name="VCPostBuildEventTool"/> 
    196       <Tool 
    197         Name="VCPreBuildEventTool"/> 
    198       <Tool 
    199         Name="VCPreLinkEventTool"/> 
    200       <Tool 
    201         Name="VCResourceCompilerTool"/> 
    202       <Tool 
    203         Name="VCWebServiceProxyGeneratorTool"/> 
    204       <Tool 
    205         Name="VCXMLDataGeneratorTool"/> 
    206       <Tool 
    207         Name="VCWebDeploymentTool"/> 
    208       <Tool 
    209         Name="VCManagedWrapperGeneratorTool"/> 
    210       <Tool 
    211         Name="VCAuxiliaryManagedWrapperGeneratorTool"/> 
    212     </Configuration> 
    213     <Configuration 
    214114      Name="Distribution|Win32" 
    215115      OutputDirectory="$(ConfigurationName)" 
     
    292192        </FileConfiguration> 
    293193        <FileConfiguration 
    294           Name="Dynamic Linkage|Win32"> 
     194          Name="Distribution|Win32"> 
    295195          <Tool 
    296196            Name="VCCLCompilerTool" 
    297197            UsePrecompiledHeader="1"/> 
    298198        </FileConfiguration> 
    299         <FileConfiguration 
    300           Name="Debug Dynamic Linkage|Win32"> 
    301           <Tool 
    302             Name="VCCLCompilerTool" 
    303             UsePrecompiledHeader="1"/> 
     199      </File> 
     200      <File 
     201        RelativePath="..\..\include\utils.cpp"> 
     202        <FileConfiguration 
     203          Name="Debug|Win32"> 
     204          <Tool 
     205            Name="VCCLCompilerTool" 
     206            UsePrecompiledHeader="0"/> 
     207        </FileConfiguration> 
     208        <FileConfiguration 
     209          Name="Release|Win32"> 
     210          <Tool 
     211            Name="VCCLCompilerTool" 
     212            UsePrecompiledHeader="0"/> 
    304213        </FileConfiguration> 
    305214        <FileConfiguration 
     
    307216          <Tool 
    308217            Name="VCCLCompilerTool" 
    309             UsePrecompiledHeader="1"/> 
     218            UsePrecompiledHeader="0"/> 
    310219        </FileConfiguration> 
    311220      </File> 
     
    323232      <File 
    324233        RelativePath=".\stdafx.h"> 
     234      </File> 
     235      <File 
     236        RelativePath="..\..\include\utils.h"> 
    325237      </File> 
    326238    </Filter> 
  • modules/FileLogger/FileLogger.cpp

    r452fd41 rcea178b  
    1919 
    2020bool FileLogger::loadModule() { 
    21   file_ = NSCModuleHelper::getSettingsString("log", "file", "nsclient.log"); 
     21  file_ = NSCModuleHelper::getSettingsString(LOG_SECTION_TITLE, LOG_FILENAME, LOG_FILENAME_DEFAULT); 
    2222  return true; 
    2323} 
     
    2626} 
    2727std::string FileLogger::getModuleName() { 
    28   return "File logger: " + NSCModuleHelper::getSettingsString("log", "file", "nsclient.log"); 
     28  return "File logger: " + NSCModuleHelper::getSettingsString(LOG_SECTION_TITLE, LOG_FILENAME, LOG_FILENAME_DEFAULT); 
    2929} 
    3030NSCModuleWrapper::module_version FileLogger::getModuleVersion() { 
  • modules/FileLogger/FileLogger.vcproj

    r452fd41 rcea178b  
    204204      UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> 
    205205      <File 
     206        RelativePath="..\..\include\config.h"> 
     207      </File> 
     208      <File 
    206209        RelativePath=".\FileLogger.h"> 
    207210      </File> 
  • modules/FileLogger/stdafx.h

    ra0528c4 rcea178b  
    1616#include <fstream> 
    1717 
     18#include <config.h> 
     19 
    1820 
    1921// TODO: reference additional headers your program requires here 
  • modules/NRPEListener/NRPEListener.cpp

    r452fd41 rcea178b  
    77#include <time.h> 
    88#include <config.h> 
     9#include "NRPEPacket.h" 
    910 
    1011NRPEListener gNRPEListener; 
     
    2425 
    2526bool NRPEListener::loadModule() { 
    26   timeout = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_TIMEOUT ,60); 
     27  bUseSSL_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_USE_SSL ,NRPE_SETTINGS_USE_SSL_DEFAULT)==1; 
     28  timeout = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_TIMEOUT ,NRPE_SETTINGS_TIMEOUT_DEFAULT); 
    2729  std::list<std::string> commands = NSCModuleHelper::getSettingsSection(NRPE_HANDLER_SECTION_TITLE); 
    2830  std::list<std::string>::iterator it; 
     
    4042  } 
    4143 
    42   socket.setAllowedHosts(strEx::splitEx(NSCModuleHelper::getSettingsString(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOWED, ""), ",")); 
     44  allowedHosts.setAllowedHosts(strEx::splitEx(NSCModuleHelper::getSettingsString(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOWED, NRPE_SETTINGS_ALLOWED_DEFAULT), ",")); 
    4345  try { 
    44     socket.StartListen(NSCModuleHelper::getSettingsInt("NSClient", NRPE_SETTINGS_PORT, DEFAULT_NRPE_PORT)); 
     46    if (bUseSSL_) { 
     47      socket_ssl_.setHandler(this); 
     48      socket_ssl_.StartListener(NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_PORT, NRPE_SETTINGS_PORT_DEFAULT)); 
     49    } else { 
     50      socket_.setHandler(this); 
     51      socket_.StartListener(NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_PORT, NRPE_SETTINGS_PORT_DEFAULT)); 
     52    } 
    4553  } catch (simpleSocket::SocketException e) { 
    4654    NSC_LOG_ERROR_STD("Exception caught: " + e.getMessage()); 
    4755    return false; 
    48   } 
     56  } catch (simpleSSL::SSLException e) { 
     57    NSC_LOG_ERROR_STD("Exception caught: " + e.getMessage()); 
     58    return false; 
     59  } 
     60 
    4961  return true; 
    5062} 
    5163bool NRPEListener::unloadModule() { 
    5264  try { 
    53     socket.close(); 
     65    if (bUseSSL_) { 
     66      socket_ssl_.removeHandler(this); 
     67      socket_ssl_.StopListener(); 
     68    } else { 
     69      socket_.removeHandler(this); 
     70      socket_.StopListener(); 
     71    } 
    5472  } catch (simpleSocket::SocketException e) { 
     73    NSC_LOG_ERROR_STD("Exception caught: " + e.getMessage()); 
     74    return false; 
     75  } catch (simpleSSL::SSLException e) { 
    5576    NSC_LOG_ERROR_STD("Exception caught: " + e.getMessage()); 
    5677    return false; 
     
    81102 
    82103  std::string str = (*it).second; 
    83   if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_ARGUMENTS, 0) == 1) { 
     104  if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_ARGUMENTS, NRPE_SETTINGS_ALLOW_ARGUMENTS_DEFAULT) == 1) { 
    84105    arrayBuffer::arrayList arr = arrayBuffer::arrayBuffer2list(argLen, char_args); 
    85106    arrayBuffer::arrayList::const_iterator cit = arr.begin(); 
     
    87108 
    88109    for (;cit!=arr.end();cit++,i++) { 
    89       if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_NASTY_META, 0) == 0) { 
     110      if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_NASTY_META, NRPE_SETTINGS_ALLOW_NASTY_META_DEFAULT) == 0) { 
    90111        if ((*cit).find_first_of(NASTY_METACHARS) != std::string::npos) { 
    91112          NSC_LOG_ERROR("Request string contained illegal metachars!"); 
     
    199220  return result; 
    200221} 
    201  
     222void NRPEListener::onClose() 
     223{} 
     224 
     225void NRPEListener::onAccept(simpleSocket::Socket &client)  
     226{ 
     227  if (!allowedHosts.inAllowedHosts(client.getAddrString())) { 
     228    NSC_LOG_ERROR("Unothorized access from: " + client.getAddrString()); 
     229    client.close(); 
     230    return; 
     231  } 
     232  try { 
     233    simpleSocket::DataBuffer block; 
     234 
     235    for (int i=0;i<100;i++) { 
     236      client.readAll(block); 
     237      if (block.getLength() >= NRPEPacket::getBufferLength()) 
     238        break; 
     239      Sleep(100); 
     240    } 
     241    if (i == 100) { 
     242      NSC_LOG_ERROR_STD("Could not retrieve NRPE packet."); 
     243      client.close(); 
     244      return; 
     245    } 
     246 
     247    if (block.getLength() == NRPEPacket::getBufferLength()) { 
     248      try { 
     249        NRPEPacket out = handlePacket(NRPEPacket(block.getBuffer(), block.getLength())); 
     250        block.copyFrom(out.getBuffer(), out.getBufferLength()); 
     251      } catch (NRPEPacket::NRPEPacketException e) { 
     252        NSC_LOG_ERROR_STD("NRPESocketException: " + e.getMessage()); 
     253        client.close(); 
     254        return; 
     255      } 
     256      client.send(block); 
     257    } 
     258  } catch (simpleSocket::SocketException e) { 
     259    NSC_LOG_ERROR_STD("SocketException: " + e.getMessage()); 
     260  } catch (NRPEException e) { 
     261    NSC_LOG_ERROR_STD("NRPEException: " + e.getMessage()); 
     262  } 
     263  client.close(); 
     264} 
     265 
     266NRPEPacket NRPEListener::handlePacket(NRPEPacket p) { 
     267  if (p.getType() != NRPEPacket::queryPacket) { 
     268    NSC_LOG_ERROR("Request is not a query."); 
     269    throw NRPEException("Invalid query type"); 
     270  } 
     271  if (p.getVersion() != NRPEPacket::version2) { 
     272    NSC_LOG_ERROR("Request had unsupported version."); 
     273    throw NRPEException("Invalid version"); 
     274  } 
     275  if (!p.verifyCRC()) { 
     276    NSC_LOG_ERROR("Request had invalid checksum."); 
     277    throw NRPEException("Invalid checksum"); 
     278  } 
     279  strEx::token cmd = strEx::getToken(p.getPayload(), '!'); 
     280  std::string msg, perf; 
     281  NSC_DEBUG_MSG_STD("Command: " + cmd.first); 
     282  NSC_DEBUG_MSG_STD("Arguments: " + cmd.second); 
     283 
     284  if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_ARGUMENTS, NRPE_SETTINGS_ALLOW_ARGUMENTS_DEFAULT) == 0) { 
     285    if (!cmd.second.empty()) { 
     286      NSC_LOG_ERROR("Request contained arguments (not currently allowed)."); 
     287      throw NRPEException("Request contained arguments (not currently allowed)."); 
     288    } 
     289  } 
     290  if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_NASTY_META, NRPE_SETTINGS_ALLOW_NASTY_META_DEFAULT) == 0) { 
     291    if (cmd.first.find_first_of(NASTY_METACHARS) != std::string::npos) { 
     292      NSC_LOG_ERROR("Request command contained illegal metachars!"); 
     293      throw NRPEException("Request command contained illegal metachars!"); 
     294    } 
     295    if (cmd.second.find_first_of(NASTY_METACHARS) != std::string::npos) { 
     296      NSC_LOG_ERROR("Request arguments contained illegal metachars!"); 
     297      throw NRPEException("Request command contained illegal metachars!"); 
     298    } 
     299  } 
     300 
     301  NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first, cmd.second, '!', msg, perf); 
     302  if (perf.empty()) { 
     303    return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg); 
     304  } else { 
     305    return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg + "|" + perf); 
     306  } 
     307} 
    202308 
    203309NSC_WRAPPERS_MAIN_DEF(gNRPEListener); 
  • modules/NRPEListener/NRPEListener.h

    r1a5449e rcea178b  
    11NSC_WRAPPERS_MAIN(); 
     2#include <Socket.h> 
     3#include <SSLSocket.h> 
     4#include <map> 
     5#include "NRPEPacket.h" 
    26 
    3  
    4 #include "NRPESocket.h" 
    5 #include <Socket.h> 
    6 #include <map> 
    7  
    8 class NRPEListener { 
     7class NRPEListener : public simpleSocket::ListenerHandler { 
    98private: 
    10   NRPESocket socket; 
     9  bool bUseSSL_; 
     10  simpleSSL::Listener socket_ssl_; 
     11  simpleSocket::Listener<> socket_; 
    1112  typedef std::map<std::string, std::string> commandList; 
    1213  commandList commands; 
    1314  unsigned int timeout; 
     15  socketHelpers::allowedHosts allowedHosts; 
    1416 
    1517public: 
     
    2628 
    2729private: 
     30  class NRPEException { 
     31    std::string error_; 
     32  public: 
     33/*    NRPESocketException(simpleSSL::SSLException e) { 
     34      error_ = e.getMessage(); 
     35    } 
     36    NRPEException(NRPEPacket::NRPEPacketException e) { 
     37      error_ = e.getMessage(); 
     38    } 
     39    */ 
     40    NRPEException(std::string s) { 
     41      error_ = s; 
     42    } 
     43    std::string getMessage() { 
     44      return error_; 
     45    } 
     46  }; 
     47 
     48 
     49private: 
     50  void onAccept(simpleSocket::Socket &client); 
     51  void onClose(); 
     52 
     53 
     54  NRPEPacket handlePacket(NRPEPacket p); 
    2855  int executeNRPECommand(std::string command, std::string &msg, std::string &perf); 
    2956  void addCommand(std::string key, std::string args) { 
    3057    commands[key] = args; 
    3158  } 
     59 
    3260}; 
    3361 
  • modules/NRPEListener/NRPEListener.vcproj

    r452fd41 rcea178b  
    3333      <Tool 
    3434        Name="VCLinkerTool" 
    35         AdditionalDependencies="ws2_32.lib" 
     35        AdditionalDependencies="ws2_32.lib ssleay32.lib libeay32.lib" 
    3636        OutputFile="../../Debug/modules/$(ProjectName).dll" 
    3737        LinkIncremental="2" 
     38        AdditionalLibraryDirectories="&quot;C:\Source\openssl-0.9.7f\out32dll.dbg&quot;" 
    3839        ModuleDefinitionFile="NRPEListener.def" 
    3940        GenerateDebugInformation="TRUE" 
     
    8283      <Tool 
    8384        Name="VCLinkerTool" 
    84         AdditionalDependencies="ws2_32.lib" 
     85        AdditionalDependencies="ws2_32.lib ssleay32.lib libeay32.lib" 
    8586        OutputFile="../../Release/modules/$(ProjectName).dll" 
    8687        LinkIncremental="1" 
     88        AdditionalLibraryDirectories="&quot;C:\Source\openssl-0.9.7f\out32dll&quot;" 
    8789        ModuleDefinitionFile="NRPEListener.def" 
    8890        GenerateDebugInformation="TRUE" 
     
    132134      <Tool 
    133135        Name="VCLinkerTool" 
    134         AdditionalDependencies="ws2_32.lib" 
     136        AdditionalDependencies="ws2_32.lib ssleay32.lib libeay32.lib" 
    135137        OutputFile="../../Dist/modules/$(ProjectName).dll" 
    136138        LinkIncremental="1" 
     
    181183      </File> 
    182184      <File 
    183         RelativePath=".\NRPESocket.cpp"> 
     185        RelativePath=".\NRPEPacket.cpp"> 
    184186      </File> 
    185187      <File 
     
    190192      </File> 
    191193      <File 
     194        RelativePath="..\..\include\SSLSocket.cpp"> 
     195      </File> 
     196      <File 
    192197        RelativePath=".\stdafx.cpp"> 
    193198        <FileConfiguration 
     
    208213            Name="VCCLCompilerTool" 
    209214            UsePrecompiledHeader="1"/> 
     215        </FileConfiguration> 
     216      </File> 
     217      <File 
     218        RelativePath="..\..\include\utils.cpp"> 
     219        <FileConfiguration 
     220          Name="Debug|Win32"> 
     221          <Tool 
     222            Name="VCCLCompilerTool" 
     223            UsePrecompiledHeader="0"/> 
     224        </FileConfiguration> 
     225        <FileConfiguration 
     226          Name="Release|Win32"> 
     227          <Tool 
     228            Name="VCCLCompilerTool" 
     229            UsePrecompiledHeader="0"/> 
     230        </FileConfiguration> 
     231        <FileConfiguration 
     232          Name="Distribution|Win32"> 
     233          <Tool 
     234            Name="VCCLCompilerTool" 
     235            UsePrecompiledHeader="0"/> 
    210236        </FileConfiguration> 
    211237      </File> 
     
    219245      </File> 
    220246      <File 
     247        RelativePath="..\..\include\config.h"> 
     248      </File> 
     249      <File 
    221250        RelativePath=".\NRPEListener.h"> 
    222251      </File> 
    223252      <File 
    224         RelativePath=".\NRPESocket.h"> 
    225       </File> 
    226       <File 
    227253        RelativePath="..\..\include\NSCHelper.h"> 
    228254      </File> 
     
    231257      </File> 
    232258      <File 
     259        RelativePath="..\..\include\SSLSocket.h"> 
     260      </File> 
     261      <File 
    233262        RelativePath=".\stdafx.h"> 
    234263      </File> 
    235264      <File 
    236265        RelativePath="..\..\include\thread.h"> 
     266      </File> 
     267      <File 
     268        RelativePath="..\..\include\utils.h"> 
    237269      </File> 
    238270    </Filter> 
  • modules/NRPEListener/NRPESocket.cpp

    r452fd41 rcea178b  
    33#include "NRPESocket.h" 
    44 
    5 /** 
    6  * Default c-tor 
    7  */ 
    8 NRPESocket::NRPESocket() { 
    9 } 
    10  
    11 NRPESocket::~NRPESocket() { 
    12 } 
    135 
    146 
    157 
    16 typedef short int16_t; 
    17 typedef unsigned long u_int32_t; 
    188 
    19 static unsigned long crc32_table[256]; 
    20 static bool hascrc32 = false; 
    21 void generate_crc32_table(void){ 
    22   unsigned long crc, poly; 
    23   int i, j; 
    24   poly=0xEDB88320L; 
    25   for(i=0;i<256;i++){ 
    26     crc=i; 
    27     for(j=8;j>0;j--){ 
    28       if(crc & 1) 
    29         crc=(crc>>1)^poly; 
    30       else 
    31         crc>>=1; 
    32     } 
    33     crc32_table[i]=crc; 
    34   } 
    35   hascrc32 = true; 
    36 } 
    37 unsigned long calculate_crc32(const char *buffer, int buffer_size){ 
    38   if (!hascrc32) 
    39     generate_crc32_table(); 
    40   register unsigned long crc; 
    41   int this_char; 
    42   int current_index; 
    43  
    44   crc=0xFFFFFFFF; 
    45  
    46   for(current_index=0;current_index<buffer_size;current_index++){ 
    47     this_char=(int)buffer[current_index]; 
    48     crc=((crc>>8) & 0x00FFFFFF) ^ crc32_table[(crc ^ this_char) & 0xFF]; 
    49   } 
    50  
    51   return (crc ^ 0xFFFFFFFF); 
     9const char* NRPEPacket::getBuffer() { 
     10  delete [] tmpBuffer; 
     11  tmpBuffer = new char[getBufferLength()]; 
     12  packet *p = reinterpret_cast<packet*>(tmpBuffer); 
     13  p->result_code = htons(NSCHelper::nagios2int(result_)); 
     14  p->packet_type = htons(type_); 
     15  p->packet_version = htons(version_); 
     16  p->crc32_value = 0; 
     17  strncpy(p->buffer, payload_.c_str(), 1023); 
     18  p->buffer[1024] = 0; 
     19  p->crc32_value = htonl(calculate_crc32(tmpBuffer, getBufferLength())); 
     20  return tmpBuffer; 
    5221} 
    5322 
    54  
    55 class NRPEPacket { 
    56 public: 
    57   static const short queryPacket = 1; 
    58   static const short responsePacket = 2; 
    59   static const short version2 = 2; 
    60 private: 
    61   typedef struct packet { 
    62     int16_t   packet_version; 
    63     int16_t   packet_type; 
    64     u_int32_t crc32_value; 
    65     int16_t   result_code; 
    66     char      buffer[1024]; 
    67   } packet; 
    68   std::string payload_; 
    69   short type_; 
    70   short version_; 
    71   NSCAPI::nagiosReturn result_; 
    72   unsigned int crc32_; 
    73   unsigned int calculatedCRC32_; 
    74   char *tmpBuffer; 
    75 public: 
    76   NRPEPacket(const char *buffer) : tmpBuffer(NULL) { 
    77     const packet *p = reinterpret_cast<const packet*>(buffer); 
    78     type_ = ntohs(p->packet_type); 
    79     assert( (type_ == queryPacket)||(type_ == responsePacket)); 
    80     version_ = ntohs(p->packet_version); 
    81     assert(version_ == version2); 
    82     crc32_ = ntohl(p->crc32_value); 
    83     // Verify CRC32 
    84     // @todo Fix this, currently we need a const buffer so we cannot change the crc to 0. 
    85     char * tb = new char[getBufferLength()]; 
    86     memcpy(tb, buffer, getBufferLength()); 
    87     packet *p2 = reinterpret_cast<packet*>(tb); 
    88     p2->crc32_value = 0; 
    89     calculatedCRC32_ = calculate_crc32(tb, getBufferLength()); 
    90     delete [] tb; 
    91     // Verify CRC32 end 
    92     result_ = NSCHelper::int2nagios(ntohs(p->result_code)); 
    93     payload_ = std::string(p->buffer); 
    94   } 
    95   NRPEPacket(short type, short version, NSCAPI::nagiosReturn result, std::string payLoad)  
    96     : tmpBuffer(NULL)  
    97     ,type_(type) 
    98     ,version_(version) 
    99     ,result_(result) 
    100     ,payload_(payLoad) 
    101   { 
    102   } 
    103   ~NRPEPacket() { 
    104     delete [] tmpBuffer; 
    105   } 
    106   unsigned short getVersion() const { return version_; } 
    107   unsigned short getType() const { return type_; } 
    108   unsigned short getResult() const { return result_; } 
    109   std::string getPayload() const { return payload_; } 
    110   const char* getBuffer() { 
    111     delete [] tmpBuffer; 
    112     tmpBuffer = new char[getBufferLength()]; 
    113     packet *p = reinterpret_cast<packet*>(tmpBuffer); 
    114     p->result_code = htons(NSCHelper::nagios2int(result_)); 
    115     p->packet_type = htons(type_); 
    116     p->packet_version = htons(version_); 
    117     p->crc32_value = 0; 
    118     strncpy(p->buffer, payload_.c_str(), 1023); 
    119     p->buffer[1024] = 0; 
    120     p->crc32_value = htonl(calculate_crc32(tmpBuffer, getBufferLength())); 
    121     return tmpBuffer; 
    122   } 
    123   bool verifyCRC() { 
    124     return calculatedCRC32_ == crc32_; 
    125   } 
    126   const unsigned int getBufferLength() const { 
    127     return sizeof(packet); 
    128   } 
    129 }; 
    130  
    131 void NRPESocket::onAccept(simpleSocket::Socket client) { 
    132   if (!inAllowedHosts(client.getAddrString())) { 
    133     NSC_LOG_ERROR("Unothorized access from: " + client.getAddrString()); 
    134     client.close(); 
    135     return; 
    136   } 
    137   simpleSocket::DataBuffer block; 
    138   client.readAll(block); 
    139   NRPEPacket p(block.getBuffer()); 
    140   if (p.getType() != NRPEPacket::queryPacket) { 
    141     NSC_LOG_ERROR("Request is not a query."); 
    142     client.close(); 
    143     return; 
    144   } 
    145   if (p.getVersion() != NRPEPacket::version2) { 
    146     NSC_LOG_ERROR("Request had unsupported version."); 
    147     client.close(); 
    148     return; 
    149   } 
    150   if (!p.verifyCRC()) { 
    151     NSC_LOG_ERROR("Request had invalid checksum."); 
    152     client.close(); 
    153     return; 
    154   } 
    155   strEx::token cmd = strEx::getToken(p.getPayload(), '!'); 
    156   std::string msg, perf; 
    157   NSC_DEBUG_MSG_STD("Command: " + cmd.first); 
    158   NSC_DEBUG_MSG_STD("Arguments: " + cmd.second); 
    159  
    160   if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_ARGUMENTS, 0) == 0) { 
    161     if (!cmd.second.empty()) { 
    162       NSC_LOG_ERROR("Request contained arguments (not currently allowed)."); 
    163       client.close(); 
    164       return; 
    165     } 
    166   } 
    167   if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_NASTY_META, 0) == 0) { 
    168     if (cmd.first.find_first_of(NASTY_METACHARS) != std::string::npos) { 
    169       NSC_LOG_ERROR("Request command contained illegal metachars!"); 
    170       client.close(); 
    171       return; 
    172     } 
    173     if (cmd.second.find_first_of(NASTY_METACHARS) != std::string::npos) { 
    174       NSC_LOG_ERROR("Request arguments contained illegal metachars!"); 
    175       client.close(); 
    176       return; 
    177     } 
    178   } 
    179  
    180   NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first, cmd.second, '!', msg, perf); 
    181   if (perf.empty()) { 
    182     NRPEPacket p2(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg); 
    183     client.send(p2.getBuffer(), p2.getBufferLength(), 0); 
    184   } else { 
    185     NRPEPacket p2(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg + "|" + perf); 
    186     client.send(p2.getBuffer(), p2.getBufferLength(), 0); 
    187   } 
    188   client.close(); 
    189 } 
    190  
  • modules/NRPEListener/NRPESocket.h

    r452fd41 rcea178b  
    22#include "resource.h" 
    33#include <Socket.h> 
     4#include <SSLSocket.h> 
     5 
    46/** 
    57 * @ingroup NSClient++ 
     
    3032#define NASTY_METACHARS         "|`&><'\"\\[]{}"        /* This may need to be modified for windows directory seperator */ 
    3133 
    32 class NRPESocket : public simpleSocket::Listener { 
     34typedef short int16_t; 
     35typedef unsigned long u_int32_t; 
     36 
     37 
     38template <class TBase> 
     39class NRPESocket : public TBase { 
    3340private: 
    3441  strEx::splitList allowedHosts_; 
    3542 
     43  class NRPESocketException { 
     44    std::string error_; 
     45  public: 
     46    NRPESocketException(simpleSSL::SSLException e) { 
     47      error_ = e.getMessage(); 
     48    } 
     49    NRPESocketException(NRPEPacket::NRPEPacketException e) { 
     50      error_ = e.getMessage(); 
     51    } 
     52    NRPESocketException(std::string s) { 
     53      error_ = s; 
     54    } 
     55    std::string getMessage() { 
     56      return error_; 
     57    } 
     58  }; 
     59 
     60 
    3661public: 
    37   NRPESocket(); 
    38   virtual ~NRPESocket(); 
    39  
    40   void setAllowedHosts(strEx::splitList allowedHosts) { 
    41     allowedHosts_ = allowedHosts; 
     62  NRPESocket() { 
    4263  } 
    43   bool inAllowedHosts(std::string s) { 
    44     if (allowedHosts_.empty()) 
    45       return true; 
    46     strEx::splitList::const_iterator cit; 
    47     for (cit = allowedHosts_.begin();cit!=allowedHosts_.end();++cit) { 
    48       if ( (*cit) == s) 
    49         return true; 
    50     } 
    51     return false; 
     64  virtual ~NRPESocket() { 
    5265  } 
    5366 
     67 
    5468private: 
    55   virtual void onAccept(simpleSocket::Socket client); 
     69  NRPEPacket handlePacket(NRPEPacket p) { 
     70    if (p.getType() != NRPEPacket::queryPacket) { 
     71      NSC_LOG_ERROR("Request is not a query."); 
     72      throw NRPESocketException("Invalid query type"); 
     73    } 
     74    if (p.getVersion() != NRPEPacket::version2) { 
     75      NSC_LOG_ERROR("Request had unsupported version."); 
     76      throw NRPESocketException("Invalid version"); 
     77    } 
     78    if (!p.verifyCRC()) { 
     79      NSC_LOG_ERROR("Request had invalid checksum."); 
     80      throw NRPESocketException("Invalid checksum"); 
     81    } 
     82    strEx::token cmd = strEx::getToken(p.getPayload(), '!'); 
     83    std::string msg, perf; 
     84    NSC_DEBUG_MSG_STD("Command: " + cmd.first); 
     85    NSC_DEBUG_MSG_STD("Arguments: " + cmd.second); 
     86 
     87    if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_ARGUMENTS, NRPE_SETTINGS_ALLOW_ARGUMENTS_DEFAULT) == 0) { 
     88      if (!cmd.second.empty()) { 
     89        NSC_LOG_ERROR("Request contained arguments (not currently allowed)."); 
     90        throw NRPESocketException("Request contained arguments (not currently allowed)."); 
     91      } 
     92    } 
     93    if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_NASTY_META, NRPE_SETTINGS_ALLOW_NASTY_META_DEFAULT) == 0) { 
     94      if (cmd.first.find_first_of(NASTY_METACHARS) != std::string::npos) { 
     95        NSC_LOG_ERROR("Request command contained illegal metachars!"); 
     96        throw NRPESocketException("Request command contained illegal metachars!"); 
     97      } 
     98      if (cmd.second.find_first_of(NASTY_METACHARS) != std::string::npos) { 
     99        NSC_LOG_ERROR("Request arguments contained illegal metachars!"); 
     100        throw NRPESocketException("Request command contained illegal metachars!"); 
     101      } 
     102    } 
     103 
     104    NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first, cmd.second, '!', msg, perf); 
     105    if (perf.empty()) { 
     106      return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg); 
     107    } else { 
     108      return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg + "|" + perf); 
     109    } 
     110  } 
     111  void setupDH(simpleSSL::DH &dh); 
    56112}; 
    57  
    58  
    59  
    60  
  • modules/NRPEListener/stdafx.h

    r452fd41 rcea178b  
    1515 
    1616#include <config.h> 
     17#include <utils.h> 
    1718 
    1819#include <NSCAPI.h> 
  • modules/NSClientCompat/NSClientCompat.vcproj

    r452fd41 rcea178b  
    254254      UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"> 
    255255    </Filter> 
    256     <File 
    257       RelativePath=".\ReadMe.txt"> 
    258     </File> 
    259256  </Files> 
    260257  <Globals> 
  • modules/NSClientListener/NSClientListener.cpp

    r452fd41 rcea178b  
    99 
    1010NSClientListener gNSClientListener; 
     11 
     12 
     13#define REQ_CLIENTVERSION 1 // Works fine! 
     14#define REQ_CPULOAD     2 // Quirks 
     15#define REQ_UPTIME      3 // Works fine! 
     16#define REQ_USEDDISKSPACE 4 // Works fine! 
     17#define REQ_SERVICESTATE  5 // Works fine! 
     18#define REQ_PROCSTATE   6 // Works fine! 
     19#define REQ_MEMUSE      7 // Works fine! 
     20//#define REQ_COUNTER   8 // ! - not implemented Have to look at this, if anyone has a sample let me know... 
     21//#define REQ_FILEAGE   9 // ! - not implemented Dont know how to use 
     22//#define REQ_INSTANCES 10  // ! - not implemented Dont know how to use 
    1123 
    1224BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) 
     
    2234 
    2335bool NSClientListener::loadModule() { 
    24   socket.setAllowedHosts(strEx::splitEx(NSCModuleHelper::getSettingsString("NRPE", "allowed_hosts", ""), ",")); 
     36  allowedHosts.setAllowedHosts(strEx::splitEx(NSCModuleHelper::getSettingsString(NSCLIENT_SECTION_TITLE, NSCLIENT_SETTINGS_ALLOWED, NSCLIENT_SETTINGS_ALLOWED_DEFAULT), ",")); 
    2537  try { 
    26     socket.StartListen(NSCModuleHelper::getSettingsInt("NSClient", "port", DEFAULT_NSCLIENT_PORT)); 
     38    socket.setHandler(this); 
     39    socket.StartListener(NSCModuleHelper::getSettingsInt(NSCLIENT_SECTION_TITLE, NSCLIENT_SETTINGS_PORT, NSCLIENT_SETTINGS_PORT_DEFAULT)); 
    2740  } catch (simpleSocket::SocketException e) { 
    2841    NSC_LOG_ERROR_STD("Exception caught: " + e.getMessage()); 
     
    3346bool NSClientListener::unloadModule() { 
    3447  try { 
    35     socket.close(); 
     48    socket.removeHandler(this); 
     49    socket.StopListener(); 
    3650  } catch (simpleSocket::SocketException e) { 
    3751    NSC_LOG_ERROR_STD("Exception caught: " + e.getMessage()); 
     
    4963} 
    5064 
     65 
     66std::string NSClientListener::parseRequest(std::string buffer)  { 
     67  strEx::token pwd = strEx::getToken(buffer, '&'); 
     68  NSC_DEBUG_MSG("Password: " + pwd.first); 
     69  if ( (pwd.first.empty()) || (pwd.first != NSCModuleHelper::getSettingsString(NSCLIENT_SECTION_TITLE, NSCLIENT_SETTINGS_PWD, NSCLIENT_SETTINGS_PWD_DEFAULT)) ) 
     70    return "ERROR: Invalid password."; 
     71  if (pwd.second.empty()) 
     72    return "ERRRO: No command specified."; 
     73  strEx::token cmd = strEx::getToken(pwd.second, '&'); 
     74  if (cmd.first.empty()) 
     75    return "ERRRO: No command specified."; 
     76  NSC_DEBUG_MSG("Command: " + cmd.first); 
     77 
     78  int c = atoi(cmd.first.c_str()); 
     79 
     80  // prefix various commands 
     81  switch (c) { 
     82    case REQ_CPULOAD: 
     83      cmd.first = "checkCPU"; 
     84      cmd.second += "&nsclient"; 
     85      break; 
     86    case REQ_UPTIME: 
     87      cmd.first = "checkUpTime"; 
     88      cmd.second = "nsclient"; 
     89      break; 
     90    case REQ_USEDDISKSPACE: 
     91      cmd.first = "CheckDriveSize"; 
     92      cmd.second += "&nsclient"; 
     93      break; 
     94    case REQ_CLIENTVERSION: 
     95      { 
     96        std::string v = NSCModuleHelper::getSettingsString("nsclient compat", "version", "modern"); 
     97        if (v == "modern") 
     98          return NSCModuleHelper::getApplicationName() + " " + NSCModuleHelper::getApplicationVersionString(); 
     99        return NSCModuleHelper::getSettingsString("nsclient compat", "version", "modern"); 
     100      } 
     101    case REQ_SERVICESTATE: 
     102      cmd.first = "checkServiceState"; 
     103      break; 
     104    case REQ_PROCSTATE: 
     105      cmd.first = "checkProcState"; 
     106      break; 
     107    case REQ_MEMUSE: 
     108      cmd.first = "checkMem"; 
     109      cmd.second = "nsclient"; 
     110      break; 
     111  } 
     112 
     113 
     114  std::string message, perf; 
     115  NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first.c_str(), cmd.second.c_str(), '&', message, perf); 
     116  if (!NSCHelper::isNagiosReturnCode(ret)) { 
     117    if (message.empty()) 
     118      return "ERROR: Could not complete the request check log file for more information."; 
     119    return "ERROR: " + message; 
     120  } 
     121  switch (c) { 
     122    case REQ_UPTIME:    // Some check_nt commands has no return code syntax 
     123    case REQ_MEMUSE: 
     124    case REQ_CPULOAD: 
     125    case REQ_CLIENTVERSION: 
     126    case REQ_USEDDISKSPACE: 
     127      return message; 
     128 
     129    case REQ_SERVICESTATE:  // Some check_nt commands return the return code (coded as a string) 
     130    case REQ_PROCSTATE: 
     131      return NSCHelper::translateReturn(ret) + "&" + message; 
     132 
     133    default:        // "New" check_nscp also returns performance data 
     134      if (perf.empty()) 
     135        return NSCHelper::translateReturn(ret) + "&" + message; 
     136      return NSCHelper::translateReturn(ret) + "&" + message + "|" + perf; 
     137  } 
     138} 
     139 
     140void NSClientListener::onClose() 
     141{} 
     142 
     143void NSClientListener::onAccept(simpleSocket::Socket &client) { 
     144  if (!allowedHosts.inAllowedHosts(client.getAddrString())) { 
     145    NSC_LOG_ERROR("Unothorized access from: " + client.getAddrString()); 
     146    client.close(); 
     147    return; 
     148  } 
     149  simpleSocket::DataBuffer db; 
     150  client.readAll(db); 
     151  if (db.getLength() > 0) { 
     152    std::string incoming(db.getBuffer(), db.getLength()); 
     153    NSC_DEBUG_MSG_STD("Incoming data: " + incoming); 
     154    std::string response = parseRequest(incoming); 
     155    NSC_DEBUG_MSG("Outgoing data: " + response); 
     156    client.send(response.c_str(), static_cast<int>(response.length()), 0); 
     157  } 
     158  client.close(); 
     159} 
     160 
    51161NSC_WRAPPERS_MAIN_DEF(gNSClientListener); 
    52162NSC_WRAPPERS_IGNORE_MSG_DEF(); 
  • modules/NSClientListener/NSClientListener.h

    r1a5449e rcea178b  
    1 #include "NSClientSocket.h" 
     1#include <Socket.h> 
     2#include <string> 
     3#include <utils.h> 
    24 
    35NSC_WRAPPERS_MAIN(); 
    46 
    5 class NSClientListener { 
     7class NSClientListener  : public simpleSocket::ListenerHandler { 
    68private: 
    7   NSClientSocket socket; 
     9  simpleSocket::Listener<> socket; 
     10  socketHelpers::allowedHosts allowedHosts; 
    811 
    912public: 
     
    1518  std::string getModuleName(); 
    1619  NSCModuleWrapper::module_version getModuleVersion(); 
     20  std::string parseRequest(std::string buffer); 
     21 
     22  // simpleSocket::ListenerHandler implementation 
     23  void onAccept(simpleSocket::Socket &client); 
     24  void onClose(); 
     25 
    1726}; 
  • modules/NSClientListener/NSClientListener.vcproj

    r452fd41 rcea178b  
    122122        Name="VCCLCompilerTool" 
    123123        AdditionalIncludeDirectories="../include;../../include" 
    124         PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;NSCLIENTLISTENER_EXPORTS" 
     124        PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS" 
    125125        RuntimeLibrary="2" 
    126126        UsePrecompiledHeader="3" 
     
    179179      <File 
    180180        RelativePath=".\NSClientListener.cpp"> 
    181       </File> 
    182       <File 
    183         RelativePath=".\NSClientSocket.cpp"> 
    184181      </File> 
    185182      <File 
     
    219216      </File> 
    220217      <File 
     218        RelativePath="..\..\include\config.h"> 
     219      </File> 
     220      <File 
    221221        RelativePath=".\NSClientListener.h"> 
    222       </File> 
    223       <File 
    224         RelativePath=".\NSClientSocket.h"> 
    225222      </File> 
    226223      <File 
  • modules/SysTray/SysTray.vcproj

    r452fd41 rcea178b  
    207207      UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> 
    208208      <File 
     209        RelativePath="..\..\include\config.h"> 
     210      </File> 
     211      <File 
    209212        RelativePath="..\..\include\NSCHelper.h"> 
    210213      </File> 
Note: See TracChangeset for help on using the changeset viewer.