summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/lib/save_v1trap.c
blob: 78671c79c7684fe384cfadf6764c2ddabc7cbe9a (plain)
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
#include "ipf.h"
#include "netinet/ipl.h"
#include "ipmon.h"
#include <ctype.h>

#define	IPF_ENTERPRISE	9932
/*
 * Enterprise number OID:
 * 1.3.6.1.4.1.9932
 */
static u_char ipf_enterprise[] = { 6, 7, 0x2b, 6, 1, 4, 1, 0xcd, 0x4c };
static u_char ipf_trap0_1[] = { 6, 10, 0x2b, 6, 1, 4, 1, 0xcd, 0x4c, 1, 1, 1 };
static u_char ipf_trap0_2[] = { 6, 10, 0x2b, 6, 1, 4, 1, 0xcd, 0x4c, 1, 1, 2 };

static int writeint __P((u_char *, int));
static int writelength __P((u_char *, u_int));
static int maketrap_v1 __P((char *, u_char *, int, u_char *, int, u_32_t,
			    time_t));
static void snmpv1_destroy __P((void *));
static void *snmpv1_dup __P((void *));
static int snmpv1_match __P((void *, void *));
static void *snmpv1_parse __P((char **));
static void snmpv1_print __P((void *));
static int snmpv1_send __P((void *, ipmon_msg_t *));

typedef struct snmpv1_opts_s {
	char			*community;
	int			fd;
	int			v6;
	int			ref;
#ifdef USE_INET6
	struct sockaddr_in6	sin6;
#endif
	struct sockaddr_in	sin;
} snmpv1_opts_t;

ipmon_saver_t snmpv1saver = {
	"snmpv1",
	snmpv1_destroy,
	snmpv1_dup,		/* dup */
	snmpv1_match,		/* match */
	snmpv1_parse,
	snmpv1_print,
	snmpv1_send
};


static int
snmpv1_match(ctx1, ctx2)
	void *ctx1, *ctx2;
{
	snmpv1_opts_t *s1 = ctx1, *s2 = ctx2;

	if (s1->v6 != s2->v6)
		return 1;

	if (strcmp(s1->community, s2->community))
		return 1;

#ifdef USE_INET6
	if (s1->v6 == 1) {
		if (memcmp(&s1->sin6, &s2->sin6, sizeof(s1->sin6)))
			return 1;
	} else
#endif
	{
		if (memcmp(&s1->sin, &s2->sin, sizeof(s1->sin)))
			return 1;
	}

	return 0;
}


static void *
snmpv1_dup(ctx)
	void *ctx;
{
	snmpv1_opts_t *s = ctx;

	s->ref++;
	return s;
}


static void
snmpv1_print(ctx)
	void *ctx;
{
	snmpv1_opts_t *snmpv1 = ctx;

	printf("%s ", snmpv1->community);
#ifdef USE_INET6
	if (snmpv1->v6 == 1) {
		char buf[80];

		printf("%s", inet_ntop(AF_INET6, &snmpv1->sin6.sin6_addr, buf,
				       sizeof(snmpv1->sin6.sin6_addr)));
	} else
#endif
	{
		printf("%s", inet_ntoa(snmpv1->sin.sin_addr));
	}
}


