Changeset 465866c in nscp for modules/NSCPClient/NSCPClient.cpp
- Timestamp:
- 06/05/12 07:35:30 (12 months ago)
- Branches:
- master, 0.4.1, 0.4.2
- Children:
- 742b035
- Parents:
- 695f240
- File:
-
- 1 edited
-
modules/NSCPClient/NSCPClient.cpp (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
modules/NSCPClient/NSCPClient.cpp
r84cdb9b r465866c 21 21 #include "stdafx.h" 22 22 #include "NSCPClient.h" 23 23 24 #include <time.h> 24 #include <boost/filesystem.hpp>25 26 25 #include <strEx.h> 27 #include <net/net.hpp>28 #include <nscp/client/socket.hpp>29 26 30 27 #include <protobuf/plugin.pb.h> … … 64 61 65 62 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")); 67 64 target_path = settings.alias().get_settings_path(_T("targets")); 68 65 … … 75 72 (_T("targets"), sh::fun_values_path(boost::bind(&NSCPClient::add_target, this, _1, _2)), 76 73 _T("REMOTE TARGET DEFINITIONS"), _T("")) 77 78 74 ; 79 75 … … 96 92 register_command(_T("nscp_exec"), _T("Execute remote command on a remote host via NSCP")); 97 93 register_command(_T("nscp_help"), _T("Help on using NSCP Client")); 98 99 94 } catch (nscapi::nscapi_exception &e) { 100 95 NSC_LOG_ERROR_STD(_T("NSClient API exception: ") + utf8::to_unicode(e.what())); … … 193 188 desc.add_options() 194 189 ("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.") 201 194 ; 202 195 } … … 209 202 std::wstring recipient = utf8::cvt<std::wstring>(config.data->recipient.id); 210 203 if (!targets.has_object(recipient)) { 211 NSC_LOG_ERROR(_T("Target not found (using default): ") + recipient);212 204 recipient = _T("default"); 213 205 } … … 255 247 256 248 std::list<nscp::packet> chunks; 257 chunks.push_back(nscp::factory::create_envelope_request(1));258 249 chunks.push_back(nscp::factory::create_payload(nscp::data::command_request, request_message.SerializeAsString(), 0)); 259 250 chunks = instance->send(con, chunks); … … 311 302 312 303 std::list<nscp::packet> chunks; 313 chunks.push_back(nscp::factory::create_envelope_request(1));314 304 chunks.push_back(nscp::factory::create_payload(nscp::data::exec_request, request_message.SerializeAsString(), 0)); 315 305 chunks = instance->send(con, chunks); … … 334 324 // Protocol implementations 335 325 // 326 struct 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 }; 336 343 337 344 std::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; 347 353 #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 } 380 371 } 381 372
Note: See TracChangeset
for help on using the changeset viewer.








