Beispiel #1
0
ni_bool_t
ni_iaid_map_to_vars(const ni_iaid_map_t *map, ni_var_array_t *vars)
{
	xml_node_t *root, *node = NULL;
	const char *name;

	if (!vars)
		return FALSE;

	if (!(root = ni_iaid_map_root_node(map)))
		return FALSE;

	ni_var_array_destroy(vars);
	while ((node = ni_iaid_map_next_node(root, node))) {
		if (ni_string_empty(node->cdata))
			continue;

		name = xml_node_get_attr(node, NI_CONFIG_DEFAULT_IAID_DEVICE);
		if (ni_string_empty(name))
			continue;

		ni_var_array_set(vars, name, node->cdata);
	}
	return TRUE;
}
Beispiel #2
0
void
ni_var_array_set_double(ni_var_array_t *nva, const char *name, double value)
{
	char buffer[32];

	snprintf(buffer, sizeof(buffer), "%g", value);
	ni_var_array_set(nva, name, buffer);
}
Beispiel #3
0
void
ni_var_array_set_long(ni_var_array_t *nva, const char *name, unsigned long value)
{
	char buffer[32];

	snprintf(buffer, sizeof(buffer), "%lu", value);
	ni_var_array_set(nva, name, buffer);
}
Beispiel #4
0
void
ni_var_array_set_integer(ni_var_array_t *nva, const char *name, unsigned int value)
{
	char buffer[32];

	snprintf(buffer, sizeof(buffer), "%u", value);
	ni_var_array_set(nva, name, buffer);
}
Beispiel #5
0
static ni_bool_t
ni_config_parse_extension(ni_extension_t *ex, xml_node_t *node)
{
	xml_node_t *child;

	for (child = node->children; child; child = child->next) {
		if (!strcmp(child->name, "action") || !strcmp(child->name, "script")) {
			const char *name, *command;

			if (!(name = xml_node_get_attr(child, "name"))) {
				ni_error("action element without name attribute");
				return FALSE;
			}
			if (!(command = xml_node_get_attr(child, "command"))) {
				ni_error("action element without command attribute");
				return FALSE;
			}

			if (!ni_extension_script_new(ex, name, command))
				return FALSE;
		} else
		if (!strcmp(child->name, "builtin")) {
			const char *name, *library, *symbol;

			if (!(name = xml_node_get_attr(child, "name"))) {
				ni_error("builtin element without name attribute");
				return FALSE;
			}
			if (!(symbol = xml_node_get_attr(child, "symbol"))) {
				ni_error("action element without command attribute");
				return FALSE;
			}
			library = xml_node_get_attr(child, "library");

			ni_c_binding_new(&ex->c_bindings, name, library, symbol);
		} else
		if (!strcmp(child->name, "putenv")) {
			const char *name, *value;

			if (!(name = xml_node_get_attr(child, "name"))) {
				ni_error("%s: <putenv> element without name attribute",
						xml_node_location(child));
				return FALSE;
			}
			value = xml_node_get_attr(child, "value");
			ni_var_array_set(&ex->environment, name, value);
		}
	}

	return TRUE;
}
Beispiel #6
0
void
__ni_ifsysctl_vars_map(ni_var_array_t *vars, const char *key, const char *val)
{
	ni_stringbuf_t buf = NI_STRINGBUF_INIT_DYNAMIC;
	const char *ptr;

	/* Normalize the net.ipv4.ip_forward alias */
	if (!strcmp(key, "net.ipv4.ip_forward"))
		key = "net.ipv4.conf.all.forwarding";

	/*
	 * Filter out net.ipv4.conf.* and net.ipv6.conf.* only.
	 */
	if (strncmp(key, "net.ipv4.conf.", sizeof("net.ipv4.conf.")-1)
	&&  strncmp(key, "net.ipv6.conf.", sizeof("net.ipv6.conf.")-1))
		return;

	/*
	 * Resolve $INTERFACE and $SYSCTL_IF wildcard crap
	 */
	if ((ptr = strstr(key, "$INTERFACE"))) {
		ni_stringbuf_puts(&buf, key);
		ni_stringbuf_truncate(&buf, ptr - key);
		ni_stringbuf_puts(&buf, "default");
		ptr += sizeof("$INTERFACE")-1;
		ni_stringbuf_puts(&buf, ptr);
		key = buf.string;
	} else
	if ((ptr = strstr(key, "$SYSCTL_IF"))) {
		ni_stringbuf_puts(&buf, key);
		ni_stringbuf_truncate(&buf, ptr - key);
		ni_stringbuf_puts(&buf, "default");
		ptr += sizeof("$SYSCTL_IF")-1;
		ni_stringbuf_puts(&buf, ptr);
		key = buf.string;
	}

	/*
	 * And finally add it to the array
	 */
	ni_var_array_set(vars, key, val);
	ni_stringbuf_destroy(&buf);
}
Beispiel #7
0
ni_bool_t
ni_ifsysctl_vars_set(ni_var_array_t *vars, const char *value, const char *keyfmt, ...)
{
	char *key = NULL;
	va_list ap;
	int ret;

	if(!vars || !keyfmt || !value)
		return FALSE;

	va_start(ap, keyfmt);
	ret = vasprintf(&key, keyfmt, ap);
	va_end(ap);
	if (ret < 0)
		return FALSE;

	__ni_sysctl_rewrite_to_dot(key);
	ni_var_array_set(vars, key, value);
	ni_string_free(&key);
	return TRUE;
}
Beispiel #8
0
void
xml_node_add_attr(xml_node_t *node, const char *name, const char *value)
{
	ni_var_array_set(&node->attrs, name, value);
}
Beispiel #9
0
void
ni_var_array_set_boolean(ni_var_array_t *nva, const char *name, int value)
{
	ni_var_array_set(nva, name, value? "yes" : "no");
}
Beispiel #10
0
static int
__ni_config_parse_dhcp6_vendor_opt_node(xml_node_t *node, ni_var_array_t *opts, const char *parent)
{
	const char *attrval;
	const char *code = NULL;
	enum {
		FORMAT_STR,	/* normal string */
		FORMAT_HEX,	/* XX:XX format  */
	};
	int format = FORMAT_STR;
	size_t len;

	if (strcmp(node->name, "option")) {
		ni_error("config: <%s> is not a valid <%s> option node",
			node->name, parent);
		return -1;
	}

	if ((attrval = xml_node_get_attr(node, "code")) != NULL) {
		char *      err;
		long        num;

		num = strtol(attrval, &err, 0);
		if (*err != '\0' || num < 0 || num > 0xffff) {
			ni_error("config: unable to parse %s <option code=\"%s\"",
				parent, attrval);
			return -1;
		}
		code = attrval;
	} else {
		ni_error("config: missed %s <option> without code attribute",
			parent);
		return -1;
	}

	if ((attrval = xml_node_get_attr(node, "format")) != NULL) {
		if (!strcmp(attrval, "hex") || !strcmp(attrval, "mac")) {
			format = FORMAT_HEX;
		} else
		if (!strcmp(attrval, "str") || !strcmp(attrval, "string")) {
			format = FORMAT_STR;
		} else {
			ni_error("config: unknown %s <option format=\"%s\"",
				parent, attrval);
			return -1;
		}
	}

	len = ni_string_len(node->cdata);
	if(format == FORMAT_HEX) {
		unsigned char *buf;

		/* verify the format early ... */
		if (len > 0) {
			len = (len / 3) + 1;
			buf = xcalloc(1, len);
			if (ni_parse_hex(node->cdata, buf, len) <= 0) {
				ni_error("config: unable to parse %s hex option data",
					parent);
				free(buf);
				return -1;
			}
			free(buf);
		}

		ni_var_array_set(opts, code, node->cdata);
	} else {
		ni_stringbuf_t buf = NI_STRINGBUF_INIT_DYNAMIC;

		/* convert to hex-string format */
		if (len > 0) {
			ni_stringbuf_grow(&buf, (len * 3));
			ni_format_hex((unsigned char *)node->cdata, len, buf.string, buf.size);
		}

		ni_var_array_set(opts, code, buf.string);
		ni_stringbuf_destroy(&buf);
	}

	return 0;
}