Changeset 2f01f93 in nscp


Ignore:
Timestamp:
02/20/08 16:16:16 (5 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
5b9d247
Parents:
95e4ace
Message:

+ Added new module NRPEClient that can act as a NRPE client, might be useful for testing things and

eventually for relaying events.
Usage: nsclient++ -noboot NRPEClient -H 192.168.0.1 -p 5666 -c check_something -a foo bar
This is an early concept so don't expect much...

Files:
10 edited

Legend:

Unmodified
Added
Removed
  • AutoBuild.h

    r394f7a1 r2f01f93  
    33// change the FALSE to TRUE for autoincrement of build number 
    44#define INCREMENT_VERSION TRUE 
    5 #define FILEVER        0,3,0,58 
    6 #define PRODUCTVER     0,3,0,58 
    7 #define STRFILEVER     _T("0.3.0.58") 
    8 #define STRPRODUCTVER  _T("0.3.0.58") 
    9 #define STRPRODUCTDATE  _T("2008-02-14") 
     5#define FILEVER        0,3,0,60 
     6#define PRODUCTVER     0,3,0,60 
     7#define STRFILEVER     _T("0.3.0.60") 
     8#define STRPRODUCTVER  _T("0.3.0.60") 
     9#define STRPRODUCTDATE  _T("2008-02-19") 
    1010#endif // AUTOBUILD_H 
  • NSClient++.cpp

    r95e4ace r2f01f93  
    107107      LOG_MESSAGE(SZAPPNAME _T(" Version: ") SZVERSION _T(", Plattform: ") SZARCH); 
    108108    } else if ( _wcsicmp( _T("noboot"), argv[1]+1 ) == 0 ) { 
    109       g_bConsoleLog = true; 
     109      mainClient.setBoot(false); 
     110      g_bConsoleLog = false; 
    110111      mainClient.enableDebug(true); 
     112      mainClient.InitiateService(); 
    111113      int nRetCode = -1; 
    112114      if (argc>=4) 
     
    114116      else if (argc>=3) 
    115117        nRetCode = mainClient.commandLineExec(argv[2], argv[3], 0, NULL); 
     118      mainClient.TerminateService(); 
    116119      return nRetCode; 
    117120    } else if ( _wcsicmp( _T("test"), argv[1]+1 ) == 0 ) { 
     
    221224    return false; 
    222225  } 
    223   try { 
    224     SettingsT::sectionList list = Settings::getInstance()->getSection(_T("modules")); 
    225     for (SettingsT::sectionList::iterator it = list.begin(); it != list.end(); it++) { 
    226       try { 
    227         loadPlugin(getBasePath() + _T("modules\\") + (*it)); 
    228       } catch(const NSPluginException& e) { 
    229         LOG_ERROR_STD(_T("Exception raised: ") + e.error_ + _T(" in module: ") + e.file_); 
    230         //return false; 
    231       } catch (...) { 
    232         LOG_ERROR_STD(_T("Unknown exception loading plugin: ") + (*it)); 
    233         return false; 
    234       } 
    235     } 
    236   } catch (SettingsException e) { 
    237     NSC_LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage()); 
    238   } 
    239   try { 
    240     loadPlugins(); 
    241   } catch (...) { 
    242     LOG_ERROR_STD(_T("Unknown exception loading plugins")); 
    243     return false; 
     226  if (boot_) { 
     227    try { 
     228      SettingsT::sectionList list = Settings::getInstance()->getSection(_T("modules")); 
     229      for (SettingsT::sectionList::iterator it = list.begin(); it != list.end(); it++) { 
     230        try { 
     231          loadPlugin(getBasePath() + _T("modules\\") + (*it)); 
     232        } catch(const NSPluginException& e) { 
     233          LOG_ERROR_STD(_T("Exception raised: ") + e.error_ + _T(" in module: ") + e.file_); 
     234          //return false; 
     235        } catch (...) { 
     236          LOG_ERROR_STD(_T("Unknown exception loading plugin: ") + (*it)); 
     237          return false; 
     238        } 
     239      } 
     240    } catch (SettingsException e) { 
     241      NSC_LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage()); 
     242    } 
     243    try { 
     244      loadPlugins(); 
     245    } catch (...) { 
     246      LOG_ERROR_STD(_T("Unknown exception loading plugins")); 
     247      return false; 
     248    } 
    244249  } 
    245250  return true; 
     
    250255 */ 
    251256void NSClientT::TerminateService(void) { 
    252   try { 
    253     mainClient.unloadPlugins(); 
    254   } catch(NSPluginException &e) { 
    255     std::wcout << _T("Exception raised: ") << e.error_ << _T(" in module: ") << e.file_ << std::endl;; 
     257  if (boot_) { 
     258    try { 
     259      mainClient.unloadPlugins(); 
     260    } catch(NSPluginException &e) { 
     261      std::wcout << _T("Exception raised: ") << e.error_ << _T(" in module: ") << e.file_ << std::endl;; 
     262    } 
    256263  } 
    257264  try { 
     
    307314    } 
    308315  } 
    309   LOG_MESSAGE_STD(_T("Module was not loaded, attempt to load it")); 
    310316  try { 
    311317    plugin_type plugin = loadPlugin(getBasePath() + _T("modules\\") + module); 
  • NSClient++.h

    r394f7a1 r2f01f93  
    7272  typedef enum log_status {log_unknown, log_debug, log_nodebug }; 
    7373  log_status debug_; 
     74  bool boot_; 
    7475 
    7576public: 
    7677  // c-tor, d-tor 
    77   NSClientT(void) : debug_(log_unknown) {} 
     78  NSClientT(void) : debug_(log_unknown), boot_(true) {} 
    7879  virtual ~NSClientT(void) {} 
    7980  void enableDebug(bool debug = true) { 
     
    8283    else 
    8384      debug_ = log_nodebug; 
     85  } 
     86  void setBoot(bool boot = true) { 
     87    boot_ = boot; 
    8488  } 
    8589 
  • changelog

    r95e4ace r2f01f93  
    33 * Fix configuration GUI (low priority) 
    44 * Fix installer (after configuration UI) 
    5  * Add module for relaying events 
     5 * Add module for relaying events (in progress) 
    66 * Add API for rehashing the daemon (or implement it the API is there but does nothing) 
     7 * Improved socket performance (would be nice if we could be used as a "hub") 
     8 
     92008-02-19 MickeM 
     10 + Added new module NRPEClient that can act as a NRPE client, might be useful for testing things and  
     11   eventually for relaying events. 
     12   Usage: nsclient++ -noboot NRPEClient -H 192.168.0.1 -p 5666 -c check_something -a foo bar 
     13   This is an early concept so don't expect much... 
    714 
    8152008-02-19 MickeM 
     
    1017  
    11182008-02-18 MickeM 
    12  + Added propper output handling to process subsystem (now you can execute programs tat return "much" data. 
     19 + Added proper output handling to process subsystem (now you can execute programs tat return "much" data. 
    1320 + Added select support for SSL_write (now you can send "any amount of data" to the (SSL) socket. 
    1421   Since check_nrpe doesn't do this it wont work in that end, but still... 
    1522 
    16232008-02-16 MickeM 
    17  + Refactored ExternalCommand handling so NRPE and new module does the same thing. 
     24 + Re factored ExternalCommand handling so NRPE and new module does the same thing. 
    1825 
    19262008-02-14 MickeM 
     
    23302008-02-13 MickeM 
    2431 + Added string_length to [Settings] as well (used internally) for all "injected" buffers. 
    25  * Fixed issue with scripts result truncated efter 1024 chars  
    26     (now they return "all" output and thus you can use the NRPE settings I adde yesterday :) 
    27  + Added hostname setting to [NSCA] section (must have been braindead when I did not add it before) 
     32 * Fixed issue with scripts result truncated after 1024 chars  
     33    (now they return "all" output and thus you can use the NRPE settings I added yesterday :) 
     34 + Added hostname setting to [NSCA] section (must have been when I did not add it before) 
    2835 + Added to NSCA truncates output when to long. 
    2936 
     
    3441 
    35422008-02-11 MickeM 
    36  + Added encryption support for NSCA module (about half of the algorithms are avalible,  
     43 + Added encryption support for NSCA module (about half of the algorithms are available,  
    3744     if someone wants to use one not available let me know, and I will try to add it) 
    3845 
     
    4855 
    49562008-02-07 MickeM 
    50  + Added default catch handlers to all wrapped plugin calls. 
     57 + Added default catch handlers to all wrapped plug in calls. 
    5158 
    52592008-02-05 MickeM 
  • include/SSLSocket.cpp

    r394f7a1 r2f01f93  
    134134} 
    135135 
    136 void simpleSSL::Listener::StartListener(std::wstring host, int port, unsigned int listenQue) { 
    137   // @todo init SSL 
     136int simpleSSL::Socket::connect_() { 
    138137  simpleSSL::SSL_init(); 
    139138 
    140   context.createSSLv23(); 
     139  Context context; 
     140  context.createSSLv23Client(); 
     141  context.setCipherList(); 
     142  /* 
     143  simpleSSL::DH dh; 
     144  dh.create(); 
     145  setupDH(dh); 
     146  context.setTmpDH(dh.getDH()); 
     147  dh.free(); 
     148  */ 
     149  if (tBase::connect_() == SOCKET_ERROR) { 
     150    throw simpleSocket::SocketException(_T("Failed to connect to host: ") + inet_ntoa(to_.sin_addr.s_addr), WSAGetLastError()); 
     151  } 
     152  ssl.setContext(context); 
     153  ssl.set_fd(socket_); 
     154  ssl.connect(); 
     155  return 0; 
     156} 
     157 
     158 
     159void simpleSSL::Listener::StartListener(std::wstring host, int port, unsigned int listenQue) { 
     160  simpleSSL::SSL_init(); 
     161  context.createSSLv23Server(); 
    141162  context.setCipherList(); 
    142163  simpleSSL::DH dh; 
     
    145166  context.setTmpDH(dh.getDH()); 
    146167  dh.free(); 
    147  
    148168  if (!lock_cs) { 
    149169    lock_cs_count = simpleSSL::Crypto::getNumberOfLocks(); 
  • include/SSLSocket.h

    r394f7a1 r2f01f93  
    112112 
    113113    void destroy() { 
    114       assert(ctx_); 
     114      if (ctx_ != NULL) 
     115        throw SSLException(_T("Error: SSL Context already initalized.")); 
    115116      SSL_CTX_free(ctx_); 
    116117      ctx_ = NULL; 
    117118    } 
    118     void createSSLv23() { 
    119       assert(ctx_ == NULL); 
     119    void createSSLv23Server() { 
     120      if (ctx_ != NULL) 
     121        throw SSLException(_T("Error: SSL Context already initalized.")); 
    120122      ctx_ = SSL_CTX_new(SSLv23_server_method()); 
    121123      if (ctx_ == NULL) { 
     
    123125      } 
    124126    } 
     127    void createSSLv23Client() { 
     128      if (ctx_ != NULL) 
     129        throw SSLException(_T("Error: SSL Context already initalized.")); 
     130      ctx_ = SSL_CTX_new(SSLv23_client_method()); 
     131      if (ctx_ == NULL) 
     132        throw SSLException(_T("Error: could not create SSL context.")); 
     133      SSL_CTX_set_options(ctx_,SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); 
     134    } 
    125135    void setCipherList(std::string s = "ADH") { 
    126       assert(ctx_); 
     136      if (ctx_ == NULL) 
     137        throw SSLException(_T("Error: SSL Context already initalized.")); 
    127138      SSL_CTX_set_cipher_list(ctx_, s.c_str()); 
    128139    } 
     
    169180    } 
    170181    void create() { 
    171       assert(!ssl_); 
     182      if (ssl_ != NULL)  
     183        throw SSLException(_T("Error: Could not create SSL connection structure, SSL is already inatialized.")); 
    172184      ssl_ = context_.newSSL(); 
    173185      if (ssl_ == NULL)  
     
    186198      int i = 0; 
    187199      while ((rc = SSL_accept(ssl_)) != 1) { 
     200        if (++i >= 100) { 
     201          throw SSLException(_T("SSL: Could not complete SSL handshake.")); 
     202        } 
     203        int rc2 = getError(rc); 
     204        if ((rc2 == SSL_ERROR_WANT_READ) || (rc2 == SSL_ERROR_WANT_WRITE)) { 
     205          Sleep(100); 
     206          continue; 
     207        } else { 
     208          throw SSLException(_T("Error: Could not complete SSL handshake : "), rc, rc2); 
     209        } 
     210      } 
     211      /**/ 
     212    } 
     213    void connect() { 
     214      if (!ssl_) 
     215        create(); 
     216      int rc = 0; 
     217      int i = 0; 
     218      while ((rc = SSL_connect(ssl_)) != 1) { 
    188219        if (++i >= 100) { 
    189220          throw SSLException(_T("SSL: Could not complete SSL handshake.")); 
     
    244275    Socket(Socket &other) : tBase(other), ssl(other.ssl) { 
    245276    } 
     277    Socket(bool create) : tBase(create) { 
     278    } 
    246279    virtual ~Socket() { 
    247280      ssl.shutdown(); 
     
    302335      tBase::close(); 
    303336    } 
     337    virtual int connect(std::wstring host, u_short port) { 
     338      return tBase::connect(host, port); 
     339    } 
     340 
     341    virtual int connect_(); 
    304342    void setContext(Context c) { 
    305343      ssl.setContext(c); 
  • include/Socket.h

    r394f7a1 r2f01f93  
    211211        to_.sin_port = htons(port); 
    212212        to_.sin_addr.s_addr = inet_addr(host); 
    213         return ::connect(socket_, (SOCKADDR*) &to_, sizeof(to_)); 
     213        return connect_(); 
    214214      } 
    215215      return SOCKET_ERROR; 
     216    } 
     217    virtual int connect_() { 
     218      return ::connect(socket_, (SOCKADDR*) &to_, sizeof(to_)); 
    216219    } 
    217220 
     
    252255      else 
    253256        ::select(NULL, &read_, &write_, &excp_, &timeout_); 
    254       return FD_ISSET(socket_, &write_); 
     257      if (FD_ISSET(socket_, &write_)) 
     258        return true; 
     259      return false; 
    255260    } 
    256261 
  • include/strEx.h

    r394f7a1 r2f01f93  
    3535 
    3636namespace strEx { 
     37  namespace s { 
     38    inline std::string itos(float i) { 
     39      std::stringstream ss; 
     40      ss << i; 
     41      return ss.str(); 
     42    } 
     43    inline std::string itos(int i) { 
     44      std::stringstream ss; 
     45      ss << i; 
     46      return ss.str(); 
     47    } 
     48    inline std::string itos(unsigned int i) { 
     49      std::stringstream ss; 
     50      ss << i; 
     51      return ss.str(); 
     52    } 
     53  } 
    3754 
    3855  inline void append_list(std::wstring &lst, std::wstring &append) { 
  • modules/NRPEClient/NRPEClient.cpp

    r394f7a1 r2f01f93  
    6666 
    6767int NRPEClient::commandLineExec(const TCHAR* command, const unsigned int argLen, TCHAR** args) { 
    68   if (_wcsicmp(command, _T("check")) == 0) { 
    69     std::wcout << args[0] << std::endl; 
    70  
    71  
    7268    try { 
    7369      boost::program_options::options_description desc("Allowed options"); 
     70      buffer_length_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_STRLEN, NRPE_SETTINGS_STRLEN_DEFAULT); 
    7471      desc.add_options() 
    75         ("help", "Show this help message.") 
    76         ("host", boost::program_options::value<std::string>(), "remote NRPE host") 
    77         ("port", boost::program_options::value<int>(), "remote NRPE port") 
    78         ("-c", "command to execute") 
    79         ("-a", "list of arguments") 
    80         ("compression", boost::program_options::value<int>(), "set compression level") 
     72        ("help,h", "Show this help message.") 
     73        ("host,H", boost::program_options::wvalue<std::wstring>(), "The address of the host running the NRPE daemon") 
     74        ("port,p", boost::program_options::value<int>(), "The port on which the daemon is running (default=5666)") 
     75        ("command,c", boost::program_options::wvalue<std::wstring>(), "The name of the command that the remote daemon should run") 
     76        ("timeout,t", boost::program_options::value<int>(), "Number of seconds before connection times out (default=10)") 
     77        ("buffer-length,l", boost::program_options::value<int>(), std::string("Length of payload (has to be same as on the server (default=" + strEx::s::itos(buffer_length_) + ")").c_str()) 
     78        ("no-ssl,n", "Do not initial an ssl handshake with the server, talk in plaintext.") 
     79        ("arguments,a", boost::program_options::wvalue<std::vector<std::wstring>>(), "list of arguments") 
    8180        ; 
    82  
     81      boost::program_options::positional_options_description p; 
     82      p.add("arguments", -1); 
     83       
    8384      boost::program_options::variables_map vm; 
    84       boost::program_options::store(basic_command_line_parser_ex<wchar_t>::parse_command_line(argLen, args, desc, 0), vm); 
     85      boost::program_options::store( 
     86        basic_command_line_parser_ex<TCHAR>(command, argLen, args).options(desc).positional(p).run() 
     87        , vm); 
    8588      boost::program_options::notify(vm);     
    8689 
     
    8992        return 1; 
    9093      } 
    91  
    92       if (vm.count("host")) { 
    93         std::cout << "Host level was set to "  
    94           << vm["host"].as<std::string>() << ".\n"; 
    95         std::cout << "Port level was set to "  
    96           << vm["port"].as<int>() << ".\n"; 
    97       } else { 
    98         std::cout << "Compression level was not set.\n"; 
     94      std::wstring host = _T("localhost"); 
     95      std::wstring command; 
     96      std::wstring arguments; 
     97      int port = 5666; 
     98      int timeout = 10; 
     99      bool ssl = true; 
     100 
     101      if (vm.count("host")) 
     102        host = vm["host"].as<std::wstring>(); 
     103      if (vm.count("port")) 
     104        port = vm["port"].as<int>(); 
     105      if (vm.count("timeout")) 
     106        timeout = vm["timeout"].as<int>(); 
     107      if (vm.count("buffer-length")) 
     108        buffer_length_ = vm["buffer-length"].as<int>(); 
     109      if (vm.count("command")) 
     110        command = vm["command"].as<std::wstring>(); 
     111      if (vm.count("arguments")) { 
     112        std::vector<std::wstring> v = vm["arguments"].as<std::vector<std::wstring>>(); 
     113        for (std::vector<std::wstring>::const_iterator cit = v.begin(); cit != v.end(); ++cit) { 
     114          if (!arguments.empty()) 
     115            arguments += _T("!"); 
     116          arguments += *cit; 
     117        } 
    99118      } 
     119      if (vm.count("no-ssl")) 
     120        ssl = false; 
     121      return execute_nrpe_command(host, port, ssl, timeout, command, arguments); 
    100122    } catch (boost::program_options::validation_error &e) { 
    101123      std::cout << e.what() << std::endl; 
     
    103125      std::cout << "Unknown exception parsing command line" << std::endl; 
    104126    } 
    105     std::wcout << _T("Checking...") << std::endl; 
    106   } 
    107127  return 0; 
    108128} 
     129int NRPEClient::execute_nrpe_command(std::wstring host, int port, bool ssl, int timeout, std::wstring command, std::wstring arguments) { 
     130  try { 
     131    std::wstring cmd = command; 
     132    if (cmd.empty()) 
     133      cmd = _T("_NRPE_CHECK"); 
     134    if (!arguments.empty()) 
     135      cmd += _T("!") + arguments; 
     136    NRPEPacket packet; 
     137    if (ssl) 
     138      packet = send_ssl(host, port, timeout, NRPEPacket::make_request(cmd, buffer_length_)); 
     139    else 
     140      packet = send_nossl(host, port, timeout, NRPEPacket::make_request(cmd, buffer_length_)); 
     141    std::wcout << packet.getPayload() << std::endl; 
     142    return packet.getResult(); 
     143  } catch (simpleSocket::SocketException &e) { 
     144    std::wcout << _T("whoops...") << e.getMessage() <<  std::endl; 
     145  } catch (simpleSSL::SSLException &e) { 
     146    std::wcout << _T("whoops...") << e.getMessage() <<  std::endl; 
     147  } catch (...) { 
     148    std::cout << "whoops..." << std::endl; 
     149  } 
     150  return NSCAPI::returnUNKNOWN; 
     151} 
     152NRPEPacket NRPEClient::send_ssl(std::wstring host, int port, int timeout, NRPEPacket packet) 
     153{ 
     154  simpleSSL::Socket socket(true); 
     155  socket.connect(host, port); 
     156  socket.sendAll(packet.getBuffer(), packet.getBufferLength()); 
     157  simpleSocket::DataBuffer buffer; 
     158  socket.readAll(buffer); 
     159  packet.readFrom(buffer.getBuffer(), buffer.getLength()); 
     160  return packet; 
     161} 
     162NRPEPacket NRPEClient::send_nossl(std::wstring host, int port, int timeout, NRPEPacket packet) 
     163{ 
     164  simpleSocket::Socket socket(true); 
     165  socket.connect(host, port); 
     166  socket.sendAll(packet.getBuffer(), packet.getBufferLength()); 
     167  simpleSocket::DataBuffer buffer; 
     168  socket.readAll(buffer); 
     169  packet.readFrom(buffer.getBuffer(), buffer.getLength()); 
     170  return packet; 
     171} 
     172 
     173 
    109174 
    110175 
  • modules/NRPEClient/NRPEClient.h

    r394f7a1 r2f01f93  
    7575 
    7676private: 
     77  int execute_nrpe_command(std::wstring host, int port, bool ssl, int timeout, std::wstring command, std::wstring arguments); 
     78  NRPEPacket send_nossl(std::wstring host, int port, int timeout, NRPEPacket packet); 
     79  NRPEPacket send_ssl(std::wstring host, int port, int timeout, NRPEPacket packet); 
    7780  class NRPEException { 
    7881    std::wstring error_; 
Note: See TracChangeset for help on using the changeset viewer.