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