summaryrefslogtreecommitdiff
path: root/usr.sbin/snmpd/trap.c
blob: 5375418a415eab65527d246aa2ba0af6bdfbe2fc (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
/*	$OpenBSD: trap.c,v 1.22 2014/04/14 12:55:10 blambert Exp $	*/

/*
 * Copyright (c) 2008 Reyk Floeter <reyk@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/queue.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/tree.h>

#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>

#include "snmpd.h"
#include "mib.h"

extern struct snmpd	*env;

void
trap_init(void)
{
	struct ber_oid	 trapoid = OID(MIB_coldStart);

	/*
	 * Send a coldStart to notify that the daemon has been
	 * started and re-initialized.
	 */
	trap_send(&trapoid, NULL);
}

int
trap_agentx(struct agentx_handle *h, struct agentx_pdu *pdu, int *idx,
    char **varcpy, int *vcpylen)
{
	struct agentx_varbind_hdr	 vbhdr;
	struct ber_oid			 o, oid;
	struct ber_oid			 uptime = OID(MIB_sysUpTime);
	struct ber_oid			 trapoid = OID(MIB_snmpTrapOID);
	u_int32_t			 d;
	u_int64_t			 l;
	char				*str;
	int				 slen;
	struct ber_element		*a, *ber, *varbind;
	int				 x = 0, state = 0;
	int				 ret = AGENTX_ERR_NONE;
	int				 seensysuptime, seentrapoid;
	size_t				 len = 0;
	pid_t				 pid = -1;
	char				*v = NULL;

	*varcpy = NULL;
	ber = varbind = NULL;
	seensysuptime = seentrapoid = 0;

	if (pdu->hdr->flags & AGENTX_NON_DEFAULT_CONTEXT) {
		ret = AGENTX_ERR_UNSUPPORTED_CONTEXT;
		goto done;
	}

	if ((v = malloc(pdu->hdr->length)) == NULL ||
	    snmp_agentx_copy_raw(pdu, v, pdu->hdr->length) == -1) {
		ret = AGENTX_ERR_PROCESSING_ERROR;
		goto done;
	}

	while (pdu->datalen > sizeof(struct agentx_hdr)) {
		x++;

		if (snmp_agentx_read_vbhdr(pdu, &vbhdr) == -1 ||
		    snmp_agentx_read_oid(pdu, (struct snmp_oid *)&oid) == -1) {
			ret = AGENTX_ERR_PARSE_ERROR;
			goto done;
		}
		if (state < 2) {
			if (state == 0 && ber_oid_cmp(&oid, &uptime) == 0) {
				if (snmp_agentx_read_int(pdu, &d) == -1) {
					ret = AGENTX_ERR_PARSE_ERROR;
					goto done;
				}
				state = 1;
				continue;
			} else if (ber_oid_cmp(&oid, &trapoid) == 0) {
				if (snmp_agentx_read_oid(pdu,
				    (struct snmp_oid *)&o) == -1) {
					ret = AGENTX_ERR_PARSE_ERROR;
					goto done;
				}
				state = 2;
				continue;
			} else {
				ret = AGENTX_ERR_PROCESSING_ERROR;
				goto done;
			}
		}

		ber = ber_add_sequence(ber);
		if (varbind == NULL)
		varbind = ber;

		a = ber_add_oid(ber, &oid);

		switch (vbhdr.type) {
		case AGENTX_NO_SUCH_OBJECT:
		case AGENTX_NO_SUCH_INSTANCE:
		case AGENTX_END_OF_MIB_VIEW:
		case AGENTX_NULL:
			a = ber_add_null(a);
			break;

		case AGENTX_IP_ADDRESS:
		case AGENTX_OPAQUE:
		case AGENTX_OCTET_STRING:
			str = snmp_agentx_read_octetstr(pdu, &slen);
			if (str == NULL) {
				ret = AGENTX_ERR_PARSE_ERROR;
				goto done;
			}
			a = ber_add_nstring(a, str, slen);
			break;

		case AGENTX_OBJECT_IDENTIFIER:
			if (snmp_agentx_read_oid(pdu,
			    (struct snmp_oid *)&oid) == -1) {
				ret = AGENTX_ERR_PARSE_ERROR;
				goto done;
			}
			a = ber_add_oid(a, &oid);
			break;

		case AGENTX_INTEGER:
		case AGENTX_COUNTER32:
		case AGENTX_GAUGE32:
		case AGENTX_TIME_TICKS:
			if (snmp_agentx_read_int(pdu, &d) == -1) {
				ret = AGENTX_ERR_PARSE_ERROR;
				goto done;
			}
			a = ber_add_integer(a, d);
			break;

		case AGENTX_COUNTER64:
			if (snmp_agentx_read_int64(pdu, &l) == -1) {
				ret = AGENTX_ERR_PARSE_ERROR;
				goto done;
			}
			a = ber_add_integer(a, l);
			break;

		default:
			log_debug("unknown data type '%i'", vbhdr.type);
			ret = AGENTX_ERR_PARSE_ERROR;
			goto done;
		}

		/* AgentX types correspond to BER types */
		switch (vbhdr.type) {
		case BER_TYPE_INTEGER:
		case BER_TYPE_BITSTRING:
		case BER_TYPE_OCTETSTRING:
		case BER_TYPE_NULL:
		case BER_TYPE_OBJECT:
			/* universal types */
			break;
		default:
			/* application-specific types */
			ber_set_header(a, BER_CLASS_APPLICATION, vbhdr.type);
			break;
		}
	}

	if (varbind != NULL)
		len = ber_calc_len(varbind);
	log_debug("trap_agentx: from pid %u len %d elements %d",
	    pid, len, x);

	trap_send(&o, varbind);

	*varcpy = v;
	*vcpylen = pdu->hdr->length;

	return (AGENTX_ERR_NONE);
 done:
	if (varbind != NULL)
		ber_free_elements(varbind);
	if (v)
		free(v);
	*idx = x;
	return (ret);
}

