2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: bsnmp/lib/snmpagent.c,v 1.20 2005/10/04 11:21:33 brandt_h Exp $
31 * SNMP Agent functions
33 #include <sys/types.h>
34 #include <sys/queue.h>
41 #elif defined(HAVE_INTTYPES_H)
49 #include "snmpagent.h"
51 static void snmp_debug_func(const char *fmt, ...);
53 void (*snmp_debug)(const char *fmt, ...) = snmp_debug_func;
55 struct snmp_node *tree;
59 * Structure to hold dependencies during SET processing
60 * The last two members of this structure must be the
61 * dependency visible by the user and the user data.
64 TAILQ_ENTRY(depend) link;
65 size_t len; /* size of data part */
67 struct snmp_dependency dep;
68 #if defined(__GNUC__) && __GNUC__ < 3
74 TAILQ_HEAD(depend_list, depend);
80 struct snmp_context ctx;
81 struct depend_list dlist;
82 const struct snmp_node *node[SNMP_MAX_BINDINGS];
83 struct snmp_scratch scratch[SNMP_MAX_BINDINGS];
84 struct depend *depend;
87 #define TR(W) (snmp_trace & SNMP_TRACE_##W)
90 static char oidbuf[ASN_OIDSTRLEN];
96 snmp_init_context(void)
98 struct context *context;
100 if ((context = malloc(sizeof(*context))) == NULL)
103 memset(context, 0, sizeof(*context));
104 TAILQ_INIT(&context->dlist);
106 return (&context->ctx);
110 * Find a variable for SET/GET and the first GETBULK pass.
111 * Return the node pointer. If the search fails, set the errp to
112 * the correct SNMPv2 GET exception code.
114 static struct snmp_node *
115 find_node(const struct snmp_value *value, enum snmp_syntax *errp)
117 struct snmp_node *tp;
120 snmp_debug("find: searching %s",
121 asn_oid2str_r(&value->var, oidbuf));
124 * If we have an exact match (the entry in the table is a
125 * sub-oid from the variable) we have found what we are for.
126 * If the table oid is higher than the variable, there is no match.
128 for (tp = tree; tp < tree + tree_size; tp++) {
129 if (asn_is_suboid(&tp->oid, &value->var))
131 if (asn_compare_oid(&tp->oid, &value->var) >= 0)
136 snmp_debug("find: no match");
137 *errp = SNMP_SYNTAX_NOSUCHOBJECT;
141 /* leafs must have a 0 instance identifier */
142 if (tp->type == SNMP_NODE_LEAF &&
143 (value->var.len != tp->oid.len + 1 ||
144 value->var.subs[tp->oid.len] != 0)) {
146 snmp_debug("find: bad leaf index");
147 *errp = SNMP_SYNTAX_NOSUCHINSTANCE;
151 snmp_debug("find: found %s",
152 asn_oid2str_r(&value->var, oidbuf));
156 static struct snmp_node *
157 find_subnode(const struct snmp_value *value)
159 struct snmp_node *tp;
161 for (tp = tree; tp < tree + tree_size; tp++) {
162 if (asn_is_suboid(&value->var, &tp->oid))
169 snmp_pdu_create_response(const struct snmp_pdu *pdu, struct snmp_pdu *resp)
171 memset(resp, 0, sizeof(*resp));
172 strcpy(resp->community, pdu->community);
173 resp->version = pdu->version;
174 if (pdu->flags & SNMP_MSG_AUTODISCOVER)
175 resp->type = SNMP_PDU_REPORT; /* RFC 3414.4 */
177 resp->type = SNMP_PDU_RESPONSE;
178 resp->request_id = pdu->request_id;
179 resp->version = pdu->version;
181 if (resp->version != SNMP_V3)
184 memcpy(&resp->engine, &pdu->engine, sizeof(pdu->engine));
185 memcpy(&resp->user, &pdu->user, sizeof(pdu->user));
186 snmp_pdu_init_secparams(resp);
187 resp->identifier = pdu->identifier;
188 resp->security_model = pdu->security_model;
189 resp->context_engine_len = pdu->context_engine_len;
190 memcpy(resp->context_engine, pdu->context_engine,
191 resp->context_engine_len);
192 strlcpy(resp->context_name, pdu->context_name,
193 sizeof(resp->context_name));
197 * Execute a GET operation. The tree is rooted at the global 'root'.
198 * Build the response PDU on the fly. If the return code is SNMP_RET_ERR
199 * the pdu error status and index will be set.
202 snmp_get(struct snmp_pdu *pdu, struct asn_buf *resp_b,
203 struct snmp_pdu *resp, void *data)
207 struct snmp_node *tp;
208 enum snmp_syntax except;
209 struct context context;
212 memset(&context, 0, sizeof(context));
213 context.ctx.data = data;
215 snmp_pdu_create_response(pdu, resp);
217 if (snmp_pdu_encode_header(resp_b, resp) != SNMP_CODE_OK)
218 /* cannot even encode header - very bad */
219 return (SNMP_RET_IGN);
221 for (i = 0; i < pdu->nbindings; i++) {
222 resp->bindings[i].var = pdu->bindings[i].var;
223 if ((tp = find_node(&pdu->bindings[i], &except)) == NULL) {
224 if (pdu->version == SNMP_V1) {
226 snmp_debug("get: nosuchname");
227 pdu->error_status = SNMP_ERR_NOSUCHNAME;
228 pdu->error_index = i + 1;
230 return (SNMP_RET_ERR);
233 snmp_debug("get: exception %u", except);
234 resp->bindings[i].syntax = except;
237 /* call the action to fetch the value. */
238 resp->bindings[i].syntax = tp->syntax;
239 ret = (*tp->op)(&context.ctx, &resp->bindings[i],
240 tp->oid.len, tp->index, SNMP_OP_GET);
242 snmp_debug("get: action returns %d", ret);
244 if (ret == SNMP_ERR_NOSUCHNAME) {
245 if (pdu->version == SNMP_V1) {
246 pdu->error_status = SNMP_ERR_NOSUCHNAME;
247 pdu->error_index = i + 1;
249 return (SNMP_RET_ERR);
252 snmp_debug("get: exception noSuchInstance");
253 resp->bindings[i].syntax = SNMP_SYNTAX_NOSUCHINSTANCE;
255 } else if (ret != SNMP_ERR_NOERROR) {
256 pdu->error_status = SNMP_ERR_GENERR;
257 pdu->error_index = i + 1;
259 return (SNMP_RET_ERR);
264 err = snmp_binding_encode(resp_b, &resp->bindings[i]);
266 if (err == ASN_ERR_EOBUF) {
267 pdu->error_status = SNMP_ERR_TOOBIG;
268 pdu->error_index = 0;
270 return (SNMP_RET_ERR);
272 if (err != ASN_ERR_OK) {
274 snmp_debug("get: binding encoding: %u", err);
275 pdu->error_status = SNMP_ERR_GENERR;
276 pdu->error_index = i + 1;
278 return (SNMP_RET_ERR);
282 if (snmp_fix_encoding(resp_b, resp) != SNMP_CODE_OK) {
283 snmp_debug("get: failed to encode PDU");
284 return (SNMP_RET_ERR);
287 return (SNMP_RET_OK);
290 static struct snmp_node *
291 next_node(const struct snmp_value *value, int *pnext)
293 struct snmp_node *tp;
296 snmp_debug("next: searching %s",
297 asn_oid2str_r(&value->var, oidbuf));
300 for (tp = tree; tp < tree + tree_size; tp++) {
301 if (asn_is_suboid(&tp->oid, &value->var)) {
302 /* the tree OID is a sub-oid of the requested OID. */
303 if (tp->type == SNMP_NODE_LEAF) {
304 if (tp->oid.len == value->var.len) {
305 /* request for scalar type */
307 snmp_debug("next: found scalar %s",
308 asn_oid2str_r(&tp->oid, oidbuf));
314 snmp_debug("next: found column %s",
315 asn_oid2str_r(&tp->oid, oidbuf));
318 } else if (asn_is_suboid(&value->var, &tp->oid) ||
319 asn_compare_oid(&tp->oid, &value->var) >= 0) {
321 snmp_debug("next: found %s",
322 asn_oid2str_r(&tp->oid, oidbuf));
329 snmp_debug("next: failed");
335 do_getnext(struct context *context, const struct snmp_value *inb,
336 struct snmp_value *outb, struct snmp_pdu *pdu)
338 const struct snmp_node *tp;
341 if ((tp = next_node(inb, &next)) == NULL)
344 /* retain old variable if we are doing a GETNEXT on an exact
345 * matched leaf only */
346 if (tp->type == SNMP_NODE_LEAF || next)
349 outb->var = inb->var;
352 outb->syntax = tp->syntax;
353 if (tp->type == SNMP_NODE_LEAF) {
354 /* make a GET operation */
355 outb->var.subs[outb->var.len++] = 0;
356 ret = (*tp->op)(&context->ctx, outb, tp->oid.len,
357 tp->index, SNMP_OP_GET);
360 ret = (*tp->op)(&context->ctx, outb, tp->oid.len,
361 tp->index, SNMP_OP_GETNEXT);
363 if (ret != SNMP_ERR_NOSUCHNAME) {
365 if (ret != SNMP_ERR_NOERROR && TR(GETNEXT))
366 snmp_debug("getnext: %s returns %u",
367 asn_oid2str(&outb->var), ret);
371 /* object has no data - try next */
372 if (++tp == tree + tree_size)
376 snmp_debug("getnext: no data - avancing to %s",
377 asn_oid2str(&tp->oid));
382 if (ret == SNMP_ERR_NOSUCHNAME) {
384 outb->var = inb->var;
385 if (pdu->version == SNMP_V1) {
386 pdu->error_status = SNMP_ERR_NOSUCHNAME;
387 return (SNMP_RET_ERR);
389 outb->syntax = SNMP_SYNTAX_ENDOFMIBVIEW;
391 } else if (ret != SNMP_ERR_NOERROR) {
392 pdu->error_status = SNMP_ERR_GENERR;
393 return (SNMP_RET_ERR);
395 return (SNMP_RET_OK);
400 * Execute a GETNEXT operation. The tree is rooted at the global 'root'.
401 * Build the response PDU on the fly. The return is:
404 snmp_getnext(struct snmp_pdu *pdu, struct asn_buf *resp_b,
405 struct snmp_pdu *resp, void *data)
407 struct context context;
410 enum snmp_ret result;
412 memset(&context, 0, sizeof(context));
413 context.ctx.data = data;
415 snmp_pdu_create_response(pdu, resp);
417 if (snmp_pdu_encode_header(resp_b, resp))
418 return (SNMP_RET_IGN);
420 for (i = 0; i < pdu->nbindings; i++) {
421 result = do_getnext(&context, &pdu->bindings[i],
422 &resp->bindings[i], pdu);
424 if (result != SNMP_RET_OK) {
425 pdu->error_index = i + 1;
432 err = snmp_binding_encode(resp_b, &resp->bindings[i]);
434 if (err == ASN_ERR_EOBUF) {
435 pdu->error_status = SNMP_ERR_TOOBIG;
436 pdu->error_index = 0;
438 return (SNMP_RET_ERR);
440 if (err != ASN_ERR_OK) {
442 snmp_debug("getnext: binding encoding: %u", err);
443 pdu->error_status = SNMP_ERR_GENERR;
444 pdu->error_index = i + 1;
446 return (SNMP_RET_ERR);
450 if (snmp_fix_encoding(resp_b, resp) != SNMP_CODE_OK) {
451 snmp_debug("getnext: failed to encode PDU");
452 return (SNMP_RET_ERR);
455 return (SNMP_RET_OK);
459 snmp_getbulk(struct snmp_pdu *pdu, struct asn_buf *resp_b,
460 struct snmp_pdu *resp, void *data)
462 struct context context;
467 enum snmp_ret result;
470 memset(&context, 0, sizeof(context));
471 context.ctx.data = data;
473 snmp_pdu_create_response(pdu, resp);
475 if (snmp_pdu_encode_header(resp_b, resp) != SNMP_CODE_OK)
476 /* cannot even encode header - very bad */
477 return (SNMP_RET_IGN);
479 if ((non_rep = pdu->error_status) > pdu->nbindings)
480 non_rep = pdu->nbindings;
483 for (i = 0; i < non_rep; i++) {
484 result = do_getnext(&context, &pdu->bindings[i],
485 &resp->bindings[resp->nbindings], pdu);
487 if (result != SNMP_RET_OK) {
488 pdu->error_index = i + 1;
493 err = snmp_binding_encode(resp_b,
494 &resp->bindings[resp->nbindings++]);
496 if (err == ASN_ERR_EOBUF)
499 if (err != ASN_ERR_OK) {
501 snmp_debug("getnext: binding encoding: %u", err);
502 pdu->error_status = SNMP_ERR_GENERR;
503 pdu->error_index = i + 1;
505 return (SNMP_RET_ERR);
509 if (non_rep == pdu->nbindings)
513 for (cnt = 0; cnt < pdu->error_index; cnt++) {
515 for (i = non_rep; i < pdu->nbindings; i++) {
517 if (resp->nbindings == SNMP_MAX_BINDINGS)
522 result = do_getnext(&context, &pdu->bindings[i],
523 &resp->bindings[resp->nbindings], pdu);
525 result = do_getnext(&context,
526 &resp->bindings[resp->nbindings -
527 (pdu->nbindings - non_rep)],
528 &resp->bindings[resp->nbindings], pdu);
530 if (result != SNMP_RET_OK) {
531 pdu->error_index = i + 1;
535 if (resp->bindings[resp->nbindings].syntax !=
536 SNMP_SYNTAX_ENDOFMIBVIEW)
539 err = snmp_binding_encode(resp_b,
540 &resp->bindings[resp->nbindings++]);
542 if (err == ASN_ERR_EOBUF)
545 if (err != ASN_ERR_OK) {
547 snmp_debug("getnext: binding encoding: %u", err);
548 pdu->error_status = SNMP_ERR_GENERR;
549 pdu->error_index = i + 1;
551 return (SNMP_RET_ERR);
559 if (snmp_fix_encoding(resp_b, resp) != SNMP_CODE_OK) {
560 snmp_debug("getnext: failed to encode PDU");
561 return (SNMP_RET_ERR);
564 return (SNMP_RET_OK);
568 * Rollback a SET operation. Failed index is 'i'.
571 rollback(struct context *context, struct snmp_pdu *pdu, u_int i)
573 struct snmp_value *b;
574 const struct snmp_node *np;
578 b = &pdu->bindings[i];
579 np = context->node[i];
581 context->ctx.scratch = &context->scratch[i];
583 ret = (*np->op)(&context->ctx, b, np->oid.len, np->index,
586 if (ret != SNMP_ERR_NOERROR) {
587 snmp_error("set: rollback failed (%d) on variable %s "
588 "index %u", ret, asn_oid2str(&b->var), i);
589 if (pdu->version != SNMP_V1) {
590 pdu->error_status = SNMP_ERR_UNDO_FAILED;
591 pdu->error_index = 0;
598 * Commit dependencies.
601 snmp_dep_commit(struct snmp_context *ctx)
603 struct context *context = (struct context *)ctx;
606 TAILQ_FOREACH(context->depend, &context->dlist, link) {
607 ctx->dep = &context->depend->dep;
610 snmp_debug("set: dependency commit %s",
611 asn_oid2str(&ctx->dep->obj));
613 ret = context->depend->func(ctx, ctx->dep, SNMP_DEPOP_COMMIT);
615 if (ret != SNMP_ERR_NOERROR) {
617 snmp_debug("set: dependency failed %d", ret);
621 return (SNMP_ERR_NOERROR);
625 * Rollback dependencies
628 snmp_dep_rollback(struct snmp_context *ctx)
630 struct context *context = (struct context *)ctx;
632 char objbuf[ASN_OIDSTRLEN];
633 char idxbuf[ASN_OIDSTRLEN];
635 ret1 = SNMP_ERR_NOERROR;
636 while ((context->depend =
637 TAILQ_PREV(context->depend, depend_list, link)) != NULL) {
638 ctx->dep = &context->depend->dep;
641 snmp_debug("set: dependency rollback %s",
642 asn_oid2str(&ctx->dep->obj));
644 ret = context->depend->func(ctx, ctx->dep, SNMP_DEPOP_ROLLBACK);
646 if (ret != SNMP_ERR_NOERROR) {
647 snmp_debug("set: dep rollback returns %u: %s %s", ret,
648 asn_oid2str_r(&ctx->dep->obj, objbuf),
649 asn_oid2str_r(&ctx->dep->idx, idxbuf));
650 if (ret1 == SNMP_ERR_NOERROR)
658 snmp_dep_finish(struct snmp_context *ctx)
660 struct context *context = (struct context *)ctx;
663 while ((d = TAILQ_FIRST(&context->dlist)) != NULL) {
665 (void)d->func(ctx, ctx->dep, SNMP_DEPOP_FINISH);
666 TAILQ_REMOVE(&context->dlist, d, link);
672 * Do a SET operation.
675 snmp_set(struct snmp_pdu *pdu, struct asn_buf *resp_b,
676 struct snmp_pdu *resp, void *data)
681 struct context context;
682 const struct snmp_node *np;
683 struct snmp_value *b;
684 enum snmp_syntax except;
686 memset(&context, 0, sizeof(context));
687 TAILQ_INIT(&context.dlist);
688 context.ctx.data = data;
690 snmp_pdu_create_response(pdu, resp);
692 if (snmp_pdu_encode_header(resp_b, resp))
693 return (SNMP_RET_IGN);
696 * 1. Find all nodes, check that they are writeable and
697 * that the syntax is ok, copy over the binding to the response.
699 for (i = 0; i < pdu->nbindings; i++) {
700 b = &pdu->bindings[i];
702 if ((np = context.node[i] = find_node(b, &except)) == NULL) {
703 /* not found altogether or LEAF with wrong index */
705 snmp_debug("set: node not found %s",
706 asn_oid2str_r(&b->var, oidbuf));
707 if (pdu->version == SNMP_V1) {
708 pdu->error_index = i + 1;
709 pdu->error_status = SNMP_ERR_NOSUCHNAME;
710 } else if ((np = find_subnode(b)) != NULL) {
711 /* 2. intermediate object */
712 pdu->error_index = i + 1;
713 pdu->error_status = SNMP_ERR_NOT_WRITEABLE;
714 } else if (except == SNMP_SYNTAX_NOSUCHOBJECT) {
715 pdu->error_index = i + 1;
716 pdu->error_status = SNMP_ERR_NO_ACCESS;
718 pdu->error_index = i + 1;
719 pdu->error_status = SNMP_ERR_NO_CREATION;
722 return (SNMP_RET_ERR);
725 * 2. write/createable?
726 * Can check this for leafs only, because in v2 we have
727 * to differentiate between NOT_WRITEABLE and NO_CREATION
728 * and only the action routine for COLUMNS knows, whether
731 if (np->type == SNMP_NODE_LEAF &&
732 !(np->flags & SNMP_NODE_CANSET)) {
733 if (pdu->version == SNMP_V1) {
734 pdu->error_index = i + 1;
735 pdu->error_status = SNMP_ERR_NOSUCHNAME;
737 pdu->error_index = i + 1;
738 pdu->error_status = SNMP_ERR_NOT_WRITEABLE;
741 return (SNMP_RET_ERR);
744 * 3. Ensure the right syntax
746 if (np->syntax != b->syntax) {
747 if (pdu->version == SNMP_V1) {
748 pdu->error_index = i + 1;
749 pdu->error_status = SNMP_ERR_BADVALUE; /* v2: wrongType */
751 pdu->error_index = i + 1;
752 pdu->error_status = SNMP_ERR_WRONG_TYPE;
755 return (SNMP_RET_ERR);
760 if (snmp_value_copy(&resp->bindings[i], b)) {
761 pdu->error_index = i + 1;
762 pdu->error_status = SNMP_ERR_GENERR;
764 return (SNMP_RET_ERR);
766 asnerr = snmp_binding_encode(resp_b, &resp->bindings[i]);
767 if (asnerr == ASN_ERR_EOBUF) {
768 pdu->error_index = i + 1;
769 pdu->error_status = SNMP_ERR_TOOBIG;
771 return (SNMP_RET_ERR);
772 } else if (asnerr != ASN_ERR_OK) {
773 pdu->error_index = i + 1;
774 pdu->error_status = SNMP_ERR_GENERR;
776 return (SNMP_RET_ERR);
781 context.ctx.code = SNMP_RET_OK;
784 * 2. Call the SET method for each node. If a SET fails, rollback
785 * everything. Map error codes depending on the version.
787 for (i = 0; i < pdu->nbindings; i++) {
788 b = &pdu->bindings[i];
789 np = context.node[i];
791 context.ctx.var_index = i + 1;
792 context.ctx.scratch = &context.scratch[i];
794 ret = (*np->op)(&context.ctx, b, np->oid.len, np->index,
798 snmp_debug("set: action %s returns %d", np->name, ret);
800 if (pdu->version == SNMP_V1) {
802 case SNMP_ERR_NO_ACCESS:
803 ret = SNMP_ERR_NOSUCHNAME;
805 case SNMP_ERR_WRONG_TYPE:
806 /* should no happen */
807 ret = SNMP_ERR_BADVALUE;
809 case SNMP_ERR_WRONG_LENGTH:
810 ret = SNMP_ERR_BADVALUE;
812 case SNMP_ERR_WRONG_ENCODING:
813 /* should not happen */
814 ret = SNMP_ERR_BADVALUE;
816 case SNMP_ERR_WRONG_VALUE:
817 ret = SNMP_ERR_BADVALUE;
819 case SNMP_ERR_NO_CREATION:
820 ret = SNMP_ERR_NOSUCHNAME;
822 case SNMP_ERR_INCONS_VALUE:
823 ret = SNMP_ERR_BADVALUE;
825 case SNMP_ERR_RES_UNAVAIL:
826 ret = SNMP_ERR_GENERR;
828 case SNMP_ERR_COMMIT_FAILED:
829 ret = SNMP_ERR_GENERR;
831 case SNMP_ERR_UNDO_FAILED:
832 ret = SNMP_ERR_GENERR;
834 case SNMP_ERR_AUTH_ERR:
835 /* should not happen */
836 ret = SNMP_ERR_GENERR;
838 case SNMP_ERR_NOT_WRITEABLE:
839 ret = SNMP_ERR_NOSUCHNAME;
841 case SNMP_ERR_INCONS_NAME:
842 ret = SNMP_ERR_BADVALUE;
846 if (ret != SNMP_ERR_NOERROR) {
847 pdu->error_index = i + 1;
848 pdu->error_status = ret;
850 rollback(&context, pdu, i);
853 context.ctx.code = SNMP_RET_ERR;
860 * 3. Call dependencies
863 snmp_debug("set: set operations ok");
865 if ((ret = snmp_dep_commit(&context.ctx)) != SNMP_ERR_NOERROR) {
866 pdu->error_status = ret;
867 pdu->error_index = context.ctx.var_index;
869 if ((ret = snmp_dep_rollback(&context.ctx)) != SNMP_ERR_NOERROR) {
870 if (pdu->version != SNMP_V1) {
871 pdu->error_status = SNMP_ERR_UNDO_FAILED;
872 pdu->error_index = 0;
875 rollback(&context, pdu, i);
878 context.ctx.code = SNMP_RET_ERR;
884 * 4. Commit and copy values from the original packet to the response.
885 * This is not the commit operation from RFC 1905 but rather an
886 * 'FREE RESOURCES' operation. It shouldn't fail.
889 snmp_debug("set: commiting");
891 for (i = 0; i < pdu->nbindings; i++) {
892 b = &resp->bindings[i];
893 np = context.node[i];
895 context.ctx.var_index = i + 1;
896 context.ctx.scratch = &context.scratch[i];
898 ret = (*np->op)(&context.ctx, b, np->oid.len, np->index,
901 if (ret != SNMP_ERR_NOERROR)
902 snmp_error("set: commit failed (%d) on"
903 " variable %s index %u", ret,
904 asn_oid2str_r(&b->var, oidbuf), i);
907 if (snmp_fix_encoding(resp_b, resp) != SNMP_CODE_OK) {
908 snmp_error("set: fix_encoding failed");
910 context.ctx.code = SNMP_RET_IGN;
917 snmp_dep_finish(&context.ctx);
920 snmp_debug("set: returning %d", context.ctx.code);
922 return (context.ctx.code);
925 * Lookup a dependency. If it doesn't exist, create one
927 struct snmp_dependency *
928 snmp_dep_lookup(struct snmp_context *ctx, const struct asn_oid *obj,
929 const struct asn_oid *idx, size_t len, snmp_depop_t func)
931 struct context *context;
934 context = (struct context *)(void *)
935 ((char *)ctx - offsetof(struct context, ctx));
937 snmp_debug("depend: looking for %s", asn_oid2str(obj));
939 snmp_debug("depend: index is %s", asn_oid2str(idx));
941 TAILQ_FOREACH(d, &context->dlist, link)
942 if (asn_compare_oid(obj, &d->dep.obj) == 0 &&
943 ((idx == NULL && d->dep.idx.len == 0) ||
944 (idx != NULL && asn_compare_oid(idx, &d->dep.idx) == 0))) {
946 snmp_debug("depend: found");
951 snmp_debug("depend: creating");
953 if ((d = malloc(offsetof(struct depend, dep) + len)) == NULL)
955 memset(&d->dep, 0, len);
965 TAILQ_INSERT_TAIL(&context->dlist, d, link);
971 * Make an error response from a PDU. We do this without decoding the
972 * variable bindings. This means we can sent the junk back to a caller
973 * that has sent us junk in the first place.
976 snmp_make_errresp(const struct snmp_pdu *pdu, struct asn_buf *pdu_b,
977 struct asn_buf *resp_b)
981 struct snmp_pdu resp;
985 snmp_pdu_create_response(pdu, &resp);
987 if ((code = snmp_pdu_decode_header(pdu_b, &resp)) != SNMP_CODE_OK)
988 return (SNMP_RET_IGN);
990 if (pdu->version == SNMP_V3) {
991 if (resp.user.priv_proto != SNMP_PRIV_NOPRIV &&
992 (asn_get_header(pdu_b, &type, &resp.scoped_len) != ASN_ERR_OK
993 || type != ASN_TYPE_OCTETSTRING)) {
994 snmp_error("cannot decode encrypted pdu");
995 return (SNMP_RET_IGN);
998 if (asn_get_sequence(pdu_b, &len) != ASN_ERR_OK) {
999 snmp_error("cannot decode scoped pdu header");
1000 return (SNMP_RET_IGN);
1003 len = SNMP_ENGINE_ID_SIZ;
1004 if (asn_get_octetstring(pdu_b, (u_char *)resp.context_engine,
1005 &len) != ASN_ERR_OK) {
1006 snmp_error("cannot decode msg context engine");
1007 return (SNMP_RET_IGN);
1009 resp.context_engine_len = len;
1010 len = SNMP_CONTEXT_NAME_SIZ;
1011 if (asn_get_octetstring(pdu_b, (u_char *)resp.context_name,
1012 &len) != ASN_ERR_OK) {
1013 snmp_error("cannot decode msg context name");
1014 return (SNMP_RET_IGN);
1016 resp.context_name[len] = '\0';
1020 if (asn_get_header(pdu_b, &type, &len) != ASN_ERR_OK) {
1021 snmp_error("cannot get pdu header");
1022 return (SNMP_RET_IGN);
1025 if ((type & ~ASN_TYPE_MASK) !=
1026 (ASN_TYPE_CONSTRUCTED | ASN_CLASS_CONTEXT)) {
1027 snmp_error("bad pdu header tag");
1028 return (SNMP_RET_IGN);
1031 err = snmp_parse_pdus_hdr(pdu_b, &resp, &len);
1032 if (ASN_ERR_STOPPED(err))
1033 return (SNMP_RET_IGN);
1034 if (pdu_b->asn_len < len)
1035 return (SNMP_RET_IGN);
1036 pdu_b->asn_len = len;
1038 /* now we have the bindings left - construct new message */
1039 resp.error_status = pdu->error_status;
1040 resp.error_index = pdu->error_index;
1041 resp.type = SNMP_PDU_RESPONSE;
1043 code = snmp_pdu_encode_header(resp_b, &resp);
1044 if (code != SNMP_CODE_OK)
1045 return (SNMP_RET_IGN);
1047 if (pdu_b->asn_len > resp_b->asn_len)
1049 return (SNMP_RET_IGN);
1050 (void)memcpy(resp_b->asn_ptr, pdu_b->asn_cptr, pdu_b->asn_len);
1051 resp_b->asn_len -= pdu_b->asn_len;
1052 resp_b->asn_ptr += pdu_b->asn_len;
1054 code = snmp_fix_encoding(resp_b, &resp);
1055 if (code != SNMP_CODE_OK)
1056 return (SNMP_RET_IGN);
1058 return (SNMP_RET_OK);
1062 snmp_debug_func(const char *fmt, ...)
1067 vfprintf(stderr, fmt, ap);
1069 fprintf(stderr, "\n");