summaryrefslogtreecommitdiff
path: root/usr.sbin/snmpd/trap.c
blob: b141a530b879100ca34966884a25697ed24ec7d5 (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
/*	$OpenBSD: trap.c,v 1.28 2016/11/09 20:31:56 jca 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/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"

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;
	u_int32_t			 d;
	struct ber_oid			 o, oid;
	struct ber_oid			 uptime = OID(MIB_sysUpTime);
	struct ber_oid			 trapoid = OID(MIB_snmpTrapOID);
	struct ber_element		*varbind, *iter;
	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;
	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) {
			ret = AGENTX_ERR_PARSE_ERROR;
			goto done;
		}

		if (state < 2) {
			if (snmp_agentx_read_oid(pdu, (struct snmp_oid *)&oid) == -1) {
				ret = AGENTX_ERR_PARSE_ERROR;
				goto done;
			}
			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;
			}
		}

		ret = varbind_convert(pdu, &vbhdr, &varbind, &iter);
		if (ret != AGENTX_ERR_NONE)
			goto done;
	}

	if (varbind != NULL)
		len = ber_calc_len(varbind);
	log_debug("trap_agentx: from pid %u len %zd 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);
	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(&snmpd_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, &snmpd_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;
		}
		if (tr->sa_srcaddr != NULL) {
			if (bind(s, (struct sockaddr *)&tr->sa_srcaddr->ss,
			    tr->sa_srcaddr->ss.ss_len) == -1) {
				ret = -1;
				goto done;
			}
		}

		cmn = tr->sa_community != NULL ?
		    tr->sa_community : snmpd_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) {
			snmpd_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);
}