1*3c554feaSmartijn /* $OpenBSD: application.h,v 1.13 2023/11/12 16:07:34 martijn Exp $ */ 27f594a49Smartijn 37f594a49Smartijn /* 47f594a49Smartijn * Copyright (c) 2021 Martijn van Duren <martijn@openbsd.org> 57f594a49Smartijn * 67f594a49Smartijn * Permission to use, copy, modify, and distribute this software for any 77f594a49Smartijn * purpose with or without fee is hereby granted, provided that the above 87f594a49Smartijn * copyright notice and this permission notice appear in all copies. 97f594a49Smartijn * 107f594a49Smartijn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 117f594a49Smartijn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 127f594a49Smartijn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 137f594a49Smartijn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 147f594a49Smartijn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 157f594a49Smartijn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 167f594a49Smartijn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 177f594a49Smartijn */ 187f594a49Smartijn 197f594a49Smartijn #include <sys/tree.h> 207f594a49Smartijn 217f594a49Smartijn #include <ber.h> 227f594a49Smartijn 237f594a49Smartijn #include <stdint.h> 247f594a49Smartijn 257f594a49Smartijn #define APPL_OIDMAX 128 /* RFC 2578 Section 3.5 */ 267f594a49Smartijn #define APPL_CONTEXTNAME_MAX 32 /* RFC 3415 vacmContextName */ 277f594a49Smartijn 287f594a49Smartijn /* Combination of RFC 3416 error-status and RFC 2741 res.error */ 297f594a49Smartijn enum appl_error { 307f594a49Smartijn APPL_ERROR_NOERROR = 0, 317f594a49Smartijn APPL_ERROR_TOOBIG = 1, 327f594a49Smartijn APPL_ERROR_NOSUCHNAME = 2, 337f594a49Smartijn APPL_ERROR_BADVALUE = 3, 347f594a49Smartijn APPL_ERROR_READONLY = 4, 357f594a49Smartijn APPL_ERROR_GENERR = 5, 367f594a49Smartijn APPL_ERROR_NOACCESS = 6, 377f594a49Smartijn APPL_ERROR_WRONGTYPE = 7, 387f594a49Smartijn APPL_ERROR_WRONGLENGTH = 8, 397f594a49Smartijn APPL_ERROR_WRONGENCODING = 9, 407f594a49Smartijn APPL_ERROR_WRONGVALUE = 10, 417f594a49Smartijn APPL_ERROR_NOCREATION = 11, 427f594a49Smartijn APPL_ERROR_INCONSISTENTVALUE = 12, 437f594a49Smartijn APPL_ERROR_RESOURCEUNAVAILABLE = 13, 447f594a49Smartijn APPL_ERROR_COMMITFAILED = 14, 457f594a49Smartijn APPL_ERROR_UNDOFAILED = 15, 467f594a49Smartijn APPL_ERROR_AUTHORIZATIONERROR = 16, 477f594a49Smartijn APPL_ERROR_NOTWRITABLE = 17, 487f594a49Smartijn APPL_ERROR_INCONSISTENTNAME = 18, 497f594a49Smartijn APPL_ERROR_OPENFAILED = 256, 507f594a49Smartijn APPL_ERROR_NOTOPEN = 257, 517f594a49Smartijn APPL_ERROR_INDEXWRONGTYPE = 258, 527f594a49Smartijn APPL_ERROR_INDEXALREADYALLOCATED= 259, 537f594a49Smartijn APPL_ERROR_INDEXNONEAVAILABLE = 260, 547f594a49Smartijn APPL_ERROR_INDEXNOTALLOCATED = 261, 557f594a49Smartijn APPL_ERROR_UNSUPPORTEDCONTEXT = 262, 567f594a49Smartijn APPL_ERROR_DUPLICATEREGISTRATION= 263, 577f594a49Smartijn APPL_ERROR_UNKNOWNREGISTRATION = 264, 587f594a49Smartijn APPL_ERROR_UNKNOWNAGENTCAPS = 265, 597f594a49Smartijn APPL_ERROR_PARSEERROR = 266, 607f594a49Smartijn APPL_ERROR_REQUESTDENIED = 267, 617f594a49Smartijn APPL_ERROR_PROCESSINGERROR = 268 627f594a49Smartijn }; 637f594a49Smartijn 647f594a49Smartijn enum appl_exception { 657f594a49Smartijn APPL_EXC_NOSUCHOBJECT = 0, 667f594a49Smartijn APPL_EXC_NOSUCHINSTANCE = 1, 677f594a49Smartijn APPL_EXC_ENDOFMIBVIEW = 2 687f594a49Smartijn }; 697f594a49Smartijn 707f594a49Smartijn enum appl_close_reason { 717f594a49Smartijn APPL_CLOSE_REASONOTHER = 1, 727f594a49Smartijn APPL_CLOSE_REASONPARSEERROR = 2, 737f594a49Smartijn APPL_CLOSE_REASONPROTOCOLERROR = 3, 747f594a49Smartijn APPL_CLOSE_REASONTIMEOUTS = 4, 757f594a49Smartijn APPL_CLOSE_REASONSHUTDOWN = 5, 767f594a49Smartijn APPL_CLOSE_REASONBYMANAGER = 6 777f594a49Smartijn }; 787f594a49Smartijn 797f594a49Smartijn struct appl_varbind { 807f594a49Smartijn int8_t av_include; /* RFC 2741 section 5.1 */ 817f594a49Smartijn struct ber_oid av_oid; 827f594a49Smartijn struct ber_oid av_oid_end; 837f594a49Smartijn struct ber_element *av_value; 847f594a49Smartijn 857f594a49Smartijn struct appl_varbind *av_next; 867f594a49Smartijn }; 877f594a49Smartijn 887f594a49Smartijn struct snmp_message; 897f594a49Smartijn enum snmp_version; 907f594a49Smartijn struct appl_backend; 91253352b6Smartijn struct appl_context; 927f594a49Smartijn 937f594a49Smartijn struct appl_backend_functions { 947f594a49Smartijn void (*ab_close)(struct appl_backend *, enum appl_close_reason); 957f594a49Smartijn void (*ab_get)(struct appl_backend *, int32_t, int32_t, const char *, 967f594a49Smartijn struct appl_varbind *); 977f594a49Smartijn void (*ab_getnext)(struct appl_backend *, int32_t, int32_t, const char *, 987f594a49Smartijn struct appl_varbind *); 997f594a49Smartijn /* 1007f594a49Smartijn * RFC 3416 section 3: non-repeaters/max-repetitions = 0..max-bindings 1017f594a49Smartijn * max-bindings = (2^31)-1 1027f594a49Smartijn * RFC 2741 section 6.2.7: non-repeaters/max-repetitions = 2 bytes 1037f594a49Smartijn * Go for the lowest common denominator. 1047f594a49Smartijn */ 1057f594a49Smartijn void (*ab_getbulk)(struct appl_backend *, int32_t, int32_t, int16_t, 1067f594a49Smartijn int16_t, const char *, struct appl_varbind *); 1077f594a49Smartijn }; 1087f594a49Smartijn 1097f594a49Smartijn struct appl_backend { 1107f594a49Smartijn char *ab_name; 1117f594a49Smartijn void *ab_cookie; 1127f594a49Smartijn uint8_t ab_retries; 1131345d1c8Smartijn int ab_range; /* Supports searchrange */ 1147f594a49Smartijn struct appl_backend_functions *ab_fn; 1157f594a49Smartijn /* 1167f594a49Smartijn * Only store downstream requests: they reference upstream and when 1177f594a49Smartijn * downstream requests are done the upstream request is finalized. 1187f594a49Smartijn */ 1197f594a49Smartijn RB_HEAD(appl_requests, appl_request_downstream) ab_requests; 1207f594a49Smartijn }; 1217f594a49Smartijn 1224100cc5fSmartijn void appl(void); 1237f594a49Smartijn void appl_init(void); 1247f594a49Smartijn void appl_shutdown(void); 125253352b6Smartijn struct appl_context *appl_context(const char *, int); 1262993c1b4Smartijn enum appl_error appl_addagentcaps(const char *, struct ber_oid *, const char *, 1272993c1b4Smartijn struct appl_backend *); 1282993c1b4Smartijn enum appl_error appl_removeagentcaps(const char *, struct ber_oid *, 1292993c1b4Smartijn struct appl_backend *); 130215fd4bcSmartijn struct ber_element *appl_sysorlastchange(struct ber_oid *); 131215fd4bcSmartijn struct ber_element *appl_sysortable(struct ber_oid *); 132215fd4bcSmartijn struct ber_element *appl_sysortable_getnext(int8_t, struct ber_oid *); 13392d4cb1dSmartijn struct ber_element *appl_targetmib(struct ber_oid *); 1347f594a49Smartijn enum appl_error appl_register(const char *, uint32_t, uint8_t, struct ber_oid *, 1357f594a49Smartijn int, int, uint8_t, uint32_t, struct appl_backend *); 1367f594a49Smartijn enum appl_error appl_unregister(const char *, uint8_t, struct ber_oid *, 1377f594a49Smartijn uint8_t, uint32_t, struct appl_backend *); 1387f594a49Smartijn void appl_close(struct appl_backend *); 1397f594a49Smartijn void appl_processpdu(struct snmp_message *, const char *, 1407f594a49Smartijn enum snmp_version , struct ber_element *); 1417f594a49Smartijn void appl_response(struct appl_backend *, int32_t, enum appl_error, int16_t, 1427f594a49Smartijn struct appl_varbind *); 1437ffaedafSmartijn void appl_report(struct snmp_message *, int32_t, struct ber_oid *); 1447f594a49Smartijn struct ber_element *appl_exception(enum appl_exception); 1457f594a49Smartijn 1464100cc5fSmartijn /* application_agentx.c */ 1474100cc5fSmartijn void appl_agentx(void); 1484100cc5fSmartijn void appl_agentx_init(void); 1494100cc5fSmartijn void appl_agentx_shutdown(void); 1501985d3ebSmartijn void appl_agentx_backend(int); 1514100cc5fSmartijn 152614c3698Smartijn /* application_blocklist.c */ 153614c3698Smartijn void appl_blocklist_init(void); 154614c3698Smartijn void appl_blocklist_shutdown(void); 15516365c53Smartijn 15616365c53Smartijn /* application_internal.c */ 15716365c53Smartijn void appl_internal_init(void); 15816365c53Smartijn void appl_internal_shutdown(void); 1599352f69fSmartijn const char *appl_internal_object_int(struct ber_oid *, int32_t); 1609352f69fSmartijn const char *appl_internal_object_string(struct ber_oid *, char *); 161