-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcheck_dhcp.c
407 lines (352 loc) · 11.8 KB
/
check_dhcp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/***
* Monitoring Plugin - check_dhcp.c
**
*
* check_dhcp - Check dhcp server.
*
* Copyright (C) 2012 Marius Rieder <marius.rieder@durchmesser.ch>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id$
*/
const char *progname = "check_dhcp";
const char *progdesc = "Check dhcp server.";
const char *progvers = "0.1";
const char *progcopy = "2012";
const char *progauth = "Marius Rieder <marius.rieder@durchmesser.ch>";
const char *progusage = "[-i <DEV>] [-H <HOSTNAME> [--unicast]] [--mac <MAC>]";
/* MP Includes */
#include "mp_common.h"
#include "mp_net.h"
#include "dhcp_utils.h"
/* Default Includes */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <inttypes.h>
#include <netdb.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <time.h>
/* Global Vars */
const char *hostname = NULL;
struct sockaddr_in dhcpd;
struct sockaddr_in dhcpc;
uint8_t mac[16];
int maclen = 0;
const char *interface = "eth0";
int unicast = 0;
in_addr_t request = 0;
uint8_t type = 0;
uint32_t xid;
/* Function prototype */
int dhcp_setup();
int main (int argc, char **argv) {
/* Local Vars */
struct ifreq ifr;
int sock;
ssize_t s;
uint8_t val;
struct dhcp_pkt *pkt;
struct dhcp_pkt_opt *opt;
struct hostent *hostent = NULL;
//uid_t uid;
/* Set signal handling and alarm */
if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR)
critical("Setup SIGALRM trap failed!");
/* Process check arguments */
if (process_arguments(argc, argv) != OK)
unknown("Parsing arguments failed!");
// Need to be root
mp_noneroot_die();
// Check hostname
if (hostname) {
hostent = gethostbyname(hostname);
if (!hostent)
unknown("Host %s not found", hostname);
}
/* Start plugin timeout */
alarm(mp_timeout);
sock = dhcp_setup();
if (sock < 0)
warning("socket setup failed");
// Read MAC of interface if not specified
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
ioctl(sock, SIOCGIFHWADDR, &ifr);
if (maclen == 0) {
bzero(mac, 16);
memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
maclen = 6;
}
// Create XID
srandom(time(NULL));
xid = (uint32_t)random();
// Create Request
pkt = mp_malloc(sizeof(struct dhcp_pkt));
bzero(pkt, sizeof(struct dhcp_pkt));
pkt->op = BOOTREQUEST;
pkt->htype = 1;
pkt->hlen = maclen;
pkt->hops = 1;
pkt->xid = xid;
pkt->secs = 0;
pkt->flags = unicast ? 0x00: 0x80;
memcpy(pkt->chaddr, mac, 16);
val = DHCPDISCOVER;
if (request) {
val = DHCPREQUEST;
mp_dhcp_pkt_opt(pkt, DHCPOPT_RequestIP, 4, (char *)&request);
}
mp_dhcp_pkt_opt(pkt, DHCPOPT_MessageType, 1, (char *)&val);
mp_dhcp_pkt_opt(pkt, DHCPOPT_Class, strlen(progname), (char *)progname);
if (unicast) {
ioctl(sock, SIOCGIFADDR, &ifr);
memcpy(&pkt->giaddr, &((struct sockaddr_in *)(void *)&ifr.ifr_addr)->sin_addr, 4);
}
s = mp_dhcp_send(sock, pkt, &dhcpc, &dhcpd, interface, unicast);
if (s < 0)
warning("DHCP packet not sent");
if (hostname)
hostent = gethostbyname(hostname);
int ansers = 0;
while ((pkt = mp_dhcp_recv(sock, xid))) {
ansers++;
if (hostname && hostent) {
opt = mp_dhcp_pkt_getopt(pkt, DHCPOPT_ServerId);
if (opt && hostent) {
if (memcmp(hostent->h_addr, &(opt->data.inaddr), 4) != 0)
continue;
} else {
if (strcmp(hostname, pkt->sname) != 0)
continue;
}
opt = mp_dhcp_pkt_getopt(pkt, DHCPOPT_MessageType);
if (!opt) {
continue;
} else if (opt->data.uint8 == type) {
switch (type) {
case DHCPOFFER:
ok("Got offer for %s from %s", inet_ntoa(pkt->yiaddr), pkt->sname);
break;
case DHCPACK:
ok("Got ACK from %s", pkt->sname);
break;
case DHCPNAK:
ok("Got NAK from %s", pkt->sname);
break;
};
} else {
switch (opt->data.uint8) {
case DHCPOFFER:
ok("Got offer for %s from %s", inet_ntoa(pkt->yiaddr), pkt->sname);
break;
case DHCPACK:
ok("Got ACK from %s", pkt->sname);
break;
case DHCPNAK:
ok("Got NAK from %s", pkt->sname);
break;
};
}
} else {
opt = mp_dhcp_pkt_getopt(pkt, DHCPOPT_MessageType);
if (!opt) {
continue;
} else if (opt->data.uint8 == type) {
switch (type) {
case DHCPOFFER:
ok("Got offer for %s from %s", inet_ntoa(pkt->yiaddr), pkt->sname);
break;
case DHCPACK:
ok("Got ACK from %s", pkt->sname);
break;
case DHCPNAK:
ok("Got NAK from %s", pkt->sname);
break;
};
}
}
free(pkt->opts);
free(pkt);
}
if (hostname) {
critical("Got no DHCP reply from %s", hostname);
} else {
switch (type) {
case DHCPOFFER:
ok("No DHCP Offer received.");
break;
case DHCPACK:
ok("No DHCP Ack received.");
break;
case DHCPNAK:
ok("No DHCP Nak received.");
break;
}
}
critical("You should never reach this point.");
}
int dhcp_setup() {
struct servent *servent;
struct hostent *hostent;
struct ifreq ifr;
int sock = -1;
int one = 1;
/* dhcpd sockaddr */
dhcpd.sin_family = AF_INET;
if (unicast) {
if ((hostent = gethostbyname(hostname)) == NULL) {
perror("gethostbyname");
return -1;
}
memcpy(&dhcpd.sin_addr.s_addr, hostent->h_addr, hostent->h_length);
} else {
dhcpd.sin_addr.s_addr = INADDR_BROADCAST;
}
servent = getservbyname("bootps", 0);
dhcpd.sin_port = servent->s_port;
/* dhcpc sockaddr */
dhcpc.sin_family = AF_INET;
dhcpc.sin_addr.s_addr = INADDR_ANY;
if (!unicast) {
servent = getservbyname("bootpc", 0);
}
dhcpc.sin_port = servent->s_port;
bzero(dhcpc.sin_zero, sizeof(dhcpc.sin_zero));
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == -1)
critical("Can't open udp/ip socket");
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof one) < 0)
critical("Set socket option failed");
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&one, sizeof one) < 0)
critical("Set socket to broadcast mode failed");
bzero(&ifr, sizeof(ifr));
strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0)
critical("Bind socket to '%s' failed", interface);
if (bind(sock, (struct sockaddr *)&dhcpc, sizeof(dhcpc)) < 0) {
perror("bind");
critical("Socket bind failed.");
}
return sock;
}
int process_arguments (int argc, char **argv) {
int c;
int option = 0;
static struct option longopts[] = {
MP_LONGOPTS_DEFAULT,
MP_LONGOPTS_HOST,
{"interface", required_argument, NULL, (int)'i'},
{"mac", required_argument, NULL, (int)'m'},
{"unicast", no_argument, NULL, (int)'u'},
{"broadcast", no_argument, NULL, (int)'b'},
{"request", required_argument, NULL, (int)'r'},
{"type", required_argument, NULL, (int)'T'},
MP_LONGOPTS_END
};
while (1) {
c = mp_getopt(&argc, &argv, MP_OPTSTR_DEFAULT"H:i:m:ubr:T:", longopts, &option);
if (c == -1 || c == EOF)
break;
switch(c) {
/* Hostname opt */
case 'H':
getopt_host(optarg, &hostname);
break;
case 'i':
interface = optarg;
break;
case 'm': {
memset(&mac,0,sizeof(mac));
maclen = sscanf(optarg, "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8,
&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5],
&mac[6],&mac[7],&mac[8],&mac[9],&mac[10],&mac[11],
&mac[12],&mac[13],&mac[14],&mac[15]);
break;
}
case 'u':
unicast = 1;
break;
case 'b':
unicast = 0;
break;
case 'r':
if (is_hostaddr(optarg)) {
request = inet_addr(optarg);
} else {
usage("Request option must be a ip address");
}
break;
case 'T':
if (strcasecmp(optarg, "offer") == 0) {
type = DHCPOFFER;
} else if (strcasecmp(optarg, "ack") == 0) {
type = DHCPACK;
} else if (strcasecmp(optarg, "nak") == 0) {
type = DHCPNAK;
} else {
usage("Unknown type '%s'", optarg);
}
break;
}
}
if (unicast && !hostname)
usage("Unicast require hostname");
if (type == 0) {
type = DHCPOFFER;
if (request)
type = DHCPACK;
}
return(OK);
}
void print_help (void) {
print_revision();
print_copyright();
printf("\n");
printf("Check description: %s", progdesc);
printf("\n\n");
printf(" Mode 1:\n");
printf(" Run without hostname to ask broadcast and accept first answer.\n\n");
printf(" Mode 2:\n");
printf(" Run with hostname to ask broadcast and accept only answers from given host.\n\n");
printf(" Mode 3:\n");
printf(" Run with hostname and unicast flag. Act as forwarder/redirector and ask\n");
printf(" the server directly in the name of a client.");
printf("\n\n");
print_usage();
print_help_default();
print_help_host();
printf(" -i, --interface=DEV\n");
printf(" Network interface to use. (Default: eth0)\n");
printf(" -m, --mac=MAC\n");
printf(" MAC Address to use. (Default: from interface)\n");
printf(" -u, --unicast\n");
printf(" Run in unicast mode.\n");
printf(" -b, --broadcast\n");
printf(" Run in broadcast mode.\n");
}
/* vim: set ts=4 sw=4 et syn=c : */