Example #1
0
/*
 * Read user input OID - one of following formats:
 * 1) 1.2.1.1.2.1.0 - that is if option numeric was given;
 * 2) string - in such case append .0 to the asn_oid subs;
 * 3) string.1 - no additional processing required in such case.
 */
static char *
snmptools_parse_stroid(struct snmp_toolinfo *snmptoolctx,
    struct snmp_object *obj, char *argv)
{
	char string[MAXSTR], *str;
	int32_t i = 0;
	struct asn_oid in_oid;

	str = argv;

	if (*str == '.')
		str++;

	while (isalpha(*str) || *str == '_' || (i != 0 && isdigit(*str))) {
		str++;
		i++;
	}

	if (i <= 0 || i >= MAXSTR)
		return (NULL);

	memset(&in_oid, 0, sizeof(struct asn_oid));
	if ((str = snmp_parse_suboid((argv + i), &in_oid)) == NULL) {
		warnx("Invalid OID - %s", argv);
		return (NULL);
	}

	strlcpy(string, argv, i + 1);
	if (snmp_lookup_oidall(snmptoolctx, obj, string) < 0) {
		warnx("No entry for %s in mapping lists", string);
		return (NULL);
	}

	/* If OID given on command line append it. */
	if (in_oid.len > 0)
		asn_append_oid(&(obj->val.var), &in_oid);
	else if (*str == '[') {
		if ((str = snmp_parse_index(snmptoolctx, str + 1, obj)) == NULL)
			return (NULL);
	} else if (obj->val.syntax > 0 && GET_PDUTYPE(snmptoolctx) ==
	    SNMP_PDU_GET) {
		if (snmp_suboid_append(&(obj->val.var), (asn_subid_t) 0) < 0)
			return (NULL);
	}

	return (str);
}
Example #2
0
static int
snmp_import_object(struct snmp_toolinfo *tool)
{
	char 	*string;
	int	i;
	enum  tok tok;
	struct snmp_oid2str 	*oid2str;

	if (snmp_import_head(tool) < 0)
		return (-1);

	if ((oid2str = snmp_malloc(sizeof(struct snmp_oid2str))) == NULL) {
		return (-1);
	}

	if ((string = snmp_malloc(strlen(nexttok) + 1)) == NULL) {
		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 -> goes to snmp_enumlist
	 * 2) new line , ( - nonleaf oid -> goes to snmp_nodelist
	 * 2) ':' - this is table entry - a variable length SYNTAX_TYPE (one or more) may follow
	 *                   and second string must end line -> goes to snmp_tablelist
	 * 3) OID , string  ) - this is a trap entry or maybe leaf -> snmp_oidlist
	 * 4) SYNTAX_TYPE, string (not always), get/set modifier - always last  and )- this is definitely a leaf
	 */

	switch (tok = gettoken(tool)) {
		case  TOK_RPAREN:
			if ((i = snmp_enum_insert(tool, oid2str)) < 0)
				goto error;
			if (i == 0) {
				free(oid2str->string);
				free(oid2str);
			}
			return (1);
		case TOK_LPAREN:
			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(tool, oid2str)) <= 0) {
				free(string);
				free(oid2str);
			}
			return (snmp_import_object(tool));
		case TOK_COLON:
			if (snmp_suboid_append(&current_oid, (asn_subid_t) val) < 0)
				goto error;
			if (snmp_import_table(tool, 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:
			if (snmp_import_leaf(tool, &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);
}
Example #3
0
static int32_t
snmp_import_object(struct snmp_toolinfo *snmptoolctx)
{
	char *string;
	int i;
	int32_t tok;
	struct snmp_oid2str *oid2str;

	if (snmp_import_head(snmptoolctx) < 0)
		return (-1);

	if ((oid2str = calloc(1, sizeof(struct snmp_oid2str))) == NULL) {
		syslog(LOG_ERR, "calloc() failed: %s", strerror(errno));
		return (-1);
	}

	if ((string = strdup(nexttok)) == NULL) {
		syslog(LOG_ERR, "strdup() failed: %s", strerror(errno));
		free(oid2str);
		return (-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);
}