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