static void *
snmpv1_parse(char **strings)
{
	snmpv1_opts_t *ctx;
	int result;
	char *str;
	char *s;

	if (strings[0] == NULL || strings[0][0] == '\0')
		return NULL;

	if (strchr(*strings, ' ') == NULL)
		return NULL;

	str = strdup(*strings);

	ctx = calloc(1, sizeof(*ctx));
	if (ctx == NULL)
		return NULL;

	ctx->fd = -1;

	s = strchr(str, ' ');
	*s++ = '\0';
	ctx->community = str;

	while (ISSPACE(*s))
		s++;
	if (!*s) {
		free(str);
		free(ctx);
		return NULL;
	}

#ifdef USE_INET6
	if (strchr(s, ':') == NULL) {
		result = inet_pton(AF_INET, s, &ctx->sin.sin_addr);
		if (result == 1) {
			ctx->fd = socket(AF_INET, SOCK_DGRAM, 0);
			if (ctx->fd >= 0) {
				ctx->sin.sin_family = AF_INET;
				ctx->sin.sin_port = htons(162);
				if (connect(ctx->fd,
					    (struct sockaddr *)&ctx->sin,
					    sizeof(ctx->sin)) != 0) {
						snmpv1_destroy(ctx);
						return NULL;
				}
			}
		}
	} else {
		result = inet_pton(AF_INET6, s, &ctx->sin6.sin6_addr);
		if (result == 1) {
			ctx->v6 = 1;
			ctx->fd = socket(AF_INET6, SOCK_DGRAM, 0);
			if (ctx->fd >= 0) {
				ctx->sin6.sin6_family = AF_INET6;
				ctx->sin6.sin6_port = htons(162);
				if (connect(ctx->fd,
					    (struct sockaddr *)&ctx->sin6,
					    sizeof(ctx->sin6)) != 0) {
						snmpv1_destroy(ctx);
						return NULL;
				}
			}
		}
	}
#else
	result = inet_aton(s, &ctx->sin.sin_addr);
	if (result == 1) {
		ctx->fd = socket(AF_INET, SOCK_DGRAM, 0);
		if (ctx->fd >= 0) {
			ctx->sin.sin_family = AF_INET;
			ctx->sin.sin_port = htons(162);
			if (connect(ctx->fd, (struct sockaddr *)&ctx->sin,
				    sizeof(ctx->sin)) != 0) {
					snmpv1_destroy(ctx);
					return NULL;
			}
		}
	}
#endif

	if (result != 1) {
		free(str);
		free(ctx);
		return NULL;
	}

	ctx->ref = 1;

	return ctx;
}


static void
snmpv1_destroy(ctx)
	void *ctx;
{
	snmpv1_opts_t *v1 = ctx;

	v1->ref--;
	if (v1->ref > 0)
		return;

	if (v1->community)
		free(v1->community);
	if (v1->fd >= 0)
		close(v1->fd);
	free(v1);
}


static int
snmpv1_send(ctx, msg)
	void *ctx;
	ipmon_msg_t *msg;
{
	snmpv1_opts_t *v1 = ctx;

	return sendtrap_v1_0(v1->fd, v1->community,
			     msg->imm_msg, msg->imm_msglen, msg->imm_when);
}

static char def_community[] = "public";	/* ublic */

static int
writelength(buffer, value)
	u_char *buffer;
	u_int value;
{
	u_int n = htonl(value);
	int len;

	if (value < 128) {
		*buffer = value;
		return 1;
	}
	if (value > 0xffffff)
		len = 4;
	else if (value > 0xffff)
		len = 3;
	else if (value > 0xff)
		len = 2;
	else
		len = 1;

	*buffer = 0x80 | len;

	bcopy((u_char *)&n + 4 - len, buffer + 1, len);

	return len + 1;
}


static int
writeint(buffer, value)
	u_char *buffer;
	int value;
{
	u_char *s = buffer;
	u_int n = value;

	if (value == 0) {
		*buffer = 0;
		return 1;
	}

	if (n >  4194304) {
		*s++ = 0x80 | (n / 4194304);
		n -= 4194304 * (n / 4194304);
	}
	if (n >  32768) {
		*s++ = 0x80 | (n / 32768);
		n -= 32768 * (n / 327678);
	}
	if (n > 128) {
		*s++ = 0x80 | (n / 128);
		n -= (n / 128) * 128;
	}
	*s++ = (u_char)n;

	return s - buffer;
}



/*
 * First style of traps is:
 * 1.3.6.1.4.1.9932.1.1
 */
