check_dhcp.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /******************************************************************************
  2. *
  3. * CHECK_DHCP.C
  4. *
  5. * Program: DHCP plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * License Information:
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. *****************************************************************************/
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <sys/time.h>
  32. #include <sys/ioctl.h>
  33. #include <fcntl.h>
  34. #include <features.h>
  35. #include <linux/if_ether.h>
  36. #include <getopt.h>
  37. #include <net/if.h>
  38. #include <sys/socket.h>
  39. #include <sys/types.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <netdb.h>
  43. const char *progname = "check_dhcp";
  44. /*#define DEBUG*/
  45. #define HAVE_GETOPT_H
  46. /**** Common definitions ****/
  47. #define STATE_OK 0
  48. #define STATE_WARNING 1
  49. #define STATE_CRITICAL 2
  50. #define STATE_UNKNOWN -1
  51. #define OK 0
  52. #define ERROR -1
  53. #define FALSE 0
  54. #define TRUE 1
  55. /**** DHCP definitions ****/
  56. #define MAX_DHCP_CHADDR_LENGTH 16
  57. #define MAX_DHCP_SNAME_LENGTH 64
  58. #define MAX_DHCP_FILE_LENGTH 128
  59. #define MAX_DHCP_OPTIONS_LENGTH 312
  60. typedef struct dhcp_packet_struct{
  61. u_int8_t op; /* packet type */
  62. u_int8_t htype; /* type of hardware address for this machine (Ethernet, etc) */
  63. u_int8_t hlen; /* length of hardware address (of this machine) */
  64. u_int8_t hops; /* hops */
  65. u_int32_t xid; /* random transaction id number - chosen by this machine */
  66. u_int16_t secs; /* seconds used in timing */
  67. u_int16_t flags; /* flags */
  68. struct in_addr ciaddr; /* IP address of this machine (if we already have one) */
  69. struct in_addr yiaddr; /* IP address of this machine (offered by the DHCP server) */
  70. struct in_addr siaddr; /* IP address of DHCP server */
  71. struct in_addr giaddr; /* IP address of DHCP relay */
  72. unsigned char chaddr [MAX_DHCP_CHADDR_LENGTH]; /* hardware address of this machine */
  73. char sname [MAX_DHCP_SNAME_LENGTH]; /* name of DHCP server */
  74. char file [MAX_DHCP_FILE_LENGTH]; /* boot file name (used for diskless booting?) */
  75. char options[MAX_DHCP_OPTIONS_LENGTH]; /* options */
  76. }dhcp_packet;
  77. typedef struct dhcp_offer_struct{
  78. struct in_addr server_address; /* address of DHCP server that sent this offer */
  79. struct in_addr offered_address; /* the IP address that was offered to us */
  80. u_int32_t lease_time; /* lease time in seconds */
  81. u_int32_t renewal_time; /* renewal time in seconds */
  82. u_int32_t rebinding_time; /* rebinding time in seconds */
  83. struct dhcp_offer_struct *next;
  84. }dhcp_offer;
  85. typedef struct requested_server_struct{
  86. struct in_addr server_address;
  87. struct requested_server_struct *next;
  88. }requested_server;
  89. #define BOOTREQUEST 1
  90. #define BOOTREPLY 2
  91. #define DHCPDISCOVER 1
  92. #define DHCPOFFER 2
  93. #define DHCPREQUEST 3
  94. #define DHCPDECLINE 4
  95. #define DHCPACK 5
  96. #define DHCPNACK 6
  97. #define DHCPRELEASE 7
  98. #define DHCP_OPTION_MESSAGE_TYPE 53
  99. #define DHCP_OPTION_HOST_NAME 12
  100. #define DHCP_OPTION_BROADCAST_ADDRESS 28
  101. #define DHCP_OPTION_REQUESTED_ADDRESS 50
  102. #define DHCP_OPTION_LEASE_TIME 51
  103. #define DHCP_OPTION_RENEWAL_TIME 58
  104. #define DHCP_OPTION_REBINDING_TIME 59
  105. #define DHCP_INFINITE_TIME 0xFFFFFFFF
  106. #define DHCP_BROADCAST_FLAG 32768
  107. #define DHCP_SERVER_PORT 67
  108. #define DHCP_CLIENT_PORT 68
  109. #define ETHERNET_HARDWARE_ADDRESS 1 /* used in htype field of dhcp packet */
  110. #define ETHERNET_HARDWARE_ADDRESS_LENGTH 6 /* length of Ethernet hardware addresses */
  111. unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]="";
  112. char network_interface_name[8]="eth0";
  113. u_int32_t packet_xid=0;
  114. u_int32_t dhcp_lease_time=0;
  115. u_int32_t dhcp_renewal_time=0;
  116. u_int32_t dhcp_rebinding_time=0;
  117. int dhcpoffer_timeout=2;
  118. dhcp_offer *dhcp_offer_list=NULL;
  119. requested_server *requested_server_list=NULL;
  120. int valid_responses=0; /* number of valid DHCPOFFERs we received */
  121. int requested_servers=0;
  122. int requested_responses=0;
  123. int request_specific_address=FALSE;
  124. int received_requested_address=FALSE;
  125. struct in_addr requested_address;
  126. int process_arguments(int, char **);
  127. int call_getopt(int, char **);
  128. int validate_arguments(void);
  129. void print_usage(void);
  130. void print_help(void);
  131. int get_hardware_address(int,char *);
  132. int send_dhcp_discover(int);
  133. int get_dhcp_offer(int);
  134. int get_results(void);
  135. int add_dhcp_offer(struct in_addr,dhcp_packet *);
  136. int free_dhcp_offer_list(void);
  137. int free_requested_server_list(void);
  138. int create_dhcp_socket(void);
  139. int close_dhcp_socket(int);
  140. int send_dhcp_packet(void *,int,int,struct sockaddr_in *);
  141. int receive_dhcp_packet(void *,int,int,int,struct sockaddr_in *);
  142. int main(int argc, char **argv){
  143. int dhcp_socket;
  144. int result;
  145. if(process_arguments(argc,argv)!=OK){
  146. /*usage("Invalid command arguments supplied\n");*/
  147. printf("Invalid command arguments supplied\n");
  148. exit(STATE_UNKNOWN);
  149. }
  150. /* create socket for DHCP communications */
  151. dhcp_socket=create_dhcp_socket();
  152. /* get hardware address of client machine */
  153. get_hardware_address(dhcp_socket,network_interface_name);
  154. /* send DHCPDISCOVER packet */
  155. send_dhcp_discover(dhcp_socket);
  156. /* wait for a DHCPOFFER packet */
  157. get_dhcp_offer(dhcp_socket);
  158. /* close socket we created */
  159. close_dhcp_socket(dhcp_socket);
  160. /* determine state/plugin output to return */
  161. result=get_results();
  162. /* free allocated memory */
  163. free_dhcp_offer_list();
  164. free_requested_server_list();
  165. return result;
  166. }
  167. /* determines hardware address on client machine */
  168. int get_hardware_address(int sock,char *interface_name){
  169. struct ifreq ifr;
  170. strncpy((char *)&ifr.ifr_name,interface_name,sizeof(ifr.ifr_name));
  171. /* try and grab hardware address of requested interface */
  172. if(ioctl(sock,SIOCGIFHWADDR,&ifr)<0){
  173. printf("Error: Could not get hardware address of interface '%s'\n",interface_name);
  174. exit(STATE_UNKNOWN);
  175. }
  176. memcpy(&client_hardware_address[0],&ifr.ifr_hwaddr.sa_data,6);
  177. #ifdef DEBUG
  178. printf("Hardware address: %02x:%02x:%02x:",client_hardware_address[0],client_hardware_address[1],client_hardware_address[2]);
  179. printf("%02x:",client_hardware_address[3]);
  180. printf("%02x:%02x\n",client_hardware_address[4],client_hardware_address[5]);
  181. printf("\n");
  182. #endif
  183. return OK;
  184. }
  185. /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */
  186. int send_dhcp_discover(int sock){
  187. dhcp_packet discover_packet;
  188. struct sockaddr_in sockaddr_broadcast;
  189. /* clear the packet data structure */
  190. bzero(&discover_packet,sizeof(discover_packet));
  191. /* boot request flag (backward compatible with BOOTP servers) */
  192. discover_packet.op=BOOTREQUEST;
  193. /* hardware address type */
  194. discover_packet.htype=ETHERNET_HARDWARE_ADDRESS;
  195. /* length of our hardware address */
  196. discover_packet.hlen=ETHERNET_HARDWARE_ADDRESS_LENGTH;
  197. discover_packet.hops=0;
  198. /* transaction id is supposed to be random */
  199. srand(time(NULL));
  200. packet_xid=random();
  201. discover_packet.xid=htonl(packet_xid);
  202. /**** WHAT THE HECK IS UP WITH THIS?!? IF I DON'T MAKE THIS CALL, ONLY ONE SERVER RESPONSE IS PROCESSED!!!! ****/
  203. /* downright bizzarre... */
  204. ntohl(discover_packet.xid);
  205. /*discover_packet.secs=htons(65535);*/
  206. discover_packet.secs=0xFF;
  207. /* tell server it should broadcast its response */
  208. discover_packet.flags=htons(DHCP_BROADCAST_FLAG);
  209. /* our hardware address */
  210. memcpy(discover_packet.chaddr,client_hardware_address,ETHERNET_HARDWARE_ADDRESS_LENGTH);
  211. /* first four bytes of options field is magic cookie (as per RFC 2132) */
  212. discover_packet.options[0]='\x63';
  213. discover_packet.options[1]='\x82';
  214. discover_packet.options[2]='\x53';
  215. discover_packet.options[3]='\x63';
  216. /* DHCP message type is embedded in options field */
  217. discover_packet.options[4]=DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */
  218. discover_packet.options[5]='\x01'; /* DHCP message option length in bytes */
  219. discover_packet.options[6]=DHCPDISCOVER;
  220. /* the IP address we're requesting */
  221. if(request_specific_address==TRUE){
  222. discover_packet.options[7]=DHCP_OPTION_REQUESTED_ADDRESS;
  223. discover_packet.options[8]='\x04';
  224. memcpy(&discover_packet.options[9],&requested_address,sizeof(requested_address));
  225. }
  226. /* send the DHCPDISCOVER packet to broadcast address */
  227. sockaddr_broadcast.sin_family=AF_INET;
  228. sockaddr_broadcast.sin_port=htons(DHCP_SERVER_PORT);
  229. sockaddr_broadcast.sin_addr.s_addr=INADDR_BROADCAST;
  230. bzero(&sockaddr_broadcast.sin_zero,sizeof(sockaddr_broadcast.sin_zero));
  231. #ifdef DEBUG
  232. printf("DHCPDISCOVER to %s port %d\n",inet_ntoa(sockaddr_broadcast.sin_addr),ntohs(sockaddr_broadcast.sin_port));
  233. printf("DHCPDISCOVER XID: %lu (0x%X)\n",ntohl(discover_packet.xid),ntohl(discover_packet.xid));
  234. printf("DHCDISCOVER ciaddr: %s\n",inet_ntoa(discover_packet.ciaddr));
  235. printf("DHCDISCOVER yiaddr: %s\n",inet_ntoa(discover_packet.yiaddr));
  236. printf("DHCDISCOVER siaddr: %s\n",inet_ntoa(discover_packet.siaddr));
  237. printf("DHCDISCOVER giaddr: %s\n",inet_ntoa(discover_packet.giaddr));
  238. #endif
  239. /* send the DHCPDISCOVER packet out */
  240. send_dhcp_packet(&discover_packet,sizeof(discover_packet),sock,&sockaddr_broadcast);
  241. #ifdef DEBUG
  242. printf("\n\n");
  243. #endif
  244. return OK;
  245. }
  246. /* waits for a DHCPOFFER message from one or more DHCP servers */
  247. int get_dhcp_offer(int sock){
  248. dhcp_packet offer_packet;
  249. struct sockaddr_in source;
  250. int result=OK;
  251. int timeout=1;
  252. int responses=0;
  253. int x;
  254. time_t start_time;
  255. time_t current_time;
  256. time(&start_time);
  257. /* receive as many responses as we can */
  258. for(responses=0,valid_responses=0;;){
  259. time(&current_time);
  260. if((current_time-start_time)>=dhcpoffer_timeout)
  261. break;
  262. #ifdef DEBUG
  263. printf("\n\n");
  264. #endif
  265. bzero(&source,sizeof(source));
  266. bzero(&offer_packet,sizeof(offer_packet));
  267. result=OK;
  268. result=receive_dhcp_packet(&offer_packet,sizeof(offer_packet),sock,dhcpoffer_timeout,&source);
  269. if(result!=OK){
  270. #ifdef DEBUG
  271. printf("Result=ERROR\n");
  272. #endif
  273. continue;
  274. }
  275. else{
  276. #ifdef DEBUG
  277. printf("Result=OK\n");
  278. #endif
  279. responses++;
  280. }
  281. #ifdef DEBUG
  282. printf("DHCPOFFER from IP address %s\n",inet_ntoa(source.sin_addr));
  283. printf("DHCPOFFER XID: %lu (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid));
  284. #endif
  285. /* check packet xid to see if its the same as the one we used in the discover packet */
  286. if(ntohl(offer_packet.xid)!=packet_xid){
  287. #ifdef DEBUG
  288. printf("DHCPOFFER XID (%lu) did not match DHCPDISCOVER XID (%lu) - ignoring packet\n",ntohl(offer_packet.xid),packet_xid);
  289. #endif
  290. continue;
  291. }
  292. /* check hardware address */
  293. result=OK;
  294. #ifdef DEBUG
  295. printf("DHCPOFFER chaddr: ");
  296. #endif
  297. for(x=0;x<ETHERNET_HARDWARE_ADDRESS_LENGTH;x++){
  298. #ifdef DEBUG
  299. printf("%02X",(unsigned char)offer_packet.chaddr[x]);
  300. #endif
  301. if(offer_packet.chaddr[x]!=client_hardware_address[x]){
  302. result=ERROR;
  303. }
  304. }
  305. #ifdef DEBUG
  306. printf("\n");
  307. #endif
  308. if(result==ERROR){
  309. #ifdef DEBUG
  310. printf("DHCPOFFER hardware address did not match our own - ignoring packet\n");
  311. #endif
  312. continue;
  313. }
  314. #ifdef DEBUG
  315. printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr));
  316. printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr));
  317. printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr));
  318. printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr));
  319. #endif
  320. add_dhcp_offer(source.sin_addr,&offer_packet);
  321. valid_responses++;
  322. }
  323. #ifdef DEBUG
  324. printf("Total responses seen on the wire: %d\n",responses);
  325. printf("Valid responses for this machine: %d\n",valid_responses);
  326. #endif
  327. return OK;
  328. }
  329. /* sends a DHCP packet */
  330. int send_dhcp_packet(void *buffer, int buffer_size, int sock, struct sockaddr_in *dest){
  331. struct sockaddr_in myname;
  332. int result;
  333. result=sendto(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)dest,sizeof(*dest));
  334. #ifdef DEBUG
  335. printf("send_dhcp_packet result: %d\n",result);
  336. #endif
  337. if(result<0)
  338. return ERROR;
  339. return OK;
  340. }
  341. /* receives a DHCP packet */
  342. int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, struct sockaddr_in *address){
  343. struct timeval tv;
  344. fd_set readfds;
  345. int recv_result;
  346. socklen_t address_size;
  347. struct sockaddr_in source_address;
  348. /* wait for data to arrive (up time timeout) */
  349. tv.tv_sec=timeout;
  350. tv.tv_usec=0;
  351. FD_ZERO(&readfds);
  352. FD_SET(sock,&readfds);
  353. select(sock+1,&readfds,NULL,NULL,&tv);
  354. /* make sure some data has arrived */
  355. if(!FD_ISSET(sock,&readfds)){
  356. #ifdef DEBUG
  357. printf("No (more) data received\n");
  358. #endif
  359. return ERROR;
  360. }
  361. else{
  362. /* why do we need to peek first? i don't know, its a hack. without it, the source address of the first packet received was
  363. not being interpreted correctly. sigh... */
  364. bzero(&source_address,sizeof(source_address));
  365. address_size=sizeof(source_address);
  366. recv_result=recvfrom(sock,(char *)buffer,buffer_size,MSG_PEEK,(struct sockaddr *)&source_address,&address_size);
  367. #ifdef DEBUG
  368. printf("recv_result_1: %d\n",recv_result);
  369. #endif
  370. recv_result=recvfrom(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)&source_address,&address_size);
  371. #ifdef DEBUG
  372. printf("recv_result_2: %d\n",recv_result);
  373. #endif
  374. if(recv_result==-1){
  375. #ifdef DEBUG
  376. printf("recvfrom() failed, ");
  377. printf("errno: (%d) -> %s\n",errno,strerror(errno));
  378. #endif
  379. return ERROR;
  380. }
  381. else{
  382. #ifdef DEBUG
  383. printf("receive_dhcp_packet() result: %d\n",recv_result);
  384. printf("receive_dhcp_packet() source: %s\n",inet_ntoa(source_address.sin_addr));
  385. #endif
  386. memcpy(address,&source_address,sizeof(source_address));
  387. return OK;
  388. }
  389. }
  390. return OK;
  391. }
  392. /* creates a socket for DHCP communication */
  393. int create_dhcp_socket(void){
  394. struct sockaddr_in myname;
  395. struct ifreq interface;
  396. int sock;
  397. int flag=1;
  398. /* Set up the address we're going to bind to. */
  399. bzero(&myname,sizeof(myname));
  400. myname.sin_family=AF_INET;
  401. myname.sin_port=htons(DHCP_CLIENT_PORT);
  402. myname.sin_addr.s_addr=INADDR_ANY; /* listen on any address */
  403. bzero(&myname.sin_zero,sizeof(myname.sin_zero));
  404. /* create a socket for DHCP communications */
  405. sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  406. if(sock<0){
  407. printf("Error: Could not create socket!\n");
  408. exit(STATE_UNKNOWN);
  409. }
  410. #ifdef DEBUG
  411. printf("DHCP socket: %d\n",sock);
  412. #endif
  413. /* set the reuse address flag so we don't get errors when restarting */
  414. flag=1;
  415. if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){
  416. printf("Error: Could not set reuse address option on DHCP socket!\n");
  417. exit(STATE_UNKNOWN);
  418. }
  419. /* set the broadcast option - we need this to listen to DHCP broadcast messages */
  420. if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&flag,sizeof flag)<0){
  421. printf("Error: Could not set broadcast option on DHCP socket!\n");
  422. exit(STATE_UNKNOWN);
  423. }
  424. /* bind socket to interface */
  425. strncpy(interface.ifr_ifrn.ifrn_name,network_interface_name,IFNAMSIZ);
  426. if(setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,(char *)&interface,sizeof(interface))<0){
  427. printf("Error: Could not bind socket to interface %s. Check your privileges...\n",network_interface_name);
  428. exit(STATE_UNKNOWN);
  429. }
  430. /* bind the socket */
  431. if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){
  432. printf("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n",DHCP_CLIENT_PORT);
  433. exit(STATE_UNKNOWN);
  434. }
  435. return sock;
  436. }
  437. /* closes DHCP socket */
  438. int close_dhcp_socket(int sock){
  439. close(sock);
  440. return OK;
  441. }
  442. /* adds a requested server address to list in memory */
  443. int add_requested_server(struct in_addr server_address){
  444. requested_server *new_server;
  445. new_server=(requested_server *)malloc(sizeof(requested_server));
  446. if(new_server==NULL)
  447. return ERROR;
  448. new_server->server_address=server_address;
  449. new_server->next=requested_server_list;
  450. requested_server_list=new_server;
  451. requested_servers++;
  452. #ifdef DEBUG
  453. printf("Requested server address: %s\n",inet_ntoa(new_server->server_address));
  454. #endif
  455. return OK;
  456. }
  457. /* adds a DHCP OFFER to list in memory */
  458. int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){
  459. dhcp_offer *new_offer;
  460. int x;
  461. int y;
  462. unsigned option_type;
  463. unsigned option_length;
  464. if(offer_packet==NULL)
  465. return ERROR;
  466. /* process all DHCP options present in the packet */
  467. for(x=4;x<MAX_DHCP_OPTIONS_LENGTH;){
  468. /* end of options (0 is really just a pad, but bail out anyway) */
  469. if((int)offer_packet->options[x]==-1 || (int)offer_packet->options[x]==0)
  470. break;
  471. /* get option type */
  472. option_type=offer_packet->options[x++];
  473. /* get option length */
  474. option_length=offer_packet->options[x++];
  475. #ifdef DEBUG
  476. printf("Option: %d (0x%02X)\n",option_type,option_length);
  477. #endif
  478. /* get option data */
  479. if(option_type==DHCP_OPTION_LEASE_TIME)
  480. dhcp_lease_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  481. if(option_type==DHCP_OPTION_RENEWAL_TIME)
  482. dhcp_renewal_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  483. if(option_type==DHCP_OPTION_REBINDING_TIME)
  484. dhcp_rebinding_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  485. /* skip option data we're ignoring */
  486. else
  487. for(y=0;y<option_length;y++,x++);
  488. }
  489. #ifdef DEBUG
  490. if(dhcp_lease_time==DHCP_INFINITE_TIME)
  491. printf("Lease Time: Infinite\n");
  492. else
  493. printf("Lease Time: %lu seconds\n",(unsigned long)dhcp_lease_time);
  494. if(dhcp_renewal_time==DHCP_INFINITE_TIME)
  495. printf("Renewal Time: Infinite\n");
  496. else
  497. printf("Renewal Time: %lu seconds\n",(unsigned long)dhcp_renewal_time);
  498. if(dhcp_rebinding_time==DHCP_INFINITE_TIME)
  499. printf("Rebinding Time: Infinite\n");
  500. printf("Rebinding Time: %lu seconds\n",(unsigned long)dhcp_rebinding_time);
  501. #endif
  502. new_offer=(dhcp_offer *)malloc(sizeof(dhcp_offer));
  503. if(new_offer==NULL)
  504. return ERROR;
  505. new_offer->server_address=source;
  506. new_offer->offered_address=offer_packet->yiaddr;
  507. new_offer->lease_time=dhcp_lease_time;
  508. new_offer->renewal_time=dhcp_renewal_time;
  509. new_offer->rebinding_time=dhcp_rebinding_time;
  510. #ifdef DEBUG
  511. printf("Added offer from server @ %s",inet_ntoa(new_offer->server_address));
  512. printf(" of IP address %s\n",inet_ntoa(new_offer->offered_address));
  513. #endif
  514. /* add new offer to head of list */
  515. new_offer->next=dhcp_offer_list;
  516. dhcp_offer_list=new_offer;
  517. return OK;
  518. }
  519. /* frees memory allocated to DHCP OFFER list */
  520. int free_dhcp_offer_list(void){
  521. dhcp_offer *this_offer;
  522. dhcp_offer *next_offer;
  523. for(this_offer=dhcp_offer_list;this_offer!=NULL;this_offer=next_offer){
  524. next_offer=this_offer->next;
  525. free(this_offer);
  526. }
  527. return OK;
  528. }
  529. /* frees memory allocated to requested server list */
  530. int free_requested_server_list(void){
  531. requested_server *this_server;
  532. requested_server *next_server;
  533. for(this_server=requested_server_list;this_server!=NULL;this_server=next_server){
  534. next_server=this_server->next;
  535. free(this_server);
  536. }
  537. return OK;
  538. }
  539. /* gets state and plugin output to return */
  540. int get_results(void){
  541. dhcp_offer *temp_offer;
  542. requested_server *temp_server;
  543. int result;
  544. u_int32_t max_lease_time=0;
  545. received_requested_address=FALSE;
  546. /* checks responses from requested servers */
  547. requested_responses=0;
  548. if(requested_servers>0){
  549. for(temp_server=requested_server_list;temp_server!=NULL;temp_server=temp_server->next){
  550. for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
  551. /* get max lease time we were offered */
  552. if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
  553. max_lease_time=temp_offer->lease_time;
  554. /* see if we got the address we requested */
  555. if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
  556. received_requested_address=TRUE;
  557. /* see if the servers we wanted a response from talked to us or not */
  558. if(!memcmp(&temp_offer->server_address,&temp_server->server_address,sizeof(temp_server->server_address))){
  559. #ifdef DEBUG
  560. printf("DHCP Server Match: Offerer=%s",inet_ntoa(temp_offer->server_address));
  561. printf(" Requested=%s\n",inet_ntoa(temp_server->server_address));
  562. #endif
  563. requested_responses++;
  564. }
  565. }
  566. }
  567. }
  568. /* else check and see if we got our requested address from any server */
  569. else{
  570. for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
  571. /* get max lease time we were offered */
  572. if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
  573. max_lease_time=temp_offer->lease_time;
  574. /* see if we got the address we requested */
  575. if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
  576. received_requested_address=TRUE;
  577. }
  578. }
  579. result=STATE_OK;
  580. if(valid_responses==0)
  581. result=STATE_CRITICAL;
  582. else if(requested_servers>0 && requested_responses==0)
  583. result=STATE_CRITICAL;
  584. else if(requested_responses<requested_servers)
  585. result=STATE_WARNING;
  586. else if(request_specific_address==TRUE && received_requested_address==FALSE)
  587. result=STATE_WARNING;
  588. printf("DHCP %s: ",(result==STATE_OK)?"ok":"problem");
  589. /* we didn't receive any DHCPOFFERs */
  590. if(dhcp_offer_list==NULL){
  591. printf("No DHCPOFFERs were received.\n");
  592. return result;
  593. }
  594. printf("Received %d DHCPOFFER(s)",valid_responses);
  595. if(requested_servers>0)
  596. printf(", %s%d of %d requested servers responded",((requested_responses<requested_servers) && requested_responses>0)?"only ":"",requested_responses,requested_servers);
  597. if(request_specific_address==TRUE)
  598. printf(", requested address (%s) was %soffered",inet_ntoa(requested_address),(received_requested_address==TRUE)?"":"not ");
  599. printf(", max lease time = ");
  600. if(max_lease_time==DHCP_INFINITE_TIME)
  601. printf("Infinity");
  602. else
  603. printf("%lu sec",(unsigned long)max_lease_time);
  604. printf(".\n");
  605. return result;
  606. }
  607. /* print usage help */
  608. void print_help(void){
  609. /*print_revision(progname,"$Revision$");*/
  610. printf("Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)\n\n");
  611. printf("This plugin tests the availability of DHCP servers on a network.\n\n");
  612. print_usage();
  613. printf
  614. ("\nOptions:\n"
  615. " -s, --serverip=IPADDRESS\n"
  616. " IP address of DHCP server that we must hear from\n"
  617. " -r, --requestedip=IPADDRESS\n"
  618. " IP address that should be offered by at least one DHCP server\n"
  619. " -t, --timeout=INTEGER\n"
  620. " Seconds to wait for DHCPOFFER before timeout occurs\n"
  621. " -i, --interface=STRING\n"
  622. " Interface to to use for listening (i.e. eth0)\n"
  623. " -v, --verbose\n"
  624. " Print extra information (command-line use only)\n"
  625. " -h, --help\n"
  626. " Print detailed help screen\n"
  627. " -V, --version\n"
  628. " Print version information\n\n"
  629. );
  630. /*support();*/
  631. return;
  632. }
  633. /* prints usage information */
  634. void print_usage(void){
  635. printf("Usage: %s [-s serverip] [-r requestedip] [-t timeout] [-i interface]\n",progname);
  636. printf(" %s --help\n",progname);
  637. printf(" %s --version\n",progname);
  638. return;
  639. }
  640. /* process command-line arguments */
  641. int process_arguments(int argc, char **argv){
  642. int c;
  643. if(argc<1)
  644. return ERROR;
  645. c=0;
  646. while((c+=(call_getopt(argc-c,&argv[c])))<argc){
  647. /*
  648. if(is_option(argv[c]))
  649. continue;
  650. */
  651. }
  652. return validate_arguments();
  653. }
  654. int call_getopt(int argc, char **argv){
  655. int c=0;
  656. int i=0;
  657. struct in_addr ipaddress;
  658. #ifdef HAVE_GETOPT_H
  659. int option_index = 0;
  660. static struct option long_options[] =
  661. {
  662. {"serverip", required_argument,0,'s'},
  663. {"requestedip", required_argument,0,'r'},
  664. {"timeout", required_argument,0,'t'},
  665. {"interface", required_argument,0,'i'},
  666. {"verbose", no_argument, 0,'v'},
  667. {"version", no_argument, 0,'V'},
  668. {"help", no_argument, 0,'h'},
  669. {0,0,0,0}
  670. };
  671. #endif
  672. while(1){
  673. #ifdef HAVE_GETOPT_H
  674. c=getopt_long(argc,argv,"+hVvt:s:r:t:i:",long_options,&option_index);
  675. #else
  676. c=getopt(argc,argv,"+?hVvt:s:r:t:i:");
  677. #endif
  678. i++;
  679. if(c==-1||c==EOF||c==1)
  680. break;
  681. switch(c){
  682. case 'w':
  683. case 'r':
  684. case 't':
  685. case 'i':
  686. i++;
  687. break;
  688. default:
  689. break;
  690. }
  691. switch(c){
  692. case 's': /* DHCP server address */
  693. if(inet_aton(optarg,&ipaddress))
  694. add_requested_server(ipaddress);
  695. /*
  696. else
  697. usage("Invalid server IP address\n");
  698. */
  699. break;
  700. case 'r': /* address we are requested from DHCP servers */
  701. if(inet_aton(optarg,&ipaddress)){
  702. requested_address=ipaddress;
  703. request_specific_address=TRUE;
  704. }
  705. /*
  706. else
  707. usage("Invalid requested IP address\n");
  708. */
  709. break;
  710. case 't': /* timeout */
  711. /*
  712. if(is_intnonneg(optarg))
  713. */
  714. if(atoi(optarg)>0)
  715. dhcpoffer_timeout=atoi(optarg);
  716. /*
  717. else
  718. usage("Time interval must be a nonnegative integer\n");
  719. */
  720. break;
  721. case 'i': /* interface name */
  722. strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1);
  723. network_interface_name[sizeof(network_interface_name)-1]='\x0';
  724. break;
  725. case 'V': /* version */
  726. /*print_revision(progname,"$Revision$");*/
  727. exit(STATE_OK);
  728. case 'h': /* help */
  729. print_help();
  730. exit(STATE_OK);
  731. case '?': /* help */
  732. /*usage("Invalid argument\n");*/
  733. break;
  734. default:
  735. break;
  736. }
  737. }
  738. return i;
  739. }
  740. int validate_arguments(void){
  741. return OK;
  742. }