1 /*
2 * ProFTPD - mod_snmp message routines
3 * Copyright (c) 2008-2016 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 */
24
25 #include "mod_snmp.h"
26 #include "msg.h"
27 #include "pdu.h"
28 #include "smi.h"
29 #include "asn1.h"
30 #include "packet.h"
31 #include "db.h"
32
33 static const char *trace_channel = "snmp.msg";
34
snmp_msg_get_versionstr(long snmp_version)35 const char *snmp_msg_get_versionstr(long snmp_version) {
36 const char *versionstr = "unknown";
37
38 switch (snmp_version) {
39 case SNMP_PROTOCOL_VERSION_1:
40 versionstr = "SNMPv1";
41 break;
42
43 case SNMP_PROTOCOL_VERSION_2:
44 versionstr = "SNMPv2";
45 break;
46
47 case SNMP_PROTOCOL_VERSION_3:
48 versionstr = "SNMPv3";
49 break;
50 }
51
52 return versionstr;
53 }
54
55 /* RFC 1901: Introduction to Community-based SNMPv2:
56 *
57 * Message ::=
58 * SEQUENCE {
59 * version INTEGER
60 * community OCTET STRING
61 * data
62 * }
63 *
64 * and, before that: RFC 1157: A Simple Network Management Protocol (SNMP):
65 *
66 * Message ::=
67 * SEQUENCE {
68 * version INTEGER
69 * community OCTET STRING
70 * data
71 * }
72 */
73
snmp_msg_read(pool * p,unsigned char ** buf,size_t * buflen,char ** community,unsigned int * community_len,long * snmp_version,struct snmp_pdu ** pdu)74 int snmp_msg_read(pool *p, unsigned char **buf, size_t *buflen,
75 char **community, unsigned int *community_len, long *snmp_version,
76 struct snmp_pdu **pdu) {
77 unsigned char asn1_type;
78 unsigned int asn1_len;
79 int res;
80
81 res = snmp_asn1_read_header(p, buf, buflen, &asn1_type, &asn1_len, 0);
82 if (res < 0) {
83 return -1;
84 }
85
86 if (asn1_type != (SNMP_ASN1_TYPE_SEQUENCE|SNMP_ASN1_CONSTRUCT)) {
87 pr_trace_msg(trace_channel, 3,
88 "unable to read SNMP message (tag '%s')",
89 snmp_asn1_get_tagstr(p, asn1_type));
90
91 errno = EINVAL;
92 return -1;
93 }
94
95 res = snmp_asn1_read_int(p, buf, buflen, &asn1_type, snmp_version, 0);
96 if (res < 0) {
97 return -1;
98 }
99
100 pr_trace_msg(trace_channel, 17,
101 "read SNMP message for %s", snmp_msg_get_versionstr(*snmp_version));
102
103 /* XXX Don't support SNMPv3 yet. */
104
105 if (*snmp_version != SNMP_PROTOCOL_VERSION_1 &&
106 *snmp_version != SNMP_PROTOCOL_VERSION_2) {
107 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
108 "%s messages not currently supported, dropping packet",
109 snmp_msg_get_versionstr(*snmp_version));
110
111 res = snmp_db_incr_value(p, SNMP_DB_SNMP_F_PKTS_DROPPED_TOTAL, 1);
112 if (res < 0) {
113 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
114 "error incrementing snmp.packetsDroppedTotal: %s", strerror(errno));
115 }
116
117 errno = ENOSYS;
118 return -1;
119 }
120
121 res = snmp_asn1_read_string(p, buf, buflen, &asn1_type, community,
122 community_len);
123 if (res < 0) {
124 return -1;
125 }
126
127 /* Check that asn1_type is a UNIVERSAL/PRIMITIVE/OCTETSTRING. */
128 if (!(asn1_type == (SNMP_ASN1_CLASS_UNIVERSAL|SNMP_ASN1_PRIMITIVE|SNMP_ASN1_TYPE_OCTETSTRING))) {
129 pr_trace_msg(trace_channel, 3,
130 "unable to read OCTET_STRING (received type '%s')",
131 snmp_asn1_get_tagstr(p, asn1_type));
132 errno = EINVAL;
133 return -1;
134 }
135
136 pr_trace_msg(trace_channel, 17,
137 "read %s message: community = '%s'",
138 snmp_msg_get_versionstr(*snmp_version), *community);
139
140 res = snmp_pdu_read(p, buf, buflen, pdu, *snmp_version);
141 if (res < 0) {
142 return -1;
143 }
144
145 return 0;
146 }
147
snmp_msg_write(pool * p,unsigned char ** buf,size_t * buflen,char * community,unsigned int community_len,long snmp_version,struct snmp_pdu * pdu)148 int snmp_msg_write(pool *p, unsigned char **buf, size_t *buflen,
149 char *community, unsigned int community_len, long snmp_version,
150 struct snmp_pdu *pdu) {
151 unsigned char asn1_type;
152 unsigned int asn1_len;
153 unsigned char *msg_ptr, *msg_hdr_start, *msg_hdr_end;
154 size_t msg_hdr_startlen, msg_len;
155 int res;
156
157 if (p == NULL ||
158 buf == NULL ||
159 buflen == NULL ||
160 community == NULL ||
161 pdu == NULL) {
162 errno = EINVAL;
163 return -1;
164 }
165
166 msg_ptr = msg_hdr_start = *buf;
167 msg_hdr_startlen = *buflen;
168
169 asn1_type = (SNMP_ASN1_TYPE_SEQUENCE|SNMP_ASN1_CONSTRUCT);
170 asn1_len = 0;
171
172 res = snmp_asn1_write_header(p, buf, buflen, asn1_type, asn1_len, 0);
173 if (res < 0) {
174 return -1;
175 }
176
177 msg_hdr_end = *buf;
178
179 asn1_type = (SNMP_ASN1_CLASS_UNIVERSAL|SNMP_ASN1_PRIMITIVE|SNMP_ASN1_TYPE_INTEGER);
180 res = snmp_asn1_write_int(p, buf, buflen, asn1_type, snmp_version, 0);
181 if (res < 0) {
182 return -1;
183 }
184
185 asn1_type = (SNMP_ASN1_CLASS_UNIVERSAL|SNMP_ASN1_PRIMITIVE|SNMP_ASN1_TYPE_OCTETSTRING);
186 res = snmp_asn1_write_string(p, buf, buflen, asn1_type, community,
187 community_len);
188 if (res < 0) {
189 return -1;
190 }
191
192 if (pdu != NULL) {
193 res = snmp_pdu_write(p, buf, buflen, pdu, snmp_version);
194 if (res < 0) {
195 return -1;
196 }
197 }
198
199 /* Calculate the full message length, for use later. */
200 msg_len = (*buf - msg_hdr_start);
201
202 /* Having written out the entire message now, we can go back and fill
203 * in the appropriate length in the header.
204 */
205
206 asn1_type = (SNMP_ASN1_TYPE_SEQUENCE|SNMP_ASN1_CONSTRUCT);
207 asn1_len = (*buf - msg_hdr_end);
208
209 pr_trace_msg(trace_channel, 18,
210 "updating SNMP message header to have length %u", asn1_len);
211
212 res = snmp_asn1_write_header(p, &msg_hdr_start, &msg_hdr_startlen,
213 asn1_type, asn1_len, 0);
214 if (res < 0) {
215 return -1;
216 }
217
218 /* XXX This is a bit of a hack here. We started with a buflen, and steadily
219 * decremented that value as we wrote data into the buffer.
220 *
221 * However, buflen needs to the amount of data IN the buffer once we return
222 * the caller, NOT the amount of data REMAINING in the buffer. So we
223 * cheat here.
224 *
225 * We also cheat by resetting buf to point to the start of the message.
226 */
227
228 *buflen = msg_len;
229 *buf = msg_ptr;
230
231 return 0;
232 }
233