xref: /freebsd/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.h (revision 665877ced9ea53f838f80176739a8621a61d40c6)
1 /*-
2  * Copyright (c) 2005-2006 The FreeBSD Project
3  * All rights reserved.
4  *
5  * Author: Shteryana Shopova <syrinx@FreeBSD.org>
6  *
7  * Redistribution of this software and documentation and use in source and
8  * binary forms, with or without modification, are permitted provided that
9  * the following conditions are met:
10  *
11  * 1. Redistributions of source code or documentation must retain the above
12  *    copyright 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.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE 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 THE 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
27  * SUCH DAMAGE.
28  *
29  * Helper functions common for all tools.
30  */
31 
32 #ifndef	_BSNMP_TOOLS_H_
33 #define	_BSNMP_TOOLS_H_
34 
35 /* From asn1.h + 1 byte for trailing zero. */
36 #define	MAX_OCTSTRING_LEN	ASN_MAXOCTETSTRING + 1
37 #define	MAX_CMD_SYNTAX_LEN	12
38 
39 /* Arbitrary upper limit on node names and function names - gensnmptree.c. */
40 #define	MAXSTR			1000
41 
42 /* Should be enough to fetch the biggest allowed octet string. */
43 #define	MAX_BUFF_SIZE		(ASN_MAXOCTETSTRING + 50)
44 
45 #define	SNMP_DEFS_DIR		"/usr/share/snmp/defs/"
46 
47 #define	SNMP_MAX_REPETITIONS	10
48 
49 enum snmp_access {
50 	SNMP_ACCESS_NONE = 0,
51 	SNMP_ACCESS_GET,
52 	SNMP_ACCESS_SET,
53 	SNMP_ACCESS_GETSET,
54 };
55 
56 /* A structure for integer-string enumerations. */
57 struct enum_pair {
58 	int32_t	enum_val;
59 	char	*enum_str;
60 	STAILQ_ENTRY(enum_pair)	link;
61 };
62 
63 STAILQ_HEAD(enum_pairs, enum_pair);
64 
65 struct enum_type {
66 	char		*name;
67 	uint32_t	syntax;
68 	int32_t		is_enum;
69 	int32_t		is_bits;
70 	struct enum_pairs	*snmp_enum;
71 	SLIST_ENTRY(enum_type)	link;
72 };
73 
74 SLIST_HEAD(snmp_enum_tc, enum_type);
75 
76 struct index {
77 	enum snmp_tc		tc;
78 	enum snmp_syntax	syntax;
79 	struct enum_pairs	*snmp_enum;
80 	STAILQ_ENTRY(index)	link;
81 };
82 
83 STAILQ_HEAD(snmp_idxlist, index);
84 
85 struct snmp_index_entry {
86 	char			*string;
87 	uint32_t		strlen;
88 	struct asn_oid		var;
89 	struct snmp_idxlist	index_list;
90 	SLIST_ENTRY(snmp_index_entry)	link;
91 };
92 
93 /* Information needed for oid to string conversion. */
94 struct snmp_oid2str {
95 	char			*string;
96 	uint32_t		strlen;
97 	enum snmp_tc		tc;
98 	enum snmp_syntax	syntax;
99 	enum snmp_access	access;
100 	struct asn_oid		var;
101 	/* A pointer to a entry from the table list - OK if NULL. */
102 	struct snmp_index_entry	*table_idx;
103 	/*
104 	 * A singly-linked tail queue of all (int, string) pairs -
105 	 * for INTEGER syntax only.
106 	 */
107 	struct enum_pairs	*snmp_enum;
108 	SLIST_ENTRY(snmp_oid2str)	link;
109 };
110 
111 /* A structure to hold each oid input by user. */
112 struct snmp_object {
113 	/* Flag - if set, the variable caused error in a previous request. */
114 	int32_t			error;
115 	/*
116 	 * A pointer in the mapping lists - not used if OIDs are input as
117 	 * numericals.
118 	 */
119 	struct snmp_oid2str	*info;
120 	/* A snmp value to hold the actual oid, syntax and value. */
121 	struct snmp_value	val;
122 	SLIST_ENTRY(snmp_object)	link;
123 };
124 
125 struct fname {
126 	char		*name;
127 	int32_t		done;
128 	struct asn_oid	cut;
129 	SLIST_ENTRY(fname)	link;
130 };
131 
132 SLIST_HEAD(snmp_mapping, snmp_oid2str);
133 SLIST_HEAD(fname_list, fname);
134 SLIST_HEAD(snmp_table_index, snmp_index_entry);
135 
136 /*
137  * Keep a list for every syntax type.
138  */
139 struct snmp_mappings {
140 	/* The list containing all non-leaf nodes. */
141 	struct snmp_mapping		nodelist;
142 	/* INTEGER/INTEGER32 types. */
143 	struct snmp_mapping		intlist;
144 	/* OCTETSTRING types. */
145 	struct snmp_mapping		octlist;
146 	/* OID types. */
147 	struct snmp_mapping		oidlist;
148 	/* IPADDRESS types. */
149 	struct snmp_mapping		iplist;
150 	/* TIMETICKS types. */
151 	struct snmp_mapping		ticklist;
152 	/* COUNTER types. */
153 	struct snmp_mapping		cntlist;
154 	/* GAUGE types. */
155 	struct snmp_mapping		gaugelist;
156 	/* COUNTER64 types. */
157 	struct snmp_mapping		cnt64list;
158 	/* ENUM values for oid types. */
159 	struct snmp_mapping		enumlist;
160 	/* Description of all table entry types. */
161 	struct snmp_table_index		tablelist;
162 	/* Defined enumerated textual conventions. */
163 	struct snmp_enum_tc		tclist;
164 };
165 
166 struct snmp_toolinfo {
167 	uint32_t					flags;
168 	/* Number of initially input OIDs. */
169 	int32_t						objects;
170 	/* List of all input OIDs. */
171 	SLIST_HEAD(snmp_objectlist, snmp_object)	snmp_objectlist;
172 	/* All known OID to string mapping data. */
173 	struct snmp_mappings				*mappings;
174 	/* A list of .defs filenames to search oid<->string mapping. */
175 	struct fname_list				filelist;
176 	/* SNMPv3 USM user credentials */
177 	char						*passwd;
178 };
179 
180 /* XXX we might want to get away with this and will need to touch
181  * XXX the MACROS then too */
182 extern struct snmp_toolinfo snmptool;
183 
184 /* Definitions for some flags' bits. */
185 #define	OUTPUT_BITS	0x00000003	/* bits 0-1 for output type */
186 #define	NUMERIC_BIT	0x00000004	/* bit 2 for numeric oids */
187 #define	RETRY_BIT	0x00000008 	/* bit 3 for retry on error response */
188 #define	ERRIGNORE_BIT	0x00000010	/* bit 4 for skip sanity checking */
189 #define	ERRIGNORE_BIT	0x00000010	/* bit 4 for skip sanity checking */
190 #define	EDISCOVER_BIT	0x00000020	/* bit 5 for SNMP Engine Discovery */
191 #define	LOCALKEY_BIT	0x00000040	/* bit 6 for using localized key */
192 		/*	0x00000080 */	/* bit 7 reserved */
193 #define	PDUTYPE_BITS	0x00000f00	/* bits 8-11 for pdu type */
194 		/*	0x0000f000 */	/* bit 12-15 reserved */
195 #define	MAXREP_BITS	0x00ff0000	/* bits 16-23 for max-repetit. value */
196 #define	NONREP_BITS	0xff000000	/* bits 24-31 for non-repeaters value */
197 
198 #define	OUTPUT_SHORT		0x0
199 #define	OUTPUT_VERBOSE		0x1
200 #define	OUTPUT_TABULAR		0x2
201 #define	OUTPUT_QUIET		0x3
202 
203 /* Macros for playing with flags' bits. */
204 #define	SET_OUTPUT(ctx, type)	((ctx)->flags |= ((type) & OUTPUT_BITS))
205 #define	GET_OUTPUT(ctx)		((ctx)->flags & OUTPUT_BITS)
206 
207 #define	SET_NUMERIC(ctx)	((ctx)->flags |= NUMERIC_BIT)
208 #define	ISSET_NUMERIC(ctx)	((ctx)->flags & NUMERIC_BIT)
209 
210 #define	SET_RETRY(ctx)		((ctx)->flags |= RETRY_BIT)
211 #define	ISSET_RETRY(ctx)	((ctx)->flags & RETRY_BIT)
212 
213 #define	SET_ERRIGNORE(ctx)	((ctx)->flags |= ERRIGNORE_BIT)
214 #define	ISSET_ERRIGNORE(ctx)	((ctx)->flags & ERRIGNORE_BIT)
215 
216 #define	SET_EDISCOVER(ctx)	((ctx)->flags |= EDISCOVER_BIT)
217 #define	ISSET_EDISCOVER(ctx)	((ctx)->flags & EDISCOVER_BIT)
218 
219 #define	SET_LOCALKEY(ctx)	((ctx)->flags |= LOCALKEY_BIT)
220 #define	ISSET_LOCALKEY(ctx)	((ctx)->flags & LOCALKEY_BIT)
221 
222 #define	SET_PDUTYPE(ctx, type)	(((ctx)->flags |= (((type) & 0xf) << 8)))
223 #define	GET_PDUTYPE(ctx)	(((ctx)->flags & PDUTYPE_BITS) >> 8)
224 
225 #define	SET_MAXREP(ctx, i)	(((ctx)->flags |= (((i) & 0xff) << 16)))
226 #define	GET_MAXREP(ctx)		(((ctx)->flags & MAXREP_BITS) >> 16)
227 
228 #define	SET_NONREP(ctx, i)	(((ctx)->flags |= (((i) & 0xff) << 24)))
229 #define	GET_NONREP(ctx)		(((ctx)->flags & NONREP_BITS) >> 24)
230 
231 extern int _bsnmptools_debug;
232 extern const struct asn_oid IsoOrgDod_OID;
233 
234 int snmptool_init(struct snmp_toolinfo *);
235 int32_t snmp_import_file(struct snmp_toolinfo *, struct fname *);
236 int32_t snmp_import_all(struct snmp_toolinfo *);
237 int32_t add_filename(struct snmp_toolinfo *, const char *,
238     const struct asn_oid *, int32_t);
239 void free_filelist(struct snmp_toolinfo *);
240 void snmp_tool_freeall(struct snmp_toolinfo *);
241 void snmp_import_dump(int);
242 
243 /* bsnmpmap.c */
244 struct snmp_mappings *snmp_mapping_init(void);
245 int32_t snmp_mapping_free(struct snmp_toolinfo *);
246 void snmp_index_listfree(struct snmp_idxlist *);
247 void snmp_dump_oid2str(struct snmp_oid2str *);
248 int32_t snmp_node_insert(struct snmp_toolinfo *, struct snmp_oid2str *);
249 int32_t snmp_leaf_insert(struct snmp_toolinfo *, struct snmp_oid2str *);
250 int32_t snmp_enum_insert(struct snmp_toolinfo *, struct snmp_oid2str *);
251 struct enum_pairs *enum_pairs_init(void);
252 void enum_pairs_free(struct enum_pairs *);
253 void snmp_mapping_entryfree(struct snmp_oid2str *);
254 int32_t enum_pair_insert(struct enum_pairs *, int32_t, char *);
255 char *enum_string_lookup(struct enum_pairs *, int32_t);
256 int32_t enum_number_lookup(struct enum_pairs *, char *);
257 int32_t snmp_syntax_insert(struct snmp_idxlist *, struct enum_pairs *,
258     enum snmp_syntax, enum snmp_tc);
259 int32_t snmp_table_insert(struct snmp_toolinfo *, struct snmp_index_entry *);
260 
261 struct enum_type *snmp_enumtc_init(char *);
262 void snmp_enumtc_free(struct enum_type *);
263 void snmp_enumtc_insert(struct snmp_toolinfo *, struct enum_type *);
264 struct enum_type *snmp_enumtc_lookup(struct snmp_toolinfo *, char *);
265 
266 void snmp_mapping_dump(struct snmp_toolinfo *);
267 int32_t snmp_lookup_leafstring(struct snmp_toolinfo *, struct snmp_object *);
268 int32_t snmp_lookup_enumstring(struct snmp_toolinfo *, struct snmp_object *);
269 int32_t snmp_lookup_oidstring(struct snmp_toolinfo *, struct snmp_object *);
270 int32_t snmp_lookup_nonleaf_string(struct snmp_toolinfo *, struct snmp_object *);
271 int32_t snmp_lookup_allstring(struct snmp_toolinfo *, struct snmp_object *);
272 int32_t snmp_lookup_nodestring(struct snmp_toolinfo *, struct snmp_object *);
273 int32_t snmp_lookup_oidall(struct snmp_toolinfo *, struct snmp_object *, char *);
274 int32_t snmp_lookup_enumoid(struct snmp_toolinfo *, struct snmp_object *, char *);
275 int32_t snmp_lookup_oid(struct snmp_toolinfo *, struct snmp_object *, char *);
276 
277 /* Functions parsing common options for all tools. */
278 int32_t parse_server(char *);
279 int32_t parse_timeout(char *);
280 int32_t parse_retry(char *);
281 int32_t parse_version(char *);
282 int32_t parse_local_path(char *);
283 int32_t parse_buflen(char *);
284 int32_t parse_debug(void);
285 int32_t parse_discovery(struct snmp_toolinfo *);
286 int32_t parse_local_key(struct snmp_toolinfo *);
287 int32_t parse_num_oids(struct snmp_toolinfo *);
288 int32_t parse_file(struct snmp_toolinfo *, char *);
289 int32_t parse_include(struct snmp_toolinfo *, char *);
290 int32_t parse_output(struct snmp_toolinfo *, char *);
291 int32_t parse_errors(struct snmp_toolinfo *);
292 int32_t parse_skip_access(struct snmp_toolinfo *);
293 int32_t parse_authentication(struct snmp_toolinfo *, char *);
294 int32_t parse_privacy(struct snmp_toolinfo *, char *);
295 int32_t parse_context(struct snmp_toolinfo *, char *);
296 int32_t parse_user_security(struct snmp_toolinfo *, char *);
297 
298 typedef int32_t (*snmp_verify_inoid_f) (struct snmp_toolinfo *,
299     struct snmp_object *, char *);
300 int32_t snmp_object_add(struct snmp_toolinfo *, snmp_verify_inoid_f, char *);
301 int32_t snmp_object_remove(struct snmp_toolinfo *, struct asn_oid *oid);
302 int32_t snmp_object_seterror(struct snmp_toolinfo *, struct snmp_value *,
303     int32_t);
304 
305 enum snmp_syntax parse_syntax(char *);
306 char *snmp_parse_suboid(char *, struct asn_oid *);
307 char *snmp_parse_index(struct snmp_toolinfo *, char *, struct snmp_object *);
308 int32_t snmp_parse_numoid(char *, struct asn_oid *);
309 int32_t snmp_suboid_append(struct asn_oid *, asn_subid_t);
310 int32_t snmp_suboid_pop(struct asn_oid *);
311 
312 typedef int32_t (*snmp_verify_vbind_f) (struct snmp_toolinfo *,
313     struct snmp_pdu *, struct snmp_object *);
314 typedef int32_t (*snmp_add_vbind_f) (struct snmp_pdu *, struct snmp_object *);
315 int32_t snmp_pdu_add_bindings(struct snmp_toolinfo *, snmp_verify_vbind_f,
316     snmp_add_vbind_f, struct snmp_pdu *, int32_t);
317 
318 int32_t snmp_parse_get_resp(struct snmp_pdu *, struct snmp_pdu *);
319 int32_t snmp_parse_getbulk_resp(struct snmp_pdu *, struct snmp_pdu *);
320 int32_t snmp_parse_getnext_resp(struct snmp_pdu *, struct snmp_pdu *);
321 int32_t snmp_parse_resp(struct snmp_pdu *, struct snmp_pdu *);
322 int32_t snmp_output_numval(struct snmp_toolinfo *, struct snmp_value *,
323     struct snmp_oid2str *);
324 void snmp_output_val(struct snmp_value *);
325 int32_t snmp_output_resp(struct snmp_toolinfo *, struct snmp_pdu *, struct asn_oid *);
326 void snmp_output_err_resp(struct snmp_toolinfo *, struct snmp_pdu *);
327 void snmp_output_engine(void);
328 void snmp_output_keys(void);
329 
330 #endif /* _BSNMP_TOOLS_H_ */
331