Exemplo n.º 1
0
/*
  Set up all details:
  - ip server name
  - dns server name
  - username, passwd
  - ...
  Implementation:
  - load defaults
  - parse cmd line
  - assign settings that may change due to cmd line options
  - check data
  Note:
  - if no argument is specified tries to call the cmd line parser
  with the default cfg file path.
*/
int get_config_data(ddns_t *ctx, int argc, char **argv)
{
	int i;
	int rc = 0;
	int cache_file_len;
	cmd_desc_t *it;

	do {
		rc = ddns_default_config_data(ctx);
		if (rc != 0)
			break;

		it = cmd_options_table;
		while (it->option != NULL) {
			it->handler.context = ctx;
			++it;
		}

		/* in case of no options, assume the default cfg file may be present */
		if (argc == 1) {
			char *custom_argv[] = { "", DYNDNS_INPUT_FILE_OPT_STRING,
				DYNDNS_DEFAULT_CONFIG_FILE
			};
			int custom_argc = sizeof(custom_argv) / sizeof(char *);

			if (ctx->dbg.level)
				logit(LOG_NOTICE, "Using default config file %s", DYNDNS_DEFAULT_CONFIG_FILE);

			if (ctx->cfgfile)
				free(ctx->cfgfile);
			ctx->cfgfile = strdup(DYNDNS_DEFAULT_CONFIG_FILE);
			rc = get_cmd_parse_data(custom_argv, custom_argc, cmd_options_table);
		} else {
			rc = get_cmd_parse_data(argv, argc, cmd_options_table);
		}

		if (rc != 0 || ctx->abort)
			break;

		/* settings that may change due to cmd line options */
		i = 0;
		do {
			/*ip server */
			if (strlen(ctx->info[i].ip_server_name.name) == 0) {
				if (sizeof(ctx->info[i].ip_server_name.name) <
				    strlen(ctx->info[i].system->ip_server_name)) {
					rc = RC_DYNDNS_BUFFER_TOO_SMALL;
					break;
				}
				strcpy(ctx->info[i].ip_server_name.name, ctx->info[i].system->ip_server_name);

				if (sizeof(ctx->info[i].ip_server_url) <
				    strlen(ctx->info[i].system->ip_server_url)) {
					rc = RC_DYNDNS_BUFFER_TOO_SMALL;
					break;
				}
				strcpy(ctx->info[i].ip_server_url, ctx->info[i].system->ip_server_url);
			}

			/*dyndns server */
			if (strlen(ctx->info[i].dyndns_server_name.name) == 0) {
				if (sizeof(ctx->info[i].dyndns_server_name.name)
				    < strlen(ctx->info[i].system->ddns_server_name)) {
					rc = RC_DYNDNS_BUFFER_TOO_SMALL;
					break;
				}
				strcpy(ctx->info[i].dyndns_server_name.name,
				       ctx->info[i].system->ddns_server_name);

				if (sizeof(ctx->info[i].dyndns_server_url) <
				    strlen(ctx->info[i].system->ddns_server_url)) {
					rc = RC_DYNDNS_BUFFER_TOO_SMALL;
					break;
				}
				strcpy(ctx->info[i].dyndns_server_url, ctx->info[i].system->ddns_server_url);
			}
		}
		while (++i < ctx->info_count);

		/* Check if the neccessary params have been provided */
		if (validate_configuration(ctx)) {
			rc = RC_DYNDNS_INVALID_OR_MISSING_PARAMETERS;
			break;
		}

		/* Setup a default cache file, unless the user provided one for us. */
		if (ctx->bind_interface && !ctx->cache_file) {
			cache_file_len = (strlen(DYNDNS_CACHE_FILE) - 2) + strlen(ctx->bind_interface);
			if ((ctx->cache_file = malloc(cache_file_len + 1)) == NULL) {
				rc = RC_OUT_OF_MEMORY;
				break;
			}

			if (snprintf
			    (ctx->cache_file, cache_file_len + 1,
			     DYNDNS_CACHE_FILE, ctx->bind_interface) != cache_file_len) {
				rc = RC_ERROR;
				break;
			}
		} else {
			ctx->cache_file = strdup(DYNDNS_DEFAULT_CACHE_FILE);
		}

	}
	while (0);

	return rc;
}
Exemplo n.º 2
0
/*
  Set up all details:
  - ip server name
  - dns server name
  - username, passwd
  - ...
  Implementation:
  - load defaults
  - parse cmd line
  - assign settings that may change due to cmd line options
  - check data
  Note:
  - if no argument is specified tries to call the cmd line parser
  with the default cfg file path.
*/
RC_TYPE get_config_data(DYN_DNS_CLIENT *p_self, int argc, char** argv)
{
	int i;
	RC_TYPE rc = RC_OK;

	do
	{
		/*load default data */
		rc = get_default_config_data(p_self);
		if (rc != RC_OK)
		{
			break;
		}
		/*set up the context pointers */
		{
			CMD_DESCRIPTION_TYPE *it = cmd_options_table;
			while (it->p_option != NULL)
			{
				it->p_handler.p_context = (void*) p_self;
				++it;
			}
		}

		/* in case of no options, assume the default cfg file may be present */
		if (argc == 1)
		{
			char *custom_argv[] = {"", DYNDNS_INPUT_FILE_OPT_STRING, DYNDNS_DEFAULT_CONFIG_FILE};
			int custom_argc = sizeof(custom_argv) / sizeof(char*);

			if (p_self->dbg.level > 0)
			{
				logit(LOG_NOTICE, MODULE_TAG "Using default config file %s", DYNDNS_DEFAULT_CONFIG_FILE);
			}

			if (p_self->cfgfile)
				free(p_self->cfgfile);
			p_self->cfgfile = strdup(DYNDNS_DEFAULT_CONFIG_FILE);
			rc = get_cmd_parse_data(custom_argv, custom_argc, cmd_options_table);
		}
		else
		{
			rc = get_cmd_parse_data(argv, argc, cmd_options_table);
		}

		if (rc != RC_OK || p_self->abort)
		{
			break;
		}

		/* settings that may change due to cmd line options */
		i = 0;
		do
		{
			/*ip server*/
			if (strlen(p_self->info[i].ip_server_name.name) == 0)
			{
				if (sizeof(p_self->info[i].ip_server_name.name) < strlen(p_self->info[i].p_dns_system->p_ip_server_name))
				{
					rc = RC_DYNDNS_BUFFER_TOO_SMALL;
					break;
				}
				strcpy(p_self->info[i].ip_server_name.name, p_self->info[i].p_dns_system->p_ip_server_name);

				if (sizeof(p_self->info[i].ip_server_url) < strlen(p_self->info[i].p_dns_system->p_ip_server_url))
				{
					rc = RC_DYNDNS_BUFFER_TOO_SMALL;
					break;
				}
				strcpy(p_self->info[i].ip_server_url, p_self->info[i].p_dns_system->p_ip_server_url);
			}

			/*dyndns server*/
			if (strlen(p_self->info[i].dyndns_server_name.name) == 0)
			{
				if (sizeof(p_self->info[i].dyndns_server_name.name) < strlen(p_self->info[i].p_dns_system->p_dyndns_server_name))
				{
					rc = RC_DYNDNS_BUFFER_TOO_SMALL;
					break;
				}
				strcpy(p_self->info[i].dyndns_server_name.name, p_self->info[i].p_dns_system->p_dyndns_server_name);

				if (sizeof(p_self->info[i].dyndns_server_url) < strlen(p_self->info[i].p_dns_system->p_dyndns_server_url))
				{
					rc = RC_DYNDNS_BUFFER_TOO_SMALL;
					break;
				}
				strcpy(p_self->info[i].dyndns_server_url, p_self->info[i].p_dns_system->p_dyndns_server_url);
			}
		}
		while(++i < p_self->info_count);

		/* Check if the neccessary params have been provided */
		if ((p_self->info_count == 0) ||
		    (p_self->info[0].alias_count == 0) ||
		    (strlen(p_self->info[0].dyndns_server_name.name) == 0)  ||
		    (strlen(p_self->info[0].ip_server_name.name) == 0))
		{
			rc = RC_DYNDNS_INVALID_OR_MISSING_PARAMETERS;
			break;
		}
	}
	while (0);

	return rc;
}
Exemplo n.º 3
0
/*
  Set up all details:
  - ip server name
  - dns server name
  - username, passwd
  - ...
  Implementation:
  - load defaults
  - parse cmd line
  - assign settings that may change due to cmd line options
  - check data
  Note:
  - if no argument is specified tries to call the cmd line parser
  with the default cfg file path.
*/
int get_config_data(ddns_t *ctx, int argc, char *argv[])
{
	int i;
	int rc = 0;
	cmd_desc_t *it;

	do {
		TRY(default_config(ctx));

		it = cmd_options_table;
		while (it->option != NULL) {
			it->handler.context = ctx;
			++it;
		}

		/* in case of no options, assume the default cfg file may be present */
		if (argc == 1) {
			char *custom_argv[] = {
				"",
				DYNDNS_INPUT_FILE_OPT_STRING,
				DEFAULT_CONFIG_FILE
			};
			int custom_argc = sizeof(custom_argv) / sizeof(char *);

			if (ctx->dbg.level > 1)
				logit(LOG_NOTICE, "Using default config file %s", DEFAULT_CONFIG_FILE);

			if (ctx->cfgfile)
				free(ctx->cfgfile);
			ctx->cfgfile = strdup(DEFAULT_CONFIG_FILE);
			rc = get_cmd_parse_data(custom_argv, custom_argc, cmd_options_table);
		} else {
			rc = get_cmd_parse_data(argv, argc, cmd_options_table);
		}

		if (rc || ctx->abort)
			break;

		/* settings that may change due to cmd line options */
		i = 0;
		do {
			int port;
			size_t src_len;
			ddns_info_t *info = &ctx->info[i];

			if (strlen(info->checkip_name.name) == 0) {
				if (strlen(info->system->checkip_name) > 0) {
					port = -1;
					info->checkip_name.port = HTTP_DEFAULT_PORT;
					if (get_name_and_port(info->system->checkip_name, info->checkip_name.name, sizeof(info->checkip_name.name), &port) == 0) {
						if (port > 0 && port < 65535)
							info->checkip_name.port = port;
					}
				}
				src_len = strlen(info->system->checkip_url);
				if (src_len > 0 && src_len < sizeof(info->checkip_url))
					strcpy(info->checkip_url, info->system->checkip_url);
			}

			if (strlen(info->server_name.name) == 0) {
				if (strlen(info->system->server_name) > 0) {
					port = -1;
					info->server_name.port = HTTP_DEFAULT_PORT;
					if (get_name_and_port(info->system->server_name, info->server_name.name, sizeof(info->server_name.name), &port) == 0) {
						if (port > 0 && port < 65535)
							info->server_name.port = port;
					}
				}
				src_len = strlen(info->system->server_url);
				if (src_len > 0 && src_len < sizeof(info->server_url))
					strcpy(info->server_url, info->system->server_url);
			}
		}
		while (++i < ctx->info_count);

		/* Check if the neccessary params have been provided */
		TRY(validate_configuration(ctx));
	}
	while (0);

	return rc;
}