| 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 |
#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 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 200 |
msg_len = (*buf - msg_hdr_start); |
| 201 |
|
| 202 |
|
| 203 |
|
| 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 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
*buflen = msg_len; |
| 229 |
*buf = msg_ptr; |
| 230 |
|
| 231 |
return 0; |
| 232 |
} |