Ignore:
Timestamp:
06/05/12 07:35:30 (12 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)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 
Note: See TracChangeset for help on using the changeset viewer.