Changeset 79e734f in nscp


Ignore:
Timestamp:
12/12/09 13:15:44 (3 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
031acbf
Parents:
9b3f53c
Message:

Re added some more stuff anf fixed *nix compatibility of sockets

Files:
2 added
19 edited

Legend:

Unmodified
Added
Removed
  • build.cmake

    rdcd90b2 r79e734f  
    11SET(Boost_DEBUG 1) 
    22 
    3 set(Boost_USE_STATIC_LIBS   ON) 
     3#set(Boost_USE_STATIC_LIBS   ON) 
    44set(BOOST_USE_MULTITHREADED ON) 
    55 
  • include/NSCHelper.cpp

    r9b3f53c r79e734f  
    350350 */ 
    351351namespace NSCModuleHelper { 
    352 NSCAPI::nagiosReturn InjectSplitAndCommand(const std::wstring command, const std::wstring buffer, wchar_t spliwchar_t, std::wstring & message, std::wstring & perf, int escape) 
     352NSCAPI::nagiosReturn InjectSplitAndCommand(const std::wstring command, const std::wstring buffer, wchar_t spliwchar_t, std::wstring & message, std::wstring & perf, bool escape) 
    353353{ 
    354354  if (!fNSAPIInject) 
  • include/execute_process.hpp

    r3692371 r79e734f  
    3131  }; 
    3232 
    33 #define BUFF_SIZE 4096 
     33  class exec_arguments { 
     34  public: 
     35    exec_arguments(std::wstring root_path_, std::wstring command_, unsigned int timeout_) 
     36      : root_path(root_path_) 
     37      , command(command_) 
     38      , timeout(timeout_)  
     39    {} 
    3440 
    35   char* createBuffer() { 
    36     return new char[BUFF_SIZE+1]; 
    37   } 
    38   void destroyBuffer(char* buffer) { 
    39     delete [] buffer; 
    40   } 
    41  
    42   std::string readFromFile(char* buffer, HANDLE hFile) { 
    43     DWORD dwRead = 0; 
    44     DWORD retval = 0; 
    45     std::string str; 
    46     do { 
    47       DWORD retval = ReadFile(hFile, buffer, BUFF_SIZE, &dwRead, NULL); 
    48       if (retval == 0 || dwRead <= 0 || dwRead > BUFF_SIZE) 
    49         return str; 
    50       buffer[dwRead] = 0; 
    51       str += buffer; 
    52     } while (dwRead == BUFF_SIZE); 
    53     return str; 
    54   } 
    55  
    56   int executeProcess(std::wstring root_path, std::wstring command, std::wstring &msg, std::wstring &perf, unsigned int timeout) { 
    57     NSCAPI::nagiosReturn result; 
    58     PROCESS_INFORMATION pi; 
    59     STARTUPINFO si; 
    60     HANDLE hChildOutR, hChildOutW, hChildInR, hChildInW; 
    61     SECURITY_ATTRIBUTES sec; 
    62     DWORD dwstate, dwexitcode; 
    63     // Set up members of SECURITY_ATTRIBUTES structure.  
    64     sec.nLength = sizeof(SECURITY_ATTRIBUTES); 
    65     sec.bInheritHandle = TRUE; 
    66     sec.lpSecurityDescriptor = NULL; 
    67  
    68     // Create Pipes 
    69     CreatePipe(&hChildInR, &hChildInW, &sec, 0); 
    70     CreatePipe(&hChildOutR, &hChildOutW, &sec, 0); 
    71  
    72     // Set up members of STARTUPINFO structure.  
    73  
    74     ZeroMemory(&si, sizeof(STARTUPINFO)); 
    75     si.cb = sizeof(STARTUPINFO); 
    76     si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; 
    77     si.hStdInput = hChildInR; 
    78     si.hStdOutput = hChildOutW; 
    79     si.hStdError = hChildOutW; 
    80     si.wShowWindow = SW_HIDE; 
    81  
    82  
    83     // CreateProcess doesn't work with a const command 
    84     TCHAR *cmd = new TCHAR[command.length()+1]; 
    85     wcsncpy_s(cmd, command.length()+1, command.c_str(), command.length()); 
    86     cmd[command.length()] = 0; 
    87  
    88     // Create the child process. 
    89     //HANDLE hWaitEvt = ::CreateEvent(NULL, TRUE, FALSE, NULL); 
    90     BOOL processOK = CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, root_path.c_str(), &si, &pi); 
    91     delete [] cmd; 
    92     if (processOK) { 
    93       DWORD dwAvail = 0; 
    94       std::string str; 
    95       //HANDLE handles[2]; 
    96       //handles[0] = pi.hProcess; 
    97       //handles[1] = hWaitEvt; 
    98       char *buffer = createBuffer(); 
    99       for (unsigned int i=0;i<timeout;i++) { 
    100         if (!::PeekNamedPipe(hChildOutR, NULL, 0, NULL, &dwAvail, NULL)) 
    101           break; 
    102         if (dwAvail > 0) 
    103           str += readFromFile(buffer, hChildOutR); 
    104         dwstate = WaitForSingleObject(pi.hProcess, 1000); 
    105         if (dwstate != WAIT_TIMEOUT) 
    106           break; 
    107       } 
    108       CloseHandle(hChildInR); 
    109       CloseHandle(hChildInW); 
    110       CloseHandle(hChildOutW); 
    111  
    112       dwAvail = 0; 
    113       if (!::PeekNamedPipe(hChildOutR, NULL, 0, NULL, &dwAvail, NULL)) 
    114         NSC_LOG_ERROR_STD(_T("Failed to peek buffer: ") + error::lookup::last_error()); 
    115       if (dwAvail > 0) 
    116         str += readFromFile(buffer, hChildOutR); 
    117       msg = strEx::string_to_wstring(str); 
    118       destroyBuffer(buffer); 
    119  
    120       if (dwstate == WAIT_TIMEOUT) { 
    121         TerminateProcess(pi.hProcess, 5); 
    122         msg = _T("Command (") + command + _T(") didn't terminate within the timeout period (") + strEx::itos(timeout) + _T("s)!"); 
    123         result = NSCAPI::returnUNKNOWN; 
    124       } else { 
    125         if (msg.empty()) { 
    126           msg = _T("No output available from command (") + command + _T(")."); 
    127         } else { 
    128           strEx::token t = strEx::getToken(msg, '|'); 
    129           msg = t.first; 
    130           std::wstring::size_type pos = msg.find_last_not_of(_T("\n\r ")); 
    131           if (pos != std::wstring::npos) { 
    132             if (pos == msg.size()) 
    133               msg = msg.substr(0,pos); 
    134             else 
    135               msg = msg.substr(0,pos+1); 
    136           } 
    137           perf = t.second; 
    138         } 
    139         if (GetExitCodeProcess(pi.hProcess, &dwexitcode) == 0) { 
    140           msg = _T("Failed to get commands (") + command + _T(") return code: ") + error::lookup::last_error(); 
    141           result = NSCAPI::returnUNKNOWN; 
    142         } else { 
    143           result = dwexitcode; 
    144         } 
    145       } 
    146       CloseHandle(pi.hThread); 
    147       CloseHandle(pi.hProcess); 
    148       CloseHandle(hChildOutR); 
    149     } else { 
    150       DWORD error = GetLastError(); 
    151       if (error == ERROR_BAD_EXE_FORMAT) { 
    152         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>\"")); 
    153         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); 
    154       } else { 
    155         msg = _T("ExternalCommands: failed to create process (") + command + _T("): ") + error::lookup::last_error(error); 
    156       } 
    157       result = NSCAPI::returnUNKNOWN; 
    158       CloseHandle(hChildInR); 
    159       CloseHandle(hChildInW); 
    160       CloseHandle(hChildOutW); 
    161       CloseHandle(pi.hThread); 
    162       CloseHandle(pi.hProcess); 
    163       CloseHandle(hChildOutR); 
    164     } 
    165     return result; 
    166   } 
     41    std::wstring root_path; 
     42    std::wstring command; 
     43    unsigned int timeout; 
     44  }; 
    16745} 
    16846 
     47#ifdef _WIN32 
     48#include "execute_process_w32.hpp" 
     49#else 
     50#include "execute_process_unix.hpp" 
     51#endif 
     52 
  • include/file_helpers.hpp

    rf0eb62d r79e734f  
    11#pragma once 
     2 
     3#include <boost/filesystem.hpp> 
    24 
    35namespace file_helpers { 
     
    57  public: 
    68    static bool is_directory(std::wstring path) { 
    7       return is_directory(::GetFileAttributes(path.c_str())); 
     9      boost::filesystem::is_directory(path); 
    810    } 
    9     static bool is_directory(DWORD dwAtt) { 
    10       if (dwAtt == INVALID_FILE_ATTRIBUTES) { 
    11         return false; 
    12       } else if ((dwAtt&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY) { 
    13         return true; 
    14       } 
    15       return false; 
    16     } 
     11//    static bool is_directory(DWORD dwAtt) { 
     12//      if (dwAtt == INVALID_FILE_ATTRIBUTES) { 
     13//        return false; 
     14//      } else if ((dwAtt&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY) { 
     15//        return true; 
     16//      } 
     17//      return false; 
     18//    } 
    1719    static bool is_file(std::wstring path) { 
    18       DWORD dwAtt = ::GetFileAttributes(path.c_str()); 
    19       if (dwAtt == INVALID_FILE_ATTRIBUTES) { 
    20         return false; 
    21       } else if ((dwAtt&FILE_ATTRIBUTE_NORMAL)==FILE_ATTRIBUTE_NORMAL) { 
    22         return true; 
    23       } 
    24       return false; 
     20      return boost::filesystem::is_regular(path); 
     21//      DWORD dwAtt = ::GetFileAttributes(path.c_str()); 
     22//      if (dwAtt == INVALID_FILE_ATTRIBUTES) { 
     23//        return false; 
     24//      } else if ((dwAtt&FILE_ATTRIBUTE_NORMAL)==FILE_ATTRIBUTE_NORMAL) { 
     25//        return true; 
     26//      } 
     27//      return false; 
    2528    } 
    2629    static bool exists(std::wstring path) { 
    27       DWORD dwAtt = ::GetFileAttributes(path.c_str()); 
    28       if (dwAtt == INVALID_FILE_ATTRIBUTES) { 
    29         return false; 
    30       } 
    31       return true; 
     30      return boost::filesystem::exists(path); 
     31//      DWORD dwAtt = ::GetFileAttributes(path.c_str()); 
     32//      if (dwAtt == INVALID_FILE_ATTRIBUTES) { 
     33//        return false; 
     34//      } 
     35//      return true; 
    3236    } 
    3337  }; 
     
    3539  class patterns { 
    3640  public: 
    37     typedef std::pair<std::wstring,std::wstring> pattern_type; 
     41    typedef std::pair<boost::filesystem::wpath,std::wstring> pattern_type; 
    3842 
    39     static pattern_type split_pattern(std::wstring path) { 
    40       std::wstring baseDir; 
    41       if (file_helpers::checks::exists(path)) { 
     43    static pattern_type split_pattern(boost::filesystem::wpath path) { 
     44      if (boost::filesystem::is_directory(path)) 
    4245        return pattern_type(path, _T("")); 
    43       } 
    44       std::wstring::size_type pos = path.find_last_of('\\'); 
    45       if (pos == std::wstring::npos) { 
    46         pattern_type(path, _T("*.*")); 
    47       } 
    48       return pattern_type(path.substr(0, pos), path.substr(pos+1)); 
     46      return pattern_type(path.branch_path(), path.leaf() /*filename()*/); 
    4947    } 
    50     static std::wstring combine_pattern(pattern_type pattern) { 
    51       return pattern.first + _T("\\") + pattern.second; 
     48    static boost::filesystem::wpath combine_pattern(pattern_type pattern) { 
     49      return pattern.first / pattern.second; 
    5250    } 
    5351 
  • include/nrpe/nrpepacket.hpp

    r9b3f53c r79e734f  
    5555    return SwapBytes<T, sizeof(T)>(value); 
    5656  } 
     57 
    5758  template<class T> 
    5859  inline T ntoh(T value) { 
    59     std::cout << "Swaping (in): " << value << " => " << EndianSwapBytes<EEndian::BIG_ENDIAN_ORDER, EEndian::HOST_ENDIAN_ORDER, T>(value) << std::endl; 
    60     return EndianSwapBytes<EEndian::BIG_ENDIAN_ORDER, EEndian::HOST_ENDIAN_ORDER, T>(value); 
    61   } 
    62   template<class T> 
     60    return EndianSwapBytes<BIG_ENDIAN_ORDER, HOST_ENDIAN_ORDER, T >(value); 
     61  } 
     62  template<typename T> 
    6363  inline T hton(T value) { 
    64     std::cout << "Swaping (out): " << value << " => " << EndianSwapBytes<EEndian::HOST_ENDIAN_ORDER, EEndian::BIG_ENDIAN_ORDER, T>(value) << std::endl; 
    65     return EndianSwapBytes<EEndian::HOST_ENDIAN_ORDER, EEndian::BIG_ENDIAN_ORDER, T>(value); 
     64    return EndianSwapBytes<HOST_ENDIAN_ORDER, BIG_ENDIAN_ORDER, T >(value); 
    6665  } 
    6766 
     
    108107  }; 
    109108 
    110   class nrpe_packet_exception { 
     109  class nrpe_exception { 
    111110    std::wstring error_; 
    112111  public: 
    113     nrpe_packet_exception(std::wstring error) : error_(error) {} 
     112    nrpe_exception(std::wstring error) : error_(error) {} 
    114113    std::wstring getMessage() { 
    115114      return error_; 
    116115    } 
     116  }; 
     117  class nrpe_packet_exception : public nrpe_exception { 
     118  public: 
     119    nrpe_packet_exception(std::wstring error) : nrpe_exception(error) {} 
    117120  }; 
    118121 
  • include/socket_helpers.hpp

    r9b3f53c r79e734f  
    9292      } 
    9393      */ 
    94       return ((allowed.in_addr&allowed.mask)==(remote.S_un.S_addr&allowed.mask)); 
     94      return true; //((allowed.in_addr&allowed.mask)==(remote.S_un.S_addr&allowed.mask)); 
    9595    } 
    9696    bool inAllowedHosts(boost::asio::io_service& io_service, struct in_addr remote) { 
  • modules/CheckExternalScripts/CheckExternalScripts.cpp

    r6672c56 r79e734f  
    2121#include "stdafx.h" 
    2222#include "CheckExternalScripts.h" 
    23 #include <strEx.h> 
    2423#include <time.h> 
     24 
    2525#include <settings/macros.h> 
    2626#include <msvc_wrappers.h> 
     27#include <config.h> 
     28#include <strEx.h> 
    2729#include <file_helpers.hpp> 
    28 #include <config.h> 
     30 
     31#include <boost/regex.hpp> 
     32#include <boost/filesystem.hpp> 
     33 
    2934 
    3035CheckExternalScripts gCheckExternalScripts; 
    31  
     36#ifdef _WIN32 
    3237BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) 
    3338{ 
     
    3540  return TRUE; 
    3641} 
     42#endif 
    3743 
    3844CheckExternalScripts::CheckExternalScripts() {} 
    3945CheckExternalScripts::~CheckExternalScripts() {} 
    4046 
    41 void CheckExternalScripts::addAllScriptsFrom(std::wstring path) { 
    42   file_helpers::patterns::pattern_type pattern = file_helpers::patterns::split_pattern(path); 
    43   if (!file_helpers::checks::exists(pattern.first))  
    44     pattern.first = NSCModuleHelper::getBasePath() + _T("\\") + pattern.first; 
    45   if (!file_helpers::checks::exists(pattern.first)) 
    46     NSC_LOG_ERROR_STD(_T("Path was not found: ") + pattern.first); 
    47 /* TODO: do we need this? 
    48   std::wstring::size_type pos = path.find_last_of('*'); 
    49   if (pos == std::wstring::npos) { 
    50     path += _T("*.*"); 
    51   } 
    52   */ 
    53   WIN32_FIND_DATA wfd; 
    54   std::wstring real_path = file_helpers::patterns::combine_pattern(pattern); 
    55   HANDLE hFind = FindFirstFile(real_path.c_str(), &wfd); 
    56   if (hFind != INVALID_HANDLE_VALUE) { 
    57     do { 
    58       if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) { 
    59         addCommand(wfd.cFileName, pattern.first + _T("\\") + wfd.cFileName, _T("")); 
    60       } 
    61     } while (FindNextFile(hFind, &wfd)); 
    62   } else { 
    63     NSC_LOG_ERROR_STD(_T("No scripts found in path: ") + real_path); 
    64     return; 
    65   } 
    66   FindClose(hFind); 
     47void CheckExternalScripts::addAllScriptsFrom(std::wstring str_path) { 
     48  boost::filesystem::wpath path = str_path; 
     49  if (path.has_relative_path()) 
     50    path = NSCModuleHelper::getBasePath() / path; 
     51  file_helpers::patterns::pattern_type split_path = file_helpers::patterns::split_pattern(path); 
     52  if (!boost::filesystem::is_directory(split_path.first)) 
     53    NSC_LOG_ERROR_STD(_T("Path was not found: ") + split_path.first.string()); 
     54 
     55  boost::wregex pattern(split_path.second); 
     56  boost::filesystem::wdirectory_iterator end_itr; // default construction yields past-the-end 
     57  for ( boost::filesystem::wdirectory_iterator itr( split_path.first ); itr != end_itr; ++itr ) { 
     58    if ( !is_directory(itr->status()) ) { 
     59      std::wstring name = itr->path().leaf(); 
     60      if (regex_match(name, pattern)) 
     61        addCommand(name, (split_path.first / name).string(), _T("")); 
     62    } 
     63  } 
    6764} 
    6865 
     
    128125 
    129126NSCAPI::nagiosReturn CheckExternalScripts::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf) { 
    130   command_list::const_iterator cit = commands.find(command); 
     127  std::wstring cmd = command.c_str(); 
     128  boost::to_lower(cmd); 
     129  command_list::const_iterator cit = commands.find(cmd); 
    131130  bool isAlias = false; 
    132131  if (cit == commands.end()) { 
    133     cit = alias.find(command); 
     132    cit = alias.find(cmd); 
    134133    if (cit == alias.end()) 
    135134      return NSCAPI::returnIgnored; 
     
    157156    return NSCModuleHelper::InjectSplitAndCommand(cd.command, args, ' ', message, perf, true); 
    158157  } else { 
    159     int result = process::executeProcess(root_, cd.command + _T(" ") + args, message, perf, timeout); 
     158    int result = process::executeProcess(process::exec_arguments(root_, cd.command + _T(" ") + args, timeout), message, perf); 
    160159    if (!NSCHelper::isNagiosReturnCode(result)) { 
    161160      NSC_LOG_ERROR_STD(_T("The command (") + cd.command + _T(") returned an invalid return code: ") + strEx::itos(result)); 
  • modules/CheckExternalScripts/CheckExternalScripts.h

    r5da0459 r79e734f  
    3333    std::wstring arguments; 
    3434  }; 
    35   typedef std::map<strEx::blindstr, command_data> command_list; 
     35  typedef std::map<std::wstring, command_data> command_list; 
    3636  command_list commands; 
    3737  command_list alias; 
     
    8181private: 
    8282  void addAllScriptsFrom(std::wstring path); 
    83   void addCommand(strEx::blindstr key, std::wstring cmd, std::wstring args) { 
     83  void addCommand(std::wstring key, std::wstring cmd, std::wstring args) { 
     84    boost::to_lower(key); 
    8485    commands[key] = command_data(cmd, args); 
    8586  } 
    86   void addAlias(strEx::blindstr key, std::wstring cmd, std::wstring args) { 
     87  void addAlias(std::wstring key, std::wstring cmd, std::wstring args) { 
     88    boost::to_lower(key); 
    8789    alias[key] = command_data(cmd, args); 
    8890  } 
  • modules/CheckExternalScripts/stdafx.h

    r7f9c823 r79e734f  
    2121#pragma once 
    2222 
    23 #define WIN32_LEAN_AND_MEAN   // Exclude rarely-used stuff from Windows headers 
    24 // Windows Header Files: 
    25 #include <windows.h> 
    26  
    2723#include <string> 
    2824#include <functional> 
    29  
    3025#include <utils.h> 
    3126 
    3227#include <boost/lexical_cast.hpp> 
    3328 
     29#include <types.hpp> 
    3430#include <NSCAPI.h> 
    3531#include <nsc_module_wrapper.hpp> 
  • modules/CheckHelpers/CheckHelpers.cpp

    rd05c3f0 r79e734f  
    2626 
    2727CheckHelpers gCheckHelpers; 
    28 #ifdef WIN32 
     28#ifdef _WIN32 
    2929BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) 
    3030{ 
  • modules/NRPEClient/NRPEClient.cpp

    r9b3f53c r79e734f  
    3232NRPEClient gNRPEClient; 
    3333 
    34 #ifdef WIN32 
     34#ifdef _WIN32 
    3535BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) 
    3636{ 
  • modules/NRPEClient/stdafx.h

    rdcd90b2 r79e734f  
    2121#pragma once 
    2222 
    23 #ifdef _WIN32 
    24 #define _WIN32_WINNT 0x0400 
    25 #define WIN32_LEAN_AND_MEAN   // Exclude rarely-used stuff from Windows headers 
    26 // Windows Header Files: 
    27 #include <windows.h> 
    28 #include <winsock2.h> 
    29 #endif 
    30  
    3123#include <string> 
    3224#include <functional> 
     
    3527#include <utils.h> 
    3628 
     29#include <types.hpp> 
    3730#include <NSCAPI.h> 
    3831#include <NSCHelper.h> 
  • modules/NRPEListener/NRPEListener.cpp

    r9b3f53c r79e734f  
    2828NRPEListener gNRPEListener; 
    2929 
     30#ifdef _WIN32 
    3031BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) 
    3132{ 
     
    3334  return TRUE; 
    3435} 
     36#endif 
    3537 
    3638NRPEListener::NRPEListener() : noPerfData_(false), buffer_length_(0) { 
  • modules/NRPEListener/nrpe_connection.cpp

    r9b3f53c r79e734f  
    2424    void connection::start() { 
    2525      parser_.set_payload_length(handler_.get_payload_length()); 
    26       std::cout << "Starting..." << std::endl; 
    2726      socket_.async_read_some(boost::asio::buffer(buffer_), 
    2827        strand_.wrap( 
     
    3130          boost::asio::placeholders::bytes_transferred) 
    3231        )); 
    33       std::cout << "Starting...done" << std::endl; 
    3432    } 
    3533 
    3634    void connection::handle_read(const boost::system::error_code& e, std::size_t bytes_transferred) { 
    37       std::cout << "read: " << buffer_.c_array() << ": " << bytes_transferred << "(" << e << ")" << std::endl; 
    3835      if (!e) { 
    3936        bool result; 
     
    103100        socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec); 
    104101      } 
    105  
    106       // No new asynchronous operations are started. This means that all shared_ptr 
    107       // references to the connection object will disappear and the object will be 
    108       // destroyed automatically after this handler returns. The connection class's 
    109       // destructor closes the socket. 
    110102    } 
    111103 
  • modules/NRPEListener/nrpe_handler.cpp

    r9b3f53c r79e734f  
    22#include <boost/asio.hpp> 
    33#include "nrpe_handler.hpp" 
     4#include <NSCHelper.h> 
    45 
    56namespace nrpe { 
    67  namespace server { 
     8    nrpe::packet handler::handle(nrpe::packet p) { 
     9      strEx::token cmd = strEx::getToken(p.getPayload(), '!'); 
     10      if (cmd.first == _T("_NRPE_CHECK")) { 
     11        return nrpe::packet::create_response(NSCAPI::returnOK, _T("I (") + NSCModuleHelper::getApplicationVersionString() + _T(") seem to be doing fine..."), p.get_payload_length()); 
     12      } 
     13      std::wstring msg, perf; 
     14 
     15      if (allowArgs_) { 
     16        if (!cmd.second.empty()) { 
     17          NSC_LOG_ERROR(_T("Request contained arguments (not currently allowed, check the allow_arguments option).")); 
     18          throw nrpe::nrpe_exception(_T("Request contained arguments (not currently allowed, check the allow_arguments option).")); 
     19        } 
     20      } 
     21      if (allowNasty_) { 
     22        if (cmd.first.find_first_of(NASTY_METACHARS) != std::wstring::npos) { 
     23          NSC_LOG_ERROR(_T("Request command contained illegal metachars!")); 
     24          throw nrpe::nrpe_exception(_T("Request command contained illegal metachars!")); 
     25        } 
     26        if (cmd.second.find_first_of(NASTY_METACHARS) != std::wstring::npos) { 
     27          NSC_LOG_ERROR(_T("Request arguments contained illegal metachars!")); 
     28          throw nrpe::nrpe_exception(_T("Request command contained illegal metachars!")); 
     29        } 
     30      } 
     31      //TODO REMOVE THIS 
     32      //return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnUNKNOWN, _T("TEST TEST TEST"), buffer_length_); 
     33 
     34      NSCAPI::nagiosReturn ret = -3; 
     35      try { 
     36        ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first, cmd.second, '!', msg, perf); 
     37      } catch (...) { 
     38        return nrpe::packet::create_response(NSCAPI::returnUNKNOWN, _T("UNKNOWN: Internal exception"), p.get_payload_length()); 
     39      } 
     40      switch (ret) { 
     41      case NSCAPI::returnInvalidBufferLen: 
     42        return nrpe::packet::create_response(NSCAPI::returnUNKNOWN, _T("UNKNOWN: Return buffer to small to handle this command."), p.get_payload_length()); 
     43      case NSCAPI::returnIgnored: 
     44        return nrpe::packet::create_response(NSCAPI::returnUNKNOWN, _T("UNKNOWN: No handler for that command."), p.get_payload_length()); 
     45      case NSCAPI::returnOK: 
     46      case NSCAPI::returnWARN: 
     47      case NSCAPI::returnCRIT: 
     48      case NSCAPI::returnUNKNOWN: 
     49        break; 
     50      default: 
     51        return nrpe::packet::create_response(NSCAPI::returnUNKNOWN, _T("UNKNOWN: Internal error."), p.get_payload_length()); 
     52      } 
     53      if (msg.length() >= p.get_payload_length()-1) { 
     54        NSC_LOG_ERROR(_T("Truncating returndata as it is bigger then NRPE allowes :(")); 
     55        msg = msg.substr(0,p.get_payload_length()-2); 
     56      } 
     57      if (perf.empty()||noPerfData_) { 
     58        return nrpe::packet::create_response(ret, msg, p.get_payload_length()); 
     59      } else { 
     60        return nrpe::packet::create_response(ret, msg + _T("|") + perf, p.get_payload_length()); 
     61      } 
     62    } 
    763  } 
    864} 
  • modules/NRPEListener/nrpe_handler.hpp

    r9b3f53c r79e734f  
    66    class handler { 
    77      unsigned int payload_length_; 
     8      bool allowArgs_; 
     9      bool allowNasty_; 
     10      bool noPerfData_; 
    811    public: 
    912      handler(unsigned int payload_length)  
     
    2023        return payload_length_; 
    2124      } 
     25      nrpe::packet handle(nrpe::packet packet); 
     26      /* 
    2227      nrpe::packet handle(nrpe::packet packet) { 
    2328        return nrpe::packet::create_response(1, _T("HELLO!"), payload_length_); 
    2429      } 
     30      */ 
    2531      nrpe::packet create_error(std::wstring msg) { 
    2632        return nrpe::packet::create_response(4, msg, payload_length_); 
  • modules/NRPEListener/stdafx.h

    r9b3f53c r79e734f  
    2121#pragma once 
    2222 
    23 #define WIN32_LEAN_AND_MEAN   // Exclude rarely-used stuff from Windows headers 
    24 // Windows Header Files: 
    25 #include <windows.h> 
    26  
    2723#include <string> 
    2824#include <functional> 
    2925#include <map> 
    3026#include <vector> 
    31  
    3227#include <config.h> 
    3328#include <utils.h> 
    3429 
     30#include <types.hpp> 
    3531#include <NSCAPI.h> 
    3632#include <NSCHelper.h> 
  • modules/NSClientListener/NSClientListener.h

    r9567d4b r79e734f  
    1919*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * 
    2020***************************************************************************/ 
    21 #include <Socket.h> 
     21//#include <Socket.h> 
    2222#include <string> 
    2323#include <utils.h> 
  • service/StdAfx.h

    rb3078b4 r79e734f  
    2121#pragma once 
    2222#include <types.hpp> 
    23 /* 
    24 #ifdef WIN32 
    25 #define _WINSOCKAPI_ 
    26 #include <tchar.h> 
    27 #define VC_EXTRALEAN    // Exclude rarely-used stuff from Windows headers 
    28 #include <windows.h> 
    29 #endif  
    3023 
    31 #define COMPILE_NEWAPIS_STUBS 
    32 #define WANT_GETLONGPATHNAME_WRAPPER 
    33 //#include <NewAPIs.h> 
    34 */ 
    3524#include <iostream> 
    3625#include <string> 
Note: See TracChangeset for help on using the changeset viewer.