diff --git a/include/swupd.h b/include/swupd.h index 5fad5e9f..49a616ec 100644 --- a/include/swupd.h +++ b/include/swupd.h @@ -124,11 +124,11 @@ extern void *tm_dlhandle; extern char *bundle_to_add; extern struct timeval start_time; -extern char *version_server_urls[]; -extern char *preferred_version_url; -extern char *content_server_urls[]; -extern char *preferred_content_url; +extern char *version_url; +extern char *content_url; extern long update_server_port; +extern void set_content_url(char *url); +extern void set_version_url(char *url); extern void check_root(void); extern void clean_curl_multi_queue(void); diff --git a/src/check_update.c b/src/check_update.c index 69cb0379..4ac14d2f 100644 --- a/src/check_update.c +++ b/src/check_update.c @@ -72,10 +72,7 @@ static bool parse_options(int argc, char **argv) goto err; } set_local_download(optarg); - if (version_server_urls[0]) { - free(version_server_urls[0]); - } - string_or_die(&version_server_urls[0], "%s", optarg); + set_version_url(optarg); break; case 'P': if (sscanf(optarg, "%ld", &update_server_port) != 1) { diff --git a/src/clr_bundle_add.c b/src/clr_bundle_add.c index 6eb24000..ed42e030 100644 --- a/src/clr_bundle_add.c +++ b/src/clr_bundle_add.c @@ -82,14 +82,8 @@ static bool parse_options(int argc, char **argv) goto err; } set_local_download(optarg); - if (version_server_urls[0]) { - free(version_server_urls[0]); - } - if (content_server_urls[0]) { - free(content_server_urls[0]); - } - string_or_die(&version_server_urls[0], "%s", optarg); - string_or_die(&content_server_urls[0], "%s", optarg); + set_version_url(optarg); + set_content_url(optarg); break; case 'p': /* default empty path_prefix verifies the running OS */ if (!optarg) { diff --git a/src/clr_bundle_rm.c b/src/clr_bundle_rm.c index 70136ef7..dfa574a8 100644 --- a/src/clr_bundle_rm.c +++ b/src/clr_bundle_rm.c @@ -86,14 +86,8 @@ static bool parse_options(int argc, char **argv) goto err; } set_local_download(optarg); - if (version_server_urls[0]) { - free(version_server_urls[0]); - } - if (content_server_urls[0]) { - free(content_server_urls[0]); - } - string_or_die(&version_server_urls[0], "%s", optarg); - string_or_die(&content_server_urls[0], "%s", optarg); + set_version_url(optarg); + set_content_url(optarg); break; case 'P': if (sscanf(optarg, "%ld", &update_server_port) != 1) { diff --git a/src/curl.c b/src/curl.c index 75f66c8e..e82f636d 100644 --- a/src/curl.c +++ b/src/curl.c @@ -426,7 +426,7 @@ CURLcode swupd_curl_set_basic_options(CURL *curl, const char *url) } } - if (strncmp(url, content_server_urls[1], strlen(content_server_urls[1])) == 0) { + if (strncmp(url, content_url, strlen(content_url)) == 0) { #warning "SECURITY HOLE since we can't SSL pin arbitrary servers" curl_ret = swupd_curl_set_security_opts(curl); if (curl_ret != CURLE_OK) { diff --git a/src/download.c b/src/download.c index ed92e487..32187bc9 100644 --- a/src/download.c +++ b/src/download.c @@ -479,7 +479,7 @@ void full_download(struct file *file) goto out_bad; } - string_or_die(&url, "%s/%i/files/%s.tar", preferred_content_url, file->last_change, file->hash); + string_or_die(&url, "%s/%i/files/%s.tar", content_url, file->last_change, file->hash); string_or_die(&filename, "%s/download/.%s.tar", STATE_DIR, file->hash); file->staging = filename; diff --git a/src/globals.c b/src/globals.c index 171a1096..213176aa 100644 --- a/src/globals.c +++ b/src/globals.c @@ -60,20 +60,95 @@ bool download_only; bool local_download = false; bool have_manifest_diskspace = false; /* assume no until checked */ bool have_network = false; /* assume no access until proved */ -#define URL_COUNT 2 -char *version_server_urls[URL_COUNT] = { - NULL, - "https://download.clearlinux.org/update", -}; -char *content_server_urls[URL_COUNT] = { - NULL, - "https://download.clearlinux.org/update", -}; -char *preferred_version_url; -char *preferred_content_url; +char *version_url = NULL; +char *content_url = NULL; long update_server_port = -1; #define SWUPD_DEFAULT_FORMAT "3" +static const char *default_version_url_path = "/usr/share/defaults/swupd/versionurl"; +static const char *default_content_url_path = "/usr/share/defaults/swupd/contenturl"; + +static int set_default_value(char **global, const char *path) +{ + char line[LINE_MAX]; + FILE *file; + char *c; + + file = fopen(path, "r"); + if (!file) { + printf("Error: Unable to open %s\n", path); + return -1; + } + + /* the file should contain exactly one line */ + line[0] = 0; + if (fgets(line, LINE_MAX, file) == NULL) { + if (ferror(file)) { + printf("Error: Unable to read data from %s\n", path); + return -1; + } + if (feof(file)) { + printf("Error: Contents of %s are empty\n", path); + return -1; + } + } + + /* remove newline if present */ + c = strchr(line, '\n'); + if (c) { + *c = '\0'; + } + + string_or_die(global, "%s", line); + + return 0; +} + +static int set_url(char **global, char *url, const char *path) +{ + int ret = 0; + + if (url) { + if (*global) { + free(*global); + } + string_or_die(global, "%s", url); + } else { + if (*global) { + /* option passed on command line previously */ + return ret; + } else { + /* no option passed; use the default value */ + ret = set_default_value(global, path); + if (ret < 0) { + return ret; + } + } + } + + return ret; +} + +void set_content_url(char *url) { + int ret; + + ret = set_url(&content_url, url, default_content_url_path); + if (ret < 0) { + printf("\nDefault content URL cannot be read. Use the -c option instead.\n"); + exit(EXIT_FAILURE); + } +} + +void set_version_url(char *url) { + int ret; + + ret = set_url(&version_url, url, default_version_url_path); + if (ret < 0) { + printf("\nDefault version URL cannot be read. Use the -v option instead.\n"); + exit(EXIT_FAILURE); + } +} + bool set_format_string(char *userinput) { int version; @@ -116,17 +191,8 @@ bool init_globals(void) gettimeofday(&start_time, NULL); - /* pick urls simply from user specified or default */ - if (version_server_urls[0] != NULL) { - preferred_version_url = version_server_urls[0]; - } else { - preferred_version_url = version_server_urls[1]; - } - if (content_server_urls[0] != NULL) { - preferred_content_url = content_server_urls[0]; - } else { - preferred_content_url = content_server_urls[1]; - } + set_version_url(NULL); + set_content_url(NULL); /* insure path_prefix is absolute, at least '/', ends in '/', * and is a valid dir */ @@ -170,8 +236,8 @@ bool init_globals(void) void free_globals(void) { - free(content_server_urls[0]); - free(version_server_urls[0]); + free(content_url); + free(version_url); free(path_prefix); free(format_string); free(mounted_dirs); diff --git a/src/helpers.c b/src/helpers.c index 616a3481..2d597692 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -749,7 +749,7 @@ int verify_fix_path(char *targetpath, struct manifest *target_MoM) // clean up in case any prior download failed in a partial state unlink(tar_dotfile); - string_or_die(&url, "%s/%i/files/%s.tar", preferred_content_url, file->last_change, file->hash); + string_or_die(&url, "%s/%i/files/%s.tar", content_url, file->last_change, file->hash); ret = swupd_curl_get_file(url, tar_dotfile, NULL, NULL, false); if (ret != 0) { diff --git a/src/main.c b/src/main.c index e5037e9e..fa5f8c46 100644 --- a/src/main.c +++ b/src/main.c @@ -92,14 +92,8 @@ static bool parse_options(int argc, char **argv) goto err; } set_local_download(optarg); - if (version_server_urls[0]) { - free(version_server_urls[0]); - } - if (content_server_urls[0]) { - free(content_server_urls[0]); - } - string_or_die(&version_server_urls[0], "%s", optarg); - string_or_die(&content_server_urls[0], "%s", optarg); + set_version_url(optarg); + set_content_url(optarg); break; case 'P': if (sscanf(optarg, "%ld", &update_server_port) != 1) { @@ -112,20 +106,14 @@ static bool parse_options(int argc, char **argv) printf("Invalid --contenturl argument\n\n"); goto err; } - if (content_server_urls[0]) { - free(content_server_urls[0]); - } - string_or_die(&content_server_urls[0], "%s", optarg); + set_content_url(optarg); break; case 'v': if (!optarg) { printf("Invalid --versionurl argument\n\n"); goto err; } - if (version_server_urls[0]) { - free(version_server_urls[0]); - } - string_or_die(&version_server_urls[0], "%s", optarg); + set_version_url(optarg); break; case 's': cmd_line_status = true; diff --git a/src/manifest.c b/src/manifest.c index f903425b..9aebc6b7 100644 --- a/src/manifest.c +++ b/src/manifest.c @@ -437,7 +437,7 @@ static int try_delta_manifest_download(int current, int new, char *component, st memset(&buf, 0, sizeof(struct stat)); ret = stat(deltafile, &buf); if (ret || buf.st_size == 0) { - string_or_die(&url, "%s/%i/Manifest-%s-delta-from-%i", preferred_content_url, new, component, current); + string_or_die(&url, "%s/%i/Manifest-%s-delta-from-%i", content_url, new, component, current); ret = swupd_curl_get_file(url, deltafile, NULL, NULL, false); if (ret != 0) { @@ -509,7 +509,7 @@ static int retrieve_manifests(int current, int version, char *component, struct string_or_die(&filename, "%s/%i/Manifest.%s.tar", STATE_DIR, version, component); - string_or_die(&url, "%s/%i/Manifest.%s.tar", preferred_content_url, version, component); + string_or_die(&url, "%s/%i/Manifest.%s.tar", content_url, version, component); ret = swupd_curl_get_file(url, filename, NULL, NULL, false); if (ret) { diff --git a/src/packs.c b/src/packs.c index 4ce40753..e1817c10 100644 --- a/src/packs.c +++ b/src/packs.c @@ -54,7 +54,7 @@ static int download_pack(int oldversion, int newversion, char *module) printf("Downloading %s pack for version %i\n", module, newversion); - string_or_die(&url, "%s/%i/pack-%s-from-%i.tar", preferred_content_url, newversion, module, oldversion); + string_or_die(&url, "%s/%i/pack-%s-from-%i.tar", content_url, newversion, module, oldversion); err = swupd_curl_get_file(url, filename, NULL, NULL, true); if (err) { diff --git a/src/search.c b/src/search.c index c7abfbec..2e007605 100644 --- a/src/search.c +++ b/src/search.c @@ -106,15 +106,8 @@ static bool parse_options(int argc, char **argv) goto err; } set_local_download(optarg); - if (version_server_urls[0]) { - free(version_server_urls[0]); - } - if (content_server_urls[0]) { - free(content_server_urls[0]); - } - string_or_die(&version_server_urls[0], "%s", optarg); - string_or_die(&content_server_urls[0], "%s", optarg); - + set_version_url(optarg); + set_content_url(optarg); break; case 'P': if (sscanf(optarg, "%ld", &update_server_port) != 1) { @@ -356,7 +349,7 @@ static double query_total_download_size(struct list *list) if (access(untard_file, F_OK) == -1) { /* Does not exist client-side. Must download */ - string_or_die(&url, "%s/%i/Manifest.%s.tar", preferred_content_url, + string_or_die(&url, "%s/%i/Manifest.%s.tar", content_url, file->last_change, file->filename); ret = swupd_query_url_content_size(url); @@ -436,7 +429,7 @@ int download_manifests(struct manifest **MoM) } if (access(untard_file, F_OK) == -1) { - string_or_die(&url, "%s/%i/Manifest.%s.tar", preferred_content_url, current_version, + string_or_die(&url, "%s/%i/Manifest.%s.tar", content_url, current_version, file->filename); printf("Error: Failure reading from %s\n", url); diff --git a/src/verify.c b/src/verify.c index f5398066..aab18da9 100644 --- a/src/verify.c +++ b/src/verify.c @@ -126,14 +126,8 @@ static bool parse_options(int argc, char **argv) goto err; } set_local_download(optarg); - if (version_server_urls[0]) { - free(version_server_urls[0]); - } - if (content_server_urls[0]) { - free(content_server_urls[0]); - } - string_or_die(&version_server_urls[0], "%s", optarg); - string_or_die(&content_server_urls[0], "%s", optarg); + set_version_url(optarg); + set_content_url(optarg); break; case 'P': if (sscanf(optarg, "%ld", &update_server_port) != 1) { @@ -146,20 +140,14 @@ static bool parse_options(int argc, char **argv) printf("Invalid --contenturl argument\n\n"); goto err; } - if (content_server_urls[0]) { - free(content_server_urls[0]); - } - string_or_die(&content_server_urls[0], "%s", optarg); + set_content_url(optarg); break; case 'v': if (!optarg) { printf("Invalid --versionurl argument\n\n"); goto err; } - if (version_server_urls[0]) { - free(version_server_urls[0]); - } - string_or_die(&version_server_urls[0], "%s", optarg); + set_version_url(optarg); break; case 'f': cmdline_option_fix = true; diff --git a/src/version.c b/src/version.c index e94cc455..4d39f6cf 100644 --- a/src/version.c +++ b/src/version.c @@ -46,7 +46,7 @@ static int try_version_download(void) abort(); } - string_or_die(&url, "%s/version/format%s/latest", preferred_version_url, format_string); + string_or_die(&url, "%s/version/format%s/latest", version_url, format_string); string_or_die(&path, "%s/server_version", STATE_DIR);