forked from sem-hub/dhcprelya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_plugin.c
596 lines (541 loc) · 15.2 KB
/
log_plugin.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/* Copyright (c) 2007-2017 Sergey Matveychuk Yandex, LLC. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. 2.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. 4. Neither the name
* of the company nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#include <netinet/in.h>
#include "dhcprelya.h"
#include "dhcp_options.h"
#define BITS8 0
#define BITS32 1
#define SPERW (7 * 24 * 3600)
#define SPERD (24 * 3600)
#define SPERH (3600)
#define SPERM (60)
static unsigned detailed = 0, print_only_incoming = 0;
int
log_plugin_init(plugin_options_head_t *options_head)
{
struct plugin_options *opts, *opts_tmp;
char *p;
SLIST_FOREACH_SAFE(opts, options_head, next, opts_tmp) {
if ((p = strchr(opts->option_line, '=')) == NULL) {
logd(LOG_ERR, "log_plugin: Syntax error at line: %s", opts->option_line);
return 0;
}
*p = '\0';
p++;
if (strcasecmp(opts->option_line, "detailed") == 0) {
if ((detailed = get_bool_value(p)) == -1) {
logd(LOG_ERR, "log_plugin: Syntax error at line: %s", opts->option_line);
return 0;
}
if (detailed)
logd(LOG_DEBUG, "log_plugin: Detailed: on");
} else if (strcasecmp(opts->option_line, "print_only_incoming") == 0) {
if ((print_only_incoming = get_bool_value(p)) == -1) {
logd(LOG_ERR, "log_plugin: Syntax error at line: %s", opts->option_line);
return 0;
}
if (print_only_incoming)
logd(LOG_DEBUG, "log_plugin: Print only incoming: on");
} else {
logd(LOG_ERR, "log_plugin: Unknown option at line: %s", opts->option_line);
return 0;
}
free(opts->option_line);
SLIST_REMOVE(options_head, opts, plugin_options, next);
free(opts);
}
return 1;
}
void
log_plugin_get_time(char *buf)
{
struct timeval tv;
struct timezone tz;
struct tm tm;
gettimeofday(&tv, &tz);
localtime_r((const time_t *)&tv.tv_sec, &tm);
sprintf(buf, "%02d:%02d:%02d.%06lu",
tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec);
}
/* print the data as a hex-list, with the translation into ascii behind it */
void
printHexString(uint8_t *data, int len)
{
int i, j, k;
char c;
for (i = 0; i <= len / 8; i++) {
for (j = 0; j < 8; j++) {
if (i * 8 + j >= len)
break;
printf("%02x", data[i * 8 + j]);
}
for (k = j; k < 8; k++)
printf(" ");
printf(" ");
for (j = 0; j < 8; j++) {
c = data[i * 8 + j];
if (i * 8 + j >= len)
break;
printf("%c", isprint(c) ? c : '.');
}
if (i * 8 + j < len)
printf("\n\t\t\t\t\t ");
}
}
/* print the data as a hex-list, without the translation into ascii behind it */
void
printHex(uint8_t *data, int len)
{
int i, j;
for (i = 0; i <= len / 8; i++) {
for (j = 0; j < 8; j++) {
if (i * 8 + j >= len)
break;
printf("%02x", data[i * 8 + j]);
}
if (i * 8 + j < len)
printf("\n\t\t\t\t\t ");
}
}
/* print the data as a hex-list seperated by colons */
void
printHexColon(uint8_t *data, int len)
{
int i;
for (i = 0; i < len; i++) {
if (i != 0)
printf(":");
printf("%02x", data[i]);
}
}
const char*
print_tok(const struct tok *t, uint8_t v)
{
while (t->s != NULL) {
if (t->v == v)
return t->s;
t++;
}
return "unknown";
}
void
print_word(uint8_t *data)
{
printf("%d", (data[0] << 8) + data[1]);
}
/* print the data as a 8 and 32 bits time-value */
void
print_time(uint8_t *data, int bits)
{
int t;
if (bits == BITS8)
t = data[0];
else
t = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
printf("%d (", t);
if (t > SPERW) {
printf("%dw", t / (SPERW));
t %= SPERW;
}
if (t > SPERD) {
printf("%dd", t / (SPERD));
t %= SPERD;
}
if (t > SPERH) {
printf("%dh", t / (SPERH));
t %= SPERH;
}
if (t > SPERM) {
printf("%dm", t / (SPERM));
t %= SPERM;
}
if (t > 0)
printf("%ds", t);
printf(")");
}
void
printReqParmList(uint8_t *data, int len)
{
int i;
for (i = 0; i < len; i++) {
printf("%3d (%s)\n", data[i], dhcp_options[data[i]]);
printf("\t\t\t\t\t ");
}
}
void
print_dhcp_packet(struct dhcp_packet *dhcp, int data_len)
{
char buf[1024];
uint8_t *data;
int i, j;
puts("---------------------------------------------------------------------------");
printf("op: %d (%s)\n", dhcp->op,
dhcp->op == 1 ? "BOOTREQUEST" :
(dhcp->op == 2 ? "BOOTREPLY" : "illegal"));
printf("htype: %d (%s)\n", dhcp->htype, dhcp->htype == 1 ? "Ethernet" :
"");
printf("hlen: %d\n", dhcp->hlen);
printf("hops: %d\n", dhcp->hops);
printf("xid: %s\n", print_xid(dhcp->xid, buf));
printf("secs: %d\n", dhcp->secs);
printf("flags: 0x%X\n", dhcp->flags);
printf("ciaddr: %s\n", inet_ntop(AF_INET, &dhcp->ciaddr.s_addr,
buf, sizeof(buf)));
printf("yiaddr: %s\n", inet_ntop(AF_INET, &dhcp->yiaddr.s_addr,
buf, sizeof(buf)));
printf("siaddr: %s\n", inet_ntop(AF_INET, &dhcp->siaddr.s_addr,
buf, sizeof(buf)));
printf("giaddr: %s\n", inet_ntop(AF_INET, &dhcp->giaddr.s_addr,
buf, sizeof(buf)));
printf("chaddr: %s\n", ether_ntoa_r((struct ether_addr*)dhcp->chaddr,
buf));
printf("sname: %s.\n", dhcp->sname);
printf("file: %s.\n", dhcp->file);
data = (uint8_t *)dhcp;
/* print options */
j = DHCP_FIXED_NON_UDP;
/* XXX print cookie */
j += DHCP_COOKIE_LEN;
while (j < data_len && data[j] != 255) {
printf("OPTION: %3d (%3d) %-26s", data[j], data[j + 1],
dhcp_options[data[j]]);
switch (data[j]) {
default:
printHexString(data + j + 2, data[j + 1]);
break;
case 0: /* padding */
break;
case 1: /* Subnetmask */
case 3: /* Routers */
case 16: /* Swap server */
case 28: /* Broadcast address */
case 32: /* Router solicitation */
case 50: /* Requested IP address */
case 54: /* Server identifier */
printf("%s", inet_ntop(AF_INET, data + j + 2,
buf, sizeof(buf)));
break;
case 12: /* Hostname */
case 14: /* Merit dump file */
case 15: /* Domain name */
case 17: /* Root Path */
case 18: /* Extensions path */
case 40: /* NIS domain */
case 56: /* Message */
case 62: /* Netware/IP domain name */
case 64: /* NIS+ domain */
case 66: /* TFTP server name */
case 67: /* bootfile name */
case 60: /* Vendor class identifier */
case 86: /* NDS Tree name */
case 87: /* NDS context */
case 252: /* MSFT - WinSock Proxy Auto Detect */
strlcpy(buf, (char *)&data[j + 2], data[j + 1]+1);
buf[data[j + 1]] = 0;
printf("%s", buf);
break;
case 4: /* Time servers */
case 5: /* Name servers */
case 6: /* DNS server */
case 7: /* Log server */
case 8: /* Cookie server */
case 9: /* LPR server */
case 10: /* Impress server */
case 11: /* Resource location server */
case 41: /* NIS servers */
case 42: /* NTP servers */
case 44: /* NetBIOS name server */
case 45: /* NetBIOS datagram distribution server */
case 48: /* X Window System font server */
case 49: /* X Window System display server */
case 65: /* NIS+ servers */
case 68: /* Mobile IP home agent */
case 69: /* SMTP server */
case 70: /* POP3 server */
case 71: /* NNTP server */
case 72: /* WWW server */
case 73: /* Finger server */
case 74: /* IRC server */
case 75: /* StreetTalk server */
case 76: /* StreetTalk directory assistance server */
case 85: /* NDS server */
case 150: /* CiscoCallManagerTFTP */
for (i = 0; i < data[j + 1] / 4; i++) {
if (i != 0)
printf(",");
printf("%s", inet_ntop(AF_INET,
data + j + 2 + i * 4,
buf, sizeof(buf)));
}
break;
case 21: /* Policy filter */
for (i = 0; i < data[j + 1] / 8; i++) {
if (i != 0)
printf(",");
printf("%s/%s",
inet_ntop(AF_INET,
data + j + 2 + i * 8,
buf, sizeof(buf)),
inet_ntop(AF_INET,
(data + j + 2 + i * 8) + 4,
buf+16, sizeof(buf)-16)
);
}
break;
case 33: /* Static route */
for (i = 0; i < data[j + 1] / 8; i++) {
if (i != 0)
printf(",");
printf("%s %s",
inet_ntop(AF_INET,
data + j + 2 + i * 8,
buf, sizeof(buf)),
inet_ntop(AF_INET,
(data + j + 2 + i * 8) + 4,
buf+16, sizeof(buf)-16)
);
}
break;
case 25: /* Path MTU plateau table */
for (i = 0; i < data[j + 1] / 2; i++) {
if (i != 0)
printf(",");
print_word(data + j + 2 + i * 2);
}
break;
case 13: /* bootfile size */
case 22: /* Maximum datagram reassembly size */
case 26: /* Interface MTU */
case 57: /* Maximum DHCP message size */
print_word(data + j + 2);
break;
case 19: /* IP forwarding enabled/disable */
case 20: /* Non-local source routing */
case 27: /* All subnets local */
case 29: /* Perform mask discovery */
case 30: /* Mask supplier */
case 31: /* Perform router discovery */
case 34: /* Trailer encapsulation */
case 39: /* TCP keepalive garbage */
printf("%d (%s)", data[j + 2], enabledisable[data[j + 2]]);
break;
case 23: /* Default IP TTL */
print_time(data + j + 2, BITS8);
break;
case 37: /* TCP default TTL */
printf("%d", data[j + 2]);
break;
case 43: /* Vendor specific info */
case 47: /* NetBIOS scope (no idea how it looks like) */
printHexString(data + j + 2, data[j + 1]);
break;
case 46: /* NetBIOS over TCP/IP node type */
printf("%d (%s)",
data[j + 2], netbios_node_type[data[j + 2]]);
break;
case 2: /* Time offset */
case 24: /* Path MTU aging timeout */
case 35: /* ARP cache timeout */
case 38: /* TCP keepalive interval */
case 51: /* IP address leasetime */
case 58: /* T1 */
case 59: /* T2 */
print_time(data + j + 2, BITS32);
break;
case 36: /* Ethernet encapsulation */
printf("%d (%s)",
data[j + 2],
data[j + 2] > sizeof(ethernet_encapsulation) ?
"*wrong value*" :
ethernet_encapsulation[data[j + 2]]);
break;
case 52: /* Option overload */
printf("%d (%s)",
data[j + 2],
data[j + 2] > sizeof(option_overload) ?
"*wrong value*" :
option_overload[data[j + 2]]);
break;
case 53: /* DHCP message type */
printf("%d (%s)",
data[j + 2],
data[j + 2] > sizeof(dhcp_message_types) ?
"*wrong value*" :
dhcp_message_types[data[j + 2]]);
break;
case 55: /* Parameter Request List */
printReqParmList(data + j + 2, data[j + 1]);
break;
case 63: /* Netware/IP domain information */
printHex(data + j + 2, data[j + 1]);
break;
case 61: /* Client identifier */
if (data[j + 2] != 0) {
printf("%s ", print_tok(arp2str, data[j + 2]));
printHexColon(data + j + 3, data[j + 1] - 1);
} else {
strlcpy(buf, (char *)&data[j + 3], data[j + 1]-1);
buf[data[j + 1] - 1] = 0;
printf("%s", buf);
}
break;
case 81: /* Client FQDN */
printf("%d", data[j + 2]);
printf("-");
printf("%d", data[j + 3]);
printf("-");
printf("%d", data[j + 4]);
printf(" ");
strlcpy(buf, (char *)&data[j + 5], data[j + 1] - 2);
buf[data[j + 1] - 3] = 0;
printf("%s", buf);
break;
case 82: /* Relay Agent Information */
for (i = j + 2; i < j + data[j + 1];) {
printf("\n%-5s subopt: %3d (%3d) %-19s ", " ",
data[i], data[i + 1],
data[i] > sizeof(relayagent_suboptions) ?
"*wrong value*" :
relayagent_suboptions[data[i]]);
if (i + data[i + 1] > j + data[j + 1]) {
printf("*MALFORMED -- TOO LARGE*\n");
break;
}
printHexString(data + i + 2, data[i + 1]);
i += data[i + 1] + 2;
}
break;
}
printf("\n");
if (data[j] == 0) /* padding */
j++;
else
j += data[j + 1] + 2;
}
puts("---------------------------------------------------------------------------\n");
}
int
log_plugin_client_request(const struct interface *intf,
struct dhcp_packet *dhcp, struct packet_headers *headers)
{
char buf[18 * 2 + 11], timebuf[16], logbuf[256];
size_t dhcp_len;
if (debug) {
dhcp_len = get_dhcp_len(dhcp);
log_plugin_get_time(timebuf);
sprintf(logbuf, "%s request on %s XID: %s %s -> %s (%zu bytes)", timebuf,
intf->name,
print_xid(dhcp->xid, buf),
ether_ntoa_r((struct ether_addr*)headers->eh.ether_shost, buf+11),
ether_ntoa_r((struct ether_addr*)headers->eh.ether_dhost, buf+29),
dhcp_len
);
puts(logbuf);
if (detailed)
print_dhcp_packet(dhcp, dhcp_len);
}
return 1;
}
int
log_plugin_send_to_server(const struct sockaddr_in *server,
const struct interface *input_intf, struct dhcp_packet *dhcp)
{
char buf[16 + 11], timebuf[16], logbuf[256];
size_t dhcp_len;
if (debug && !print_only_incoming) {
dhcp_len = get_dhcp_len(dhcp);
log_plugin_get_time(timebuf);
sprintf(logbuf, "%s send XID: %s to server %s (%zu bytes)", timebuf,
print_xid(dhcp->xid, buf),
inet_ntop(AF_INET, &server->sin_addr.s_addr,
buf+11, sizeof(buf)-11),
dhcp_len
);
puts(logbuf);
if (detailed)
print_dhcp_packet(dhcp, dhcp_len);
}
return 1;
}
int
log_plugin_server_answer(const struct sockaddr_in *server,
struct dhcp_packet *dhcp)
{
char buf[16 + 11], timebuf[16], logbuf[256];
size_t dhcp_len;
if (debug) {
dhcp_len = get_dhcp_len(dhcp);
log_plugin_get_time(timebuf);
sprintf(logbuf, "%s reply from server (%s) XID: %s (%zu bytes)", timebuf,
inet_ntop(AF_INET, &server->sin_addr.s_addr,
buf, sizeof(buf)),
print_xid(dhcp->xid, buf + 16),
dhcp_len
);
puts(logbuf);
if (detailed)
print_dhcp_packet(dhcp, dhcp_len);
}
return 1;
}
int
log_plugin_send_to_client(const struct sockaddr_in *server,
const struct interface *intf,
struct dhcp_packet *dhcp, struct packet_headers *headers)
{
char buf[11 + 16 + 18], timebuf[16], logbuf[256];
size_t dhcp_len;
if (debug && !print_only_incoming) {
dhcp_len = get_dhcp_len(dhcp);
log_plugin_get_time(timebuf);
sprintf(logbuf, "%s (from %s) send XID: %s for %s via %s (%zu bytes)", timebuf,
inet_ntop(AF_INET, &server->sin_addr.s_addr,
buf, sizeof(buf)),
print_xid(dhcp->xid, buf + 16),
ether_ntoa_r((struct ether_addr*)dhcp->chaddr, buf+27),
intf->name, dhcp_len
);
puts(logbuf);
if (detailed)
print_dhcp_packet(dhcp, dhcp_len);
}
return 1;
}
struct plugin_data log_plugin = {
"log",
log_plugin_init,
NULL, /* no destroy() function */
log_plugin_client_request,
log_plugin_send_to_server,
log_plugin_server_answer,
log_plugin_send_to_client
};