From 802e46f8ea36c344f112d7e1dd8d64d17a4cc939 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Mon, 15 Sep 2025 12:59:37 +0200 Subject: Run clang-format again --- plugins-root/check_dhcp.c | 258 ++++++++++++++-------- plugins-root/check_icmp.d/config.h | 2 +- plugins-root/pst3.c | 441 ++++++++++++++++++------------------- 3 files changed, 378 insertions(+), 323 deletions(-) (limited to 'plugins-root') diff --git a/plugins-root/check_dhcp.c b/plugins-root/check_dhcp.c index daed9cb0..9a96547f 100644 --- a/plugins-root/check_dhcp.c +++ b/plugins-root/check_dhcp.c @@ -127,17 +127,17 @@ static long mac_addr_dlpi(const char *, int, u_char *); #define MAX_DHCP_OPTIONS_LENGTH 312 typedef struct dhcp_packet_struct { - uint8_t op; /* packet type */ - uint8_t htype; /* type of hardware address for this machine (Ethernet, etc) */ - uint8_t hlen; /* length of hardware address (of this machine) */ - uint8_t hops; /* hops */ - uint32_t xid; /* random transaction id number - chosen by this machine */ - uint16_t secs; /* seconds used in timing */ - uint16_t flags; /* flags */ - struct in_addr ciaddr; /* IP address of this machine (if we already have one) */ - struct in_addr yiaddr; /* IP address of this machine (offered by the DHCP server) */ - struct in_addr siaddr; /* IP address of next server */ - struct in_addr giaddr; /* IP address of DHCP relay */ + uint8_t op; /* packet type */ + uint8_t htype; /* type of hardware address for this machine (Ethernet, etc) */ + uint8_t hlen; /* length of hardware address (of this machine) */ + uint8_t hops; /* hops */ + uint32_t xid; /* random transaction id number - chosen by this machine */ + uint16_t secs; /* seconds used in timing */ + uint16_t flags; /* flags */ + struct in_addr ciaddr; /* IP address of this machine (if we already have one) */ + struct in_addr yiaddr; /* IP address of this machine (offered by the DHCP server) */ + struct in_addr siaddr; /* IP address of next server */ + struct in_addr giaddr; /* IP address of DHCP relay */ unsigned char chaddr[MAX_DHCP_CHADDR_LENGTH]; /* hardware address of this machine */ char sname[MAX_DHCP_SNAME_LENGTH]; /* name of DHCP server */ char file[MAX_DHCP_FILE_LENGTH]; /* boot file name (used for diskless booting?) */ @@ -199,7 +199,8 @@ static void print_help(void); static void resolve_host(const char * /*in*/, struct in_addr * /*out*/); static unsigned char *mac_aton(const char * /*string*/); static void print_hardware_address(const unsigned char * /*address*/); -static int get_hardware_address(int /*sock*/, char * /*interface_name*/, unsigned char *client_hardware_address); +static int get_hardware_address(int /*sock*/, char * /*interface_name*/, + unsigned char *client_hardware_address); typedef struct get_ip_address_wrapper { int error; @@ -211,32 +212,40 @@ typedef struct send_dhcp_discover_wrapper { int error; uint32_t packet_xid; } send_dhcp_discover_wrapper; -static send_dhcp_discover_wrapper send_dhcp_discover(int socket, bool unicast, struct in_addr dhcp_ip, struct in_addr requested_address, - bool request_specific_address, struct in_addr my_ip, - unsigned char *client_hardware_address); +static send_dhcp_discover_wrapper +send_dhcp_discover(int socket, bool unicast, struct in_addr dhcp_ip, + struct in_addr requested_address, bool request_specific_address, + struct in_addr my_ip, unsigned char *client_hardware_address); typedef struct get_dhcp_offer_wrapper { int error; int valid_responses; dhcp_offer *dhcp_offer_list; } get_dhcp_offer_wrapper; -static get_dhcp_offer_wrapper get_dhcp_offer(int /*sock*/, int dhcpoffer_timeout, uint32_t packet_xid, dhcp_offer *dhcp_offer_list, +static get_dhcp_offer_wrapper get_dhcp_offer(int /*sock*/, int dhcpoffer_timeout, + uint32_t packet_xid, dhcp_offer *dhcp_offer_list, const unsigned char *client_hardware_address); -static mp_subcheck get_results(bool exclusive, int requested_servers, struct in_addr requested_address, bool request_specific_address, - requested_server *requested_server_list, int valid_responses, dhcp_offer *dhcp_offer_list); +static mp_subcheck get_results(bool exclusive, int requested_servers, + struct in_addr requested_address, bool request_specific_address, + requested_server *requested_server_list, int valid_responses, + dhcp_offer *dhcp_offer_list); typedef struct add_dhcp_offer_wrapper { int error; dhcp_offer *dhcp_offer_list; } add_dhcp_offer_wrapper; -static add_dhcp_offer_wrapper add_dhcp_offer(struct in_addr /*source*/, dhcp_packet * /*offer_packet*/, dhcp_offer *dhcp_offer_list); +static add_dhcp_offer_wrapper add_dhcp_offer(struct in_addr /*source*/, + dhcp_packet * /*offer_packet*/, + dhcp_offer *dhcp_offer_list); static int free_dhcp_offer_list(dhcp_offer *dhcp_offer_list); static int free_requested_server_list(requested_server *requested_server_list); static int create_dhcp_socket(bool /*unicast*/, char *network_interface_name); static int close_dhcp_socket(int /*sock*/); -static int send_dhcp_packet(void * /*buffer*/, int /*buffer_size*/, int /*sock*/, struct sockaddr_in * /*dest*/); -static int receive_dhcp_packet(void * /*buffer*/, int /*buffer_size*/, int /*sock*/, int /*timeout*/, struct sockaddr_in * /*address*/); +static int send_dhcp_packet(void * /*buffer*/, int /*buffer_size*/, int /*sock*/, + struct sockaddr_in * /*dest*/); +static int receive_dhcp_packet(void * /*buffer*/, int /*buffer_size*/, int /*sock*/, + int /*timeout*/, struct sockaddr_in * /*address*/); int main(int argc, char **argv) { setlocale(LC_ALL, ""); @@ -271,7 +280,8 @@ int main(int argc, char **argv) { struct in_addr my_ip = {0}; if (config.unicast_mode) { /* get IP address of client machine */ - get_ip_address_wrapper tmp_get_ip = get_ip_address(dhcp_socket, config.network_interface_name); + get_ip_address_wrapper tmp_get_ip = + get_ip_address(dhcp_socket, config.network_interface_name); if (tmp_get_ip.error == OK) { my_ip = tmp_get_ip.my_ip; } else { @@ -281,8 +291,9 @@ int main(int argc, char **argv) { } /* send DHCPDISCOVER packet */ - send_dhcp_discover_wrapper disco_res = send_dhcp_discover(dhcp_socket, config.unicast_mode, config.dhcp_ip, config.requested_address, - config.request_specific_address, my_ip, client_hardware_address); + send_dhcp_discover_wrapper disco_res = send_dhcp_discover( + dhcp_socket, config.unicast_mode, config.dhcp_ip, config.requested_address, + config.request_specific_address, my_ip, client_hardware_address); if (disco_res.error != OK) { // DO something? @@ -290,8 +301,8 @@ int main(int argc, char **argv) { } /* wait for a DHCPOFFER packet */ - get_dhcp_offer_wrapper offer_res = - get_dhcp_offer(dhcp_socket, config.dhcpoffer_timeout, disco_res.packet_xid, NULL, client_hardware_address); + get_dhcp_offer_wrapper offer_res = get_dhcp_offer( + dhcp_socket, config.dhcpoffer_timeout, disco_res.packet_xid, NULL, client_hardware_address); int valid_responses = 0; dhcp_offer *dhcp_offer_list = NULL; @@ -308,8 +319,10 @@ int main(int argc, char **argv) { mp_check overall = mp_check_init(); /* determine state/plugin output to return */ - mp_subcheck sc_res = get_results(config.exclusive_mode, config.num_of_requested_servers, config.requested_address, - config.request_specific_address, config.requested_server_list, valid_responses, dhcp_offer_list); + mp_subcheck sc_res = + get_results(config.exclusive_mode, config.num_of_requested_servers, + config.requested_address, config.request_specific_address, + config.requested_server_list, valid_responses, dhcp_offer_list); mp_add_subcheck_to_check(&overall, sc_res); /* free allocated memory */ free_dhcp_offer_list(dhcp_offer_list); @@ -357,17 +370,20 @@ int get_hardware_address(int sock, char *interface_name, unsigned char *client_h } if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { - printf(_("Error: Couldn't get hardware address from %s. sysctl 1 error - %s.\n"), interface_name, strerror(errno)); + printf(_("Error: Couldn't get hardware address from %s. sysctl 1 error - %s.\n"), + interface_name, strerror(errno)); exit(STATE_UNKNOWN); } if ((buf = malloc(len)) == NULL) { - printf(_("Error: Couldn't get hardware address from interface %s. malloc error - %s.\n"), interface_name, strerror(errno)); + printf(_("Error: Couldn't get hardware address from interface %s. malloc error - %s.\n"), + interface_name, strerror(errno)); exit(4); } if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { - printf(_("Error: Couldn't get hardware address from %s. sysctl 2 error - %s.\n"), interface_name, strerror(errno)); + printf(_("Error: Couldn't get hardware address from %s. sysctl 2 error - %s.\n"), + interface_name, strerror(errno)); exit(STATE_UNKNOWN); } @@ -398,12 +414,16 @@ int get_hardware_address(int sock, char *interface_name, unsigned char *client_h unit = atoi(p); strncat(dev, interface_name, 6); } else { - printf(_("Error: can't find unit number in interface_name (%s) - expecting TypeNumber eg lnc0.\n"), interface_name); + printf(_("Error: can't find unit number in interface_name (%s) - expecting TypeNumber eg " + "lnc0.\n"), + interface_name); exit(STATE_UNKNOWN); } stat = mac_addr_dlpi(dev, unit, client_hardware_address); if (stat != 0) { - printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit); + printf( + _("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), + dev, unit); exit(STATE_UNKNOWN); } @@ -415,7 +435,9 @@ int get_hardware_address(int sock, char *interface_name, unsigned char *client_h stat = mac_addr_dlpi(dev, unit, client_hardware_address); if (stat != 0) { - printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit); + printf( + _("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), + dev, unit); exit(STATE_UNKNOWN); } /* Kompf 2000-2003 */ @@ -463,8 +485,10 @@ get_ip_address_wrapper get_ip_address(int sock, char *interface_name) { } /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */ -static send_dhcp_discover_wrapper send_dhcp_discover(int sock, bool unicast, struct in_addr dhcp_ip, struct in_addr requested_address, - bool request_specific_address, struct in_addr my_ip, +static send_dhcp_discover_wrapper send_dhcp_discover(int sock, bool unicast, struct in_addr dhcp_ip, + struct in_addr requested_address, + bool request_specific_address, + struct in_addr my_ip, unsigned char *client_hardware_address) { dhcp_packet discover_packet = {0}; /* boot request flag (backward compatible with BOOTP servers) */ @@ -506,8 +530,9 @@ static send_dhcp_discover_wrapper send_dhcp_discover(int sock, bool unicast, str unsigned short opts = 4; /* DHCP message type is embedded in options field */ - discover_packet.options[opts++] = DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */ - discover_packet.options[opts++] = '\x01'; /* DHCP message option length in bytes */ + discover_packet.options[opts++] = + DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */ + discover_packet.options[opts++] = '\x01'; /* DHCP message option length in bytes */ discover_packet.options[opts++] = DHCPDISCOVER; /* the IP address we're requesting */ @@ -535,8 +560,10 @@ static send_dhcp_discover_wrapper send_dhcp_discover(int sock, bool unicast, str }; if (verbose) { - printf(_("DHCPDISCOVER to %s port %d\n"), inet_ntoa(sockaddr_broadcast.sin_addr), ntohs(sockaddr_broadcast.sin_port)); - printf("DHCPDISCOVER XID: %u (0x%X)\n", ntohl(discover_packet.xid), ntohl(discover_packet.xid)); + printf(_("DHCPDISCOVER to %s port %d\n"), inet_ntoa(sockaddr_broadcast.sin_addr), + ntohs(sockaddr_broadcast.sin_port)); + printf("DHCPDISCOVER XID: %u (0x%X)\n", ntohl(discover_packet.xid), + ntohl(discover_packet.xid)); printf("DHCDISCOVER ciaddr: %s\n", inet_ntoa(discover_packet.ciaddr)); printf("DHCDISCOVER yiaddr: %s\n", inet_ntoa(discover_packet.yiaddr)); printf("DHCDISCOVER siaddr: %s\n", inet_ntoa(discover_packet.siaddr)); @@ -554,7 +581,8 @@ static send_dhcp_discover_wrapper send_dhcp_discover(int sock, bool unicast, str } /* waits for a DHCPOFFER message from one or more DHCP servers */ -get_dhcp_offer_wrapper get_dhcp_offer(int sock, int dhcpoffer_timeout, uint32_t packet_xid, dhcp_offer *dhcp_offer_list, +get_dhcp_offer_wrapper get_dhcp_offer(int sock, int dhcpoffer_timeout, uint32_t packet_xid, + dhcp_offer *dhcp_offer_list, const unsigned char *client_hardware_address) { time_t start_time; time(&start_time); @@ -578,7 +606,8 @@ get_dhcp_offer_wrapper get_dhcp_offer(int sock, int dhcpoffer_timeout, uint32_t dhcp_packet offer_packet = {0}; result = OK; - result = receive_dhcp_packet(&offer_packet, sizeof(offer_packet), sock, dhcpoffer_timeout, &source); + result = receive_dhcp_packet(&offer_packet, sizeof(offer_packet), sock, dhcpoffer_timeout, + &source); if (result != OK) { if (verbose) { @@ -607,8 +636,9 @@ get_dhcp_offer_wrapper get_dhcp_offer(int sock, int dhcpoffer_timeout, uint32_t /* check packet xid to see if its the same as the one we used in the discover packet */ if (ntohl(offer_packet.xid) != packet_xid) { if (verbose) { - printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"), ntohl(offer_packet.xid), - packet_xid); + printf( + _("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"), + ntohl(offer_packet.xid), packet_xid); } continue; @@ -648,7 +678,8 @@ get_dhcp_offer_wrapper get_dhcp_offer(int sock, int dhcpoffer_timeout, uint32_t printf("DHCPOFFER giaddr: %s\n", inet_ntoa(offer_packet.giaddr)); } - add_dhcp_offer_wrapper add_res = add_dhcp_offer(source.sin_addr, &offer_packet, dhcp_offer_list); + add_dhcp_offer_wrapper add_res = + add_dhcp_offer(source.sin_addr, &offer_packet, dhcp_offer_list); if (add_res.error != OK) { // TODO } else { @@ -673,7 +704,8 @@ get_dhcp_offer_wrapper get_dhcp_offer(int sock, int dhcpoffer_timeout, uint32_t /* sends a DHCP packet */ int send_dhcp_packet(void *buffer, int buffer_size, int sock, struct sockaddr_in *dest) { - int result = sendto(sock, (char *)buffer, buffer_size, 0, (struct sockaddr *)dest, sizeof(*dest)); + int result = + sendto(sock, (char *)buffer, buffer_size, 0, (struct sockaddr *)dest, sizeof(*dest)); if (verbose) { printf(_("send_dhcp_packet result: %d\n"), result); @@ -687,7 +719,8 @@ int send_dhcp_packet(void *buffer, int buffer_size, int sock, struct sockaddr_in } /* receives a DHCP packet */ -int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, struct sockaddr_in *address) { +int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, + struct sockaddr_in *address) { /* wait for data to arrive (up time timeout) */ struct timeval timeout_val = { .tv_sec = timeout, @@ -711,7 +744,8 @@ int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, st struct sockaddr_in source_address = {0}; socklen_t address_size = sizeof(source_address); - int recv_result = recvfrom(sock, (char *)buffer, buffer_size, 0, (struct sockaddr *)&source_address, &address_size); + int recv_result = recvfrom(sock, (char *)buffer, buffer_size, 0, + (struct sockaddr *)&source_address, &address_size); if (verbose) { printf("recv_result: %d\n", recv_result); } @@ -775,7 +809,8 @@ int create_dhcp_socket(bool unicast, char *network_interface_name) { strncpy(interface.ifr_ifrn.ifrn_name, network_interface_name, IFNAMSIZ - 1); interface.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = '\0'; if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (char *)&interface, sizeof(interface)) < 0) { - printf(_("Error: Could not bind socket to interface %s. Check your privileges...\n"), network_interface_name); + printf(_("Error: Could not bind socket to interface %s. Check your privileges...\n"), + network_interface_name); exit(STATE_UNKNOWN); } @@ -786,7 +821,8 @@ int create_dhcp_socket(bool unicast, char *network_interface_name) { /* bind the socket */ if (bind(sock, (struct sockaddr *)&myname, sizeof(myname)) < 0) { - printf(_("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n"), DHCP_CLIENT_PORT); + printf(_("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n"), + DHCP_CLIENT_PORT); exit(STATE_UNKNOWN); } @@ -800,7 +836,8 @@ int close_dhcp_socket(int sock) { } /* adds a requested server address to list in memory */ -int add_requested_server(struct in_addr server_address, int *requested_servers, requested_server **requested_server_list) { +int add_requested_server(struct in_addr server_address, int *requested_servers, + requested_server **requested_server_list) { requested_server *new_server = (requested_server *)malloc(sizeof(requested_server)); if (new_server == NULL) { return ERROR; @@ -822,7 +859,8 @@ int add_requested_server(struct in_addr server_address, int *requested_servers, } /* adds a DHCP OFFER to list in memory */ -add_dhcp_offer_wrapper add_dhcp_offer(struct in_addr source, dhcp_packet *offer_packet, dhcp_offer *dhcp_offer_list) { +add_dhcp_offer_wrapper add_dhcp_offer(struct in_addr source, dhcp_packet *offer_packet, + dhcp_offer *dhcp_offer_list) { if (offer_packet == NULL) { add_dhcp_offer_wrapper tmp = { .error = ERROR, @@ -859,15 +897,18 @@ add_dhcp_offer_wrapper add_dhcp_offer(struct in_addr source, dhcp_packet *offer_ dhcp_lease_time = ntohl(dhcp_lease_time); break; case DHCP_OPTION_RENEWAL_TIME: - memcpy(&dhcp_renewal_time, &offer_packet->options[dchp_opt_idx], sizeof(dhcp_renewal_time)); + memcpy(&dhcp_renewal_time, &offer_packet->options[dchp_opt_idx], + sizeof(dhcp_renewal_time)); dhcp_renewal_time = ntohl(dhcp_renewal_time); break; case DHCP_OPTION_REBINDING_TIME: - memcpy(&dhcp_rebinding_time, &offer_packet->options[dchp_opt_idx], sizeof(dhcp_rebinding_time)); + memcpy(&dhcp_rebinding_time, &offer_packet->options[dchp_opt_idx], + sizeof(dhcp_rebinding_time)); dhcp_rebinding_time = ntohl(dhcp_rebinding_time); break; case DHCP_OPTION_SERVER_IDENTIFIER: - memcpy(&serv_ident.s_addr, &offer_packet->options[dchp_opt_idx], sizeof(serv_ident.s_addr)); + memcpy(&serv_ident.s_addr, &offer_packet->options[dchp_opt_idx], + sizeof(serv_ident.s_addr)); break; } @@ -955,7 +996,8 @@ int free_dhcp_offer_list(dhcp_offer *dhcp_offer_list) { /* frees memory allocated to requested server list */ int free_requested_server_list(requested_server *requested_server_list) { requested_server *next_server; - for (requested_server *this_server = requested_server_list; this_server != NULL; this_server = next_server) { + for (requested_server *this_server = requested_server_list; this_server != NULL; + this_server = next_server) { next_server = this_server->next; free(this_server); } @@ -964,8 +1006,10 @@ int free_requested_server_list(requested_server *requested_server_list) { } /* gets state and plugin output to return */ -mp_subcheck get_results(bool exclusive, const int requested_servers, const struct in_addr requested_address, bool request_specific_address, - requested_server *requested_server_list, int valid_responses, dhcp_offer *dhcp_offer_list) { +mp_subcheck get_results(bool exclusive, const int requested_servers, + const struct in_addr requested_address, bool request_specific_address, + requested_server *requested_server_list, int valid_responses, + dhcp_offer *dhcp_offer_list) { mp_subcheck sc_dhcp_results = mp_subcheck_init(); sc_dhcp_results = mp_set_subcheck_default_state(sc_dhcp_results, STATE_OK); @@ -995,22 +1039,28 @@ mp_subcheck get_results(bool exclusive, const int requested_servers, const struc /* checks responses from requested servers */ int requested_responses = 0; if (requested_servers > 0) { - for (requested_server *temp_server = requested_server_list; temp_server != NULL; temp_server = temp_server->next) { - for (dhcp_offer *temp_offer = dhcp_offer_list; temp_offer != NULL; temp_offer = temp_offer->next) { + for (requested_server *temp_server = requested_server_list; temp_server != NULL; + temp_server = temp_server->next) { + for (dhcp_offer *temp_offer = dhcp_offer_list; temp_offer != NULL; + temp_offer = temp_offer->next) { /* get max lease time we were offered */ - if (temp_offer->lease_time > max_lease_time || temp_offer->lease_time == DHCP_INFINITE_TIME) { + if (temp_offer->lease_time > max_lease_time || + temp_offer->lease_time == DHCP_INFINITE_TIME) { max_lease_time = temp_offer->lease_time; } /* see if we got the address we requested */ - if (!memcmp(&requested_address, &temp_offer->offered_address, sizeof(requested_address))) { + if (!memcmp(&requested_address, &temp_offer->offered_address, + sizeof(requested_address))) { received_requested_address = true; } /* see if the servers we wanted a response from, talked to us or not */ - if (!memcmp(&temp_offer->server_address, &temp_server->server_address, sizeof(temp_server->server_address))) { + if (!memcmp(&temp_offer->server_address, &temp_server->server_address, + sizeof(temp_server->server_address))) { if (verbose) { - printf(_("DHCP Server Match: Offerer=%s"), inet_ntoa(temp_offer->server_address)); + printf(_("DHCP Server Match: Offerer=%s"), + inet_ntoa(temp_offer->server_address)); printf(_(" Requested=%s"), inet_ntoa(temp_server->server_address)); if (temp_server->answered) { printf(_(" (duplicate)")); @@ -1028,7 +1078,8 @@ mp_subcheck get_results(bool exclusive, const int requested_servers, const struc } /* exclusive mode: check for undesired offers */ - for (dhcp_offer *temp_offer = dhcp_offer_list; temp_offer != NULL; temp_offer = temp_offer->next) { + for (dhcp_offer *temp_offer = dhcp_offer_list; temp_offer != NULL; + temp_offer = temp_offer->next) { if (!temp_offer->desired) { undesired_offer = temp_offer; /* Checks only for the first undesired offer */ break; /* no further checks needed */ @@ -1036,7 +1087,8 @@ mp_subcheck get_results(bool exclusive, const int requested_servers, const struc } mp_subcheck sc_rqust_srvs = mp_subcheck_init(); - xasprintf(&sc_rqust_srvs.output, "%d of %d requested servers responded", requested_responses, requested_servers); + xasprintf(&sc_rqust_srvs.output, "%d of %d requested servers responded", + requested_responses, requested_servers); if (requested_responses == requested_servers) { sc_rqust_srvs = mp_set_subcheck_state(sc_rqust_srvs, STATE_OK); @@ -1053,14 +1105,17 @@ mp_subcheck get_results(bool exclusive, const int requested_servers, const struc } else { /* else check and see if we got our requested address from any server */ - for (dhcp_offer *temp_offer = dhcp_offer_list; temp_offer != NULL; temp_offer = temp_offer->next) { + for (dhcp_offer *temp_offer = dhcp_offer_list; temp_offer != NULL; + temp_offer = temp_offer->next) { /* get max lease time we were offered */ - if (temp_offer->lease_time > max_lease_time || temp_offer->lease_time == DHCP_INFINITE_TIME) { + if (temp_offer->lease_time > max_lease_time || + temp_offer->lease_time == DHCP_INFINITE_TIME) { max_lease_time = temp_offer->lease_time; } /* see if we got the address we requested */ - if (!memcmp(&requested_address, &temp_offer->offered_address, sizeof(requested_address))) { + if (!memcmp(&requested_address, &temp_offer->offered_address, + sizeof(requested_address))) { received_requested_address = true; } } @@ -1069,7 +1124,8 @@ mp_subcheck get_results(bool exclusive, const int requested_servers, const struc if (max_lease_time == DHCP_INFINITE_TIME) { xasprintf(&sc_dhcp_results.output, "%s, max lease time = Infinity", sc_dhcp_results.output); } else { - xasprintf(&sc_dhcp_results.output, "%s, max lease time = %" PRIu32 " seconds", sc_dhcp_results.output, max_lease_time); + xasprintf(&sc_dhcp_results.output, "%s, max lease time = %" PRIu32 " seconds", + sc_dhcp_results.output, max_lease_time); } if (exclusive) { @@ -1083,8 +1139,8 @@ mp_subcheck get_results(bool exclusive, const int requested_servers, const struc // Get the addresses for printout // 1.address of the sending server char server_address[INET_ADDRSTRLEN]; - const char *server_address_transformed = - inet_ntop(AF_INET, &undesired_offer->server_address, server_address, sizeof(server_address)); + const char *server_address_transformed = inet_ntop( + AF_INET, &undesired_offer->server_address, server_address, sizeof(server_address)); if (server_address != server_address_transformed) { die(STATE_UNKNOWN, "inet_ntop failed"); @@ -1093,13 +1149,15 @@ mp_subcheck get_results(bool exclusive, const int requested_servers, const struc // 2.address offered char offered_address[INET_ADDRSTRLEN]; const char *offered_address_transformed = - inet_ntop(AF_INET, &undesired_offer->offered_address, offered_address, sizeof(offered_address)); + inet_ntop(AF_INET, &undesired_offer->offered_address, offered_address, + sizeof(offered_address)); if (offered_address != offered_address_transformed) { die(STATE_UNKNOWN, "inet_ntop failed"); } - xasprintf(&sc_rogue_server.output, "Rogue DHCP Server detected! Server %s offered %s", server_address, offered_address); + xasprintf(&sc_rogue_server.output, "Rogue DHCP Server detected! Server %s offered %s", + server_address, offered_address); } else { sc_rogue_server = mp_set_subcheck_state(sc_rogue_server, STATE_OK); xasprintf(&sc_rogue_server.output, "No Rogue DHCP Server detected"); @@ -1112,10 +1170,12 @@ mp_subcheck get_results(bool exclusive, const int requested_servers, const struc if (received_requested_address) { sc_rqustd_addr = mp_set_subcheck_state(sc_rqustd_addr, STATE_OK); - xasprintf(&sc_rqustd_addr.output, "Requested address (%s) was offered", inet_ntoa(requested_address)); + xasprintf(&sc_rqustd_addr.output, "Requested address (%s) was offered", + inet_ntoa(requested_address)); } else { sc_rqustd_addr = mp_set_subcheck_state(sc_rqustd_addr, STATE_WARNING); - xasprintf(&sc_rqustd_addr.output, "Requested address (%s) was NOT offered", inet_ntoa(requested_address)); + xasprintf(&sc_rqustd_addr.output, "Requested address (%s) was NOT offered", + inet_ntoa(requested_address)); } mp_add_subcheck_to_subcheck(&sc_dhcp_results, sc_rqustd_addr); @@ -1138,18 +1198,19 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { }; int option_index = 0; - static struct option long_options[] = {{"serverip", required_argument, 0, 's'}, - {"requestedip", required_argument, 0, 'r'}, - {"timeout", required_argument, 0, 't'}, - {"interface", required_argument, 0, 'i'}, - {"mac", required_argument, 0, 'm'}, - {"unicast", no_argument, 0, 'u'}, - {"exclusive", no_argument, 0, 'x'}, - {"verbose", no_argument, 0, 'v'}, - {"version", no_argument, 0, 'V'}, - {"help", no_argument, 0, 'h'}, - {"output-format", required_argument, 0, output_format_index}, - {0, 0, 0, 0}}; + static struct option long_options[] = { + {"serverip", required_argument, 0, 's'}, + {"requestedip", required_argument, 0, 'r'}, + {"timeout", required_argument, 0, 't'}, + {"interface", required_argument, 0, 'i'}, + {"mac", required_argument, 0, 'm'}, + {"unicast", no_argument, 0, 'u'}, + {"exclusive", no_argument, 0, 'x'}, + {"verbose", no_argument, 0, 'v'}, + {"version", no_argument, 0, 'V'}, + {"help", no_argument, 0, 'h'}, + {"output-format", required_argument, 0, output_format_index}, + {0, 0, 0, 0}}; check_dhcp_config config = check_dhcp_config_init(); int option_char = 0; @@ -1163,7 +1224,8 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { switch (option_char) { case 's': /* DHCP server address */ resolve_host(optarg, &config.dhcp_ip); - add_requested_server(config.dhcp_ip, &config.num_of_requested_servers, &config.requested_server_list); + add_requested_server(config.dhcp_ip, &config.num_of_requested_servers, + &config.requested_server_list); break; case 'r': /* address we are requested from DHCP servers */ @@ -1187,7 +1249,8 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { break; case 'i': /* interface name */ - strncpy(config.network_interface_name, optarg, sizeof(config.network_interface_name) - 1); + strncpy(config.network_interface_name, optarg, + sizeof(config.network_interface_name) - 1); config.network_interface_name[sizeof(config.network_interface_name) - 1] = '\x0'; break; @@ -1289,7 +1352,8 @@ static int put_ctrl(int fd, int len, int pri) { ctl.len = len; if (putmsg(fd, &ctl, 0, pri) < 0) { - printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n"), strerror(errno)); + printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n"), + strerror(errno)); exit(STATE_UNKNOWN); } @@ -1302,7 +1366,8 @@ static int put_both(int fd, int clen, int dlen, int pri) { ctl.len = clen; dat.len = dlen; if (putmsg(fd, &ctl, &dat, pri) < 0) { - printf(_("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"), strerror(errno)); + printf(_("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"), + strerror(errno)); exit(STATE_UNKNOWN); } @@ -1314,7 +1379,8 @@ static int dl_open(const char *dev, int unit, int *fd) { dl_attach_req_t *attach_req = (dl_attach_req_t *)ctl_area; if ((*fd = open(dev, O_RDWR)) == -1) { - printf(_("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n"), dev, strerror(errno)); + printf(_("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n"), + dev, strerror(errno)); exit(STATE_UNKNOWN); } attach_req->dl_primitive = DL_ATTACH_REQ; @@ -1338,7 +1404,8 @@ static int dl_bind(int fd, int sap, u_char *addr) { put_ctrl(fd, sizeof(dl_bind_req_t), 0); get_msg(fd); if (GOT_ERR == check_ctrl(DL_BIND_ACK)) { - printf(_("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n"), strerror(errno)); + printf(_("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n"), + strerror(errno)); exit(STATE_UNKNOWN); } bcopy((u_char *)bind_ack + bind_ack->dl_addr_offset, addr, bind_ack->dl_addr_length); @@ -1455,7 +1522,8 @@ void print_help(void) { printf(" %s\n", "-u, --unicast"); printf(" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s")); printf(" %s\n", "-x, --exclusive"); - printf(" %s\n", _("Only requested DHCP server may response (rogue DHCP server detection), requires -s")); + printf(" %s\n", + _("Only requested DHCP server may response (rogue DHCP server detection), requires -s")); printf(UT_SUPPORT); } diff --git a/plugins-root/check_icmp.d/config.h b/plugins-root/check_icmp.d/config.h index c348bef5..be388d0a 100644 --- a/plugins-root/check_icmp.d/config.h +++ b/plugins-root/check_icmp.d/config.h @@ -97,7 +97,7 @@ typedef struct icmp_ping_data { /* 80 msec packet interval by default */ // DEPRECATED, remove when removing the option -#define DEFAULT_PKT_INTERVAL 80000 +#define DEFAULT_PKT_INTERVAL 80000 #define DEFAULT_TARGET_INTERVAL 0 diff --git a/plugins-root/pst3.c b/plugins-root/pst3.c index 1f69f3a6..1bfe3d35 100644 --- a/plugins-root/pst3.c +++ b/plugins-root/pst3.c @@ -1,45 +1,45 @@ /***************************************************************************** -* -* pst3 -* -* License: GPL -* Copyright (c) 2008 Monitoring Plugins Development Team -* -* Description: -* -* This file contains the pst3 executable. This is a replacement ps command -* for Solaris to get output which provides a long argument listing, which -* is not possible with the standard ps command (due to truncation). /usr/ucb/ps -* also has issues where some fields run into each other. -* -* This executable works by reading process address structures, so needs -* to be executed as root -* -* Originally written by R.W.Ingraham -* Rewritten by Duncan Ferguson (Altinity Ltd, June 2008) -* The rewrite was necessary as /dev/kmem is not available within -* non-global zones on Solaris 10 -* -* Details for rewrite came from -* source of /usr/ucb/ps on Solaris: -* http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/ucbcmd/ps/ps.c#argvoff -* usenet group posting -* http://groups.google.com/group/comp.unix.solaris/tree/browse_frm/month/2001-09/bfa40c08bac819a2?rnum=141&_done=%2Fgroup%2Fcomp.unix.solaris%2Fbrowse_frm%2Fmonth%2F2001-09%3F -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -* -*****************************************************************************/ + * + * pst3 + * + * License: GPL + * Copyright (c) 2008 Monitoring Plugins Development Team + * + * Description: + * + * This file contains the pst3 executable. This is a replacement ps command + * for Solaris to get output which provides a long argument listing, which + * is not possible with the standard ps command (due to truncation). /usr/ucb/ps + * also has issues where some fields run into each other. + * + * This executable works by reading process address structures, so needs + * to be executed as root + * + * Originally written by R.W.Ingraham + * Rewritten by Duncan Ferguson (Altinity Ltd, June 2008) + * The rewrite was necessary as /dev/kmem is not available within + * non-global zones on Solaris 10 + * + * Details for rewrite came from + * source of /usr/ucb/ps on Solaris: + * http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/ucbcmd/ps/ps.c#argvoff + * usenet group posting + * http://groups.google.com/group/comp.unix.solaris/tree/browse_frm/month/2001-09/bfa40c08bac819a2?rnum=141&_done=%2Fgroup%2Fcomp.unix.solaris%2Fbrowse_frm%2Fmonth%2F2001-09%3F + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *****************************************************************************/ #include #include @@ -55,14 +55,14 @@ * Constants */ -#define PROC_DIR "/proc" -#define ARGS 30 +#define PROC_DIR "/proc" +#define ARGS 30 /* * Globals */ -static char * szProg; +static char *szProg; /* * Prototypes @@ -71,192 +71,179 @@ void usage(); /*----------------------------------------------------------------------------*/ -int main (int argc, char **argv) -{ - DIR *procdir; - struct dirent *proc; - char ps_name[ARGS]; - char as_name[ARGS]; - psinfo_t psinfo; - - /* Set our program name global */ - if ((szProg = strrchr(argv[0], '/')) != NULL) - szProg++; - else - szProg = argv[0]; - - /* if given any parameters, print out help */ - if(argc > 1) { - (void)usage(); - exit(1); - } - - /* Make sure that our euid is root */ - if (geteuid() != 0) - { - fprintf(stderr, "%s: This program can only be run by the root user!\n", szProg); - exit(1); - } - - if ((procdir = opendir(PROC_DIR)) == NULL) { - fprintf(stderr, "%s: cannot open PROC directory %s\n", szProg, PROC_DIR); - exit(1); - } - - /* Display column headings */ - printf("%c %5s %5s %5s %6s %6s %4s %s %s\n", - 'S', - "UID", - "PID", - "PPID", - "VSZ", - "RSS", - "%CPU", - "COMMAND", - "ARGS" - ); - - /* Zip through all of the process entries */ - while((proc = readdir(procdir))) { - int ps_fd; - int as_fd; - off_t argoff; - int i; - char *args; - char *procname; - char *ptr; - int argslen; - uintptr_t args_addr;; - uintptr_t *args_vecs;; - int args_count; - - if(proc->d_name[0] == '.') - continue; - - sprintf(ps_name,"%s/%s/%s",PROC_DIR,proc->d_name,"psinfo"); - sprintf(as_name,"%s/%s/%s",PROC_DIR,proc->d_name,"as"); -try_again: - if((ps_fd = open(ps_name, O_RDONLY)) == -1) - continue; - - if((as_fd = open(as_name, O_RDONLY)) == -1) { - close(ps_fd); - continue; - } - - if(read(ps_fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) { - int err = errno; - close(ps_fd); - close(as_fd); - if(err == EAGAIN) goto try_again; - if(err != ENOENT) - fprintf(stderr, "%s: read() on %s: %s\n", szProg, - ps_name, strerror(err)); - continue; - } - close(ps_fd); - - /* system process, ignore since the previous version did */ - if( - psinfo.pr_nlwp == 0 || - strcmp(psinfo.pr_lwp.pr_clname, "SYS") == 0 - ) { - continue; - } - - /* get the procname to match previous versions */ - procname = strdup(psinfo.pr_psargs); - if((ptr = strchr(procname, ' ')) != NULL) - *ptr = '\0'; - if((ptr = strrchr(procname, '/')) != NULL) - ptr++; - else - ptr = procname; - - /* - * print out what we currently know - */ - printf("%c %5d %5d %5d %6lu %6lu %4.1f %s ", - psinfo.pr_lwp.pr_sname, - psinfo.pr_euid, - psinfo.pr_pid, - psinfo.pr_ppid, - psinfo.pr_size, - psinfo.pr_rssize, - ((float)(psinfo.pr_pctcpu) / 0x8000 * 100.0), - ptr - ); - free(procname); - - /* - * and now for the command line stuff - */ - - args_addr = psinfo.pr_argv; - args_count = psinfo.pr_argc; - args_vecs = malloc(args_count * sizeof(uintptr_t)); - - if(psinfo.pr_dmodel == PR_MODEL_NATIVE) { - /* this process matches target process */ - pread(as_fd,args_vecs, args_count * sizeof(uintptr_t), - args_addr); - } else { - /* this process is 64bit, target process is 32 bit*/ - caddr32_t *args_vecs32 = (caddr32_t *)args_vecs; - pread(as_fd,args_vecs32,args_count * sizeof(caddr32_t), - args_addr); - for (i=args_count-1;i>=0;--i) - args_vecs[i]=args_vecs32[i]; - } - - /* - * now read in the args - if what we read in fills buffer - * resize buffer and reread that bit again - */ - argslen=ARGS; - args=malloc(argslen+1); - for(i=0;i 1) { + (void)usage(); + exit(1); + } + + /* Make sure that our euid is root */ + if (geteuid() != 0) { + fprintf(stderr, "%s: This program can only be run by the root user!\n", szProg); + exit(1); + } + + if ((procdir = opendir(PROC_DIR)) == NULL) { + fprintf(stderr, "%s: cannot open PROC directory %s\n", szProg, PROC_DIR); + exit(1); + } + + /* Display column headings */ + printf("%c %5s %5s %5s %6s %6s %4s %s %s\n", 'S', "UID", "PID", "PPID", "VSZ", "RSS", "%CPU", + "COMMAND", "ARGS"); + + /* Zip through all of the process entries */ + while ((proc = readdir(procdir))) { + int ps_fd; + int as_fd; + off_t argoff; + int i; + char *args; + char *procname; + char *ptr; + int argslen; + uintptr_t args_addr; + ; + uintptr_t *args_vecs; + ; + int args_count; + + if (proc->d_name[0] == '.') { + continue; + } + + sprintf(ps_name, "%s/%s/%s", PROC_DIR, proc->d_name, "psinfo"); + sprintf(as_name, "%s/%s/%s", PROC_DIR, proc->d_name, "as"); + try_again: + if ((ps_fd = open(ps_name, O_RDONLY)) == -1) { + continue; + } + + if ((as_fd = open(as_name, O_RDONLY)) == -1) { + close(ps_fd); + continue; + } + + if (read(ps_fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) { + int err = errno; + close(ps_fd); + close(as_fd); + if (err == EAGAIN) { + goto try_again; + } + if (err != ENOENT) { + fprintf(stderr, "%s: read() on %s: %s\n", szProg, ps_name, strerror(err)); + } + continue; + } + close(ps_fd); + + /* system process, ignore since the previous version did */ + if (psinfo.pr_nlwp == 0 || strcmp(psinfo.pr_lwp.pr_clname, "SYS") == 0) { + continue; + } + + /* get the procname to match previous versions */ + procname = strdup(psinfo.pr_psargs); + if ((ptr = strchr(procname, ' ')) != NULL) { + *ptr = '\0'; + } + if ((ptr = strrchr(procname, '/')) != NULL) { + ptr++; + } else { + ptr = procname; + } + + /* + * print out what we currently know + */ + printf("%c %5d %5d %5d %6lu %6lu %4.1f %s ", psinfo.pr_lwp.pr_sname, psinfo.pr_euid, + psinfo.pr_pid, psinfo.pr_ppid, psinfo.pr_size, psinfo.pr_rssize, + ((float)(psinfo.pr_pctcpu) / 0x8000 * 100.0), ptr); + free(procname); + + /* + * and now for the command line stuff + */ + + args_addr = psinfo.pr_argv; + args_count = psinfo.pr_argc; + args_vecs = malloc(args_count * sizeof(uintptr_t)); + + if (psinfo.pr_dmodel == PR_MODEL_NATIVE) { + /* this process matches target process */ + pread(as_fd, args_vecs, args_count * sizeof(uintptr_t), args_addr); + } else { + /* this process is 64bit, target process is 32 bit*/ + caddr32_t *args_vecs32 = (caddr32_t *)args_vecs; + pread(as_fd, args_vecs32, args_count * sizeof(caddr32_t), args_addr); + for (i = args_count - 1; i >= 0; --i) { + args_vecs[i] = args_vecs32[i]; + } + } + + /* + * now read in the args - if what we read in fills buffer + * resize buffer and reread that bit again + */ + argslen = ARGS; + args = malloc(argslen + 1); + for (i = 0; i < args_count; i++) { + memset(args, '\0', argslen + 1); + if (pread(as_fd, args, argslen, args_vecs[i]) <= 0) { + break; + } + args[argslen] = '\0'; + if (strlen(args) == argslen) { + argslen += ARGS; + args = realloc(args, argslen + 1); + i--; + continue; + } + printf(" %s", args); + } + free(args_vecs); + free(args); + close(as_fd); + printf("\n"); + } + + (void)closedir(procdir); + + return (0); } /*----------------------------------------------------------------------------*/ void usage() { - printf("%s: Help output\n\n", szProg); - printf("If this program is given any arguments, this help is displayed.\n"); - printf("This command is used to print out the full command line for all\n"); - printf("running processes because /usr/bin/ps is limited to 80 chars and\n"); - printf("/usr/ucb/ps can merge columns together.\n\n"); - printf("Columns are:\n"); - printf("\tS - State of process - see 'ps' man page\n"); - printf("\tUID - UID of the process owner\n"); - printf("\tPID - PID of the process\n"); - printf("\tPPID - PID of the parent process\n"); - printf("\tVSZ - Virtual memory usage (kilobytes)\n"); - printf("\tRSS - Real memory usage (kilobytes)\n"); - printf("\t%%CPU - CPU usage\n"); - printf("\tCOMMAND - Command being run\n"); - printf("\tARGS - Full command line with arguments\n"); - return; + printf("%s: Help output\n\n", szProg); + printf("If this program is given any arguments, this help is displayed.\n"); + printf("This command is used to print out the full command line for all\n"); + printf("running processes because /usr/bin/ps is limited to 80 chars and\n"); + printf("/usr/ucb/ps can merge columns together.\n\n"); + printf("Columns are:\n"); + printf("\tS - State of process - see 'ps' man page\n"); + printf("\tUID - UID of the process owner\n"); + printf("\tPID - PID of the process\n"); + printf("\tPPID - PID of the parent process\n"); + printf("\tVSZ - Virtual memory usage (kilobytes)\n"); + printf("\tRSS - Real memory usage (kilobytes)\n"); + printf("\t%%CPU - CPU usage\n"); + printf("\tCOMMAND - Command being run\n"); + printf("\tARGS - Full command line with arguments\n"); + return; } -- cgit v1.2.3-74-g34f1 From 811da10fda11b1d0453453040647270700862d8f Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Mon, 15 Sep 2025 15:49:26 +0200 Subject: More refactoring --- plugins-root/check_icmp.d/check_icmp_helpers.c | 4 +- plugins/check_load.c | 4 +- plugins/netutils.c | 224 +++++++++++++------------ plugins/netutils.h | 32 ++-- 4 files changed, 139 insertions(+), 125 deletions(-) (limited to 'plugins-root') diff --git a/plugins-root/check_icmp.d/check_icmp_helpers.c b/plugins-root/check_icmp.d/check_icmp_helpers.c index d56fbd8b..1b96a392 100644 --- a/plugins-root/check_icmp.d/check_icmp_helpers.c +++ b/plugins-root/check_icmp.d/check_icmp_helpers.c @@ -76,7 +76,7 @@ check_icmp_state check_icmp_state_init() { ping_target_create_wrapper ping_target_create(struct sockaddr_storage address) { ping_target_create_wrapper result = { - .errorcode = OK, + .errorcode = 0, }; struct sockaddr_storage *tmp_addr = &address; @@ -88,7 +88,7 @@ ping_target_create_wrapper ping_target_create(struct sockaddr_storage address) { ((struct sockaddr_in *)tmp_addr)->sin_addr.s_addr == INADDR_ANY))) || (tmp_addr->ss_family == AF_INET6 && (((struct sockaddr_in6 *)tmp_addr)->sin6_addr.s6_addr == in6addr_any.s6_addr))) { - result.errorcode = ERROR; + result.errorcode = 1; return result; } diff --git a/plugins/check_load.c b/plugins/check_load.c index f7a6f7fd..644cd604 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c @@ -452,8 +452,8 @@ static top_processes_result print_top_consuming_processes(unsigned long n_procs_ top_processes_result result = { .errorcode = OK, }; - struct output chld_out; - struct output chld_err; + output chld_out; + output chld_err; if (np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0) { fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND); result.errorcode = ERROR; diff --git a/plugins/netutils.c b/plugins/netutils.c index 92c53e4c..b4c6ff0a 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c @@ -30,13 +30,14 @@ #include "common.h" #include "output.h" #include "states.h" +#include #include "netutils.h" unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; -unsigned int socket_timeout_state = STATE_CRITICAL; - -int econn_refuse_state = STATE_CRITICAL; +mp_state_enum socket_timeout_state = STATE_CRITICAL; +mp_state_enum econn_refuse_state = STATE_CRITICAL; bool was_refused = false; + #if USE_IPV6 int address_family = AF_UNSPEC; #else @@ -63,39 +64,40 @@ void socket_timeout_alarm_handler(int sig) { /* connects to a host on a specified tcp port, sends a string, and gets a response. loops on select-recv until timeout or eof to get all of a multi-packet answer */ -int process_tcp_request2(const char *server_address, int server_port, const char *send_buffer, - char *recv_buffer, int recv_size) { +mp_state_enum process_tcp_request2(const char *server_address, const int server_port, + const char *send_buffer, char *recv_buffer, + const int recv_size) { - int result; - int send_result; - int recv_result; - int sd; - struct timeval tv; - fd_set readfds; - int recv_length = 0; + int socket; - result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); - if (result != STATE_OK) { + mp_state_enum connect_result = + np_net_connect(server_address, server_port, &socket, IPPROTO_TCP); + if (connect_result != STATE_OK) { return STATE_CRITICAL; } - send_result = send(sd, send_buffer, strlen(send_buffer), 0); + mp_state_enum result; + ssize_t send_result = send(socket, send_buffer, strlen(send_buffer), 0); if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { // printf("%s\n", _("Send failed")); result = STATE_WARNING; } - while (1) { + fd_set readfds; + ssize_t recv_length = 0; + while (true) { /* wait up to the number of seconds for socket timeout minus one for data from the host */ - tv.tv_sec = socket_timeout - 1; - tv.tv_usec = 0; + struct timeval timeout = { + .tv_sec = socket_timeout - 1, + .tv_usec = 0, + }; FD_ZERO(&readfds); - FD_SET(sd, &readfds); - select(sd + 1, &readfds, NULL, NULL, &tv); + FD_SET(socket, &readfds); + select(socket + 1, &readfds, NULL, NULL, &timeout); /* make sure some data has arrived */ - if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ + if (!FD_ISSET(socket, &readfds)) { /* it hasn't */ if (!recv_length) { strcpy(recv_buffer, ""); // printf("%s\n", _("No data was received from host!")); @@ -104,72 +106,69 @@ int process_tcp_request2(const char *server_address, int server_port, const char recv_buffer[recv_length] = 0; } break; - } else { /* it has */ - recv_result = - recv(sd, recv_buffer + recv_length, (size_t)recv_size - recv_length - 1, 0); - if (recv_result == -1) { - /* recv failed, bail out */ - strcpy(recv_buffer + recv_length, ""); - result = STATE_WARNING; - break; - } else if (recv_result == 0) { - /* end of file ? */ - recv_buffer[recv_length] = 0; - break; - } else { /* we got data! */ - recv_length += recv_result; - if (recv_length >= recv_size - 1) { - /* buffer full, we're done */ - recv_buffer[recv_size - 1] = 0; - break; - } - } + } /* it has */ + + ssize_t recv_result = + recv(socket, recv_buffer + recv_length, (size_t)(recv_size - recv_length - 1), 0); + if (recv_result == -1) { + /* recv failed, bail out */ + strcpy(recv_buffer + recv_length, ""); + result = STATE_WARNING; + break; + } + + if (recv_result == 0) { + /* end of file ? */ + recv_buffer[recv_length] = 0; + break; + } + + /* we got data! */ + recv_length += recv_result; + if (recv_length >= recv_size - 1) { + /* buffer full, we're done */ + recv_buffer[recv_size - 1] = 0; + break; } /* end if(!FD_ISSET(sd,&readfds)) */ } - /* end while(1) */ - close(sd); + close(socket); return result; } /* connects to a host on a specified port, sends a string, and gets a response */ -int process_request(const char *server_address, int server_port, int proto, const char *send_buffer, - char *recv_buffer, int recv_size) { - int result; - int sd; +mp_state_enum process_request(const char *server_address, const int server_port, const int proto, + const char *send_buffer, char *recv_buffer, const int recv_size) { - result = STATE_OK; - - result = np_net_connect(server_address, server_port, &sd, proto); + mp_state_enum result = STATE_OK; + int socket; + result = np_net_connect(server_address, server_port, &socket, proto); if (result != STATE_OK) { return STATE_CRITICAL; } - result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); + result = send_request(socket, proto, send_buffer, recv_buffer, recv_size); - close(sd); + close(socket); return result; } /* opens a tcp or udp connection to a remote host or local socket */ -int np_net_connect(const char *host_name, int port, int *sd, int proto) { +mp_state_enum np_net_connect(const char *host_name, int port, int *socketDescriptor, + const int proto) { /* send back STATE_UNKOWN if there's an error send back STATE_OK if we connect send back STATE_CRITICAL if we can't connect. Let upstream figure out what to send to the user. */ - struct addrinfo hints; - struct addrinfo *r, *res; - struct sockaddr_un su; - char port_str[6], host[MAX_HOST_ADDRESS_LENGTH]; - size_t len; - int socktype, result; - short is_socket = (host_name[0] == '/'); - - socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; + bool is_socket = (host_name[0] == '/'); + int socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; + struct addrinfo hints = {}; + struct addrinfo *res = NULL; + int result; /* as long as it doesn't start with a '/', it's assumed a host or ip */ if (!is_socket) { memset(&hints, 0, sizeof(hints)); @@ -177,38 +176,46 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { hints.ai_protocol = proto; hints.ai_socktype = socktype; - len = strlen(host_name); + size_t len = strlen(host_name); /* check for an [IPv6] address (and strip the brackets) */ if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') { host_name++; len -= 2; } + + char host[MAX_HOST_ADDRESS_LENGTH]; + if (len >= sizeof(host)) { return STATE_UNKNOWN; } + memcpy(host, host_name, len); host[len] = '\0'; + + char port_str[6]; snprintf(port_str, sizeof(port_str), "%d", port); - result = getaddrinfo(host, port_str, &hints, &res); + int getaddrinfo_err = getaddrinfo(host, port_str, &hints, &res); - if (result != 0) { + if (getaddrinfo_err != 0) { // printf("%s\n", gai_strerror(result)); return STATE_UNKNOWN; } - r = res; - while (r) { + struct addrinfo *addressPointer = res; + while (addressPointer) { /* attempt to create a socket */ - *sd = socket(r->ai_family, socktype, r->ai_protocol); + *socketDescriptor = + socket(addressPointer->ai_family, socktype, addressPointer->ai_protocol); - if (*sd < 0) { + if (*socketDescriptor < 0) { // printf("%s\n", _("Socket creation failed")); - freeaddrinfo(r); + freeaddrinfo(addressPointer); return STATE_UNKNOWN; } /* attempt to open a connection */ - result = connect(*sd, r->ai_addr, r->ai_addrlen); + result = + connect(*socketDescriptor, addressPointer->ai_addr, addressPointer->ai_addrlen); if (result == 0) { was_refused = false; @@ -223,24 +230,28 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { } } - close(*sd); - r = r->ai_next; + close(*socketDescriptor); + addressPointer = addressPointer->ai_next; } + freeaddrinfo(res); - } - /* else the hostname is interpreted as a path to a unix socket */ - else { + + } else { + /* else the hostname is interpreted as a path to a unix socket */ if (strlen(host_name) >= UNIX_PATH_MAX) { die(STATE_UNKNOWN, _("Supplied path too long unix domain socket")); } - memset(&su, 0, sizeof(su)); + + struct sockaddr_un su = {}; su.sun_family = AF_UNIX; strncpy(su.sun_path, host_name, UNIX_PATH_MAX); - *sd = socket(PF_UNIX, SOCK_STREAM, 0); - if (*sd < 0) { + *socketDescriptor = socket(PF_UNIX, SOCK_STREAM, 0); + + if (*socketDescriptor < 0) { die(STATE_UNKNOWN, _("Socket creation failed")); } - result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); + + result = connect(*socketDescriptor, (struct sockaddr *)&su, sizeof(su)); if (result < 0 && errno == ECONNREFUSED) { was_refused = true; } @@ -248,7 +259,9 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { if (result == 0) { return STATE_OK; - } else if (was_refused) { + } + + if (was_refused) { switch (econn_refuse_state) { /* a user-defined expected outcome */ case STATE_OK: case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ @@ -275,14 +288,11 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { } } -int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size) { - int result = STATE_OK; - int send_result; - int recv_result; - struct timeval tv; - fd_set readfds; +mp_state_enum send_request(const int socket, const int proto, const char *send_buffer, + char *recv_buffer, const int recv_size) { + mp_state_enum result = STATE_OK; - send_result = send(sd, send_buffer, strlen(send_buffer), 0); + ssize_t send_result = send(socket, send_buffer, strlen(send_buffer), 0); if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { // printf("%s\n", _("Send failed")); result = STATE_WARNING; @@ -290,21 +300,22 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, /* wait up to the number of seconds for socket timeout minus one for data from the host */ - tv.tv_sec = socket_timeout - 1; - tv.tv_usec = 0; + struct timeval timestamp = { + .tv_sec = socket_timeout - 1, + .tv_usec = 0, + }; + fd_set readfds; FD_ZERO(&readfds); - FD_SET(sd, &readfds); - select(sd + 1, &readfds, NULL, NULL, &tv); + FD_SET(socket, &readfds); + select(socket + 1, &readfds, NULL, NULL, ×tamp); /* make sure some data has arrived */ - if (!FD_ISSET(sd, &readfds)) { + if (!FD_ISSET(socket, &readfds)) { strcpy(recv_buffer, ""); // printf("%s\n", _("No data was received from host!")); result = STATE_WARNING; - } - - else { - recv_result = recv(sd, recv_buffer, (size_t)recv_size - 1, 0); + } else { + ssize_t recv_result = recv(socket, recv_buffer, (size_t)(recv_size - 1), 0); if (recv_result == -1) { strcpy(recv_buffer, ""); if (proto != IPPROTO_TCP) { @@ -318,6 +329,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, /* die returned string */ recv_buffer[recv_size - 1] = 0; } + return result; } @@ -339,27 +351,27 @@ bool is_addr(const char *address) { #ifdef USE_IPV6 if (address_family == AF_INET && is_inet_addr(address)) { return true; - } else if (address_family == AF_INET6 && is_inet6_addr(address)) { + } + + if (address_family == AF_INET6 && is_inet6_addr(address)) { return true; } #else if (is_inet_addr(address)) { - return (true); + return true; } #endif - return (false); + return false; } -int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { +bool dns_lookup(const char *node_string, struct sockaddr_storage *ss, const int family) { struct addrinfo hints; - struct addrinfo *res; - int retval; - memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = family; - retval = getaddrinfo(in, NULL, &hints, &res); + struct addrinfo *res; + int retval = getaddrinfo(node_string, NULL, &hints, &res); if (retval != 0) { return false; } @@ -367,6 +379,8 @@ int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { if (ss != NULL) { memcpy(ss, res->ai_addr, res->ai_addrlen); } + freeaddrinfo(res); + return true; } diff --git a/plugins/netutils.h b/plugins/netutils.h index 6adb8e01..c4461113 100644 --- a/plugins/netutils.h +++ b/plugins/netutils.h @@ -31,7 +31,6 @@ #ifndef _NETUTILS_H_ #define _NETUTILS_H_ -#include "common.h" #include "output.h" #include "states.h" #include "utils.h" @@ -56,25 +55,26 @@ process_request(addr, port, IPPROTO_TCP, sbuf, rbuf, rsize) #define process_udp_request(addr, port, sbuf, rbuf, rsize) \ process_request(addr, port, IPPROTO_UDP, sbuf, rbuf, rsize) -int process_tcp_request2(const char *address, int port, const char *sbuffer, char *rbuffer, - int rsize); -int process_request(const char *address, int port, int proto, const char *sbuffer, char *rbuffer, - int rsize); +mp_state_enum process_tcp_request2(const char *server_address, int server_port, + const char *send_buffer, char *recv_buffer, int recv_size); +mp_state_enum process_request(const char *server_address, int server_port, int proto, + const char *send_buffer, char *recv_buffer, int recv_size); /* my_connect and wrapper macros */ #define my_tcp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_TCP) #define my_udp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_UDP) -int np_net_connect(const char *address, int port, int *sd, int proto); +mp_state_enum np_net_connect(const char *host_name, int port, int *socketDescriptor, int proto); /* send_request and wrapper macros */ #define send_tcp_request(s, sbuf, rbuf, rsize) send_request(s, IPPROTO_TCP, sbuf, rbuf, rsize) #define send_udp_request(s, sbuf, rbuf, rsize) send_request(s, IPPROTO_UDP, sbuf, rbuf, rsize) -int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size); +mp_state_enum send_request(int socket, int proto, const char *send_buffer, char *recv_buffer, + int recv_size); /* "is_*" wrapper macros and functions */ bool is_host(const char *); bool is_addr(const char *); -int dns_lookup(const char *, struct sockaddr_storage *, int); +bool dns_lookup(const char *, struct sockaddr_storage *, int); void host_or_die(const char *str); #define resolve_host_or_addr(addr, family) dns_lookup(addr, NULL, family) #define is_inet_addr(addr) resolve_host_or_addr(addr, AF_INET) @@ -86,8 +86,8 @@ void host_or_die(const char *str); #endif extern unsigned int socket_timeout; -extern unsigned int socket_timeout_state; -extern int econn_refuse_state; +extern mp_state_enum socket_timeout_state; +extern mp_state_enum econn_refuse_state; extern bool was_refused; extern int address_family; @@ -106,12 +106,12 @@ void socket_timeout_alarm_handler(int) __attribute__((noreturn)); # define MP_TLSv1_1_OR_NEWER 9 # define MP_TLSv1_2_OR_NEWER 10 /* maybe this could be merged with the above np_net_connect, via some flags */ -int np_net_ssl_init(int sd); -int np_net_ssl_init_with_hostname(int sd, char *host_name); -int np_net_ssl_init_with_hostname_and_version(int sd, char *host_name, int version); -int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int version, char *cert, - char *privkey); -void np_net_ssl_cleanup(); +int np_net_ssl_init(int socket); +int np_net_ssl_init_with_hostname(int socket, char *host_name); +int np_net_ssl_init_with_hostname_and_version(int socket, char *host_name, int version); +int np_net_ssl_init_with_hostname_version_and_cert(int socket, char *host_name, int version, + char *cert, char *privkey); +void np_net_ssl_cleanup(void); int np_net_ssl_write(const void *buf, int num); int np_net_ssl_read(void *buf, int num); mp_state_enum np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit); -- cgit v1.2.3-74-g34f1