Add bsnmpd(1)'s SNMP client tools (including SNMPv3 support) to the base system.

Sponsored by:   The FreeBSD Foundation (the SNMPv3 bits), Google Summer of Code 2005
Reviewed by:    philip@ (mostly), bz@ (earlier version based on p4 ch124545)
Approved by:    philip@
This commit is contained in:
Shteryana Shopova 2010-12-08 14:30:25 +00:00
parent 135f7de5dd
commit a739872374
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=216295
13 changed files with 7563 additions and 1 deletions

View File

@ -2,6 +2,7 @@
SUBDIR= gensnmptree \
bsnmpd \
modules
modules \
tools
.include <bsd.subdir.mk>

View File

@ -0,0 +1,7 @@
# $FreeBSD$
# Author: Shteryana Shopova <syrinx@FreeBSD.org>
SUBDIR= libbsnmptools \
bsnmptools
.include <bsd.subdir.mk>

View File

@ -0,0 +1,13 @@
# $FreeBSD$
# Author: Shteryana Shopova <syrinx@FreeBSD.org>
BINDIR?= /usr/bin
CFLAGS+= -I. -I${.CURDIR}
.if exists(${.OBJDIR}/../libbsnmptools)
LIBBSNMPTOOLSDIR= ${.OBJDIR}/../libbsnmptools
.else
LIBBSNMPTOOLSDIR= ${.CURDIR}/../libbsnmptools
.endif
LIBBSNMPTOOLS= ${LIBBSNMPTOOLSDIR}/libbsnmptools.a

View File

@ -0,0 +1,28 @@
# $FreeBSD$
# Author: Shteryana Shopova <syrinx@FreeBSD.org>
.include <bsd.own.mk>
.PATH: ${.CURDIR}
PROG= bsnmpget
DPADD+= ${LIBBSNMP} ${LIBBSNMPTOOLS}
LDADD+= -lbsnmp -lbsnmptools
CFLAGS+= -I${.CURDIR}/../libbsnmptools
LDFLAGS+= -L${LIBBSNMPTOOLSDIR}
.if ${MK_OPENSSL} != "no"
DPADD+= ${LIBCRYPTO}
LDADD+= -lcrypto
.endif
LINKS= ${DESTDIR}/usr/bin/bsnmpget ${DESTDIR}/usr/bin/bsnmpwalk
LINKS+= ${DESTDIR}/usr/bin/bsnmpget ${DESTDIR}/usr/bin/bsnmpset
MAN= bsnmpget.1
MLINKS= bsnmpget.1 bsnmpwalk.1
MLINKS+= bsnmpget.1 bsnmpset.1
.include <bsd.prog.mk>

View File

