| 1 |
/* Metalink module. |
| 2 |
Copyright (C) 2015, 2018-2024 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Wget. |
| 5 |
|
| 6 |
GNU Wget is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Wget is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with Wget. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
Additional permission under GNU GPL version 3 section 7 |
| 20 |
|
| 21 |
If you modify this program, or any covered work, by linking or |
| 22 |
combining it with the OpenSSL project's OpenSSL library (or a |
| 23 |
modified version of that library), containing parts covered by the |
| 24 |
terms of the OpenSSL or SSLeay licenses, the Free Software Foundation |
| 25 |
grants you additional permission to convey the resulting work. |
| 26 |
Corresponding Source for a non-source form of such a combination |
| 27 |
shall include the source code for the parts of OpenSSL used as well |
| 28 |
as that of the covered work. */ |
| 29 |
|
| 30 |
#include "wget.h" |
| 31 |
#ifdef HAVE_METALINK |
| 32 |
|
| 33 |
#include "metalink.h" |
| 34 |
#include "retr.h" |
| 35 |
#include "exits.h" |
| 36 |
#include "utils.h" |
| 37 |
#include "md2.h" |
| 38 |
#include "md4.h" |
| 39 |
#include "md5.h" |
| 40 |
#include "sha1.h" |
| 41 |
#include "sha256.h" |
| 42 |
#include "sha512.h" |
| 43 |
#include "filename.h" |
| 44 |
#include "xmemdup0.h" |
| 45 |
#include "xstrndup.h" |
| 46 |
#include "c-strcase.h" |
| 47 |
#include <errno.h> |
| 48 |
#include <unistd.h> /* For unlink. */ |
| 49 |
#include <metalink/metalink_parser.h> |
| 50 |
#ifdef HAVE_GPGME |
| 51 |
#include <gpgme.h> |
| 52 |
#include <fcntl.h> /* For open and close. */ |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifdef TESTING |
| 56 |
#include "../tests/unit-tests.h" |
| 57 |
#endif |
| 58 |
|
| 59 |
/* Loop through all files in metalink structure and retrieve them. |
| 60 |
Returns RETROK if all files were downloaded. |
| 61 |
Returns last retrieval error (from retrieve_url) if some files |
| 62 |
could not be downloaded. */ |
| 63 |
uerr_t |
| 64 |
retrieve_from_metalink (const metalink_t* metalink) |
| 65 |
{ |
| 66 |
metalink_file_t **mfile_ptr; |
| 67 |
uerr_t last_retr_err = RETROK; /* Store last encountered retrieve error. */ |
| 68 |
|
| 69 |
FILE *_output_stream = output_stream; |
| 70 |
bool _output_stream_regular = output_stream_regular; |
| 71 |
char *_output_document = opt.output_document; |
| 72 |
|
| 73 |
/* metalink file counter */ |
| 74 |
unsigned mfc = 0; |
| 75 |
|
| 76 |
/* metalink retrieval type */ |
| 77 |
const char *metatpy = metalink->origin ? "Metalink/HTTP" : "Metalink/XML"; |
| 78 |
|
| 79 |
/* metalink mother source */ |
| 80 |
char *metasrc = metalink->origin ? metalink->origin : opt.input_metalink; |
| 81 |
|
| 82 |
DEBUGP (("Retrieving from Metalink %s\n", quote (metasrc))); |
| 83 |
|
| 84 |
/* No files to download. */ |
| 85 |
if (!metalink->files) |
| 86 |
return RETROK; |
| 87 |
|
| 88 |
if (opt.output_document) |
| 89 |
{ |
| 90 |
/* We cannot support output_document as we need to compute checksum |
| 91 |
of downloaded file, and to remove it if the checksum is bad. */ |
| 92 |
logputs (LOG_NOTQUIET, |
| 93 |
_("-O not supported for metalink download. Ignoring.\n")); |
| 94 |
} |
| 95 |
|
| 96 |
for (mfile_ptr = metalink->files; *mfile_ptr; mfile_ptr++) |
| 97 |
{ |
| 98 |
metalink_file_t *mfile = *mfile_ptr; |
| 99 |
metalink_resource_t **mres_ptr; |
| 100 |
char *planname = NULL; |
| 101 |
char *trsrname = NULL; |
| 102 |
char *filename; |
| 103 |
char *basename; |
| 104 |
char *safename = NULL; |
| 105 |
char *destname = NULL; |
| 106 |
bool size_ok = false; |
| 107 |
bool hash_ok = false; |
| 108 |
|
| 109 |
uerr_t retr_err = METALINK_MISSING_RESOURCE; |
| 110 |
|
| 111 |
/* -1 -> file should be rejected |
| 112 |
0 -> could not verify |
| 113 |
1 -> verified successfully */ |
| 114 |
char sig_status = 0; |
| 115 |
|
| 116 |
bool skip_mfile = false; |
| 117 |
|
| 118 |
output_stream = NULL; |
| 119 |
|
| 120 |
mfc++; |
| 121 |
|
| 122 |
/* The directory prefix for opt.metalink_over_http is handled by |
| 123 |
src/url.c (url_file_name), do not add it a second time. */ |
| 124 |
if (!metalink->origin && opt.dir_prefix && strlen (opt.dir_prefix)) |
| 125 |
planname = aprintf ("%s/%s", opt.dir_prefix, mfile->name); |
| 126 |
else |
| 127 |
planname = xstrdup (mfile->name); |
| 128 |
|
| 129 |
/* With Metalink/HTTP, trust the metalink file name (from cli). |
| 130 |
With --trust-server-names, trust the Metalink/XML file name, |
| 131 |
otherwise, use the basename of --input-metalink followed by |
| 132 |
the metalink file counter as suffix. */ |
| 133 |
if (metalink->origin || opt.trustservernames) |
| 134 |
{ |
| 135 |
trsrname = xstrdup (mfile->name); |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
trsrname = xstrdup (get_metalink_basename (opt.input_metalink)); |
| 140 |
append_suffix_number (&trsrname, ".#", mfc); |
| 141 |
} |
| 142 |
|
| 143 |
/* Add the directory prefix for opt.input_metalink. */ |
| 144 |
if (!metalink->origin && opt.dir_prefix && strlen (opt.dir_prefix)) |
| 145 |
filename = aprintf ("%s/%s", opt.dir_prefix, trsrname); |
| 146 |
else |
| 147 |
filename = xstrdup (trsrname); |
| 148 |
|
| 149 |
/* Enforce libmetalink's metalink_check_safe_path(). */ |
| 150 |
basename = get_metalink_basename (filename); |
| 151 |
safename = metalink_check_safe_path (filename) ? filename : basename; |
| 152 |
|
| 153 |
DEBUGP (("Processing metalink file %s...\n", quote (mfile->name))); |
| 154 |
DEBUGP (("\n")); |
| 155 |
DEBUGP ((" %s\n", metatpy)); |
| 156 |
DEBUGP (("\n")); |
| 157 |
DEBUGP ((" --trust-server-names %s\n", opt.trustservernames ? "true" : "false")); |
| 158 |
DEBUGP ((" --directory-prefix %s\n", quote (opt.dir_prefix ? opt.dir_prefix : ""))); |
| 159 |
DEBUGP (("\n")); |
| 160 |
DEBUGP ((" Counted metalink file %u\n", mfc)); |
| 161 |
DEBUGP ((" Planned metalink file %s\n", quote (planname ? planname : ""))); |
| 162 |
DEBUGP ((" Trusted metalink file %s\n", quote (trsrname ? trsrname : ""))); |
| 163 |
DEBUGP ((" Current metalink file %s\n", quote (filename ? filename : ""))); |
| 164 |
DEBUGP ((" Cleaned metalink file %s\n", quote (basename ? basename : ""))); |
| 165 |
DEBUGP ((" Secured metalink file %s\n", quote (safename ? safename : ""))); |
| 166 |
DEBUGP (("\n")); |
| 167 |
|
| 168 |
/* Verify if the planned metalink file name is safe. */ |
| 169 |
if (!safename || strcmp (planname, safename)) |
| 170 |
{ |
| 171 |
logprintf (LOG_NOTQUIET, |
| 172 |
_("[--trust-server-names %s, --directory-prefix=%s]\n"), |
| 173 |
(opt.trustservernames ? "true" : "false"), |
| 174 |
quote (opt.dir_prefix ? opt.dir_prefix : "")); |
| 175 |
logprintf (LOG_NOTQUIET, |
| 176 |
_("Planned metalink file: %s\n"), |
| 177 |
quote (planname ? planname : "")); |
| 178 |
logprintf (LOG_NOTQUIET, |
| 179 |
_("Secured metalink file: %s\n"), |
| 180 |
quote (safename ? safename : "")); |
| 181 |
if (!safename) |
| 182 |
{ |
| 183 |
logprintf (LOG_NOTQUIET, |
| 184 |
_("Rejecting metalink file. Unsafe name.\n")); |
| 185 |
xfree (planname); |
| 186 |
xfree (trsrname); |
| 187 |
xfree (filename); |
| 188 |
continue; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/* Process the chosen application/metalink4+xml metaurl. */ |
| 193 |
if (opt.metalink_index >= 0) |
| 194 |
{ |
| 195 |
int _metalink_index = opt.metalink_index; |
| 196 |
|
| 197 |
metalink_metaurl_t **murl_ptr; |
| 198 |
int abs_count = 0, meta_count = 0; |
| 199 |
|
| 200 |
uerr_t x_retr_err = METALINK_MISSING_RESOURCE; |
| 201 |
|
| 202 |
opt.metalink_index = -1; |
| 203 |
|
| 204 |
DEBUGP (("Searching application/metalink4+xml ordinal number %d...\n", _metalink_index)); |
| 205 |
|
| 206 |
if (mfile->metaurls && mfile->metaurls[0]) |
| 207 |
for (murl_ptr = mfile->metaurls; *murl_ptr; murl_ptr++) |
| 208 |
{ |
| 209 |
metalink_t* metaurl_xml; |
| 210 |
metalink_error_t meta_err; |
| 211 |
metalink_metaurl_t *murl = *murl_ptr; |
| 212 |
|
| 213 |
char *_dir_prefix = opt.dir_prefix; |
| 214 |
char *_input_metalink = opt.input_metalink; |
| 215 |
|
| 216 |
char *metafile = NULL; |
| 217 |
char *metadest = NULL; |
| 218 |
char *metadir = NULL; |
| 219 |
|
| 220 |
abs_count++; |
| 221 |
|
| 222 |
if (strcmp (murl->mediatype, "application/metalink4+xml")) |
| 223 |
continue; |
| 224 |
|
| 225 |
meta_count++; |
| 226 |
|
| 227 |
DEBUGP ((" Ordinal number %d: %s\n", meta_count, quote (murl->url))); |
| 228 |
|
| 229 |
if (_metalink_index > 0) |
| 230 |
{ |
| 231 |
if (meta_count < _metalink_index) |
| 232 |
continue; |
| 233 |
else if (meta_count > _metalink_index) |
| 234 |
break; |
| 235 |
} |
| 236 |
|
| 237 |
logprintf (LOG_NOTQUIET, |
| 238 |
_("Processing metaurl %s...\n"), quote (murl->url)); |
| 239 |
|
| 240 |
/* Metalink/XML download file name. */ |
| 241 |
metafile = xstrdup (safename); |
| 242 |
|
| 243 |
if (opt.trustservernames) |
| 244 |
replace_metalink_basename (&metafile, murl->name ? murl->name : murl->url); |
| 245 |
else |
| 246 |
append_suffix_number (&metafile, ".meta#", meta_count); |
| 247 |
|
| 248 |
if (!metalink_check_safe_path (metafile)) |
| 249 |
{ |
| 250 |
logprintf (LOG_NOTQUIET, |
| 251 |
_("Rejecting metaurl file %s. Unsafe name.\n"), |
| 252 |
quote (metafile)); |
| 253 |
xfree (metafile); |
| 254 |
if (_metalink_index > 0) |
| 255 |
break; |
| 256 |
continue; |
| 257 |
} |
| 258 |
|
| 259 |
/* For security reasons, always save metalink metaurl |
| 260 |
files as new unique files. Keep them on failure. */ |
| 261 |
x_retr_err = fetch_metalink_file (murl->url, false, false, |
| 262 |
metafile, &metadest); |
| 263 |
|
| 264 |
/* On failure, try the next metalink metaurl. */ |
| 265 |
if (x_retr_err != RETROK) |
| 266 |
{ |
| 267 |
logprintf (LOG_VERBOSE, |
| 268 |
_("Failed to download %s. Skipping metaurl.\n"), |
| 269 |
quote (metadest ? metadest : metafile)); |
| 270 |
inform_exit_status (x_retr_err); |
| 271 |
xfree (metadest); |
| 272 |
xfree (metafile); |
| 273 |
if (_metalink_index > 0) |
| 274 |
break; |
| 275 |
continue; |
| 276 |
} |
| 277 |
|
| 278 |
/* Parse Metalink/XML. */ |
| 279 |
meta_err = metalink_parse_file (metadest, &metaurl_xml); |
| 280 |
|
| 281 |
/* On failure, try the next metalink metaurl. */ |
| 282 |
if (meta_err) |
| 283 |
{ |
| 284 |
logprintf (LOG_NOTQUIET, |
| 285 |
_("Unable to parse metaurl file %s.\n"), quote (metadest)); |
| 286 |
x_retr_err = METALINK_PARSE_ERROR; |
| 287 |
inform_exit_status (x_retr_err); |
| 288 |
xfree (metadest); |
| 289 |
xfree (metafile); |
| 290 |
if (_metalink_index > 0) |
| 291 |
break; |
| 292 |
continue; |
| 293 |
} |
| 294 |
|
| 295 |
/* We need to sort the resources if preferred location |
| 296 |
was specified by the user. */ |
| 297 |
if (opt.preferred_location && opt.preferred_location[0]) |
| 298 |
{ |
| 299 |
metalink_file_t **x_mfile_ptr; |
| 300 |
for (x_mfile_ptr = metaurl_xml->files; *x_mfile_ptr; x_mfile_ptr++) |
| 301 |
{ |
| 302 |
metalink_resource_t **x_mres_ptr; |
| 303 |
metalink_file_t *x_mfile = *x_mfile_ptr; |
| 304 |
size_t mres_count = 0; |
| 305 |
|
| 306 |
for (x_mres_ptr = x_mfile->resources; *x_mres_ptr; x_mres_ptr++) |
| 307 |
mres_count++; |
| 308 |
|
| 309 |
stable_sort (x_mfile->resources, |
| 310 |
mres_count, |
| 311 |
sizeof (metalink_resource_t *), |
| 312 |
metalink_res_cmp); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
/* Insert the current "Directory Options". */ |
| 317 |
if (metalink->origin) |
| 318 |
{ |
| 319 |
/* WARNING: Do not use lib/dirname.c (dir_name) to |
| 320 |
get the directory name, it may append a dot '.' |
| 321 |
character to the directory name. */ |
| 322 |
metadir = xstrdup (planname); |
| 323 |
replace_metalink_basename (&metadir, NULL); |
| 324 |
} |
| 325 |
else |
| 326 |
{ |
| 327 |
metadir = xstrdup (opt.dir_prefix); |
| 328 |
} |
| 329 |
|
| 330 |
opt.dir_prefix = metadir; |
| 331 |
opt.input_metalink = metadest; |
| 332 |
|
| 333 |
x_retr_err = retrieve_from_metalink (metaurl_xml); |
| 334 |
|
| 335 |
if (x_retr_err != RETROK) |
| 336 |
logprintf (LOG_NOTQUIET, |
| 337 |
_("Could not download all resources from %s.\n"), |
| 338 |
quote (metadest)); |
| 339 |
|
| 340 |
metalink_delete (metaurl_xml); |
| 341 |
metaurl_xml = NULL; |
| 342 |
|
| 343 |
opt.input_metalink = _input_metalink; |
| 344 |
opt.dir_prefix = _dir_prefix; |
| 345 |
|
| 346 |
xfree (metadir); |
| 347 |
xfree (metadest); |
| 348 |
xfree (metafile); |
| 349 |
|
| 350 |
break; |
| 351 |
} |
| 352 |
|
| 353 |
if (x_retr_err != RETROK) |
| 354 |
logprintf (LOG_NOTQUIET, _("Metaurls processing returned with error.\n")); |
| 355 |
|
| 356 |
xfree (destname); |
| 357 |
xfree (filename); |
| 358 |
xfree (trsrname); |
| 359 |
xfree (planname); |
| 360 |
|
| 361 |
opt.output_document = _output_document; |
| 362 |
output_stream_regular = _output_stream_regular; |
| 363 |
output_stream = _output_stream; |
| 364 |
|
| 365 |
opt.metalink_index = _metalink_index; |
| 366 |
|
| 367 |
return x_retr_err; |
| 368 |
} |
| 369 |
|
| 370 |
/* Resources are sorted by priority. */ |
| 371 |
for (mres_ptr = mfile->resources; |
| 372 |
*mres_ptr && mfile->checksums && !skip_mfile; mres_ptr++) |
| 373 |
{ |
| 374 |
metalink_resource_t *mres = *mres_ptr; |
| 375 |
metalink_checksum_t **mchksum_ptr, *mchksum; |
| 376 |
struct iri *iri; |
| 377 |
struct url *url; |
| 378 |
file_stats_t flstats; |
| 379 |
int url_err; |
| 380 |
|
| 381 |
clean_metalink_string (&mres->url); |
| 382 |
|
| 383 |
if (!RES_TYPE_SUPPORTED (mres->type)) |
| 384 |
{ |
| 385 |
logprintf (LOG_VERBOSE, |
| 386 |
_("Resource type %s not supported, ignoring...\n"), |
| 387 |
quote (mres->type)); |
| 388 |
continue; |
| 389 |
} |
| 390 |
|
| 391 |
/* The file is fully downloaded, but some problems were |
| 392 |
encountered (checksum failure?). The loop had been |
| 393 |
continued to switch to the next url. */ |
| 394 |
if (output_stream && retr_err == RETROK) |
| 395 |
{ |
| 396 |
/* Do not rename/remove a continued file. Skip it. */ |
| 397 |
if (opt.always_rest) |
| 398 |
{ |
| 399 |
skip_mfile = true; |
| 400 |
continue; |
| 401 |
} |
| 402 |
|
| 403 |
fclose (output_stream); |
| 404 |
output_stream = NULL; |
| 405 |
badhash_or_remove (destname); |
| 406 |
xfree (destname); |
| 407 |
} |
| 408 |
else if (!output_stream && destname) |
| 409 |
{ |
| 410 |
xfree (destname); |
| 411 |
} |
| 412 |
|
| 413 |
retr_err = METALINK_RETR_ERROR; |
| 414 |
|
| 415 |
/* Parse our resource URL. */ |
| 416 |
iri = iri_new (); |
| 417 |
set_uri_encoding (iri, opt.locale, true); |
| 418 |
url = url_parse (mres->url, &url_err, iri, false); |
| 419 |
|
| 420 |
if (!url) |
| 421 |
{ |
| 422 |
logprintf (LOG_NOTQUIET, "%s: %s.\n", mres->url, url_error (url_err)); |
| 423 |
inform_exit_status (URLERROR); |
| 424 |
iri_free (iri); |
| 425 |
continue; |
| 426 |
} |
| 427 |
else |
| 428 |
{ |
| 429 |
/* Avoid recursive Metalink from HTTP headers. */ |
| 430 |
bool _metalink_http = opt.metalink_over_http; |
| 431 |
|
| 432 |
/* If output_stream is not NULL, then we have failed on |
| 433 |
previous resource and are retrying. Thus, continue |
| 434 |
with the next resource. Do not close output_stream |
| 435 |
while iterating over the resources, or the download |
| 436 |
progress will be lost. */ |
| 437 |
if (output_stream) |
| 438 |
{ |
| 439 |
DEBUGP (("Previous resource failed, continue with next resource.\n")); |
| 440 |
} |
| 441 |
else |
| 442 |
{ |
| 443 |
/* Assure proper local file name regardless of the URL |
| 444 |
of particular Metalink resource. |
| 445 |
To do that we create the local file here and put |
| 446 |
it as output_stream. We restore the original configuration |
| 447 |
after we are finished with the file. */ |
| 448 |
if (opt.always_rest) |
| 449 |
/* continue previous download */ |
| 450 |
output_stream = fopen (safename, "ab"); |
| 451 |
else |
| 452 |
/* create a file with an unique name */ |
| 453 |
output_stream = unique_create (safename, true, &destname); |
| 454 |
} |
| 455 |
|
| 456 |
output_stream_regular = true; |
| 457 |
|
| 458 |
/* |
| 459 |
At this point, if output_stream is NULL, the file |
| 460 |
couldn't be created/opened. |
| 461 |
|
| 462 |
This happens when the metalink:file has a "path/file" |
| 463 |
name format and its directory tree cannot be created: |
| 464 |
* stdio.h (fopen) |
| 465 |
* src/utils.c (unique_create) |
| 466 |
|
| 467 |
RFC5854 requires a proper "path/file" format handling, |
| 468 |
this can be achieved setting opt.output_document while |
| 469 |
output_stream is left to NULL: |
| 470 |
* src/http.c (open_output_stream): If output_stream is |
| 471 |
NULL, create the opt.output_document "path/file" |
| 472 |
*/ |
| 473 |
if (!destname) |
| 474 |
destname = xstrdup (safename); |
| 475 |
|
| 476 |
/* Store the real file name for displaying in messages, |
| 477 |
and for proper RFC5854 "path/file" handling. */ |
| 478 |
opt.output_document = destname; |
| 479 |
|
| 480 |
opt.metalink_over_http = false; |
| 481 |
DEBUGP (("Storing to %s\n", destname)); |
| 482 |
retr_err = retrieve_url (url, mres->url, NULL, NULL, |
| 483 |
NULL, NULL, opt.recursive, iri, false); |
| 484 |
opt.metalink_over_http = _metalink_http; |
| 485 |
|
| 486 |
/* |
| 487 |
Bug: output_stream is NULL, but retrieve_url() somehow |
| 488 |
created destname. |
| 489 |
|
| 490 |
Bugfix: point output_stream to destname if it exists. |
| 491 |
*/ |
| 492 |
memset(&flstats, 0, sizeof(flstats)); |
| 493 |
if (!output_stream && file_exists_p (destname, &flstats)) |
| 494 |
output_stream = fopen_stat (destname, "ab", &flstats); |
| 495 |
} |
| 496 |
url_free (url); |
| 497 |
iri_free (iri); |
| 498 |
|
| 499 |
if (retr_err == RETROK) |
| 500 |
{ |
| 501 |
FILE *local_file; |
| 502 |
|
| 503 |
/* Check the digest. */ |
| 504 |
local_file = fopen (destname, "rb"); |
| 505 |
if (!local_file) |
| 506 |
{ |
| 507 |
logprintf (LOG_NOTQUIET, _("Could not open downloaded file.\n")); |
| 508 |
continue; |
| 509 |
} |
| 510 |
|
| 511 |
size_ok = false; |
| 512 |
logprintf (LOG_VERBOSE, _("Computing size for %s\n"), quote (destname)); |
| 513 |
|
| 514 |
if (!mfile->size) |
| 515 |
{ |
| 516 |
size_ok = true; |
| 517 |
logprintf (LOG_VERBOSE, _("File size not declared. Skipping check.\n")); |
| 518 |
} |
| 519 |
else |
| 520 |
{ |
| 521 |
wgint local_file_size = file_size (destname); |
| 522 |
|
| 523 |
if (local_file_size == -1) |
| 524 |
{ |
| 525 |
logprintf (LOG_NOTQUIET, _("Could not get downloaded file's size.\n")); |
| 526 |
fclose (local_file); |
| 527 |
local_file = NULL; |
| 528 |
continue; |
| 529 |
} |
| 530 |
|
| 531 |
/* FIXME: what about int64? */ |
| 532 |
DEBUGP (("Declared size: %lld\n", mfile->size)); |
| 533 |
DEBUGP (("Computed size: %lld\n", (long long) local_file_size)); |
| 534 |
|
| 535 |
if (local_file_size != (wgint) mfile->size) |
| 536 |
{ |
| 537 |
logprintf (LOG_NOTQUIET, _("Size mismatch for file %s.\n"), quote (destname)); |
| 538 |
fclose (local_file); |
| 539 |
local_file = NULL; |
| 540 |
continue; |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
size_ok = true; |
| 545 |
logputs (LOG_VERBOSE, _("Size matches.\n")); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
for (mchksum_ptr = mfile->checksums; *mchksum_ptr; mchksum_ptr++) |
| 550 |
{ |
| 551 |
char md2[MD2_DIGEST_SIZE]; |
| 552 |
char md2_txt[2 * MD2_DIGEST_SIZE + 1]; |
| 553 |
|
| 554 |
char md4[MD4_DIGEST_SIZE]; |
| 555 |
char md4_txt[2 * MD4_DIGEST_SIZE + 1]; |
| 556 |
|
| 557 |
char md5[MD5_DIGEST_SIZE]; |
| 558 |
char md5_txt[2 * MD5_DIGEST_SIZE + 1]; |
| 559 |
|
| 560 |
char sha1[SHA1_DIGEST_SIZE]; |
| 561 |
char sha1_txt[2 * SHA1_DIGEST_SIZE + 1]; |
| 562 |
|
| 563 |
char sha224[SHA224_DIGEST_SIZE]; |
| 564 |
char sha224_txt[2 * SHA224_DIGEST_SIZE + 1]; |
| 565 |
|
| 566 |
char sha256[SHA256_DIGEST_SIZE]; |
| 567 |
char sha256_txt[2 * SHA256_DIGEST_SIZE + 1]; |
| 568 |
|
| 569 |
char sha384[SHA384_DIGEST_SIZE]; |
| 570 |
char sha384_txt[2 * SHA384_DIGEST_SIZE + 1]; |
| 571 |
|
| 572 |
char sha512[SHA512_DIGEST_SIZE]; |
| 573 |
char sha512_txt[2 * SHA512_DIGEST_SIZE + 1]; |
| 574 |
|
| 575 |
hash_ok = false; |
| 576 |
mchksum = *mchksum_ptr; |
| 577 |
|
| 578 |
/* I have seen both variants... */ |
| 579 |
if (c_strcasecmp (mchksum->type, "md2") |
| 580 |
&& c_strcasecmp (mchksum->type, "md4") |
| 581 |
&& c_strcasecmp (mchksum->type, "md5") |
| 582 |
&& c_strcasecmp (mchksum->type, "sha1") |
| 583 |
&& c_strcasecmp (mchksum->type, "sha-1") |
| 584 |
&& c_strcasecmp (mchksum->type, "sha224") |
| 585 |
&& c_strcasecmp (mchksum->type, "sha-224") |
| 586 |
&& c_strcasecmp (mchksum->type, "sha256") |
| 587 |
&& c_strcasecmp (mchksum->type, "sha-256") |
| 588 |
&& c_strcasecmp (mchksum->type, "sha384") |
| 589 |
&& c_strcasecmp (mchksum->type, "sha-384") |
| 590 |
&& c_strcasecmp (mchksum->type, "sha512") |
| 591 |
&& c_strcasecmp (mchksum->type, "sha-512")) |
| 592 |
{ |
| 593 |
DEBUGP (("Ignoring unsupported checksum type %s.\n", |
| 594 |
quote (mchksum->type))); |
| 595 |
continue; |
| 596 |
} |
| 597 |
|
| 598 |
logprintf (LOG_VERBOSE, _("Computing checksum for %s\n"), |
| 599 |
quote (destname)); |
| 600 |
|
| 601 |
DEBUGP (("Declared hash: %s\n", mchksum->hash)); |
| 602 |
|
| 603 |
if (c_strcasecmp (mchksum->type, "md2") == 0) |
| 604 |
{ |
| 605 |
md2_stream (local_file, md2); |
| 606 |
wg_hex_to_string (md2_txt, md2, MD2_DIGEST_SIZE); |
| 607 |
DEBUGP (("Computed hash: %s\n", md2_txt)); |
| 608 |
if (!strcmp (md2_txt, mchksum->hash)) |
| 609 |
hash_ok = true; |
| 610 |
} |
| 611 |
else if (c_strcasecmp (mchksum->type, "md4") == 0) |
| 612 |
{ |
| 613 |
md4_stream (local_file, md4); |
| 614 |
wg_hex_to_string (md4_txt, md4, MD4_DIGEST_SIZE); |
| 615 |
DEBUGP (("Computed hash: %s\n", md4_txt)); |
| 616 |
if (!strcmp (md4_txt, mchksum->hash)) |
| 617 |
hash_ok = true; |
| 618 |
} |
| 619 |
else if (c_strcasecmp (mchksum->type, "md5") == 0) |
| 620 |
{ |
| 621 |
md5_stream (local_file, md5); |
| 622 |
wg_hex_to_string (md5_txt, md5, MD5_DIGEST_SIZE); |
| 623 |
DEBUGP (("Computed hash: %s\n", md5_txt)); |
| 624 |
if (!strcmp (md5_txt, mchksum->hash)) |
| 625 |
hash_ok = true; |
| 626 |
} |
| 627 |
else if (c_strcasecmp (mchksum->type, "sha1") == 0 |
| 628 |
|| c_strcasecmp (mchksum->type, "sha-1") == 0) |
| 629 |
{ |
| 630 |
sha1_stream (local_file, sha1); |
| 631 |
wg_hex_to_string (sha1_txt, sha1, SHA1_DIGEST_SIZE); |
| 632 |
DEBUGP (("Computed hash: %s\n", sha1_txt)); |
| 633 |
if (!strcmp (sha1_txt, mchksum->hash)) |
| 634 |
hash_ok = true; |
| 635 |
} |
| 636 |
else if (c_strcasecmp (mchksum->type, "sha224") == 0 |
| 637 |
|| c_strcasecmp (mchksum->type, "sha-224") == 0) |
| 638 |
{ |
| 639 |
sha224_stream (local_file, sha224); |
| 640 |
wg_hex_to_string (sha224_txt, sha224, SHA224_DIGEST_SIZE); |
| 641 |
DEBUGP (("Computed hash: %s\n", sha224_txt)); |
| 642 |
if (!strcmp (sha224_txt, mchksum->hash)) |
| 643 |
hash_ok = true; |
| 644 |
} |
| 645 |
else if (c_strcasecmp (mchksum->type, "sha256") == 0 |
| 646 |
|| c_strcasecmp (mchksum->type, "sha-256") == 0) |
| 647 |
{ |
| 648 |
sha256_stream (local_file, sha256); |
| 649 |
wg_hex_to_string (sha256_txt, sha256, SHA256_DIGEST_SIZE); |
| 650 |
DEBUGP (("Computed hash: %s\n", sha256_txt)); |
| 651 |
if (!strcmp (sha256_txt, mchksum->hash)) |
| 652 |
hash_ok = true; |
| 653 |
} |
| 654 |
else if (c_strcasecmp (mchksum->type, "sha384") == 0 |
| 655 |
|| c_strcasecmp (mchksum->type, "sha-384") == 0) |
| 656 |
{ |
| 657 |
sha384_stream (local_file, sha384); |
| 658 |
wg_hex_to_string (sha384_txt, sha384, SHA384_DIGEST_SIZE); |
| 659 |
DEBUGP (("Computed hash: %s\n", sha384_txt)); |
| 660 |
if (!strcmp (sha384_txt, mchksum->hash)) |
| 661 |
hash_ok = true; |
| 662 |
} |
| 663 |
else if (c_strcasecmp (mchksum->type, "sha512") == 0 |
| 664 |
|| c_strcasecmp (mchksum->type, "sha-512") == 0) |
| 665 |
{ |
| 666 |
sha512_stream (local_file, sha512); |
| 667 |
wg_hex_to_string (sha512_txt, sha512, SHA512_DIGEST_SIZE); |
| 668 |
DEBUGP (("Computed hash: %s\n", sha512_txt)); |
| 669 |
if (!strcmp (sha512_txt, mchksum->hash)) |
| 670 |
hash_ok = true; |
| 671 |
} |
| 672 |
|
| 673 |
if (hash_ok) |
| 674 |
{ |
| 675 |
logputs (LOG_VERBOSE, |
| 676 |
_("Checksum matches.\n")); |
| 677 |
} |
| 678 |
else |
| 679 |
{ |
| 680 |
logprintf (LOG_NOTQUIET, |
| 681 |
_("Checksum mismatch for file %s.\n"), |
| 682 |
quote (destname)); |
| 683 |
} |
| 684 |
|
| 685 |
/* Stop as soon as we checked the supported checksum. */ |
| 686 |
break; |
| 687 |
} /* Iterate over available checksums. */ |
| 688 |
fclose (local_file); |
| 689 |
local_file = NULL; |
| 690 |
|
| 691 |
if (!hash_ok) |
| 692 |
continue; |
| 693 |
|
| 694 |
sig_status = 0; /* Not verified. */ |
| 695 |
|
| 696 |
#ifdef HAVE_GPGME |
| 697 |
/* Check the crypto signature. |
| 698 |
|
| 699 |
Note that the signatures from Metalink in XML will not be |
| 700 |
parsed when using libmetalink version older than 0.1.3. |
| 701 |
Metalink-over-HTTP is not affected by this problem. */ |
| 702 |
if (mfile->signature) |
| 703 |
{ |
| 704 |
metalink_signature_t *msig = mfile->signature; |
| 705 |
gpgme_error_t gpgerr; |
| 706 |
gpgme_ctx_t gpgctx; |
| 707 |
gpgme_data_t gpgsigdata, gpgdata; |
| 708 |
gpgme_verify_result_t gpgres; |
| 709 |
gpgme_signature_t gpgsig; |
| 710 |
int fd; |
| 711 |
|
| 712 |
/* Initialize the library - as name suggests. */ |
| 713 |
gpgme_check_version (NULL); |
| 714 |
|
| 715 |
/* Open data file. */ |
| 716 |
fd = open (destname, O_RDONLY); |
| 717 |
if (fd == -1) |
| 718 |
{ |
| 719 |
logputs (LOG_NOTQUIET, |
| 720 |
_("Could not open downloaded file for signature " |
| 721 |
"verification.\n")); |
| 722 |
goto gpg_skip_verification; |
| 723 |
} |
| 724 |
|
| 725 |
/* Assign file descriptor to GPG data structure. */ |
| 726 |
gpgerr = gpgme_data_new_from_fd (&gpgdata, fd); |
| 727 |
if (gpgerr != GPG_ERR_NO_ERROR) |
| 728 |
{ |
| 729 |
logprintf (LOG_NOTQUIET, |
| 730 |
"GPGME data_new_from_fd: %s\n", |
| 731 |
gpgme_strerror (gpgerr)); |
| 732 |
goto gpg_skip_verification; |
| 733 |
} |
| 734 |
|
| 735 |
/* Prepare new GPGME context. */ |
| 736 |
gpgerr = gpgme_new (&gpgctx); |
| 737 |
if (gpgerr != GPG_ERR_NO_ERROR) |
| 738 |
{ |
| 739 |
logprintf (LOG_NOTQUIET, |
| 740 |
"GPGME new: %s\n", |
| 741 |
gpgme_strerror (gpgerr)); |
| 742 |
gpgme_data_release (gpgdata); |
| 743 |
goto gpg_skip_verification; |
| 744 |
} |
| 745 |
|
| 746 |
DEBUGP (("Verifying signature %s:\n%s\n", |
| 747 |
quote (msig->mediatype), |
| 748 |
msig->signature)); |
| 749 |
|
| 750 |
/* Check signature type. */ |
| 751 |
if (strcmp (msig->mediatype, "application/pgp-signature")) |
| 752 |
{ |
| 753 |
/* Unsupported signature type. */ |
| 754 |
gpgme_release (gpgctx); |
| 755 |
gpgme_data_release (gpgdata); |
| 756 |
goto gpg_skip_verification; |
| 757 |
} |
| 758 |
|
| 759 |
gpgerr = gpgme_set_protocol (gpgctx, GPGME_PROTOCOL_OpenPGP); |
| 760 |
if (gpgerr != GPG_ERR_NO_ERROR) |
| 761 |
{ |
| 762 |
logprintf (LOG_NOTQUIET, |
| 763 |
"GPGME set_protocol: %s\n", |
| 764 |
gpgme_strerror (gpgerr)); |
| 765 |
gpgme_release (gpgctx); |
| 766 |
gpgme_data_release (gpgdata); |
| 767 |
goto gpg_skip_verification; |
| 768 |
} |
| 769 |
|
| 770 |
/* Load the signature. */ |
| 771 |
gpgerr = gpgme_data_new_from_mem (&gpgsigdata, |
| 772 |
msig->signature, |
| 773 |
strlen (msig->signature), |
| 774 |
0); |
| 775 |
if (gpgerr != GPG_ERR_NO_ERROR) |
| 776 |
{ |
| 777 |
logprintf (LOG_NOTQUIET, |
| 778 |
_("GPGME data_new_from_mem: %s\n"), |
| 779 |
gpgme_strerror (gpgerr)); |
| 780 |
gpgme_release (gpgctx); |
| 781 |
gpgme_data_release (gpgdata); |
| 782 |
goto gpg_skip_verification; |
| 783 |
} |
| 784 |
|
| 785 |
/* Verify the signature. */ |
| 786 |
gpgerr = gpgme_op_verify (gpgctx, gpgsigdata, gpgdata, NULL); |
| 787 |
if (gpgerr != GPG_ERR_NO_ERROR) |
| 788 |
{ |
| 789 |
logprintf (LOG_NOTQUIET, |
| 790 |
_("GPGME op_verify: %s\n"), |
| 791 |
gpgme_strerror (gpgerr)); |
| 792 |
gpgme_data_release (gpgsigdata); |
| 793 |
gpgme_release (gpgctx); |
| 794 |
gpgme_data_release (gpgdata); |
| 795 |
goto gpg_skip_verification; |
| 796 |
} |
| 797 |
|
| 798 |
/* Check the results. */ |
| 799 |
gpgres = gpgme_op_verify_result (gpgctx); |
| 800 |
if (!gpgres) |
| 801 |
{ |
| 802 |
logputs (LOG_NOTQUIET, |
| 803 |
_("GPGME op_verify_result: NULL\n")); |
| 804 |
gpgme_data_release (gpgsigdata); |
| 805 |
gpgme_release (gpgctx); |
| 806 |
gpgme_data_release (gpgdata); |
| 807 |
goto gpg_skip_verification; |
| 808 |
} |
| 809 |
|
| 810 |
/* The list is null-terminated. */ |
| 811 |
for (gpgsig = gpgres->signatures; gpgsig; gpgsig = gpgsig->next) |
| 812 |
{ |
| 813 |
DEBUGP (("Checking signature %s\n", gpgsig->fpr)); |
| 814 |
|
| 815 |
if (gpgsig->summary |
| 816 |
& (GPGME_SIGSUM_VALID | GPGME_SIGSUM_GREEN)) |
| 817 |
{ |
| 818 |
logputs (LOG_VERBOSE, |
| 819 |
_("Signature validation succeeded.\n")); |
| 820 |
sig_status = 1; |
| 821 |
break; |
| 822 |
} |
| 823 |
|
| 824 |
if (gpgsig->summary & GPGME_SIGSUM_RED) |
| 825 |
{ |
| 826 |
logputs (LOG_NOTQUIET, |
| 827 |
_("Invalid signature. Rejecting resource.\n")); |
| 828 |
sig_status = -1; |
| 829 |
break; |
| 830 |
} |
| 831 |
|
| 832 |
if (gpgsig->summary == 0 |
| 833 |
&& (gpgsig->status & 0xFFFF) == GPG_ERR_NO_ERROR) |
| 834 |
{ |
| 835 |
logputs (LOG_VERBOSE, |
| 836 |
_("Data matches signature, but signature " |
| 837 |
"is not trusted.\n")); |
| 838 |
} |
| 839 |
|
| 840 |
if ((gpgsig->status & 0xFFFF) != GPG_ERR_NO_ERROR) |
| 841 |
{ |
| 842 |
logprintf (LOG_NOTQUIET, |
| 843 |
"GPGME: %s\n", |
| 844 |
gpgme_strerror (gpgsig->status & 0xFFFF)); |
| 845 |
} |
| 846 |
} |
| 847 |
gpgme_data_release (gpgsigdata); |
| 848 |
gpgme_release (gpgctx); |
| 849 |
gpgme_data_release (gpgdata); |
| 850 |
gpg_skip_verification: |
| 851 |
if (fd != -1) |
| 852 |
close (fd); |
| 853 |
} /* endif (mfile->signature) */ |
| 854 |
#endif |
| 855 |
/* Stop if file was downloaded with success. */ |
| 856 |
if (sig_status >= 0) |
| 857 |
break; |
| 858 |
} /* endif RETR_OK. */ |
| 859 |
} /* Iterate over resources. */ |
| 860 |
|
| 861 |
if (!mfile->checksums) |
| 862 |
{ |
| 863 |
logprintf (LOG_NOTQUIET, _("No checksums found.\n")); |
| 864 |
retr_err = METALINK_CHKSUM_ERROR; |
| 865 |
} |
| 866 |
|
| 867 |
if (retr_err != RETROK) |
| 868 |
{ |
| 869 |
logprintf (LOG_VERBOSE, _("Failed to download %s. Skipping resource.\n"), |
| 870 |
quote (destname ? destname : safename)); |
| 871 |
} |
| 872 |
else if (!size_ok) |
| 873 |
{ |
| 874 |
retr_err = METALINK_SIZE_ERROR; |
| 875 |
logprintf (LOG_NOTQUIET, |
| 876 |
_("File %s retrieved but size does not match. " |
| 877 |
"\n"), quote (destname)); |
| 878 |
} |
| 879 |
else if (!hash_ok) |
| 880 |
{ |
| 881 |
retr_err = METALINK_CHKSUM_ERROR; |
| 882 |
logprintf (LOG_NOTQUIET, |
| 883 |
_("File %s retrieved but checksum does not match. " |
| 884 |
"\n"), quote (destname)); |
| 885 |
} |
| 886 |
#ifdef HAVE_GPGME |
| 887 |
/* Signature will be only validated if hash check was successful. */ |
| 888 |
else if (sig_status < 0) |
| 889 |
{ |
| 890 |
retr_err = METALINK_SIG_ERROR; |
| 891 |
logprintf (LOG_NOTQUIET, |
| 892 |
_("File %s retrieved but signature does not match. " |
| 893 |
"\n"), quote (destname)); |
| 894 |
} |
| 895 |
#endif |
| 896 |
last_retr_err = retr_err == RETROK ? last_retr_err : retr_err; |
| 897 |
|
| 898 |
/* Rename the file if error encountered; remove if option specified. |
| 899 |
Note: the file has been downloaded using *_loop. Therefore, it |
| 900 |
is not necessary to keep the file for continuated download. */ |
| 901 |
if (((retr_err != RETROK && !opt.always_rest) || opt.delete_after) |
| 902 |
&& destname != NULL && file_exists_p (destname, NULL)) |
| 903 |
{ |
| 904 |
badhash_or_remove (destname); |
| 905 |
} |
| 906 |
if (output_stream) |
| 907 |
{ |
| 908 |
fclose (output_stream); |
| 909 |
output_stream = NULL; |
| 910 |
} |
| 911 |
xfree (destname); |
| 912 |
xfree (filename); |
| 913 |
xfree (trsrname); |
| 914 |
xfree (planname); |
| 915 |
} /* Iterate over files. */ |
| 916 |
|
| 917 |
/* Restore original values. */ |
| 918 |
opt.output_document = _output_document; |
| 919 |
output_stream_regular = _output_stream_regular; |
| 920 |
output_stream = _output_stream; |
| 921 |
|
| 922 |
return last_retr_err; |
| 923 |
} |
| 924 |
|
| 925 |
/* |
| 926 |
Replace/remove the basename of a file name. |
| 927 |
|
| 928 |
The file name is permanently modified. |
| 929 |
|
| 930 |
Always set NAME to a string, even an empty one. |
| 931 |
|
| 932 |
Use REF's basename as replacement. If REF is NULL or if it doesn't |
| 933 |
provide a valid basename candidate, then remove NAME's basename. |
| 934 |
*/ |
| 935 |
void |
| 936 |
replace_metalink_basename (char **name, char *ref) |
| 937 |
{ |
| 938 |
int n; |
| 939 |
char *p, *new, *basename; |
| 940 |
|
| 941 |
if (!name) |
| 942 |
return; |
| 943 |
|
| 944 |
/* Strip old basename. */ |
| 945 |
if (*name) |
| 946 |
{ |
| 947 |
basename = last_component (*name); |
| 948 |
|
| 949 |
if (basename == *name) |
| 950 |
xfree (*name); |
| 951 |
else |
| 952 |
*basename = '\0'; |
| 953 |
} |
| 954 |
|
| 955 |
/* New basename from file name reference. */ |
| 956 |
if (ref) |
| 957 |
ref = last_component (ref); |
| 958 |
|
| 959 |
/* Replace the old basename. */ |
| 960 |
new = aprintf ("%s%s", *name ? *name : "", ref ? ref : ""); |
| 961 |
xfree (*name); |
| 962 |
*name = new; |
| 963 |
|
| 964 |
/* Remove prefix drive letters if required, i.e. when in w32 |
| 965 |
environments. */ |
| 966 |
p = new; |
| 967 |
while (p[0] != '\0') |
| 968 |
{ |
| 969 |
while ((n = FILE_SYSTEM_PREFIX_LEN (p)) > 0) |
| 970 |
p += n; |
| 971 |
|
| 972 |
if (p != new) |
| 973 |
{ |
| 974 |
while (ISSLASH (p[0])) |
| 975 |
++p; |
| 976 |
new = p; |
| 977 |
continue; |
| 978 |
} |
| 979 |
|
| 980 |
break; |
| 981 |
} |
| 982 |
|
| 983 |
if (*name != new) |
| 984 |
{ |
| 985 |
new = xstrdup (new); |
| 986 |
xfree (*name); |
| 987 |
*name = new; |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
/* |
| 992 |
Strip the directory components from the given name. |
| 993 |
|
| 994 |
Return a pointer to the end of the leading directory components. |
| 995 |
Return NULL if the resulting name is unsafe or invalid. |
| 996 |
|
| 997 |
Due to security issues posed by saving files with unsafe names, here |
| 998 |
the use of libmetalink's metalink_check_safe_path() is enforced. If |
| 999 |
this appears redundant because the given name was already verified, |
| 1000 |
just remember to never underestimate unsafe file names. |
| 1001 |
*/ |
| 1002 |
char * |
| 1003 |
get_metalink_basename (char *name) |
| 1004 |
{ |
| 1005 |
int n; |
| 1006 |
char *basename; |
| 1007 |
|
| 1008 |
if (!name) |
| 1009 |
return NULL; |
| 1010 |
|
| 1011 |
basename = last_component (name); |
| 1012 |
|
| 1013 |
while ((n = FILE_SYSTEM_PREFIX_LEN (basename)) > 0) |
| 1014 |
basename += n; |
| 1015 |
|
| 1016 |
return metalink_check_safe_path (basename) ? basename : NULL; |
| 1017 |
} |
| 1018 |
|
| 1019 |
/* |
| 1020 |
Append a separator and a numeric suffix to a string. |
| 1021 |
|
| 1022 |
The string is permanently modified. |
| 1023 |
*/ |
| 1024 |
void |
| 1025 |
append_suffix_number (char **str, const char *sep, wgint num) |
| 1026 |
{ |
| 1027 |
char *new, buf[24]; |
| 1028 |
|
| 1029 |
number_to_string (buf, num); |
| 1030 |
new = aprintf ("%s%s%s", *str ? *str : "", sep ? sep : "", buf); |
| 1031 |
xfree (*str); |
| 1032 |
*str = new; |
| 1033 |
} |
| 1034 |
|
| 1035 |
/* |
| 1036 |
Remove the string's trailing/leading whitespaces and line breaks. |
| 1037 |
|
| 1038 |
The string is permanently modified. |
| 1039 |
*/ |
| 1040 |
void |
| 1041 |
clean_metalink_string (char **str) |
| 1042 |
{ |
| 1043 |
int c; |
| 1044 |
size_t len; |
| 1045 |
char *new, *beg, *end; |
| 1046 |
|
| 1047 |
if (!str || !*str) |
| 1048 |
return; |
| 1049 |
|
| 1050 |
beg = *str; |
| 1051 |
|
| 1052 |
while ((c = *beg) && (c == '\n' || c == '\r' || c == '\t' || c == ' ')) |
| 1053 |
beg++; |
| 1054 |
|
| 1055 |
end = beg; |
| 1056 |
|
| 1057 |
/* To not truncate a string containing spaces, search the first '\r' |
| 1058 |
or '\n' which ipotetically marks the end of the string. */ |
| 1059 |
while ((c = *end) && (c != '\r') && (c != '\n')) |
| 1060 |
end++; |
| 1061 |
|
| 1062 |
/* If we are at the end of the string, search the first legit |
| 1063 |
character going backward. */ |
| 1064 |
if (*end == '\0') |
| 1065 |
while ((c = *(end - 1)) && (c == '\n' || c == '\r' || c == '\t' || c == ' ')) |
| 1066 |
end--; |
| 1067 |
|
| 1068 |
len = end - beg; |
| 1069 |
|
| 1070 |
new = xmemdup0 (beg, len); |
| 1071 |
xfree (*str); |
| 1072 |
*str = new; |
| 1073 |
} |
| 1074 |
|
| 1075 |
/* |
| 1076 |
Remove the quotation surrounding a string. |
| 1077 |
|
| 1078 |
The string is permanently modified. |
| 1079 |
*/ |
| 1080 |
void |
| 1081 |
dequote_metalink_string (char **str) |
| 1082 |
{ |
| 1083 |
char *new; |
| 1084 |
size_t str_len; |
| 1085 |
|
| 1086 |
if (!str || !*str || ((*str)[0] != '\"' && (*str)[0] != '\'')) |
| 1087 |
return; |
| 1088 |
|
| 1089 |
str_len = strlen (*str); /* current string length */ |
| 1090 |
|
| 1091 |
/* Verify if the current string is surrounded by quotes. */ |
| 1092 |
if (str_len < 2 || (*str)[0] != (*str)[str_len - 1]) |
| 1093 |
return; |
| 1094 |
|
| 1095 |
/* Dequoted string. */ |
| 1096 |
new = xmemdup0 (*str + 1, str_len - 2); |
| 1097 |
xfree (*str); |
| 1098 |
*str = new; |
| 1099 |
} |
| 1100 |
|
| 1101 |
/* Append the suffix ".badhash" to the file NAME, except without |
| 1102 |
overwriting an existing file with that name and suffix. */ |
| 1103 |
void |
| 1104 |
badhash_suffix (char *name) |
| 1105 |
{ |
| 1106 |
char *bhash, *uname; |
| 1107 |
|
| 1108 |
bhash = concat_strings (name, ".badhash", (char *)0); |
| 1109 |
uname = unique_name (bhash); |
| 1110 |
|
| 1111 |
logprintf (LOG_VERBOSE, _("Renaming %s to %s.\n"), |
| 1112 |
quote_n (0, name), quote_n (1, uname)); |
| 1113 |
|
| 1114 |
if (link (name, uname)) |
| 1115 |
logprintf (LOG_NOTQUIET, "link: %s\n", strerror (errno)); |
| 1116 |
else if (unlink (name)) |
| 1117 |
logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno)); |
| 1118 |
|
| 1119 |
xfree (bhash); |
| 1120 |
xfree (uname); |
| 1121 |
} |
| 1122 |
|
| 1123 |
/* Append the suffix ".badhash" to the file NAME, except without |
| 1124 |
overwriting an existing file with that name and suffix. |
| 1125 |
|
| 1126 |
Remove the file NAME if the option --delete-after is specified, or |
| 1127 |
if the option --keep-badhash isn't set. */ |
| 1128 |
void |
| 1129 |
badhash_or_remove (char *name) |
| 1130 |
{ |
| 1131 |
if (opt.delete_after || !opt.keep_badhash) |
| 1132 |
{ |
| 1133 |
logprintf (LOG_VERBOSE, _("Removing %s.\n"), quote (name)); |
| 1134 |
if (unlink (name)) |
| 1135 |
logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno)); |
| 1136 |
} |
| 1137 |
else |
| 1138 |
{ |
| 1139 |
badhash_suffix(name); |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
/* |
| 1144 |
Simple file fetch. |
| 1145 |
|
| 1146 |
Set DESTNAME to the name of the saved file. |
| 1147 |
|
| 1148 |
Resume previous download if RESUME is true. To disable |
| 1149 |
Metalink/HTTP, set METALINK_HTTP to false. |
| 1150 |
*/ |
| 1151 |
uerr_t |
| 1152 |
fetch_metalink_file (const char *url_str, |
| 1153 |
bool resume, bool metalink_http, |
| 1154 |
const char *filename, char **destname) |
| 1155 |
{ |
| 1156 |
FILE *_output_stream = output_stream; |
| 1157 |
bool _output_stream_regular = output_stream_regular; |
| 1158 |
char *_output_document = opt.output_document; |
| 1159 |
bool _metalink_http = opt.metalink_over_http; |
| 1160 |
|
| 1161 |
char *local_file = NULL; |
| 1162 |
|
| 1163 |
uerr_t retr_err = URLERROR; |
| 1164 |
|
| 1165 |
struct iri *iri; |
| 1166 |
struct url *url; |
| 1167 |
int url_err; |
| 1168 |
|
| 1169 |
/* Parse the URL. */ |
| 1170 |
iri = iri_new (); |
| 1171 |
set_uri_encoding (iri, opt.locale, true); |
| 1172 |
url = url_parse (url_str, &url_err, iri, false); |
| 1173 |
|
| 1174 |
if (!url) |
| 1175 |
{ |
| 1176 |
logprintf (LOG_NOTQUIET, "%s: %s.\n", url_str, url_error (url_err)); |
| 1177 |
inform_exit_status (retr_err); |
| 1178 |
iri_free (iri); |
| 1179 |
return retr_err; |
| 1180 |
} |
| 1181 |
|
| 1182 |
output_stream = NULL; |
| 1183 |
|
| 1184 |
if (resume) |
| 1185 |
/* continue previous download */ |
| 1186 |
output_stream = fopen (filename, "ab"); |
| 1187 |
else |
| 1188 |
/* create a file with an unique name */ |
| 1189 |
output_stream = unique_create (filename, true, &local_file); |
| 1190 |
|
| 1191 |
output_stream_regular = true; |
| 1192 |
|
| 1193 |
/* |
| 1194 |
If output_stream is NULL, the file couldn't be created/opened. |
| 1195 |
This could be due to the impossibility to create a directory tree: |
| 1196 |
* stdio.h (fopen) |
| 1197 |
* src/utils.c (unique_create) |
| 1198 |
|
| 1199 |
A call to retrieve_url() can indirectly create a directory tree, |
| 1200 |
when opt.output_document is set to the destination file name and |
| 1201 |
output_stream is left to NULL: |
| 1202 |
* src/http.c (open_output_stream): If output_stream is NULL, |
| 1203 |
create the destination opt.output_document "path/file" |
| 1204 |
*/ |
| 1205 |
if (!local_file) |
| 1206 |
local_file = xstrdup (filename); |
| 1207 |
|
| 1208 |
/* Store the real file name for displaying in messages, and for |
| 1209 |
proper "path/file" handling. */ |
| 1210 |
opt.output_document = local_file; |
| 1211 |
|
| 1212 |
opt.metalink_over_http = metalink_http; |
| 1213 |
|
| 1214 |
DEBUGP (("Storing to %s\n", local_file)); |
| 1215 |
retr_err = retrieve_url (url, url_str, NULL, NULL, |
| 1216 |
NULL, NULL, opt.recursive, iri, false); |
| 1217 |
|
| 1218 |
if (retr_err == RETROK) |
| 1219 |
{ |
| 1220 |
if (destname) |
| 1221 |
*destname = local_file; |
| 1222 |
else |
| 1223 |
xfree (local_file); |
| 1224 |
} |
| 1225 |
|
| 1226 |
if (output_stream) |
| 1227 |
{ |
| 1228 |
fclose (output_stream); |
| 1229 |
output_stream = NULL; |
| 1230 |
} |
| 1231 |
|
| 1232 |
opt.metalink_over_http = _metalink_http; |
| 1233 |
opt.output_document = _output_document; |
| 1234 |
output_stream_regular = _output_stream_regular; |
| 1235 |
output_stream = _output_stream; |
| 1236 |
|
| 1237 |
inform_exit_status (retr_err); |
| 1238 |
|
| 1239 |
iri_free (iri); |
| 1240 |
url_free (url); |
| 1241 |
|
| 1242 |
return retr_err; |
| 1243 |
} |
| 1244 |
|
| 1245 |
int metalink_res_cmp (const void* v1, const void* v2) |
| 1246 |
{ |
| 1247 |
const metalink_resource_t *res1 = *(metalink_resource_t **) v1, |
| 1248 |
*res2 = *(metalink_resource_t **) v2; |
| 1249 |
if (res1->preference != res2->preference) |
| 1250 |
return res2->preference - res1->preference; |
| 1251 |
if (res1->priority != res2->priority) |
| 1252 |
return res1->priority - res2->priority; |
| 1253 |
if (opt.preferred_location) |
| 1254 |
{ |
| 1255 |
int cmp = 0; |
| 1256 |
if (res1->location && |
| 1257 |
!c_strcasecmp (opt.preferred_location, res1->location)) |
| 1258 |
cmp -= 1; |
| 1259 |
if (res2->location && |
| 1260 |
!c_strcasecmp (opt.preferred_location, res2->location)) |
| 1261 |
cmp += 1; |
| 1262 |
return cmp; |
| 1263 |
} |
| 1264 |
return 0; |
| 1265 |
} |
| 1266 |
|
| 1267 |
int metalink_meta_cmp (const void* v1, const void* v2) |
| 1268 |
{ |
| 1269 |
const metalink_metaurl_t *meta1 = *(metalink_metaurl_t **) v1, |
| 1270 |
*meta2 = *(metalink_metaurl_t **) v2; |
| 1271 |
if (meta1->priority != meta2->priority) |
| 1272 |
return meta1->priority - meta2->priority; |
| 1273 |
return 0; |
| 1274 |
} |
| 1275 |
|
| 1276 |
/* |
| 1277 |
Find value of given key. This is intended for Link header, but will |
| 1278 |
work with any header that uses ';' as field separator and '=' as key-value |
| 1279 |
separator. |
| 1280 |
|
| 1281 |
Link = "Link" ":" #link-value |
| 1282 |
link-value = "<" URI-Reference ">" *( ";" link-param ) |
| 1283 |
link-param = ( ( "rel" "=" relation-types ) |
| 1284 |
| ( "anchor" "=" <"> URI-Reference <"> ) |
| 1285 |
| ( "rev" "=" relation-types ) |
| 1286 |
| ( "hreflang" "=" Language-Tag ) |
| 1287 |
| ( "media" "=" ( MediaDesc | ( <"> MediaDesc <"> ) ) ) |
| 1288 |
| ( "title" "=" quoted-string ) |
| 1289 |
| ( "title*" "=" ext-value ) |
| 1290 |
| ( "type" "=" ( media-type | quoted-mt ) ) |
| 1291 |
| ( link-extension ) ) |
| 1292 |
link-extension = ( parmname [ "=" ( ptoken | quoted-string ) ] ) |
| 1293 |
| ( ext-name-star "=" ext-value ) |
| 1294 |
ext-name-star = parmname "*" ; reserved for RFC2231-profiled |
| 1295 |
; extensions. Whitespace NOT |
| 1296 |
; allowed in between. |
| 1297 |
ptoken = 1*ptokenchar |
| 1298 |
ptokenchar = "!" | "#" | "$" | "%" | "&" | "'" | "(" |
| 1299 |
| ")" | "*" | "+" | "-" | "." | "/" | DIGIT |
| 1300 |
| ":" | "<" | "=" | ">" | "?" | "@" | ALPHA |
| 1301 |
| "[" | "]" | "^" | "_" | "`" | "{" | "|" |
| 1302 |
| "}" | "~" |
| 1303 |
media-type = type-name "/" subtype-name |
| 1304 |
quoted-mt = <"> media-type <"> |
| 1305 |
relation-types = relation-type |
| 1306 |
| <"> relation-type *( 1*SP relation-type ) <"> |
| 1307 |
relation-type = reg-rel-type | ext-rel-type |
| 1308 |
reg-rel-type = LOALPHA *( LOALPHA | DIGIT | "." | "-" ) |
| 1309 |
ext-rel-type = URI |
| 1310 |
|
| 1311 |
See more: rfc5988 |
| 1312 |
*/ |
| 1313 |
bool |
| 1314 |
find_key_value (const char *start, const char *end, const char *key, char **value) |
| 1315 |
{ |
| 1316 |
const char *eq; |
| 1317 |
size_t key_len = strlen (key); |
| 1318 |
const char *val_beg, *val_end; |
| 1319 |
const char *key_beg; |
| 1320 |
|
| 1321 |
key_beg = start; |
| 1322 |
|
| 1323 |
while (key_beg + key_len + 1 < end) |
| 1324 |
{ |
| 1325 |
/* Skip whitespaces. */ |
| 1326 |
while (key_beg + key_len + 1 < end && c_isspace (*key_beg)) |
| 1327 |
key_beg++; |
| 1328 |
if (strncmp (key_beg, key, key_len)) |
| 1329 |
{ |
| 1330 |
/* Find next token. */ |
| 1331 |
while (key_beg + key_len + 1 < end && *key_beg != ';') |
| 1332 |
key_beg++; |
| 1333 |
key_beg++; |
| 1334 |
continue; |
| 1335 |
} |
| 1336 |
else |
| 1337 |
{ |
| 1338 |
/* Find equals sign. */ |
| 1339 |
eq = key_beg + key_len; |
| 1340 |
while (eq < end && c_isspace (*eq)) |
| 1341 |
eq++; |
| 1342 |
if (eq == end) |
| 1343 |
return false; |
| 1344 |
if (*eq != '=') |
| 1345 |
{ |
| 1346 |
key_beg++; |
| 1347 |
continue; |
| 1348 |
} |
| 1349 |
|
| 1350 |
val_beg = eq + 1; |
| 1351 |
while (val_beg < end && c_isspace (*val_beg)) |
| 1352 |
val_beg++; |
| 1353 |
if (val_beg == end) |
| 1354 |
return false; |
| 1355 |
val_end = val_beg + 1; |
| 1356 |
while (val_end < end && *val_end != ';' && !c_isspace (*val_end)) |
| 1357 |
val_end++; |
| 1358 |
*value = xstrndup (val_beg, val_end - val_beg); |
| 1359 |
dequote_metalink_string (value); |
| 1360 |
return true; |
| 1361 |
} |
| 1362 |
} |
| 1363 |
*value = NULL; |
| 1364 |
return false; |
| 1365 |
} |
| 1366 |
|
| 1367 |
/* This is to check if given token exists in HTTP header. Tokens are |
| 1368 |
separated by ';'. */ |
| 1369 |
bool |
| 1370 |
has_key (const char *start, const char *end, const char *key) |
| 1371 |
{ |
| 1372 |
const char *pos; /* Here would the token start. */ |
| 1373 |
size_t key_len = strlen (key); |
| 1374 |
|
| 1375 |
pos = start; |
| 1376 |
while (pos + key_len <= end) |
| 1377 |
{ |
| 1378 |
/* Skip whitespaces at beginning. */ |
| 1379 |
while (pos + key_len <= end && c_isspace (*pos)) |
| 1380 |
pos++; |
| 1381 |
|
| 1382 |
/* Does the prefix of pos match our key? */ |
| 1383 |
if (strncmp (key, pos, key_len)) |
| 1384 |
{ |
| 1385 |
/* This was not a match. |
| 1386 |
Skip all characters until beginning of next token. */ |
| 1387 |
while (pos + key_len <= end && *pos != ';') |
| 1388 |
pos++; |
| 1389 |
pos++; |
| 1390 |
continue; |
| 1391 |
} |
| 1392 |
|
| 1393 |
/* key is prefix of pos. Is it the exact token or just a prefix? */ |
| 1394 |
pos += key_len; |
| 1395 |
while (pos < end && c_isspace (*pos)) |
| 1396 |
pos++; |
| 1397 |
if (pos == end || *pos == ';') |
| 1398 |
return true; |
| 1399 |
|
| 1400 |
/* This was not a match (just a prefix). |
| 1401 |
Skip all characters until beginning of next token. */ |
| 1402 |
while (pos + key_len <= end && *pos != ';') |
| 1403 |
pos++; |
| 1404 |
pos++; |
| 1405 |
} |
| 1406 |
return false; |
| 1407 |
} |
| 1408 |
|
| 1409 |
/* Find all key=value pairs delimited with ';' or ','. This is intended for |
| 1410 |
Digest header parsing. |
| 1411 |
The usage is: |
| 1412 |
|
| 1413 |
const char *pos; |
| 1414 |
for (pos = header_beg; pos = find_key_values (pos, header_end, &key, &val); pos++) |
| 1415 |
{ |
| 1416 |
... |
| 1417 |
} |
| 1418 |
|
| 1419 |
*/ |
| 1420 |
const char * |
| 1421 |
find_key_values (const char *start, const char *end, char **key, char **value) |
| 1422 |
{ |
| 1423 |
const char *key_start, *key_end; |
| 1424 |
const char *eq; |
| 1425 |
const char *val_start, *val_end; |
| 1426 |
|
| 1427 |
eq = start; |
| 1428 |
while (eq < end && *eq != '=') |
| 1429 |
{ |
| 1430 |
/* Skip tokens without =value part. */ |
| 1431 |
if (*eq == ';' || *eq == ',') |
| 1432 |
start = eq + 1; |
| 1433 |
eq++; |
| 1434 |
} |
| 1435 |
|
| 1436 |
if (eq >= end) |
| 1437 |
return NULL; |
| 1438 |
|
| 1439 |
key_start = start; |
| 1440 |
while (key_start < eq && c_isspace (*key_start)) |
| 1441 |
key_start++; |
| 1442 |
|
| 1443 |
key_end = eq - 1; |
| 1444 |
while (key_end > key_start && c_isspace (*key_end)) |
| 1445 |
key_end--; |
| 1446 |
key_end++; |
| 1447 |
|
| 1448 |
val_start = eq + 1; |
| 1449 |
while (val_start < end && c_isspace (*val_start)) |
| 1450 |
val_start++; |
| 1451 |
|
| 1452 |
val_end = val_start; |
| 1453 |
|
| 1454 |
while (val_end < end && *val_end != ';' && |
| 1455 |
*val_end != ',' && !c_isspace (*val_end)) |
| 1456 |
val_end++; |
| 1457 |
|
| 1458 |
*key = xstrndup (key_start, key_end - key_start); |
| 1459 |
*value = xstrndup (val_start, val_end - val_start); |
| 1460 |
dequote_metalink_string (value); |
| 1461 |
|
| 1462 |
/* Skip trailing whitespaces. */ |
| 1463 |
while (val_end < end && c_isspace (*val_end)) |
| 1464 |
val_end++; |
| 1465 |
|
| 1466 |
return val_end; |
| 1467 |
} |
| 1468 |
|
| 1469 |
#ifdef TESTING |
| 1470 |
const char * |
| 1471 |
test_find_key_values (void) |
| 1472 |
{ |
| 1473 |
static const char *header_data = "key1=val1;key2=\"val2\" ;key3=val3; key4=val4"\ |
| 1474 |
" ; key5=val5;key6 ='val6';key7= val7; "\ |
| 1475 |
"key8 = val8 ; key9 = \"val9\" "\ |
| 1476 |
" ,key10= 'val10',key11,key12=val12"; |
| 1477 |
static const struct |
| 1478 |
{ |
| 1479 |
const char *key; |
| 1480 |
const char *val; |
| 1481 |
} test_array[] = |
| 1482 |
{ |
| 1483 |
{ "key1", "val1" }, |
| 1484 |
{ "key2", "val2" }, |
| 1485 |
{ "key3", "val3" }, |
| 1486 |
{ "key4", "val4" }, |
| 1487 |
{ "key5", "val5" }, |
| 1488 |
{ "key6", "val6" }, |
| 1489 |
{ "key7", "val7" }, |
| 1490 |
{ "key8", "val8" }, |
| 1491 |
{ "key9", "val9" }, |
| 1492 |
{ "key10", "val10" }, |
| 1493 |
{ "key12", "val12" } |
| 1494 |
}; |
| 1495 |
const char *pos; |
| 1496 |
char *key, *value; |
| 1497 |
size_t i = 0; |
| 1498 |
|
| 1499 |
for (pos = header_data; (pos = find_key_values (pos, |
| 1500 |
header_data + strlen (header_data), |
| 1501 |
&key, &value)); pos++) |
| 1502 |
{ |
| 1503 |
mu_assert ("test_find_key_values: wrong result", |
| 1504 |
!strcmp (test_array[i].val, value) && |
| 1505 |
!strcmp (test_array[i].key, key)); |
| 1506 |
xfree (key); |
| 1507 |
xfree (value); |
| 1508 |
i++; |
| 1509 |
} |
| 1510 |
|
| 1511 |
return NULL; |
| 1512 |
} |
| 1513 |
|
| 1514 |
const char * |
| 1515 |
test_find_key_value (void) |
| 1516 |
{ |
| 1517 |
static const char *header_data = "key1=val1;key2=val2 ;key3='val3'; key4=val4"\ |
| 1518 |
" ; key5='val5';key6 =val6;key7= \"val7\"; "\ |
| 1519 |
"key8 = \"val8\" ; key9 = val9 "; |
| 1520 |
static const struct |
| 1521 |
{ |
| 1522 |
const char *key; |
| 1523 |
const char *val; |
| 1524 |
bool result; |
| 1525 |
} test_array[] = |
| 1526 |
{ |
| 1527 |
{ "key1", "val1", true }, |
| 1528 |
{ "key2", "val2", true }, |
| 1529 |
{ "key3", "val3", true }, |
| 1530 |
{ "key4", "val4", true }, |
| 1531 |
{ "key5", "val5", true }, |
| 1532 |
{ "key6", "val6", true }, |
| 1533 |
{ "key7", "val7", true }, |
| 1534 |
{ "key8", "val8", true }, |
| 1535 |
{ "key9", "val9", true }, |
| 1536 |
{ "key10", NULL, false }, |
| 1537 |
{ "ey1", NULL, false }, |
| 1538 |
{ "dey1", NULL, false } |
| 1539 |
}; |
| 1540 |
size_t i; |
| 1541 |
|
| 1542 |
for (i=0; i < countof (test_array); ++i) |
| 1543 |
{ |
| 1544 |
bool result; |
| 1545 |
char *value; |
| 1546 |
|
| 1547 |
result = find_key_value (header_data, |
| 1548 |
header_data + strlen(header_data), |
| 1549 |
test_array[i].key, &value); |
| 1550 |
|
| 1551 |
mu_assert ("test_find_key_value: wrong result", |
| 1552 |
result == test_array[i].result && |
| 1553 |
((!test_array[i].result && !value) || |
| 1554 |
!strcmp (value, test_array[i].val))); |
| 1555 |
|
| 1556 |
xfree (value); |
| 1557 |
} |
| 1558 |
|
| 1559 |
return NULL; |
| 1560 |
} |
| 1561 |
|
| 1562 |
const char * |
| 1563 |
test_has_key (void) |
| 1564 |
{ |
| 1565 |
static const char *header_data = "key1=val2;token1;xyz; token2;xyz;token3 ;"\ |
| 1566 |
"xyz; token4 ;xyz; token5 "; |
| 1567 |
struct |
| 1568 |
{ |
| 1569 |
const char *token; |
| 1570 |
bool result; |
| 1571 |
} test_array[] = |
| 1572 |
{ |
| 1573 |
{ "key1=val2", true }, |
| 1574 |
{ "token1", true }, |
| 1575 |
{ "token2", true }, |
| 1576 |
{ "token3", true }, |
| 1577 |
{ "token4", true }, |
| 1578 |
{ "token5", true }, |
| 1579 |
{ "token6", false }, |
| 1580 |
{ "oken1", false }, |
| 1581 |
{ "poken1", false }, |
| 1582 |
{ "key1=val2", true } |
| 1583 |
}; |
| 1584 |
size_t i; |
| 1585 |
|
| 1586 |
for (i = 0; i < countof (test_array); ++i) |
| 1587 |
mu_assert ("test_has_key: wrong result", |
| 1588 |
has_key (header_data, header_data + strlen (header_data), |
| 1589 |
test_array[i].token) == test_array[i].result); |
| 1590 |
|
| 1591 |
return NULL; |
| 1592 |
} |
| 1593 |
#endif |
| 1594 |
|
| 1595 |
#endif /* HAVE_METALINK */ |