/** @file * @brief Logger class implementation. This class implements the Logging facility. * * * * @copyright Intra2net AG * @license GPLv2 */ #include "logger.hpp" #include "version_info.h" #include #include #include #include namespace po = boost::program_options; typedef boost::shared_ptr Options_descriptionPtr; using namespace std; /** * Default Constructor */ Logger::Logger() : Loglevel(0) , Syslog(false) , ExternalWarningLevel(0) , ExternalLogOnlyOnce(false) , LogPasswords(false) { set_log_facility(Loglevel,Syslog,ExternalWarningLog,ExternalWarningLevel,ExternalLogOnlyOnce); } /** * Default Destructor */ Logger::~Logger() { } /** * Decides if a external log message can be send. * @param msg The message to log. */ bool Logger::is_allowed_to_send( const string& msg ) const { if ( (ExternalLogOnlyOnce) && (ExternalSendMessages.find(msg) != ExternalSendMessages.end()) ) return false; return true; } /** * Clears the external send messages set. */ void Logger::clear_external_send_messages() { ExternalSendMessages.clear(); } /** * Decides if Logging through syslog if enabled or through std. * @param msg The message to log. */ void Logger::log_notice(const string& msg) const { if ( Syslog ) syslog(LOG_NOTICE,msg.c_str()); else cout << msg << endl;; } /** * Escape shell arguments. * @param input The input to escape. * @return The escaped string ready to use for the shell. */ string Logger::escape_shellarg(const string &input) { string output = "'"; string::const_iterator it, it_end = input.end(); for (it = input.begin(); it != it_end; it++) { if ( (*it) == '\'') output += "'\\'"; output += *it; } output += "'"; return output; } /** * Decides if Logging through syslog if enabled or through std. * @param msg The message to log. */ void Logger::log_warning(const string& msg, int level) { if ( Syslog ) syslog(LOG_WARNING,msg.c_str()); else cout << msg << endl; if ( (level <= ExternalWarningLevel) && (!ExternalWarningLog.empty()) && (is_allowed_to_send(msg)) ) { string message = msg; // Remove endline from msg. if (!message.empty() && message[message.length()-1] == '\n') message.erase(message.length()-1); /*lint !e534 */ message = escape_shellarg(message); string external = ExternalWarningLog; external.append(" "); external.append(message); if ( system(external.c_str()) != 0 ) print_error_external_logging(external); else ExternalSendMessages.insert(msg); /*lint !e534 */ } } /** * Decides if Logging through syslog if enabled or through std. * @param msg The message to log. */ void Logger::log_error(const string& msg) const { if ( Syslog ) syslog(LOG_ERR,msg.c_str()); else cerr << msg << endl; } /** * Setter for member Loglevel. * @param _loglevel Value to set Loglevel to. */ void Logger::set_external_log_only_once( const bool _external_log_only_once ) { ExternalLogOnlyOnce = _external_log_only_once; } /** * Setter for member LogPasswords. * @param _log_passwords If we want to log passwords or not. */ void Logger::set_log_passwords( const bool _log_passwords ) { LogPasswords = _log_passwords; } /** * Setter for member Loglevel. * @param _loglevel Value to set Loglevel to. */ void Logger::set_loglevel(const int _loglevel) { Loglevel = _loglevel; } /** * Getter for member Loglevel. * @return Loglevel. */ int Logger::get_loglevel() const { return Loglevel; } /** * Setter for member Syslog. * @param _syslog Wether to log through syslog or not. */ void Logger::set_syslog(const bool _syslog) { Syslog = _syslog; } /** * Getter for member Syslog. * @return True if logging through syslog is enabled, false otherwise. */ bool Logger::get_syslog() const { return Syslog; } /** * Initialize the logging facility. */ void Logger::set_log_facility(const int _loglevel, const bool _syslog, const string& _external_error_log, const int _external_error_level, const bool _external_log_only_once ) { Loglevel = _loglevel; Syslog = _syslog; ExternalWarningLog = _external_error_log; ExternalWarningLevel = _external_error_level; ExternalLogOnlyOnce = _external_log_only_once; if ( Syslog ) openlog("bpdyndnsd",LOG_PID,LOG_DAEMON); } /** * Prints out the usage to the command line. */ void Logger::print_usage(const Options_descriptionPtr opt_desc) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Usage: bpdyndnsd [Command line options]" << "\n" << endl; msg << *opt_desc << endl; log_notice(msg.str()); } } /** * Prints out the programm name and the given version string on stdout. * @param version Version string. */ void Logger::print_version() const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << MAJOR_VERSION << "." << MINOR_VERSION << endl; log_notice(msg.str()); } } /** * Prints out the successful parsing of the command line options. */ void Logger::print_cmd_parsed() const { int level = 1; if ( level <= Loglevel ) { log_notice("Command line options successfully parsed."); } } void Logger::print_conf_files_parsed() const { int level = 1; if ( level <= Loglevel ) { log_notice("Config files successfully parsed."); } } /** * Prints out the successful parsing of the config files. * @param config_path The specified config path. */ void Logger::print_conf_loaded(const string& config_path) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Config files successfully loaded in: " << config_path << endl; log_notice(msg.str()); } } /** * Prints out the unsuccessful parsing of the config files. * @param config_path The specified config path. */ void Logger::print_conf_not_loaded(const string& config_path) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Config files couldn't be loaded in: " << config_path << endl; log_error(msg.str()); } } /** * Prints out the unsuccessful parsing of the config files. */ void Logger::print_conf_reload_failed_exit() const { int level = 0; if ( level <= Loglevel ) log_error("Config files couldn't be reloaded. Exiting."); } /** * A file could not be opened for reading. * @param filename The filename. */ void Logger::print_error_opening_r(const string& filename) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Error opening file for reading: " << filename << endl; log_error(msg.str()); } } /** * A file could not be opened for writing. * @param filename The filename. */ void Logger::print_error_opening_rw(const string& filename) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Error opening file for writing: " << filename << endl; log_error(msg.str()); } } /** * Desctructor of specified class was called. * @param _class Name of the class. */ void Logger::print_destructor_call(const string& _class) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Destructor call: " << _class << endl; log_notice(msg.str()); } } /** * Constructor of specified class was called. * @param _class Name of the class. */ void Logger::print_constructor_call(const string& _class) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Constructor call: " << _class << endl; log_notice(msg.str()); } } /** * Update method for specified service was called. * @param service The service for which the update is running. */ void Logger::print_update_service(const string& service) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Running update for service: " << service << endl; log_notice(msg.str()); } } /** * Service is blocked for update because of too many errors * @param service The service for which the update is running. * @param remaining_seconds Remaining seconds this service is blocked */ void Logger::print_update_service_is_blocked(const std::string& service, int remaining_seconds) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Service " << service << " is blocked for " << remaining_seconds << " seconds because of too many errors" << endl; log_notice(msg.str()); } } /** * Service will be blocked because of update errors * @param service The service for which the update is running. * @param block_seconds Number of seconds this service is blocked */ void Logger::print_block_service(const std::string& service, int block_seconds) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Service " << service << " will be blocked for " << block_seconds << " seconds because of too many errors" << endl; log_notice(msg.str()); } } /** * An unknown option on the command line was detected. * @param unknown_option The unknown option. */ void Logger::print_unknown_cmd_option(const string& unknown_option) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Unknown option on command line detected: " << unknown_option << endl; log_error(msg.str()); } } /** * An unknown protocol was specified. * @param protocol The unknown protocol. */ void Logger::print_unknown_protocol(const string& protocol) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Unknown protocol defined: " << protocol << endl; log_error(msg.str()); } } /** * Loading a service config file. * @param filename The service config file. */ void Logger::print_load_service_conf(const string& filename) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Loading service config file: " << filename << endl; log_notice(msg.str()); } } /** * Loading the main config file. * @param filename The main config file. */ void Logger::print_load_main_conf(const string& filename) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Loading main config file: " << filename << endl; log_notice(msg.str()); } } /** * There is an unknown option in a service config file. * @param unknown_option The unknown option. */ void Logger::print_unknown_service_conf_option(const string& service_conf_file, const string& unknown_option) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Unknown option in service config file detected: " << service_conf_file << " Unknown option: " << unknown_option << endl; log_error(msg.str()); } } /** * There is an unknown option in the main config file. * @param unknown_option The unknown option. */ void Logger::print_unknown_main_conf_option(const string& unknown_option) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Unknown option in main config file detected: " << unknown_option << endl; log_error(msg.str()); } } /** * The defined config path doesn't exist or is not a directory. * @param config_path The defined config path. */ void Logger::print_error_config_path(const string& config_path) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Config path doesn't exists or is not a diretory: " << config_path << endl; log_error(msg.str()); } } /** * There is a missing command line option to initialize a service object. */ void Logger::print_missing_cmd_service_option() const { int level = 0; if ( level <= Loglevel ) { log_error("Missing option to initialize service. Protocol, host, login and password must be specified."); } } /** * Missing option in service config file. * @param service_conf_file Service config file */ void Logger::print_missing_service_conf_option(const string& service_conf_file) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Missing option in service config file " << service_conf_file << " to initialize service. Protocol, host, login and password must be specified." << endl; log_error(msg.str()); } } /** * Process running as daemon. * @param pid The pid of the daemon. */ void Logger::print_runnig_as_daemon(const int pid) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Running as daemon: " << pid << endl; log_notice(msg.str()); } } /** * Prints out the daemon mode. * @param daemon_mode The daemon mode. */ void Logger::print_daemon_mode(const bool daemon_mode) const { int level = 1; if ( level <= Loglevel ) { string mode = "disabled"; if (daemon_mode == true) mode = "enabled"; ostringstream msg; msg << "Daemon mode is " << mode << "." << endl; log_notice(msg.str()); } } /** * There was an error while trying to fork. */ void Logger::print_error_fork() const { int level = 0; if ( level <= Loglevel ) { log_error("Error while trying to fork."); } } /** * A pid in the pidfile was found. * @param pid The pid found in the pidfile. */ void Logger::print_pid_found(const int pid) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl; log_notice(msg.str()); } } /** * Another process is already running. * @param pid The pid of the other process. */ void Logger::print_process_already_running(const int pid) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl; log_error(msg.str()); } } /** * SIGTERM caught. */ void Logger::print_caught_sigterm() const { int level = 0; if ( level <= Loglevel ) { log_notice("Caught SIGTERM. Exiting..."); } } /** * SIGUSR1 caught. */ void Logger::print_caught_siguser1() const { int level = 0; if ( level <= Loglevel ) { log_notice("Caught SIGUSR1. Switching to offline mode..."); } } /** * SIGHUP caught. */ void Logger::print_caught_sighup() const { int level = 0; if ( level <= Loglevel ) { log_notice("Caught SIGHUP. Reloading config..."); } } /** * SIGUSR2 caught. */ void Logger::print_caught_siguser2() const { int level = 0; if ( level <= Loglevel ) { log_notice("Caught SIGUSR2. Switching to online mode..."); } } /** * SIGRTMIN caught. */ void Logger::print_caught_sigrtmin() const { int level = 0; if ( level <= Loglevel ) { log_notice("Caught SIGRTMIN. Switching to online mode with webcheck enabled..."); } } /** * SIGRTMAX caught. */ void Logger::print_caught_sigrtmax(int new_loglevel) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Caught SIGRTMAX. Increasing log level to " << new_loglevel << endl; log_error(msg.str()); } } /** * Error while setting signal handler. */ void Logger::print_error_setting_signal(const string& signal) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Error while setting signal handler for: " << signal << endl; log_error(msg.str()); } } /** * Error while setting signal handler. */ void Logger::print_init_log_facility() const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << " ExternalLogOnlyOnce: " << ExternalLogOnlyOnce << endl; log_notice(msg.str()); } } /** * Be verbose. Currently we are in offline mode. */ void Logger::print_offline_mode() const { int level = 1; if ( level <= Loglevel ) { log_notice("Offline mode..."); } } /** * Dialup mode: Print how long we plan to delay the next network traffic * @param sleep_until Timestamp until we plan to keep quiet */ void Logger::print_sleep_dialup_mode(time_t sleep_until) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Skipped update in dialup mode (" << sleep_until-time(NULL) << " more seconds)" << endl; log_error(msg.str()); } } /** * Objects successfully serialized. */ void Logger::print_serialized_objects_success() const { int level = 1; if ( level <= Loglevel ) { log_notice("Serialized objects successfully."); } } /** * Objects successfully de-serialized. */ void Logger::print_deserialized_objects_success() const { int level = 1; if ( level <= Loglevel ) { log_notice("De-serialized objects successfully."); } } /** * Prints out the content of a service object. * @param message Message to be added on output first. * @param protocol Service's protocol. * @param hostname Service's hostname. * @param login Service's login. * @param password Service's password. * @param actual_ip Service's actual_ip. * @param lastupdated Service's lastupdated. * @param activated Service's activated. */ void Logger::print_service_object(const string& message, const string& protocol, const string& hostname, const string& login, const string& password, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const int dns_cache_ttl , const string& actual_ip, std::map lastupdated, const bool activated) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << message << endl; msg << "\t" << "Protocol: " << protocol << endl; msg << "\t" << "Hostname: " << hostname << endl; msg << "\t" << "Login: " << login << endl; if (LogPasswords) msg << "\t" << "Password: " << password << endl; else msg << "\t" << "Password: (*hidden*)" << endl; msg << "\t" << "Update Interval: " << update_interval << endl; msg << "\t" << "Max Updates: " << max_updates_within_interval << endl; msg << "\t" << "Max equal Updates:" << max_equal_updates_in_succession << endl; msg << "\t" << "DNS Cache TTL: " << dns_cache_ttl << endl; msg << "\t" << "Actual_IP: " << actual_ip << endl; for ( std::map::reverse_iterator r_iter = lastupdated.rbegin(); r_iter != lastupdated.rend(); r_iter++ ) { msg << "\t" << "Lastupdated: " << r_iter->first << " -> " << r_iter->second << endl; } msg << "\t" << "Activated: " << activated << endl; log_notice(msg.str()); } } /** * Caught exception while serialize. * @param exception Exception message. */ void Logger::print_exception_serialize(const string& errMsg) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Error while trying to serialize Serviceholder object: " << errMsg << endl; log_error(msg.str()); } } /** * Caught exception while de-serialize. * @param exception Exception message. */ void Logger::print_exception_deserialize(const string& errMsg) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Error while trying to de-serialize Serviceholder object: " << errMsg << ". Continue without recovering state from old services!" << endl; log_error(msg.str()); } } /** * Child couldn't be killed by parent. * @param pid Pid of the child. */ void Logger::print_error_kill_child(const int pid) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Could not kill child process with PID: " << pid << endl; log_error(msg.str()); } } /** * Child was killed by parent because of error. * @param pid The pid (child) killed. */ void Logger::print_child_killed(const int pid) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Killed child process with PID: " << pid << endl; log_notice(msg.str()); } } /** * There is no object file. * @param object_file The object file. */ void Logger::print_no_object_file(const string& object_file) { int level = 1; if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; msg << "There is no object file: " << object_file << ". Continue without recovering state from old services!" << endl; log_warning(msg.str(),level); } } /** * Prints out the given hostname * @param hostname Hostname as string. */ void Logger::print_hostname(const string& hostname) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Detected following hostname of localhost: " << hostname << endl; log_notice(msg.str()); } } /** * Prints out the detected own ipv4 address * @param ip_addr String representation of the detected ip. */ void Logger::print_own_ipv4(const string& ip_addr_v4, const string& hostname) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Detected following IPv4-Address of host: " << hostname << " : " << ip_addr_v4 << endl; log_notice(msg.str()); } } /** * Prints out the detected own ipv5 address * @param ip_addr String representation of the detected ip. */ void Logger::print_own_ipv6(const string& ip_addr_v6, const string& hostname) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Detected following IPv6-Address of host: " << hostname << " : " << ip_addr_v6 << endl; log_notice(msg.str()); } } /** * Exception while trying to resolve hostname to ip. * @param exception The exception caught. * @param hostname The hostname. */ void Logger::print_error_hostname_to_ip(const string& errMsg, const string& hostname) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Could not resolve the hostname: " << hostname << " to an IP-Address: " << errMsg << endl; log_error(msg.str()); } } /** * The update of the given service was successful. * @param service The service. */ void Logger::print_update_service_successful(const string& service, const std::string& ip_addr) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Updated service successful: " << service << " to IP: " << ip_addr << endl; log_notice(msg.str()); } } /** * No ip could be determined through webcheck */ void Logger::print_webcheck_no_ip() { int level = 0; if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { log_warning("IP-Address of this host could not be determined through any configured webcheck url.",level); } } /** * Connection problem while trying to get ip through webcheck url * @param curl_err_buff Curl error message * @param url the url */ void Logger::print_webcheck_url_connection_problem(const char * curl_err_buff, const string& url) { int level = 1; if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; msg << "There was a problem while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl; log_warning(msg.str(),level); } } /** * Prints out curl error. * @param curl_err_buff Curl error message. * @param url URL */ void Logger::print_webcheck_error(const char * curl_err_buff, const string& url) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "There was an error while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl; log_error(msg.str()); } } /** * Prints out the received data through curl. * @param curl_data Data string */ void Logger::print_received_curl_data(const string& curl_data) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Received CURL data: " << curl_data << endl; log_notice(msg.str()); } } /** * IP was foudn through regex * @param ip The IP found. */ void Logger::print_regex_found_ip(const string& ip) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Found IP-Address via regex: " << ip << endl; log_notice(msg.str()); } } /** * No IP was found through regex. * @param data The data string which should contain a valid IP.s */ void Logger::print_regex_ip_not_found(const string& data) { int level = 1; if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; msg << "Could not extract an IP-Address via regex from following data:\n" << data << endl; log_warning(msg.str(),level); } } /** * Detected multiple occurrences of the same option. * @param message Error message. */ void Logger::print_multiple_cmd_option(const string& message) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "The same option is only allowed once: " << message << endl; log_error(msg.str()); } } /** * An update would exceed the update interval. Prints out a warning message. * @param override_log_level Override log level with zero if true * @param current_time Current time. * @param old_time Time of update #MaxUpdatesWithinInterval ago. * @param MaxUpdatesWithinInterval Number of allowed updates in one update interval. * @param service The service which exceeds update interval. */ void Logger::print_update_not_allowed(bool override_log_level, const time_t current_time, const time_t old_time, const int MaxUpdatesWithinInterval, const string& service) { int level = 1; if (override_log_level) level = 0; if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; msg << "Update not allowed for service: " << service << ". Too many updates within max update interval. Current time: " << current_time << ". Update time before " << MaxUpdatesWithinInterval << " updates: " << old_time << endl; log_warning(msg.str(),level); } } /** * Failure while running update for service. * @param service Services' name. */ void Logger::print_update_service_failure(const string& service) { int level = 0; if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; msg << "Could not update service: " << service << endl; log_warning(msg.str(),level); } } /** * Start message */ void Logger::print_started() const { int level = 0; if ( level <= Loglevel ) { log_notice("Started"); } } /** * Starting shutdown */ void Logger::print_starting_shutdown() const { int level = 0; if ( level <= Loglevel ) { log_notice("Shutting down ..."); } } /** * Shutdown complete */ void Logger::print_shutdown_succeeded() const { int level = 0; if ( level <= Loglevel ) { log_notice("Shutting down complete ..."); } } /** * Shutdown parent succeeded */ void Logger::print_shutdown_parent_succeeded() const { int level = 0; if ( level <= Loglevel ) { log_notice("Shutting down parent process completed ..."); } } /** * Starting shutdown parent */ void Logger::print_starting_shutdown_parent() const { int level = 0; if ( level <= Loglevel ) { log_notice("Shutting down parent process ..."); } } /** * DNS cache record timeout * @param hostname Hostname * @param lastupdated Lastupdated * @param dns_cache_ttl DNS cache TTL * @param current_time Current time */ void Logger::print_recheck_dns_entry(const string& hostname, const int lastupdated, const int dns_cache_ttl, const int current_time) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "DNS cache record for host <" << hostname << "> expired or host will be updated for the first time: Lastupdated: " << lastupdated << " DNS cache ttl: " << dns_cache_ttl << " Current time: " << current_time << " Checking current DNS cache status." << endl; log_notice(msg.str()); } } /** * Missing proxy option on command line. */ void Logger::print_missing_cmd_proxy_option() const { int level = 0; if ( level <= Loglevel ) { log_error("Missing option to initialize proxy. http_proxy and http_proxy_port must be specified."); } } /** * Multiple option in service config file. * @param service_conf_file Service config file * @param message Multiple option text */ void Logger::print_multiple_service_conf_option(const string& service_conf_file, const string& message) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Multiple occurrences of the same option in service config file detected: " << service_conf_file << " " << message << endl; log_error(msg.str()); } } /** * Multiple option in main config file. * @param service_conf_file Service config file * @param message Multiple option text */ void Logger::print_multiple_main_conf_option(const string& main_conf_file, const string& message) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Multiple occurrences of the same option in main config file detected: " << main_conf_file << " " << message << endl; log_error(msg.str()); } } /** * Missing proxy option in main config file. * @param main_conf_filename The concerning config file. */ void Logger::print_missing_conf_proxy_option(const string& main_conf_filename) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Missing option to initialize proxy in main config file: " << main_conf_filename << " http_proxy and http_proxy_port must be specified." << endl; log_error(msg.str()); } } /** * There is no domain part in the given hostname * @param hostname The hostname with no domain part in it. */ void Logger::print_no_domain_part(const string& hostname) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "There is no domain part in the given hostname: " << hostname << endl; log_notice(msg.str()); } } /** * Service is not initialized properly. * @param service The service. */ void Logger::print_httphelper_not_initialized() const { int level = 0; if ( level <= Loglevel ) { log_error("HTTP-Helper is not initialized properly."); } } /** * A curl error occurred. * @param msg The error message * @param curl_err_code The resulting curl error code */ void Logger::print_curl_error_init(const std::string& err_msg, const CURLcode curl_err_code) const { string curl_err = ""; if ( curl_err_code == CURLE_FAILED_INIT ) curl_err = "CURLE_FAILED_INIT"; else curl_err = "UNKNOWN"; int level = 0; if ( (level <= Loglevel) ) { ostringstream msg; msg << "Curl error: " << err_msg << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */ log_error(msg.str()); } } /** * A curl error occurred. * @param url The url requested by the curl operation * @param curl_err_code The resulting curl error code */ void Logger::print_curl_error(const string& url, const CURLcode curl_err_code) const { string curl_err = ""; if ( curl_err_code == CURLE_URL_MALFORMAT ) curl_err = "CURLE_URL_MALFORMAT"; else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST ) curl_err = "CURLE_COULDNT_RESOLVE_HOST"; else if ( curl_err_code == CURLE_COULDNT_CONNECT ) curl_err = "CURLE_COULDNT_CONNECT"; else curl_err = "UNKNOWN"; int level = 0; if ( (level <= Loglevel) ) { ostringstream msg; msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */ log_error(msg.str()); } } /** * A curl error occurred. * @param url The url requested by the curl operation * @param curl_err_code The resulting curl error code * @param curl_err_buff The curl error buffer */ void Logger::print_curl_error(const string& url, const CURLcode curl_err_code, const char * curl_err_buff) const { string curl_err = ""; if ( curl_err_code == CURLE_URL_MALFORMAT ) curl_err = "CURLE_URL_MALFORMAT"; else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST ) curl_err = "CURLE_COULDNT_RESOLVE_HOST"; else if ( curl_err_code == CURLE_COULDNT_CONNECT ) curl_err = "CURLE_COULDNT_CONNECT"; else curl_err = "UNKNOWN"; int level = 0; if ( (level <= Loglevel) ) { ostringstream msg; msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << " " << curl_err_buff << endl; /*lint !e641 */ log_error(msg.str()); } } /** * Prints out the data received by curl operation * @param CurlWritedataBuff */ void Logger::print_curl_data(const string& curl_writedata_buff) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Data received by curl: " << curl_writedata_buff << endl; log_notice(msg.str()); } } /** * Not authorized to perform requested update operation * @param service The requested service. * @param username Username * @param password Password */ void Logger::print_service_not_authorized(const string& service, const string& username, const string& password) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Not authorized to perform update operation on service: " << service << " Please check username and password: " << username << ":"; if (LogPasswords) msg << password << endl; else msg << "(*hidden*)" << endl; log_notice(msg.str()); } } /** * Prints out the http status code * @param url Url * @param output HTTP status code */ void Logger::print_http_status_code(const string& url, const long http_code) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Requested URL: " << url << " Received HTTP status code: " << http_code << endl; log_notice(msg.str()); } } /** * Generic failure while trying to update service * @param url The requested URL * @param curl_data The received curl_data from the server */ void Logger::print_update_failure(const string& url, const string& curl_data) const { int level = 0; if ( (level <= Loglevel) ) { ostringstream msg; msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << curl_data << endl; log_error(msg.str()); } } /** * Generic failure while trying to update service * @param url The requested URL * @param http_status_code The received http status code */ void Logger::print_update_failure(const string& url, const long http_status_code) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << http_status_code << endl; log_error(msg.str()); } } /** * Hostname is invalid, contains no or only one domain part. * @param hostname The full qualified host name. */ void Logger::print_invalid_hostname(const string& hostname) { int level = 0; if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; msg << "The configured hostname: " << hostname << " is invalid. Please add the corresponding domain part." << endl; log_warning(msg.str(),level); } } /** * An IP in a private range was detected * @param ip The private IP */ void Logger::print_ip_is_local(const string& ip) { int level = 1; if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; msg << "The detected IP is within a private IP range: " << ip << endl; log_warning(msg.str(),level); } } /** * Regex is matching in string * @param regex The regex pattern * @param matching_string The string */ void Logger::print_regex_match(const string& regex, const string& matching_string) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Regex: " << regex << " is matching in: " << matching_string << endl; log_notice(msg.str()); } } /** * Regex is not matching * @param regex Regex * @param not_matching_string String */ void Logger::print_no_regex_match(const string& regex, const string& not_matching_string) { int level = 1; if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; msg << "Regex: " << regex << " is not matching in: " << not_matching_string << endl; log_warning(msg.str(),level); } } /** * Could not parse gnudip initial reply. * @param curl_data The data received from gnudip server which should contain salt, time and sign. */ void Logger::print_could_not_parse_received_data(const string& curl_data) const { int level = 0; if ( (level <= Loglevel) ) { ostringstream msg; msg << "Could not parse salt, time and sign from initial gnudip server reply: " << curl_data << endl; log_error(msg.str()); } } /** * Gnudip salt, time and sign could not be got from map */ void Logger::print_could_not_get_initial_gnudip_data() const { int level = 0; if ( (level <= Loglevel) ) { log_error("Could not get salt, time and sign from map."); } } /** * Gnudip protocol requires explicit declaration of a servername. */ void Logger::print_gnudip_requires_servername() { int level = 0; if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { log_warning("Gnudip requires explicit definition of servername via config!",level); } } /** * An exception occurred while computing the md5 sum. * @param what The exception occurred. */ void Logger::print_exception_md5_sum(const string& what) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "An exception occurred while computing a md5 sum: " << what << endl; log_error(msg.str()); } } /** * A network exception occurred. * @param what The exception occurred. */ void Logger::print_network_error(const string& what) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "A network exception occurred: " << what << endl; log_error(msg.str()); } } /** * An undefined protocol error occurred. * @param protocol The protocol * @param error The error */ void Logger::print_undefined_protocol_error(const string& protocol, const string& error) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "An undefined protocol error occurred. Protocol: " << protocol << " Error: " << error << endl; log_error(msg.str()); } } /** * Error while trying to log through external program. * @param external_prog The external program called. */ void Logger::print_error_external_logging(const string& external_prog) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Error while trying to log through external program: " << external_prog << endl; log_notice(msg.str()); } } /** * Error while parsing config file. * @param error Error occurred. * @param config_file Config file. */ void Logger::print_error_parsing_config_file(const string& filename, const string& error) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Error while parsing config file: " << filename << " Error: " << error << endl; log_notice(msg.str()); } } /** * Error while parsing cmd option * @param error Error */ void Logger::print_error_parsing_cmd(const string& error) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Error while parsing cmd options: " << error << endl; log_error(msg.str()); } } /** * The webcheck interval was exceeded, so webcheck not allowed. * @param last_webcheck Time of last webcheck. * @param webcheck_interval Webcheck interval time in seconds. * @param current_time Current system time. */ void Logger::print_webcheck_exceed_interval( const time_t last_webcheck, const int webcheck_interval, const time_t current_time ) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Skipped webcheck in idle period (" << (last_webcheck+webcheck_interval)-current_time << " more seconds"; msg << ", last_webcheck: " << last_webcheck << ", webcheck_interval(sec): " << webcheck_interval << ")" << endl; log_notice(msg.str()); } } /** * Checking if hosts needs update. * @param hostname Hostname * @param current_time Current time * @param lastupdated Last updated */ void Logger::print_check_service_update(const string& hostname, const time_t current_time, const time_t lastupdated) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Checking if host: " << hostname << " needs update. Current time: " << current_time << " Last updated: " << lastupdated << endl; log_notice(msg.str()); } } /** * Cached DNS entry * @param hostname Hostname * @param ip_dns_recheck DNS recheck IP * @param ip_last_update IP set in last update * @param ip_host Hosts' IP */ void Logger::print_cached_dns_entry(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Cached DNS record for host <" << hostname << "> : " << ip_dns_recheck << " Last updated IP: " << ip_last_update << " Hosts IP: " << ip_host << endl; log_notice(msg.str()); } } /** * Updating service * @param hostname Hostname * @param ip_dns_recheck Cached DNS entry * @param ip_last_update IP set in last update * @param ip_host Hosts IP * @param lastupdated Lastupdated */ void Logger::print_update_service(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated) const{ int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Updating service. Hostname: " << hostname << " DNS-Record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Lastupdated: " << lastupdated << " Hosts IP: " << ip_host << endl; log_notice(msg.str()); } } /** * TTL expired * @param hostname Hostname * @param ip_dns_recheck Cached DNS entry * @param ip_last_update IP set in last update * @param ip_host Hosts IP * @param lastupdated Last updated * @param dns_cache_ttl DNS cache ttl * @param current_time Current time */ void Logger::print_update_service_ttl_expired(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated, const int dns_cache_ttl, const time_t current_time) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "TTL for service expired and still pointing to old IP. Hostname: " << hostname << " DNS-Record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Lastupdated: " << lastupdated << "DNS Cache TTL: " << dns_cache_ttl << " Current Time: " << current_time << " Hosts IP: " << ip_host << endl; log_notice(msg.str()); } } /** * TTL expired * @param override_log_level Override log level with zero if true * @param hostname Hostname * @param ip_dns_recheck Cached DNS entry * @param ip_last_update IP set in last update * @param ip_host Hosts IP * @param lastupdated Last updated * @param dns_cache_ttl DNS cache ttl * @param current_time Current time */ void Logger::print_update_service_ttl_not_expired(bool override_log_level, const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated, const int dns_cache_ttl, const time_t current_time) const { int level = 1; if (override_log_level) level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Waiting for DNS cache TTL to expire. Hostname: " << hostname << " DNS-Record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Lastupdated: " << lastupdated << "DNS Cache TTL: " << dns_cache_ttl << " Current Time: " << current_time << " Hosts IP: " << ip_host << endl; log_notice(msg.str()); } } /** * No update needed * @param override_log_level Override log level with zero if true * @param hostname Hostname * @param ip_dns_recheck Cached DNS entry * @param ip_last_update IP set in last update * @param ip_host Hosts IP * @param lastupdated Last updated */ void Logger::print_no_update_needed(bool override_log_level, const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated) const { int level = 1; if (override_log_level) level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "No update needed for host: " << hostname << " Cached DNS record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Hosts IP: " << ip_host << " Last updated: " << lastupdated << endl; log_notice(msg.str()); } } /** * Error while trying to get local wan interface IP address through getifaddrs. * @param error The system call which raised the error. * @param error The error set by getifaddrs. */ void Logger::print_error_getting_local_wan_ip(const std::string& system_call, const std::string& error) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Error while trying to get local wan interface IP address through '" << system_call << "': " << error << endl; log_error(msg.str()); } } /** * Could not get IP address of local wan interface. * @param override_log_level Override log level with zero if true */ void Logger::print_no_wan_ip(bool override_log_level) const { int level = 1; if (override_log_level) level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Could not get IP address of local wan interface. Will retry later." << endl; log_error(msg.str()); } } /** * Print extenral WAN IP * @param override_log_level Override log level with zero if true * @param ip External WAN IP */ void Logger::print_external_wan_ip(bool override_log_level, const std::string &ip) const { int level = 1; if (override_log_level) level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Determined WAN interface IP: " << ip << endl; log_error(msg.str()); } } /** * Could not resolve current IP for DNS name * @param override_log_level Override log level with zero if true */ void Logger::print_dns_lookup_failed(bool override_log_level, const std::string &hostname) const { int level = 1; if (override_log_level) level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "Could not resolve IP address for host " << hostname << ". Will retry later." << endl; log_error(msg.str()); } } /** * Invalid service config was detected. */ void Logger::print_invalid_service_config() const { int level = 0; if ( level <= Loglevel ) { log_error("Ignoring invalid service. Please check.\n"); } } /** * Print simple message. */ void Logger::print_msg( const string& msg ) const { int level = 0; if ( level <= Loglevel ) { log_error(msg); } } /** * Print the last updates. * @param ip_host Actual host IP. * @param max_equal_updates_in_succession Maximal number of updates for the same IP in succession. * @param last_updates Map with the last updates in it. * @param hostname The service hostname. */ void Logger::print_last_updates( const std::string& ip_host, const time_t current_time, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const std::map& last_updates, const std::string& servicename ) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; msg << "Last updates for " << servicename << " : "; int i = 0; for ( std::map::const_reverse_iterator r_iter = last_updates.rbegin(); (r_iter != last_updates.rend()) && (i < max_equal_updates_in_succession); r_iter++ ) { msg << r_iter->first << "->" << r_iter->second; i++; } msg << ". Actual internet IP: " << ip_host << " Current time: " << current_time << " Update interval: " << update_interval << " Max updates within interval: " << max_updates_within_interval << " Max equal updates in succession: " << max_equal_updates_in_succession << endl; log_notice(msg.str()); } } /** * Print the burnt IP. * @param ip_host Actual host IP which is burnt. * @param hostname The service hostname. */ void Logger::print_ip_burnt( const std::string& ip_host, const std::string& hostname ) const { int level = 0; if ( level <= Loglevel ) { ostringstream msg; msg << "IP address " << ip_host << " was updated too often in succession, host " << hostname << " blocked until IP address will be different." << endl; log_error(msg.str()); } }