From 65237fd7a5e70b05ba39f26141d8fc8aa1fc99dc Mon Sep 17 00:00:00 2001 From: Patrick Cervicek Date: Fri, 9 Oct 2015 11:46:51 +0200 Subject: check_dhcp.c merged patch from #752 - added dhcp rogue detection contributed by Patrick Cervicek (patrick AT cervicek.de) - closes #752 diff --git a/plugins-root/check_dhcp.c b/plugins-root/check_dhcp.c index 0ddace5..8b8bb98 100644 --- a/plugins-root/check_dhcp.c +++ b/plugins-root/check_dhcp.c @@ -150,6 +150,7 @@ typedef struct dhcp_offer_struct{ uint32_t lease_time; /* lease time in seconds */ uint32_t renewal_time; /* renewal time in seconds */ uint32_t rebinding_time; /* rebinding time in seconds */ + u_int8_t desired; /* is this offer desired (necessary in exclusive mode) */ struct dhcp_offer_struct *next; }dhcp_offer; @@ -193,6 +194,7 @@ typedef struct requested_server_struct{ #define ETHERNET_HARDWARE_ADDRESS_LENGTH 6 /* length of Ethernet hardware addresses */ uint8_t unicast = 0; /* unicast mode: mimic a DHCP relay */ +u_int8_t exclusive = 0; /* exclusive mode aka "rogue DHCP server detection" */ struct in_addr my_ip; /* our address (required for relay) */ struct in_addr dhcp_ip; /* server to query (if in unicast mode) */ unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]=""; @@ -894,6 +896,7 @@ int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){ new_offer->lease_time=dhcp_lease_time; new_offer->renewal_time=dhcp_renewal_time; new_offer->rebinding_time=dhcp_rebinding_time; + new_offer->desired=FALSE; /* exclusive mode: we'll check that in get_results */ if(verbose){ @@ -939,7 +942,7 @@ int free_requested_server_list(void){ /* gets state and plugin output to return */ int get_results(void){ - dhcp_offer *temp_offer; + dhcp_offer *temp_offer, *undesired_offer=NULL; requested_server *temp_server; int result; uint32_t max_lease_time=0; @@ -979,6 +982,13 @@ int get_results(void){ } } + /* exclusive mode: check for undesired offers */ + for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next) { + if (temp_offer->desired == FALSE) { + undesired_offer=temp_offer; /* Checks only for the first undesired offer */ + break; /* no further checks needed */ + } + } } /* else check and see if we got our requested address from any server */ @@ -1006,6 +1016,9 @@ int get_results(void){ else if(request_specific_address && !received_requested_address) result=STATE_WARNING; + if(exclusive && undesired_offer) + result=STATE_CRITICAL; + if(result==0) /* garrett honeycutt 2005 */ printf("OK: "); else if(result==1) @@ -1023,6 +1036,13 @@ int get_results(void){ printf(_("Received %d DHCPOFFER(s)"),valid_responses); + + if(exclusive && undesired_offer){ + printf(_(", Rogue DHCP Server detected! Server %s"),inet_ntoa(undesired_offer->server_address)); + printf(_(" offered %s \n"),inet_ntoa(undesired_offer->offered_address)); + return result; + } + if(requested_servers>0) printf(_(", %s%d of %d requested servers responded"),((requested_responses0)?"only ":"",requested_responses,requested_servers); @@ -1065,16 +1085,16 @@ int call_getopt(int argc, char **argv){ {"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'}, {0,0,0,0} }; + int c=0; while(1){ - int c=0; - - c=getopt_long(argc,argv,"+hVvt:s:r:t:i:m:u",long_options,&option_index); + c=getopt_long(argc,argv,"+hVvxt:s:r:t:i:m:u",long_options,&option_index); if(c==-1||c==EOF||c==1) break; @@ -1120,9 +1140,12 @@ int call_getopt(int argc, char **argv){ break; - case 'u': /* unicast testing */ - unicast=1; - break; + case 'u': /* unicast testing */ + unicast=1; + break; + case 'x': /* exclusive testing aka "rogue DHCP server detection" */ + exclusive=1; + break; case 'V': /* version */ print_revision(progname, NP_VERSION); @@ -1135,7 +1158,6 @@ int call_getopt(int argc, char **argv){ case 'v': /* verbose */ verbose=1; break; - case '?': /* help */ usage5 (); break; @@ -1372,6 +1394,8 @@ void print_help(void){ printf (" %s\n", _("MAC address to use in the DHCP request")); 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 (UT_SUPPORT); return; @@ -1382,8 +1406,8 @@ void print_usage(void){ printf ("%s\n", _("Usage:")); - printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname); + printf (" %s [-v] [-u] [-x] [-s serverip] [-r requestedip] [-t timeout]\n",progname); printf (" [-i interface] [-m mac]\n"); return; -} + } -- cgit v0.10-9-g596f From 8b04f5e11d77a9668fb83bc88d507e291996514e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 1 Oct 2023 15:17:28 +0200 Subject: Update translations diff --git a/po/de.po b/po/de.po index 9ea32df..628ab09 100644 --- a/po/de.po +++ b/po/de.po @@ -4993,9 +4993,6 @@ msgstr "" msgid ", requested address (%s) was %soffered" msgstr "" -msgid "not " -msgstr "" - #, c-format msgid ", max lease time = " msgstr "" @@ -5004,9 +5001,6 @@ msgstr "" msgid "Infinity" msgstr "" -msgid "Got unexpected non-option argument" -msgstr "" - #, c-format msgid "Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n" msgstr "" diff --git a/po/fr.po b/po/fr.po index 28deb94..6200dd3 100644 --- a/po/fr.po +++ b/po/fr.po @@ -5206,9 +5206,6 @@ msgstr ", %s%d de %d serveurs ont répondus" msgid ", requested address (%s) was %soffered" msgstr ", l'adresse demandée (%s) %s été offerte" -msgid "not " -msgstr "n'as pas" - #, c-format msgid ", max lease time = " msgstr ", bail maximum = " @@ -5217,9 +5214,6 @@ msgstr ", bail maximum = " msgid "Infinity" msgstr "Infini" -msgid "Got unexpected non-option argument" -msgstr "" - #, c-format msgid "Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n" msgstr "" diff --git a/po/monitoring-plugins.pot b/po/monitoring-plugins.pot index d019a7e..21acfd8 100644 --- a/po/monitoring-plugins.pot +++ b/po/monitoring-plugins.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: devel@monitoring-plugins.org\n" -"POT-Creation-Date: 2023-09-22 15:36+0200\n" +"POT-Creation-Date: 2023-10-01 15:10+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -4813,6 +4813,14 @@ msgid "Received %d DHCPOFFER(s)" msgstr "" #, c-format +msgid ", Rogue DHCP Server detected! Server %s" +msgstr "" + +#, c-format +msgid " offered %s \n" +msgstr "" + +#, c-format msgid ", %s%d of %d requested servers responded" msgstr "" @@ -4880,6 +4888,11 @@ msgstr "" msgid "Unicast testing: mimic a DHCP relay, requires -s" msgstr "" +msgid "" +"Only requested DHCP server may response (rogue DHCP server detection), " +"requires -s" +msgstr "" + msgid "specify a target" msgstr "" -- cgit v0.10-9-g596f From 2723d48d8474315c454e6d7577430b7839d3e196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 1 Oct 2023 14:46:13 +0200 Subject: New variable is actually a boolean diff --git a/plugins-root/check_dhcp.c b/plugins-root/check_dhcp.c index 8b8bb98..049268f 100644 --- a/plugins-root/check_dhcp.c +++ b/plugins-root/check_dhcp.c @@ -150,7 +150,7 @@ typedef struct dhcp_offer_struct{ uint32_t lease_time; /* lease time in seconds */ uint32_t renewal_time; /* renewal time in seconds */ uint32_t rebinding_time; /* rebinding time in seconds */ - u_int8_t desired; /* is this offer desired (necessary in exclusive mode) */ + bool desired; /* is this offer desired (necessary in exclusive mode) */ struct dhcp_offer_struct *next; }dhcp_offer; @@ -193,8 +193,8 @@ typedef struct requested_server_struct{ #define ETHERNET_HARDWARE_ADDRESS 1 /* used in htype field of dhcp packet */ #define ETHERNET_HARDWARE_ADDRESS_LENGTH 6 /* length of Ethernet hardware addresses */ -uint8_t unicast = 0; /* unicast mode: mimic a DHCP relay */ -u_int8_t exclusive = 0; /* exclusive mode aka "rogue DHCP server detection" */ +bool unicast = 0; /* unicast mode: mimic a DHCP relay */ +bool exclusive = 0; /* exclusive mode aka "rogue DHCP server detection" */ struct in_addr my_ip; /* our address (required for relay) */ struct in_addr dhcp_ip; /* server to query (if in unicast mode) */ unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]=""; @@ -896,7 +896,7 @@ int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){ new_offer->lease_time=dhcp_lease_time; new_offer->renewal_time=dhcp_renewal_time; new_offer->rebinding_time=dhcp_rebinding_time; - new_offer->desired=FALSE; /* exclusive mode: we'll check that in get_results */ + new_offer->desired=false; /* exclusive mode: we'll check that in get_results */ if(verbose){ @@ -977,6 +977,7 @@ int get_results(void){ if(!temp_server->answered){ requested_responses++; temp_server->answered=true; + temp_offer->desired=true; } } } @@ -984,7 +985,7 @@ int get_results(void){ /* exclusive mode: check for undesired offers */ for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next) { - if (temp_offer->desired == FALSE) { + if (!temp_offer->desired) { undesired_offer=temp_offer; /* Checks only for the first undesired offer */ break; /* no further checks needed */ } @@ -1141,10 +1142,10 @@ int call_getopt(int argc, char **argv){ break; case 'u': /* unicast testing */ - unicast=1; + unicast=true; break; case 'x': /* exclusive testing aka "rogue DHCP server detection" */ - exclusive=1; + exclusive=true; break; case 'V': /* version */ -- cgit v0.10-9-g596f From ec9ed2526510b2313095a09185ef598667a86722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:20:24 +0200 Subject: Some code formatting diff --git a/plugins-root/check_dhcp.c b/plugins-root/check_dhcp.c index 049268f..ee794b4 100644 --- a/plugins-root/check_dhcp.c +++ b/plugins-root/check_dhcp.c @@ -1141,12 +1141,12 @@ int call_getopt(int argc, char **argv){ break; - case 'u': /* unicast testing */ - unicast=true; - break; - case 'x': /* exclusive testing aka "rogue DHCP server detection" */ - exclusive=true; - break; + case 'u': /* unicast testing */ + unicast=true; + break; + case 'x': /* exclusive testing aka "rogue DHCP server detection" */ + exclusive=true; + break; case 'V': /* version */ print_revision(progname, NP_VERSION); @@ -1411,4 +1411,4 @@ print_usage(void){ printf (" [-i interface] [-m mac]\n"); return; - } +} -- cgit v0.10-9-g596f From 64d2459029efb34f418bfd445964c27198724483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:20:37 +0200 Subject: Update translations diff --git a/po/de.po b/po/de.po index 628ab09..b10201b 100644 --- a/po/de.po +++ b/po/de.po @@ -9,7 +9,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: devel@monitoring-plugins.org\n" -"POT-Creation-Date: 2023-09-22 15:36+0200\n" +"POT-Creation-Date: 2023-10-01 15:10+0200\n" "PO-Revision-Date: 2004-12-23 17:46+0100\n" "Last-Translator: \n" "Language-Team: Monitoring Plugin Development Team Date: Tue, 3 Oct 2023 12:25:48 +0200 Subject: Make some booleans nicer diff --git a/plugins-root/check_dhcp.c b/plugins-root/check_dhcp.c index ee794b4..5ba9372 100644 --- a/plugins-root/check_dhcp.c +++ b/plugins-root/check_dhcp.c @@ -193,8 +193,8 @@ typedef struct requested_server_struct{ #define ETHERNET_HARDWARE_ADDRESS 1 /* used in htype field of dhcp packet */ #define ETHERNET_HARDWARE_ADDRESS_LENGTH 6 /* length of Ethernet hardware addresses */ -bool unicast = 0; /* unicast mode: mimic a DHCP relay */ -bool exclusive = 0; /* exclusive mode aka "rogue DHCP server detection" */ +bool unicast = false; /* unicast mode: mimic a DHCP relay */ +bool exclusive = false; /* exclusive mode aka "rogue DHCP server detection" */ struct in_addr my_ip; /* our address (required for relay) */ struct in_addr dhcp_ip; /* server to query (if in unicast mode) */ unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]=""; @@ -1094,7 +1094,7 @@ int call_getopt(int argc, char **argv){ }; int c=0; - while(1){ + while(true){ c=getopt_long(argc,argv,"+hVvxt:s:r:t:i:m:u",long_options,&option_index); if(c==-1||c==EOF||c==1) -- cgit v0.10-9-g596f