int
trap_send(struct ber_oid *oid, struct ber_element *elm)
{
	int			 ret = 0, s;
	struct address		*tr;
	struct ber_element	*root, *b, *c, *trap;
	struct ber		 ber;
	char			*cmn;
	ssize_t			 len;
	u_int8_t		*ptr;
	struct			 ber_oid uptime = OID(MIB_sysUpTime);
	struct			 ber_oid trapoid = OID(MIB_snmpTrapOID);
	char			 ostr[SNMP_MAX_OID_STRLEN];
	struct oid		 oa, ob;

	if (TAILQ_EMPTY(&env->sc_trapreceivers))
		return (0);

	smi_scalar_oidlen(&uptime);
	smi_scalar_oidlen(&trapoid);
	smi_scalar_oidlen(oid);

	smi_oid2string(oid, ostr, sizeof(ostr), 0);
	log_debug("trap_send: oid %s", ostr);

	/* Setup OIDs to compare against the trap receiver MIB */
	bzero(&oa, sizeof(oa));
	bcopy(oid->bo_id, &oa.o_oid, sizeof(oa.o_oid));
	oa.o_oidlen = oid->bo_n;
	bzero(&ob, sizeof(ob));
	ob.o_flags = OID_TABLE;

	/* Add mandatory varbind elements */
	trap = ber_add_sequence(NULL);
	c = ber_printf_elements(trap, "{Odt}{OO}",
	    &uptime, smi_getticks(),
	    BER_CLASS_APPLICATION, SNMP_T_TIMETICKS,
	    &trapoid, oid);
	if (elm != NULL)
		ber_link_elements(c, elm);

	bzero(&ber, sizeof(ber));
	ber.fd = -1;

	TAILQ_FOREACH(tr, &env->sc_trapreceivers, entry) {
		if (tr->sa_oid != NULL && tr->sa_oid->bo_n) {
			/* The trap receiver may want only a specified MIB */
			bcopy(&tr->sa_oid->bo_id, &ob.o_oid,
			    sizeof(ob.o_oid));
			ob.o_oidlen = tr->sa_oid->bo_n;
			if (smi_oid_cmp(&oa, &ob) != 0)
				continue;
		}

		if ((s = snmpd_socket_af(&tr->ss, htons(tr->port))) == -1) {
			ret = -1;
			goto done;
		}

		cmn = tr->sa_community != NULL ?
		    tr->sa_community : env->sc_trcommunity;

		/* SNMP header */
		root = ber_add_sequence(NULL);
		b = ber_printf_elements(root, "ds{tddd",
		    SNMP_V2, cmn, BER_CLASS_CONTEXT, SNMP_C_TRAPV2,
		    arc4random(), 0, 0);
		ber_link_elements(b, trap);

#ifdef DEBUG
		smi_debug_elements(root);
#endif
		len = ber_write_elements(&ber, root);
		if (ber_get_writebuf(&ber, (void *)&ptr) > 0 &&
		    sendto(s, ptr, len, 0, (struct sockaddr *)&tr->ss,
		    tr->ss.ss_len) != -1) {
			env->sc_stats.snmp_outpkts++;
			ret++;
		}

		close(s);
		ber_unlink_elements(b);
		ber_free_elements(root);
	}

 done:
	ber_free_elements(trap);
	ber_free(&ber);

	return (ret);
}