xref: /openbsd-src/usr.sbin/snmpd/application.h (revision 25c4e8bd056e974b28f4a0ffd39d76c190a56013)
1 /*	$OpenBSD: application.h,v 1.2 2022/06/30 11:28:36 martijn Exp $	*/
2 
3 /*
4  * Copyright (c) 2021 Martijn van Duren <martijn@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/tree.h>
20 
21 #include <ber.h>
22 
23 #include <stdint.h>
24 
25 #define APPL_OIDMAX 128 /* RFC 2578 Section 3.5 */
26 #define APPL_CONTEXTNAME_MAX 32 /* RFC 3415 vacmContextName */
27 
28 /* Combination of RFC 3416 error-status and RFC 2741 res.error */
29 enum appl_error {
30 	APPL_ERROR_NOERROR		= 0,
31 	APPL_ERROR_TOOBIG		= 1,
32 	APPL_ERROR_NOSUCHNAME		= 2,
33 	APPL_ERROR_BADVALUE		= 3,
34 	APPL_ERROR_READONLY		= 4,
35 	APPL_ERROR_GENERR		= 5,
36 	APPL_ERROR_NOACCESS		= 6,
37 	APPL_ERROR_WRONGTYPE		= 7,
38 	APPL_ERROR_WRONGLENGTH		= 8,
39 	APPL_ERROR_WRONGENCODING	= 9,
40 	APPL_ERROR_WRONGVALUE		= 10,
41 	APPL_ERROR_NOCREATION		= 11,
42 	APPL_ERROR_INCONSISTENTVALUE	= 12,
43 	APPL_ERROR_RESOURCEUNAVAILABLE	= 13,
44 	APPL_ERROR_COMMITFAILED		= 14,
45 	APPL_ERROR_UNDOFAILED		= 15,
46 	APPL_ERROR_AUTHORIZATIONERROR	= 16,
47 	APPL_ERROR_NOTWRITABLE		= 17,
48 	APPL_ERROR_INCONSISTENTNAME	= 18,
49 	APPL_ERROR_OPENFAILED		= 256,
50 	APPL_ERROR_NOTOPEN		= 257,
51 	APPL_ERROR_INDEXWRONGTYPE	= 258,
52 	APPL_ERROR_INDEXALREADYALLOCATED= 259,
53 	APPL_ERROR_INDEXNONEAVAILABLE	= 260,
54 	APPL_ERROR_INDEXNOTALLOCATED	= 261,
55 	APPL_ERROR_UNSUPPORTEDCONTEXT	= 262,
56 	APPL_ERROR_DUPLICATEREGISTRATION= 263,
57 	APPL_ERROR_UNKNOWNREGISTRATION	= 264,
58 	APPL_ERROR_UNKNOWNAGENTCAPS	= 265,
59 	APPL_ERROR_PARSEERROR		= 266,
60 	APPL_ERROR_REQUESTDENIED	= 267,
61 	APPL_ERROR_PROCESSINGERROR	= 268
62 };
63 
64 enum appl_exception {
65 	APPL_EXC_NOSUCHOBJECT		= 0,
66 	APPL_EXC_NOSUCHINSTANCE		= 1,
67 	APPL_EXC_ENDOFMIBVIEW		= 2
68 };
69 
70 enum appl_close_reason {
71 	APPL_CLOSE_REASONOTHER		= 1,
72 	APPL_CLOSE_REASONPARSEERROR	= 2,
73 	APPL_CLOSE_REASONPROTOCOLERROR	= 3,
74 	APPL_CLOSE_REASONTIMEOUTS	= 4,
75 	APPL_CLOSE_REASONSHUTDOWN	= 5,
76 	APPL_CLOSE_REASONBYMANAGER	= 6
77 };
78 
79 struct appl_varbind {
80 	int8_t av_include; /* RFC 2741 section 5.1 */
81 	struct ber_oid av_oid;
82 	struct ber_oid av_oid_end;
83 	struct ber_element *av_value;
84 
85 	struct appl_varbind *av_next;
86 };
87 
88 struct snmp_message;
89 enum snmp_version;
90 struct appl_backend;
91 
92 struct appl_backend_functions {
93 	void (*ab_close)(struct appl_backend *, enum appl_close_reason);
94 	void (*ab_get)(struct appl_backend *, int32_t, int32_t, const char *,
95 	    struct appl_varbind *);
96 	void (*ab_getnext)(struct appl_backend *, int32_t, int32_t, const char *,
97 	    struct appl_varbind *);
98 	/*
99 	 * RFC 3416 section 3: non-repeaters/max-repetitions = 0..max-bindings
100 	 * max-bindings = (2^31)-1
101 	 * RFC 2741 section 6.2.7: non-repeaters/max-repetitions = 2 bytes
102 	 * Go for the lowest common denominator.
103 	 */
104 	void (*ab_getbulk)(struct appl_backend *, int32_t, int32_t, int16_t,
105 	    int16_t, const char *, struct appl_varbind *);
106 };
107 
108 struct appl_backend {
109 	char *ab_name;
110 	void *ab_cookie;
111 	uint8_t ab_retries;
112 	struct appl_backend_functions *ab_fn;
113 	/*
114 	 * Only store downstream requests: they reference upstream and when
115 	 * downstream requests are done the upstream request is finalized.
116 	 */
117 	RB_HEAD(appl_requests, appl_request_downstream) ab_requests;
118 };
119 
120 void appl_init(void);
121 void appl_shutdown(void);
122 enum appl_error appl_register(const char *, uint32_t, uint8_t, struct ber_oid *,
123     int, int, uint8_t, uint32_t, struct appl_backend *);
124 enum appl_error appl_unregister(const char *, uint8_t, struct ber_oid *,
125     uint8_t, uint32_t, struct appl_backend *);
126 void appl_close(struct appl_backend *);
127 void appl_processpdu(struct snmp_message *, const char *,
128     enum snmp_version , struct ber_element *);
129 void appl_response(struct appl_backend *, int32_t, enum appl_error, int16_t,
130     struct appl_varbind *);
131 struct ber_element *appl_exception(enum appl_exception);
132 
133 /* application_legacy.c */
134 void	 appl_legacy_init(void);
135 void	 appl_legacy_shutdown(void);
136 
137 /* application_blocklist.c */
138 void	 appl_blocklist_init(void);
139 void	 appl_blocklist_shutdown(void);
140