check_dhcp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /******************************************************************************
  2. *
  3. * CHECK_DHCP.C
  4. *
  5. * Program: DHCP plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 2001-2002 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 recieved\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. recv_result=recvfrom(sock,(char *)buffer,buffer_size,MSG_PEEK,(struct sockaddr *)&source_address,&address_size);
  366. #ifdef DEBUG
  367. printf("recv_result_1: %d\n",recv_result);
  368. #endif
  369. recv_result=recvfrom(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)&source_address,&address_size);
  370. #ifdef DEBUG
  371. printf("recv_result_2: %d\n",recv_result);
  372. #endif
  373. if(recv_result==-1){
  374. #ifdef DEBUG
  375. printf("recvfrom() failed, ");
  376. printf("errno: (%d) -> %s\n",errno,strerror(errno));
  377. #endif
  378. return ERROR;
  379. }
  380. else{
  381. #ifdef DEBUG
  382. printf("receive_dhcp_packet() result: %d\n",recv_result);
  383. printf("receive_dhcp_packet() source: %s\n",inet_ntoa(source_address.sin_addr));
  384. #endif
  385. memcpy(address,&source_address,sizeof(source_address));
  386. return OK;
  387. }
  388. }
  389. return OK;
  390. }
  391. /* creates a socket for DHCP communication */
  392. int create_dhcp_socket(void){
  393. struct sockaddr_in myname;
  394. struct ifreq interface;
  395. int sock;
  396. int flag=1;
  397. /* Set up the address we're going to bind to. */
  398. bzero(&myname,sizeof(myname));
  399. myname.sin_family=AF_INET;
  400. myname.sin_port=htons(DHCP_CLIENT_PORT);
  401. myname.sin_addr.s_addr=INADDR_ANY; /* listen on any address */
  402. bzero(&myname.sin_zero,sizeof(myname.sin_zero));
  403. /* create a socket for DHCP communications */
  404. sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  405. if(sock<0){
  406. printf("Error: Could not create socket!\n");
  407. exit(STATE_UNKNOWN);
  408. }
  409. #ifdef DEBUG
  410. printf("DHCP socket: %d\n",sock);
  411. #endif
  412. /* set the reuse address flag so we don't get errors when restarting */
  413. flag=1;
  414. if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){
  415. printf("Error: Could not set reuse address option on DHCP socket!\n");
  416. exit(STATE_UNKNOWN);
  417. }
  418. /* set the broadcast option - we need this to listen to DHCP broadcast messages */
  419. if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&flag,sizeof flag)<0){
  420. printf("Error: Could not set broadcast option on DHCP socket!\n");
  421. exit(STATE_UNKNOWN);
  422. }
  423. /* bind socket to interface */
  424. strncpy(interface.ifr_ifrn.ifrn_name,network_interface_name,IFNAMSIZ);
  425. if(setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,(char *)&interface,sizeof(interface))<0){
  426. printf("Error: Could not bind socket to interface %s. Check your privileges...\n",network_interface_name);
  427. exit(STATE_UNKNOWN);
  428. }
  429. /* bind the socket */
  430. if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){
  431. printf("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n",DHCP_CLIENT_PORT);
  432. exit(STATE_UNKNOWN);
  433. }
  434. return sock;
  435. }
  436. /* closes DHCP socket */
  437. int close_dhcp_socket(int sock){
  438. close(sock);
  439. return OK;
  440. }
  441. /* adds a requested server address to list in memory */
  442. int add_requested_server(struct in_addr server_address){
  443. requested_server *new_server;
  444. new_server=(requested_server *)malloc(sizeof(requested_server));
  445. if(new_server==NULL)
  446. return ERROR;
  447. new_server->server_address=server_address;
  448. new_server->next=requested_server_list;
  449. requested_server_list=new_server;
  450. requested_servers++;
  451. #ifdef DEBUG
  452. printf("Requested server address: %s\n",inet_ntoa(new_server->server_address));
  453. #endif
  454. return OK;
  455. }
  456. /* adds a DHCP OFFER to list in memory */
  457. int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){
  458. dhcp_offer *new_offer;
  459. int x;
  460. int y;
  461. unsigned option_type;
  462. unsigned option_length;
  463. if(offer_packet==NULL)
  464. return ERROR;
  465. /* process all DHCP options present in the packet */
  466. for(x=4;x<MAX_DHCP_OPTIONS_LENGTH;){
  467. /* end of options (0 is really just a pad, but bail out anyway) */
  468. if((int)offer_packet->options[x]==-1 || (int)offer_packet->options[x]==0)
  469. break;
  470. /* get option type */
  471. option_type=offer_packet->options[x++];
  472. /* get option length */
  473. option_length=offer_packet->options[x++];
  474. #ifdef DEBUG
  475. printf("Option: %d (0x%02X)\n",option_type,option_length);
  476. #endif
  477. /* get option data */
  478. if(option_type==DHCP_OPTION_LEASE_TIME)
  479. dhcp_lease_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  480. if(option_type==DHCP_OPTION_RENEWAL_TIME)
  481. dhcp_renewal_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  482. if(option_type==DHCP_OPTION_REBINDING_TIME)
  483. dhcp_rebinding_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  484. /* skip option data we're ignoring */
  485. else
  486. for(y=0;y<option_length;y++,x++);
  487. }
  488. #ifdef DEBUG
  489. if(dhcp_lease_time==DHCP_INFINITE_TIME)
  490. printf("Lease Time: Infinite\n");
  491. else
  492. printf("Lease Time: %lu seconds\n",(unsigned long)dhcp_lease_time);
  493. if(dhcp_renewal_time==DHCP_INFINITE_TIME)
  494. printf("Renewal Time: Infinite\n");
  495. else
  496. printf("Renewal Time: %lu seconds\n",(unsigned long)dhcp_renewal_time);
  497. if(dhcp_rebinding_time==DHCP_INFINITE_TIME)
  498. printf("Rebinding Time: Infinite\n");
  499. printf("Rebinding Time: %lu seconds\n",(unsigned long)dhcp_rebinding_time);
  500. #endif
  501. new_offer=(dhcp_offer *)malloc(sizeof(dhcp_offer));
  502. if(new_offer==NULL)
  503. return ERROR;
  504. new_offer->server_address=source;
  505. new_offer->offered_address=offer_packet->yiaddr;
  506. new_offer->lease_time=dhcp_lease_time;
  507. new_offer->renewal_time=dhcp_renewal_time;
  508. new_offer->rebinding_time=dhcp_rebinding_time;
  509. #ifdef DEBUG
  510. printf("Added offer from server @ %s",inet_ntoa(new_offer->server_address));
  511. printf(" of IP address %s\n",inet_ntoa(new_offer->offered_address));
  512. #endif
  513. /* add new offer to head of list */
  514. new_offer->next=dhcp_offer_list;
  515. dhcp_offer_list=new_offer;
  516. return OK;
  517. }
  518. /* frees memory allocated to DHCP OFFER list */
  519. int free_dhcp_offer_list(void){
  520. dhcp_offer *this_offer;
  521. dhcp_offer *next_offer;
  522. for(this_offer=dhcp_offer_list;this_offer!=NULL;this_offer=next_offer){
  523. next_offer=this_offer->next;
  524. free(this_offer);
  525. }
  526. return OK;
  527. }
  528. /* frees memory allocated to requested server list */
  529. int free_requested_server_list(void){
  530. requested_server *this_server;
  531. requested_server *next_server;
  532. for(this_server=requested_server_list;this_server!=NULL;this_server=next_server){
  533. next_server=this_server->next;
  534. free(this_server);
  535. }
  536. return OK;
  537. }
  538. /* gets state and plugin output to return */
  539. int get_results(void){
  540. dhcp_offer *temp_offer;
  541. requested_server *temp_server;
  542. int result;
  543. u_int32_t max_lease_time=0;
  544. received_requested_address=FALSE;
  545. /* checks responses from requested servers */
  546. requested_responses=0;
  547. if(requested_servers>0){
  548. for(temp_server=requested_server_list;temp_server!=NULL;temp_server=temp_server->next){
  549. for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
  550. /* get max lease time we were offered */
  551. if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
  552. max_lease_time=temp_offer->lease_time;
  553. /* see if we got the address we requested */
  554. if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
  555. received_requested_address=TRUE;
  556. /* see if the servers we wanted a response from talked to us or not */
  557. if(!memcmp(&temp_offer->server_address,&temp_server->server_address,sizeof(temp_server->server_address))){
  558. #ifdef DEBUG
  559. printf("DHCP Server Match: Offerer=%s",inet_ntoa(temp_offer->server_address));
  560. printf(" Requested=%s\n",inet_ntoa(temp_server->server_address));
  561. #endif
  562. requested_responses++;
  563. }
  564. }
  565. }
  566. }
  567. /* else check and see if we got our requested address from any server */
  568. else{
  569. for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
  570. /* get max lease time we were offered */
  571. if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
  572. max_lease_time=temp_offer->lease_time;
  573. /* see if we got the address we requested */
  574. if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
  575. received_requested_address=TRUE;
  576. }
  577. }
  578. result=STATE_OK;
  579. if(valid_responses==0)
  580. result=STATE_CRITICAL;
  581. else if(requested_servers>0 && requested_responses==0)
  582. result=STATE_CRITICAL;
  583. else if(requested_responses<requested_servers)
  584. result=STATE_WARNING;
  585. else if(request_specific_address==TRUE && received_requested_address==FALSE)
  586. result=STATE_WARNING;
  587. printf("DHCP %s: ",(result==STATE_OK)?"ok":"problem");
  588. /* we didn't receive any DHCPOFFERs */
  589. if(dhcp_offer_list==NULL){
  590. printf("No DHCPOFFERs were received.\n");
  591. return result;
  592. }
  593. printf("Received %d DHCPOFFER(s)",valid_responses);
  594. if(requested_servers>0)
  595. printf(", %s%d of %d requested servers responded",((requested_responses<requested_servers) && requested_responses>0)?"only ":"",requested_responses,requested_servers);
  596. if(request_specific_address==TRUE)
  597. printf(", requested address (%s) was %soffered",inet_ntoa(requested_address),(received_requested_address==TRUE)?"":"not ");
  598. printf(", max lease time = ");
  599. if(max_lease_time==DHCP_INFINITE_TIME)
  600. printf("Infinity");
  601. else
  602. printf("%lu sec",(unsigned long)max_lease_time);
  603. printf(".\n");
  604. return result;
  605. }
  606. /* print usage help */
  607. void print_help(void){
  608. /*print_revision(progname,"$Revision$");*/
  609. printf("Copyright (c) 2001-2002 Ethan Galstad (nagios@nagios.org)\n\n");
  610. printf("This plugin tests the availability of DHCP servers on a network.\n\n");
  611. print_usage();
  612. printf
  613. ("\nOptions:\n"
  614. " -s, --serverip=IPADDRESS\n"
  615. " IP address of DHCP server that we must hear from\n"
  616. " -r, --requestedip=IPADDRESS\n"
  617. " IP address that should be offered by at least one DHCP server\n"
  618. " -t, --timeout=INTEGER\n"
  619. " Seconds to wait for DHCPOFFER before timeout occurs\n"
  620. " -i, --interface=STRING\n"
  621. " Interface to to use for listening (i.e. eth0)\n"
  622. " -v, --verbose\n"
  623. " Print extra information (command-line use only)\n"
  624. " -h, --help\n"
  625. " Print detailed help screen\n"
  626. " -V, --version\n"
  627. " Print version information\n\n"
  628. );
  629. /*support();*/
  630. return;
  631. }
  632. /* prints usage information */
  633. void print_usage(void){
  634. printf("Usage: %s [-s serverip] [-r requestedip] [-t timeout] [-i interface]\n",progname);
  635. printf(" %s --help\n",progname);
  636. printf(" %s --version\n",progname);
  637. return;
  638. }
  639. /* process command-line arguments */
  640. int process_arguments(int argc, char **argv){
  641. int c;
  642. if(argc<1)
  643. return ERROR;
  644. c=0;
  645. while((c+=(call_getopt(argc-c,&argv[c])))<argc){
  646. /*
  647. if(is_option(argv[c]))
  648. continue;
  649. */
  650. }
  651. return validate_arguments();
  652. }
  653. int call_getopt(int argc, char **argv){
  654. int c=0;
  655. int i=0;
  656. struct in_addr ipaddress;
  657. #ifdef HAVE_GETOPT_H
  658. int option_index = 0;
  659. static struct option long_options[] =
  660. {
  661. {"serverip", required_argument,0,'s'},
  662. {"requestedip", required_argument,0,'r'},
  663. {"timeout", required_argument,0,'t'},
  664. {"interface", required_argument,0,'i'},
  665. {"verbose", no_argument, 0,'v'},
  666. {"version", no_argument, 0,'V'},
  667. {"help", no_argument, 0,'h'},
  668. {0,0,0,0}
  669. };
  670. #endif
  671. while(1){
  672. #ifdef HAVE_GETOPT_H
  673. c=getopt_long(argc,argv,"+hVvt:s:r:t:i:",long_options,&option_index);
  674. #else
  675. c=getopt(argc,argv,"+?hVvt:s:r:t:i:");
  676. #endif
  677. i++;
  678. if(c==-1||c==EOF||c==1)
  679. break;
  680. switch(c){
  681. case 'w':
  682. case 'r':
  683. case 't':
  684. case 'i':
  685. i++;
  686. break;
  687. default:
  688. break;
  689. }
  690. switch(c){
  691. case 's': /* DHCP server address */
  692. if(inet_aton(optarg,&ipaddress))
  693. add_requested_server(ipaddress);
  694. /*
  695. else
  696. usage("Invalid server IP address\n");
  697. */
  698. break;
  699. case 'r': /* address we are requested from DHCP servers */
  700. if(inet_aton(optarg,&ipaddress)){
  701. requested_address=ipaddress;
  702. request_specific_address=TRUE;
  703. }
  704. /*
  705. else
  706. usage("Invalid requested IP address\n");
  707. */
  708. break;
  709. case 't': /* timeout */
  710. /*
  711. if(is_intnonneg(optarg))
  712. */
  713. if(atoi(optarg)>0)
  714. dhcpoffer_timeout=atoi(optarg);
  715. /*
  716. else
  717. usage("Time interval must be a nonnegative integer\n");
  718. */
  719. break;
  720. case 'i': /* interface name */
  721. strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1);
  722. network_interface_name[sizeof(network_interface_name)-1]='\x0';
  723. break;
  724. case 'V': /* version */
  725. /*print_revision(progname,"$Revision$");*/
  726. exit(STATE_OK);
  727. case 'h': /* help */
  728. print_help();
  729. exit(STATE_OK);
  730. case '?': /* help */
  731. /*usage("Invalid argument\n");*/
  732. break;
  733. default:
  734. break;
  735. }
  736. }
  737. return i;
  738. }
  739. int validate_arguments(void){
  740. return OK;
  741. }