check_dhcp.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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__)
  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. #else
  282. printf("Error: can't get MAC address for this architcture.\n");
  283. exit(STATE_UNKNOWN);
  284. #endif
  285. if (verbose) {
  286. printf( "Hadrware address: ");
  287. for (i=0; i<6; ++i)
  288. printf("%2.2x", client_hardware_address[i]);
  289. printf( "\n");
  290. }
  291. return OK;
  292. }
  293. /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */
  294. int send_dhcp_discover(int sock){
  295. dhcp_packet discover_packet;
  296. struct sockaddr_in sockaddr_broadcast;
  297. /* clear the packet data structure */
  298. bzero(&discover_packet,sizeof(discover_packet));
  299. /* boot request flag (backward compatible with BOOTP servers) */
  300. discover_packet.op=BOOTREQUEST;
  301. /* hardware address type */
  302. discover_packet.htype=ETHERNET_HARDWARE_ADDRESS;
  303. /* length of our hardware address */
  304. discover_packet.hlen=ETHERNET_HARDWARE_ADDRESS_LENGTH;
  305. discover_packet.hops=0;
  306. /* transaction id is supposed to be random */
  307. srand(time(NULL));
  308. packet_xid=random();
  309. discover_packet.xid=htonl(packet_xid);
  310. /**** WHAT THE HECK IS UP WITH THIS?!? IF I DON'T MAKE THIS CALL, ONLY ONE SERVER RESPONSE IS PROCESSED!!!! ****/
  311. /* downright bizzarre... */
  312. ntohl(discover_packet.xid);
  313. /*discover_packet.secs=htons(65535);*/
  314. discover_packet.secs=0xFF;
  315. /* tell server it should broadcast its response */
  316. discover_packet.flags=htons(DHCP_BROADCAST_FLAG);
  317. /* our hardware address */
  318. memcpy(discover_packet.chaddr,client_hardware_address,ETHERNET_HARDWARE_ADDRESS_LENGTH);
  319. /* first four bytes of options field is magic cookie (as per RFC 2132) */
  320. discover_packet.options[0]='\x63';
  321. discover_packet.options[1]='\x82';
  322. discover_packet.options[2]='\x53';
  323. discover_packet.options[3]='\x63';
  324. /* DHCP message type is embedded in options field */
  325. discover_packet.options[4]=DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */
  326. discover_packet.options[5]='\x01'; /* DHCP message option length in bytes */
  327. discover_packet.options[6]=DHCPDISCOVER;
  328. /* the IP address we're requesting */
  329. if(request_specific_address==TRUE){
  330. discover_packet.options[7]=DHCP_OPTION_REQUESTED_ADDRESS;
  331. discover_packet.options[8]='\x04';
  332. memcpy(&discover_packet.options[9],&requested_address,sizeof(requested_address));
  333. }
  334. /* send the DHCPDISCOVER packet to broadcast address */
  335. sockaddr_broadcast.sin_family=AF_INET;
  336. sockaddr_broadcast.sin_port=htons(DHCP_SERVER_PORT);
  337. sockaddr_broadcast.sin_addr.s_addr=INADDR_BROADCAST;
  338. bzero(&sockaddr_broadcast.sin_zero,sizeof(sockaddr_broadcast.sin_zero));
  339. if (verbose) {
  340. printf("DHCPDISCOVER to %s port %d\n",inet_ntoa(sockaddr_broadcast.sin_addr),ntohs(sockaddr_broadcast.sin_port));
  341. printf("DHCPDISCOVER XID: %lu (0x%X)\n",ntohl(discover_packet.xid),ntohl(discover_packet.xid));
  342. printf("DHCDISCOVER ciaddr: %s\n",inet_ntoa(discover_packet.ciaddr));
  343. printf("DHCDISCOVER yiaddr: %s\n",inet_ntoa(discover_packet.yiaddr));
  344. printf("DHCDISCOVER siaddr: %s\n",inet_ntoa(discover_packet.siaddr));
  345. printf("DHCDISCOVER giaddr: %s\n",inet_ntoa(discover_packet.giaddr));
  346. }
  347. /* send the DHCPDISCOVER packet out */
  348. send_dhcp_packet(&discover_packet,sizeof(discover_packet),sock,&sockaddr_broadcast);
  349. if (verbose)
  350. printf("\n\n");
  351. return OK;
  352. }
  353. /* waits for a DHCPOFFER message from one or more DHCP servers */
  354. int get_dhcp_offer(int sock){
  355. dhcp_packet offer_packet;
  356. struct sockaddr_in source;
  357. int result=OK;
  358. int timeout=1;
  359. int responses=0;
  360. int x;
  361. time_t start_time;
  362. time_t current_time;
  363. time(&start_time);
  364. /* receive as many responses as we can */
  365. for(responses=0,valid_responses=0;;){
  366. time(&current_time);
  367. if((current_time-start_time)>=dhcpoffer_timeout)
  368. break;
  369. if (verbose)
  370. printf("\n\n");
  371. bzero(&source,sizeof(source));
  372. bzero(&offer_packet,sizeof(offer_packet));
  373. result=OK;
  374. result=receive_dhcp_packet(&offer_packet,sizeof(offer_packet),sock,dhcpoffer_timeout,&source);
  375. if(result!=OK){
  376. if (verbose)
  377. printf("Result=ERROR\n");
  378. continue;
  379. }
  380. else{
  381. if (verbose)
  382. printf("Result=OK\n");
  383. responses++;
  384. }
  385. if (verbose) {
  386. printf("DHCPOFFER from IP address %s\n",inet_ntoa(source.sin_addr));
  387. printf("DHCPOFFER XID: %lu (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid));
  388. }
  389. /* check packet xid to see if its the same as the one we used in the discover packet */
  390. if(ntohl(offer_packet.xid)!=packet_xid){
  391. if (verbose)
  392. printf("DHCPOFFER XID (%lu) did not match DHCPDISCOVER XID (%lu) - ignoring packet\n",ntohl(offer_packet.xid),packet_xid);
  393. continue;
  394. }
  395. /* check hardware address */
  396. result=OK;
  397. if (verbose)
  398. printf("DHCPOFFER chaddr: ");
  399. for(x=0;x<ETHERNET_HARDWARE_ADDRESS_LENGTH;x++){
  400. if (verbose)
  401. printf("%02X",(unsigned char)offer_packet.chaddr[x]);
  402. if(offer_packet.chaddr[x]!=client_hardware_address[x])
  403. result=ERROR;
  404. }
  405. if (verbose)
  406. printf("\n");
  407. if(result==ERROR){
  408. if (verbose)
  409. printf("DHCPOFFER hardware address did not match our own - ignoring packet\n");
  410. continue;
  411. }
  412. if (verbose) {
  413. printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr));
  414. printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr));
  415. printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr));
  416. printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr));
  417. }
  418. add_dhcp_offer(source.sin_addr,&offer_packet);
  419. valid_responses++;
  420. }
  421. if (verbose) {
  422. printf("Total responses seen on the wire: %d\n",responses);
  423. printf("Valid responses for this machine: %d\n",valid_responses);
  424. }
  425. return OK;
  426. }
  427. /* sends a DHCP packet */
  428. int send_dhcp_packet(void *buffer, int buffer_size, int sock, struct sockaddr_in *dest){
  429. struct sockaddr_in myname;
  430. int result;
  431. result=sendto(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)dest,sizeof(*dest));
  432. if (verbose)
  433. printf("send_dhcp_packet result: %d\n",result);
  434. if(result<0)
  435. return ERROR;
  436. return OK;
  437. }
  438. /* receives a DHCP packet */
  439. int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, struct sockaddr_in *address){
  440. struct timeval tv;
  441. fd_set readfds;
  442. int recv_result;
  443. socklen_t address_size;
  444. struct sockaddr_in source_address;
  445. /* wait for data to arrive (up time timeout) */
  446. tv.tv_sec=timeout;
  447. tv.tv_usec=0;
  448. FD_ZERO(&readfds);
  449. FD_SET(sock,&readfds);
  450. select(sock+1,&readfds,NULL,NULL,&tv);
  451. /* make sure some data has arrived */
  452. if(!FD_ISSET(sock,&readfds)){
  453. if (verbose)
  454. printf("No (more) data received\n");
  455. return ERROR;
  456. }
  457. else{
  458. /* 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
  459. not being interpreted correctly. sigh... */
  460. bzero(&source_address,sizeof(source_address));
  461. address_size=sizeof(source_address);
  462. recv_result=recvfrom(sock,(char *)buffer,buffer_size,MSG_PEEK,(struct sockaddr *)&source_address,&address_size);
  463. if (verbose)
  464. printf("recv_result_1: %d\n",recv_result);
  465. recv_result=recvfrom(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)&source_address,&address_size);
  466. if (verbose)
  467. printf("recv_result_2: %d\n",recv_result);
  468. if(recv_result==-1){
  469. if (verbose) {
  470. printf("recvfrom() failed, ");
  471. printf("errno: (%d) -> %s\n",errno,strerror(errno));
  472. }
  473. return ERROR;
  474. }
  475. else{
  476. if (verbose) {
  477. printf("receive_dhcp_packet() result: %d\n",recv_result);
  478. printf("receive_dhcp_packet() source: %s\n",inet_ntoa(source_address.sin_addr));
  479. }
  480. memcpy(address,&source_address,sizeof(source_address));
  481. return OK;
  482. }
  483. }
  484. return OK;
  485. }
  486. /* creates a socket for DHCP communication */
  487. int create_dhcp_socket(void){
  488. struct sockaddr_in myname;
  489. struct ifreq interface;
  490. int sock;
  491. int flag=1;
  492. /* Set up the address we're going to bind to. */
  493. bzero(&myname,sizeof(myname));
  494. myname.sin_family=AF_INET;
  495. myname.sin_port=htons(DHCP_CLIENT_PORT);
  496. myname.sin_addr.s_addr=INADDR_ANY; /* listen on any address */
  497. bzero(&myname.sin_zero,sizeof(myname.sin_zero));
  498. /* create a socket for DHCP communications */
  499. sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  500. if(sock<0){
  501. printf("Error: Could not create socket!\n");
  502. exit(STATE_UNKNOWN);
  503. }
  504. if (verbose)
  505. printf("DHCP socket: %d\n",sock);
  506. /* set the reuse address flag so we don't get errors when restarting */
  507. flag=1;
  508. if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){
  509. printf("Error: Could not set reuse address option on DHCP socket!\n");
  510. exit(STATE_UNKNOWN);
  511. }
  512. /* set the broadcast option - we need this to listen to DHCP broadcast messages */
  513. if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&flag,sizeof flag)<0){
  514. printf("Error: Could not set broadcast option on DHCP socket!\n");
  515. exit(STATE_UNKNOWN);
  516. }
  517. /* bind socket to interface */
  518. #if defined(__linux__)
  519. strncpy(interface.ifr_ifrn.ifrn_name,network_interface_name,IFNAMSIZ);
  520. if(setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,(char *)&interface,sizeof(interface))<0){
  521. printf("Error: Could not bind socket to interface %s. Check your privileges...\n",network_interface_name);
  522. exit(STATE_UNKNOWN);
  523. }
  524. #else
  525. strncpy(interface.ifr_name,network_interface_name,IFNAMSIZ);
  526. #endif
  527. /* bind the socket */
  528. if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){
  529. printf("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n",DHCP_CLIENT_PORT);
  530. exit(STATE_UNKNOWN);
  531. }
  532. return sock;
  533. }
  534. /* closes DHCP socket */
  535. int close_dhcp_socket(int sock){
  536. close(sock);
  537. return OK;
  538. }
  539. /* adds a requested server address to list in memory */
  540. int add_requested_server(struct in_addr server_address){
  541. requested_server *new_server;
  542. new_server=(requested_server *)malloc(sizeof(requested_server));
  543. if(new_server==NULL)
  544. return ERROR;
  545. new_server->server_address=server_address;
  546. new_server->next=requested_server_list;
  547. requested_server_list=new_server;
  548. requested_servers++;
  549. if (verbose)
  550. printf("Requested server address: %s\n",inet_ntoa(new_server->server_address));
  551. return OK;
  552. }
  553. /* adds a DHCP OFFER to list in memory */
  554. int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){
  555. dhcp_offer *new_offer;
  556. int x;
  557. int y;
  558. unsigned option_type;
  559. unsigned option_length;
  560. if(offer_packet==NULL)
  561. return ERROR;
  562. /* process all DHCP options present in the packet */
  563. for(x=4;x<MAX_DHCP_OPTIONS_LENGTH;){
  564. /* end of options (0 is really just a pad, but bail out anyway) */
  565. if((int)offer_packet->options[x]==-1 || (int)offer_packet->options[x]==0)
  566. break;
  567. /* get option type */
  568. option_type=offer_packet->options[x++];
  569. /* get option length */
  570. option_length=offer_packet->options[x++];
  571. if (verbose)
  572. printf("Option: %d (0x%02X)\n",option_type,option_length);
  573. /* get option data */
  574. if(option_type==DHCP_OPTION_LEASE_TIME)
  575. dhcp_lease_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  576. if(option_type==DHCP_OPTION_RENEWAL_TIME)
  577. dhcp_renewal_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  578. if(option_type==DHCP_OPTION_REBINDING_TIME)
  579. dhcp_rebinding_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
  580. /* skip option data we're ignoring */
  581. else
  582. for(y=0;y<option_length;y++,x++);
  583. }
  584. if (verbose) {
  585. if(dhcp_lease_time==DHCP_INFINITE_TIME)
  586. printf("Lease Time: Infinite\n");
  587. else
  588. printf("Lease Time: %lu seconds\n",(unsigned long)dhcp_lease_time);
  589. if(dhcp_renewal_time==DHCP_INFINITE_TIME)
  590. printf("Renewal Time: Infinite\n");
  591. else
  592. printf("Renewal Time: %lu seconds\n",(unsigned long)dhcp_renewal_time);
  593. if(dhcp_rebinding_time==DHCP_INFINITE_TIME)
  594. printf("Rebinding Time: Infinite\n");
  595. printf("Rebinding Time: %lu seconds\n",(unsigned long)dhcp_rebinding_time);
  596. }
  597. new_offer=(dhcp_offer *)malloc(sizeof(dhcp_offer));
  598. if(new_offer==NULL)
  599. return ERROR;
  600. new_offer->server_address=source;
  601. new_offer->offered_address=offer_packet->yiaddr;
  602. new_offer->lease_time=dhcp_lease_time;
  603. new_offer->renewal_time=dhcp_renewal_time;
  604. new_offer->rebinding_time=dhcp_rebinding_time;
  605. if (verbose) {
  606. printf("Added offer from server @ %s",inet_ntoa(new_offer->server_address));
  607. printf(" of IP address %s\n",inet_ntoa(new_offer->offered_address));
  608. }
  609. /* add new offer to head of list */
  610. new_offer->next=dhcp_offer_list;
  611. dhcp_offer_list=new_offer;
  612. return OK;
  613. }
  614. /* frees memory allocated to DHCP OFFER list */
  615. int free_dhcp_offer_list(void){
  616. dhcp_offer *this_offer;
  617. dhcp_offer *next_offer;
  618. for(this_offer=dhcp_offer_list;this_offer!=NULL;this_offer=next_offer){
  619. next_offer=this_offer->next;
  620. free(this_offer);
  621. }
  622. return OK;
  623. }
  624. /* frees memory allocated to requested server list */
  625. int free_requested_server_list(void){
  626. requested_server *this_server;
  627. requested_server *next_server;
  628. for(this_server=requested_server_list;this_server!=NULL;this_server=next_server){
  629. next_server=this_server->next;
  630. free(this_server);
  631. }
  632. return OK;
  633. }
  634. /* gets state and plugin output to return */
  635. int get_results(void){
  636. dhcp_offer *temp_offer;
  637. requested_server *temp_server;
  638. int result;
  639. u_int32_t max_lease_time=0;
  640. received_requested_address=FALSE;
  641. /* checks responses from requested servers */
  642. requested_responses=0;
  643. if(requested_servers>0){
  644. for(temp_server=requested_server_list;temp_server!=NULL;temp_server=temp_server->next){
  645. for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
  646. /* get max lease time we were offered */
  647. if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
  648. max_lease_time=temp_offer->lease_time;
  649. /* see if we got the address we requested */
  650. if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
  651. received_requested_address=TRUE;
  652. /* see if the servers we wanted a response from talked to us or not */
  653. if(!memcmp(&temp_offer->server_address,&temp_server->server_address,sizeof(temp_server->server_address))){
  654. if (verbose) {
  655. printf("DHCP Server Match: Offerer=%s",inet_ntoa(temp_offer->server_address));
  656. printf(" Requested=%s\n",inet_ntoa(temp_server->server_address));
  657. }
  658. requested_responses++;
  659. }
  660. }
  661. }
  662. }
  663. /* else check and see if we got our requested address from any server */
  664. else{
  665. for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
  666. /* get max lease time we were offered */
  667. if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
  668. max_lease_time=temp_offer->lease_time;
  669. /* see if we got the address we requested */
  670. if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
  671. received_requested_address=TRUE;
  672. }
  673. }
  674. result=STATE_OK;
  675. if(valid_responses==0)
  676. result=STATE_CRITICAL;
  677. else if(requested_servers>0 && requested_responses==0)
  678. result=STATE_CRITICAL;
  679. else if(requested_responses<requested_servers)
  680. result=STATE_WARNING;
  681. else if(request_specific_address==TRUE && received_requested_address==FALSE)
  682. result=STATE_WARNING;
  683. printf("DHCP %s: ",(result==STATE_OK)?"ok":"problem");
  684. /* we didn't receive any DHCPOFFERs */
  685. if(dhcp_offer_list==NULL){
  686. printf("No DHCPOFFERs were received.\n");
  687. return result;
  688. }
  689. printf("Received %d DHCPOFFER(s)",valid_responses);
  690. if(requested_servers>0)
  691. printf(", %s%d of %d requested servers responded",((requested_responses<requested_servers) && requested_responses>0)?"only ":"",requested_responses,requested_servers);
  692. if(request_specific_address==TRUE)
  693. printf(", requested address (%s) was %soffered",inet_ntoa(requested_address),(received_requested_address==TRUE)?"":"not ");
  694. printf(", max lease time = ");
  695. if(max_lease_time==DHCP_INFINITE_TIME)
  696. printf("Infinity");
  697. else
  698. printf("%lu sec",(unsigned long)max_lease_time);
  699. printf(".\n");
  700. return result;
  701. }
  702. /* print usage help */
  703. void print_help(void){
  704. /*print_revision(progname,"$Revision$");*/
  705. printf("Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)\n\n");
  706. printf("This plugin tests the availability of DHCP servers on a network.\n\n");
  707. print_usage();
  708. printf
  709. ("\nOptions:\n"
  710. " -s, --serverip=IPADDRESS\n"
  711. " IP address of DHCP server that we must hear from\n"
  712. " -r, --requestedip=IPADDRESS\n"
  713. " IP address that should be offered by at least one DHCP server\n"
  714. " -t, --timeout=INTEGER\n"
  715. " Seconds to wait for DHCPOFFER before timeout occurs\n"
  716. " -i, --interface=STRING\n"
  717. " Interface to to use for listening (i.e. eth0)\n"
  718. " -v, --verbose\n"
  719. " Print extra information (command-line use only)\n"
  720. " -h, --help\n"
  721. " Print detailed help screen\n"
  722. " -V, --version\n"
  723. " Print version information\n\n"
  724. );
  725. /*support();*/
  726. return;
  727. }
  728. /* prints usage information */
  729. void print_usage(void){
  730. printf("Usage: %s [-s serverip] [-r requestedip] [-t timeout] [-i interface]\n",progname);
  731. printf(" %s --help\n",progname);
  732. printf(" %s --version\n",progname);
  733. return;
  734. }
  735. /* process command-line arguments */
  736. int process_arguments(int argc, char **argv){
  737. int c;
  738. if(argc<1)
  739. return ERROR;
  740. c=0;
  741. while((c+=(call_getopt(argc-c,&argv[c])))<argc){
  742. /*
  743. if(is_option(argv[c]))
  744. continue;
  745. */
  746. }
  747. return validate_arguments();
  748. }
  749. int call_getopt(int argc, char **argv){
  750. int c=0;
  751. int i=0;
  752. struct in_addr ipaddress;
  753. #ifdef HAVE_GETOPT_H
  754. int option_index = 0;
  755. static struct option long_options[] =
  756. {
  757. {"serverip", required_argument,0,'s'},
  758. {"requestedip", required_argument,0,'r'},
  759. {"timeout", required_argument,0,'t'},
  760. {"interface", required_argument,0,'i'},
  761. {"verbose", no_argument, 0,'v'},
  762. {"version", no_argument, 0,'V'},
  763. {"help", no_argument, 0,'h'},
  764. {0,0,0,0}
  765. };
  766. #endif
  767. while(1){
  768. #ifdef HAVE_GETOPT_H
  769. c=getopt_long(argc,argv,"+hVvt:s:r:t:i:",long_options,&option_index);
  770. #else
  771. c=getopt(argc,argv,"+?hVvt:s:r:t:i:");
  772. #endif
  773. i++;
  774. if(c==-1||c==EOF||c==1)
  775. break;
  776. switch(c){
  777. case 'w':
  778. case 'r':
  779. case 't':
  780. case 'i':
  781. i++;
  782. break;
  783. default:
  784. break;
  785. }
  786. switch(c){
  787. case 's': /* DHCP server address */
  788. if(inet_aton(optarg,&ipaddress))
  789. add_requested_server(ipaddress);
  790. /*
  791. else
  792. usage("Invalid server IP address\n");
  793. */
  794. break;
  795. case 'r': /* address we are requested from DHCP servers */
  796. if(inet_aton(optarg,&ipaddress)){
  797. requested_address=ipaddress;
  798. request_specific_address=TRUE;
  799. }
  800. /*
  801. else
  802. usage("Invalid requested IP address\n");
  803. */
  804. break;
  805. case 't': /* timeout */
  806. /*
  807. if(is_intnonneg(optarg))
  808. */
  809. if(atoi(optarg)>0)
  810. dhcpoffer_timeout=atoi(optarg);
  811. /*
  812. else
  813. usage("Time interval must be a nonnegative integer\n");
  814. */
  815. break;
  816. case 'i': /* interface name */
  817. strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1);
  818. network_interface_name[sizeof(network_interface_name)-1]='\x0';
  819. break;
  820. case 'V': /* version */
  821. /*print_revision(progname,"$Revision$");*/
  822. exit(STATE_OK);
  823. case 'h': /* help */
  824. print_help();
  825. exit(STATE_OK);
  826. case 'v': /* verbose */
  827. verbose=1;
  828. break;
  829. case '?': /* help */
  830. /*usage("Invalid argument\n");*/
  831. break;
  832. default:
  833. break;
  834. }
  835. }
  836. return i;
  837. }
  838. int validate_arguments(void){
  839. return OK;
  840. }
  841. #if defined(__sun__) || defined(__solaris__)
  842. /*
  843. * Copyright @2000, 2003 Martin Kompf, martin@kompf.de
  844. *
  845. * Nagios plugins thanks Martin for this code.
  846. */
  847. /* get a message from a stream; return type of message */
  848. static int get_msg(int fd)
  849. {
  850. int flags = 0;
  851. int res, ret;
  852. ctl_area[0] = 0;
  853. dat_area[0] = 0;
  854. ret = 0;
  855. res = getmsg(fd, &ctl, &dat, &flags);
  856. if(res < 0) {
  857. if(errno == EINTR) {
  858. return(GOT_INTR);
  859. } else {
  860. printf("%s\n", "get_msg FAILED.");
  861. return(GOT_ERR);
  862. }
  863. }
  864. if(ctl.len > 0) {
  865. ret |= GOT_CTRL;
  866. }
  867. if(dat.len > 0) {
  868. ret |= GOT_DATA;
  869. }
  870. return(ret);
  871. }
  872. /* verify that dl_primitive in ctl_area = prim */
  873. static int check_ctrl(int prim)
  874. {
  875. dl_error_ack_t *err_ack = (dl_error_ack_t *)ctl_area;
  876. if(err_ack->dl_primitive != prim) {
  877. printf("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n", strerror(errno));
  878. exit(STATE_UNKNOWN);
  879. }
  880. return 0;
  881. }
  882. /* put a control message on a stream */
  883. static int put_ctrl(int fd, int len, int pri)
  884. {
  885. ctl.len = len;
  886. if(putmsg(fd, &ctl, 0, pri) < 0) {
  887. printf("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n", strerror(errno));
  888. exit(STATE_UNKNOWN);
  889. }
  890. return 0;
  891. }
  892. /* put a control + data message on a stream */
  893. static int put_both(int fd, int clen, int dlen, int pri)
  894. {
  895. ctl.len = clen;
  896. dat.len = dlen;
  897. if(putmsg(fd, &ctl, &dat, pri) < 0) {
  898. printf("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n", strerror(errno));
  899. exit(STATE_UNKNOWN);
  900. }
  901. return 0;
  902. }
  903. /* open file descriptor and attach */
  904. static int dl_open(const char *dev, int unit, int *fd)
  905. {
  906. dl_attach_req_t *attach_req = (dl_attach_req_t *)ctl_area;
  907. if((*fd = open(dev, O_RDWR)) == -1) {
  908. printf("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n", dev, strerror(errno));
  909. exit(STATE_UNKNOWN);
  910. }
  911. attach_req->dl_primitive = DL_ATTACH_REQ;
  912. attach_req->dl_ppa = unit;
  913. put_ctrl(*fd, sizeof(dl_attach_req_t), 0);
  914. get_msg(*fd);
  915. return check_ctrl(DL_OK_ACK);
  916. }
  917. /* send DL_BIND_REQ */
  918. static int dl_bind(int fd, int sap, u_char *addr)
  919. {
  920. dl_bind_req_t *bind_req = (dl_bind_req_t *)ctl_area;
  921. dl_bind_ack_t *bind_ack = (dl_bind_ack_t *)ctl_area;
  922. bind_req->dl_primitive = DL_BIND_REQ;
  923. bind_req->dl_sap = sap;
  924. bind_req->dl_max_conind = 1;
  925. bind_req->dl_service_mode = DL_CLDLS;
  926. bind_req->dl_conn_mgmt = 0;
  927. bind_req->dl_xidtest_flg = 0;
  928. put_ctrl(fd, sizeof(dl_bind_req_t), 0);
  929. get_msg(fd);
  930. if (GOT_ERR == check_ctrl(DL_BIND_ACK)) {
  931. printf("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n", strerror(errno));
  932. exit(STATE_UNKNOWN);
  933. }
  934. bcopy((u_char *)bind_ack + bind_ack->dl_addr_offset, addr,
  935. bind_ack->dl_addr_length);
  936. return 0;
  937. }
  938. /***********************************************************************
  939. * interface:
  940. * function mac_addr_dlpi - get the mac address of the interface with
  941. * type dev (eg lnc, hme) and unit (0, 1 ..)
  942. *
  943. * parameter: addr: an array of six bytes, has to be allocated by the caller
  944. *
  945. * return: 0 if OK, -1 if the address could not be determined
  946. *
  947. *
  948. ***********************************************************************/
  949. long mac_addr_dlpi( const char *dev, int unit, u_char *addr) {
  950. int fd;
  951. u_char mac_addr[25];
  952. if (GOT_ERR != dl_open(dev, unit, &fd)) {
  953. if (GOT_ERR != dl_bind(fd, INSAP, mac_addr)) {
  954. bcopy( mac_addr, addr, 6);
  955. return 0;
  956. }
  957. }
  958. close(fd);
  959. return -1;
  960. }
  961. #endif