Read default URLs from installed config files

Instead of hardcoding the default version URL and content URL in the
binary, read the defaults from config files that live in
/usr/share/defaults/swupd/.

If the config files do not exist, then users can pass the -c or -v
options as necessary (or -u, which sets the same value for -c and -v).

The primary motivation for this change is to enable a better experience
for the swupd mixer; users mixing a version of Clear Linux can set the
default URLs at mix time instead of always passing -c/-v/-u at runtime.

Signed-off-by: Patrick McCarty <patrick.mccarty@intel.com>
This commit is contained in:
Patrick McCarty
2016-04-04 11:42:14 -07:00
parent d822817bc7
commit 9085a4f4db
14 changed files with 118 additions and 98 deletions
+4 -4
View File
@@ -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);
+1 -4
View File
@@ -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) {
+2 -8
View File
@@ -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) {
+2 -8
View File
@@ -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) {
+1 -1
View File
@@ -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) {
+1 -1
View File
@@ -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;
+90 -24
View File
@@ -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);
+1 -1
View File
@@ -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) {
+4 -16
View File
@@ -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;
+2 -2
View File
@@ -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) {
+1 -1
View File
@@ -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) {
+4 -11
View File
@@ -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);
+4 -16
View File
@@ -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;
+1 -1
View File
@@ -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);