xref: /openbsd/usr.sbin/snmpd/application.h (revision 3c554fea)
1 /*	$OpenBSD: application.h,v 1.13 2023/11/12 16:07:34 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 struct appl_context;
92 
93 struct appl_backend_functions {
94 	void (*ab_close)(struct appl_backend *, enum appl_close_reason);
95 	void (*ab_get)(struct appl_backend *, int32_t, int32_t, const char *,
96 	    struct appl_varbind *);
97 	void (*ab_getnext)(struct appl_backend *, int32_t, int32_t, const char *,
98 	    struct appl_varbind *);
99 	/*
100 	 * RFC 3416 section 3: non-repeaters/max-repetitions = 0..max-bindings
101 	 * max-bindings = (2^31)-1
102 	 * RFC 2741 section 6.2.7: non-repeaters/max-repetitions = 2 bytes
103 	 * Go for the lowest common denominator.
104 	 */
105 	void (*ab_getbulk)(struct appl_backend *, int32_t, int32_t, int16_t,
106 	    int16_t, const char *, struct appl_varbind *);
107 };
108 
109 struct appl_backend {
110 	char *ab_name;
111 	void *ab_cookie;
112 	uint8_t ab_retries;
113 	int ab_range; /* Supports searchrange */
114 	struct appl_backend_functions *ab_fn;
115 	/*
116 	 * Only store downstream requests: they reference upstream and when
117 	 * downstream requests are done the upstream request is finalized.
118 	 */
119 	RB_HEAD(appl_requests, appl_request_downstream) ab_requests;
120 };
121 
122 void appl(void);
123 void appl_init(void);
124 void appl_shutdown(void);
125 struct appl_context *appl_context(const char *, int);
126 enum appl_error appl_addagentcaps(const char *, struct ber_oid *, const char *,
127     struct appl_backend *);
128 enum appl_error appl_removeagentcaps(const char *, struct ber_oid *,
129     struct appl_backend *);
130 struct ber_element *appl_sysorlastchange(struct ber_oid *);
131 struct ber_element *appl_sysortable(struct ber_oid *);
132 struct ber_element *appl_sysortable_getnext(int8_t, struct ber_oid *);
133 struct ber_element *appl_targetmib(struct ber_oid *);
134 enum appl_error appl_register(const char *, uint32_t, uint8_t, struct ber_oid *,
135     int, int, uint8_t, uint32_t, struct appl_backend *);
136 enum appl_error appl_unregister(const char *, uint8_t, struct ber_oid *,
137     uint8_t, uint32_t, struct appl_backend *);
138 void appl_close(struct appl_backend *);
139 void appl_processpdu(struct snmp_message *, const char *,
140     enum snmp_version , struct ber_element *);
141 void appl_response(struct appl_backend *, int32_t, enum appl_error, int16_t,
142     struct appl_varbind *);
143 void appl_report(struct snmp_message *, int32_t, struct ber_oid *);
144 struct ber_element *appl_exception(enum appl_exception);
145 
146 /* application_agentx.c */
147 void	 appl_agentx(void);
148 void	 appl_agentx_init(void);
149 void	 appl_agentx_shutdown(void);
150 void	 appl_agentx_backend(int);
151 
152 /* application_blocklist.c */
153 void	 appl_blocklist_init(void);
154 void	 appl_blocklist_shutdown(void);
155 
156 /* application_internal.c */
157 void	 appl_internal_init(void);
158 void	 appl_internal_shutdown(void);
159 const char *appl_internal_object_int(struct ber_oid *, int32_t);
160 const char *appl_internal_object_string(struct ber_oid *, char *);
161