@ -0,0 +1,401 @@
.\"
.\" Copyright (c) 2010 The FreeBSD Foundation
.\" All rights reserved.
.\"
.\" Portions of this documentation were written by Shteryana Sotirova Shopova
.\" under sponsorship from the FreeBSD Foundation.
.\"
.\" Copyright (c) 2005-2007 The FreeBSD Project.
.\" All rights reserved.
.\"
.\" Author: Shteryana Shopova <syrinx@FreeBSD.org>
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd September 17, 2007
.Dt BSNMPGET 1
.Os
.Sh NAME
.Nm bsnmpget ,
.Nm bsnmpwalk ,
.Nm bsnmpset
.Nd "simple tools for querying SNMP agents"
.Sh SYNOPSIS
.Nm
.Op Fl aDdehnK
.Op Fl A Ar options
.Op Fl b Ar buffersize
.Op Fl C Ar options
.Op Fl I Ar options
.Op Fl i Ar filelist
.Op Fl l Ar filename
.Op Fl M Ar max-repetitions
.Op Fl N Ar non-repeaters
.Op Fl o Ar output
.Op Fl P Ar options
.Op Fl p Ar pdu
.Op Fl r Ar retries
.Op Fl s Ar [trans::][community@][server][:port]
.Op Fl t Ar timeout
.Op Fl U Ar options
.Op Fl v Ar version
.Op Ar OID ...
.Pp
.Nm bsnmpwalk
.Op Fl dhnK
.Op Fl A Ar options
.Op Fl b Ar buffersize
.Op Fl C Ar options
.Op Fl I Ar options
.Op Fl i Ar filelist
.Op Fl l Ar filename
.Op Fl o Ar output
.Op Fl P Ar options
.Op Fl r Ar retries
.Op Fl s Ar [trans::][community@][server][:port]
.Op Fl t Ar timeout
.Op Fl U Ar options
.Op Fl v Ar version
.Op Ar OID ...
.Pp
.Nm bsnmpset
.Op Fl adehnK
.Op Fl A Ar options
.Op Fl b Ar buffersize
.Op Fl C Ar options
.Op Fl I Ar options
.Op Fl i Ar filelist
.Op Fl l Ar filename
.Op Fl o Ar output
.Op Fl P Ar options
.Op Fl r Ar retries
.Op Fl s Ar [trans::][community@][server][:port]
.Op Fl t Ar timeout
.Op Fl U Ar options
.Op Fl v Ar version
.Ar OID Ns = Ar syntax Ns : Ns Ar value
.Op Ar OID Ns = Ar syntax Ns : Ns Ar value ...
.Sh DESCRIPTION
.Nm ,
.Nm bsnmpwalk
and
.Nm bsnmpset
are simple tools for retrieving management information from and setting
management information to a Simple Network Managment Protocol (SNMP) agent.
.Pp
Depending on the options
.Nm bsnmpget
constructs either a SMNP GetRequest, GetNextRequest
or a GetBulkRequest packet, fills in the object identifiers (OIDs) of the
objects whose values will be retrived, waits for a response and prints it if
received successfully.
.Pp
.Nm Bsnmpwalk
queries an agent with SMNP GetNextRequest packets,
asking for values of OID instances that are a part of the object subtree
rooted at the provided OIDs.
.Pp
.Nm Bsnmpset
constructs a SMNP SetRequest packet, fills in the OIDs (object identifiers),
syntaxes and values of the objects whose values are to be set and waits for a
responce from server.
.Sh OPTIONS
.Pp
The options are as follows (not all apply to all three programs):
.Bl -tag -width ".It Fl D Ar options"
.It Fl A Ar options
Authentication options to use with SNMPv3 PDUs
.Bl -tag -width
.It Cm proto=[md5|sha]
The protocol to use when calculating the PDU message digest.
.It Cm key=authkey
A binary localized authentication key to use when calculating the PDU message
digest.
.El
.Pp
By default SNMPv3 PDUs are sent unauthenticated.
.It Fl a
Skip any sanity checks when adding OIDs to a Protocol Data Unit (PDU):
ingore syntax/access type, allow adding of non-leaf objects for GetPdu and
read-only objects to a SetPDU.
.It Fl b Ar buffersize
Tune the size of buffers used to send and receive packets.
The default size is 10000 bytes which should be enough unless an agent sends
a really large octetstring.
The maximum allowed length is 65535 according to the Structure of Management
Information (SMIv2).
.It Fl C Ar options
The context to query with SNMPv3 PDUs.
.Bl -tag -width
.It Cm context=name
The context name. Default is "" (empty).
.It Cm context-engine=engine-id
The SNMP Engine ID of the context to query with SNMPv3 PDUs, represented as
binary octet string. By default, this is set to the Engine ID of the SNMP agent.
.El
.It Fl D
Perform SNMP USM Engine Discovery, rather than sending a request for the value
of a specific object.
.It Fl d
Turn on debugging.
This option will cause the packets sent and received to be dumped to the
terminal.
.It Fl e
Retry on error.
If an error is returned in the response PDU, resend the request removing the
variable that caused the error until a valid response is received.
This is only usefull for a GetRequest- and a GetNextRequest-PDU.
.It Fl h
Print a short help text with default values for various options.
.It Fl I Ar options
Load each MIB description file from the given list to translate symbolic
object names to their numerical representation and vice versa.
Use the other options to obtain a non-default behaviour:
.Bl -tag -width
.It Cm cut=OID
Specifies the initial OID that was cut by
.Xr gensnmpdef 1
when producing the MIB description file.
The default value is .iso(1).org(3).dod(6) which is what should have been
used for all the files installed under /usr/share/snmp/defs/ .
Use this only if you generated your own files, providing a '-c' option to
.Xr gensnmpdef 1 .
.It Cm path=filedir
The directory where files in the list will be searched.
The default is
.Pa /usr/share/snmp/defs/ .
.It Cm file=filelist
A comma separated list of files to which the two options above will apply.
.El
.Pp
The file suboption has to come after the other suboptions so that their
non-default values will be applied to the list of files.
The order of the other suboptions before each file suboption can be random.
Suboptions may be separated either by commas or by spaces.
If using spaces make sure the entire option string is one argument, for
example using quotes.
.It Fl i Ar filelist
List of MIB description files produced by
.Xr gensnmpdef 1 which
.Nm bsnmpget ,
.Nm bsnmpwalk
or
.Nm bsnmpset
will search to translate numerical OIDs to their symbolic object names.
Multiple files can be provided either giving this option multiple times
or a comma separated list of file names.
If a filename begins with a letter the default directory,
/usr/share/snmp/defs/ ,
will be searched.
.It Fl K
Calculate and display the localized authentication and privacy keys
corresponding to a plain text password. The password is obtain via the
environment. Additionally, if one or more OIDs are specified, the calculated
keys are used when processing the SNMPv3 requests.
.It Fl l Ar filename
The path of the posix local (unix domain) socket if local
transport is used.
.It Fl M Ar max-repetitions
The value for the max-repetitions field in a GetBulk PDU.
Default is 1.
.It Fl N Ar non-repeaters
The value for the non-repeaters field in a GetBulk PDU.
Default is 0.
.It Fl n
Only use numerical representations for input and output OIDs and do not
try to resolve symbolic object names.
Note that
.Nm bsnmpget ,
.Nm bsnmpwalk
and
.Nm bsnmpset
will print numerical OIDs anyway if the corresponding string representation
is not found in the MIB description files.
.It Fl o Ar [quiet|short|verbose]
The format used to print the received response.
Quiet only prints values, short (default) prints an abbreviated OID
representation and the value.
In addition to the short output verbose prints the type before the value.
.It Fl P Ar options
Privacy options to use with SNMPv3 PDUs
.Bl -tag -width
.It Cm proto=[aes|des]
The protocol to use when encypting/decrypting SNMPv3 PDU data.
.It Cm key=privkey
A binary localized privacy key to use when encypting/decrypting SNMPv3 PDU data.
.El
.Pp
By default plain text SNMPv3 PDUs are sent.
.It Fl p Ar [get|getnext|getbulk]
The PDU type to send by
.Nm bsmpget .
Default is get.
.It Fl r Ar retries
Number of resends of request packets before giving up if the agent does
not respond after the first try.
Default is 3.
.It Fl s Ar [trans::] Ns Ar [community@] Ns Ar [server] Ns Ar [:port]
Each of the server specification components is optional but at least one
has to be provided if '-s' option is used.
The server specification is constructed in the following manner:
.Bl -tag -width
.It Cm trans::
Transport type may be one of udp, stream or dgram.
If this option is not provided an udp inet/inet6 socket will be used, which
is the most common.
Stream stands for a posix local stream socket and a posix local datagram
socket will be used if dgram is specified.
.It Cm community@
Specify an SNMP community string to be used when sending packets.
If the option is skipped the default "public" will be used for
.Nm
and
.Nm bsnmpwalk
and the default "private" community string will be used for
.Nm bsnmpset .
.It Cm server
This might be either the IP address or the hostname where the agent is
listening.
The default is 'localhost'.
.It Cm port
The destination port to send the requests to.
This is useful if the SNMP agent listens on a non-default port.
Default is given by the 'snmp' entry in /etc/services, port 161.
.El
.It Fl t Ar timeout
Number of seconds before resending a request packet if the agent does
not respond.
The default value is 3 seconds.
.It Fl U Ar options
User credentials when sending SNMPv3 PDUs.
.Bl -tag -width
.It Cm engine=id
The Engine ID of the SNMP agent represented as a binary octet string.
.It Cm engine-boots=value
The value of the snmpEngineBoots of the SNMP agent.
.It Cm engine-time=value
The value of the snmpEngineTime of the SNMP agent.
.Pp
If any of the above is not specified, SNMP USM Engine Discovery is attempted.
This is also the default behavior.
.It Cm name=username
The USM user name to include in the SNMPv3 PDUs. By default, the user name is
obtain via the environment
.El
.It Fl v Ar version
The SNMP protocol version to use when sending requests. SNMP versions 1, 2 and
3 are supported.
If no version option is provided
.Nm bsnmpget ,
.Nm bsnmpwalk
and
.Nm bsnmpset
will use version 2.
Note that GetBulkRequest-PDUs were introduced in SNMPv2 thus setting the
version to 1 is incompatiable with sending a GetBulk PDU.
.It OID
The object identifier whose value to retrive.
At least one OID should be provided for
.Nm bsnmpget
to be able to send a request.
.Pp
For
.Nm bsnmpwalk
this is the root object identifier of the subtree whose values are to be
retrived.
If no OID is provided
.Nm bsnmpwalk
will walk the mib2 subtree rooted
at .iso(1).org(3).dod(6).internet(1).mgmt(2).mib2(1) .
.Pp
Any of the formats used to print a single variable
is valid as input OID:
.Bl -tag -width
.It 1.3.6.1.2.1.25.1.1.0
.It sysDescr
.It ifPhysAddress.1
.It ifRcvAddressStatus.2.6.255.255.255.255.255.255
.It ifRcvAddressType[2,ff:ff:ff:ff:ff:ff]
.It ifRcvAddressStatus[Integer:1,OctetString:ff:ff:ff:ff:ff:ff]
(requires '-o verbose' option)
.El
.Pp
Square brackets are used to denote an entry's indexes.
When used in an input OID, the square brackets may have to be
escaped or the OID has to be quoted to protect it from the shell.
Note there is no difference between ifName.1 and "ifName[1]".
.It OID Ns = Ns Ar [syntax Ns :] Ns Ar value
The object identifier with its syntax type and value that is to be set.
At least one such string OID=[syntax:]value should be provided to
.Nm bsnmpset
to be able to send a request.
.Bl -tag -width
.It Cm OID
OID may be input as a string, a string followed by a random number of integers
(suboids) separated by dots, a sequence of integers separated by dots - that is
if '-n' options is used - and in such case a syntax is required for every value,
or a string followed by square brackets (used to denote an entry's indexes) and
corresponding indexes.
Any of formats used to print a single variable by
.Nm bsnmpset is
valid for inpit OID as well:
.Bl -tag -width
.It 1.3.6.1.2.1.25.1.1.0=TimeTicks:537615486
.It sysLocation=OctetString:"@ Home" (with '-o verbose' option)
.It sysLocation.0="@ Home"
.It 1.3.6.1.2.1.2.2.1.6.1=OctetString:ffffffffffff
.It ifPhysAddress.1="00:02:b3:1d:1c:a3"
.It ifRcvAddressStatus.1.6.255.255.255.255.255.255=1
.It "ifRcvAddressStatus[Integer:1,OctetString:ff:ff:ff:ff:ff:ff]=Integer:1"
(with '-o verbose' option)
.El
.It Cm syntax
where syntax string is one of :
Integer, OctetString, OID, IpAddress, Counter32, Gauge, TimeTicks, Counter64.
.It Cm value
The value to be set - IP address in form of u.u.u.u - for example
1.3.1.6.1.2.0=IpAddress:192.168.0.1, strings require inverted-commas if they
contain any special characters or spaces, all other numeric types don't.
.El
.Sh ENVIRONMENT
.Nm ,
.Nm bsnmpwalk
and
.Nm bsnmpset
use the following environment variables:
.Bl -tag -width SNMPAUTH
.It Ev SNMPAUTH
Specifies a default SNMP USM authentication protocol.
.It Ev SNMPPRIV
Specifies a default SNMP USM privacy protocol.
.It Ev SNMPUSER
Specifies a default SNMP USM user name.
.It Ev SNMPPASSWD
Specifies the SNMP USM plain text password to use when calculating localized
authentication and privacy keys. If this variable exists in the environment,
SMNPv3 is the default version to use for outgoing requests.
.Sh SEE ALSO
.Xr gensnmpdef 1
.Sh AUTHORS
.An Shteryana Shopova Aq syrinx@FreeBSD.org

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,14 @@
#
# $FreeBSD$
#
.PATH: ${.CURDIR}
LIB= bsnmptools
#INTERNALLIB=
SRCS= bsnmpimport.c bsnmpmap.c bsnmptools.c bsnmptc.c
CFLAGS+= -g -Wall -Werror
SHLIB_MAJOR= 0
.include <bsd.lib.mk>

View File

@ -0,0 +1,971 @@
/*-
* Copyright (c) 2006 The FreeBSD Project
* All rights reserved.
*
* Author: Shteryana Shopova <syrinx@FreeBSD.org>
*
* Redistribution of this software and documentation and use in source and
* binary forms, with or without modification, are permitted provided that
* the following conditions are met:
*
* 1. Redistributions of source code or documentation must retain the above
* copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
/*
* Read file containing table description - reuse magic from gensnmptree.c.
* Hopefully one day most of the code here will be part of libbsnmp and
* this duplication won't be necessary.
*
* Syntax is:
* ---------
* file := top | top file
*
* top := tree | typedef | include
*
* tree := head elements ')'
*
* entry := head ':' index STRING elements ')'
*
* leaf := head type STRING ACCESS ')'
*
* column := head type ACCESS ')'
*
* type := BASETYPE | BASETYPE '|' subtype | enum | bits
*
* subtype := STRING
*
* enum := ENUM '(' value ')'
*
* bits := BITS '(' value ')'
*
* value := INT STRING | INT STRING value
*
* head := '(' INT STRING
*
* elements := EMPTY | elements element
*
* element := tree | leaf | column
*
* index := type | index type
*
* typedef := 'typedef' STRING type
*
* include := 'include' filespec
*
* filespec := '"' STRING '"' | '<' STRING '>'
*/
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <bsnmp/asn1.h>
#include <bsnmp/snmp.h>
#include <bsnmp/snmpagent.h> /* SNMP_INDEXES_MAX */
#include "bsnmptc.h"
#include "bsnmptools.h"
enum snmp_tbl_entry {
ENTRY_NONE = 0,
ENTRY_INDEX,
ENTRY_DATA
};
enum {
FL_GET = 0x01,
FL_SET = 0x02,
};
/************************************************************
*
* Allocate memory and panic just in the case...
*/
static void *
xalloc(size_t size)
{
void *ptr;
if ((ptr = malloc(size)) == NULL)
err(1, "allocing %zu bytes", size);
return (ptr);
}
static char *
savestr(const char *s)
{
if (s == NULL)
return (NULL);
return (strcpy(xalloc(strlen(s) + 1), s));
}
/************************************************************
*
* Input stack
*/
struct input {
FILE *fp;
uint32_t lno;
char *fname;
char *path;
LIST_ENTRY(input) link;
};
LIST_HEAD(, input) inputs = LIST_HEAD_INITIALIZER(inputs);
struct input *input = NULL;
int32_t pbchar = -1;
#define MAX_PATHS 100
static const char *paths[MAX_PATHS + 1] = {
"/usr/share/snmp/defs",
"/usr/local/share/snmp/defs",
NULL
};
static void
input_new(FILE *fp, const char *path, const char *fname)
{
struct input *ip;
ip = xalloc(sizeof(*ip));
ip->fp = fp;
ip->lno = 1;
ip->fname = savestr(fname);
ip->path = savestr(path);
LIST_INSERT_HEAD(&inputs, ip, link);
input = ip;
}
static void
input_close(void)
{
if (input == NULL)
return;
fclose(input->fp);
free(input->fname);
free(input->path);
LIST_REMOVE(input, link);
free(input);
input = LIST_FIRST(&inputs);
}
static FILE *
tryopen(const char *path, const char *fname)
{
char *fn;
FILE *fp;
if (path == NULL)
fn = savestr(fname);
else {
fn = xalloc(strlen(path) + strlen(fname) + 2);
sprintf(fn, "%s/%s", path, fname);
}
fp = fopen(fn, "r");
free(fn);
return (fp);
}
static int32_t
input_fopen(const char *fname)
{
FILE *fp;
u_int p;
if (fname[0] == '/' || fname[0] == '.' || fname[0] == '~') {
if ((fp = tryopen(NULL, fname)) != NULL) {
input_new(fp, NULL, fname);
return (0);
}
} else {
for (p = 0; paths[p] != NULL; p++)
if ((fp = tryopen(paths[p], fname)) != NULL) {
input_new(fp, paths[p], fname);
return (0);
}
}
warnx("cannot open '%s'", fname);
return (-1);
}
static int32_t
tgetc(void)
{
int c;
if (pbchar != -1) {
c = pbchar;
pbchar = -1;
return (c);
}
for (;;) {
if (input == NULL)
return (EOF);
if ((c = getc(input->fp)) != EOF)
return (c);
input_close();
}
}
static int32_t
tungetc(int c)
{
if (pbchar != -1)
return (-1);
pbchar = c;
return (1);
}
/************************************************************
*
* Parsing input
*/
enum tok {
TOK_EOF = 0200, /* end-of-file seen */
TOK_NUM, /* number */
TOK_STR, /* string */
TOK_ACCESS, /* access operator */
TOK_TYPE, /* type operator */
TOK_ENUM, /* enum token (kind of a type) */
TOK_TYPEDEF, /* typedef directive */
TOK_DEFTYPE, /* defined type */
TOK_INCLUDE, /* include directive */
TOK_FILENAME, /* filename ("foo.bar" or <foo.bar>) */
TOK_BITS, /* bits token (kind of a type) */
TOK_ERR /* unexpected char - exit */
};
static const struct {
const char *str;
enum tok tok;
uint32_t val;
} keywords[] = {
{ "GET", TOK_ACCESS, FL_GET },
{ "SET", TOK_ACCESS, FL_SET },
{ "NULL", TOK_TYPE, SNMP_SYNTAX_NULL },
{ "INTEGER", TOK_TYPE, SNMP_SYNTAX_INTEGER },
{ "INTEGER32", TOK_TYPE, SNMP_SYNTAX_INTEGER },
{ "UNSIGNED32", TOK_TYPE, SNMP_SYNTAX_GAUGE },
{ "OCTETSTRING", TOK_TYPE, SNMP_SYNTAX_OCTETSTRING },
{ "IPADDRESS", TOK_TYPE, SNMP_SYNTAX_IPADDRESS },
{ "OID", TOK_TYPE, SNMP_SYNTAX_OID },
{ "TIMETICKS", TOK_TYPE, SNMP_SYNTAX_TIMETICKS },
{ "COUNTER", TOK_TYPE, SNMP_SYNTAX_COUNTER },
{ "GAUGE", TOK_TYPE, SNMP_SYNTAX_GAUGE },
{ "COUNTER64", TOK_TYPE, SNMP_SYNTAX_COUNTER64 },
{ "ENUM", TOK_ENUM, SNMP_SYNTAX_INTEGER },
{ "BITS", TOK_BITS, SNMP_SYNTAX_OCTETSTRING },
{ "typedef", TOK_TYPEDEF, 0 },
{ "include", TOK_INCLUDE, 0 },
{ NULL, 0, 0 }
};
struct {
/* Current OID type, regarding table membership. */
enum snmp_tbl_entry tbl_type;
/* A pointer to a structure in table list to add to its members. */
struct snmp_index_entry *table_idx;
} table_data;
struct asn_oid current_oid;
char nexttok[MAXSTR];
u_long val; /* integer values */
int32_t all_cond; /* all conditions are true */
int32_t saved_token = -1;
/* Prepare the global data before parsing a new file. */
static void
snmp_import_init(struct asn_oid *append)
{
memset(&table_data, 0, sizeof(table_data));
memset(&current_oid, 0, sizeof(struct asn_oid));
memset(nexttok, 0, MAXSTR);
if (append != NULL)
asn_append_oid(&current_oid, append);
all_cond = 0;
val = 0;
saved_token = -1;
}
static int32_t
gettoken(struct snmp_toolinfo *snmptoolctx)
{
int c;
struct enum_type *t;
if (saved_token != -1) {
c = saved_token;
saved_token = -1;
return (c);
}
again:
/*
* Skip any whitespace before the next token.
*/
while ((c = tgetc()) != EOF) {
if (c == '\n')
input->lno++;
if (!isspace(c))
break;
}
if (c == EOF)
return (TOK_EOF);
if (!isascii(c)) {
warnx("unexpected character %#2x", (u_int) c);
return (TOK_ERR);
}
/*
* Skip comments.
*/
if (c == '#') {
while ((c = tgetc()) != EOF) {
if (c == '\n') {
input->lno++;
goto again;
}
}
warnx("unexpected EOF in comment");
return (TOK_ERR);
}
/*
* Single character tokens.
*/
if (strchr("():|", c) != NULL)
return (c);
if (c == '"' || c == '<') {
int32_t end = c;
size_t n = 0;
val = 1;
if (c == '<') {
val = 0;
end = '>';
}
while ((c = tgetc()) != EOF) {
if (c == end)
break;
if (n == sizeof(nexttok) - 1) {
nexttok[n++] = '\0';
warnx("filename too long '%s...'", nexttok);
return (TOK_ERR);
}
nexttok[n++] = c;
}
nexttok[n++] = '\0';
return (TOK_FILENAME);
}
/*
* Sort out numbers.
*/
if (isdigit(c)) {
size_t n = 0;
nexttok[n++] = c;
while ((c = tgetc()) != EOF) {
if (!isdigit(c)) {
if (tungetc(c) < 0)
return (TOK_ERR);
break;
}
if (n == sizeof(nexttok) - 1) {
nexttok[n++] = '\0';
warnx("number too long '%s...'", nexttok);
return (TOK_ERR);
}
nexttok[n++] = c;
}
nexttok[n++] = '\0';
sscanf(nexttok, "%lu", &val);
return (TOK_NUM);
}
/*
* So that has to be a string.
*/
if (isalpha(c) || c == '_' || c == '-') {
size_t n = 0;
nexttok[n++] = c;
while ((c = tgetc()) != EOF) {
if (!isalnum(c) && c != '_' && c != '-') {
if (tungetc (c) < 0)
return (TOK_ERR);
break;
}
if (n == sizeof(nexttok) - 1) {
nexttok[n++] = '\0';
warnx("string too long '%s...'", nexttok);
return (TOK_ERR);
}
nexttok[n++] = c;
}
nexttok[n++] = '\0';
/*
* Keywords.
*/
for (c = 0; keywords[c].str != NULL; c++)
if (strcmp(keywords[c].str, nexttok) == 0) {
val = keywords[c].val;
return (keywords[c].tok);
}
if ((t = snmp_enumtc_lookup(snmptoolctx, nexttok)) != NULL) {
val = t->syntax;
return (TOK_DEFTYPE);
}
return (TOK_STR);
}
if (isprint(c))
warnx("%u: unexpected character '%c'", input->lno, c);
else
warnx("%u: unexpected character 0x%02x", input->lno, (u_int) c);
return (TOK_ERR);
}
/*
* Update table information.
*/
static struct snmp_index_entry *
snmp_import_update_table(enum snmp_tbl_entry te, struct snmp_index_entry *tbl)
{
switch (te) {
case ENTRY_NONE:
if (table_data.tbl_type == ENTRY_NONE)
return (NULL);
if (table_data.tbl_type == ENTRY_INDEX)
table_data.table_idx = NULL;
table_data.tbl_type--;
return (NULL);
case ENTRY_INDEX:
if (tbl == NULL)
warnx("No table_index to add!!!");
table_data.table_idx = tbl;
table_data.tbl_type = ENTRY_INDEX;
return (tbl);
case ENTRY_DATA:
if (table_data.tbl_type == ENTRY_INDEX) {
table_data.tbl_type = ENTRY_DATA;
return (table_data.table_idx);
}
return (NULL);
default:
/* NOTREACHED */
warnx("Unknown table entry type!!!");
break;
}
return (NULL);
}
static int32_t
parse_enum(struct snmp_toolinfo *snmptoolctx, enum tok *tok,
struct enum_pairs *enums)
{
while ((*tok = gettoken(snmptoolctx)) == TOK_STR) {
if (enum_pair_insert(enums, val, nexttok) < 0)
return (-1);
if ((*tok = gettoken(snmptoolctx)) != TOK_NUM)
break;
}
if (*tok != ')') {
warnx("')' at end of enums");
return (-1);
}
return (1);
}
static int32_t
parse_subtype(struct snmp_toolinfo *snmptoolctx, enum tok *tok,
enum snmp_tc *tc)
{
if ((*tok = gettoken(snmptoolctx)) != TOK_STR) {
warnx("subtype expected after '|'");
return (-1);
}
*tc = snmp_get_tc(nexttok);
*tok = gettoken(snmptoolctx);
return (1);
}
static int32_t
parse_type(struct snmp_toolinfo *snmptoolctx, enum tok *tok,
enum snmp_tc *tc, struct enum_pairs **snmp_enum)
{
int32_t syntax, mem;
syntax = val;
*tc = 0;
if (*tok == TOK_ENUM || *tok == TOK_BITS) {
if (*snmp_enum == NULL) {
if ((*snmp_enum = enum_pairs_init()) == NULL)
return (-1);
mem = 1;
*tc = SNMP_TC_OWN;
} else
mem = 0;
if (gettoken(snmptoolctx) != '(') {
warnx("'(' expected after ENUM/BITS");
return (-1);
}
if ((*tok = gettoken(snmptoolctx)) != TOK_NUM) {
warnx("need value for ENUM//BITS");
if (mem == 1) {
free(*snmp_enum);
*snmp_enum = NULL;
}
return (-1);
}
if (parse_enum(snmptoolctx, tok, *snmp_enum) < 0) {
enum_pairs_free(*snmp_enum);
*snmp_enum = NULL;
return (-1);
}
*tok = gettoken(snmptoolctx);
} else if (*tok == TOK_DEFTYPE) {
struct enum_type *t;
*tc = 0;
t = snmp_enumtc_lookup(snmptoolctx, nexttok);
if (t != NULL)
*snmp_enum = t->snmp_enum;
*tok = gettoken(snmptoolctx);
} else {
if ((*tok = gettoken(snmptoolctx)) == '|') {
if (parse_subtype(snmptoolctx, tok, tc) < 0)
return (-1);
}
}
return (syntax);
}
static int32_t
snmp_import_head(struct snmp_toolinfo *snmptoolctx)
{
enum tok tok;
if ((tok = gettoken(snmptoolctx)) == '(')
tok = gettoken(snmptoolctx);
if (tok != TOK_NUM || val > ASN_MAXID ) {
warnx("Suboid expected - line %d", input->lno);
return (-1);
}
if (gettoken(snmptoolctx) != TOK_STR) {
warnx("Node name expected at line %d", input->lno);
return (-1);
}
return (1);
}
static int32_t
snmp_import_table(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *obj)
{
int32_t i;
enum snmp_tc tc;
enum tok tok;
struct snmp_index_entry *entry;
if ((entry = malloc(sizeof(struct snmp_index_entry))) == NULL) {
syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));
return (-1);
}
memset(entry, 0, sizeof(struct snmp_index_entry));
STAILQ_INIT(&(entry->index_list));
for (i = 0, tok = gettoken(snmptoolctx); i < SNMP_INDEXES_MAX; i++) {
int32_t syntax;
struct enum_pairs *enums = NULL;
if (tok != TOK_TYPE && tok != TOK_DEFTYPE && tok != TOK_ENUM &&
tok != TOK_BITS)
break;
if ((syntax = parse_type(snmptoolctx, &tok, &tc, &enums)) < 0) {
enum_pairs_free(enums);
snmp_index_listfree(&(entry->index_list));
free(entry);
return (-1);
}
if (snmp_syntax_insert(&(entry->index_list), enums, syntax,
tc) < 0) {
snmp_index_listfree(&(entry->index_list));
enum_pairs_free(enums);
free(entry);
return (-1);
}
}
if (i == 0 || i > SNMP_INDEXES_MAX) {
warnx("Bad number of indexes at line %d", input->lno);
snmp_index_listfree(&(entry->index_list));
free(entry);
return (-1);
}
if (tok != TOK_STR) {
warnx("String expected after indexes at line %d", input->lno);
snmp_index_listfree(&(entry->index_list));
free(entry);
return (-1);
}
entry->string = obj->string;
entry->strlen = obj->strlen;
asn_append_oid(&(entry->var), &(obj->var));
if ((i = snmp_table_insert(snmptoolctx, entry)) < 0) {
snmp_index_listfree(&(entry->index_list));
free(entry);
return (-1);
} else if (i == 0) {
/* Same entry already present in lists. */
free(entry->string);
free(entry);
}
(void) snmp_import_update_table(ENTRY_INDEX, entry);
return (1);
}
/*
* Read everything after the syntax type that is certainly a leaf OID info.
*/
static int32_t
snmp_import_leaf(struct snmp_toolinfo *snmptoolctx, enum tok *tok,
struct snmp_oid2str *oid2str)
{
int32_t i, syntax;
if ((syntax = parse_type(snmptoolctx, tok, &(oid2str->tc), &(oid2str->snmp_enum)))
< 0)
return(-1);
oid2str->syntax = syntax;
/*
* That is the name of the function, corresponding to the entry.
* It is used by bsnmpd, but is not interesting for us.
*/
if (*tok == TOK_STR)
*tok = gettoken(snmptoolctx);
for (i = 0; i < SNMP_ACCESS_GETSET && *tok == TOK_ACCESS; i++) {
oid2str->access |= (uint32_t) val;
*tok = gettoken(snmptoolctx);
}
if (*tok != ')') {
warnx("')' expected at end of line %d", input->lno);
return (-1);
}
oid2str->table_idx = snmp_import_update_table(ENTRY_DATA, NULL);
if ((i = snmp_leaf_insert(snmptoolctx, oid2str)) < 0) {
warnx("Error adding leaf %s to list", oid2str->string);
return (-1);
}
/*
* Same entry is already present in the mapping lists and
* the new one was not inserted.
*/
if (i == 0) {
free(oid2str->string);
free(oid2str);
}
(void) snmp_import_update_table(ENTRY_NONE, NULL);
return (1);
}
static int32_t
snmp_import_object(struct snmp_toolinfo *snmptoolctx)
{
char *string;
int i;
enum tok tok;
struct snmp_oid2str *oid2str;
if (snmp_import_head(snmptoolctx) < 0)
return (-1);
if ((oid2str = malloc(sizeof(struct snmp_oid2str))) == NULL) {
syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));
return (-1);
}
if ((string = malloc(strlen(nexttok) + 1)) == NULL) {
syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));
free(oid2str);
return (-1);
}
memset(oid2str, 0, sizeof(struct snmp_oid2str));
strlcpy(string, nexttok, strlen(nexttok) + 1);
oid2str->string = string;
oid2str->strlen = strlen(nexttok);
asn_append_oid(&(oid2str->var), &(current_oid));
if (snmp_suboid_append(&(oid2str->var), (asn_subid_t) val) < 0)
goto error;
/*
* Prepared the entry - now figure out where to insert it.
* After the object we have following options:
* 1) new line, blank, ) - then it is an enum oid -> snmp_enumlist;
* 2) new line , ( - nonleaf oid -> snmp_nodelist;
* 2) ':' - table entry - a variable length SYNTAX_TYPE (one or more)
* may follow and second string must end line -> snmp_tablelist;
* 3) OID , string ) - this is a trap entry or a leaf -> snmp_oidlist;
* 4) SYNTAX_TYPE, string (not always), get/set modifier - always last
* and )- this is definitely a leaf.
*/
switch (tok = gettoken(snmptoolctx)) {
case ')':
if ((i = snmp_enum_insert(snmptoolctx, oid2str)) < 0)
goto error;
if (i == 0) {
free(oid2str->string);
free(oid2str);
}
return (1);
case '(':
if (snmp_suboid_append(&current_oid, (asn_subid_t) val) < 0)
goto error;
/*
* Ignore the error for nodes since the .def files currently
* contain different strings for 1.3.6.1.2.1 - mibII. Only make
* sure the memory is freed and don't complain.
*/
if ((i = snmp_node_insert(snmptoolctx, oid2str)) <= 0) {
free(string);
free(oid2str);
}
return (snmp_import_object(snmptoolctx));
case ':':
if (snmp_suboid_append(&current_oid, (asn_subid_t) val) < 0)
goto error;
if (snmp_import_table(snmptoolctx, oid2str) < 0)
goto error;
/*
* A different table entry type was malloced and the data is
* contained there.
*/
free(oid2str);
return (1);
case TOK_TYPE:
/* FALLTHROUGH */
case TOK_DEFTYPE:
/* FALLTHROUGH */
case TOK_ENUM:
/* FALLTHROUGH */
case TOK_BITS:
if (snmp_import_leaf(snmptoolctx, &tok, oid2str) < 0)
goto error;
return (1);
default:
warnx("Unexpected token at line %d - %s", input->lno,
input->fname);
break;
}
error:
snmp_mapping_entryfree(oid2str);
return (-1);
}
static int32_t
snmp_import_tree(struct snmp_toolinfo *snmptoolctx, enum tok *tok)
{
while (*tok != TOK_EOF) {
switch (*tok) {
case TOK_ERR:
return (-1);
case '(':
if (snmp_import_object(snmptoolctx) < 0)
return (-1);
break;
case ')':
if (snmp_suboid_pop(&current_oid) < 0)
return (-1);
(void) snmp_import_update_table(ENTRY_NONE, NULL);
break;
default:
/* Anything else here would be illegal. */
return (-1);
}
*tok = gettoken(snmptoolctx);
}
return (0);
}
static int32_t
snmp_import_top(struct snmp_toolinfo *snmptoolctx, enum tok *tok)
{
enum snmp_tc tc;
struct enum_type *t;
if (*tok == '(')
return (snmp_import_tree(snmptoolctx, tok));
if (*tok == TOK_TYPEDEF) {
if ((*tok = gettoken(snmptoolctx)) != TOK_STR) {
warnx("type name expected after typedef - %s",
input->fname);
return (-1);
}
t = snmp_enumtc_init(nexttok);
*tok = gettoken(snmptoolctx);
t->is_enum = (*tok == TOK_ENUM);
t->is_bits = (*tok == TOK_BITS);
t->syntax = parse_type(snmptoolctx, tok, &tc, &(t->snmp_enum));
snmp_enumtc_insert(snmptoolctx, t);
return (1);
}
if (*tok == TOK_INCLUDE) {
int i;
*tok = gettoken(snmptoolctx);
if (*tok != TOK_FILENAME) {
warnx("filename expected in include directive - %s",
nexttok);
return (-1);
}
if (( i = add_filename(snmptoolctx, nexttok, NULL, 1)) == 0) {
*tok = gettoken(snmptoolctx);
return (1);
}
if (i == -1)
return (-1);
input_fopen(nexttok);
*tok = gettoken(snmptoolctx);
return (1);
}
warnx("'(' or 'typedef' expected - %s", nexttok);
return (-1);
}
static int32_t
snmp_import(struct snmp_toolinfo *snmptoolctx)
{
int i;
enum tok tok;
tok = gettoken(snmptoolctx);
do
i = snmp_import_top(snmptoolctx, &tok);
while (i > 0);
return (i);
}
/*
* Read a .def file and import oid<->string mapping.
* Mappings are inserted into a global structure containing list for each OID
* syntax type.
*/
int32_t
snmp_import_file(struct snmp_toolinfo *snmptoolctx, struct fname *file)
{
int idx;
snmp_import_init(&(file->cut));
input_fopen(file->name);
if ((idx = snmp_import(snmptoolctx)) < 0)
warnx("Failed to read mappings from file %s", file->name);
input_close();
return (idx);
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,95 @@
/*-
* Copyright (c) 2006 The FreeBSD Project
* All rights reserved.
*
* Author: Shteryana Shopova <syrinx@FreeBSD.org>
*
* Redistribution of this software and documentation and use in source and
* binary forms, with or without modification, are permitted provided that
* the following conditions are met:
*
* 1. Redistributions of source code or documentation must retain the above
* copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Textual conventions for snmp
*
* $FreeBSD$
*/
#ifndef _BSNMP_TEXT_CONV_H_
#define _BSNMP_TEXT_CONV_H_
/* Variable display length string. */
#define SNMP_VAR_STRSZ -1
/*
* 11 bytes - octets that represent DateAndTime Textual convention
* and the size of string used to diplay that.
*/
#define SNMP_DATETIME_OCTETS 11
#define SNMP_DATETIME_STRSZ 32
/*
* 6 bytes - octets that represent PhysAddress Textual convention
* and the size of string used to diplay that.
*/
#define SNMP_PHYSADDR_OCTETS 6
#define SNMP_PHYSADDR_STRSZ 19
/* NTPTimeStamp. */
#define SNMP_NTP_TS_OCTETS 8
#define SNMP_NTP_TS_STRSZ 10
/* BridgeId. */
#define SNMP_BRIDGEID_OCTETS 8
#define SNMP_BRIDGEID_STRSZ 25
#define SNMP_MAX_BRIDGE_PRIORITY 65535
/* BridgePortId. */
#define SNMP_BPORT_OCTETS 2
#define SNMP_BPORT_STRSZ 7
#define SNMP_MAX_BPORT_PRIORITY 255
/* InetAddress. */
#define SNMP_INADDRS_STRSZ INET6_ADDRSTRLEN
enum snmp_tc {
SNMP_STRING = 0,
SNMP_DISPLAYSTRING = 1,
SNMP_DATEANDTIME = 2,
SNMP_PHYSADDR = 3,
SNMP_ATMESI = 4,
SNMP_NTP_TIMESTAMP = 5,
SNMP_MACADDRESS = 6,
SNMP_BRIDGE_ID = 7,
SNMP_BPORT_ID = 8,
SNMP_INETADDRESS = 9,
SNMP_TC_OWN = 10,
SNMP_UNKNOWN, /* keep last */
};
typedef char * (*snmp_oct2tc_f) (uint32_t len, char *octs, char *buf);
typedef char * (*snmp_tc2oid_f) (char *str, struct asn_oid *oid);
typedef int32_t (*snmp_tc2oct_f) (struct snmp_value *value, char *string);
enum snmp_tc snmp_get_tc(char *str);
char *snmp_oct2tc(enum snmp_tc tc, uint32_t len, char *octets);
char *snmp_tc2oid(enum snmp_tc tc, char *str, struct asn_oid *oid);
int32_t snmp_tc2oct(enum snmp_tc tc, struct snmp_value *value, char *string);
#endif /* _BSNMP_TEXT_CONV_H_ */

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,331 @@
/*-
* Copyright (c) 2005-2006 The FreeBSD Project
* All rights reserved.
*
* Author: Shteryana Shopova <syrinx@FreeBSD.org>
*
* Redistribution of this software and documentation and use in source and
* binary forms, with or without modification, are permitted provided that
* the following conditions are met:
*
* 1. Redistributions of source code or documentation must retain the above
* copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Helper functions common for all tools.
*
* $FreeBSD$
*/
#ifndef _BSNMP_TOOLS_H_
#define _BSNMP_TOOLS_H_
/* From asn1.h + 1 byte for trailing zero. */
#define MAX_OCTSTRING_LEN ASN_MAXOCTETSTRING + 1
#define MAX_CMD_SYNTAX_LEN 12
/* Arbitrary upper limit on node names and function names - gensnmptree.c. */
#define MAXSTR 1000
/* Should be enough to fetch the biggest allowed octet string. */
#define MAX_BUFF_SIZE (ASN_MAXOCTETSTRING + 50)
#define SNMP_DEFS_DIR "/usr/share/snmp/defs/"
#define SNMP_DEFAULT_LOCAL "/var/run/snmpd.sock"
enum snmp_access {
SNMP_ACCESS_NONE = 0,
SNMP_ACCESS_GET,
SNMP_ACCESS_SET,
SNMP_ACCESS_GETSET,
};
/* A structure for integer-string enumerations. */
struct enum_pair {
int32_t enum_val;
char *enum_str;
STAILQ_ENTRY(enum_pair) link;
};
STAILQ_HEAD(enum_pairs, enum_pair);
struct enum_type {
char *name;
uint32_t syntax;
int32_t is_enum;
int32_t is_bits;
struct enum_pairs *snmp_enum;
SLIST_ENTRY(enum_type) link;
};
SLIST_HEAD(snmp_enum_tc, enum_type);
struct index {
enum snmp_tc tc;
enum snmp_syntax syntax;
struct enum_pairs *snmp_enum;
STAILQ_ENTRY(index) link;
};
STAILQ_HEAD(snmp_idxlist, index);
struct snmp_index_entry {
char *string;
uint32_t strlen;
struct asn_oid var;
struct snmp_idxlist index_list;
SLIST_ENTRY(snmp_index_entry) link;
};
/* Information needed for oid to string conversion. */
struct snmp_oid2str {
char *string;
uint32_t strlen;
enum snmp_tc tc;
enum snmp_syntax syntax;
enum snmp_access access;
struct asn_oid var;
/* A pointer to a entry from the table list - OK if NULL. */
struct snmp_index_entry *table_idx;
/*
* A singly-linked tail queue of all (int, string) pairs -
* for INTEGER syntax only.
*/
struct enum_pairs *snmp_enum;
SLIST_ENTRY(snmp_oid2str) link;
};
/* A structure to hold each oid input by user. */
struct snmp_object {
/* Flag - if set, the variable caused error in a previous request. */
int32_t error;
/*
* A pointer in the mapping lists - not used if OIDs are input as
* numericals.
*/
struct snmp_oid2str *info;
/* A snmp value to hold the actual oid, syntax and value. */
struct snmp_value val;
SLIST_ENTRY(snmp_object) link;
};
struct fname {
char *name;
int32_t done;
struct asn_oid cut;
SLIST_ENTRY(fname) link;
};
SLIST_HEAD(snmp_mapping, snmp_oid2str);
SLIST_HEAD(fname_list, fname);
SLIST_HEAD(snmp_table_index, snmp_index_entry);
/*
* Keep a list for every syntax type.
*/
struct snmp_mappings {
/* The list containing all non-leaf nodes. */
struct snmp_mapping nodelist;
/* INTEGER/INTEGER32 types. */
struct snmp_mapping intlist;
/* OCTETSTRING types. */
struct snmp_mapping octlist;
/* OID types. */
struct snmp_mapping oidlist;
/* IPADDRESS types. */
struct snmp_mapping iplist;
/* TIMETICKS types. */
struct snmp_mapping ticklist;
/* COUNTER types. */
struct snmp_mapping cntlist;
/* GAUGE types. */
struct snmp_mapping gaugelist;
/* COUNTER64 types. */
struct snmp_mapping cnt64list;
/* ENUM values for oid types. */
struct snmp_mapping enumlist;
/* Description of all table entry types. */
struct snmp_table_index tablelist;
/* Defined enumerated textual conventions. */
struct snmp_enum_tc tclist;
};
struct snmp_toolinfo {
uint32_t flags;
/* Number of initially input OIDs. */
int32_t objects;
/* List of all input OIDs. */
SLIST_HEAD(snmp_objectlist, snmp_object) snmp_objectlist;
/* All known OID to string mapping data. */
struct snmp_mappings *mappings;
/* A list of .defs filenames to search oid<->string mapping. */
struct fname_list filelist;
/* SNMPv3 USM user credentials */
char *passwd;
};
/* XXX we might want to get away with this and will need to touch
* XXX the MACROS then too */
extern struct snmp_toolinfo snmptool;
/* Definitions for some flags' bits. */
#define OUTPUT_BITS 0x00000003 /* bits 0-1 for output type */
#define NUMERIC_BIT 0x00000004 /* bit 2 for numeric oids */
#define RETRY_BIT 0x00000008 /* bit 3 for retry on error responce */
#define ERRIGNORE_BIT 0x00000010 /* bit 4 for skip sanity checking */
#define ERRIGNORE_BIT 0x00000010 /* bit 4 for skip sanity checking */
#define EDISCOVER_BIT 0x00000020 /* bit 5 for SNMP Engine Discovery */
#define LOCALKEY_BIT 0x00000040 /* bit 6 for using localized key */
/* 0x00000080 */ /* bit 7 reserverd */
#define PDUTYPE_BITS 0x00000f00 /* bits 8-11 for pdu type */
/* 0x0000f000 */ /* bit 12-15 reserverd */
#define MAXREP_BITS 0x00ff0000 /* bits 16-23 for max-repetit. value */
#define NONREP_BITS 0xff000000 /* bits 24-31 for non-repeaters value */
#define OUTPUT_SHORT 0x0
#define OUTPUT_VERBOSE 0x1
#define OUTPUT_TABULAR 0x2
#define OUTPUT_QUIET 0x3
/* Macros for playing with flags' bits. */
#define SET_OUTPUT(ctx, type) ((ctx)->flags |= ((type) & OUTPUT_BITS))
#define GET_OUTPUT(ctx) ((ctx)->flags & OUTPUT_BITS)
#define SET_NUMERIC(ctx) ((ctx)->flags |= NUMERIC_BIT)
#define ISSET_NUMERIC(ctx) ((ctx)->flags & NUMERIC_BIT)
#define SET_RETRY(ctx) ((ctx)->flags |= RETRY_BIT)
#define ISSET_RETRY(ctx) ((ctx)->flags & RETRY_BIT)
#define SET_ERRIGNORE(ctx) ((ctx)->flags |= ERRIGNORE_BIT)
#define ISSET_ERRIGNORE(ctx) ((ctx)->flags & ERRIGNORE_BIT)
#define SET_EDISCOVER(ctx) ((ctx)->flags |= EDISCOVER_BIT)
#define ISSET_EDISCOVER(ctx) ((ctx)->flags & EDISCOVER_BIT)
#define SET_LOCALKEY(ctx) ((ctx)->flags |= LOCALKEY_BIT)
#define ISSET_LOCALKEY(ctx) ((ctx)->flags & LOCALKEY_BIT)
#define SET_PDUTYPE(ctx, type) (((ctx)->flags |= (((type) & 0xf) << 8)))
#define GET_PDUTYPE(ctx) (((ctx)->flags & PDUTYPE_BITS) >> 8)
#define SET_MAXREP(ctx, i) (((ctx)->flags |= (((i) & 0xff) << 16)))
#define GET_MAXREP(ctx) (((ctx)->flags & MAXREP_BITS) >> 16)
#define SET_NONREP(ctx, i) (((ctx)->flags |= (((i) & 0xff) << 24)))
#define GET_NONREP(ctx) (((ctx)->flags & NONREP_BITS) >> 24)
extern const struct asn_oid IsoOrgDod_OID;
int snmptool_init(struct snmp_toolinfo *);
int32_t snmp_import_file(struct snmp_toolinfo *, struct fname *);
int32_t snmp_import_all(struct snmp_toolinfo *);
int32_t add_filename(struct snmp_toolinfo *, const char *,
const struct asn_oid *, int32_t);
void free_filelist(struct snmp_toolinfo *);
void snmp_tool_freeall(struct snmp_toolinfo *);
void snmp_import_dump(int);
/* bsnmpmap.c */
struct snmp_mappings *snmp_mapping_init(void);
int32_t snmp_mapping_free(struct snmp_toolinfo *);
void snmp_index_listfree(struct snmp_idxlist *);
void snmp_dump_oid2str(struct snmp_oid2str *);
int32_t snmp_node_insert(struct snmp_toolinfo *, struct snmp_oid2str *);
int32_t snmp_leaf_insert(struct snmp_toolinfo *, struct snmp_oid2str *);
int32_t snmp_enum_insert(struct snmp_toolinfo *, struct snmp_oid2str *);
struct enum_pairs *enum_pairs_init(void);
void enum_pairs_free(struct enum_pairs *);
void snmp_mapping_entryfree(struct snmp_oid2str *);
int32_t enum_pair_insert(struct enum_pairs *, int32_t, char *);
char *enum_string_lookup(struct enum_pairs *, int32_t);
int32_t enum_number_lookup(struct enum_pairs *, char *);
int32_t snmp_syntax_insert(struct snmp_idxlist *, struct enum_pairs *,
enum snmp_syntax, enum snmp_tc);
int32_t snmp_table_insert(struct snmp_toolinfo *, struct snmp_index_entry *);
struct enum_type *snmp_enumtc_init(char *);
void snmp_enumtc_free(struct enum_type *);
void snmp_enumtc_insert(struct snmp_toolinfo *, struct enum_type *);
struct enum_type *snmp_enumtc_lookup(struct snmp_toolinfo *, char *);
void snmp_mapping_dump(struct snmp_toolinfo *);
int32_t snmp_lookup_leafstring(struct snmp_toolinfo *, struct snmp_object *);
int32_t snmp_lookup_enumstring(struct snmp_toolinfo *, struct snmp_object *);
int32_t snmp_lookup_oidstring(struct snmp_toolinfo *, struct snmp_object *);
int32_t snmp_lookup_nonleaf_string(struct snmp_toolinfo *, struct snmp_object *);
int32_t snmp_lookup_allstring(struct snmp_toolinfo *, struct snmp_object *);
int32_t snmp_lookup_nodestring(struct snmp_toolinfo *, struct snmp_object *);
int32_t snmp_lookup_oidall(struct snmp_toolinfo *, struct snmp_object *, char *);
int32_t snmp_lookup_enumoid(struct snmp_toolinfo *, struct snmp_object *, char *);
int32_t snmp_lookup_oid(struct snmp_toolinfo *, struct snmp_object *, char *);
/* Functions parsing common options for all tools. */
int32_t parse_server(char *);
int32_t parse_timeout(char *);
int32_t parse_retry(char *);
int32_t parse_version(char *);
int32_t parse_local_path(char *);
int32_t parse_buflen(char *);
int32_t parse_debug(void);
int32_t parse_discovery(struct snmp_toolinfo *);
int32_t parse_local_key(struct snmp_toolinfo *);
int32_t parse_num_oids(struct snmp_toolinfo *);
int32_t parse_file(struct snmp_toolinfo *, char *);
int32_t parse_include(struct snmp_toolinfo *, char *);
int32_t parse_output(struct snmp_toolinfo *, char *);
int32_t parse_errors(struct snmp_toolinfo *);
int32_t parse_skip_access(struct snmp_toolinfo *);
int32_t parse_authentication(struct snmp_toolinfo *, char *);
int32_t parse_privacy(struct snmp_toolinfo *, char *);
int32_t parse_context(struct snmp_toolinfo *, char *);
int32_t parse_user_security(struct snmp_toolinfo *, char *);
typedef int32_t (*snmp_verify_inoid_f) (struct snmp_toolinfo *,
struct snmp_object *, char *);
int32_t snmp_object_add(struct snmp_toolinfo *, snmp_verify_inoid_f, char *);
int32_t snmp_object_remove(struct snmp_toolinfo *, struct asn_oid *oid);
int32_t snmp_object_seterror(struct snmp_toolinfo *, struct snmp_value *,
int32_t);
enum snmp_syntax parse_syntax(char *);
char *snmp_parse_suboid(char *, struct asn_oid *);
char *snmp_parse_index(struct snmp_toolinfo *, char *, struct snmp_object *);
int32_t snmp_parse_numoid(char *, struct asn_oid *);
int32_t snmp_suboid_append(struct asn_oid *, asn_subid_t);
int32_t snmp_suboid_pop(struct asn_oid *);
typedef int32_t (*snmp_verify_vbind_f) (struct snmp_toolinfo *,
struct snmp_pdu *, struct snmp_object *);
typedef int32_t (*snmp_add_vbind_f) (struct snmp_pdu *, struct snmp_object *);
int32_t snmp_pdu_add_bindings(struct snmp_toolinfo *, snmp_verify_vbind_f,
snmp_add_vbind_f, struct snmp_pdu *, int32_t);
int32_t snmp_parse_get_resp(struct snmp_pdu *, struct snmp_pdu *);
int32_t snmp_parse_getbulk_resp(struct snmp_pdu *, struct snmp_pdu *);
int32_t snmp_parse_getnext_resp(struct snmp_pdu *, struct snmp_pdu *);
int32_t snmp_parse_resp(struct snmp_pdu *, struct snmp_pdu *);
int32_t snmp_output_numval(struct snmp_toolinfo *, struct snmp_value *,
struct snmp_oid2str *);
void snmp_output_val(struct snmp_value *);
int32_t snmp_output_resp(struct snmp_toolinfo *, struct snmp_pdu *);
void snmp_output_err_resp(struct snmp_toolinfo *, struct snmp_pdu *);
void snmp_output_engine(void);
void snmp_output_keys(void);
#endif /* _BSNMP_TOOLS_H_ */