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