Changeset 465866c in nscp


Ignore:
Timestamp:
06/05/12 07:35:30 (13 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.1, 0.4.2
Children:
742b035
Parents:
695f240
Message:

2012-06-05 MickeM

  • Tweaked all servers to use the new internals and added first testcase for NSCP socket

2012-05-24 MickeM

  • Reworked real time event log support to be a lot more flexible You can now specify all options on a "filter" level.
  • WARNING* Old syntax NOT supported (and will not upgrade) but hopefully not to many will be affected.
  • Added support for ipv6 allowed hosts validation

2012-05-21 MickeM

  • Sofia Born (My second daughter)
Files:
3 added
11 deleted
45 edited

Legend:

Unmodified
Added
Removed
  • changelog

    r695f240 r465866c  
    44 * Fixa dependonservice LanManWorkStation (old win) 
    55 * Fix RtlStringFromGUID problem on NT4 
     6 
     72012-05-19 MickeM 
     8 * Tweaked all servers to use the new internals and added first testcase for NSCP socket 
     9 
     102012-05-24 MickeM 
     11 * Reworked real time event log support to be a lot more flexible 
     12   You can now specify all options on a "filter" level. 
     13 * WARNING* 
     14   Old syntax NOT supported (and will not upgrade) but hopefully not to many will be affected. 
     15 * Added support for ipv6 allowed hosts validation 
     16 
     172012-05-21 MickeM 
     18 * Sofia Born (My second daughter) 
    619 
    7202012-05-19 MickeM 
  • include/nsca/client/nsca_client_protocol.hpp

    raf05fa1 r465866c  
    9999  } 
    100100} 
    101  
    102  
    103 /* 
    104 #pragma once 
    105  
    106 #include <boost/shared_ptr.hpp> 
    107 #include <boost/asio.hpp> 
    108  
    109 #include <socket/socket_helpers.hpp> 
    110  
    111 #include <nsca/nsca_packet.hpp> 
    112 #include <nsca/nsca_enrypt.hpp> 
    113  
    114 using boost::asio::ip::tcp; 
    115  
    116 namespace nsca { 
    117  
    118   class socket : public boost::noncopyable { 
    119   private: 
    120     boost::shared_ptr<tcp::socket> socket_; 
    121     boost::asio::io_service &io_service_; 
    122     nsca_encrypt crypt_inst; 
    123     int time; 
    124   public: 
    125     typedef boost::asio::basic_socket<tcp,boost::asio::stream_socket_service<tcp> >  basic_socket_type; 
    126  
    127   public: 
    128     socket(boost::asio::io_service &io_service) : io_service_(io_service), time(0) { 
    129       socket_.reset(new tcp::socket(io_service_)); 
    130     } 
    131     ~socket() { 
    132       if (socket_) 
    133         socket_->close(); 
    134       socket_.reset(); 
    135     } 
    136  
    137     virtual void connect(std::string host, std::string port) { 
    138       NSC_DEBUG_MSG(_T("Connecting to: ") + utf8::cvt<std::wstring>(host) + _T(" (") + utf8::cvt<std::wstring>(port) + _T(")")); 
    139       tcp::resolver resolver(io_service_); 
    140       tcp::resolver::query query(host, port); 
    141  
    142       tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); 
    143       tcp::resolver::iterator end; 
    144  
    145       boost::system::error_code error = boost::asio::error::host_not_found; 
    146       while (error && endpoint_iterator != end) { 
    147         tcp::resolver::endpoint_type ep = *endpoint_iterator; 
    148         socket_->close(); 
    149         socket_->connect(*endpoint_iterator++, error); 
    150         NSC_DEBUG_MSG(_T("Connected to: ") + utf8::cvt<std::wstring>(ep.address().to_string())); 
    151       } 
    152       if (error) { 
    153         NSC_DEBUG_MSG(_T("Failed to connect to:") + utf8::to_unicode(host)); 
    154         throw boost::system::system_error(error); 
    155       } 
    156     } 
    157  
    158  
    159     virtual void shutdown() { 
    160       NSC_DEBUG_MSG(_T("Ending socket (gracefully)")); 
    161       // Initiate graceful connection closure. 
    162       boost::system::error_code ignored_ec; 
    163       if (socket_) 
    164         socket_->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec); 
    165     }; 
    166     virtual void close() { 
    167       if (socket_) 
    168         socket_->close(); 
    169       socket_.reset(); 
    170     }; 
    171  
    172     virtual void send_nsca(const nsca::packet &packet, const boost::posix_time::seconds timeout) { 
    173       if (!socket_ || !socket_->is_open()) { 
    174         NSC_LOG_ERROR_STD(_T("Socket was closed when trying to send data...")); 
    175         return; 
    176       } 
    177       std::string buffer = crypt_inst.get_rand_buffer(packet.get_packet_length()); 
    178       packet.get_buffer(buffer, time); 
    179       crypt_inst.encrypt_buffer(buffer); 
    180       NSC_DEBUG_MSG(_T("Sending data: ") + strEx::itos(buffer.size())); 
    181       write_with_timeout(buffer, timeout); 
    182     } 
    183     virtual bool recv_iv(std::string password, int encryption_method, boost::posix_time::seconds timeout) { 
    184       if (!socket_ || !socket_->is_open()) { 
    185         NSC_LOG_ERROR_STD(_T("Socket was closed when trying to read data...")); 
    186         return false; 
    187       } 
    188       unsigned int len = nsca::length::iv::get_packet_length(); 
    189       std::vector<char> buf(len); 
    190       if (!read_with_timeout(buf, timeout)) { 
    191         NSC_LOG_ERROR_STD(_T("Failed to read IV from server (using ") + strEx::itos(encryption_method) + _T(", ") + strEx::itos(len) + _T(").")); 
    192         return false; 
    193       } 
    194       nsca::iv_packet iv_packet(std::string(buf.begin(), buf.end())); 
    195       std::string iv = iv_packet.get_iv(); 
    196       time = iv_packet.get_time(); 
    197       NSC_DEBUG_MSG(_T("Encrypting using: ") + utf8::cvt<std::wstring>(nsca::nsca_encrypt::helpers::encryption_to_string(encryption_method)) + _T(", password '") + utf8::cvt<std::wstring>(password) + _T("'")); 
    198       crypt_inst.encrypt_init(password, encryption_method, iv); 
    199       return true; 
    200     } 
    201     virtual bool read_with_timeout(std::vector<char> &buf, boost::posix_time::seconds timeout) { 
    202       return socket_helpers::io::read_with_timeout(*socket_, *socket_, boost::asio::buffer(buf), timeout); 
    203     } 
    204     virtual void write_with_timeout(std::string &buf, boost::posix_time::seconds timeout) { 
    205       socket_helpers::io::write_with_timeout(*socket_, *socket_, boost::asio::buffer(buf), timeout); 
    206     } 
    207   }; 
    208 } 
    209 */ 
  • include/nscapi/macros.hpp

    r8d89d7a r465866c  
    3737#define NSC_LOG_ERROR_STD(msg) if (GET_CORE()->should_log(NSCAPI::log_level::error)) { NSC_ANY_MSG((std::wstring)msg, NSCAPI::log_level::error); } 
    3838#define NSC_LOG_ERROR(msg) if (GET_CORE()->should_log(NSCAPI::log_level::error)) { NSC_ANY_MSG(msg, NSCAPI::log_level::error); } 
     39#define NSC_LOG_ERROR_LISTW(lst) if (GET_CORE()->should_log(NSCAPI::log_level::error)) { BOOST_FOREACH(const std::wstring &s, lst) { NSC_ANY_MSG(s, NSCAPI::log_level::error); } } 
     40#define NSC_LOG_ERROR_LISTS(lst) if (GET_CORE()->should_log(NSCAPI::log_level::error)) { BOOST_FOREACH(const std::string &s, lst) { NSC_ANY_MSG(s, NSCAPI::log_level::error); } } 
    3941 
    4042#define NSC_LOG_CRITICAL_STD(msg) if (GET_CORE()->should_log(NSCAPI::log_level::critical)) { NSC_ANY_MSG((std::wstring)msg, NSCAPI::log_level::critical); } 
  • include/nscapi/nscapi_core_helper.cpp

    ree52cdd r465866c  
    4040} 
    4141 
    42 bool nscapi::core_helper::submit_simple_message(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring & message, std::wstring & perf, std::wstring & response) { 
     42bool nscapi::core_helper::submit_simple_message(const std::wstring channel, const std::wstring command, const NSCAPI::nagiosReturn code, const std::wstring & message, const std::wstring & perf, std::wstring & response) { 
    4343  std::string request, buffer; 
    4444  nscapi::functions::create_simple_submit_request(channel, command, code, message, perf, request); 
  • include/nscapi/nscapi_core_helper.hpp

    ree52cdd r465866c  
    3131 
    3232    NSCAPI::nagiosReturn exec_simple_command(const std::wstring target, const std::wstring command, const std::list<std::wstring> &argument, std::list<std::wstring> & result); 
    33     bool submit_simple_message(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring & message, std::wstring & perf, std::wstring & response); 
     33    bool submit_simple_message(const std::wstring channel, const std::wstring command, const NSCAPI::nagiosReturn code, const std::wstring & message, const std::wstring & perf, std::wstring & response); 
    3434  }; 
    3535}; 
  • include/nscapi/settings_object.hpp

    r67c6d04 r465866c  
    4949          return; 
    5050        add(proxy, path, alias, value, is_template); 
     51      } 
     52 
     53      std::list<std::wstring> get_object_key_list() const { 
     54        std::list<std::wstring> ret; 
     55        BOOST_FOREACH(const typename object_list_type::value_type &t, object_list) { 
     56          ret.push_back(t.first); 
     57        } 
     58        return ret; 
     59      } 
     60      std::list<t_object_type> get_object_list() const { 
     61        std::list<t_object_type> ret; 
     62        BOOST_FOREACH(const typename object_list_type::value_type &t, object_list) { 
     63          ret.push_back(t.second); 
     64        } 
     65        return ret; 
     66      } 
     67      bool has_objects() const { 
     68        return !object_list.empty(); 
    5169      } 
    5270 
  • include/nscp/packet.hpp

    ra629015 r465866c  
    203203    ////////////////////////////////////////////////////////////////////////// 
    204204    // Write to string 
    205     std::string write_string() const { 
    206       std::string ret; 
     205    std::vector<char> write_string() const { 
     206      std::vector<char> ret; 
    207207      write_signature(ret); 
    208208      write_header(ret); 
     
    228228      sig.AppendToString(&buffer); 
    229229    } 
    230     void write_signature(std::string &buffer) const { 
     230    template<class T> 
     231    void write_signature(T &buffer) const { 
    231232      nscp::data::tcp_signature_data data = signature; 
    232       buffer.append(reinterpret_cast<char*>(&data), length::get_signature_size()); 
     233      char * begin = reinterpret_cast<char*>(&data); 
     234      char *end = &begin[length::get_signature_size()]; 
     235      buffer.insert(buffer.end(), begin, end); 
    233236    } 
    234237    std::string write_header() const { 
     
    237240      return buffer; 
    238241    } 
    239     inline void write_header(std::string &buffer) const { 
     242    template<class T> 
     243    inline void write_header(T &buffer) const { 
    240244      if (!header.empty()) 
    241245        buffer.insert(buffer.end(), header.begin(), header.end()); 
     
    246250      return buffer; 
    247251    } 
    248     inline void write_payload(std::string &buffer) const { 
     252    template<class T> 
     253    inline void write_payload(T &buffer) const { 
    249254      if (!payload.empty()) 
    250255        buffer.insert(buffer.end(), payload.begin(), payload.end()); 
  • include/nscp/server/handler.hpp

    r2b2e9b8 r465866c  
    66namespace nscp { 
    77  namespace server { 
    8     class server_handler { 
    9     private: 
    10       server_handler(const server_handler &other) {} 
    11       server_handler& operator= (const server_handler &other) { 
    12         return *this; 
    13       } 
     8    class handler : public boost::noncopyable { 
    149    public: 
    15       server_handler() {} 
    1610      virtual nscp::packet process(const nscp::packet &packet) = 0; 
    17       virtual std::list<nscp::packet> process_all(const std::list<nscp::packet> &packet) = 0; 
    1811 
    19       virtual void log_debug(std::string file, int line, std::wstring msg) = 0; 
    20       virtual void log_error(std::string file, int line, std::wstring msg) = 0; 
     12      virtual void log_debug(std::string module, std::string file, int line, std::string msg) const = 0; 
     13      virtual void log_error(std::string module, std::string file, int line, std::string msg) const = 0; 
    2114      virtual nscp::packet create_error(std::wstring msg) = 0; 
     15 
    2216 
    2317    }; 
    2418  }// namespace server 
    25 } // namespace nrpe 
     19} // namespace nscp 
  • include/nscp/server/parser.hpp

    r2b2e9b8 r465866c  
    1010namespace nscp { 
    1111  namespace server { 
     12 
    1213    class parser : public boost::noncopyable { 
    1314      std::vector<char> buffer_; 
    14       boost::shared_ptr<nscp::server::server_handler> handler_; 
    1515    public: 
    16       parser(boost::shared_ptr<nscp::server::server_handler> handler) : handler_(handler) {} 
    17  
    18       typedef boost::function<boost::tuple<bool, char*>(parser*, char*, char*)> digest_function; 
    1916 
    2017      template <typename InputIterator> 
     
    2623      } 
    2724 
    28       boost::tuple<bool, char*> digest_signature(char* begin, char* end) { 
     25      template <typename InputIterator> 
     26      boost::tuple<bool, InputIterator> digest_signature(InputIterator begin, InputIterator end) { 
    2927        return digest_anything(begin, end, nscp::length::get_signature_size()); 
    3028      } 
    3129 
    3230      template <typename InputIterator> 
    33       InputIterator digest_header(InputIterator begin, InputIterator end, const nscp::data::tcp_signature_data &signature) { 
     31      boost::tuple<bool, InputIterator> digest_header(InputIterator begin, InputIterator end, const nscp::data::tcp_signature_data &signature) { 
    3432        return digest_anything(begin, end, nscp::length::get_header_size(signature)); 
    3533      } 
    3634 
    37       boost::tuple<bool, char*> digest_payload(char* begin, char* end, const nscp::data::tcp_signature_data &signature) { 
     35      template <typename InputIterator> 
     36      boost::tuple<bool, InputIterator> digest_payload(InputIterator begin, InputIterator end, const nscp::data::tcp_signature_data &signature) { 
    3837        return digest_anything(begin, end, nscp::length::get_payload_size(signature)); 
    3938      } 
    4039 
    41       nscp::data::tcp_signature_data parse_signature() { 
     40      void parse_signature(nscp::packet &packet) { 
    4241        assert(buffer_.size() >= nscp::length::get_signature_size()); 
    4342        nscp::data::tcp_signature_data *tmp = reinterpret_cast<nscp::data::tcp_signature_data*>(&(*buffer_.begin())); 
    44         nscp::data::tcp_signature_data signature = *tmp; 
     43        packet.read_signature(tmp); 
    4544        buffer_.clear(); 
    46         return signature; 
    4745      } 
    48       void parse_header(const nscp::data::tcp_signature_data &signature) { 
    49         unsigned long wanted = nscp::length::get_header_size(signature); 
     46      void parse_header(nscp::packet &packet) { 
     47        unsigned long wanted = nscp::length::get_header_size(packet.signature); 
    5048        if (wanted == 0) 
    5149          return; 
     
    6361      } 
    6462    }; 
     63 
     64 
     65    struct digester : public boost::noncopyable { 
     66      enum state { 
     67        need_signature, 
     68        need_header, 
     69        need_payload, 
     70      }; 
     71 
     72      parser parser_; 
     73      state current_state_; 
     74      nscp::packet packet_; 
     75 
     76 
     77      void reset() { 
     78        current_state_ = need_signature; 
     79      } 
     80 
     81      unsigned long long get_next_size() { 
     82        if (current_state_ == need_signature) { 
     83          return nscp::length::get_signature_size(); 
     84        } else if (current_state_ == need_header) { 
     85          return nscp::length::get_header_size(packet_.signature); 
     86        } else if (current_state_ == need_payload) { 
     87          return nscp::length::get_payload_size(packet_.signature); 
     88        } 
     89        return 0; 
     90      } 
     91 
     92      template<typename iterator_type> 
     93      boost::tuple<bool, iterator_type> digest(iterator_type begin, iterator_type end) { 
     94        bool result = false; 
     95        if (current_state_ == need_signature) { 
     96          boost::tie(result, begin) = parser_.digest_signature(begin, end); 
     97          if (result) { 
     98            parser_.parse_signature(packet_); 
     99            current_state_ = need_header; 
     100          } else  
     101            return boost::make_tuple(false, begin); 
     102        } 
     103        if (current_state_ == need_header) { 
     104          boost::tie(result, begin) = parser_.digest_header(begin, end, packet_.signature); 
     105          if (result) { 
     106            parser_.parse_header(packet_); 
     107            current_state_ = need_payload; 
     108          } else 
     109            return boost::make_tuple(false, begin); 
     110        } 
     111        if (current_state_ == need_payload) { 
     112          boost::tie(result, begin) = parser_.digest_payload(begin, end, packet_.signature); 
     113          if (result) { 
     114            parser_.parse_payload(packet_); 
     115            current_state_ = need_signature; 
     116          } 
     117          return boost::make_tuple(result, begin); 
     118        } 
     119        return boost::make_tuple(result, begin); 
     120      } 
     121      nscp::packet get_packet() const { return packet_; } 
     122    }; 
    65123  }// namespace server 
    66124} // namespace nscp 
  • include/settings/client/settings_client.cpp

    r76540c3 r465866c  
    2525    boost::shared_ptr<wpath_key_type> wpath_key(std::wstring *val) { 
    2626      boost::shared_ptr<wpath_key_type> r(new wpath_key_type(val, _T(""), false)); 
     27      return r; 
     28    } 
     29    boost::shared_ptr<path_key_type> path_key(std::string *val, std::string def) { 
     30      boost::shared_ptr<path_key_type> r(new path_key_type(val, def, true)); 
     31      return r; 
     32    } 
     33    boost::shared_ptr<path_key_type> path_key(std::string *val) { 
     34      boost::shared_ptr<path_key_type> r(new path_key_type(val, "", false)); 
    2735      return r; 
    2836    } 
  • include/settings/client/settings_client.hpp

    r6533c1a r465866c  
    255255    typedef typed_key_value<std::string, typed_string_value<std::string> > string_key_type; 
    256256    typedef typed_key_value<std::wstring, typed_path_value<std::wstring> > wpath_key_type; 
     257    typedef typed_key_value<std::string, typed_path_value<std::string> > path_key_type; 
    257258    typedef typed_key_value<unsigned int, typed_int_value<unsigned int> > uint_key_type; 
    258259    typedef typed_key_value<int, typed_int_value<int> > int_key_type; 
     
    273274    boost::shared_ptr<wpath_key_type> wpath_key(std::wstring *val, std::wstring def); 
    274275    boost::shared_ptr<wpath_key_type> wpath_key(std::wstring *val); 
     276    boost::shared_ptr<path_key_type> path_key(std::string *val, std::string def); 
     277    boost::shared_ptr<path_key_type> path_key(std::string *val); 
    275278 
    276279    template<class T> 
  • include/socket/client.hpp

    raf05fa1 r465866c  
    1616    class connection : public boost::enable_shared_from_this<connection<protocol_type> >, private boost::noncopyable { 
    1717    private: 
    18       tcp::socket socket_; 
    1918      protocol_type protocol_; 
    2019      boost::asio::io_service &io_service_; 
     
    2827      connection(boost::asio::io_service &io_service, boost::posix_time::time_duration timeout, boost::shared_ptr<typename protocol_type::client_handler> handler)  
    2928        : io_service_(io_service) 
    30         , socket_(io_service)  
    3129        , timer_(io_service) 
    3230        , timeout_(timeout) 
     
    3634 
    3735      virtual ~connection() { 
    38         stop_timer(); 
    39         close(); 
     36        try { 
     37          stop_timer(); 
     38        } catch (const std::exception &e) { 
     39          handler_->log_error(__FILE__, __LINE__, std::string("Failed to close connection: ") + e.what()); 
     40        } catch (...) { 
     41          handler_->log_error(__FILE__, __LINE__, "Failed to close connection"); 
     42        } 
    4043      } 
    4144 
     
    6366      // External API functions 
    6467      // 
    65       virtual void connect(std::string host, std::string port) { 
     68      virtual boost::system::error_code connect(std::string host, std::string port) { 
    6669        tcp::resolver resolver(io_service_); 
    6770        tcp::resolver::query query(host, port); 
     
    7679          get_socket().lowest_layer().connect(*endpoint_iterator++, error); 
    7780        } 
    78         if (error) 
    79           throw boost::system::system_error(error); 
     81        if (error) { 
     82          return error; 
     83        } 
    8084        protocol_.on_connect(); 
     85        return error; 
    8186      } 
    8287 
     
    105110      virtual void close() { 
    106111        trace("close()"); 
    107         if (!get_socket().is_open()) 
    108           return; 
    109         get_socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both); 
    110         get_socket().close(); 
     112        boost::system::error_code ignored_ec; 
     113        if (get_socket().is_open()) 
     114          get_socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec); 
     115        get_socket().close(ignored_ec); 
    111116      } 
    112117 
     
    126131      } 
    127132 
    128       virtual void start_read_request(boost::asio::mutable_buffers_1 &buffer) { 
    129         trace("start_read_request()"); 
    130         async_read(socket_, buffer,  
    131           boost::bind(&connection::handle_read_request, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred) 
    132         ); 
    133       } 
     133      virtual void start_read_request(boost::asio::mutable_buffers_1 &buffer) = 0; 
    134134 
    135135      virtual void handle_read_request(const boost::system::error_code& e, std::size_t bytes_transferred) { 
     
    143143      } 
    144144 
    145       virtual void start_write_request(boost::asio::mutable_buffers_1 &buffer) { 
    146         trace("start_write_request()"); 
    147         async_write(socket_, buffer,  
    148           boost::bind(&connection::handle_write_request, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred) 
    149           ); 
    150       } 
     145      virtual void start_write_request(boost::asio::mutable_buffers_1 &buffer) = 0; 
    151146 
    152147      virtual void handle_write_request(const boost::system::error_code& e, std::size_t bytes_transferred) { 
     
    175170        return false; 
    176171      } 
    177       virtual basic_socket_type& get_socket() { 
    178         return socket_; 
    179       } 
    180  
    181172      ////////////////////////////////////////////////////////////////////////// 
    182173      // Internal helper functions 
    183174      // 
    184175      inline void trace(std::string msg) const { 
    185         if (debug_trace)  
     176        if (debug_trace && handler_)  
    186177          handler_->log_debug(__FILE__, __LINE__, msg); 
     178      } 
     179      inline void log_error(std::string file, int line, std::string msg) const { 
     180        if (handler_)  
     181          handler_->log_error(__FILE__, __LINE__, msg); 
     182      } 
     183 
     184      virtual basic_socket_type& get_socket() = 0; 
     185 
     186    }; 
     187 
     188    template<class protocol_type> 
     189    class tcp_connection : public connection<protocol_type> { 
     190      typedef connection<protocol_type> connection_type; 
     191      tcp::socket socket_; 
     192 
     193    public: 
     194      tcp_connection(boost::asio::io_service &io_service, boost::posix_time::time_duration timeout, boost::shared_ptr<typename protocol_type::client_handler> handler)  
     195        : connection_type(io_service, timeout, handler)  
     196        , socket_(io_service) 
     197      {} 
     198      virtual ~tcp_connection() { 
     199        try { 
     200          close(); 
     201        } catch (const std::exception &e) { 
     202          log_error(__FILE__, __LINE__, std::string("Failed to close connection: ") + e.what()); 
     203        } catch (...) { 
     204          log_error(__FILE__, __LINE__, "Failed to close connection"); 
     205        } 
     206      } 
     207 
     208      virtual void start_read_request(boost::asio::mutable_buffers_1 &buffer) { 
     209        std::size_t data_size = boost::asio::buffer_size(buffer); 
     210        trace("tcp::start_read_request(" + strEx::s::itos((int)data_size) + ")"); 
     211        async_read(socket_, buffer,  
     212          boost::bind(&connection::handle_read_request, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred) 
     213          ); 
     214      } 
     215 
     216      virtual void start_write_request(boost::asio::mutable_buffers_1 &buffer) { 
     217        std::size_t data_size = boost::asio::buffer_size(buffer); 
     218        trace("tcp::start_write_request(" + strEx::s::itos((int)data_size) + ")"); 
     219        async_write(socket_, buffer,  
     220          boost::bind(&connection::handle_write_request, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred) 
     221          ); 
     222      } 
     223 
     224      virtual basic_socket_type& get_socket() { 
     225        return socket_; 
    187226      } 
    188227    }; 
     
    201240      {} 
    202241      virtual ~ssl_connection() { 
    203       } 
    204  
    205  
    206       virtual void connect(std::string host, std::string port) { 
    207         connection_type::connect(host, port); 
    208         ssl_socket_.handshake(boost::asio::ssl::stream_base::client); 
     242        try { 
     243          close(); 
     244        } catch (const std::exception &e) { 
     245          log_error(__FILE__, __LINE__, std::string("Failed to close connection: ") + e.what()); 
     246        } catch (...) { 
     247          log_error(__FILE__, __LINE__, "Failed to close connection"); 
     248        } 
     249      } 
     250 
     251      virtual boost::system::error_code connect(std::string host, std::string port) { 
     252        boost::system::error_code error = connection_type::connect(host, port); 
     253        if (!error) 
     254          ssl_socket_.handshake(boost::asio::ssl::stream_base::client); 
     255        return error; 
    209256      } 
    210257 
     
    235282 
    236283      typedef connection<protocol_type> connection_type; 
     284      typedef tcp_connection<protocol_type> tcp_connection_type; 
    237285#ifdef USE_SSL 
    238286      boost::asio::ssl::context context_; 
     
    248296      { 
    249297      } 
     298      ~client() { 
     299        try { 
     300          if (connection_) 
     301            connection_->shutdown(); 
     302        } catch (...) { 
     303          handler_->log_error(__FILE__, __LINE__, "Failed to close socket on disconnect"); 
     304        } 
     305        connection_.reset(); 
     306      } 
    250307 
    251308      void connect() { 
    252309        connection_.reset(create_connection()); 
    253         connection_->connect(handler_->get_host(), handler_->get_port()); 
     310        boost::system::error_code error = connection_->connect(handler_->get_host(), handler_->get_port()); 
     311        if (error) { 
     312          connection_.reset(); 
     313          throw std::exception(error.message().c_str()); 
     314        } 
    254315      } 
    255316 
     
    262323        } 
    263324#endif 
    264         return new connection_type(io_service_, handler_->get_timeout(), handler_); 
     325        return new tcp_connection_type(io_service_, handler_->get_timeout(), handler_); 
    265326      } 
    266327 
  • include/socket/connection.hpp

    raf05fa1 r465866c  
    1616 
    1717    using boost::asio::ip::tcp; 
    18     static const bool debug_trace = false; 
     18    static const bool debug_trace = true; 
    1919 
    2020    // 
  • include/socket/server.hpp

    r9bd40e2 r465866c  
    7777          return; 
    7878        } 
    79         if (protocol_->get_info().use_ssl) { 
     79        if (protocol_->get_info().ssl.enabled) { 
    8080#ifdef USE_SSL 
    81           SSL_CTX_set_cipher_list(context_.impl(), "ADH"); 
    82           protocol_->log_debug(__FILE__, __LINE__, "Using certificate: " + utf8::cvt<std::string>(protocol_->get_info().certificate)); 
    83           context_.use_tmp_dh_file(to_string(protocol_->get_info().certificate)); 
     81          protocol_->log_debug(__FILE__, __LINE__, "Using SSL: " + protocol_->get_info().ssl.to_string()); 
     82          //context_.use_certificate_file(protocol_->get_info().ssl.certificate); 
     83          //context_.use_private_key_file(protocol_->get_info().ssl.certificate_key); 
     84          //context_.set_verify_mode(protocol_->get_info().ssl.get_verify_mode()); 
     85          SSL_CTX_set_cipher_list(context_.impl(), protocol_->get_info().ssl.allowed_ciphers.c_str()); 
     86          context_.use_tmp_dh_file(protocol_->get_info().ssl.dh_key); 
    8487          context_.set_verify_mode(boost::asio::ssl::context::verify_none); 
    8588#else 
     
    138141      typename connection_type* create_connection() { 
    139142#ifdef USE_SSL 
    140         if (protocol_->get_info().use_ssl) { 
     143        if (protocol_->get_info().ssl.enabled) { 
    141144          return new ssl_connection_type(io_service_, context_, protocol_); 
    142145        } 
  • include/socket/socket_helpers.cpp

    r58f0e80 r465866c  
    11#include <boost/asio.hpp> 
    22#include <boost/algorithm/string.hpp> 
     3#include <boost/filesystem.hpp> 
    34 
    45#include <strEx.h> 
     
    1011namespace ip = boost::asio::ip; 
    1112 
     13std::list<std::wstring> socket_helpers::connection_info::validate() { 
     14  return validate_ssl(); 
     15} 
     16 
     17std::list<std::wstring> socket_helpers::connection_info::validate_ssl() { 
     18  std::list<std::wstring> list; 
     19  if (!ssl.enabled) 
     20    return list; 
     21#ifndef USE_SSL 
     22  list.push_back(_T("SSL is not supported (not compiled with openssl)")); 
     23#endif 
     24 
     25  if (!ssl.certificate.empty() && !boost::filesystem::is_regular(ssl.certificate)) 
     26    list.push_back(_T("Certificate not found: ") + utf8::cvt<std::wstring>(ssl.certificate)); 
     27  if (!ssl.certificate_key.empty() && !boost::filesystem::is_regular(ssl.certificate_key)) 
     28    list.push_back(_T("Certificate key not found: ") + utf8::cvt<std::wstring>(ssl.certificate_key)); 
     29  if (!ssl.dh_key.empty() && !boost::filesystem::is_regular(ssl.dh_key)) 
     30    list.push_back(_T("DH key not found: ") + utf8::cvt<std::wstring>(ssl.dh_key)); 
     31  return list; 
     32} 
    1233 
    1334std::wstring socket_helpers::allowed_hosts_manager::to_wstring() { 
    1435  std::wstring ret; 
    15   BOOST_FOREACH(const host_record &r, entries) { 
    16     ip::address_v4 a(r.in_addr); 
     36  BOOST_FOREACH(const host_record_v4 &r, entries_v4) { 
     37    ip::address_v4 a(r.addr); 
    1738    ip::address_v4 m(r.mask); 
     39    std::wstring s = utf8::cvt<std::wstring>(a.to_string()) + _T("(") + utf8::cvt<std::wstring>(m.to_string()) + _T(")"); 
     40    strEx::append_list(ret, s); 
     41  } 
     42  BOOST_FOREACH(const host_record_v6 &r, entries_v6) { 
     43    ip::address_v6 a(r.addr); 
     44    ip::address_v6 m(r.mask); 
    1845    std::wstring s = utf8::cvt<std::wstring>(a.to_string()) + _T("(") + utf8::cvt<std::wstring>(m.to_string()) + _T(")"); 
    1946    strEx::append_list(ret, s); 
     
    2249} 
    2350 
    24 unsigned int socket_helpers::allowed_hosts_manager::lookup_mask(std::string mask) { 
    25   unsigned int masklen = 32; 
     51unsigned int extract_mask(std::string &mask, unsigned int masklen) { 
    2652  if (!mask.empty()) { 
    27     std::string::size_type pos = mask.find_first_of("0123456789"); 
    28     if (pos != std::wstring::npos) { 
    29       masklen = strEx::stoi(mask.substr(pos)); 
     53    std::string::size_type p1 = mask.find_first_of("0123456789"); 
     54    if (p1 != std::wstring::npos) { 
     55      std::string::size_type p2 = mask.find_first_not_of("0123456789", p1); 
     56      if (p2 != std::wstring::npos) 
     57        masklen = strEx::stoi(mask.substr(p1, p2)); 
     58      else 
     59        masklen = strEx::stoi(mask.substr(p1)); 
    3060    } 
    3161  } 
    32   if (masklen > 32) 
    33     masklen = 32; 
    34   return (0xffffffff << (32 - masklen )) & 0xffffffff; 
     62  return masklen; 
     63} 
     64 
     65template<class addr> 
     66addr calculate_mask(std::string mask_s) { 
     67  addr ret; 
     68  const unsigned int byte_size = 8; 
     69  const unsigned int largest_byte = 0xff; 
     70  unsigned int mask = extract_mask(mask_s, byte_size*ret.size()); 
     71  unsigned int index = mask / byte_size; 
     72  unsigned int reminder = mask % byte_size; 
     73 
     74  unsigned int value = largest_byte - (largest_byte >> reminder); 
     75 
     76  for (unsigned int i=0;i<ret.size();i++) { 
     77    if (i < index) 
     78      ret[i] = largest_byte; 
     79    else if (i == index) 
     80      ret[i] = value; 
     81    else 
     82      ret[i] = 0; 
     83  } 
     84  return ret; 
    3585} 
    3686 
     
    3888  boost::asio::io_service io_service; 
    3989  ip::tcp::resolver resolver(io_service); 
    40   entries.clear(); 
    41   host_record tmp_record; 
     90  entries_v4.clear(); 
     91  entries_v6.clear(); 
    4292  BOOST_FOREACH(std::string &record, sources) { 
    4393    boost::trim(record); 
    44     if (!record.empty()) { 
    45       std::string::size_type pos = record.find('/'); 
    46       if (pos == std::string::npos) { 
    47         tmp_record.host = record; 
    48         tmp_record.mask = lookup_mask(""); 
     94    if (record.empty()) 
     95      continue; 
     96    std::string::size_type pos = record.find('/'); 
     97    std::string addr, mask; 
     98    if (pos == std::string::npos) { 
     99      addr = record; 
     100      mask = ""; 
     101    } else { 
     102      addr = record.substr(0, pos); 
     103      mask = record.substr(pos); 
     104    } 
     105    if (addr.empty()) 
     106      continue; 
     107 
     108    if (std::isdigit(addr[0])) { 
     109      ip::address a = ip::address::from_string(addr); 
     110      if (a.is_v4()) { 
     111        entries_v4.push_back(host_record_v4(record, a.to_v4().to_bytes(), calculate_mask<addr_v4>(mask))); 
     112      } else if (a.is_v6()) { 
     113        entries_v6.push_back(host_record_v6(record, a.to_v6().to_bytes(), calculate_mask<addr_v6>(mask))); 
    49114      } else { 
    50         tmp_record.host = record.substr(0, pos); 
    51         tmp_record.mask = lookup_mask(record.substr(pos)); 
     115        errors.push_back("Invalid address: " + record); 
    52116      } 
    53  
    54       if (std::isdigit(tmp_record.host[0])) { 
    55         ip::address_v4 a = ip::address_v4::from_string(tmp_record.host); 
    56         tmp_record.in_addr = a.to_ulong(); 
    57         entries.push_back(tmp_record); 
    58       } else { 
    59         try { 
    60           ip::tcp::resolver::query query(tmp_record.host, ""); 
    61           ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); 
    62           ip::tcp::resolver::iterator end; 
    63           for (;endpoint_iterator != end; ++endpoint_iterator) { 
    64             tmp_record.in_addr = endpoint_iterator->endpoint().address().to_v4().to_ulong(); 
    65             tmp_record.host = endpoint_iterator->endpoint().address().to_string(); 
    66             entries.push_back(tmp_record); 
     117    } else { 
     118      try { 
     119        ip::tcp::resolver::query query(addr, ""); 
     120        ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); 
     121        ip::tcp::resolver::iterator end; 
     122        for (;endpoint_iterator != end; ++endpoint_iterator) { 
     123          ip::address a = endpoint_iterator->endpoint().address(); 
     124          if (a.is_v4()) { 
     125            entries_v4.push_back(host_record_v4(record, a.to_v4().to_bytes(), calculate_mask<addr_v4>(mask))); 
     126          } else if (a.is_v6()) { 
     127            entries_v6.push_back(host_record_v6(record, a.to_v6().to_bytes(), calculate_mask<addr_v6>(mask))); 
     128          } else { 
     129            errors.push_back("Invalid address: " + record); 
    67130          } 
    68         } catch (const std::exception &e) { 
    69           errors.push_back("Failed to lookup allowed host " + record + ": " + e.what()); 
    70131        } 
     132      } catch (const std::exception &e) { 
     133        errors.push_back("Failed to parse host " + record + ": " + e.what()); 
    71134      } 
    72135    } 
  • include/socket/socket_helpers.hpp

    r72eea1f r465866c  
    1414 
    1515  struct allowed_hosts_manager { 
    16  
     16    template<class addr_type> 
    1717    struct host_record { 
    18       host_record() : mask(0), in_addr(0) {} 
    19       host_record(const host_record &other) : mask(other.mask), in_addr(other.in_addr), host(other.host) {} 
     18      host_record(std::string host, typename addr_type addr, typename addr_type mask)  
     19        : addr(addr) 
     20        , mask(mask) 
     21        , host(host) {} 
     22      host_record(const host_record &other)  
     23        : addr(other.addr) 
     24        , mask(other.mask) 
     25        , host(other.host) {} 
    2026      const host_record& operator=(const host_record &other) { 
     27        addr = other.addr; 
    2128        mask = other.mask; 
    22         in_addr = other.in_addr; 
    2329        host = other.host; 
    2430        return *this; 
    2531      } 
    2632      std::string host; 
    27       u_long in_addr; 
    28       unsigned long mask; 
     33      typename addr_type addr; 
     34      typename addr_type mask; 
    2935    }; 
    30  
    31     std::list<host_record> entries; 
     36    typedef boost::asio::ip::address_v4::bytes_type addr_v4; 
     37    typedef boost::asio::ip::address_v6::bytes_type addr_v6; 
     38 
     39    typedef host_record<addr_v4> host_record_v4; 
     40    typedef host_record<addr_v6> host_record_v6; 
     41 
     42    std::list<host_record_v4> entries_v4; 
     43    std::list<host_record_v6> entries_v6; 
    3244    std::list<std::string> sources; 
    33     //std::wstring list; 
    3445    bool cached; 
    3546 
    3647    allowed_hosts_manager() : cached(true) {} 
    37     allowed_hosts_manager(const allowed_hosts_manager &other) : entries(other.entries), sources(other.sources), cached(other.cached) {} 
     48    allowed_hosts_manager(const allowed_hosts_manager &other) : entries_v4(other.entries_v4), entries_v6(other.entries_v6), sources(other.sources), cached(other.cached) {} 
    3849    const allowed_hosts_manager& operator=(const allowed_hosts_manager &other) { 
    39       entries = other.entries; 
     50      entries_v4 = other.entries_v4; 
     51      entries_v6 = other.entries_v6; 
    4052      sources = other.sources; 
    4153      cached = other.cached; 
     
    4961      } 
    5062    } 
    51     unsigned int lookup_mask(std::string mask); 
     63    addr_v4 lookup_mask_v4(std::string mask); 
     64    addr_v6 lookup_mask_v6(std::string mask); 
    5265    void refresh(std::list<std::string> &errors); 
    5366 
    54     inline bool match_host(const host_record &allowed, const unsigned long &remote) const { 
    55       return ((allowed.in_addr&allowed.mask)==(remote&allowed.mask)); 
     67    template<class T> 
     68    inline bool match_host(const T &allowed, const T &mask, const T &remote) const { 
     69      for (int i=0;i<allowed.size(); i++) { 
     70        if ( (allowed[i]&mask[i]) != (remote[i]&mask[i]) ) 
     71          return false; 
     72      } 
     73      return true; 
    5674    } 
    5775    bool is_allowed(const boost::asio::ip::address &address, std::list<std::string> &errors) { 
    58       return (address.is_v4() && is_allowed_v4(address.to_v4().to_ulong(), errors)) 
    59         || (address.is_v6() && address.to_v6().is_v4_compatible() && is_allowed_v4(address.to_v6().to_v4().to_ulong(), errors)) 
    60         || (address.is_v6() && address.to_v6().is_v4_mapped() && is_allowed_v4(address.to_v6().to_v4().to_ulong(), errors)); 
    61     } 
    62     bool is_allowed_v4(const unsigned long &remote, std::list<std::string> &errors) { 
    63       errors.push_back(strEx::wstring_to_string(strEx::itos(remote))); 
    64       if (entries.empty()) 
    65         return true; 
     76      return (entries_v4.empty()&&entries_v6.empty()) 
     77        || (address.is_v4() && is_allowed_v4(address.to_v4().to_bytes(), errors)) 
     78        || (address.is_v6() && is_allowed_v6(address.to_v6().to_bytes(), errors)) 
     79        || (address.is_v6() && address.to_v6().is_v4_compatible() && is_allowed_v4(address.to_v6().to_v4().to_bytes(), errors)) 
     80        || (address.is_v6() && address.to_v6().is_v4_mapped() && is_allowed_v4(address.to_v6().to_v4().to_bytes(), errors)) 
     81        ; 
     82    } 
     83    bool is_allowed_v4(const addr_v4 &remote, std::list<std::string> &errors) { 
    6684      if (!cached) 
    6785        refresh(errors); 
    68       BOOST_FOREACH(const host_record &r, entries) { 
    69         if (match_host(r, remote)) 
     86      BOOST_FOREACH(const host_record_v4 &r, entries_v4) { 
     87        if (match_host(r.addr, r.mask, remote)) 
     88          return true; 
     89      } 
     90      return false; 
     91    } 
     92    bool is_allowed_v6(const addr_v6 &remote, std::list<std::string> &errors) { 
     93      if (!cached) 
     94        refresh(errors); 
     95      BOOST_FOREACH(const host_record_v6 &r, entries_v6) { 
     96        if (match_host(r.addr, r.mask, remote)) 
    7097          return true; 
    7198      } 
     
    77104  struct connection_info { 
    78105    static const int backlog_default; 
    79     connection_info() : back_log(backlog_default), port(0), thread_pool_size(0), use_ssl(false), timeout(30) {} 
     106    connection_info() : back_log(backlog_default), port(0), thread_pool_size(0), timeout(30) {} 
    80107 
    81108    connection_info(const connection_info &other)  
     
    84111      , thread_pool_size(other.thread_pool_size) 
    85112      , back_log(other.back_log) 
    86       , use_ssl(other.use_ssl) 
     113      , ssl(other.ssl) 
    87114      , timeout(other.timeout) 
    88       , certificate(other.certificate) 
    89115      , allowed_hosts(other.allowed_hosts) 
    90116      { 
     
    95121      thread_pool_size = other.thread_pool_size; 
    96122      back_log = other.back_log; 
    97       use_ssl = other.use_ssl; 
     123      ssl = other.ssl; 
    98124      timeout = other.timeout; 
    99       certificate = other.certificate; 
    100125      allowed_hosts = other.allowed_hosts; 
    101126      return *this; 
    102127    } 
    103128 
     129 
     130    std::list<std::wstring> validate_ssl(); 
     131    std::list<std::wstring> validate(); 
    104132 
    105133    std::string address; 
     
    107135    unsigned int thread_pool_size; 
    108136    int back_log; 
    109     bool use_ssl; 
    110137    unsigned int timeout; 
    111     std::wstring certificate; 
     138 
     139    struct ssl_opts { 
     140      ssl_opts() : enabled(false) {} 
     141 
     142      ssl_opts(const ssl_opts &other)  
     143        : enabled(other.enabled) 
     144        , certificate(other.certificate) 
     145        , certificate_format(other.certificate_format) 
     146        , certificate_key(other.certificate_key) 
     147        , ca_path(other.ca_path) 
     148        , allowed_ciphers(other.allowed_ciphers) 
     149        , dh_key(other.dh_key) 
     150        , verify_mode(other.verify_mode) 
     151      {} 
     152      ssl_opts& operator=(const ssl_opts &other) { 
     153        enabled = other.enabled; 
     154        certificate = other.certificate; 
     155        certificate_format = other.certificate_format; 
     156        certificate_key = other.certificate_key; 
     157        ca_path = other.ca_path; 
     158        allowed_ciphers = other.allowed_ciphers; 
     159        dh_key = other.dh_key; 
     160        verify_mode = other.verify_mode; 
     161      } 
     162 
     163 
     164      bool enabled; 
     165      std::string certificate; 
     166      std::string certificate_format; 
     167      std::string certificate_key; 
     168 
     169      std::string ca_path; 
     170      std::string allowed_ciphers; 
     171      std::string dh_key; 
     172 
     173      std::string verify_mode; 
     174 
     175      std::string to_string() { 
     176        std::stringstream ss; 
     177        if (enabled) { 
     178          ss << "ssl: " << verify_mode; 
     179          ss << ", cert: " << certificate << " (" << certificate_format << "), " << certificate_key; 
     180          ss << ", dh: " << dh_key << ", ciphers: " << allowed_ciphers << ", ca: " << ca_path; 
     181        } else  
     182          ss << "ssl disabled"; 
     183        return ss.str(); 
     184      } 
     185    }; 
     186    ssl_opts ssl; 
    112187 
    113188    allowed_hosts_manager allowed_hosts; 
  • include/strEx.h

    rfa11893 r465866c  
    256256  }; 
    257257  namespace s { 
     258    /* 
    258259    inline std::string itos(float i) { 
    259260      std::stringstream ss; 
     
    271272      return ss.str(); 
    272273    } 
    273     inline std::string itos(unsigned int i) { 
     274    */ 
     275    template<typename T> 
     276    inline std::string itos(T i) { 
    274277      std::stringstream ss; 
    275278      ss << i; 
  • modules/CheckEventLog/CMakeLists.txt

    r8d89d7a r465866c  
    2828    filter.hpp 
    2929 
     30    filters.hpp 
     31 
    3032    ${NSCP_DEF_PLUGIN_HPP} 
    3133    ${NSCP_FILTER_HPP} 
  • modules/CheckEventLog/CheckEventLog.cpp

    r440c0cb r465866c  
    3737 
    3838#include "filter.hpp" 
     39#include "filters.hpp" 
    3940 
    4041#include <nscapi/nscapi_protobuf_functions.hpp> 
     
    6768} 
    6869 
    69 WORD get_language(std::string lang) { 
    70   if (lang == "neutral") return LANG_NEUTRAL; 
    71   if (lang == "arabic") return LANG_ARABIC; 
    72   if (lang == "bulgarian") return LANG_BULGARIAN; 
    73   if (lang == "catalan") return LANG_CATALAN; 
    74   if (lang == "chinese") return LANG_CHINESE; 
    75   if (lang == "czech") return LANG_CZECH; 
    76   if (lang == "danish") return LANG_DANISH; 
    77   if (lang == "german") return LANG_GERMAN; 
    78   if (lang == "greek") return LANG_GREEK; 
    79   if (lang == "english") return LANG_ENGLISH; 
    80   if (lang == "spanish") return LANG_SPANISH; 
    81   if (lang == "finnish") return LANG_FINNISH; 
    82   if (lang == "french") return LANG_FRENCH; 
    83   if (lang == "hebrew") return LANG_HEBREW; 
    84   if (lang == "hungarian") return LANG_HUNGARIAN; 
    85   if (lang == "icelandic") return LANG_ICELANDIC; 
    86   if (lang == "italian") return LANG_ITALIAN; 
    87   if (lang == "japanese") return LANG_JAPANESE; 
    88   if (lang == "korean") return LANG_KOREAN; 
    89   if (lang == "dutch") return LANG_DUTCH; 
    90   if (lang == "norwegian") return LANG_NORWEGIAN; 
    91   if (lang == "polish") return LANG_POLISH; 
    92   if (lang == "portuguese") return LANG_PORTUGUESE; 
    93   if (lang == "romanian") return LANG_ROMANIAN; 
    94   if (lang == "russian") return LANG_RUSSIAN; 
    95   if (lang == "croatian") return LANG_CROATIAN; 
    96   if (lang == "serbian") return LANG_SERBIAN; 
    97   if (lang == "slovak") return LANG_SLOVAK; 
    98   if (lang == "albanian") return LANG_ALBANIAN; 
    99   if (lang == "swedish") return LANG_SWEDISH; 
    100   if (lang == "thai") return LANG_THAI; 
    101   if (lang == "turkish") return LANG_TURKISH; 
    102   if (lang == "urdu") return LANG_URDU; 
    103   if (lang == "indonesian") return LANG_INDONESIAN; 
    104   if (lang == "ukrainian") return LANG_UKRAINIAN; 
    105   if (lang == "belarusian") return LANG_BELARUSIAN; 
    106   if (lang == "slovenian") return LANG_SLOVENIAN; 
    107   if (lang == "estonian") return LANG_ESTONIAN; 
    108   if (lang == "latvian") return LANG_LATVIAN; 
    109   if (lang == "lithuanian") return LANG_LITHUANIAN; 
    110   if (lang == "farsi") return LANG_FARSI; 
    111   if (lang == "vietnamese") return LANG_VIETNAMESE; 
    112   if (lang == "armenian") return LANG_ARMENIAN; 
    113   if (lang == "azeri") return LANG_AZERI; 
    114   if (lang == "basque") return LANG_BASQUE; 
    115   if (lang == "macedonian") return LANG_MACEDONIAN; 
    116   if (lang == "afrikaans") return LANG_AFRIKAANS; 
    117   if (lang == "georgian") return LANG_GEORGIAN; 
    118   if (lang == "faeroese") return LANG_FAEROESE; 
    119   if (lang == "hindi") return LANG_HINDI; 
    120   if (lang == "malay") return LANG_MALAY; 
    121   if (lang == "kazak") return LANG_KAZAK; 
    122   if (lang == "kyrgyz") return LANG_KYRGYZ; 
    123   if (lang == "swahili") return LANG_SWAHILI; 
    124   if (lang == "uzbek") return LANG_UZBEK; 
    125   if (lang == "tatar") return LANG_TATAR; 
    126   if (lang == "punjabi") return LANG_PUNJABI; 
    127   if (lang == "gujarati") return LANG_GUJARATI; 
    128   if (lang == "tamil") return LANG_TAMIL; 
    129   if (lang == "telugu") return LANG_TELUGU; 
    130   if (lang == "kannada") return LANG_KANNADA; 
    131   if (lang == "marathi") return LANG_MARATHI; 
    132   if (lang == "sanskrit") return LANG_SANSKRIT; 
    133   if (lang == "mongolian") return LANG_MONGOLIAN; 
    134   if (lang == "galician") return LANG_GALICIAN; 
    135   if (lang == "konkani") return LANG_KONKANI; 
    136   if (lang == "syriac") return LANG_SYRIAC; 
    137   if (lang == "divehi") return LANG_DIVEHI; 
    138   return LANG_NEUTRAL; 
    139 } 
    140  
    141 void real_time_thread::set_language(std::string lang) { 
    142   WORD wLang = get_language(lang); 
    143   if (wLang == LANG_NEUTRAL) 
    144     info.dwLang = MAKELANGID(wLang, SUBLANG_DEFAULT); 
    145   else 
    146     info.dwLang = MAKELANGID(wLang, SUBLANG_NEUTRAL); 
    147 } 
    148  
    149 void real_time_thread::process_no_events(std::wstring alias) { 
     70 
     71void real_time_thread::process_no_events(const filters::filter_config_object &object) { 
    15072  std::wstring response; 
    151   if (alias.empty()) 
    152     alias = info.alias; 
    153   if (!nscapi::core_helper::submit_simple_message(info.target, alias, NSCAPI::returnOK, info.ok_msg, info.perf_msg, response)) { 
     73  if (!nscapi::core_helper::submit_simple_message(object.target, object.alias, NSCAPI::returnOK, object.ok_msg, object.perf_msg, response)) { 
    15474    NSC_LOG_ERROR(_T("Failed to submit evenhtlog result: ") + response); 
    15575  } 
    15676} 
    15777 
    158 void real_time_thread::process_record(std::wstring alias, const EventLogRecord &record) { 
     78void real_time_thread::process_record(const filters::filter_config_object &object, const EventLogRecord &record) { 
    15979  std::wstring response; 
    160   std::wstring message = record.render(true, info.syntax, DATE_FORMAT, info.dwLang); 
    161   if (alias.empty()) 
    162     alias = info.alias; 
    163   if (!nscapi::core_helper::submit_simple_message(info.target, alias, NSCAPI::returnCRIT, message, info.perf_msg, response)) { 
     80  std::wstring message = record.render(true, object.syntax, object.date_format, object.dwLang); 
     81  if (!nscapi::core_helper::submit_simple_message(object.target, object.alias, object.severity, message, object.perf_msg, response)) { 
    16482    NSC_LOG_ERROR(_T("Failed to submit evenhtlog result: ") + response); 
    16583  } 
     
    196114} 
    197115void real_time_thread::debug_miss(const EventLogRecord &record) { 
    198   std::wstring message = record.render(true, info.syntax, DATE_FORMAT, info.dwLang); 
     116  std::wstring message = record.render(true, _T("%type% %source%: %message%"), DATE_FORMAT, LANG_NEUTRAL); 
    199117  NSC_DEBUG_MSG_STD(_T("No filter matched: ") + message); 
    200118} 
     
    202120void real_time_thread::thread_proc() { 
    203121 
    204   std::list<eventlog_filter::filter_engine> filters; 
    205   BOOST_FOREACH(const filter_container &filter, filters_) { 
    206     eventlog_filter::filter_argument fargs = eventlog_filter::factories::create_argument(info.syntax, DATE_FORMAT); 
    207     fargs->filter = filter.filter; 
    208     fargs->debug = debug_; 
    209     fargs->alias = filter.alias; 
     122  std::list<filters::filter_config_object> filters; 
     123  BOOST_FOREACH(filters::filter_config_object object, filters_.get_object_list()) { 
     124    eventlog_filter::filter_argument fargs = eventlog_filter::factories::create_argument(object.syntax, object.date_format); 
     125    fargs->filter = object.filter; 
     126    fargs->debug = object.debug; 
     127    fargs->alias = object.alias; 
    210128    fargs->bShowDescriptions = true; 
    211     eventlog_filter::filter_engine engine = eventlog_filter::factories::create_engine(fargs); 
    212  
    213     if (!engine) { 
    214       NSC_LOG_ERROR_STD(_T("Invalid filter: ") + filter.filter); 
     129    // eventlog_filter::filter_engine  
     130    object.engine = eventlog_filter::factories::create_engine(fargs); 
     131 
     132    if (!object.engine) { 
     133      NSC_LOG_ERROR_STD(_T("Invalid filter: ") + object.filter); 
    215134      continue; 
    216135    } 
    217136 
    218     if (!engine->boot()) { 
    219       NSC_LOG_ERROR_STD(_T("Error booting filter: ") + filter.filter); 
     137    if (!object.engine->boot()) { 
     138      NSC_LOG_ERROR_STD(_T("Error booting filter: ") + object.filter); 
    220139      continue; 
    221140    } 
    222141 
    223142    std::wstring message; 
    224     if (!engine->validate(message)) { 
     143    if (!object.engine->validate(message)) { 
    225144      NSC_LOG_ERROR_STD(_T("Error validating filter: ") + message); 
    226145      continue; 
    227146    } 
    228     filters.push_back(engine); 
     147    filters.push_back(object); 
    229148  } 
    230149 
     
    259178    DWORD dwWaitReason = WaitForMultipleObjects(list.size()+1, handles, FALSE, dwWaitTime==0?INFINITE:dwWaitTime); 
    260179    if (dwWaitReason == WAIT_TIMEOUT) { 
    261       BOOST_FOREACH(eventlog_filter::filter_engine engine, filters) { 
    262         process_no_events(engine->data->alias); 
     180      BOOST_FOREACH(const filters::filter_config_object &object, filters) { 
     181        process_no_events(object); 
    263182      } 
    264183    } else if (dwWaitReason == WAIT_OBJECT_0) { 
     
    283202        bool matched = false; 
    284203 
    285         BOOST_FOREACH(eventlog_filter::filter_engine engine, filters) { 
    286           if (engine->match(arg)) { 
    287             process_record(engine->data->alias, elr); 
     204        BOOST_FOREACH(const filters::filter_config_object &object, filters) { 
     205          if (object.engine->match(arg)) { 
     206            process_record(object, elr); 
    288207            matched = true; 
    289208          } 
     
    311230  if (!enabled_) 
    312231    return true; 
    313   if (!has_filters()) { 
    314     add_realtime_filter(_T("default"), _T("type NOT IN ('success', 'info', 'auditSuccess')")); 
    315   } 
    316  
    317   stop_event_ = CreateEvent(NULL, TRUE, FALSE, _T("EVentLogShutdown")); 
     232 
     233  stop_event_ = CreateEvent(NULL, TRUE, FALSE, _T("EventLogShutdown")); 
    318234 
    319235  thread_ = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&real_time_thread::thread_proc, this))); 
     
    327243} 
    328244 
    329 void real_time_thread::add_realtime_filter(std::wstring key, std::wstring query) { 
    330   filter_container c; 
    331   if (!key.empty() && query.empty()) { 
    332     c.filter = key; 
    333     filters_.push_back(c); 
    334   } else if (key.empty() && query.empty()) { 
    335     return; 
    336   } else { 
    337     c.alias = key; 
    338     c.filter = query; 
    339     filters_.push_back(c); 
     245void real_time_thread::add_realtime_filter(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring key, std::wstring query) { 
     246  try { 
     247    filters_.add(proxy, filters_path_, key, query, key == _T("default")); 
     248  } catch (const std::exception &e) { 
     249    NSC_LOG_ERROR_STD(_T("Failed to add command: ") + key + _T(", ") + utf8::to_unicode(e.what())); 
     250  } catch (...) { 
     251    NSC_LOG_ERROR_STD(_T("Failed to add command: ") + key); 
    340252  } 
    341253} 
     
    352264    settings.set_alias(alias, _T("eventlog")); 
    353265     
     266    thread_.filters_path_ = settings.alias().get_settings_path(_T("real-time/filters")); 
     267 
    354268 
    355269    settings.alias().add_path_to_settings() 
     
    358272      (_T("real-time"), _T("CONFIGURE REALTIME CHECKING"), _T("A set of options to configure the real time checks")) 
    359273 
    360       (_T("real-time/filters"), sh::fun_values_path(boost::bind(&real_time_thread::add_realtime_filter, &thread_, _1, _2)),   
     274      (_T("real-time/filters"), sh::fun_values_path(boost::bind(&real_time_thread::add_realtime_filter, &thread_, get_settings_proxy(), _1, _2)),   
    361275      _T("REALTIME FILTERS"), _T("A set of filters to use in real-time mode")) 
    362276      ; 
     
    382296      _T("REAL TIME CHECKING"), _T("Spawns a backgrounnd thread which detects issues and reports them back instantly.")) 
    383297 
    384       (_T("destination"), sh::string_fun_key<std::wstring>(boost::bind(&real_time_thread::set_destination, &thread_, _1), _T("NSCA")), 
    385       _T("DESTINATION"), _T("The destination for intercepted messages")) 
    386  
    387298      (_T("startup age"), sh::string_fun_key<std::wstring>(boost::bind(&real_time_thread::set_start_age, &thread_, _1), _T("30m")), 
    388299      _T("STARTUP AGE"), _T("The initial age to scan when starting NSClient++")) 
     
    391302      _T("MAGIMUM AGE"), _T("How long before reporting \"ok\" (if this is set to off no ok will be reported only errors)")) 
    392303 
    393       (_T("filter"), sh::string_fun_key<std::wstring>(boost::bind(&real_time_thread::set_filter, &thread_, _1), _T("")), 
    394       _T("STARTUP AGE"), _T("The initial age to scan when starting NSClient++")) 
    395  
    396       (_T("syntax"), sh::wstring_key(&thread_.info.syntax, _T("%type% %source%: %message%")), 
    397       _T("STARTUP AGE"), _T("The initial age to scan when starting NSClient++")) 
    398  
    399       (_T("language"), sh::string_fun_key<std::string>(boost::bind(&real_time_thread::set_language, &thread_, _1), ""), 
    400       _T("MESSAGE LANGUAGE"), _T("The language to use for rendering message (mainly used fror testing)")) 
    401  
    402304      (_T("log"), sh::string_fun_key<std::wstring>(boost::bind(&real_time_thread::set_eventlog, &thread_, _1), _T("application")), 
    403305      _T("LOGS TO CHECK"), _T("Coma separated list of logs to check")) 
     
    408310      (_T("enable active"), sh::bool_key(&thread_.cache_, false), 
    409311      _T("ENABLE ACTIVE MONITORING"), _T("This will store all matches so you can use real-time filters from active monitoring (use CheckEventlogCache).")) 
    410  
    411       (_T("ok message"), sh::wstring_key(&thread_.info.ok_msg, _T("eventlog found no records")), 
    412       _T("OK MESSAGE"), _T("This is the message sent periodically whenever no error is discovered.")) 
    413  
    414       (_T("alias"), sh::wstring_key(&thread_.info.alias, _T("eventlog")), 
    415       _T("ALIAS"), _T("The alias to use for this event (in NSCA this constitutes the service name).")) 
    416312      ; 
    417313 
  • modules/CheckEventLog/CheckEventLog.h

    r440c0cb r465866c  
    3030#include "eventlog_record.hpp" 
    3131 
     32#include "filters.hpp" 
     33 
    3234struct real_time_thread { 
    33  
    34   struct target_information { 
    35     std::wstring target; 
    36     std::wstring alias; 
    37     std::wstring syntax; 
    38     std::wstring ok_msg; 
    39     std::wstring perf_msg; // 
    40     //bool perf; 
    41     DWORD dwLang; 
    42  
    43   }; 
    44  
    45   struct filter_container { 
    46     std::wstring filter; 
    47     std::wstring alias; 
    48   }; 
    49  
    50   target_information info; 
    5135  bool enabled_; 
    5236  //std::wstring destination_; 
     
    5438  unsigned long long max_age_; 
    5539  //std::wstring syntax_; 
    56   std::list<filter_container> filters_; 
     40  //std::list<filter_container> filters_; 
    5741  boost::shared_ptr<boost::thread> thread_; 
    5842  HANDLE stop_event_; 
     
    6044  std::list<std::wstring> hit_cache_; 
    6145  boost::timed_mutex cache_mutex_; 
     46  filters::filter_config_handler filters_; 
    6247 
    6348  bool cache_; 
    6449  bool debug_; 
     50  std::wstring filters_path_; 
    6551 
    6652  real_time_thread() : enabled_(false), start_age_(0), max_age_(0), debug_(false), cache_(false) { 
     
    6955  } 
    7056 
    71   void add_realtime_filter(std::wstring key, std::wstring query); 
     57  void add_realtime_filter(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring key, std::wstring query); 
    7258  void set_enabled(bool flag) { enabled_ = flag; }  
    73   void set_destination(std::wstring dst) { info.target = dst; }  
    7459  void set_start_age(std::wstring age) { 
    7560    start_age_ = strEx::stoi64_as_time(age); 
     
    8671 
    8772  void set_language(std::string lang); 
    88   void set_filter(std::wstring flt) { 
     73  void set_filter(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring flt) { 
    8974    if (!flt.empty()) 
    90       add_realtime_filter(_T("filter"), flt); 
     75      add_realtime_filter(proxy, _T("default"), flt); 
    9176  } 
    9277  bool has_filters() { 
    93     return !filters_.empty(); 
     78    return !filters_.has_objects(); 
    9479  } 
    9580  bool start(); 
     
    10085  void thread_proc(); 
    10186//  void process_events(eventlog_filter::filter_engine engine, eventlog_wrapper &eventlog); 
    102   void process_no_events(std::wstring alias); 
    103   void process_record(std::wstring alias, const EventLogRecord &record); 
     87  void process_no_events(const filters::filter_config_object &object); 
     88  void process_record(const filters::filter_config_object &object, const EventLogRecord &record); 
    10489  void debug_miss(const EventLogRecord &record); 
    10590//  void process_event(eventlog_filter::filter_engine engine, const EVENTLOGRECORD* record); 
  • modules/CheckExternalScripts/CMakeLists.txt

    r8d89d7a r465866c  
    1818    "${TARGET}.h" 
    1919    "${TARGET}.def" 
     20    commands.hpp 
    2021 
    2122    ${NSCP_INCLUDEDIR}/execute_process_unix.hpp 
  • modules/DistributedClient/module.cmake

    r234a037 r465866c  
    11IF(ZEROMQ_FOUND) 
    2   SET (BUILD_MODULE 1) 
     2  SET (BUILD_MODULE 0) 
    33ELSE(ZEROMQ_FOUND) 
    44  MESSAGE(STATUS "Disabling DistributedClient since zeromq was not found") 
  • modules/DistributedServer/handler_impl.cpp

    r8d89d7a r465866c  
    1414 
    1515#include "handler_impl.hpp" 
     16 
     17 
     18nscp::packet handler_impl::process(const nscp::packet &packet) { 
     19  if (nscp::checks::is_query_request(packet)) { 
     20    Plugin::QueryRequestMessage msg; 
     21    msg.ParseFromString(packet.payload); 
     22    std::wstring command = _T("todo: fixme");//utf8::cvt<std::wstring>(msg.command()); 
     23 
     24    std::string reply; 
     25    try { 
     26      NSCAPI::nagiosReturn returncode = handle_query_request(packet.payload, msg, reply); 
     27      if (returncode == NSCAPI::returnIgnored) 
     28        nscapi::functions::create_simple_query_response_unknown(command, _T("Command was not found: ") + command, _T(""), reply); 
     29    } catch (const nscp::nscp_exception &e) { 
     30      nscapi::functions::create_simple_query_response_unknown(command, _T("Processing error: ") + command + _T(": ") + utf8::cvt<std::wstring>(e.what()), _T(""), reply); 
     31    } catch (const std::exception &e) { 
     32      nscapi::functions::create_simple_query_response_unknown(command, _T("Unknown error processing: ") + command + _T(": ") + utf8::cvt<std::wstring>(e.what()), _T(""), reply); 
     33    } 
     34    return nscp::factory::create_query_response(reply); 
     35  } else if (nscp::checks::is_submit_request(packet)) { 
     36    Plugin::SubmitRequestMessage msg; 
     37    msg.ParseFromString(packet.payload); 
     38    try { 
     39      std::string reply; 
     40      NSCAPI::nagiosReturn returncode = handle_submission_request(packet.payload, msg, reply); 
     41      return nscp::factory::create_submission_response(reply); 
     42    } catch (const nscp::nscp_exception &e) { 
     43      return nscp::factory::create_error(_T("Exception processing message: ") + to_wstring(e.what())); 
     44    } catch (const std::exception &e) { 
     45      return nscp::factory::create_error(_T("Exception processing message: ") + to_wstring(e.what())); 
     46    } 
     47  } else if (nscp::checks::is_exec_request(packet)) { 
     48    Plugin::ExecuteRequestMessage msg; 
     49    msg.ParseFromString(packet.payload); 
     50    try { 
     51      std::string reply; 
     52      NSCAPI::nagiosReturn returncode = handle_exec_request(packet.payload, msg, reply); 
     53      return nscp::factory::create_submission_response(reply); 
     54    } catch (const nscp::nscp_exception &e) { 
     55      return nscp::factory::create_error(_T("Exception processing message: ") + to_wstring(e.what())); 
     56    } catch (const std::exception &e) { 
     57      return nscp::factory::create_error(_T("Exception processing message: ") + to_wstring(e.what())); 
     58    } 
     59  } else { 
     60    this->log_error(__FILE__, __LINE__, _T("Unknown packet: ") + packet.to_wstring()); 
     61    return nscp::factory::create_error(_T("Unknown packet: ") + packet.to_wstring()); 
     62  } 
     63  return nscp::factory::create_error(_T("Unknown error...")); 
     64} 
    1665 
    1766NSCAPI::nagiosReturn handler_impl::handle_query_request(const std::string &request, Plugin::QueryRequestMessage &msg, std::string &reply) { 
  • modules/DistributedServer/handler_impl.hpp

    r8013c0c r465866c  
    44#include <nscp/handler.hpp> 
    55 
    6 class handler_impl : public nscp::handler, private boost::noncopyable { 
     6class handler_impl : public nscp::server::handler, private boost::noncopyable { 
    77  bool allowArgs_; 
    88  bool allowNasty_; 
     
    1010public: 
    1111  handler_impl() : noPerfData_(false), allowNasty_(false), allowArgs_(false) {} 
     12 
     13  nscp::packet process(const nscp::packet &packet); 
    1214 
    1315  NSCAPI::nagiosReturn handle_query_request(const std::string &request, Plugin::QueryRequestMessage &msg, std::string &reply); 
  • modules/DistributedServer/module.cmake

    r2b2e9b8 r465866c  
    11IF(ZEROMQ_FOUND) 
    2   SET (BUILD_MODULE 1) 
     2  SET (BUILD_MODULE 0) 
    33ELSE(ZEROMQ_FOUND) 
    44  MESSAGE(STATUS "Disabling DistributedServer since zeromq was not found") 
  • modules/NRPEClient/CMakeLists.txt

    r8d89d7a r465866c  
    2626    "${TARGET}.def" 
    2727    ${NSCP_INCLUDEDIR}/nrpe/packet.hpp 
    28     ${NSCP_INCLUDEDIR}/nrpe/client/socket.hpp 
     28    ${NSCP_INCLUDEDIR}/nrpe/client/nrpe_client_protocol.hpp 
    2929    ${NSCP_INCLUDEDIR}/swap_bytes.hpp 
    3030    ${NSCP_INCLUDEDIR}/socket/socket_helpers.hpp 
     31    ${NSCP_INCLUDEDIR}/socket/client.hpp 
    3132    ${NSCP_INCLUDEDIR}/client/command_line_parser.hpp 
    3233 
     
    4546) 
    4647INCLUDE(${BUILD_CMAKE_FOLDER}/module.cmake) 
    47 SOURCE_GROUP("Server" REGULAR_EXPRESSION .*include/nrpe/.*) 
     48SOURCE_GROUP("Client" REGULAR_EXPRESSION .*include/nrpe/.*) 
     49SOURCE_GROUP("Socket" REGULAR_EXPRESSION .*include/socket/.*) 
  • modules/NRPEClient/NRPEClient.cpp

    raf05fa1 r465866c  
    2525#include <strEx.h> 
    2626 
    27 #include <strEx.h> 
    28  
    2927#include <settings/client/settings_client.hpp> 
    3028#include <nscapi/nscapi_protobuf_functions.hpp> 
  • modules/NRPEServer/NRPEServer.cpp

    r9bd40e2 r465866c  
    2323#include <strEx.h> 
    2424#include <time.h> 
    25 //#include <config.h> 
    2625#include "handler_impl.hpp" 
    2726 
     
    8786      _T("TIMEOUT"), _T("Timeout when reading packets on incoming sockets. If the data has not arrived within this time we will bail out.")) 
    8887 
    89       (_T("use ssl"), sh::bool_key(&info_.use_ssl, true), 
    90       _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled.")) 
     88      (_T("use ssl"), sh::bool_key(&info_.ssl.enabled, false), 
     89      _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled."), true) 
    9190 
    92       (_T("certificate"), sh::wpath_key(&info_.certificate, _T("${certificate-path}/nrpe_dh_512.pem")), 
    93       _T("SSL CERTIFICATE"), _T("Configure which SSL certificate to use (DH key)")) 
     91      (_T("certificate"), sh::path_key(&info_.ssl.dh_key, "${certificate-path}/nrpe_dh_512.pem"), 
     92      _T("DH KEY"), _T(""), true) 
    9493 
     94      (_T("certificate"), sh::path_key(&info_.ssl.certificate, "${certificate-path}/certificate.pem"), 
     95      _T("SSL CERTIFICATE"), _T(""), true) 
     96 
     97      (_T("certificate key"), sh::path_key(&info_.ssl.certificate_key, "${certificate-path}/certificate_key.pem"), 
     98      _T("SSL CERTIFICATE"), _T(""), true) 
     99 
     100      (_T("certificate format"), sh::string_key(&info_.ssl.certificate_format, "PEM"), 
     101      _T("CERTIFICATE FORMAT"), _T(""), true) 
     102 
     103      (_T("ca"), sh::path_key(&info_.ssl.ca_path, "${certificate-path}/ca.pem"), 
     104      _T("CA"), _T(""), true) 
     105 
     106      (_T("allowed ciphers"), sh::string_key(&info_.ssl.allowed_ciphers, "ADH"), 
     107      _T("ALLOWED CIPHERS"), _T("A better value is: ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"), true) 
     108 
     109      (_T("verify mode"), sh::string_key(&info_.ssl.verify_mode, "none"), 
     110      _T("VERIFY MODE"), _T(""), true) 
    95111      ; 
    96112 
     
    102118    if (info_.use_ssl) { 
    103119      NSC_LOG_ERROR_STD(_T("SSL not avalible! (not compiled with openssl support)")); 
     120      return false; 
    104121    } 
    105122#endif 
    106123    if (handler_->get_payload_length() != 1024) 
    107124      NSC_DEBUG_MSG_STD(_T("Non-standard buffer length (hope you have recompiled check_nrpe changing #define MAX_PACKETBUFFER_LENGTH = ") + strEx::itos(handler_->get_payload_length())); 
    108     if (!boost::filesystem::is_regular(info_.certificate)) 
    109       NSC_LOG_ERROR_STD(_T("Certificate not found: ") + info_.certificate); 
    110  
     125    NSC_LOG_ERROR_LISTW(info_.validate()); 
    111126 
    112127    std::list<std::string> errors; 
    113128    info_.allowed_hosts.refresh(errors); 
    114     BOOST_FOREACH(const std::string &e, errors) { 
    115       NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(e)); 
    116     } 
     129    NSC_LOG_ERROR_LISTS(errors); 
    117130    NSC_DEBUG_MSG_STD(_T("Allowed hosts definition: ") + info_.allowed_hosts.to_wstring()); 
    118131 
     
    120133 
    121134    if (mode == NSCAPI::normalStart) { 
    122 #ifndef USE_SSL 
    123       if (info_.use_ssl) { 
    124         NSC_LOG_ERROR_STD(_T("SSL is not supported (not compiled with openssl)")); 
    125         return false; 
    126       } 
    127 #endif 
    128135      server_.reset(new nrpe::server::server(boost::shared_ptr<nrpe::read_protocol>(new nrpe::read_protocol(info_, handler_)))); 
    129136      if (!server_) { 
  • modules/NRPEServer/NRPEServer.h

    r9bd40e2 r465866c  
    6262 
    6363 
    64   class NRPEException { 
     64  class NRPEExceptionn { 
    6565    std::wstring error_; 
    6666  public: 
    67     NRPEException(std::wstring s) { 
     67    NRPEExceptionn(std::wstring s) { 
    6868      error_ = s; 
    6969    } 
  • modules/NSCAClient/CMakeLists.txt

    r8d89d7a r465866c  
    88  stdafx.cpp 
    99  "${TARGET}.cpp" 
     10  ${NSCP_INCLUDEDIR}/nsca/nsca_packet.cpp 
    1011  ${NSCP_INCLUDEDIR}/socket/socket_helpers.cpp 
    11   ${NSCP_INCLUDEDIR}/nsca/nsca_packet.cpp 
    1212  ${NSCP_INCLUDEDIR}/client/command_line_parser.cpp 
    1313 
     
    3131    "${TARGET}.def" 
    3232    ${NSCP_INCLUDEDIR}/nsca/nsca_packet.hpp 
    33     ${NSCP_INCLUDEDIR}/nsca/nsca_socket.hpp 
    3433    ${NSCP_INCLUDEDIR}/nsca/nsca_enrypt.hpp 
    3534    ${NSCP_INCLUDEDIR}/swap_bytes.hpp 
    3635    ${NSCP_INCLUDEDIR}/socket/socket_helpers.hpp 
     36    ${NSCP_INCLUDEDIR}/socket/client.hpp 
    3737    ${NSCP_INCLUDEDIR}/client/command_line_parser.hpp 
    3838 
     
    4040  ) 
    4141ENDIF(WIN32) 
    42  
    4342 
    4443add_library(${TARGET} MODULE ${SRCS}) 
  • modules/NSCAClient/NSCAClient.cpp

    raf05fa1 r465866c  
    2727#include <nsca/nsca_enrypt.hpp> 
    2828#include <nsca/nsca_packet.hpp> 
    29 #include <nsca/nsca_socket.hpp> 
    3029 
    3130#include <nsca/client/nsca_client_protocol.hpp> 
  • modules/NSCAServer/NSCAServer.cpp

    r9bd40e2 r465866c  
    102102    } 
    103103    NSC_DEBUG_MSG_STD(_T("Allowed hosts definition: ") + info_.allowed_hosts.to_wstring()); 
     104    NSC_LOG_ERROR_LISTW(info_.validate()); 
    104105 
    105106    if (mode == NSCAPI::normalStart) { 
    106 #ifndef USE_SSL 
    107       if (info_.use_ssl) { 
    108         NSC_LOG_ERROR_STD(_T("SSL is not supported (not compiled with openssl)")); 
    109         return false; 
    110       } 
    111 #endif 
     107 
    112108      server_.reset(new nsca::server::server(boost::shared_ptr<nsca::read_protocol>(new nsca::read_protocol(info_, handler_)))); 
    113109      if (!server_) { 
  • modules/NSCPClient/CMakeLists.txt

    r8d89d7a r465866c  
    99  "${TARGET}.cpp" 
    1010  ${NSCP_INCLUDEDIR}/nscp/packet.cpp 
    11   ${NSCP_INCLUDEDIR}/nscp/handler.cpp 
    1211  ${NSCP_INCLUDEDIR}/socket/socket_helpers.cpp 
    1312  ${NSCP_INCLUDEDIR}/client/command_line_parser.cpp 
     
    1716 
    1817ADD_DEFINITIONS(${NSCP_GLOBAL_DEFINES}) 
     18IF(OPENSSL_FOUND) 
     19  ADD_DEFINITIONS(-DUSE_SSL) 
     20ENDIF(OPENSSL_FOUND) 
    1921 
    2022IF(WIN32) 
     
    2426    "${TARGET}.def" 
    2527    ${NSCP_INCLUDEDIR}/nscp/packet.hpp 
    26     ${NSCP_INCLUDEDIR}/nscp/handler.hpp 
    27     ${NSCP_INCLUDEDIR}/nscp/client/socket.hpp 
     28    ${NSCP_INCLUDEDIR}/nscp/client/nscp_client_protocol.hpp 
    2829    ${NSCP_INCLUDEDIR}/swap_bytes.hpp 
    2930    ${NSCP_INCLUDEDIR}/socket/socket_helpers.hpp 
     31    ${NSCP_INCLUDEDIR}/socket/client.hpp 
    3032    ${NSCP_INCLUDEDIR}/client/command_line_parser.hpp 
    3133 
     
    4446) 
    4547INCLUDE(${BUILD_CMAKE_FOLDER}/module.cmake) 
    46 SOURCE_GROUP("Server" REGULAR_EXPRESSION .*include/nscp/.*) 
     48SOURCE_GROUP("Client" REGULAR_EXPRESSION .*include/nscp/.*) 
     49SOURCE_GROUP("Socket" REGULAR_EXPRESSION .*include/socket/.*) 
  • modules/NSCPClient/NSCPClient.cpp

    r84cdb9b r465866c  
    2121#include "stdafx.h" 
    2222#include "NSCPClient.h" 
     23 
    2324#include <time.h> 
    24 #include <boost/filesystem.hpp> 
    25  
    2625#include <strEx.h> 
    27 #include <net/net.hpp> 
    28 #include <nscp/client/socket.hpp> 
    2926 
    3027#include <protobuf/plugin.pb.h> 
     
    6461 
    6562    sh::settings_registry settings(get_settings_proxy()); 
    66     settings.set_alias(_T("NSCP"), alias, _T("client")); 
     63    settings.set_alias(_T("nscp"), alias, _T("client")); 
    6764    target_path = settings.alias().get_settings_path(_T("targets")); 
    6865 
     
    7572      (_T("targets"), sh::fun_values_path(boost::bind(&NSCPClient::add_target, this, _1, _2)),  
    7673      _T("REMOTE TARGET DEFINITIONS"), _T("")) 
    77  
    7874      ; 
    7975 
     
    9692    register_command(_T("nscp_exec"), _T("Execute remote command on a remote host via NSCP")); 
    9793    register_command(_T("nscp_help"), _T("Help on using NSCP Client")); 
    98  
    9994  } catch (nscapi::nscapi_exception &e) { 
    10095    NSC_LOG_ERROR_STD(_T("NSClient API exception: ") + utf8::to_unicode(e.what())); 
     
    193188  desc.add_options() 
    194189    ("certificate,c", po::value<std::string>()->notifier(boost::bind(&nscapi::functions::destination_container::set_string_data, &data->recipient, "certificate", _1)),  
    195       "Length of payload (has to be same as on the server)") 
    196 /* 
    197     ("no-ssl,n", po::value<bool>(&command_data.no_ssl)->zero_tokens()->default_value(false), "Do not initial an ssl handshake with the server, talk in plain text.") 
    198  
    199     ("cert,c", po::value<std::wstring>(&command_data.cert)->default_value(cert_), "Certificate to use.") 
    200     */ 
     190    "Length of payload (has to be same as on the server)") 
     191 
     192    ("no-ssl,n", po::value<bool>()->zero_tokens()->default_value(false)->notifier(boost::bind(&nscapi::functions::destination_container::set_bool_data, &data->recipient, "no ssl", _1)),  
     193    "Do not initial an ssl handshake with the server, talk in plaintext.") 
    201194    ; 
    202195} 
     
    209202  std::wstring recipient = utf8::cvt<std::wstring>(config.data->recipient.id); 
    210203  if (!targets.has_object(recipient)) { 
    211     NSC_LOG_ERROR(_T("Target not found (using default): ") + recipient); 
    212204    recipient = _T("default"); 
    213205  } 
     
    255247 
    256248  std::list<nscp::packet> chunks; 
    257   chunks.push_back(nscp::factory::create_envelope_request(1)); 
    258249  chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request_message.SerializeAsString(), 0)); 
    259250  chunks = instance->send(con, chunks); 
     
    311302 
    312303  std::list<nscp::packet> chunks; 
    313   chunks.push_back(nscp::factory::create_envelope_request(1)); 
    314304  chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request_message.SerializeAsString(), 0)); 
    315305  chunks = instance->send(con, chunks); 
     
    334324// Protocol implementations 
    335325// 
     326struct client_handler : public socket_helpers::client::client_handler { 
     327  client_handler(NSCPClient::connection_data &con)  
     328    : socket_helpers::client::client_handler(con.host, con.port, con.timeout, con.use_ssl, con.cert) 
     329  { 
     330 
     331  } 
     332  void log_debug(std::string file, int line, std::string msg) const { 
     333    if (GET_CORE()->should_log(NSCAPI::log_level::debug)) { 
     334      GET_CORE()->log(NSCAPI::log_level::debug, file, line, utf8::to_unicode(msg)); 
     335    } 
     336  } 
     337  void log_error(std::string file, int line, std::string msg) const { 
     338    if (GET_CORE()->should_log(NSCAPI::log_level::error)) { 
     339      GET_CORE()->log(NSCAPI::log_level::error, file, line, utf8::to_unicode(msg)); 
     340    } 
     341  } 
     342}; 
    336343 
    337344std::list<nscp::packet> NSCPClient::send(connection_data con, std::list<nscp::packet> &chunks) { 
    338   NSC_DEBUG_MSG_STD(_T("NRPE Connection details: ") + con.to_wstring()); 
    339   chunks.push_front(nscp::factory::create_envelope_request(1)); 
    340   std::list<nscp::packet> tmp, result; 
    341   if (con.use_ssl) { 
    342 #ifdef USE_SSL 
    343     tmp = send_ssl(con.host, con.port, con.cert, con.timeout, chunks); 
    344 #else 
    345     NSC_LOG_ERROR_STD(_T("SSL not avalible (not compiled with USE_SSL)")); 
    346     result.push_back(nscp::factory::create_error(_T("SSL support not available (compiled without USE_SSL)!"))); 
     345  std::list<nscp::packet> response; 
     346  try { 
     347    NSC_DEBUG_MSG_STD(_T("NSCP Connection details: ") + con.to_wstring()); 
     348    //NSC_DEBUG_MSG_STD(_T("NSCP data: ") + utf8::cvt<std::wstring>(data)); 
     349    if (con.use_ssl) { 
     350#ifndef USE_SSL 
     351      NSC_LOG_ERROR_STD(_T("SSL not avalible (compiled without USE_SSL)")); 
     352      return response; 
    347353#endif 
    348   } else { 
    349     tmp = send_nossl(con.host, con.port, con.timeout, chunks); 
    350   } 
    351   BOOST_FOREACH(nscp::packet &p, tmp) { 
    352     if (nscp::checks::is_envelope_response(p)) { 
    353     } else { 
    354       result.push_back(p); 
    355     } 
    356   } 
    357   return result; 
    358 } 
    359  
    360 #ifdef USE_SSL 
    361 std::list<nscp::packet> NSCPClient::send_ssl(std::string host, std::string port, std::wstring cert, int timeout, const std::list<nscp::packet> &chunks) { 
    362   NSC_DEBUG_MSG_STD(_T("Connecting SSL to: ") + utf8::cvt<std::wstring>(host + ":" + port)); 
    363   boost::asio::io_service io_service; 
    364   boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::sslv23); 
    365   SSL_CTX_set_cipher_list(ctx.impl(), "ADH"); 
    366   ctx.use_tmp_dh_file(to_string(cert)); 
    367   ctx.set_verify_mode(boost::asio::ssl::context::verify_none); 
    368   nscp::client::ssl_socket socket(io_service, ctx, host, port); 
    369   socket.send(chunks, boost::posix_time::seconds(timeout)); 
    370   return socket.recv(boost::posix_time::seconds(timeout)); 
    371 } 
    372 #endif 
    373  
    374 std::list<nscp::packet> NSCPClient::send_nossl(std::string host, std::string port, int timeout, const std::list<nscp::packet> &chunks) { 
    375   NSC_DEBUG_MSG_STD(_T("Connecting to: ") + utf8::cvt<std::wstring>(host + ":" + port)); 
    376   boost::asio::io_service io_service; 
    377   nscp::client::socket socket(io_service, host, port); 
    378   socket.send(chunks, boost::posix_time::seconds(timeout)); 
    379   return socket.recv(boost::posix_time::seconds(timeout)); 
     354    } 
     355    socket_helpers::client::client<nscp::client::protocol> client(boost::shared_ptr<client_handler>(new client_handler(con))); 
     356    client.connect(); 
     357    BOOST_FOREACH(nscp::packet packet, chunks) { 
     358      response.push_back(client.process_request(packet)); 
     359    } 
     360    client.shutdown(); 
     361    return response; 
     362  } catch (std::runtime_error &e) { 
     363    NSC_LOG_ERROR_STD(_T("Socket error: ") + utf8::to_unicode(e.what())); 
     364    return response; 
     365  } catch (std::exception &e) { 
     366    NSC_LOG_ERROR_STD(_T("Error: ") + utf8::to_unicode(e.what())); 
     367    return response; 
     368  } catch (...) { 
     369    return response; 
     370  } 
    380371} 
    381372 
  • modules/NSCPClient/NSCPClient.h

    r8d89d7a r465866c  
    2929 
    3030#include <nscp/packet.hpp> 
     31 
     32#include <socket/client.hpp> 
     33#include <nscp/client/nscp_client_protocol.hpp> 
     34 
    3135 
    3236NSC_WRAPPERS_MAIN(); 
     
    101105  nscapi::targets::handler<custom_reader> targets; 
    102106  client::command_manager commands; 
    103  
     107public: 
    104108  struct connection_data { 
    105109    std::string cert; 
    106     connection_data() : use_ssl(true) {} 
     110    std::string host; 
     111    std::string port; 
     112    int timeout; 
    107113    bool use_ssl; 
    108     std::string host, port; 
    109     int timeout; 
    110114 
    111115    connection_data(nscapi::protobuf::types::destination_container arguments, nscapi::protobuf::types::destination_container target) { 
     
    120124 
    121125      host = arguments.address.host; 
    122       port = arguments.address.get_port(5668); 
     126      port = arguments.address.get_port_string("5668"); 
    123127    } 
    124128 
     
    197201private: 
    198202  std::list<nscp::packet> send(connection_data con, std::list<nscp::packet> &chunks); 
    199   std::list<nscp::packet> send_nossl(std::string host, std::string port, int timeout, const std::list<nscp::packet> &chunks); 
    200   std::list<nscp::packet> send_ssl(std::string host, std::string port, std::wstring cert, int timeout, const std::list<nscp::packet> &chunks); 
    201203 
    202204 
  • modules/NSCPServer/CMakeLists.txt

    r440c0cb r465866c  
    1212  "${TARGET}.cpp" 
    1313  "handler_impl.cpp" 
    14   ${NSCP_INCLUDEDIR}/nscp/server/server.cpp 
    15   ${NSCP_INCLUDEDIR}/nscp/server/connection.cpp 
    16   ${NSCP_INCLUDEDIR}/nscp/server/tcp_connection.cpp 
    17   ${NSCP_INCLUDEDIR}/nscp/server/ssl_connection.cpp 
    1814  ${NSCP_INCLUDEDIR}/nscp/packet.cpp 
    1915  ${NSCP_INCLUDEDIR}/socket/socket_helpers.cpp 
    20   ${NSCP_INCLUDEDIR}/nscp/handler.cpp 
    2116 
    2217  ${NSCP_DEF_PLUGIN_CPP} 
     
    3429    "${TARGET}.def" 
    3530    "handler_impl.hpp" 
    36     ${NSCP_INCLUDEDIR}/nscp/server/server.hpp 
    37     ${NSCP_INCLUDEDIR}/nscp/server/connection.hpp 
    38     ${NSCP_INCLUDEDIR}/nscp/server/tcp_connection.hpp 
    39     ${NSCP_INCLUDEDIR}/nscp/server/ssl_connection.hpp 
     31    ${NSCP_INCLUDEDIR}/nscp/server/protocol.hpp 
    4032    ${NSCP_INCLUDEDIR}/nscp/server/handler.hpp 
    4133    ${NSCP_INCLUDEDIR}/nscp/server/parser.hpp 
    4234    ${NSCP_INCLUDEDIR}/nscp/packet.hpp 
    43     ${NSCP_INCLUDEDIR}/nscp/handler.hpp 
    4435    ${NSCP_INCLUDEDIR}/swap_bytes.hpp 
    4536    ${NSCP_INCLUDEDIR}/socket/socket_helpers.hpp 
  • modules/NSCPServer/NSCPServer.cpp

    r84cdb9b r465866c  
    2323#include <strEx.h> 
    2424#include <time.h> 
    25 #include <config.h> 
    2625#include "handler_impl.hpp" 
    2726 
    2827#include <settings/client/settings_client.hpp> 
    2928 
    30  
    3129namespace sh = nscapi::settings_helper; 
    3230 
    33 NSCPListener::NSCPListener() : info_(boost::shared_ptr<nscp::server::server_handler>(new handler_impl(1024))) { 
     31 
     32NSCPListener::NSCPListener() : handler_(new handler_impl()) { 
    3433} 
    3534NSCPListener::~NSCPListener() {} 
     
    4140bool NSCPListener::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 
    4241  try { 
     42 
    4343    sh::settings_registry settings(get_settings_proxy()); 
    4444    settings.set_alias(_T("nscp"), alias, _T("server")); 
     
    5252      _T("PORT NUMBER"), _T("Port to use for NSCP.")) 
    5353 
     54      (_T("allow arguments"), sh::bool_fun_key<bool>(boost::bind(&handler_impl::set_allow_arguments, handler_, _1), false), 
     55      _T("COMMAND ARGUMENT PROCESSING"), _T("This option determines whether or not the we will allow clients to specify arguments to commands that are executed.")) 
     56 
     57 
    5458      ; 
    5559 
     
    5761 
    5862      (_T("thread pool"), sh::uint_key(&info_.thread_pool_size, 10), 
    59       _T("THREAD POOL"), _T("")) 
     63      _T("THREAD POOL"), _T(""), true) 
    6064 
    6165      (_T("bind to"), sh::string_key(&info_.address), 
    62       _T("BIND TO ADDRESS"), _T("Allows you to bind server to a specific local address. This has to be a dotted ip address not a host name. Leaving this blank will bind to all available IP addresses.")) 
     66      _T("BIND TO ADDRESS"), _T("Allows you to bind server to a specific local address. This has to be a dotted ip address not a host name. Leaving this blank will bind to all available IP addresses."), true) 
    6367 
    6468      (_T("socket queue size"), sh::int_key(&info_.back_log, 0), 
    65       _T("LISTEN QUEUE"), _T("Number of sockets to queue before starting to refuse new incoming connections. This can be used to tweak the amount of simultaneous sockets that the server accepts.")) 
     69      _T("LISTEN QUEUE"), _T("Number of sockets to queue before starting to refuse new incoming connections. This can be used to tweak the amount of simultaneous sockets that the server accepts."), true) 
    6670 
    6771      (_T("allowed hosts"), sh::string_fun_key<std::wstring>(boost::bind(&socket_helpers::allowed_hosts_manager::set_source, &info_.allowed_hosts, _1), _T("127.0.0.1")), 
     
    7478      _T("TIMEOUT"), _T("Timeout when reading packets on incoming sockets. If the data has not arrived within this time we will bail out.")) 
    7579 
    76       (_T("use ssl"), sh::bool_key(&info_.use_ssl, true), 
    77       _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled.")) 
     80      (_T("use ssl"), sh::bool_key(&info_.ssl.enabled, false), 
     81      _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled."), true) 
    7882 
    79       (_T("certificate"), sh::wpath_key(&info_.certificate, _T("${certificate-path}/nrpe_dh_512.pem")), 
    80       _T("SSL CERTIFICATE"), _T("")) 
     83      (_T("certificate"), sh::path_key(&info_.ssl.dh_key, "${certificate-path}/nrpe_dh_512.pem"), 
     84      _T("DH KEY"), _T(""), true) 
    8185 
     86      (_T("certificate"), sh::path_key(&info_.ssl.certificate, "${certificate-path}/certificate.pem"), 
     87      _T("SSL CERTIFICATE"), _T(""), true) 
     88 
     89      (_T("certificate key"), sh::path_key(&info_.ssl.certificate_key, "${certificate-path}/certificate_key.pem"), 
     90      _T("SSL CERTIFICATE"), _T(""), true) 
     91 
     92      (_T("certificate format"), sh::string_key(&info_.ssl.certificate_format, "PEM"), 
     93      _T("CERTIFICATE FORMAT"), _T(""), true) 
     94 
     95      (_T("ca"), sh::path_key(&info_.ssl.ca_path, "${certificate-path}/ca.pem"), 
     96      _T("CA"), _T(""), true) 
     97 
     98      (_T("allowed ciphers"), sh::string_key(&info_.ssl.allowed_ciphers, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"), 
     99      _T("ALLOWED CIPHERS"), _T(""), true) 
     100 
     101      (_T("verify mode"), sh::string_key(&info_.ssl.verify_mode, "none"), 
     102      _T("VERIFY MODE"), _T(""), true) 
    82103      ; 
    83104 
     
    89110    if (info_.use_ssl) { 
    90111      NSC_LOG_ERROR_STD(_T("SSL not avalible! (not compiled with openssl support)")); 
     112      return false; 
    91113    } 
    92114#endif 
    93     if (!boost::filesystem::is_regular(info_.certificate)) 
    94       NSC_LOG_ERROR_STD(_T("Certificate not found: ") + info_.certificate); 
     115    NSC_LOG_ERROR_LISTW(info_.validate()); 
    95116 
    96117    std::list<std::string> errors; 
    97118    info_.allowed_hosts.refresh(errors); 
    98     BOOST_FOREACH(const std::string &e, errors) { 
    99       NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(e)); 
    100     } 
     119    NSC_LOG_ERROR_LISTS(errors); 
    101120    NSC_DEBUG_MSG_STD(_T("Allowed hosts definition: ") + info_.allowed_hosts.to_wstring()); 
    102121 
     
    104123 
    105124    if (mode == NSCAPI::normalStart) { 
    106       if (info_.use_ssl) { 
    107 #ifdef USE_SSL 
    108         server_.reset(new nscp::server::server(info_)); 
    109 #else 
    110         NSC_LOG_ERROR_STD(_T("SSL is not supported (not compiled with openssl)")); 
    111         return false; 
    112 #endif 
    113       } else { 
    114         server_.reset(new nscp::server::server(info_)); 
    115       } 
     125      server_.reset(new nscp::server::server(boost::shared_ptr<nscp::read_protocol>(new nscp::read_protocol(info_, handler_)))); 
    116126      if (!server_) { 
    117127        NSC_LOG_ERROR_STD(_T("Failed to create server instance!")); 
     
    120130      server_->start(); 
    121131    } 
    122   } catch (nscp::server::nscp_exception &e) { 
    123     NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.what()); 
    124     return false; 
    125132  } catch (std::exception &e) { 
    126133    NSC_LOG_ERROR_STD(_T("Exception caught: ") + to_wstring(e.what())); 
  • modules/NSCPServer/NSCPServer.h

    r81e420c r465866c  
    2121 
    2222#include <socket_helpers.hpp> 
    23 #include <nscp/server/server.hpp> 
     23#include <nscp/server/protocol.hpp> 
     24#include "handler_impl.hpp" 
    2425 
    2526NSC_WRAPPERS_MAIN(); 
    2627 
    2728class NSCPListener : public nscapi::impl::simple_plugin { 
    28 private: 
    29   typedef enum { 
    30     inject, script, script_dir, 
    31   } command_type; 
    32   struct command_data { 
    33     command_data() : type(inject) {} 
    34     command_data(command_type type_, std::wstring arguments_) : type(type_), arguments(arguments_) {} 
    35     command_type type; 
    36     std::wstring arguments; 
    37   }; 
    38  
    39   nscp::server::server::connection_info info_; 
    40  
    4129public: 
    4230  NSCPListener(); 
     
    6755  NSCAPI::nagiosReturn handleCommand(const strEx::blindstr command, const unsigned int argLen, wchar_t **char_args, std::wstring &message, std::wstring &perf); 
    6856  std::wstring getConfigurationMeta(); 
     57   
     58private: 
     59  socket_helpers::connection_info info_; 
    6960  boost::shared_ptr<nscp::server::server> server_; 
     61  boost::shared_ptr<handler_impl> handler_; 
    7062}; 
    7163 
  • modules/NSCPServer/handler_impl.cpp

    r7515d00 r465866c  
    77#include "handler_impl.hpp" 
    88 
     9 
     10 
     11nscp::packet handler_impl::process(const nscp::packet &packet) { 
     12  if (nscp::checks::is_query_request(packet)) { 
     13    Plugin::QueryRequestMessage msg; 
     14    msg.ParseFromString(packet.payload); 
     15    std::wstring command = _T("todo: fixme");//utf8::cvt<std::wstring>(msg.command()); 
     16 
     17    std::string reply; 
     18    try { 
     19      NSCAPI::nagiosReturn returncode = handle_query_request(packet.payload, msg, reply); 
     20      if (returncode == NSCAPI::returnIgnored) 
     21        nscapi::functions::create_simple_query_response_unknown(command, _T("Command was not found: ") + command, _T(""), reply); 
     22    } catch (const nscp::nscp_exception &e) { 
     23      nscapi::functions::create_simple_query_response_unknown(command, _T("Processing error: ") + command + _T(": ") + utf8::cvt<std::wstring>(e.what()), _T(""), reply); 
     24    } catch (const std::exception &e) { 
     25      nscapi::functions::create_simple_query_response_unknown(command, _T("Unknown error processing: ") + command + _T(": ") + utf8::cvt<std::wstring>(e.what()), _T(""), reply); 
     26    } 
     27    return nscp::factory::create_query_response(reply); 
     28  } else if (nscp::checks::is_submit_request(packet)) { 
     29    Plugin::SubmitRequestMessage msg; 
     30    msg.ParseFromString(packet.payload); 
     31    try { 
     32      std::string reply; 
     33      NSCAPI::nagiosReturn returncode = handle_submission_request(packet.payload, msg, reply); 
     34      return nscp::factory::create_submission_response(reply); 
     35    } catch (const nscp::nscp_exception &e) { 
     36      return nscp::factory::create_error(_T("Exception processing message: ") + to_wstring(e.what())); 
     37    } catch (const std::exception &e) { 
     38      return nscp::factory::create_error(_T("Exception processing message: ") + to_wstring(e.what())); 
     39    } 
     40  } else if (nscp::checks::is_exec_request(packet)) { 
     41    Plugin::ExecuteRequestMessage msg; 
     42    msg.ParseFromString(packet.payload); 
     43    try { 
     44      std::string reply; 
     45      NSCAPI::nagiosReturn returncode = handle_exec_request(packet.payload, msg, reply); 
     46      return nscp::factory::create_submission_response(reply); 
     47    } catch (const nscp::nscp_exception &e) { 
     48      return nscp::factory::create_error(_T("Exception processing message: ") + to_wstring(e.what())); 
     49    } catch (const std::exception &e) { 
     50      return nscp::factory::create_error(_T("Exception processing message: ") + to_wstring(e.what())); 
     51    } 
     52  } else { 
     53    this->log_error("nscp:handler", __FILE__, __LINE__, "Unknown packet: " + packet.to_string()); 
     54    return nscp::factory::create_error(_T("Unknown packet: ") + packet.to_wstring()); 
     55  } 
     56  return nscp::factory::create_error(_T("Unknown error...")); 
     57} 
    958NSCAPI::nagiosReturn handler_impl::handle_query_request(const std::string &request, Plugin::QueryRequestMessage &msg, std::string &reply) { 
    1059  Plugin::Common::Header hdr; 
     
    2675      nscapi::functions::create_simple_query_response_unknown(command, _T("Arguments not allowed for command: ") + command, _T(""), outBuffer); 
    2776    } else { 
    28       bool ok = true; 
    29       if (!allowNasty_) { 
    30         for (int j=0;j<payload.arguments_size();j++) { 
    31           if (payload.arguments(j).find_first_of(NASTY_METACHARS) != std::wstring::npos) { 
    32             ok = false; 
    33             break; 
    34           } 
    35         } 
    36       } 
    37       if (ok) { 
    38         std::string tmpBuffer; 
    39         Plugin::QueryRequestMessage tmp; 
    40         tmp.mutable_header()->CopyFrom(hdr); 
    41         tmp.add_payload()->CopyFrom(payload); 
    42         tmp.SerializeToString(&tmpBuffer); 
    43         NSCAPI::nagiosReturn returncode = nscapi::plugin_singleton->get_core()->query(command, tmpBuffer, outBuffer); 
    44         if (returncode == NSCAPI::returnIgnored) { 
    45           nscapi::functions::create_simple_query_response_unknown(command, _T("Command was not found: ") + command, _T(""), outBuffer); 
    46         } 
    47       } else { 
    48         nscapi::functions::create_simple_query_response_unknown(command, _T("Nasty arguments not allowed for command: ") + command, _T(""), outBuffer); 
     77      std::string tmpBuffer; 
     78      Plugin::QueryRequestMessage tmp; 
     79      tmp.mutable_header()->CopyFrom(hdr); 
     80      tmp.add_payload()->CopyFrom(payload); 
     81      tmp.SerializeToString(&tmpBuffer); 
     82      NSCAPI::nagiosReturn returncode = nscapi::plugin_singleton->get_core()->query(command, tmpBuffer, outBuffer); 
     83      if (returncode == NSCAPI::returnIgnored) { 
     84        nscapi::functions::create_simple_query_response_unknown(command, _T("Command was not found: ") + command, _T(""), outBuffer); 
    4985      } 
    5086      Plugin::QueryResponseMessage tmpResponse; 
  • modules/NSCPServer/handler_impl.hpp

    r8013c0c r465866c  
    22 
    33#include <nscp/packet.hpp> 
    4 #include <nscp/handler.hpp> 
     4#include <nscp/server/handler.hpp> 
    55#include <boost/tuple/tuple.hpp> 
    66 
    7 class handler_impl : private boost::noncopyable, public nscp::handler { 
    8   unsigned int payload_length_; 
     7class handler_impl : public nscp::server::handler { 
    98  bool allowArgs_; 
    10   bool allowNasty_; 
    11   bool noPerfData_; 
    129public: 
    13   handler_impl(unsigned int payload_length) : payload_length_(payload_length), noPerfData_(false), allowNasty_(false), allowArgs_(false) {} 
     10  handler_impl() : allowArgs_(false) {} 
    1411 
    1512  NSCAPI::nagiosReturn handle_query_request(const std::string &request, Plugin::QueryRequestMessage &msg, std::string &reply); 
     
    2623    allowArgs_ = v; 
    2724  } 
    28   virtual void set_allow_nasty_arguments(bool v) { 
    29     allowNasty_ = v; 
    30   } 
    31   virtual void set_perf_data(bool v) { 
    32     noPerfData_ = !v; 
    33     if (noPerfData_) 
    34       log_debug(__FILE__, __LINE__, _T("Performance data disabled!")); 
    35   } 
    3625 
    37   void log_debug(std::string file, int line, std::wstring msg) { 
     26  virtual nscp::packet process(const nscp::packet &packet); 
     27 
     28  virtual void log_debug(std::string module, std::string file, int line, std::string msg) const { 
    3829    if (GET_CORE()->should_log(NSCAPI::log_level::debug)) { 
    3930      GET_CORE()->log(NSCAPI::log_level::debug, file, line, msg); 
    4031    } 
    4132  } 
    42   void log_error(std::string file, int line, std::wstring msg) { 
     33  virtual void log_error(std::string module, std::string file, int line, std::string msg) const { 
    4334    if (GET_CORE()->should_log(NSCAPI::log_level::error)) { 
    4435      GET_CORE()->log(NSCAPI::log_level::error, file, line, msg); 
  • modules/NSCPServer/stdafx.h

    r438998b r465866c  
    4747#endif 
    4848 
    49 #include <config.h> 
     49#include <strEx.h> 
    5050#include <utils.h> 
    5151 
  • modules/NSClientServer/NSClientServer.cpp

    r9bd40e2 r465866c  
    7171      _T("LISTEN QUEUE"), _T("Number of sockets to queue before starting to refuse new incoming connections. This can be used to tweak the amount of simultaneous sockets that the server accepts."), true) 
    7272 
    73       (_T("use ssl"), sh::bool_key(&info_.use_ssl, false), 
     73      (_T("use ssl"), sh::bool_key(&info_.ssl.enabled, false), 
    7474      _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled."), true) 
    7575 
    76       (_T("certificate"), sh::wpath_key(&info_.certificate, _T("${certificate-path}/nrpe_dh_512.pem")), 
     76      (_T("certificate"), sh::path_key(&info_.ssl.dh_key, "${certificate-path}/nrpe_dh_512.pem"), 
     77      _T("DH KEY"), _T(""), true) 
     78 
     79      (_T("certificate"), sh::path_key(&info_.ssl.certificate, "${certificate-path}/certificate.pem"), 
    7780      _T("SSL CERTIFICATE"), _T(""), true) 
     81 
     82      (_T("certificate key"), sh::path_key(&info_.ssl.certificate_key, "${certificate-path}/certificate_key.pem"), 
     83      _T("SSL CERTIFICATE"), _T(""), true) 
     84 
     85      (_T("certificate format"), sh::string_key(&info_.ssl.certificate_format, "PEM"), 
     86      _T("CERTIFICATE FORMAT"), _T(""), true) 
     87 
     88      (_T("ca"), sh::path_key(&info_.ssl.ca_path, "${certificate-path}/ca.pem"), 
     89      _T("CA"), _T(""), true) 
     90 
     91      (_T("allowed ciphers"), sh::string_key(&info_.ssl.allowed_ciphers, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"), 
     92      _T("ALLOWED CIPHERS"), _T(""), true) 
     93 
     94      (_T("verify mode"), sh::string_key(&info_.ssl.verify_mode, "none"), 
     95      _T("VERIFY MODE"), _T(""), true) 
    7896 
    7997      ; 
     
    109127  } 
    110128#endif 
    111   if (!boost::filesystem::is_regular(info_.certificate)) 
    112     NSC_LOG_ERROR_STD(_T("Certificate not found: ") + info_.certificate); 
    113  
     129  NSC_LOG_ERROR_LISTW(info_.validate()); 
    114130 
    115131  std::list<std::string> errors; 
  • modules/Scheduler/CMakeLists.txt

    r8d89d7a r465866c  
    2121    "${TARGET}.def" 
    2222    simple_scheduler.hpp 
     23    schedules.hpp 
    2324 
    2425    ${NSCP_DEF_PLUGIN_HPP} 
  • scripts/python/test_eventlog.py

    r74e060a r465866c  
    1616  last_message = None 
    1717  last_perfdata = None 
     18  last_tag = [] 
    1819  got_simple_response = None 
    1920  message_count = 0 
     
    3738    self.key = '_%stest_command'%prefix 
    3839    self.reg = Registry.get(plugin_id) 
    39     self.reg.simple_subscription('pytest_evlog', EventLogTest.simple_inbox_handler) 
    40  
    41   def simple_inbox_handler(channel, source, command, code, message, perf): 
     40    self.reg.simple_subscription('pytest_evlog_01', EventLogTest.simple_inbox_handler_01) 
     41    self.reg.simple_subscription('pytest_evlog_02', EventLogTest.simple_inbox_handler_02) 
     42 
     43  def simple_inbox_handler_01(channel, source, command, code, message, perf): 
    4244    instance = EventLogTest.getInstance() 
    43     return instance.simple_inbox_handler_wrapped(channel, source, command, code, message, perf) 
    44   simple_inbox_handler = Callable(simple_inbox_handler) 
    45  
    46   def simple_inbox_handler_wrapped(self, channel, source, command, status, message, perf): 
     45    return instance.simple_inbox_handler_wrapped(channel, source, command, code, message, perf, '001') 
     46  simple_inbox_handler_01 = Callable(simple_inbox_handler_01) 
     47 
     48  def simple_inbox_handler_02(channel, source, command, code, message, perf): 
     49    instance = EventLogTest.getInstance() 
     50    return instance.simple_inbox_handler_wrapped(channel, source, command, code, message, perf, '002') 
     51  simple_inbox_handler_02 = Callable(simple_inbox_handler_02) 
     52   
     53  def simple_inbox_handler_wrapped(self, channel, source, command, status, message, perf, tag): 
    4754    message = unicodedata.normalize('NFKD', message).encode('ascii','ignore') 
    4855    log('Got simple message %s on %s'%(command, channel)) 
     
    5259    self.last_status = status 
    5360    self.last_message = message 
     61    if self.last_tag: 
     62      self.last_tag.append(tag) 
     63    else: 
     64      self.last_tag = [ tag ] 
    5465    self.message_count = self.message_count + 1 
    5566    self.last_perfdata = perf 
     
    105116    sleep(500) 
    106117    result.assert_equals(self.last_message, 'error Application Error: ', 'Verify that message is sent through') 
    107     result.assert_equals(self.message_count, 1, 'Verify that onlyt one message is sent through') 
     118    result.assert_equals(self.message_count, 1, 'Verify that only one message is sent through') 
     119    log('Got tags: %s'%self.last_tag) 
    108120 
    109121    result.add_message(self.test_create('Application Error', 1000, 'info', 2, 1, 5, a_list), 'Testing to create a log message') 
     
    113125 
    114126    (res, msg, perf) = Core.get().simple_query('CheckEventLogCACHE', ['warn=eq:1', 'crit=eq:2']) 
    115     cache.assert_equals(res, status.CRITICAL, "Validate cache has items") 
     127    cache.assert_equals(res, status.CRITICAL, "Validate cache has items: %s"%msg) 
    116128    cache.assert_equals(msg, 'error Application Error: , info Application Error: , eventlog: 2 = critical', "Validate cache is ok: %s"%msg) 
    117129    cache.assert_equals(perf, "'eventlog'=2;1;2", "Validate cache is ok: %s"%msg) 
     
    145157    return result 
    146158 
     159  def install_filter(self, conf, path, target, filter): 
     160    conf.set_string(path, 'filter', filter) 
     161    conf.set_string(path, 'maximum age', '5s') 
     162    conf.set_string(path, 'destination', target) 
     163    conf.set_string(path, 'language', 'english') 
     164    conf.set_string(path, 'debug', 'true') 
     165   
    147166  def install(self, arguments): 
    148167    conf = Settings.get() 
     
    153172     
    154173    conf.set_string('/settings/pytest_eventlog/real-time', 'enabled', 'true') 
    155     conf.set_string('/settings/pytest_eventlog/real-time', 'filter', 'id = 1000 and category = 0') 
    156     conf.set_string('/settings/pytest_eventlog/real-time/filters', 'test', 'id = 1000 and category = 1') 
     174     
     175    self.install_filter(conf, '/settings/pytest_eventlog/real-time/filters/py_test_001', 'pytest_evlog_01', 'id = 1000 and category = 0') 
     176    self.install_filter(conf, '/settings/pytest_eventlog/real-time/filters/py_test_002', 'pytest_evlog_02', 'id = 1000 and category = 1') 
     177     
    157178    conf.set_string('/settings/pytest_eventlog/real-time', 'maximum age', '5s') 
    158     conf.set_string('/settings/pytest_eventlog/real-time', 'destination', 'pytest_evlog') 
    159     conf.set_string('/settings/pytest_eventlog/real-time', 'language', 'english') 
    160179    conf.set_string('/settings/pytest_eventlog/real-time', 'debug', 'true') 
    161180    conf.set_string('/settings/pytest_eventlog/real-time', 'enable active', 'true') 
  • version.hpp

    r695f240 r465866c  
    11#ifndef VERSION_HPP 
    22#define VERSION_HPP 
    3 #define PRODUCTVER     0,4,0,174 
    4 #define STRPRODUCTVER  "0,4,0,174" 
    5 #define STRPRODUCTDATE "2012-05-19" 
     3#define PRODUCTVER     0,4,1,0 
     4#define STRPRODUCTVER  "0,4,1,0" 
     5#define STRPRODUCTDATE "2012-04-23" 
    66#endif // VERSION_HPP 
Note: See TracChangeset for help on using the changeset viewer.