4
0

check_dhcp.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. /*****************************************************************************
  2. *
  3. * Nagios check_dhcp plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)
  7. * Copyright (c) 2001-2018 Nagios Plugin Development Team
  8. *
  9. * Description:
  10. *
  11. * This file contains the check_dhcp plugin
  12. *
  13. * This plugin tests the availability of DHCP servers on a network.
  14. *
  15. * Unicast mode was originally implemented by Heiti of Boras Kommun with
  16. * general improvements as well as usability fixes and "forward"-porting by
  17. * Andreas Ericsson of OP5 AB.
  18. *
  19. *
  20. * This program is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation, either version 3 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. *
  33. *
  34. *****************************************************************************/
  35. const char *progname = "check_dhcp";
  36. const char *copyright = "2001-2018";
  37. const char *email = "devel@nagios-plugins.org";
  38. #include "common.h"
  39. #include "netutils.h"
  40. #include "utils.h"
  41. #include <arpa/inet.h>
  42. #include <ctype.h>
  43. #include <errno.h>
  44. #include <fcntl.h>
  45. #include <getopt.h>
  46. #include <net/if.h>
  47. #include <netdb.h>
  48. #include <netinet/in.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <sys/ioctl.h>
  53. #include <sys/socket.h>
  54. #include <sys/time.h>
  55. #include <sys/types.h>
  56. #include <unistd.h>
  57. #if HAVE_SYS_SOCKIO_H
  58. #include <sys/sockio.h>
  59. #endif
  60. #if defined(__linux__)
  61. #include <features.h>
  62. #include <linux/if_ether.h>
  63. #elif defined(__bsd__)
  64. #include <net/if_dl.h>
  65. #include <netinet/if_ether.h>
  66. #include <sys/param.h>
  67. #include <sys/sysctl.h>
  68. #elif defined(__sun__) || defined(__solaris__) || defined(__hpux__)
  69. #define INSAP 22
  70. #define OUTSAP 24
  71. #include <ctype.h>
  72. #include <signal.h>
  73. #include <sys/dlpi.h>
  74. #include <sys/poll.h>
  75. #include <sys/stropts.h>
  76. #define bcopy(source, destination, length) memcpy(destination, source, length)
  77. #define AREA_SZ 5000 /* buffer length in bytes */
  78. static u_long ctl_area[AREA_SZ];
  79. static u_long dat_area[AREA_SZ];
  80. static struct strbuf ctl = {AREA_SZ, 0, (char *)ctl_area};
  81. static struct strbuf dat = {AREA_SZ, 0, (char *)dat_area};
  82. #define GOT_CTRL 1
  83. #define GOT_DATA 2
  84. #define GOT_BOTH 3
  85. #define GOT_INTR 4
  86. #define GOT_ERR 128
  87. #define u_int8_t uint8_t
  88. #define u_int16_t uint16_t
  89. #define u_int32_t uint32_t
  90. static int get_msg(int);
  91. static int check_ctrl(int);
  92. static int put_ctrl(int, int, int);
  93. static int put_both(int, int, int, int);
  94. static int dl_open(const char *, int, int *);
  95. static int dl_bind(int, int, u_char *);
  96. long mac_addr_dlpi(const char *, int, u_char *);
  97. #endif
  98. /* Common definitions */
  99. #define OK 0
  100. #define ERROR -1
  101. #define FALSE 0
  102. #define TRUE 1
  103. /* DHCP definitions */
  104. #define MAX_DHCP_CHADDR_LENGTH 16
  105. #define MAX_DHCP_SNAME_LENGTH 64
  106. #define MAX_DHCP_FILE_LENGTH 128
  107. #define MAX_DHCP_OPTIONS_LENGTH 312
  108. typedef struct dhcp_packet_struct {
  109. u_int8_t op; /* packet type */
  110. u_int8_t
  111. htype; /* type of hardware address for this machine (Ethernet, etc) */
  112. u_int8_t hlen; /* length of hardware address (of this machine) */
  113. u_int8_t hops; /* hops */
  114. u_int32_t xid; /* random transaction id number - chosen by this machine */
  115. u_int16_t secs; /* seconds used in timing */
  116. u_int16_t flags; /* flags */
  117. struct in_addr
  118. ciaddr; /* IP address of this machine (if we already have one) */
  119. struct in_addr
  120. yiaddr; /* IP address of this machine (offered by the DHCP server) */
  121. struct in_addr siaddr; /* IP address of next server */
  122. struct in_addr giaddr; /* IP address of DHCP relay */
  123. unsigned char
  124. chaddr[MAX_DHCP_CHADDR_LENGTH]; /* hardware address of this machine */
  125. char sname[MAX_DHCP_SNAME_LENGTH]; /* name of DHCP server */
  126. char file[MAX_DHCP_FILE_LENGTH]; /* boot file name (used for diskless
  127. booting?) */
  128. char options[MAX_DHCP_OPTIONS_LENGTH]; /* options */
  129. } dhcp_packet;
  130. typedef struct dhcp_offer_struct {
  131. struct in_addr
  132. server_address; /* address of DHCP server that sent this offer */
  133. struct in_addr offered_address; /* the IP address that was offered to us */
  134. u_int32_t lease_time; /* lease time in seconds */
  135. u_int32_t renewal_time; /* renewal time in seconds */
  136. u_int32_t rebinding_time; /* rebinding time in seconds */
  137. struct dhcp_offer_struct *next;
  138. } dhcp_offer;
  139. typedef struct requested_server_struct {
  140. struct in_addr server_address;
  141. int answered;
  142. struct requested_server_struct *next;
  143. } requested_server;
  144. #define BOOTREQUEST 1
  145. #define BOOTREPLY 2
  146. #define DHCPDISCOVER 1
  147. #define DHCPOFFER 2
  148. #define DHCPREQUEST 3
  149. #define DHCPDECLINE 4
  150. #define DHCPACK 5
  151. #define DHCPNACK 6
  152. #define DHCPRELEASE 7
  153. #define DHCP_OPTION_MESSAGE_TYPE 53
  154. #define DHCP_OPTION_HOST_NAME 12
  155. #define DHCP_OPTION_BROADCAST_ADDRESS 28
  156. #define DHCP_OPTION_REQUESTED_ADDRESS 50
  157. #define DHCP_OPTION_LEASE_TIME 51
  158. #define DHCP_OPTION_SERVER_IDENTIFIER 54
  159. #define DHCP_OPTION_RENEWAL_TIME 58
  160. #define DHCP_OPTION_REBINDING_TIME 59
  161. #define DHCP_OPTION_END 255
  162. #define DHCP_INFINITE_TIME 0xFFFFFFFF
  163. #define DHCP_BROADCAST_FLAG 32768
  164. #define DHCP_SERVER_PORT 67
  165. #define DHCP_CLIENT_PORT 68
  166. #define ETHERNET_HARDWARE_ADDRESS 1 /* used in htype field of dhcp packet */
  167. #define ETHERNET_HARDWARE_ADDRESS_LENGTH \
  168. 6 /* length of Ethernet hardware addresses */
  169. u_int8_t unicast = 0; /* unicast mode: mimic a DHCP relay */
  170. struct in_addr my_ip; /* our address (required for relay) */
  171. struct in_addr dhcp_ip; /* server to query (if in unicast mode) */
  172. unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH] = "";
  173. unsigned char *user_specified_mac = NULL;
  174. char network_interface_name[IFNAMSIZ] = "eth0";
  175. u_int32_t packet_xid = 0;
  176. u_int32_t dhcp_lease_time = 0;
  177. u_int32_t dhcp_renewal_time = 0;
  178. u_int32_t dhcp_rebinding_time = 0;
  179. int dhcpoffer_timeout = 2;
  180. dhcp_offer *dhcp_offer_list = NULL;
  181. requested_server *requested_server_list = NULL;
  182. int valid_responses = 0; /* number of valid DHCPOFFERs we received */
  183. int requested_servers = 0;
  184. int requested_responses = 0;
  185. char *responses_none;
  186. int request_specific_address = FALSE;
  187. int received_requested_address = FALSE;
  188. int verbose = 0;
  189. struct in_addr requested_address;
  190. int process_arguments(int, char **);
  191. int validate_arguments(void);
  192. void print_usage(void);
  193. void print_help(void);
  194. void resolve_host(const char *in, struct in_addr *out);
  195. unsigned char *mac_aton(const char *);
  196. void print_hardware_address(const unsigned char *);
  197. int get_hardware_address(int, char *);
  198. int get_ip_address(int, char *);
  199. int send_dhcp_discover(int);
  200. int get_dhcp_offer(int);
  201. int get_results(void);
  202. int add_dhcp_offer(struct in_addr, dhcp_packet *);
  203. int free_dhcp_offer_list(void);
  204. int free_requested_server_list(void);
  205. int create_dhcp_socket(void);
  206. int close_dhcp_socket(int);
  207. int send_dhcp_packet(void *, int, int, struct sockaddr_in *);
  208. int receive_dhcp_packet(void *, int, int, int, struct sockaddr_in *);
  209. int main(int argc, char **argv) {
  210. int dhcp_socket;
  211. int result = STATE_UNKNOWN;
  212. setlocale(LC_ALL, "");
  213. bindtextdomain(PACKAGE, LOCALEDIR);
  214. textdomain(PACKAGE);
  215. /* Set default address_family to AF_INET (IPv4) */
  216. /* It will be changed to AF_INET6 later if required */
  217. address_family = AF_INET;
  218. /* Parse extra opts if any */
  219. argv = np_extra_opts(&argc, argv, progname);
  220. if (process_arguments(argc, argv) != OK) {
  221. usage4(_("Could not parse arguments"));
  222. }
  223. /* this plugin almost certainly needs root permissions. */
  224. np_warn_if_not_root();
  225. /* create socket for DHCP communications */
  226. dhcp_socket = create_dhcp_socket();
  227. /* get hardware address of client machine */
  228. if (user_specified_mac != NULL) {
  229. memcpy(client_hardware_address, user_specified_mac, 6);
  230. } else {
  231. get_hardware_address(dhcp_socket, network_interface_name);
  232. }
  233. if (unicast) { /* get IP address of client machine */
  234. get_ip_address(dhcp_socket, network_interface_name);
  235. }
  236. /* send DHCPDISCOVER packet */
  237. send_dhcp_discover(dhcp_socket);
  238. /* wait for a DHCPOFFER packet */
  239. get_dhcp_offer(dhcp_socket);
  240. /* close socket we created */
  241. close_dhcp_socket(dhcp_socket);
  242. /* determine state/plugin output to return */
  243. result = get_results();
  244. /* free allocated memory */
  245. free_dhcp_offer_list();
  246. free_requested_server_list();
  247. return result;
  248. }
  249. /* determines hardware address on client machine */
  250. int get_hardware_address(int sock, char *interface_name) {
  251. #if defined(__linux__)
  252. struct ifreq ifr;
  253. strncpy((char *)&ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
  254. ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
  255. /* try and grab hardware address of requested interface */
  256. if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
  257. printf(_("Error: Could not get hardware address of interface '%s'\n"),
  258. interface_name);
  259. exit(STATE_UNKNOWN);
  260. }
  261. memcpy(&client_hardware_address[0], &ifr.ifr_hwaddr.sa_data, 6);
  262. #elif defined(__bsd__)
  263. int mib[6];
  264. size_t len;
  265. char *buf;
  266. unsigned char *ptr;
  267. struct if_msghdr *ifm;
  268. struct sockaddr_dl *sdl;
  269. mib[0] = CTL_NET;
  270. mib[1] = AF_ROUTE;
  271. mib[2] = 0;
  272. mib[3] = AF_LINK;
  273. mib[4] = NET_RT_IFLIST;
  274. if ((mib[5] = if_nametoindex(interface_name)) == 0) {
  275. printf(_("Error: if_nametoindex error - %s.\n"), strerror(errno));
  276. exit(STATE_UNKNOWN);
  277. }
  278. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
  279. printf(_("Error: Couldn't get hardware address from %s. sysctl 1 error - "
  280. "%s.\n"),
  281. interface_name, strerror(errno));
  282. exit(STATE_UNKNOWN);
  283. }
  284. if ((buf = malloc(len)) == NULL) {
  285. printf(_("Error: Couldn't get hardware address from interface %s. malloc "
  286. "error - %s.\n"),
  287. interface_name, strerror(errno));
  288. exit(4);
  289. }
  290. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  291. printf(_("Error: Couldn't get hardware address from %s. sysctl 2 error - "
  292. "%s.\n"),
  293. interface_name, strerror(errno));
  294. exit(STATE_UNKNOWN);
  295. }
  296. ifm = (struct if_msghdr *)buf;
  297. sdl = (struct sockaddr_dl *)(ifm + 1);
  298. ptr = (unsigned char *)LLADDR(sdl);
  299. memcpy(&client_hardware_address[0], ptr, 6);
  300. #elif defined(__sun__) || defined(__solaris__)
  301. long stat;
  302. char dev[20] = "/dev/";
  303. char *p;
  304. int unit;
  305. /* get last number from interfacename, eg lnc0, e1000g0*/
  306. int i;
  307. p = interface_name + strlen(interface_name) - 1;
  308. for (i = strlen(interface_name) - 1; i > 0; p--) {
  309. if (isalpha(*p)) {
  310. break;
  311. }
  312. }
  313. p++;
  314. if (p != interface_name) {
  315. unit = atoi(p);
  316. strncat(dev, interface_name, 6);
  317. } else {
  318. printf(_("Error: can't find unit number in interface_name (%s) - expecting "
  319. "TypeNumber eg lnc0.\n"),
  320. interface_name);
  321. exit(STATE_UNKNOWN);
  322. }
  323. stat = mac_addr_dlpi(dev, unit, client_hardware_address);
  324. if (stat != 0) {
  325. printf(_("Error: can't read MAC address from DLPI streams interface for "
  326. "device %s unit %d.\n"),
  327. dev, unit);
  328. exit(STATE_UNKNOWN);
  329. }
  330. #elif defined(__hpux__)
  331. long stat;
  332. char dev[20] = "/dev/dlpi";
  333. int unit = 0;
  334. stat = mac_addr_dlpi(dev, unit, client_hardware_address);
  335. if (stat != 0) {
  336. printf(_("Error: can't read MAC address from DLPI streams interface for "
  337. "device %s unit %d.\n"),
  338. dev, unit);
  339. exit(STATE_UNKNOWN);
  340. }
  341. #else
  342. printf(_("Error: can't get MAC address for this architecture. Use the --mac "
  343. "option.\n"));
  344. exit(STATE_UNKNOWN);
  345. #endif
  346. if (verbose) {
  347. print_hardware_address(client_hardware_address);
  348. }
  349. return OK;
  350. }
  351. /* determines IP address of the client interface */
  352. int get_ip_address(int sock, char *interface_name) {
  353. #if defined(SIOCGIFADDR)
  354. struct ifreq ifr;
  355. strncpy((char *)&ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
  356. ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
  357. if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) {
  358. printf(_("Error: Cannot determine IP address of interface %s\n"),
  359. interface_name);
  360. exit(STATE_UNKNOWN);
  361. }
  362. my_ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
  363. #else
  364. printf(_("Error: Cannot get interface IP address on this platform.\n"));
  365. exit(STATE_UNKNOWN);
  366. #endif
  367. if (verbose) {
  368. printf(_("Pretending to be relay client %s\n"), inet_ntoa(my_ip));
  369. }
  370. return OK;
  371. }
  372. /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */
  373. int send_dhcp_discover(int sock) {
  374. dhcp_packet discover_packet;
  375. struct sockaddr_in sockaddr_broadcast;
  376. unsigned short opts;
  377. /* clear the packet data structure */
  378. bzero(&discover_packet, sizeof(discover_packet));
  379. /* boot request flag (backward compatible with BOOTP servers) */
  380. discover_packet.op = BOOTREQUEST;
  381. /* hardware address type */
  382. discover_packet.htype = ETHERNET_HARDWARE_ADDRESS;
  383. /* length of our hardware address */
  384. discover_packet.hlen = ETHERNET_HARDWARE_ADDRESS_LENGTH;
  385. /*
  386. * transaction ID is supposed to be random.
  387. * This allows for proper randomness if the system supports it, and fallback
  388. * to srand & random if not.
  389. */
  390. int randfd = open("/dev/urandom", O_RDONLY);
  391. if (randfd > 2 && read(randfd, (char *)&packet_xid, sizeof(uint32_t)) >= 0) {
  392. /* no-op as we have successfully filled packet_xid */
  393. } else {
  394. /* fallback bad rand */
  395. srand(time(NULL));
  396. packet_xid = random();
  397. }
  398. if (randfd > 2) {
  399. close(randfd);
  400. }
  401. discover_packet.xid = htonl(packet_xid);
  402. /* WHAT THE HECK IS UP WITH THIS?!? IF I DON'T MAKE THIS CALL, ONLY ONE
  403. * SERVER RESPONSE IS PROCESSED!!!! */
  404. /* downright bizzarre... */
  405. ntohl(discover_packet.xid);
  406. /*discover_packet.secs=htons(65535);*/
  407. discover_packet.secs = 0xFF;
  408. /*
  409. * server needs to know if it should broadcast or unicast its response:
  410. * 0x8000L == 32768 == 1 << 15 == broadcast, 0 == unicast
  411. */
  412. discover_packet.flags = unicast ? 0 : htons(DHCP_BROADCAST_FLAG);
  413. /* our hardware address */
  414. memcpy(discover_packet.chaddr, client_hardware_address,
  415. ETHERNET_HARDWARE_ADDRESS_LENGTH);
  416. /* first four bytes of options field is magic cookie (as per RFC 2132) */
  417. discover_packet.options[0] = '\x63';
  418. discover_packet.options[1] = '\x82';
  419. discover_packet.options[2] = '\x53';
  420. discover_packet.options[3] = '\x63';
  421. opts = 4;
  422. /* DHCP message type is embedded in options field */
  423. discover_packet.options[opts++] =
  424. DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */
  425. discover_packet.options[opts++] =
  426. '\x01'; /* DHCP message option length in bytes */
  427. discover_packet.options[opts++] = DHCPDISCOVER;
  428. /* the IP address we're requesting */
  429. if (request_specific_address == TRUE) {
  430. discover_packet.options[opts++] = DHCP_OPTION_REQUESTED_ADDRESS;
  431. discover_packet.options[opts++] = '\x04';
  432. memcpy(&discover_packet.options[opts], &requested_address,
  433. sizeof(requested_address));
  434. opts += sizeof(requested_address);
  435. }
  436. discover_packet.options[opts++] = (char) DHCP_OPTION_END;
  437. /* unicast fields */
  438. if (unicast) {
  439. discover_packet.giaddr.s_addr = my_ip.s_addr;
  440. }
  441. /* see RFC 1542, 4.1.1 */
  442. discover_packet.hops = unicast ? 1 : 0;
  443. /* send the DHCPDISCOVER packet to broadcast address */
  444. sockaddr_broadcast.sin_family = address_family;
  445. sockaddr_broadcast.sin_port = htons(DHCP_SERVER_PORT);
  446. sockaddr_broadcast.sin_addr.s_addr =
  447. unicast ? dhcp_ip.s_addr : INADDR_BROADCAST;
  448. bzero(&sockaddr_broadcast.sin_zero, sizeof(sockaddr_broadcast.sin_zero));
  449. if (verbose) {
  450. printf(_("DHCPDISCOVER to %s port %d\n"),
  451. inet_ntoa(sockaddr_broadcast.sin_addr),
  452. ntohs(sockaddr_broadcast.sin_port));
  453. printf("DHCPDISCOVER XID: %u (0x%X)\n", ntohl(discover_packet.xid),
  454. ntohl(discover_packet.xid));
  455. printf("DHCDISCOVER ciaddr: %s\n", inet_ntoa(discover_packet.ciaddr));
  456. printf("DHCDISCOVER yiaddr: %s\n", inet_ntoa(discover_packet.yiaddr));
  457. printf("DHCDISCOVER siaddr: %s\n", inet_ntoa(discover_packet.siaddr));
  458. printf("DHCDISCOVER giaddr: %s\n", inet_ntoa(discover_packet.giaddr));
  459. }
  460. /* send the DHCPDISCOVER packet out */
  461. send_dhcp_packet(&discover_packet, sizeof(discover_packet), sock,
  462. &sockaddr_broadcast);
  463. if (verbose) {
  464. printf("\n\n");
  465. }
  466. return OK;
  467. }
  468. /* waits for a DHCPOFFER message from one or more DHCP servers */
  469. int get_dhcp_offer(int sock) {
  470. dhcp_packet offer_packet;
  471. struct sockaddr_in source;
  472. struct sockaddr_in via;
  473. int result = OK;
  474. int responses = 0;
  475. int x;
  476. time_t start_time;
  477. time_t current_time;
  478. time(&start_time);
  479. /* receive as many responses as we can */
  480. for (responses = 0, valid_responses = 0;;) {
  481. time(&current_time);
  482. if ((current_time - start_time) >= dhcpoffer_timeout) {
  483. break;
  484. }
  485. if (verbose) {
  486. printf("\n\n");
  487. }
  488. bzero(&source, sizeof(source));
  489. bzero(&via, sizeof(via));
  490. bzero(&offer_packet, sizeof(offer_packet));
  491. result = receive_dhcp_packet(&offer_packet, sizeof(offer_packet), sock,
  492. dhcpoffer_timeout, &source);
  493. if (result != OK) {
  494. if (verbose) {
  495. printf(_("Result=ERROR\n"));
  496. }
  497. continue;
  498. } else {
  499. if (verbose) {
  500. printf(_("Result=OK\n"));
  501. }
  502. responses++;
  503. }
  504. /* The "source" is either a server or a relay. */
  505. /* Save a copy of "source" into "via" even if it's via itself */
  506. memcpy(&via, &source, sizeof(source));
  507. if (verbose) {
  508. printf(_("DHCPOFFER from IP address %s"), inet_ntoa(source.sin_addr));
  509. printf(_(" via %s\n"), inet_ntoa(via.sin_addr));
  510. printf("DHCPOFFER XID: %u (0x%X)\n", ntohl(offer_packet.xid),
  511. ntohl(offer_packet.xid));
  512. }
  513. /* check packet xid to see if its the same as the one we used in the
  514. * discover packet */
  515. if (ntohl(offer_packet.xid) != packet_xid) {
  516. if (verbose) {
  517. printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - "
  518. "ignoring packet\n"),
  519. ntohl(offer_packet.xid), packet_xid);
  520. }
  521. continue;
  522. }
  523. /* check hardware address */
  524. result = OK;
  525. if (verbose) {
  526. printf("DHCPOFFER chaddr: ");
  527. }
  528. for (x = 0; x < ETHERNET_HARDWARE_ADDRESS_LENGTH; x++) {
  529. if (verbose) {
  530. printf("%02X", (unsigned char)offer_packet.chaddr[x]);
  531. }
  532. if (offer_packet.chaddr[x] != client_hardware_address[x]) {
  533. result = ERROR;
  534. }
  535. }
  536. if (verbose) {
  537. printf("\n");
  538. }
  539. if (result == ERROR) {
  540. if (verbose) {
  541. printf(_("DHCPOFFER hardware address did not match our own - ignoring "
  542. "packet\n"));
  543. }
  544. continue;
  545. }
  546. if (verbose) {
  547. printf("DHCPOFFER ciaddr: %s\n", inet_ntoa(offer_packet.ciaddr));
  548. printf("DHCPOFFER yiaddr: %s\n", inet_ntoa(offer_packet.yiaddr));
  549. printf("DHCPOFFER siaddr: %s\n", inet_ntoa(offer_packet.siaddr));
  550. printf("DHCPOFFER giaddr: %s\n", inet_ntoa(offer_packet.giaddr));
  551. }
  552. add_dhcp_offer(source.sin_addr, &offer_packet);
  553. valid_responses++;
  554. }
  555. if (verbose) {
  556. printf(_("Total responses seen on the wire: %d\n"), responses);
  557. printf(_("Valid responses for this machine: %d\n"), valid_responses);
  558. }
  559. return OK;
  560. }
  561. /* sends a DHCP packet */
  562. int send_dhcp_packet(void *buffer, int buffer_size, int sock,
  563. struct sockaddr_in *dest) {
  564. int result;
  565. result = sendto(sock, (char *)buffer, buffer_size, 0, (struct sockaddr *)dest,
  566. sizeof(*dest));
  567. if (verbose) {
  568. printf(_("send_dhcp_packet result: %d\n"), result);
  569. }
  570. if (result < 0) {
  571. return ERROR;
  572. }
  573. return OK;
  574. }
  575. /* receives a DHCP packet */
  576. int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout,
  577. struct sockaddr_in *address) {
  578. struct timeval tv;
  579. fd_set readfds;
  580. fd_set oobfds;
  581. int recv_result;
  582. socklen_t address_size;
  583. struct sockaddr_in source_address;
  584. int nfound;
  585. /* wait for data to arrive (up time timeout) */
  586. tv.tv_sec = timeout;
  587. tv.tv_usec = 0;
  588. FD_ZERO(&readfds);
  589. FD_ZERO(&oobfds);
  590. FD_SET(sock, &readfds);
  591. FD_SET(sock, &oobfds);
  592. nfound = select(sock + 1, &readfds, NULL, &oobfds, &tv);
  593. /* make sure some data has arrived */
  594. if (!FD_ISSET(sock, &readfds)) {
  595. if (verbose) {
  596. printf(_("No (more) data received (nfound: %d)\n"), nfound);
  597. }
  598. return ERROR;
  599. } else {
  600. /* why do we need to peek first? i don't know, its a hack. without it, the
  601. source address of the first packet received was not being interpreted
  602. correctly. sigh... */
  603. bzero(&source_address, sizeof(source_address));
  604. address_size = sizeof(source_address);
  605. recv_result = recvfrom(sock, (char *)buffer, buffer_size, MSG_PEEK,
  606. (struct sockaddr *)&source_address, &address_size);
  607. if (verbose) {
  608. printf("recv_result_1: %d\n", recv_result);
  609. }
  610. recv_result = recvfrom(sock, (char *)buffer, buffer_size, 0,
  611. (struct sockaddr *)&source_address, &address_size);
  612. if (verbose) {
  613. printf("recv_result_2: %d\n", recv_result);
  614. }
  615. if (recv_result == -1) {
  616. if (verbose) {
  617. printf(_("recvfrom() failed, "));
  618. printf("errno: (%d) -> %s\n", errno, strerror(errno));
  619. }
  620. return ERROR;
  621. } else {
  622. if (verbose) {
  623. printf(_("receive_dhcp_packet() result: %d\n"), recv_result);
  624. printf(_("receive_dhcp_packet() source: %s\n"),
  625. inet_ntoa(source_address.sin_addr));
  626. }
  627. memcpy(address, &source_address, sizeof(source_address));
  628. return OK;
  629. }
  630. }
  631. return OK;
  632. }
  633. /* creates a socket for DHCP communication */
  634. int create_dhcp_socket(void) {
  635. struct sockaddr_in myname;
  636. struct ifreq interface;
  637. int sock;
  638. int flag = 1;
  639. /* Set up the address we're going to bind to. */
  640. bzero(&myname, sizeof(myname));
  641. myname.sin_family = address_family;
  642. /* listen to DHCP server port if we're in unicast mode */
  643. myname.sin_port = htons(unicast ? DHCP_SERVER_PORT : DHCP_CLIENT_PORT);
  644. myname.sin_addr.s_addr = unicast ? my_ip.s_addr : INADDR_ANY;
  645. bzero(&myname.sin_zero, sizeof(myname.sin_zero));
  646. /* create a socket for DHCP communications */
  647. sock = socket(address_family, SOCK_DGRAM, IPPROTO_UDP);
  648. if (sock < 0) {
  649. printf(_("Error: Could not create socket!\n"));
  650. exit(STATE_UNKNOWN);
  651. }
  652. if (verbose) {
  653. printf("DHCP socket: %d\n", sock);
  654. }
  655. /* set the reuse address flag so we don't get errors when restarting */
  656. flag = 1;
  657. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof(flag)) <
  658. 0) {
  659. printf(_("Error: Could not set reuse address option on DHCP socket!\n"));
  660. exit(STATE_UNKNOWN);
  661. }
  662. /* set the broadcast option - we need this to listen to DHCP broadcast
  663. * messages */
  664. if (!unicast && setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&flag,
  665. sizeof flag) < 0) {
  666. printf(_("Error: Could not set broadcast option on DHCP socket!\n"));
  667. exit(STATE_UNKNOWN);
  668. }
  669. /* bind socket to interface */
  670. #if defined(__linux__)
  671. strncpy(interface.ifr_ifrn.ifrn_name, network_interface_name, IFNAMSIZ - 1);
  672. interface.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = '\0';
  673. if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (char *)&interface,
  674. sizeof(interface)) < 0) {
  675. printf(_("Error: Could not bind socket to interface %s. Check your "
  676. "privileges...\n"),
  677. network_interface_name);
  678. exit(STATE_UNKNOWN);
  679. }
  680. #else
  681. strncpy(interface.ifr_name, network_interface_name, IFNAMSIZ - 1);
  682. interface.ifr_name[IFNAMSIZ - 1] = '\0';
  683. #endif
  684. /* bind the socket */
  685. if (bind(sock, (struct sockaddr *)&myname, sizeof(myname)) < 0) {
  686. printf(_("Error: Could not bind to DHCP socket (port %d)! Check your "
  687. "privileges...\n"),
  688. DHCP_CLIENT_PORT);
  689. exit(STATE_UNKNOWN);
  690. }
  691. return sock;
  692. }
  693. /* closes DHCP socket */
  694. int close_dhcp_socket(int sock) {
  695. close(sock);
  696. return OK;
  697. }
  698. /* adds a requested server address to list in memory */
  699. int add_requested_server(struct in_addr server_address) {
  700. requested_server *new_server;
  701. new_server = (requested_server *)malloc(sizeof(requested_server));
  702. if (new_server == NULL) {
  703. return ERROR;
  704. }
  705. new_server->server_address = server_address;
  706. new_server->answered = FALSE;
  707. new_server->next = requested_server_list;
  708. requested_server_list = new_server;
  709. requested_servers++;
  710. if (verbose) {
  711. printf(_("Requested server address: %s\n"),
  712. inet_ntoa(new_server->server_address));
  713. }
  714. return OK;
  715. }
  716. /* adds a DHCP OFFER to list in memory */
  717. int add_dhcp_offer(struct in_addr source, dhcp_packet *offer_packet) {
  718. dhcp_offer *new_offer;
  719. int x;
  720. unsigned option_type;
  721. unsigned option_length;
  722. struct in_addr serv_ident = {0};
  723. if (offer_packet == NULL) {
  724. return ERROR;
  725. }
  726. /* process all DHCP options present in the packet */
  727. for (x = 4; x < MAX_DHCP_OPTIONS_LENGTH - 1;) {
  728. if ((int)offer_packet->options[x] == -1) {
  729. break;
  730. }
  731. /* get option type */
  732. option_type = offer_packet->options[x++];
  733. /* get option length */
  734. option_length = offer_packet->options[x++];
  735. if (verbose) {
  736. printf("Option: %d (0x%02X)\n", option_type, option_length);
  737. }
  738. /* get option data */
  739. switch (option_type) {
  740. case DHCP_OPTION_LEASE_TIME:
  741. memcpy(&dhcp_lease_time, &offer_packet->options[x],
  742. sizeof(dhcp_lease_time));
  743. dhcp_lease_time = ntohl(dhcp_lease_time);
  744. break;
  745. case DHCP_OPTION_RENEWAL_TIME:
  746. memcpy(&dhcp_renewal_time, &offer_packet->options[x],
  747. sizeof(dhcp_renewal_time));
  748. dhcp_renewal_time = ntohl(dhcp_renewal_time);
  749. break;
  750. case DHCP_OPTION_REBINDING_TIME:
  751. memcpy(&dhcp_rebinding_time, &offer_packet->options[x],
  752. sizeof(dhcp_rebinding_time));
  753. dhcp_rebinding_time = ntohl(dhcp_rebinding_time);
  754. break;
  755. case DHCP_OPTION_SERVER_IDENTIFIER:
  756. memcpy(&serv_ident.s_addr, &offer_packet->options[x],
  757. sizeof(serv_ident.s_addr));
  758. break;
  759. }
  760. /* skip option data we're ignoring */
  761. if (option_type == 0) { /* "pad" option, see RFC 2132 (3.1) */
  762. x += 1;
  763. } else {
  764. x += option_length;
  765. }
  766. }
  767. if (verbose) {
  768. if (dhcp_lease_time == DHCP_INFINITE_TIME) {
  769. printf(_("Lease Time: Infinite\n"));
  770. } else {
  771. printf(_("Lease Time: %lu seconds\n"), (unsigned long)dhcp_lease_time);
  772. }
  773. if (dhcp_renewal_time == DHCP_INFINITE_TIME) {
  774. printf(_("Renewal Time: Infinite\n"));
  775. } else {
  776. printf(_("Renewal Time: %lu seconds\n"),
  777. (unsigned long)dhcp_renewal_time);
  778. }
  779. if (dhcp_rebinding_time == DHCP_INFINITE_TIME) {
  780. printf(_("Rebinding Time: Infinite\n"));
  781. }
  782. printf(_("Rebinding Time: %lu seconds\n"),
  783. (unsigned long)dhcp_rebinding_time);
  784. }
  785. new_offer = (dhcp_offer *)malloc(sizeof(dhcp_offer));
  786. if (new_offer == NULL) {
  787. return ERROR;
  788. }
  789. /*
  790. * RFC 2131 (2.) says: "DHCP clarifies the interpretation of the
  791. * 'siaddr' field as the address of the server to use in the next step
  792. * of the client's bootstrap process. A DHCP server may return its own
  793. * address in the 'siaddr' field, if the server is prepared to supply
  794. * the next bootstrap service (e.g., delivery of an operating system
  795. * executable image). A DHCP server always returns its own address in
  796. * the 'server identifier' option." 'serv_ident' is the 'server
  797. * identifier' option, 'source' is the IP address we received the
  798. * DHCPOFFER from. If 'serv_ident' isn't available for some reason, we
  799. * use 'source'.
  800. */
  801. new_offer->server_address = serv_ident.s_addr ? serv_ident : source;
  802. new_offer->offered_address = offer_packet->yiaddr;
  803. new_offer->lease_time = dhcp_lease_time;
  804. new_offer->renewal_time = dhcp_renewal_time;
  805. new_offer->rebinding_time = dhcp_rebinding_time;
  806. if (verbose) {
  807. printf(_("Added offer from server @ %s"),
  808. inet_ntoa(new_offer->server_address));
  809. printf(_(" of IP address %s\n"), inet_ntoa(new_offer->offered_address));
  810. }
  811. /* add new offer to head of list */
  812. new_offer->next = dhcp_offer_list;
  813. dhcp_offer_list = new_offer;
  814. return OK;
  815. }
  816. /* frees memory allocated to DHCP OFFER list */
  817. int free_dhcp_offer_list(void) {
  818. dhcp_offer *this_offer;
  819. dhcp_offer *next_offer;
  820. for (this_offer = dhcp_offer_list; this_offer != NULL;
  821. this_offer = next_offer) {
  822. next_offer = this_offer->next;
  823. free(this_offer);
  824. }
  825. return OK;
  826. }
  827. /* frees memory allocated to requested server list */
  828. int free_requested_server_list(void) {
  829. requested_server *this_server;
  830. requested_server *next_server;
  831. for (this_server = requested_server_list; this_server != NULL;
  832. this_server = next_server) {
  833. next_server = this_server->next;
  834. free(this_server);
  835. }
  836. return OK;
  837. }
  838. /* gets state and plugin output to return */
  839. int get_results(void) {
  840. dhcp_offer *temp_offer;
  841. requested_server *temp_server;
  842. int result;
  843. u_int32_t max_lease_time = 0;
  844. received_requested_address = FALSE;
  845. responses_none = strdup("");
  846. /* checks responses from requested servers */
  847. requested_responses = 0;
  848. if (requested_servers > 0) {
  849. for (temp_server = requested_server_list; temp_server != NULL;
  850. temp_server = temp_server->next) {
  851. for (temp_offer = dhcp_offer_list; temp_offer != NULL;
  852. temp_offer = temp_offer->next) {
  853. /* get max lease time we were offered */
  854. if (temp_offer->lease_time > max_lease_time ||
  855. temp_offer->lease_time == DHCP_INFINITE_TIME) {
  856. max_lease_time = temp_offer->lease_time;
  857. }
  858. /* see if we got the address we requested */
  859. if (!memcmp(&requested_address, &temp_offer->offered_address,
  860. sizeof(requested_address))) {
  861. received_requested_address = TRUE;
  862. }
  863. /* see if the servers we wanted a response from talked to us or not */
  864. if (!memcmp(&temp_offer->server_address, &temp_server->server_address,
  865. sizeof(temp_server->server_address))) {
  866. if (verbose) {
  867. printf(_("DHCP Server Match: Offerer=%s"),
  868. inet_ntoa(temp_offer->server_address));
  869. printf(_(" Requested=%s"), inet_ntoa(temp_server->server_address));
  870. if (temp_server->answered) {
  871. printf(_(" (duplicate)"));
  872. }
  873. printf(_("\n"));
  874. }
  875. if (temp_server->answered == FALSE) {
  876. requested_responses++;
  877. temp_server->answered = TRUE;
  878. }
  879. }
  880. }
  881. if (temp_server->answered == FALSE) {
  882. /* Add the no response server to the responses_none string */
  883. xasprintf(&responses_none, "%s %s", responses_none,
  884. inet_ntoa(temp_server->server_address));
  885. if (verbose) {
  886. printf(_("No Response From: %s\n"),
  887. inet_ntoa(temp_server->server_address));
  888. }
  889. }
  890. }
  891. }
  892. /* else check and see if we got our requested address from any server */
  893. else {
  894. for (temp_offer = dhcp_offer_list; temp_offer != NULL;
  895. temp_offer = temp_offer->next) {
  896. /* get max lease time we were offered */
  897. if (temp_offer->lease_time > max_lease_time ||
  898. temp_offer->lease_time == DHCP_INFINITE_TIME) {
  899. max_lease_time = temp_offer->lease_time;
  900. }
  901. /* see if we got the address we requested */
  902. if (!memcmp(&requested_address, &temp_offer->offered_address,
  903. sizeof(requested_address))) {
  904. received_requested_address = TRUE;
  905. }
  906. }
  907. }
  908. result = STATE_OK;
  909. if (valid_responses == 0) {
  910. result = STATE_CRITICAL;
  911. } else if (requested_servers > 0 && requested_responses == 0) {
  912. result = STATE_CRITICAL;
  913. } else if (requested_responses < requested_servers) {
  914. result = STATE_WARNING;
  915. } else if (request_specific_address == TRUE &&
  916. received_requested_address == FALSE) {
  917. result = STATE_WARNING;
  918. }
  919. if (result == 0) {
  920. printf("OK: ");
  921. } else if (result == 1) {
  922. printf("WARNING: ");
  923. } else if (result == 2) {
  924. printf("CRITICAL: ");
  925. } else if (result == 3) {
  926. printf("UNKNOWN: ");
  927. }
  928. /* we didn't receive any DHCPOFFERs */
  929. if (dhcp_offer_list == NULL) {
  930. printf(_("No DHCPOFFERs were received.\n"));
  931. return result;
  932. }
  933. printf(_("Received %d DHCPOFFER(s)"), valid_responses);
  934. if (requested_servers > 0) {
  935. printf(
  936. _(", %s%d of %d requested servers responded"),
  937. ((requested_responses < requested_servers) && requested_responses > 0)
  938. ? "only "
  939. : "",
  940. requested_responses, requested_servers);
  941. }
  942. if (request_specific_address == TRUE) {
  943. printf(_(", requested address (%s) was %soffered"),
  944. inet_ntoa(requested_address),
  945. (received_requested_address == TRUE) ? "" : _("not "));
  946. }
  947. printf(_(", max lease time = "));
  948. if (max_lease_time == DHCP_INFINITE_TIME) {
  949. printf(_("Infinity"));
  950. } else {
  951. printf("%lu sec", (unsigned long)max_lease_time);
  952. }
  953. printf(".\n");
  954. if (requested_servers > 0 && requested_servers != requested_responses) {
  955. printf("No response from:%s\n", responses_none);
  956. }
  957. return result;
  958. }
  959. /* process command-line arguments */
  960. int process_arguments(int argc, char **argv) {
  961. int c = 0;
  962. int option_index = 0;
  963. static struct option long_options[] = {
  964. {"serverip", required_argument, 0, 's'},
  965. {"requestedip", required_argument, 0, 'r'},
  966. {"timeout", required_argument, 0, 't'},
  967. {"interface", required_argument, 0, 'i'},
  968. {"mac", required_argument, 0, 'm'},
  969. {"unicast", no_argument, 0, 'u'},
  970. {"verbose", no_argument, 0, 'v'},
  971. {"version", no_argument, 0, 'V'},
  972. {"help", no_argument, 0, 'h'},
  973. {0, 0, 0, 0}};
  974. if (argc < 1) {
  975. return ERROR;
  976. }
  977. while (1) {
  978. c = getopt_long(argc, argv, "+hVvt:s:r:t:i:m:u", long_options,
  979. &option_index);
  980. if (c == -1 || c == EOF || c == 1) {
  981. break;
  982. }
  983. switch (c) {
  984. case 'w':
  985. case 'r':
  986. case 't':
  987. case 'i':
  988. break;
  989. default:
  990. break;
  991. }
  992. switch (c) {
  993. case 's': /* DHCP server address */
  994. resolve_host(optarg, &dhcp_ip);
  995. add_requested_server(dhcp_ip);
  996. break;
  997. case 'r': /* address we are requested from DHCP servers */
  998. resolve_host(optarg, &requested_address);
  999. request_specific_address = TRUE;
  1000. break;
  1001. case 't': /* timeout */
  1002. /*
  1003. if(is_intnonneg(optarg))
  1004. */
  1005. if (atoi(optarg) > 0) {
  1006. dhcpoffer_timeout = atoi(optarg);
  1007. }
  1008. /*
  1009. else
  1010. usage("Time interval must be a nonnegative integer\n");
  1011. */
  1012. break;
  1013. case 'm': /* MAC address */
  1014. if ((user_specified_mac = mac_aton(optarg)) == NULL) {
  1015. usage("Cannot parse MAC address.\n");
  1016. }
  1017. if (verbose) {
  1018. print_hardware_address(user_specified_mac);
  1019. }
  1020. break;
  1021. case 'i': /* interface name */
  1022. strncpy(network_interface_name, optarg,
  1023. sizeof(network_interface_name) - 1);
  1024. network_interface_name[sizeof(network_interface_name) - 1] = '\x0';
  1025. break;
  1026. case 'u': /* unicast testing */
  1027. unicast = 1;
  1028. break;
  1029. case 'V': /* version */
  1030. print_revision(progname, NP_VERSION);
  1031. exit(STATE_OK);
  1032. case 'h': /* help */
  1033. print_help();
  1034. exit(STATE_OK);
  1035. case 'v': /* verbose */
  1036. verbose = 1;
  1037. break;
  1038. case '?': /* help */
  1039. usage5();
  1040. break;
  1041. default:
  1042. break;
  1043. }
  1044. }
  1045. return validate_arguments();
  1046. }
  1047. int validate_arguments(void) { return OK; }
  1048. #if defined(__sun__) || defined(__solaris__) || defined(__hpux__)
  1049. /* get a message from a stream; return type of message */
  1050. static int get_msg(int fd) {
  1051. int flags = 0;
  1052. int res, ret;
  1053. ctl_area[0] = 0;
  1054. dat_area[0] = 0;
  1055. ret = 0;
  1056. res = getmsg(fd, &ctl, &dat, &flags);
  1057. if (res < 0) {
  1058. if (errno == EINTR) {
  1059. return (GOT_INTR);
  1060. } else {
  1061. printf("%s\n", "get_msg FAILED.");
  1062. return (GOT_ERR);
  1063. }
  1064. }
  1065. if (ctl.len > 0) {
  1066. ret |= GOT_CTRL;
  1067. }
  1068. if (dat.len > 0) {
  1069. ret |= GOT_DATA;
  1070. }
  1071. return (ret);
  1072. }
  1073. /* verify that dl_primitive in ctl_area = prim */
  1074. static int check_ctrl(int prim) {
  1075. dl_error_ack_t *err_ack = (dl_error_ack_t *)ctl_area;
  1076. if (err_ack->dl_primitive != prim) {
  1077. printf(_("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n"),
  1078. strerror(errno));
  1079. exit(STATE_UNKNOWN);
  1080. }
  1081. return 0;
  1082. }
  1083. /* put a control message on a stream */
  1084. static int put_ctrl(int fd, int len, int pri) {
  1085. ctl.len = len;
  1086. if (putmsg(fd, &ctl, 0, pri) < 0) {
  1087. printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): "
  1088. "%s.\n"),
  1089. strerror(errno));
  1090. exit(STATE_UNKNOWN);
  1091. }
  1092. return 0;
  1093. }
  1094. /* put a control + data message on a stream */
  1095. static int put_both(int fd, int clen, int dlen, int pri) {
  1096. ctl.len = clen;
  1097. dat.len = dlen;
  1098. if (putmsg(fd, &ctl, &dat, pri) < 0) {
  1099. printf(
  1100. _("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"),
  1101. strerror(errno));
  1102. exit(STATE_UNKNOWN);
  1103. }
  1104. return 0;
  1105. }
  1106. /* open file descriptor and attach */
  1107. static int dl_open(const char *dev, int unit, int *fd) {
  1108. dl_attach_req_t *attach_req = (dl_attach_req_t *)ctl_area;
  1109. if ((*fd = open(dev, O_RDWR)) == -1) {
  1110. printf(_("Error: DLPI stream API failed to get MAC in "
  1111. "dl_attach_req/open(%s..): %s.\n"),
  1112. dev, strerror(errno));
  1113. exit(STATE_UNKNOWN);
  1114. }
  1115. attach_req->dl_primitive = DL_ATTACH_REQ;
  1116. attach_req->dl_ppa = unit;
  1117. put_ctrl(*fd, sizeof(dl_attach_req_t), 0);
  1118. get_msg(*fd);
  1119. return check_ctrl(DL_OK_ACK);
  1120. }
  1121. /* send DL_BIND_REQ */
  1122. static int dl_bind(int fd, int sap, u_char *addr) {
  1123. dl_bind_req_t *bind_req = (dl_bind_req_t *)ctl_area;
  1124. dl_bind_ack_t *bind_ack = (dl_bind_ack_t *)ctl_area;
  1125. bind_req->dl_primitive = DL_BIND_REQ;
  1126. bind_req->dl_sap = sap;
  1127. bind_req->dl_max_conind = 1;
  1128. bind_req->dl_service_mode = DL_CLDLS;
  1129. bind_req->dl_conn_mgmt = 0;
  1130. bind_req->dl_xidtest_flg = 0;
  1131. put_ctrl(fd, sizeof(dl_bind_req_t), 0);
  1132. get_msg(fd);
  1133. if (GOT_ERR == check_ctrl(DL_BIND_ACK)) {
  1134. printf(_("Error: DLPI stream API failed to get MAC in "
  1135. "dl_bind/check_ctrl(): %s.\n"),
  1136. strerror(errno));
  1137. exit(STATE_UNKNOWN);
  1138. }
  1139. bcopy((u_char *)bind_ack + bind_ack->dl_addr_offset, addr,
  1140. bind_ack->dl_addr_length);
  1141. return 0;
  1142. }
  1143. /***********************************************************************
  1144. * interface:
  1145. * function mac_addr_dlpi - get the mac address of the interface with
  1146. * type dev (eg lnc, hme) and unit (0, 1 ..)
  1147. *
  1148. * parameter: addr: an array of six bytes, has to be allocated by the caller
  1149. *
  1150. * return: 0 if OK, -1 if the address could not be determined
  1151. *
  1152. *
  1153. ***********************************************************************/
  1154. long mac_addr_dlpi(const char *dev, int unit, u_char *addr) {
  1155. int fd;
  1156. u_char mac_addr[25];
  1157. if (GOT_ERR != dl_open(dev, unit, &fd)) {
  1158. if (GOT_ERR != dl_bind(fd, INSAP, mac_addr)) {
  1159. bcopy(mac_addr, addr, 6);
  1160. return 0;
  1161. }
  1162. }
  1163. close(fd);
  1164. return -1;
  1165. }
  1166. #endif
  1167. /* resolve host name or die (TODO: move this to netutils.c!) */
  1168. void resolve_host(const char *in, struct in_addr *out) {
  1169. struct addrinfo hints, *ai;
  1170. memset(&hints, 0, sizeof(hints));
  1171. hints.ai_family = PF_INET;
  1172. if (getaddrinfo(in, NULL, &hints, &ai) != 0) {
  1173. usage_va(_("Invalid hostname/address - %s"), optarg);
  1174. }
  1175. memcpy(out, &((struct sockaddr_in *)ai->ai_addr)->sin_addr, sizeof(*out));
  1176. freeaddrinfo(ai);
  1177. }
  1178. /* parse MAC address string, return 6 bytes (unterminated) or NULL */
  1179. unsigned char *mac_aton(const char *string) {
  1180. static unsigned char result[6];
  1181. char tmp[3];
  1182. unsigned i, j;
  1183. for (i = 0, j = 0; string[i] != '\0' && j < sizeof(result); i++) {
  1184. /* ignore ':' and any other non-hex character */
  1185. if (!isxdigit(string[i]) || !isxdigit(string[i + 1])) {
  1186. continue;
  1187. }
  1188. tmp[0] = string[i];
  1189. tmp[1] = string[i + 1];
  1190. tmp[2] = '\0';
  1191. result[j] = strtol(tmp, (char **)NULL, 16);
  1192. i++;
  1193. j++;
  1194. }
  1195. return (j == 6) ? result : NULL;
  1196. }
  1197. void print_hardware_address(const unsigned char *address) {
  1198. int i;
  1199. printf(_("Hardware address: "));
  1200. for (i = 0; i < 5; i++) {
  1201. printf("%2.2x:", address[i]);
  1202. }
  1203. printf("%2.2x", address[i]);
  1204. putchar('\n');
  1205. }
  1206. /* print usage help */
  1207. void print_help(void) {
  1208. print_revision(progname, NP_VERSION);
  1209. printf("Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)\n");
  1210. printf(COPYRIGHT, copyright, email);
  1211. printf("%s\n",
  1212. _("This plugin tests the availability of DHCP servers on a network."));
  1213. printf("\n\n");
  1214. print_usage();
  1215. printf(UT_HELP_VRSN);
  1216. printf(UT_EXTRA_OPTS);
  1217. printf(UT_VERBOSE);
  1218. printf(" %s\n", "-s, --serverip=IPADDRESS");
  1219. printf(" %s\n", _("IP address of DHCP server that we must hear from"));
  1220. printf(" %s\n", "-r, --requestedip=IPADDRESS");
  1221. printf(" %s\n",
  1222. _("IP address that should be offered by at least one DHCP server"));
  1223. printf(" %s\n", "-t, --timeout=INTEGER");
  1224. printf(" %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs"));
  1225. printf(" %s\n", "-i, --interface=STRING");
  1226. printf(" %s\n", _("Interface to to use for listening (i.e. eth0)"));
  1227. printf(" %s\n", "-m, --mac=STRING");
  1228. printf(" %s\n", _("MAC address to use in the DHCP request"));
  1229. printf(" %s\n", "-u, --unicast");
  1230. printf(" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s"));
  1231. printf(UT_SUPPORT);
  1232. return;
  1233. }
  1234. void print_usage(void) {
  1235. printf("%s\n", _("Usage:"));
  1236. printf(" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",
  1237. progname);
  1238. printf(" [-i interface] [-m mac]\n");
  1239. return;
  1240. }