Debian Codegraph @

contrib/mod_snmp/pdu.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_snmp PDU 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
31 static const char *trace_channel = "snmp.pdu";
32
33 const char *snmp_pdu_get_request_type_desc(unsigned char request_type) {
34 const char *desc;
35
36 switch (request_type) {
37 case SNMP_PDU_GET:
38 desc = "GetRequest-PDU";
39 break;
40
41 case SNMP_PDU_GETNEXT:
42 desc = "GetNextRequest-PDU";
43 break;
44
45 case SNMP_PDU_RESPONSE:
46 desc = "GetResponse-PDU";
47 break;
48
49 case SNMP_PDU_SET:
50 desc = "SetRequest-PDU";
51 break;
52
53 case SNMP_PDU_TRAP_V1:
54 desc = "Trap-PDU";
55 break;
56
57 case SNMP_PDU_GETBULK:
58 desc = "GetBulkRequest-PDU";
59 break;
60
61 case SNMP_PDU_INFORM:
62 desc = "InformRequest-PDU";
63 break;
64
65 case SNMP_PDU_TRAP_V2:
66 desc = "TrapV2-PDU";
67 break;
68
69 case SNMP_PDU_REPORT:
70 desc = "Report-PDU";
71 break;
72
73 default:
74 desc = "Unknown";
75 break;
76 }
77
78 return desc;
79 }
80
81 struct snmp_pdu *snmp_pdu_create(pool *p, unsigned char request_type) {
82 pool *sub_pool;
83 struct snmp_pdu *pdu;
84
85 sub_pool = pr_pool_create_sz(p, 64);
86 pdu = pcalloc(sub_pool, sizeof(struct snmp_pdu));
87 pdu->pool = sub_pool;
88 pdu->request_type = request_type;
89
90 pr_trace_msg(trace_channel, 19,
91 "created PDU of type '%s'", snmp_pdu_get_request_type_desc(request_type));
92 return pdu;
93 }
94
95 /* Note: This makes a *shallow* copy of the given PDU, not a deep copy! */
96 struct snmp_pdu *snmp_pdu_dup(pool *p, struct snmp_pdu *src_pdu) {
97 struct snmp_pdu *dst_pdu;
98
99 dst_pdu = snmp_pdu_create(p, src_pdu->request_type);
100
101 dst_pdu->request_id = src_pdu->request_id;
102 dst_pdu->err_code = src_pdu->err_code;
103 dst_pdu->err_idx = src_pdu->err_idx;
104
105 dst_pdu->non_repeaters = src_pdu->non_repeaters;
106 dst_pdu->max_repetitions = src_pdu->max_repetitions;
107
108 /* XXX I'm not sure this is a good idea; once the source PDU is destroyed,
109 * this becomes a dangling pointer. Perhaps this should be a deep copy
110 * of the varlist, too?
111 */
112 dst_pdu->varlist = src_pdu->varlist;
113
114 dst_pdu->trap_oid = src_pdu->trap_oid;
115 dst_pdu->trap_oidlen = src_pdu->trap_oidlen;
116 dst_pdu->trap_cause = src_pdu->trap_cause;
117 dst_pdu->trap_type = src_pdu->trap_type;
118 dst_pdu->trap_systime = src_pdu->trap_systime;
119
120 return dst_pdu;
121 }
122
123 /* Write this PDU into a buffer.
124 *
125 * RFC 1157: A Simple Network Management Protocol (SNMP)
126 *
127 * PDU ::=
128 * SEQUENCE {
129 * request-id INTEGER
130 * error-status INTEGER
131 * error-index INTEGER
132 * Variable Bindings
133 * }
134 *
135 * TrapPDU ::=
136 * SEQUENCE {
137 * enterprise NetworkAddress
138 * generic-trap INTEGER
139 * specific-trap INTEGER
140 * time-stamp TIMETICKS
141 * Variable Bindings
142 * }
143 *
144 *
145 * RFC 1902: Structure of Management Information for SNMPv2
146 *
147 * PDU ::=
148 * SEQUENCE {
149 * request-id INTEGER32
150 * error-status INTEGER
151 * error-index INTEGER
152 * Variable Bindings
153 * }
154 *
155 * BulkPDU ::=
156 * SEQUENCE {
157 * request-id INTEGER32
158 * non-repeaters INTEGER
159 * max-repetitions INTEGER
160 * Variable Bindings
161 * }
162 */
163
164 int snmp_pdu_read(pool *p, unsigned char **buf, size_t *buflen,
165 struct snmp_pdu **pdu, long snmp_version) {
166 unsigned char asn1_type;
167 unsigned int asn1_len;
168 int flags, res;
169
170 /* Since the "type" in this header is the PDU request type, the trace logging
171 * of the ASN.1 type will be wrong. That being the case, simply tell the
172 * readers to not trace log that wrong invalid ASN.1 type. Makes the
173 * trace logging confusing and incorrect.
174 */
175 flags = SNMP_ASN1_FL_NO_TRACE_TYPESTR;
176
177 res = snmp_asn1_read_header(p, buf, buflen, &asn1_type, &asn1_len, flags);
178 if (res < 0) {
179 return -1;
180 }
181
182 pr_trace_msg(trace_channel, 19,
183 "read in PDU (0x%02x), length %u bytes", asn1_type, asn1_len);
184
185 *pdu = snmp_pdu_create(p, asn1_type);
186
187 switch (asn1_type) {
188 case SNMP_PDU_RESPONSE:
189 case SNMP_PDU_TRAP_V1:
190 case SNMP_PDU_TRAP_V2:
191 case SNMP_PDU_INFORM:
192 case SNMP_PDU_REPORT:
193 pr_trace_msg(trace_channel, 1,
194 "handling '%s' PDU not currently supported",
195 snmp_pdu_get_request_type_desc((*pdu)->request_type));
196 errno = ENOSYS;
197 return -1;
198
199 case SNMP_PDU_GETBULK:
200 /* Request ID */
201 res = snmp_asn1_read_int(p, buf, buflen, &asn1_type,
202 &((*pdu)->request_id), 0);
203 if (res < 0) {
204 return -1;
205 }
206 pr_trace_msg(trace_channel, 19,
207 "read PDU request ID: %ld", (*pdu)->request_id);
208
209 /* Non-repeaters */
210 res = snmp_asn1_read_int(p, buf, buflen, &asn1_type,
211 &((*pdu)->non_repeaters), 0);
212 if (res < 0) {
213 return -1;
214 }
215 pr_trace_msg(trace_channel, 19,
216 "read PDU non-repeaters: %ld", (*pdu)->non_repeaters);
217
218 /* As per RFC1905, if non_repeaters is negative, it is set to zero. */
219 if ((*pdu)->non_repeaters < 0) {
220 (*pdu)->non_repeaters = 0;
221 }
222
223 /* Max-repetitions */
224 res = snmp_asn1_read_int(p, buf, buflen, &asn1_type,
225 &((*pdu)->max_repetitions), 0);
226 if (res < 0) {
227 return -1;
228 }
229 pr_trace_msg(trace_channel, 19,
230 "read PDU max-repetitions: %ld", (*pdu)->max_repetitions);
231
232 /* As per RFC1905, if max_repetitions is negative, it is set to zero. */
233 if ((*pdu)->max_repetitions < 0) {
234 (*pdu)->max_repetitions = 0;
235 }
236
237 break;
238
239 default:
240 /* Request ID */
241 res = snmp_asn1_read_int(p, buf, buflen, &asn1_type,
242 &((*pdu)->request_id), 0);
243 if (res < 0) {
244 return -1;
245 }
246 pr_trace_msg(trace_channel, 19,
247 "read PDU request ID: %ld", (*pdu)->request_id);
248
249 /* Error Status/Code */
250 res = snmp_asn1_read_int(p, buf, buflen, &asn1_type,
251 &((*pdu)->err_code), 0);
252 if (res < 0) {
253 return -1;
254 }
255 pr_trace_msg(trace_channel, 19,
256 "read PDU error status/code: %ld", (*pdu)->err_code);
257
258 /* XXX What if err_code is non-zero? */
259
260 /* Error Index */
261 res = snmp_asn1_read_int(p, buf, buflen, &asn1_type,
262 &((*pdu)->err_idx), 0);
263 if (res < 0) {
264 return -1;
265 }
266 pr_trace_msg(trace_channel, 19,
267 "read PDU error index: %ld", (*pdu)->err_idx);
268
269 /* XXX What if err_idx is non-zero? */
270
271 break;
272 }
273
274 res = snmp_smi_read_vars(p, buf, buflen, &((*pdu)->varlist), snmp_version);
275 if (res < 0) {
276 return -1;
277 }
278
279 (*pdu)->varlistlen = res;
280
281 pr_trace_msg(trace_channel, 17,
282 "read %d %s from %s message", res,
283 res != 1 ? "variables" : "variable",
284 snmp_msg_get_versionstr(snmp_version));
285
286 return 0;
287 }
288
289 int snmp_pdu_write(pool *p, unsigned char **buf, size_t *buflen,
290 struct snmp_pdu *pdu, long snmp_version) {
291 unsigned char asn1_type, *pdu_hdr_start, *pdu_hdr_end;
292 size_t pdu_hdr_startlen;
293 unsigned int asn1_len;
294 int flags, res;
295
296 pr_trace_msg(trace_channel, 19,
297 "writing %s PDU (0x%02x)",
298 snmp_pdu_get_request_type_desc(pdu->request_type), pdu->request_type);
299
300 /* Since the "type" in this header is the PDU request type, the trace logging
301 * of the ASN.1 type will be wrong. That being the case, simply tell the
302 * writers to not trace log that wrong invalid ASN.1 type. Makes the
303 * trace logging confusing and incorrect.
304 */
305 flags = SNMP_ASN1_FL_NO_TRACE_TYPESTR;
306
307 asn1_type = pdu->request_type;
308 asn1_len = 0;
309
310 pdu_hdr_start = *buf;
311 pdu_hdr_startlen = *buflen;
312
313 res = snmp_asn1_write_header(p, buf, buflen, asn1_type, asn1_len, flags);
314 if (res < 0) {
315 return -1;
316 }
317
318 pdu_hdr_end = *buf;
319
320 switch (pdu->request_type) {
321 case SNMP_PDU_GETBULK:
322 asn1_type = (SNMP_ASN1_CLASS_UNIVERSAL|SNMP_ASN1_PRIMITIVE|SNMP_ASN1_TYPE_INTEGER);
323
324 /* Request ID */
325 pr_trace_msg(trace_channel, 19,
326 "writing PDU request ID: %ld", pdu->request_id);
327 res = snmp_asn1_write_int(p, buf, buflen, asn1_type, pdu->request_id, 0);
328 if (res < 0) {
329 return -1;
330 }
331
332 /* Non-repeaters */
333 pr_trace_msg(trace_channel, 19,
334 "writing PDU non-repeaters: %ld", pdu->non_repeaters);
335 res = snmp_asn1_write_int(p, buf, buflen, asn1_type,
336 pdu->non_repeaters, 0);
337 if (res < 0) {
338 return -1;
339 }
340
341 /* Max-repetitions */
342 pr_trace_msg(trace_channel, 19,
343 "writing PDU max-repetitions: %ld", pdu->max_repetitions);
344 res = snmp_asn1_write_int(p, buf, buflen, asn1_type,
345 pdu->max_repetitions, 0);
346 if (res < 0) {
347 return -1;
348 }
349
350 /* XXX write varlist? */
351
352 break;
353
354 default:
355 /* "Normal" PDU formatting. */
356
357 asn1_type = (SNMP_ASN1_CLASS_UNIVERSAL|SNMP_ASN1_PRIMITIVE|SNMP_ASN1_TYPE_INTEGER);
358
359 /* Request ID */
360 pr_trace_msg(trace_channel, 19,
361 "writing PDU request ID: %ld", pdu->request_id);
362 res = snmp_asn1_write_int(p, buf, buflen, asn1_type, pdu->request_id, 0);
363 if (res < 0) {
364 return -1;
365 }
366
367 /* Error Status/Code */
368 pr_trace_msg(trace_channel, 19,
369 "writing PDU error status/code: %ld", pdu->err_code);
370 res = snmp_asn1_write_int(p, buf, buflen, asn1_type, pdu->err_code, 0);
371 if (res < 0) {
372 return -1;
373 }
374
375 /* Error Index */
376 pr_trace_msg(trace_channel, 19,
377 "writing PDU error index: %ld", pdu->err_idx);
378 res = snmp_asn1_write_int(p, buf, buflen, asn1_type, pdu->err_idx, 0);
379 if (res < 0) {
380 return -1;
381 }
382
383 /* Variable bindings list */
384 pr_trace_msg(trace_channel, 19,
385 "writing PDU variable binding list: (%u %s)", pdu->varlistlen,
386 pdu->varlistlen != 1 ? "variables" : "variable");
387 res = snmp_smi_write_vars(p, buf, buflen, pdu->varlist, snmp_version);
388 if (res < 0) {
389 return -1;
390 }
391
392 break;
393 }
394
395 /* Rewrite the PDU header, this time with the length of the entire PDU. */
396
397 asn1_type = pdu->request_type;
398 asn1_len = (*buf - pdu_hdr_end);
399
400 pr_trace_msg(trace_channel, 18,
401 "updating PDU header to have length %u", asn1_len);
402 res = snmp_asn1_write_header(p, &pdu_hdr_start, &pdu_hdr_startlen, asn1_type,
403 asn1_len, flags);
404 if (res < 0) {
405 return -1;
406 }
407
408 return 0;
409 }