static int
maketrap_v1(community, buffer, bufsize, msg, msglen, ipaddr, when)
	char *community;
	u_char *buffer;
	int bufsize;
	u_char *msg;
	int msglen;
	u_32_t ipaddr;
	time_t when;
{
	u_char *s = buffer, *t, *pdulen, *varlen;
	int basesize = 73;
	u_short len;
	int trapmsglen;
	int pdulensz;
	int varlensz;
	int baselensz;
	int n;

	if (community == NULL || *community == '\0')
		community = def_community;
	basesize += strlen(community) + msglen;

	if (basesize + 8 > bufsize)
		return 0;

	memset(buffer, 0xff, bufsize);
	*s++ = 0x30;		/* Sequence */
	if (basesize - 1 >= 128) {
		baselensz = 2;
		basesize++;
	} else {
		baselensz = 1;
	}
	s += baselensz;
	*s++ = 0x02;		/* Integer32 */
	*s++ = 0x01;		/* length 1 */
	*s++ = 0x00;		/* version 1 */
	*s++ = 0x04;		/* octet string */
	*s++ = strlen(community);		/* length of "public" */
	bcopy(community, s, s[-1]);
	s += s[-1];
	*s++ = 0xA4;		/* PDU(4) */
	pdulen = s++;
	if (basesize - (s - buffer) >= 128) {
		pdulensz = 2;
		basesize++;
		s++;
	} else {
		pdulensz = 1;
	}

	/* enterprise */
	bcopy(ipf_enterprise, s, sizeof(ipf_enterprise));
	s += sizeof(ipf_enterprise);

	/* Agent address */
	*s++ = 0x40;
	*s++ = 0x4;
	bcopy(&ipaddr, s, 4);
	s += 4;

	/* Generic Trap code */
	*s++ = 0x2;
	n = writeint(s + 1, 6);
	if (n == 0)
		return 0;
	*s = n;
	s += n + 1;

	/* Specific Trap code */
	*s++ = 0x2;
	n = writeint(s + 1, 0);
	if (n == 0)
		return 0;
	*s = n;
	s += n + 1;

	/* Time stamp */
	*s++ = 0x43;			/* TimeTicks */
	*s++ = 0x04;			/* TimeTicks */
	s[0] = when >> 24;
	s[1] = when >> 16;
	s[2] = when >> 8;
	s[3] = when & 0xff;
	s += 4;

	/*
	 * The trap0 message is "ipfilter_version" followed by the message
	 */
	*s++ = 0x30;
	varlen = s;
	if (basesize - (s - buffer) >= 128) {
		varlensz = 2;
		basesize++;
	} else {
		varlensz = 1;
	}
	s += varlensz;

	*s++ = 0x30;
	t = s + 1;
	bcopy(ipf_trap0_1, t, sizeof(ipf_trap0_1));
	t += sizeof(ipf_trap0_1);

	*t++ = 0x2;		/* Integer */
	n = writeint(t + 1, IPFILTER_VERSION);
	*t = n;
	t += n + 1;

	len = t - s - 1;
	writelength(s, len);

	s = t;
	*s++ = 0x30;
	if (basesize - (s - buffer) >= 128) {
		trapmsglen = 2;
		basesize++;
	} else {
		trapmsglen = 1;
	}
	t = s + trapmsglen;
	bcopy(ipf_trap0_2, t, sizeof(ipf_trap0_2));
	t += sizeof(ipf_trap0_2);

	*t++ = 0x4;		/* Octet string */
	n = writelength(t, msglen);
	t += n;
	bcopy(msg, t, msglen);
	t += msglen;

	len = t - s - trapmsglen;
	writelength(s, len);

	len = t - varlen - varlensz;
	writelength(varlen, len);		/* pdu length */

	len = t - pdulen - pdulensz;
	writelength(pdulen, len);		/* pdu length */

	len = t - buffer - baselensz - 1;
	writelength(buffer + 1, len);	/* length of trap */

	return t - buffer;
}


int
sendtrap_v1_0(fd, community, msg, msglen, when)
	int fd;
	char *community, *msg;
	int msglen;
	time_t when;
{

	u_char buffer[1500];
	int n;

	n = maketrap_v1(community, buffer, sizeof(buffer),
			(u_char *)msg, msglen, 0, when);
	if (n > 0) {
		return send(fd, buffer, n, 0);
	}

	return 0;
}
OpenPOWER on IntegriCloud