| 1 |
/* |
| 2 |
* Portions of this file are subject to the following copyright(s). See |
| 3 |
* the Net-SNMP's COPYING file for more details and other copyrights |
| 4 |
* that may apply: |
| 5 |
* |
| 6 |
* Portions of this file are copyrighted by: |
| 7 |
* Copyright (c) 2016 VMware, Inc. All rights reserved. |
| 8 |
* Use is subject to license terms specified in the COPYING file |
| 9 |
* distributed with the Net-SNMP package. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <net-snmp/net-snmp-config.h> |
| 13 |
|
| 14 |
#include <net-snmp/net-snmp-includes.h> |
| 15 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 16 |
|
| 17 |
#include <net-snmp/agent/scalar_group.h> |
| 18 |
|
| 19 |
#include <stdlib.h> |
| 20 |
#ifdef HAVE_STRING_H |
| 21 |
#include <string.h> |
| 22 |
#else |
| 23 |
#include <strings.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <net-snmp/agent/instance.h> |
| 27 |
#include <net-snmp/agent/serialize.h> |
| 28 |
|
| 29 |
static void * |
| 30 |
clone_scalar_group(void *p) |
| 31 |
{ |
| 32 |
netsnmp_scalar_group* src = p; |
| 33 |
netsnmp_scalar_group *t = SNMP_MALLOC_TYPEDEF(netsnmp_scalar_group); |
| 34 |
if(t != NULL) { |
| 35 |
t->lbound = src->lbound; |
| 36 |
t->ubound = src->ubound; |
| 37 |
} |
| 38 |
return t; |
| 39 |
} |
| 40 |
|
| 41 |
/** @defgroup scalar_group_group scalar_group |
| 42 |
* Process groups of scalars. |
| 43 |
* @ingroup leaf |
| 44 |
* @{ |
| 45 |
*/ |
| 46 |
netsnmp_mib_handler * |
| 47 |
netsnmp_get_scalar_group_handler(oid first, oid last) |
| 48 |
{ |
| 49 |
netsnmp_mib_handler *ret = NULL; |
| 50 |
netsnmp_scalar_group *sgroup = NULL; |
| 51 |
|
| 52 |
ret = netsnmp_create_handler("scalar_group", |
| 53 |
netsnmp_scalar_group_helper_handler); |
| 54 |
if (ret) { |
| 55 |
sgroup = SNMP_MALLOC_TYPEDEF(netsnmp_scalar_group); |
| 56 |
if (NULL == sgroup) { |
| 57 |
netsnmp_handler_free(ret); |
| 58 |
ret = NULL; |
| 59 |
} |
| 60 |
else { |
| 61 |
sgroup->lbound = first; |
| 62 |
sgroup->ubound = last; |
| 63 |
ret->myvoid = (void *)sgroup; |
| 64 |
ret->data_free = free; |
| 65 |
ret->data_clone = clone_scalar_group; |
| 66 |
} |
| 67 |
} |
| 68 |
return ret; |
| 69 |
} |
| 70 |
|
| 71 |
int |
| 72 |
netsnmp_register_scalar_group(netsnmp_handler_registration *reginfo, |
| 73 |
oid first, oid last) |
| 74 |
{ |
| 75 |
netsnmp_mib_handler *h1, *h2; |
| 76 |
|
| 77 |
h1 = netsnmp_get_instance_handler(); |
| 78 |
h2 = netsnmp_get_scalar_group_handler(first, last); |
| 79 |
|
| 80 |
if (h1 && h2) { |
| 81 |
if (netsnmp_inject_handler(reginfo, h1) == SNMPERR_SUCCESS) { |
| 82 |
h1 = NULL; |
| 83 |
if (netsnmp_inject_handler(reginfo, h2) == SNMPERR_SUCCESS) |
| 84 |
return netsnmp_register_serialize(reginfo); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
snmp_log(LOG_ERR, "register read only scalar group failed\n"); |
| 89 |
netsnmp_handler_free(h1); |
| 90 |
netsnmp_handler_free(h2); |
| 91 |
netsnmp_handler_registration_free(reginfo); |
| 92 |
|
| 93 |
return MIB_REGISTRATION_FAILED; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
int |
| 98 |
netsnmp_scalar_group_helper_handler(netsnmp_mib_handler *handler, |
| 99 |
netsnmp_handler_registration *reginfo, |
| 100 |
netsnmp_agent_request_info *reqinfo, |
| 101 |
netsnmp_request_info *requests) |
| 102 |
{ |
| 103 |
netsnmp_variable_list *var = requests->requestvb; |
| 104 |
|
| 105 |
netsnmp_scalar_group *sgroup = (netsnmp_scalar_group *)handler->myvoid; |
| 106 |
int ret, cmp; |
| 107 |
int namelen; |
| 108 |
oid subid, root_tmp[MAX_OID_LEN], *root_save; |
| 109 |
|
| 110 |
DEBUGMSGTL(("helper:scalar_group", "Got request:\n")); |
| 111 |
namelen = SNMP_MIN(requests->requestvb->name_length, |
| 112 |
reginfo->rootoid_len); |
| 113 |
cmp = snmp_oid_compare(requests->requestvb->name, namelen, |
| 114 |
reginfo->rootoid, reginfo->rootoid_len); |
| 115 |
|
| 116 |
DEBUGMSGTL(( "helper:scalar_group", " cmp=%d, oid:", cmp)); |
| 117 |
DEBUGMSGOID(("helper:scalar_group", var->name, var->name_length)); |
| 118 |
DEBUGMSG(( "helper:scalar_group", "\n")); |
| 119 |
|
| 120 |
/* |
| 121 |
* copy root oid to root_tmp, set instance to 0. (subid set later on) |
| 122 |
* save rootoid, since we'll replace it before calling next handler, |
| 123 |
* and need to restore it afterwards. |
| 124 |
*/ |
| 125 |
memcpy(root_tmp, reginfo->rootoid, reginfo->rootoid_len * sizeof(oid)); |
| 126 |
root_tmp[reginfo->rootoid_len + 1] = 0; |
| 127 |
root_save = reginfo->rootoid; |
| 128 |
|
| 129 |
switch (reqinfo->mode) { |
| 130 |
/* |
| 131 |
* The handling of "exact" requests is basically the same. |
| 132 |
* The only difference between GET and SET requests is the |
| 133 |
* error/exception to return on failure. |
| 134 |
*/ |
| 135 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 136 |
case MODE_SET_RESERVE1: |
| 137 |
case MODE_SET_RESERVE2: |
| 138 |
case MODE_SET_ACTION: |
| 139 |
case MODE_SET_COMMIT: |
| 140 |
case MODE_SET_UNDO: |
| 141 |
case MODE_SET_FREE: |
| 142 |
#endif /* NETSNMP_NO_WRITE_SUPPORT */ |
| 143 |
case MODE_GET: |
| 144 |
ret = reqinfo->mode == MODE_GET ? SNMP_NOSUCHOBJECT : SNMP_ERR_NOCREATION; |
| 145 |
if (cmp != 0 || |
| 146 |
requests->requestvb->name_length <= reginfo->rootoid_len) { |
| 147 |
/* |
| 148 |
* Common prefix doesn't match, or only *just* matches |
| 149 |
* the registered root (so can't possibly match a scalar) |
| 150 |
*/ |
| 151 |
netsnmp_set_request_error(reqinfo, requests, ret); |
| 152 |
return SNMP_ERR_NOERROR; |
| 153 |
} else { |
| 154 |
/* |
| 155 |
* Otherwise, |
| 156 |
* extract the object subidentifier from the request, |
| 157 |
* check this is (probably) valid, and then fudge the |
| 158 |
* registered 'rootoid' to match, before passing the |
| 159 |
* request off to the next handler ('scalar'). |
| 160 |
* |
| 161 |
* Note that we don't bother checking instance subidentifiers |
| 162 |
* here. That's left to the scalar helper. |
| 163 |
*/ |
| 164 |
subid = requests->requestvb->name[reginfo->rootoid_len]; |
| 165 |
if (subid < sgroup->lbound || |
| 166 |
subid > sgroup->ubound) { |
| 167 |
netsnmp_set_request_error(reqinfo, requests, ret); |
| 168 |
return SNMP_ERR_NOERROR; |
| 169 |
} |
| 170 |
root_tmp[reginfo->rootoid_len] = subid; |
| 171 |
reginfo->rootoid_len += 2; |
| 172 |
reginfo->rootoid = root_tmp; |
| 173 |
ret = netsnmp_call_next_handler(handler, reginfo, reqinfo, |
| 174 |
requests); |
| 175 |
reginfo->rootoid = root_save; |
| 176 |
reginfo->rootoid_len -= 2; |
| 177 |
return ret; |
| 178 |
} |
| 179 |
break; |
| 180 |
|
| 181 |
case MODE_GETNEXT: |
| 182 |
/* |
| 183 |
* If we're being asked for something before (or exactly matches) |
| 184 |
* the registered root OID, then start with the first object. |
| 185 |
* If we're being asked for something that exactly matches an object |
| 186 |
* OID, then that's what we pass down. |
| 187 |
* Otherwise, we pass down the OID of the *next* object.... |
| 188 |
*/ |
| 189 |
if (cmp < 0 || |
| 190 |
requests->requestvb->name_length <= reginfo->rootoid_len) { |
| 191 |
subid = sgroup->lbound; |
| 192 |
} else if (requests->requestvb->name_length == reginfo->rootoid_len+1) |
| 193 |
subid = requests->requestvb->name[reginfo->rootoid_len]; |
| 194 |
else |
| 195 |
subid = requests->requestvb->name[reginfo->rootoid_len]+1; |
| 196 |
|
| 197 |
/* |
| 198 |
* ... always assuming this is (potentially) valid, of course. |
| 199 |
*/ |
| 200 |
if (subid < sgroup->lbound) |
| 201 |
subid = sgroup->lbound; |
| 202 |
else if (subid > sgroup->ubound) |
| 203 |
return SNMP_ERR_NOERROR; |
| 204 |
|
| 205 |
root_tmp[reginfo->rootoid_len] = subid; |
| 206 |
reginfo->rootoid_len += 2; |
| 207 |
reginfo->rootoid = root_tmp; |
| 208 |
ret = netsnmp_call_next_handler(handler, reginfo, reqinfo, |
| 209 |
requests); |
| 210 |
/* |
| 211 |
* If we didn't get an answer (due to holes in the group) |
| 212 |
* set things up to retry again. |
| 213 |
*/ |
| 214 |
if (!requests->delegated && |
| 215 |
(requests->requestvb->type == ASN_NULL || |
| 216 |
requests->requestvb->type == SNMP_NOSUCHOBJECT || |
| 217 |
requests->requestvb->type == SNMP_NOSUCHINSTANCE)) { |
| 218 |
snmp_set_var_objid(requests->requestvb, |
| 219 |
reginfo->rootoid, reginfo->rootoid_len - 1); |
| 220 |
requests->requestvb->name[reginfo->rootoid_len - 2] = ++subid; |
| 221 |
requests->requestvb->type = ASN_PRIV_RETRY; |
| 222 |
} |
| 223 |
reginfo->rootoid = root_save; |
| 224 |
reginfo->rootoid_len -= 2; |
| 225 |
return ret; |
| 226 |
} |
| 227 |
/* |
| 228 |
* got here only if illegal mode found |
| 229 |
*/ |
| 230 |
return SNMP_ERR_GENERR; |
| 231 |
} |
| 232 |
|
| 233 |
/** @} |
| 234 |
*/ |