check_dhcp.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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 "common.h"
  27. #include "netutils.h"
  28. #include "utils.h"
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <unistd.h>
  34. #include <sys/time.h>
  35. #include <sys/ioctl.h>
  36. #include <fcntl.h>
  37. #include <getopt.h>
  38. #include <sys/socket.h>
  39. #include <sys/types.h>
  40. #include <netdb.h>
  41. #include <netinet/in.h>
  42. #include <net/if.h>
  43. #include <arpa/inet.h>
  44. #if defined( __linux__ )
  45. #include <linux/if_ether.h>
  46. #include <features.h>
  47. #elif defined (__bsd__)
  48. #include <netinet/if_ether.h>
  49. #include <sys/sysctl.h>
  50. #include <net/if_dl.h>
  51. #elif defined(__sun__) || defined(__solaris__) || defined(__hpux__)
  52. #define INSAP 22
  53. #define OUTSAP 24
  54. #include <signal.h>
  55. #include <ctype.h>
  56. #include <sys/stropts.h>
  57. #include <sys/poll.h>
  58. #include <sys/dlpi.h>
  59. #define bcopy(source, destination, length) memcpy(destination, source, length)
  60. #define AREA_SZ 5000 /* buffer length in bytes */
  61. static u_long ctl_area[AREA_SZ];
  62. static u_long dat_area[AREA_SZ];
  63. static struct strbuf ctl = {AREA_SZ, 0, (char *)ctl_area};
  64. static struct strbuf dat = {AREA_SZ, 0, (char *)dat_area};
  65. #define GOT_CTRL 1
  66. #define GOT_DATA 2
  67. #define GOT_BOTH 3
  68. #define GOT_INTR 4
  69. #define GOT_ERR 128
  70. #define u_int8_t uint8_t
  71. #define u_int16_t uint16_t
  72. #define u_int32_t uint32_t
  73. static int get_msg(int);
  74. static int check_ctrl(int);
  75. static int put_ctrl(int, int, int);
  76. static int put_both(int, int, int, int);
  77. static int dl_open(const char *, int, int *);
  78. static int dl_bind(int, int, u_char *);
  79. long mac_addr_dlpi( const char *, int, u_char *);
  80. #endif
  81. const char *progname = "check_dhcp";
  82. #define HAVE_GETOPT_H
  83. /**** Common definitions ****/
  84. #define STATE_OK 0
  85. #define STATE_WARNING 1
  86. #define STATE_CRITICAL 2
  87. #define STATE_UNKNOWN -1
  88. #define OK 0
  89. #define ERROR -1
  90. #define FALSE 0
  91. #define TRUE 1
  92. /**** DHCP definitions ****/
  93. #define MAX_DHCP_CHADDR_LENGTH 16
  94. #define MAX_DHCP_SNAME_LENGTH 64
  95. #define MAX_DHCP_FILE_LENGTH 128
  96. #define MAX_DHCP_OPTIONS_LENGTH 312
  97. typedef struct dhcp_packet_struct{
  98. u_int8_t op; /* packet type */
  99. u_int8_t htype; /* type of hardware address for this machine (Ethernet, etc) */
  100. u_int8_t hlen; /* length of hardware address (of this machine) */
  101. u_int8_t hops; /* hops */
  102. u_int32_t xid; /* random transaction id number - chosen by this machine */
  103. u_int16_t secs; /* seconds used in timing */
  104. u_int16_t flags; /* flags */
  105. struct in_addr ciaddr; /* IP address of this machine (if we already have one) */
  106. struct in_addr yiaddr; /* IP address of this machine (offered by the DHCP server) */
  107. struct in_addr siaddr; /* IP address of DHCP server */
  108. struct in_addr giaddr; /* IP address of DHCP relay */
  109. unsigned char chaddr [MAX_DHCP_CHADDR_LENGTH]; /* hardware address of this machine */
  110. char sname [MAX_DHCP_SNAME_LENGTH]; /* name of DHCP server */
  111. char file [MAX_DHCP_FILE_LENGTH]; /* boot file name (used for diskless booting?) */
  112. char options[MAX_DHCP_OPTIONS_LENGTH]; /* options */
  113. }dhcp_packet;
  114. typedef struct dhcp_offer_struct{
  115. struct in_addr server_address; /* address of DHCP server that sent this offer */
  116. struct in_addr offered_address; /* the IP address that was offered to us */
  117. u_int32_t lease_time; /* lease time in seconds */
  118. u_int32_t renewal_time; /* renewal time in seconds */
  119. u_int32_t rebinding_time; /* rebinding time in seconds */
  120. struct dhcp_offer_struct *next;
  121. }dhcp_offer;
  122. typedef struct requested_server_struct{
  123. struct in_addr server_address;
  124. struct requested_server_struct *next;
  125. }requested_server;
  126. #define BOOTREQUEST 1
  127. #define BOOTREPLY 2
  128. #define DHCPDISCOVER 1
  129. #define DHCPOFFER 2
  130. #define DHCPREQUEST 3
  131. #define DHCPDECLINE 4
  132. #define DHCPACK 5
  133. #define DHCPNACK 6
  134. #define DHCPRELEASE 7
  135. #define DHCP_OPTION_MESSAGE_TYPE 53
  136. #define DHCP_OPTION_HOST_NAME 12
  137. #define DHCP_OPTION_BROADCAST_ADDRESS 28
  138. #define DHCP_OPTION_REQUESTED_ADDRESS 50
  139. #define DHCP_OPTION_LEASE_TIME 51
  140. #define DHCP_OPTION_RENEWAL_TIME 58
  141. #define DHCP_OPTION_REBINDING_TIME 59
  142. #define DHCP_INFINITE_TIME 0xFFFFFFFF
  143. #define DHCP_BROADCAST_FLAG 32768
  144. #define DHCP_SERVER_PORT 67
  145. #define DHCP_CLIENT_PORT 68
  146. #define ETHERNET_HARDWARE_ADDRESS 1 /* used in htype field of dhcp packet */
  147. #define ETHERNET_HARDWARE_ADDRESS_LENGTH 6 /* length of Ethernet hardware addresses */
  148. unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]="";
  149. char network_interface_name[8]="eth0";
  150. u_int32_t packet_xid=0;
  151. u_int32_t dhcp_lease_time=0;
  152. u_int32_t dhcp_renewal_time=0;
  153. u_int32_t dhcp_rebinding_time=0;
  154. int dhcpoffer_timeout=2;
  155. dhcp_offer *dhcp_offer_list=NULL;
  156. requested_server *requested_server_list=NULL;
  157. int valid_responses=0; /* number of valid DHCPOFFERs we received */
  158. int requested_servers=0;
  159. int requested_responses=0;
  160. int request_specific_address=FALSE;
  161. int received_requested_address=FALSE;
  162. int verbose=0;
  163. struct in_addr requested_address;
  164. int process_arguments(int, char **);
  165. int call_getopt(int, char **);
  166. int validate_arguments(void);
  167. void print_usage(void);
  168. void print_help(void);
  169. int get_hardware_address(int,char *);
  170. int send_dhcp_discover(int);
  171. int get_dhcp_offer(int);
  172. int get_results(void);
  173. int add_dhcp_offer(struct in_addr,dhcp_packet *);
  174. int free_dhcp_offer_list(void);
  175. int free_requested_server_list(void);
  176. int create_dhcp_socket(void);
  177. int close_dhcp_socket(int);
  178. int send_dhcp_packet(void *,int,int,struct sockaddr_in *);
  179. int receive_dhcp_packet(void *,int,int,int,struct sockaddr_in *);
  180. int main(int argc, char **argv){
  181. int dhcp_socket;
  182. int result;
  183. if(process_arguments(argc,argv)!=OK){
  184. /*usage("Invalid command arguments supplied\n");*/
  185. printf("Invalid command arguments supplied\n");
  186. exit(STATE_UNKNOWN);
  187. }
  188. /* create socket for DHCP communications */
  189. dhcp_socket=create_dhcp_socket();
  190. /* get hardware address of client machine */
  191. get_hardware_address(dhcp_socket,network_interface_name);
  192. /* send DHCPDISCOVER packet */
  193. send_dhcp_discover(dhcp_socket);
  194. /* wait for a DHCPOFFER packet */
  195. get_dhcp_offer(dhcp_socket);
  196. /* close socket we created */
  197. close_dhcp_socket(dhcp_socket);
  198. /* determine state/plugin output to return */
  199. result=get_results();
  200. /* free allocated memory */
  201. free_dhcp_offer_list();
  202. free_requested_server_list();
  203. return result;
  204. }
  205. /* determines hardware address on client machine */
  206. int get_hardware_address(int sock,char *interface_name){
  207. int i;
  208. #if defined(__linux__)
  209. struct ifreq ifr;
  210. strncpy((char *)&ifr.ifr_name,interface_name,sizeof(ifr.ifr_name));
  211. /* try and grab hardware address of requested interface */
  212. if(ioctl(sock,SIOCGIFHWADDR,&ifr)<0){
  213. printf("Error: Could not get hardware address of interface '%s'\n",interface_name);
  214. exit(STATE_UNKNOWN);
  215. }
  216. memcpy(&client_hardware_address[0],&ifr.ifr_hwaddr.sa_data,6);
  217. #elif defined(__bsd__)
  218. /* Code from getmac.c posted at http://lists.freebsd.org/pipermail/freebsd-hackers/2004-June/007415.html
  219. * by Alecs King based on Unix Network programming Ch 17
  220. */
  221. int mib[6], len;
  222. char *buf;
  223. unsigned char *ptr;
  224. struct if_msghdr *ifm;
  225. struct sockaddr_dl *sdl;
  226. mib[0] = CTL_NET;
  227. mib[1] = AF_ROUTE;
  228. mib[2] = 0;
  229. mib[3] = AF_LINK;
  230. mib[4] = NET_RT_IFLIST;
  231. if ((mib[5] = if_nametoindex(interface_name)) == 0) {
  232. printf("Error: if_nametoindex error - %s.\n", strerror(errno));
  233. exit(STATE_UNKNOWN);
  234. }
  235. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
  236. printf("Error: Couldn't get hardware address from %s. sysctl 1 error - %s.\n", interface_name, strerror(errno));
  237. exit(STATE_UNKNOWN);
  238. }
  239. if ((buf = malloc(len)) == NULL) {
  240. printf("Error: Couldn't get hardware address from interface %s. malloc error - %s.\n", interface_name, strerror(errno));
  241. exit(4);
  242. }
  243. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  244. printf("Error: Couldn't get hardware address from %s. sysctl 2 error - %s.\n", interface_name, strerror(errno));
  245. exit(STATE_UNKNOWN);
  246. }
  247. ifm = (struct if_msghdr *)buf;
  248. sdl = (struct sockaddr_dl *)(ifm + 1);
  249. ptr = (unsigned char *)LLADDR(sdl);
  250. memcpy(&client_hardware_address[0], ptr, 6) ;
  251. #elif defined(__sun__) || defined(__solaris__)
  252. /*
  253. * Lifted from
  254. *
  255. * mac_addr_dlpi.c
  256. *
  257. * Copyright @2000, 2003 Martin Kompf, martin@kompf.de
  258. *
  259. * Return the MAC (ie, ethernet hardware) address by using the dlpi api.
  260. *
  261. */
  262. long stat;
  263. char dev[20] = "/dev/";
  264. char *p;
  265. int unit;
  266. for (p = interface_name; *p && isalpha(*p); p++)
  267. /* no-op */ ;
  268. if ( p != '\0' ) {
  269. unit = atoi(p) ;
  270. *p = '\0' ;
  271. strncat(dev, interface_name, 6) ;
  272. } else {
  273. printf("Error: can't find unit number in interface_name (%s) - expecting TypeNumber eg lnc0.\n", interface_name);
  274. exit(STATE_UNKNOWN);
  275. }
  276. stat = mac_addr_dlpi(dev, unit, client_hardware_address);
  277. if (stat != 0) {
  278. printf("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n", dev, unit);
  279. exit(STATE_UNKNOWN);
  280. }
  281. #elif defined(__hpux__)
  282. /* Martin Kompf again
  283. *
  284. * Nagios plugins thank you sincerely
  285. */
  286. long stat;
  287. char dev[20] = "/dev/dlpi" ;
  288. int unit = 0;
  289. stat = mac_addr_dlpi(dev, unit, client_hardware_address);
  290. if (stat != 0) {
  291. printf("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n", dev, unit);
  292. exit(STATE_UNKNOWN);
  293. }
  294. #else
  295. printf("Error: can't get MAC address for this architecture.\n");
  296. exit(STATE_UNKNOWN);
  297. #endif
  298. if (verbose) {
  299. printf( "Hardware address: ");
  300. for (i=0; i<6; ++i)
  301. printf("%2.2x", client_hardware_address[i]);
  302. printf( "\n");
  303. }
  304. return OK;
  305. }
  306. /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */
  307. int send_dhcp_discover(int sock){
  308. dhcp_packet discover_packet;
  309. struct sockaddr_in sockaddr_broadcast;
  310. /* clear the packet data structure */
  311. bzero(&discover_packet,sizeof(discover_packet));
  312. /* boot request flag (backward compatible with BOOTP servers) */
  313. discover_packet.op=BOOTREQUEST;
  314. /* hardware address type */
  315. discover_packet.htype=ETHERNET_HARDWARE_ADDRESS;
  316. /* length of our hardware address */
  317. discover_packet.hlen=ETHERNET_HARDWARE_ADDRESS_LENGTH;
  318. discover_packet.hops=0;
  319. /* transaction id is supposed to be random */
  320. srand(time(NULL));
  321. packet_xid=random();
  322. discover_packet.xid=htonl(packet_xid);
  323. /**** WHAT THE HECK IS UP WITH THIS?!? IF I DON'T MAKE THIS CALL, ONLY ONE SERVER RESPONSE IS PROCESSED!!!! ****/
  324. /* downright bizzarre... */
  325. ntohl(discover_packet.xid);
  326. /*discover_packet.secs=htons(65535);*/
  327. discover_packet.secs=0xFF;
  328. /* tell server it should broadcast its response */
  329. discover_packet.flags=htons(DHCP_BROADCAST_FLAG);
  330. /* our hardware address */
  331. memcpy(discover_packet.chaddr,client_hardware_address,ETHERNET_HARDWARE_ADDRESS_LENGTH);
  332. /* first four bytes of options field is magic cookie (as per RFC 2132) */
  333. discover_packet.options[0]='\x63';
  334. discover_packet.options[1]='\x82';
  335. discover_packet.options[2]='\x53';
  336. discover_packet.options[3]='\x63';
  337. /* DHCP message type is embedded in options field */
  338. discover_packet.options[4]=DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */
  339. discover_packet.options[5]='\x01'; /* DHCP message option length in bytes */
  340. discover_packet.options[6]=DHCPDISCOVER;
  341. /* the IP address we're requesting */
  342. if(request_specific_address==TRUE){
  343. discover_packet.options[7]=DHCP_OPTION_REQUESTED_ADDRESS;
  344. discover_packet.options[8]='\x04';
  345. memcpy(&discover_packet.options[9],&requested_address,sizeof(requested_address));
  346. }
  347. /* send the DHCPDISCOVER packet to broadcast address */
  348. sockaddr_broadcast.sin_family=AF_INET;
  349. sockaddr_broadcast.sin_port=htons(DHCP_SERVER_PORT);
  350. sockaddr_broadcast.sin_addr.s_addr=INADDR_BROADCAST;
  351. bzero(&sockaddr_broadcast.sin_zero,sizeof(sockaddr_broadcast.sin_zero));
  352. if (verbose) {
  353. printf("DHCPDISCOVER to %s port %d\n",inet_ntoa(sockaddr_broadcast.sin_addr),ntohs(sockaddr_broadcast.sin_port));
  354. printf("DHCPDISCOVER XID: %lu (0x%X)\n",ntohl(discover_packet.xid),ntohl(discover_packet.xid));
  355. printf("DHCDISCOVER ciaddr: %s\n",inet_ntoa(discover_packet.ciaddr));
  356. printf("DHCDISCOVER yiaddr: %s\n",inet_ntoa(discover_packet.yiaddr));
  357. printf("DHCDISCOVER siaddr: %s\n",inet_ntoa(discover_packet.siaddr));
  358. printf("DHCDISCOVER giaddr: %s\n",inet_ntoa(discover_packet.giaddr));
  359. }
  360. /* send the DHCPDISCOVER packet out */
  361. send_dhcp_packet(&discover_packet,sizeof(discover_packet),sock,&sockaddr_broadcast);
  362. if (verbose)
  363. printf("\n\n");
  364. return OK;
  365. }
  366. /* waits for a DHCPOFFER message from one or more DHCP servers */
  367. int get_dhcp_offer(int sock){
  368. dhcp_packet offer_packet;
  369. struct sockaddr_in source;
  370. int result=OK;
  371. int timeout=1;
  372. int responses=0;
  373. int x;
  374. time_t start_time;
  375. time_t current_time;
  376. time(&start_time);
  377. /* receive as many responses as we can */
  378. for(responses=0,valid_responses=0;;){
  379. time(&current_time);
  380. if((current_time-start_time)>=dhcpoffer_timeout)
  381. break;
  382. if (verbose)
  383. printf("\n\n");
  384. bzero(&source,sizeof(source));
  385. bzero(&offer_packet,sizeof(offer_packet));
  386. result=OK;
  387. result=receive_dhcp_packet(&offer_packet,sizeof(offer_packet),sock,dhcpoffer_timeout,&source);
  388. if(result!=OK){
  389. if (verbose)
  390. printf("Result=ERROR\n");
  391. continue;
  392. }
  393. else{
  394. if (verbose)
  395. printf("Result=OK\n");
  396. responses++;
  397. }
  398. if (verbose) {
  399. printf("DHCPOFFER from IP address %s\n",inet_ntoa(source.sin_addr));
  400. printf("DHCPOFFER XID: %lu (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid));
  401. }
  402. /* check packet xid to see if its the same as the one we used in the discover packet */
  403. if(ntohl(offer_packet.xid)!=packet_xid){
  404. if (verbose)
  405. printf("DHCPOFFER XID (%lu) did not match DHCPDISCOVER XID (%lu) - ignoring packet\n",ntohl(offer_packet.xid),packet_xid);
  406. continue;
  407. }
  408. /* check hardware address */
  409. result=OK;
  410. if (verbose)
  411. printf("DHCPOFFER chaddr: ");
  412. for(x=0;x<ETHERNET_HARDWARE_ADDRESS_LENGTH;x++){
  413. if (verbose)
  414. printf("%02X",(unsigned char)offer_packet.chaddr[x]);
  415. if(offer_packet.chaddr[x]!=client_hardware_address[x])
  416. result=ERROR;
  417. }
  418. if (verbose)
  419. printf("\n");
  420. if(result==ERROR){
  421. if (verbose)
  422. printf("DHCPOFFER hardware address did not match our own - ignoring packet\n");
  423. continue;
  424. }
  425. if (verbose) {
  426. printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr));
  427. printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr));
  428. printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr));
  429. printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr));
  430. }
  431. add_dhcp_offer(source.sin_addr,&offer_packet);
  432. valid_responses++;
  433. }
  434. if (verbose) {
  435. printf("Total responses seen on the wire: %d\n",responses);
  436. printf("Valid responses for this machine: %d\n",valid_responses);
  437. }
  438. return OK;
  439. }
  440. /* sends a DHCP packet */
  441. int send_dhcp_packet(void *buffer, int buffer_size, int sock, struct sockaddr_in *dest){
  442. struct sockaddr_in myname;
  443. int result;
  444. result=sendto(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)dest,sizeof(*dest));
  445. if (verbose)
  446. printf("send_dhcp_packet result: %d\n",result);
  447. if(result<0)
  448. return ERROR;
  449. return OK;
  450. }
  451. /* receives a DHCP packet */
  452. int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, struct sockaddr_in *address){
  453. struct timeval tv;
  454. fd_set readfds;
  455. int recv_result;
  456. socklen_t address_size;
  457. struct sockaddr_in source_address;
  458. /* wait for data to arrive (up time timeout) */
  459. tv.tv_sec=timeout;
  460. tv.tv_usec=0;
  461. FD_ZERO(&readfds);
  462. FD_SET(sock,&readfds);
  463. select(sock+1,&readfds,NULL,NULL,&tv);
  464. /* make sure some data has arrived */
  465. if(!FD_ISSET(sock,&readfds)){
  466. if (verbose)
  467. printf("No (more) data received\n");
  468. return ERROR;
  469. }
  470. else{
  471. /* 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
  472. not being interpreted correctly. sigh... */
  473. bzero(&source_address,sizeof(source_address));
  474. address_size=sizeof(source_address);
  475. recv_result=recvfrom(sock,(char *)buffer,buffer_size,MSG_PEEK,(struct sockaddr *)&source_address,&address_size);
  476. if (verbose)
  477. printf("recv_result_1: %d\n",recv_result);
  478. recv_result=recvfrom(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)&source_address,&address_size);
  479. if (verbose)
  480. printf("recv_result_2: %d\n",recv_result);
  481. if(recv_result==-1){
  482. if (verbose) {
  483. printf("recvfrom() failed, ");
  484. printf("errno: (%d) -> %s\n",errno,strerror(errno));
  485. }
  486. return ERROR;
  487. }
  488. else{
  489. if (verbose) {
  490. printf("receive_dhcp_packet() result: %d\n",recv_result);
  491. printf("receive_dhcp_packet() source: %s\n",inet_ntoa(source_address.sin_addr));
  492. }
  493. memcpy(address,&source_address,sizeof(source_address));
  494. return OK;
  495. }
  496. }
  497. return OK;
  498. }
  499. /* creates a socket for DHCP communication */
  500. int create_dhcp_socket(void){
  501. struct sockaddr_in myname;
  502. struct ifreq interface;
  503. int sock;
  504. int flag=1;
  505. /* Set up the address we're going to bind to. */
  506. bzero(&myname,sizeof(myname));
  507. myname.sin_family=AF_INET;
  508. myname.sin_port=htons(DHCP_CLIENT_PORT);
  509. myname.sin_addr.s_addr=INADDR_ANY; /* listen on any address */
  510. bzero(&myname.sin_zero,sizeof(myname.sin_zero));
  511. /* create a socket for DHCP communications */
  512. sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  513. if(sock<0){
  514. printf("Error: Could not create socket!\n");
  515. exit(STATE_UNKNOWN);
  516. }
  517. if (verbose)
  518. printf("DHCP socket: %d\n",sock);
  519. /* set the reuse address flag so we don't get errors when restarting */
  520. flag=1;
  521. if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){
  522. printf("Error: Could not set reuse address option on DHCP socket!\n");
  523. exit(STATE_UNKNOWN);
  524. }
  525. /* set the broadcast option - we need this to listen to DHCP broadcast messages */
  526. if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&flag,sizeof flag)<0){
  527. printf("Error: Could not set broadcast option on DHCP socket!\n");
  528. exit(STATE_UNKNOWN);
  529. }
  530. /* bind socket to interface */
  531. #if defined(__linux__)
  532. strncpy(interface.ifr_ifrn.ifrn_name,network_interface_name,IFNAMSIZ);
  533. if(setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,(char *)&interface,sizeof(interface))<0){
  534. printf("Error: Could not bind socket to interface %s. Check your privileges...\n",network_interface_name);
  535. exit(STATE_UNKNOWN);
  536. }
  537. #else
  538. strncpy(interface.ifr_name,network_interface_name,IFNAMSIZ);
  539. #endif
  540. /* bind the socket */
  541. if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){
  542. printf("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n",DHCP_CLIENT_PORT);
  543. exit(STATE_UNKNOWN);
  544. }
  545. return sock;
  546. }
  547. /* closes DHCP socket */
  548. int close_dhcp_socket(int sock){
  549. close(sock);
  550. return OK;
  551. }
  552. /* adds a requested server address to list in memory */
  553. int add_requested_server(struct in_addr server_address){
  554. requested_server *new_server;
  555. new_server=(requested_server *)malloc(sizeof(requested_server));
  556. if(new_server==NULL)
  557. return ERROR;
  558. new_server->server_address=server_address;
  559. new_server->next=requested_server_list;
  560. requested_server_list=new_server;
  561. requested_servers++;
  562. if (verbose)
  563. printf("Requested server address: %s\n",inet_ntoa(new_server->server_address));
  564. return OK;
  565. }
  566. /* adds a DHCP OFFER to list in memory */
  567. int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){
  568. dhcp_offer *new_offer;
  569. int x;
  570. int y;
  571. unsigned option_type;
  572. unsigned option_length;
  573. if(offer_packet==NULL)
  574. return ERROR;
  575. /* process all DHCP options present in the packet */
  576. for(x=4;x<MAX_DHCP_OPTIONS_LENGTH;){
  577. /* end of options (0 is really just a pad, but bail out anyway) */
  578. if((int)offer_packet->options[x]==-1 || (int)offer_packet->options[x]==0)
  579. break;
  580. /* get option type */
  581. option_type=offer_packet->options[x++];
  582. /* get option length */
  583. option_length=offer_packet->options[x++];
  584. if (verbose)
  585. printf("Option: %d (0x%02X)\n",option_type,option_length);
  586. /* get option data */
  587. if(option_type==DHCP_OPTION_LEASE_TIME)
  588. dhcp_lease_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  589. if(option_type==DHCP_OPTION_RENEWAL_TIME)
  590. dhcp_renewal_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  591. if(option_type==DHCP_OPTION_REBINDING_TIME)
  592. dhcp_rebinding_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  593. /* skip option data we're ignoring */
  594. else
  595. for(y=0;y<option_length;y++,x++);
  596. }
  597. if (verbose) {
  598. if(dhcp_lease_time==DHCP_INFINITE_TIME)
  599. printf("Lease Time: Infinite\n");
  600. else
  601. printf("Lease Time: %lu seconds\n",(unsigned long)dhcp_lease_time);
  602. if(dhcp_renewal_time==DHCP_INFINITE_TIME)
  603. printf("Renewal Time: Infinite\n");
  604. else
  605. printf("Renewal Time: %lu seconds\n",(unsigned long)dhcp_renewal_time);
  606. if(dhcp_rebinding_time==DHCP_INFINITE_TIME)
  607. printf("Rebinding Time: Infinite\n");
  608. printf("Rebinding Time: %lu seconds\n",(unsigned long)dhcp_rebinding_time);
  609. }
  610. new_offer=(dhcp_offer *)malloc(sizeof(dhcp_offer));
  611. if(new_offer==NULL)
  612. return ERROR;
  613. new_offer->server_address=source;
  614. new_offer->offered_address=offer_packet->yiaddr;
  615. new_offer->lease_time=dhcp_lease_time;
  616. new_offer->renewal_time=dhcp_renewal_time;
  617. new_offer->rebinding_time=dhcp_rebinding_time;
  618. if (verbose) {
  619. printf("Added offer from server @ %s",inet_ntoa(new_offer->server_address));
  620. printf(" of IP address %s\n",inet_ntoa(new_offer->offered_address));
  621. }
  622. /* add new offer to head of list */
  623. new_offer->next=dhcp_offer_list;
  624. dhcp_offer_list=new_offer;
  625. return OK;
  626. }
  627. /* frees memory allocated to DHCP OFFER list */
  628. int free_dhcp_offer_list(void){
  629. dhcp_offer *this_offer;
  630. dhcp_offer *next_offer;
  631. for(this_offer=dhcp_offer_list;this_offer!=NULL;this_offer=next_offer){
  632. next_offer=this_offer->next;
  633. free(this_offer);
  634. }
  635. return OK;
  636. }
  637. /* frees memory allocated to requested server list */
  638. int free_requested_server_list(void){
  639. requested_server *this_server;
  640. requested_server *next_server;
  641. for(this_server=requested_server_list;this_server!=NULL;this_server=next_server){
  642. next_server=this_server->next;
  643. free(this_server);
  644. }
  645. return OK;
  646. }
  647. /* gets state and plugin output to return */
  648. int get_results(void){
  649. dhcp_offer *temp_offer;
  650. requested_server *temp_server;
  651. int result;
  652. u_int32_t max_lease_time=0;
  653. received_requested_address=FALSE;
  654. /* checks responses from requested servers */
  655. requested_responses=0;
  656. if(requested_servers>0){
  657. for(temp_server=requested_server_list;temp_server!=NULL;temp_server=temp_server->next){
  658. for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
  659. /* get max lease time we were offered */
  660. if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
  661. max_lease_time=temp_offer->lease_time;
  662. /* see if we got the address we requested */
  663. if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
  664. received_requested_address=TRUE;
  665. /* see if the servers we wanted a response from talked to us or not */
  666. if(!memcmp(&temp_offer->server_address,&temp_server->server_address,sizeof(temp_server->server_address))){
  667. if (verbose) {
  668. printf("DHCP Server Match: Offerer=%s",inet_ntoa(temp_offer->server_address));
  669. printf(" Requested=%s\n",inet_ntoa(temp_server->server_address));
  670. }
  671. requested_responses++;
  672. }
  673. }
  674. }
  675. }
  676. /* else check and see if we got our requested address from any server */
  677. else{
  678. for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
  679. /* get max lease time we were offered */
  680. if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
  681. max_lease_time=temp_offer->lease_time;
  682. /* see if we got the address we requested */
  683. if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
  684. received_requested_address=TRUE;
  685. }
  686. }
  687. result=STATE_OK;
  688. if(valid_responses==0)
  689. result=STATE_CRITICAL;
  690. else if(requested_servers>0 && requested_responses==0)
  691. result=STATE_CRITICAL;
  692. else if(requested_responses<requested_servers)
  693. result=STATE_WARNING;
  694. else if(request_specific_address==TRUE && received_requested_address==FALSE)
  695. result=STATE_WARNING;
  696. printf("DHCP %s: ",(result==STATE_OK)?"ok":"problem");
  697. /* we didn't receive any DHCPOFFERs */
  698. if(dhcp_offer_list==NULL){
  699. printf("No DHCPOFFERs were received.\n");
  700. return result;
  701. }
  702. printf("Received %d DHCPOFFER(s)",valid_responses);
  703. if(requested_servers>0)
  704. printf(", %s%d of %d requested servers responded",((requested_responses<requested_servers) && requested_responses>0)?"only ":"",requested_responses,requested_servers);
  705. if(request_specific_address==TRUE)
  706. printf(", requested address (%s) was %soffered",inet_ntoa(requested_address),(received_requested_address==TRUE)?"":"not ");
  707. printf(", max lease time = ");
  708. if(max_lease_time==DHCP_INFINITE_TIME)
  709. printf("Infinity");
  710. else
  711. printf("%lu sec",(unsigned long)max_lease_time);
  712. printf(".\n");
  713. return result;
  714. }
  715. /* print usage help */
  716. void print_help(void){
  717. /*print_revision(progname,"$Revision$");*/
  718. printf("Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)\n\n");
  719. printf("This plugin tests the availability of DHCP servers on a network.\n\n");
  720. print_usage();
  721. printf
  722. ("\nOptions:\n"
  723. " -s, --serverip=IPADDRESS\n"
  724. " IP address of DHCP server that we must hear from\n"
  725. " -r, --requestedip=IPADDRESS\n"
  726. " IP address that should be offered by at least one DHCP server\n"
  727. " -t, --timeout=INTEGER\n"
  728. " Seconds to wait for DHCPOFFER before timeout occurs\n"
  729. " -i, --interface=STRING\n"
  730. " Interface to to use for listening (i.e. eth0)\n"
  731. " -v, --verbose\n"
  732. " Print extra information (command-line use only)\n"
  733. " -h, --help\n"
  734. " Print detailed help screen\n"
  735. " -V, --version\n"
  736. " Print version information\n\n"
  737. );
  738. /*support();*/
  739. return;
  740. }
  741. /* prints usage information */
  742. void print_usage(void){
  743. printf("Usage: %s [-s serverip] [-r requestedip] [-t timeout] [-i interface]\n",progname);
  744. printf(" %s --help\n",progname);
  745. printf(" %s --version\n",progname);
  746. return;
  747. }
  748. /* process command-line arguments */
  749. int process_arguments(int argc, char **argv){
  750. int c;
  751. if(argc<1)
  752. return ERROR;
  753. c=0;
  754. while((c+=(call_getopt(argc-c,&argv[c])))<argc){
  755. /*
  756. if(is_option(argv[c]))
  757. continue;
  758. */
  759. }
  760. return validate_arguments();
  761. }
  762. int call_getopt(int argc, char **argv){
  763. int c=0;
  764. int i=0;
  765. struct in_addr ipaddress;
  766. #ifdef HAVE_GETOPT_H
  767. int option_index = 0;
  768. static struct option long_options[] =
  769. {
  770. {"serverip", required_argument,0,'s'},
  771. {"requestedip", required_argument,0,'r'},
  772. {"timeout", required_argument,0,'t'},
  773. {"interface", required_argument,0,'i'},
  774. {"verbose", no_argument, 0,'v'},
  775. {"version", no_argument, 0,'V'},
  776. {"help", no_argument, 0,'h'},
  777. {0,0,0,0}
  778. };
  779. #endif
  780. while(1){
  781. #ifdef HAVE_GETOPT_H
  782. c=getopt_long(argc,argv,"+hVvt:s:r:t:i:",long_options,&option_index);
  783. #else
  784. c=getopt(argc,argv,"+?hVvt:s:r:t:i:");
  785. #endif
  786. i++;
  787. if(c==-1||c==EOF||c==1)
  788. break;
  789. switch(c){
  790. case 'w':
  791. case 'r':
  792. case 't':
  793. case 'i':
  794. i++;
  795. break;
  796. default:
  797. break;
  798. }
  799. switch(c){
  800. case 's': /* DHCP server address */
  801. if(inet_aton(optarg,&ipaddress))
  802. add_requested_server(ipaddress);
  803. /*
  804. else
  805. usage("Invalid server IP address\n");
  806. */
  807. break;
  808. case 'r': /* address we are requested from DHCP servers */
  809. if(inet_aton(optarg,&ipaddress)){
  810. requested_address=ipaddress;
  811. request_specific_address=TRUE;
  812. }
  813. /*
  814. else
  815. usage("Invalid requested IP address\n");
  816. */
  817. break;
  818. case 't': /* timeout */
  819. /*
  820. if(is_intnonneg(optarg))
  821. */
  822. if(atoi(optarg)>0)
  823. dhcpoffer_timeout=atoi(optarg);
  824. /*
  825. else
  826. usage("Time interval must be a nonnegative integer\n");
  827. */
  828. break;
  829. case 'i': /* interface name */
  830. strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1);
  831. network_interface_name[sizeof(network_interface_name)-1]='\x0';
  832. break;
  833. case 'V': /* version */
  834. /*print_revision(progname,"$Revision$");*/
  835. exit(STATE_OK);
  836. case 'h': /* help */
  837. print_help();
  838. exit(STATE_OK);
  839. case 'v': /* verbose */
  840. verbose=1;
  841. break;
  842. case '?': /* help */
  843. /*usage("Invalid argument\n");*/
  844. break;
  845. default:
  846. break;
  847. }
  848. }
  849. return i;
  850. }
  851. int validate_arguments(void){
  852. return OK;
  853. }
  854. #if defined(__sun__) || defined(__solaris__) || defined(__hpux__)
  855. /*
  856. * Copyright @2000, 2003 Martin Kompf, martin@kompf.de
  857. *
  858. * Nagios plugins thanks Martin for this code.
  859. */
  860. /* get a message from a stream; return type of message */
  861. static int get_msg(int fd)
  862. {
  863. int flags = 0;
  864. int res, ret;
  865. ctl_area[0] = 0;
  866. dat_area[0] = 0;
  867. ret = 0;
  868. res = getmsg(fd, &ctl, &dat, &flags);
  869. if(res < 0) {
  870. if(errno == EINTR) {
  871. return(GOT_INTR);
  872. } else {
  873. printf("%s\n", "get_msg FAILED.");
  874. return(GOT_ERR);
  875. }
  876. }
  877. if(ctl.len > 0) {
  878. ret |= GOT_CTRL;
  879. }
  880. if(dat.len > 0) {
  881. ret |= GOT_DATA;
  882. }
  883. return(ret);
  884. }
  885. /* verify that dl_primitive in ctl_area = prim */
  886. static int check_ctrl(int prim)
  887. {
  888. dl_error_ack_t *err_ack = (dl_error_ack_t *)ctl_area;
  889. if(err_ack->dl_primitive != prim) {
  890. printf("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n", strerror(errno));
  891. exit(STATE_UNKNOWN);
  892. }
  893. return 0;
  894. }
  895. /* put a control message on a stream */
  896. static int put_ctrl(int fd, int len, int pri)
  897. {
  898. ctl.len = len;
  899. if(putmsg(fd, &ctl, 0, pri) < 0) {
  900. printf("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n", strerror(errno));
  901. exit(STATE_UNKNOWN);
  902. }
  903. return 0;
  904. }
  905. /* put a control + data message on a stream */
  906. static int put_both(int fd, int clen, int dlen, int pri)
  907. {
  908. ctl.len = clen;
  909. dat.len = dlen;
  910. if(putmsg(fd, &ctl, &dat, pri) < 0) {
  911. printf("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n", strerror(errno));
  912. exit(STATE_UNKNOWN);
  913. }
  914. return 0;
  915. }
  916. /* open file descriptor and attach */
  917. static int dl_open(const char *dev, int unit, int *fd)
  918. {
  919. dl_attach_req_t *attach_req = (dl_attach_req_t *)ctl_area;
  920. if((*fd = open(dev, O_RDWR)) == -1) {
  921. printf("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n", dev, strerror(errno));
  922. exit(STATE_UNKNOWN);
  923. }
  924. attach_req->dl_primitive = DL_ATTACH_REQ;
  925. attach_req->dl_ppa = unit;
  926. put_ctrl(*fd, sizeof(dl_attach_req_t), 0);
  927. get_msg(*fd);
  928. return check_ctrl(DL_OK_ACK);
  929. }
  930. /* send DL_BIND_REQ */
  931. static int dl_bind(int fd, int sap, u_char *addr)
  932. {
  933. dl_bind_req_t *bind_req = (dl_bind_req_t *)ctl_area;
  934. dl_bind_ack_t *bind_ack = (dl_bind_ack_t *)ctl_area;
  935. bind_req->dl_primitive = DL_BIND_REQ;
  936. bind_req->dl_sap = sap;
  937. bind_req->dl_max_conind = 1;
  938. bind_req->dl_service_mode = DL_CLDLS;
  939. bind_req->dl_conn_mgmt = 0;
  940. bind_req->dl_xidtest_flg = 0;
  941. put_ctrl(fd, sizeof(dl_bind_req_t), 0);
  942. get_msg(fd);
  943. if (GOT_ERR == check_ctrl(DL_BIND_ACK)) {
  944. printf("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n", strerror(errno));
  945. exit(STATE_UNKNOWN);
  946. }
  947. bcopy((u_char *)bind_ack + bind_ack->dl_addr_offset, addr,
  948. bind_ack->dl_addr_length);
  949. return 0;
  950. }
  951. /***********************************************************************
  952. * interface:
  953. * function mac_addr_dlpi - get the mac address of the interface with
  954. * type dev (eg lnc, hme) and unit (0, 1 ..)
  955. *
  956. * parameter: addr: an array of six bytes, has to be allocated by the caller
  957. *
  958. * return: 0 if OK, -1 if the address could not be determined
  959. *
  960. *
  961. ***********************************************************************/
  962. long mac_addr_dlpi( const char *dev, int unit, u_char *addr) {
  963. int fd;
  964. u_char mac_addr[25];
  965. if (GOT_ERR != dl_open(dev, unit, &fd)) {
  966. if (GOT_ERR != dl_bind(fd, INSAP, mac_addr)) {
  967. bcopy( mac_addr, addr, 6);
  968. return 0;
  969. }
  970. }
  971. close(fd);
  972. return -1;
  973. }
  974. #endif