Example #1
0
/*
 * Open and lock the registry file for writing. Returns an
 * open stream and the name of the lock file.
 * Follow up with _finish_write when done.
 */
static FILE *
__isns_local_registry_open_write(char **lock_name)
{
	char	lock_path[PATH_MAX];
	FILE	*fp;
	int	fd, retry;

	snprintf(lock_path, sizeof(lock_path), "%s.lock",
			isns_config.ic_local_registry_file);

	for (retry = 0; retry < 5; ++retry) {
		fd = open(lock_path, O_RDWR|O_CREAT|O_EXCL, 0644);
		if (fd >= 0)
			break;
		if (errno != EEXIST) {
			isns_error("Unable to create %s: %m\n",
					lock_path);
			return NULL;
		}
		isns_error("Cannot lock %s - retry in 1 sec\n",
					isns_config.ic_local_registry_file);
		sleep(1);
	}

	if (!(fp = fdopen(fd, "w"))) {
		isns_error("fdopen failed: %m\n");
		close(fd);
		return NULL;
	}
	isns_assign_string(lock_name, lock_path);
	return fp;
}
Example #2
0
/*
 * read initiator name from supplied filename
 */
int
isns_read_initiatorname(const char *filename)
{
	FILE	*fp;
	char	*name, *pos;

	if ((fp = fopen(filename, "r")) == NULL) {
		perror(filename);
		return -1;
	}

	while ((pos = parser_get_next_line(fp)) != NULL) {
		pos[strcspn(pos, "#")] = '\0';

		if (!(name = parser_get_next_word(&pos)))
			continue;
		if (strcmp(name, "InitiatorName"))
			continue;
		if (pos[0] == '=')
			pos++;
		if (!strncmp(pos, "iqn.", 4))
			isns_assign_string(&isns_config.ic_source_name, pos);
	}

	fclose(fp);
	return 0;
}
Example #3
0
/*
 * Read the registry file, match each entry against the given owner=<foo> tag,
 * and invoke the callback function.
 * This is used for both reading the registry, and rewriting it.
 */
static int
__isns_local_registry_read(const char *match_owner,
			__isns_local_registry_cb_fn_t handle_matching,
			__isns_local_registry_cb_fn_t handle_nonmatching,
			void *user_data)
{
	const char	*filename = isns_config.ic_local_registry_file;
	char		*line, *copy = NULL;
	FILE		*fp;
	int		rv = 0, owner_len;

	if (!(fp = fopen(filename, "r"))) {
		if (errno == ENOENT) {
			isns_debug_state("Unable to open %s: %m\n", filename);
			return 1;
		}
		isns_error("Unable to open %s: %m\n", filename);
		return 0;
	}

	owner_len = match_owner? strlen(match_owner) : 0;
	while ((line = parser_get_next_line(fp)) != NULL) {
		__isns_local_registry_cb_fn_t *cb;
		char	*argv[256], *owner;
		int	argc = 0;

		isns_assign_string(&copy, line);

		argc = isns_attr_list_split(line, argv, 255);
		if (argc <= 0)
			continue;

		/* Last attr should be owner */
		if (strncasecmp(argv[argc-1], "owner=", 6)) {
			isns_error("%s: syntax error (missing owner field)\n",
					filename);
			goto out;
		}
		owner = argv[argc-1] + 6;

		if (!strncasecmp(owner, match_owner, owner_len)
		 && (owner[owner_len] == '\0' || owner[owner_len] == ':'))
			cb = handle_matching;
		else
			cb = handle_nonmatching;

		if (cb && !cb(copy, argc, argv, user_data))
			goto out;

	}
	rv = 1;

out:
	free(copy);
	fclose(fp);
	return rv;
}
Example #4
0
/*
 * Retrieve policy from database
 */
static void
__isns_db_keystore_copy_policy_string(isns_object_t *obj,
		uint32_t tag, char **var)
{
	const char	*value;

	if (!isns_object_get_string(obj, tag, &value))
		return;
	isns_assign_string(var, value);
}
Example #5
0
int
main(int argc, char **argv)
{
	const char	*opt_configfile = ISNS_DEFAULT_ISNSADM_CONFIG;
	isns_client_t	*clnt;
	isns_attr_list_t *attrs;
	isns_simple_t	*reg;
	isns_portal_info_t portal_info;
	uint32_t	status;
	int		opt_replace = 1;
	int		c, n, timeout;

	while ((c = getopt(argc, argv, "c:d:n")) != -1) {
		switch (c) {
		case 'c':
			opt_configfile = optarg;
			break;

		case 'd':
			isns_enable_debugging(optarg);
			break;

		case 'n':
			opt_replace = 0;
			break;

		default:
			isns_fatal("Unknown option\n");
		}
	}

	if (optind != argc - 1)
		isns_fatal("Need timeout argument\n");
	timeout = parse_timeout(argv[optind]);

	isns_read_config(opt_configfile);

	/*
	    ---DevAttrReg[REPLACE]---
	    Source:
	      0020  string      : iSCSI name = "iqn.2005-03.org.open-iscsi:blue"
	    Message attributes:
	      0001  string      : Entity identifier = "blue.pauw.homeunix.net"
	    Operating attributes:
	      0001  string      : Entity identifier = "blue.pauw.homeunix.net"
	      0002  uint32      : Entity protocol = iSCSI (2)
	      0010  ipaddr      : Portal IP address = 192.168.1.2
	      0011  uint32      : Portal TCP/UDP port = 33849/tcp
	      0014  uint32      : ESI port = 56288/tcp
	      0020  string      : iSCSI name = "iqn.2005-03.org.open-iscsi:blue"
	      0021  uint32      : iSCSI node type = Initiator
	      0022  string      : iSCSI alias = "blue.pauw.homeunix.net"

	      [...]
    	      response status 0x0003 (Invalid registration)

	   This would fail because we got confused about EID in
	   the replace case.
	 */
	isns_assign_string(&isns_config.ic_source_name,
			"iqn.2005-03.org.open-iscsi:blue");

	for (n = 0; n < 2; ++n) {
		clnt = isns_create_default_client(NULL);
		reg = isns_simple_create(ISNS_DEVICE_ATTRIBUTE_REGISTER,
				clnt->ic_source, NULL);
		reg->is_replace = opt_replace;

		/* Message attributes */
		attrs = &reg->is_message_attrs;
		STR(ENTITY_IDENTIFIER,	"blue.pauw.homeunix.net");

		/* Operating attributes */
		attrs = &reg->is_operating_attrs;

		STR(ENTITY_IDENTIFIER,	"blue.pauw.homeunix.net");
		U32(ENTITY_PROTOCOL,	2);

		isns_portal_parse(&portal_info, "192.168.1.2:33849/tcp", NULL);
		isns_portal_to_attr_list(&portal_info,
				ISNS_TAG_PORTAL_IP_ADDRESS,
				ISNS_TAG_PORTAL_TCP_UDP_PORT,
				attrs);

		U32(ESI_PORT,		56288);
		STR(ISCSI_NAME,		"iqn.2005-03.org.open-iscsi:blue");
		U32(ISCSI_NODE_TYPE,	ISNS_ISCSI_INITIATOR_MASK);
		STR(ISCSI_ALIAS,	"blue.pauw.homeunix.net");
		isns_simple_print(reg, isns_print_stdout);

		status = isns_client_call(clnt, &reg);

		if (status != ISNS_SUCCESS)
			isns_fatal("Unable to register object: %s\n",
					isns_strerror(status));

		printf("Successfully registered object\n");
		// isns_simple_print(reg, isns_print_stdout);
		isns_simple_free(reg);
		isns_client_destroy(clnt);

		if (n == 0) {
			printf("Sleeping for %d seconds\n", timeout);
			sleep(timeout);
		}
	}

	return 0;
}
Example #6
0
int
main(int argc, char **argv)
{
	const char	*opt_configfile = ISNS_DEFAULT_ISNSADM_CONFIG;
	isns_client_t	*clnt;
	isns_attr_list_t *attrs;
	isns_simple_t	*reg;
	isns_portal_info_t portal_info;
	uint32_t	status;
	int		opt_replace = 1;
	int		c;

	while ((c = getopt(argc, argv, "c:d:n")) != -1) {
		switch (c) {
		case 'c':
			opt_configfile = optarg;
			break;

		case 'd':
			isns_enable_debugging(optarg);
			break;

		case 'n':
			opt_replace = 0;
			break;

		default:
			isns_fatal("Unknown option\n");
		}
	}

	isns_read_config(opt_configfile);

	isns_assign_string(&isns_config.ic_source_name,
			"iqn.1991-05.com.microsoft:orange");

	clnt = isns_create_default_client(NULL);

	/*
	 * test that we can deregister for SCN events, even though
	 * not registered
	 */
	reg = isns_simple_create(ISNS_SCN_DEREGISTER, clnt->ic_source, NULL);

	/* Message attributes */
	attrs = &reg->is_message_attrs;
	STR(ISCSI_NAME,		"iqn.1991-05.com.microsoft:orange");

	status = isns_client_call(clnt, &reg);
	if (status != ISNS_SUCCESS)
		isns_error("SCNDereg failed: %s\n", isns_strerror(status));
	else
		printf("Successfully deregistered for SCN events\n");
	isns_simple_free(reg);

	/*
	 * test that we can deregister a device, even though not
	 * registered -- note that the portal group object proceeds
	 * the initiator (name) object in the operating attribute
	 * list
	 */
	reg = isns_simple_create(ISNS_DEVICE_DEREGISTER, clnt->ic_source, NULL);

	attrs = &reg->is_operating_attrs;
	STR(ENTITY_IDENTIFIER,	"troopa.nki.nl");
	U32(ENTITY_PROTOCOL,	2);

	isns_portal_parse(&portal_info, "192.168.1.40:3229/tcp", NULL);
	isns_portal_to_attr_list(&portal_info,
			ISNS_TAG_PORTAL_IP_ADDRESS,
			ISNS_TAG_PORTAL_TCP_UDP_PORT,
			attrs);

	STR(ISCSI_NAME,		"iqn.1991-05.com.microsoft:orange");

	status = isns_client_call(clnt, &reg);
	if (status != ISNS_SUCCESS)
		isns_fatal("DevDereg failed: %s\n", isns_strerror(status));
	else
		printf("Successfully deregistered a device\n");
	isns_simple_free(reg);

	/*
	 * test that we can register (w/replace) device attributes
	 */
	reg = isns_simple_create(ISNS_DEVICE_ATTRIBUTE_REGISTER,
				 clnt->ic_source, NULL);
	reg->is_replace = opt_replace;

	attrs = &reg->is_operating_attrs;
	STR(ENTITY_IDENTIFIER,	"troopa.nki.nl");
	U32(ENTITY_PROTOCOL,	2);

	isns_portal_parse(&portal_info, "192.168.1.40:3229/tcp", NULL);
	isns_portal_to_attr_list(&portal_info,
			ISNS_TAG_PORTAL_IP_ADDRESS,
			ISNS_TAG_PORTAL_TCP_UDP_PORT,
			attrs);

	U32(SCN_PORT,		3230);
	U32(ESI_PORT,		3230);

	U32(PG_TAG,		1);
	STR(PG_ISCSI_NAME,	"iqn.1991-05.com.microsoft:orange");

	STR(ISCSI_NAME,		"iqn.1991-05.com.microsoft:orange");
	U32(ISCSI_NODE_TYPE,	ISNS_ISCSI_INITIATOR_MASK);
	STR(ISCSI_ALIAS,	"<MS SW iSCSI Initiator>");

	status = isns_client_call(clnt, &reg);
	if (status != ISNS_SUCCESS)
		isns_fatal("DevAttrReg failed: %s\n", isns_strerror(status));
	else
		printf("Successfully registered device attributes\n");
	isns_simple_free(reg);

	/*
	 * test that we can call DEVICE GET NEXT to get the next (only)
	 * iscsi Storage Node
	 */
	reg = isns_simple_create(ISNS_DEVICE_GET_NEXT, clnt->ic_source, NULL);
	attrs = &reg->is_message_attrs;
	NIL(ISCSI_NAME);

	attrs = &reg->is_operating_attrs;
	U32(ISCSI_NODE_TYPE,	ISNS_ISCSI_INITIATOR_MASK);
	NIL(ISCSI_NODE_TYPE);

	status = isns_client_call(clnt, &reg);
	if (status != ISNS_SUCCESS)
		isns_fatal("DevGetNext failed: %s\n", isns_strerror(status));
	else
		printf("Successfully got next device node\n");
	isns_simple_free(reg);

	return 0;
}
Example #7
0
int
main(int argc, char **argv)
{
	const char	*opt_configfile = ISNS_DEFAULT_ISNSADM_CONFIG;
	isns_client_t	*clnt;
	isns_attr_list_t *attrs;
	isns_simple_t	*reg;
	isns_portal_info_t portal_info;
	uint32_t	status;
	int		opt_replace = 1;
	int		c;

	while ((c = getopt(argc, argv, "c:d:n")) != -1) {
		switch (c) {
		case 'c':
			opt_configfile = optarg;
			break;

		case 'd':
			isns_enable_debugging(optarg);
			break;

		case 'n':
			opt_replace = 0;
			break;

		default:
			isns_fatal("Unknown option\n");
		}
	}

	isns_read_config(opt_configfile);

	isns_assign_string(&isns_config.ic_source_name,
			"iqn.1991-05.com.microsoft:orange");

	clnt = isns_create_default_client(NULL);

	reg = isns_simple_create(ISNS_SCN_DEREGISTER, clnt->ic_source, NULL);

	/* Message attributes */
	attrs = &reg->is_message_attrs;
	STR(ISCSI_NAME,		"iqn.1991-05.com.microsoft:orange");

	status = isns_client_call(clnt, &reg);
	if (status != ISNS_SUCCESS)
		isns_error("SCNDereg failed: %s\n", isns_strerror(status));
	isns_simple_free(reg);


	reg = isns_simple_create(ISNS_DEVICE_DEREGISTER, clnt->ic_source, NULL);

	attrs = &reg->is_operating_attrs;
	STR(ENTITY_IDENTIFIER,	"troopa.nki.nl");
	U32(ENTITY_PROTOCOL,	2);

	isns_portal_parse(&portal_info, "192.168.1.40:3229/tcp", NULL);
	isns_portal_to_attr_list(&portal_info,
			ISNS_TAG_PORTAL_IP_ADDRESS,
			ISNS_TAG_PORTAL_TCP_UDP_PORT,
			attrs);

	STR(ISCSI_NAME,		"iqn.1991-05.com.microsoft:orange");

	status = isns_client_call(clnt, &reg);
	if (status != ISNS_SUCCESS)
		isns_fatal("DevDereg failed: %s\n", isns_strerror(status));
	isns_simple_free(reg);

	reg = isns_simple_create(ISNS_DEVICE_ATTRIBUTE_REGISTER, clnt->ic_source, NULL);
	reg->is_replace = opt_replace;

	attrs = &reg->is_operating_attrs;
	STR(ENTITY_IDENTIFIER,	"troopa.nki.nl");
	U32(ENTITY_PROTOCOL,	2);

	isns_portal_parse(&portal_info, "192.168.1.40:3229/tcp", NULL);
	isns_portal_to_attr_list(&portal_info,
			ISNS_TAG_PORTAL_IP_ADDRESS,
			ISNS_TAG_PORTAL_TCP_UDP_PORT,
			attrs);

	U32(SCN_PORT,		3230);
	U32(ESI_PORT,		3230);

	U32(PG_TAG,		1);
	STR(PG_ISCSI_NAME,	"iqn.1991-05.com.microsoft:orange");

	STR(ISCSI_NAME,		"iqn.1991-05.com.microsoft:orange");
	U32(ISCSI_NODE_TYPE,	ISNS_ISCSI_INITIATOR_MASK);
	STR(ISCSI_ALIAS,	"<MS SW iSCSI Initiator>");

	status = isns_client_call(clnt, &reg);
	if (status != ISNS_SUCCESS)
		isns_fatal("DevAttrReg failed: %s\n", isns_strerror(status));
	isns_simple_free(reg);

	reg = isns_simple_create(ISNS_DEVICE_GET_NEXT, clnt->ic_source, NULL);
	attrs = &reg->is_message_attrs;
	NIL(ISCSI_NAME);

	attrs = &reg->is_operating_attrs;
	U32(ISCSI_NODE_TYPE,	ISNS_ISCSI_TARGET_MASK);
	NIL(ISCSI_NODE_TYPE);

	status = isns_client_call(clnt, &reg);
	if (status != ISNS_SUCCESS)
		isns_fatal("DevGetNext failed: %s\n", isns_strerror(status));
	isns_simple_free(reg);

	return 0;
}
Example #8
0
int
isns_config_set(const char *name, char *pos)
{
	char	*value;

	value = parser_get_rest_of_line(&pos);
	if (value)
		while (isspace(*value) || *value == '=')
			++value;
	if (!strcasecmp(name, "HostName")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_host_name, value);
	} else if (!strcasecmp(name, "SourceName")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_source_name, value);
	} else if (!strcasecmp(name, "AuthName")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_auth_name, value);
	} else if (!strcasecmp(name, "IQNPrefix")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_iqn_prefix, value);
	} else if (!strcasecmp(name, "Database")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_database, value);
	} else if (!strcasecmp(name, "ServerAddress")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_server_name, value);
	} else if (!strcasecmp(name, "BindAddress")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_bind_address, value);
	} else if (!strcasecmp(name, "ControlSocket")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_control_socket, value);
	} else if (!strcasecmp(name, "PIDFile")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_pidfile, value);
	} else if (!strcasecmp(name, "LocalRegistry")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_local_registry_file, value);
	} else if (!strcasecmp(name, "RegistrationPeriod")) {
		if (!value)
			goto no_value;
		isns_config.ic_registration_period = parse_timeout(value);
	} else if (!strcasecmp(name, "SCNTimeout")) {
		if (!value)
			goto no_value;
		isns_config.ic_scn_timeout = parse_timeout(value);
	} else if (!strcasecmp(name, "SCNRetries")) {
		if (!value)
			goto no_value;
		isns_config.ic_scn_retries = parse_int(value);
	} else if (!strcasecmp(name, "SCNCallout")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_scn_callout, value);
	} else if (!strcasecmp(name, "ESIMinInterval")) {
		if (!value)
			goto no_value;
		isns_config.ic_esi_min_interval = parse_timeout(value);
	} else if (!strcasecmp(name, "ESIMaxInterval")) {
		if (!value)
			goto no_value;
		isns_config.ic_esi_max_interval = parse_timeout(value);
	} else if (!strcasecmp(name, "ESIRetries")) {
		if (!value)
			goto no_value;
		isns_config.ic_esi_retries = parse_int(value);
	} else if (!strcasecmp(name, "DefaultDiscoveryDomain")) {
		if (!value)
			goto no_value;
		isns_config.ic_use_default_domain = parse_int(value);
	} else if (!strcasecmp(name, "SLPRegister")) {
		if (!value)
			goto no_value;
		isns_config.ic_slp_register = parse_int(value);
	} else if (!strcasecmp(name, "Security")) {
		if (!value)
			goto no_value;
		isns_config.ic_security = parse_int(value);
	} else if (!strcasecmp(name, "AuthKeyFile")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_auth_key_file, value);
	} else if (!strcasecmp(name, "ServerKeyFile")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_server_key_file, value);
	} else if (!strcasecmp(name, "ClientKeyStore")
		|| !strcasecmp(name, "KeyStore")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_client_keystore, value);
	} else if (!strcasecmp(name, "Control.SourceName")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_control_name, value);
	} else if (!strcasecmp(name, "Control.AuthKeyFile")) {
		if (!value)
			goto no_value;
		isns_assign_string(&isns_config.ic_control_key_file, value);
	} else if (!strcasecmp(name, "Auth.ReplayWindow")) {
		if (!value)
			goto no_value;
		isns_config.ic_auth.replay_window = parse_timeout(value);
	} else if (!strcasecmp(name, "Auth.TimestampJitter")) {
		if (!value)
			goto no_value;
		isns_config.ic_auth.timestamp_jitter = parse_timeout(value);
	} else if (!strcasecmp(name, "Network.MaxSockets")) {
		if (!value)
			goto no_value;
		isns_config.ic_network.max_sockets = parse_timeout(value);
	} else if (!strcasecmp(name, "Network.ConnectTimeout")) {
		if (!value)
			goto no_value;
		isns_config.ic_network.connect_timeout = parse_timeout(value);
	} else if (!strcasecmp(name, "Network.ReconnectTimeout")) {
		if (!value)
			goto no_value;
		isns_config.ic_network.reconnect_timeout = parse_timeout(value);
	} else if (!strcasecmp(name, "Network.CallTimeout")) {
		if (!value)
			goto no_value;
		isns_config.ic_network.call_timeout = parse_timeout(value);
	} else {
		fprintf(stderr, "Unknown config item %s=%s\n", name, value);
	}
	return 0;

no_value:
	fprintf(stderr,
		"*** Missing value in configuration assignment for %s ***\n",
		name);
	return -1;
}