1 /*
2  *  dload.c
3  *
4  *  Copyright (c) 2006-2018 Pacman Development Team <pacman-dev@archlinux.org>
5  *  Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/socket.h> /* setsockopt, SO_KEEPALIVE */
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <signal.h>
31 
32 #ifdef HAVE_NETINET_IN_H
33 #include <netinet/in.h> /* IPPROTO_TCP */
34 #endif
35 #ifdef HAVE_NETINET_TCP_H
36 #include <netinet/tcp.h> /* TCP_KEEPINTVL, TCP_KEEPIDLE */
37 #endif
38 
39 #ifdef HAVE_LIBCURL
40 #include <curl/curl.h>
41 #endif
42 
43 /* libalpm */
44 #include "dload.h"
45 #include "alpm_list.h"
46 #include "alpm.h"
47 #include "log.h"
48 #include "util.h"
49 #include "handle.h"
50 
51 #ifdef HAVE_LIBCURL
get_filename(const char * url)52 static const char *get_filename(const char *url)
53 {
54 	char *filename = strrchr(url, '/');
55 	if(filename != NULL) {
56 		filename++;
57 	}
58 	return filename;
59 }
60 
get_fullpath(const char * path,const char * filename,const char * suffix)61 static char *get_fullpath(const char *path, const char *filename,
62 		const char *suffix)
63 {
64 	char *filepath;
65 	/* len = localpath len + filename len + suffix len + null */
66 	size_t len = strlen(path) + strlen(filename) + strlen(suffix) + 1;
67 	MALLOC(filepath, len, return NULL);
68 	snprintf(filepath, len, "%s%s%s", path, filename, suffix);
69 
70 	return filepath;
71 }
72 
get_libcurl_handle(alpm_handle_t * handle)73 static CURL *get_libcurl_handle(alpm_handle_t *handle)
74 {
75 	if(!handle->curl) {
76 		curl_global_init(CURL_GLOBAL_SSL);
77 		handle->curl = curl_easy_init();
78 	}
79 	return handle->curl;
80 }
81 
82 enum {
83 	ABORT_SIGINT = 1,
84 	ABORT_OVER_MAXFILESIZE
85 };
86 
87 static int dload_interrupted;
inthandler(int UNUSED signum)88 static void inthandler(int UNUSED signum)
89 {
90 	dload_interrupted = ABORT_SIGINT;
91 }
92 
dload_progress_cb(void * file,curl_off_t dltotal,curl_off_t dlnow,curl_off_t UNUSED ultotal,curl_off_t UNUSED ulnow)93 static int dload_progress_cb(void *file, curl_off_t dltotal, curl_off_t dlnow,
94 		curl_off_t UNUSED ultotal, curl_off_t UNUSED ulnow)
95 {
96 	struct dload_payload *payload = (struct dload_payload *)file;
97 	off_t current_size, total_size;
98 
99 	/* avoid displaying progress bar for redirects with a body */
100 	if(payload->respcode >= 300) {
101 		return 0;
102 	}
103 
104 	/* SIGINT sent, abort by alerting curl */
105 	if(dload_interrupted) {
106 		return 1;
107 	}
108 
109 	current_size = payload->initial_size + dlnow;
110 
111 	/* is our filesize still under any set limit? */
112 	if(payload->max_size && current_size > payload->max_size) {
113 		dload_interrupted = ABORT_OVER_MAXFILESIZE;
114 		return 1;
115 	}
116 
117 	/* none of what follows matters if the front end has no callback */
118 	if(payload->handle->dlcb == NULL) {
119 		return 0;
120 	}
121 
122 	total_size = payload->initial_size + dltotal;
123 
124 	if(dltotal == 0 || payload->prevprogress == total_size) {
125 		return 0;
126 	}
127 
128 	/* initialize the progress bar here to avoid displaying it when
129 	 * a repo is up to date and nothing gets downloaded.
130 	 * payload->handle->dlcb will receive the remote_name
131 	 * and the following arguments:
132 	 * 0, -1: download initialized
133 	 * 0, 0: non-download event
134 	 * x {x>0}, x: download complete
135 	 * x {x>0, x<y}, y {y > 0}: download progress, expected total is known */
136 	if(!payload->cb_initialized) {
137 		payload->handle->dlcb(payload->remote_name, 0, -1);
138 		payload->cb_initialized = 1;
139 	}
140 	if(payload->prevprogress == current_size) {
141 		payload->handle->dlcb(payload->remote_name, 0, 0);
142 	} else {
143 	/* do NOT include initial_size since it wasn't part of the package's
144 	 * download_size (nor included in the total download size callback) */
145 		payload->handle->dlcb(payload->remote_name, dlnow, dltotal);
146 	}
147 
148 	payload->prevprogress = current_size;
149 
150 	return 0;
151 }
152 
curl_gethost(const char * url,char * buffer,size_t buf_len)153 static int curl_gethost(const char *url, char *buffer, size_t buf_len)
154 {
155 	size_t hostlen;
156 	char *p, *q;
157 
158 	if(strncmp(url, "file://", 7) == 0) {
159 		p = _("disk");
160 		hostlen = strlen(p);
161 	} else {
162 		p = strstr(url, "//");
163 		if(!p) {
164 			return 1;
165 		}
166 		p += 2; /* jump over the found // */
167 		hostlen = strcspn(p, "/");
168 
169 		/* there might be a user:pass@ on the URL. hide it. avoid using memrchr()
170 		 * for portability concerns. */
171 		q = p + hostlen;
172 		while(--q > p) {
173 			if(*q == '@') {
174 				break;
175 			}
176 		}
177 		if(*q == '@' && p != q) {
178 			hostlen -= q - p + 1;
179 			p = q + 1;
180 		}
181 	}
182 
183 	if(hostlen > buf_len - 1) {
184 		/* buffer overflow imminent */
185 		return 1;
186 	}
187 	memcpy(buffer, p, hostlen);
188 	buffer[hostlen] = '\0';
189 
190 	return 0;
191 }
192 
utimes_long(const char * path,long seconds)193 static int utimes_long(const char *path, long seconds)
194 {
195 	if(seconds != -1) {
196 		struct timeval tv[2];
197 		memset(&tv, 0, sizeof(tv));
198 		tv[0].tv_sec = tv[1].tv_sec = seconds;
199 		return utimes(path, tv);
200 	}
201 	return 0;
202 }
203 
204 /* prefix to avoid possible future clash with getumask(3) */
_getumask(void)205 static mode_t _getumask(void)
206 {
207 	mode_t mask = umask(0);
208 	umask(mask);
209 	return mask;
210 }
211 
dload_parseheader_cb(void * ptr,size_t size,size_t nmemb,void * user)212 static size_t dload_parseheader_cb(void *ptr, size_t size, size_t nmemb, void *user)
213 {
214 	size_t realsize = size * nmemb;
215 	const char *fptr, *endptr = NULL;
216 	const char * const cd_header = "Content-Disposition:";
217 	const char * const fn_key = "filename=";
218 	struct dload_payload *payload = (struct dload_payload *)user;
219 	long respcode;
220 
221 	if(_alpm_raw_ncmp(cd_header, ptr, strlen(cd_header)) == 0) {
222 		if((fptr = strstr(ptr, fn_key))) {
223 			fptr += strlen(fn_key);
224 
225 			/* find the end of the field, which is either a semi-colon, or the end of
226 			 * the data. As per curl_easy_setopt(3), we cannot count on headers being
227 			 * null terminated, so we look for the closing \r\n */
228 			endptr = fptr + strcspn(fptr, ";\r\n") - 1;
229 
230 			/* remove quotes */
231 			if(*fptr == '"' && *endptr == '"') {
232 				fptr++;
233 				endptr--;
234 			}
235 
236 			STRNDUP(payload->content_disp_name, fptr, endptr - fptr + 1,
237 					RET_ERR(payload->handle, ALPM_ERR_MEMORY, realsize));
238 		}
239 	}
240 
241 	curl_easy_getinfo(payload->handle->curl, CURLINFO_RESPONSE_CODE, &respcode);
242 	if(payload->respcode != respcode) {
243 		payload->respcode = respcode;
244 	}
245 
246 	return realsize;
247 }
248 
curl_set_handle_opts(struct dload_payload * payload,CURL * curl,char * error_buffer)249 static void curl_set_handle_opts(struct dload_payload *payload,
250 		CURL *curl, char *error_buffer)
251 {
252 	alpm_handle_t *handle = payload->handle;
253 	const char *useragent = getenv("HTTP_USER_AGENT");
254 	struct stat st;
255 
256 	/* the curl_easy handle is initialized with the alpm handle, so we only need
257 	 * to reset the handle's parameters for each time it's used. */
258 	curl_easy_reset(curl);
259 	curl_easy_setopt(curl, CURLOPT_URL, payload->fileurl);
260 	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
261 	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
262 	curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
263 	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
264 	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
265 	curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, dload_progress_cb);
266 	curl_easy_setopt(curl, CURLOPT_XFERINFODATA, (void *)payload);
267 	if(!handle->disable_dl_timeout) {
268 		curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
269 		curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10L);
270 	}
271 	curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, dload_parseheader_cb);
272 	curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *)payload);
273 	curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
274 	curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
275 	curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 60L);
276 	curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 60L);
277 	curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
278 
279 	_alpm_log(handle, ALPM_LOG_DEBUG, "url: %s\n", payload->fileurl);
280 
281 	if(payload->max_size) {
282 		_alpm_log(handle, ALPM_LOG_DEBUG, "maxsize: %jd\n",
283 				(intmax_t)payload->max_size);
284 		curl_easy_setopt(curl, CURLOPT_MAXFILESIZE_LARGE,
285 				(curl_off_t)payload->max_size);
286 	}
287 
288 	if(useragent != NULL) {
289 		curl_easy_setopt(curl, CURLOPT_USERAGENT, useragent);
290 	}
291 
292 	if(!payload->allow_resume && !payload->force && payload->destfile_name &&
293 			stat(payload->destfile_name, &st) == 0) {
294 		/* start from scratch, but only download if our local is out of date. */
295 		curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
296 		curl_easy_setopt(curl, CURLOPT_TIMEVALUE, (long)st.st_mtime);
297 		_alpm_log(handle, ALPM_LOG_DEBUG,
298 				"using time condition: %ld\n", (long)st.st_mtime);
299 	} else if(stat(payload->tempfile_name, &st) == 0 && payload->allow_resume) {
300 		/* a previous partial download exists, resume from end of file. */
301 		payload->tempfile_openmode = "ab";
302 		curl_easy_setopt(curl, CURLOPT_RESUME_FROM_LARGE, (curl_off_t)st.st_size);
303 		_alpm_log(handle, ALPM_LOG_DEBUG,
304 				"tempfile found, attempting continuation from %jd bytes\n",
305 				(intmax_t)st.st_size);
306 		payload->initial_size = st.st_size;
307 	}
308 }
309 
mask_signal(int signum,void (* handler)(int),struct sigaction * origaction)310 static void mask_signal(int signum, void (*handler)(int),
311 		struct sigaction *origaction)
312 {
313 	struct sigaction newaction;
314 
315 	newaction.sa_handler = handler;
316 	sigemptyset(&newaction.sa_mask);
317 	newaction.sa_flags = 0;
318 
319 	sigaction(signum, NULL, origaction);
320 	sigaction(signum, &newaction, NULL);
321 }
322 
unmask_signal(int signum,struct sigaction * sa)323 static void unmask_signal(int signum, struct sigaction *sa)
324 {
325 	sigaction(signum, sa, NULL);
326 }
327 
create_tempfile(struct dload_payload * payload,const char * localpath)328 static FILE *create_tempfile(struct dload_payload *payload, const char *localpath)
329 {
330 	int fd;
331 	FILE *fp;
332 	char *randpath;
333 	size_t len;
334 
335 	/* create a random filename, which is opened with O_EXCL */
336 	len = strlen(localpath) + 14 + 1;
337 	MALLOC(randpath, len, RET_ERR(payload->handle, ALPM_ERR_MEMORY, NULL));
338 	snprintf(randpath, len, "%salpmtmp.XXXXXX", localpath);
339 	if((fd = mkstemp(randpath)) == -1 ||
340 			fchmod(fd, ~(_getumask()) & 0666) ||
341 			!(fp = fdopen(fd, payload->tempfile_openmode))) {
342 		unlink(randpath);
343 		close(fd);
344 		_alpm_log(payload->handle, ALPM_LOG_ERROR,
345 				_("failed to create temporary file for download\n"));
346 		free(randpath);
347 		return NULL;
348 	}
349 	/* fp now points to our alpmtmp.XXXXXX */
350 	free(payload->tempfile_name);
351 	payload->tempfile_name = randpath;
352 	free(payload->remote_name);
353 	STRDUP(payload->remote_name, strrchr(randpath, '/') + 1,
354 			fclose(fp); RET_ERR(payload->handle, ALPM_ERR_MEMORY, NULL));
355 
356 	return fp;
357 }
358 
359 /* RFC1123 states applications should support this length */
360 #define HOSTNAME_SIZE 256
361 
curl_download_internal(struct dload_payload * payload,const char * localpath,char ** final_file,const char ** final_url)362 static int curl_download_internal(struct dload_payload *payload,
363 		const char *localpath, char **final_file, const char **final_url)
364 {
365 	int ret = -1;
366 	FILE *localf = NULL;
367 	char *effective_url;
368 	char hostname[HOSTNAME_SIZE];
369 	char error_buffer[CURL_ERROR_SIZE] = {0};
370 	struct stat st;
371 	long timecond, remote_time = -1;
372 	double remote_size, bytes_dl;
373 	struct sigaction orig_sig_pipe, orig_sig_int;
374 	/* shortcut to our handle within the payload */
375 	alpm_handle_t *handle = payload->handle;
376 	CURL *curl = get_libcurl_handle(handle);
377 	handle->pm_errno = ALPM_ERR_OK;
378 
379 	/* make sure these are NULL */
380 	FREE(payload->tempfile_name);
381 	FREE(payload->destfile_name);
382 	FREE(payload->content_disp_name);
383 
384 	payload->tempfile_openmode = "wb";
385 	if(!payload->remote_name) {
386 		STRDUP(payload->remote_name, get_filename(payload->fileurl),
387 				RET_ERR(handle, ALPM_ERR_MEMORY, -1));
388 	}
389 	if(curl_gethost(payload->fileurl, hostname, sizeof(hostname)) != 0) {
390 		_alpm_log(handle, ALPM_LOG_ERROR, _("url '%s' is invalid\n"), payload->fileurl);
391 		RET_ERR(handle, ALPM_ERR_SERVER_BAD_URL, -1);
392 	}
393 
394 	if(payload->remote_name && strlen(payload->remote_name) > 0 &&
395 			strcmp(payload->remote_name, ".sig") != 0) {
396 		payload->destfile_name = get_fullpath(localpath, payload->remote_name, "");
397 		payload->tempfile_name = get_fullpath(localpath, payload->remote_name, ".part");
398 		if(!payload->destfile_name || !payload->tempfile_name) {
399 			goto cleanup;
400 		}
401 	} else {
402 		/* URL doesn't contain a filename, so make a tempfile. We can't support
403 		 * resuming this kind of download; partial transfers will be destroyed */
404 		payload->unlink_on_fail = 1;
405 
406 		localf = create_tempfile(payload, localpath);
407 		if(localf == NULL) {
408 			goto cleanup;
409 		}
410 	}
411 
412 	curl_set_handle_opts(payload, curl, error_buffer);
413 
414 	if(localf == NULL) {
415 		localf = fopen(payload->tempfile_name, payload->tempfile_openmode);
416 		if(localf == NULL) {
417 			handle->pm_errno = ALPM_ERR_RETRIEVE;
418 			_alpm_log(handle, ALPM_LOG_ERROR,
419 					_("could not open file %s: %s\n"),
420 					payload->tempfile_name, strerror(errno));
421 			goto cleanup;
422 		}
423 	}
424 
425 	_alpm_log(handle, ALPM_LOG_DEBUG,
426 			"opened tempfile for download: %s (%s)\n", payload->tempfile_name,
427 			payload->tempfile_openmode);
428 
429 	curl_easy_setopt(curl, CURLOPT_WRITEDATA, localf);
430 
431 	/* Ignore any SIGPIPE signals. With libcurl, these shouldn't be happening,
432 	 * but better safe than sorry. Store the old signal handler first. */
433 	mask_signal(SIGPIPE, SIG_IGN, &orig_sig_pipe);
434 	dload_interrupted = 0;
435 	mask_signal(SIGINT, &inthandler, &orig_sig_int);
436 
437 	/* perform transfer */
438 	payload->curlerr = curl_easy_perform(curl);
439 	_alpm_log(handle, ALPM_LOG_DEBUG, "curl returned error %d from transfer\n",
440 			payload->curlerr);
441 
442 	/* disconnect relationships from the curl handle for things that might go out
443 	 * of scope, but could still be touched on connection teardown. This really
444 	 * only applies to FTP transfers. */
445 	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
446 	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, (char *)NULL);
447 
448 	/* was it a success? */
449 	switch(payload->curlerr) {
450 		case CURLE_OK:
451 			/* get http/ftp response code */
452 			_alpm_log(handle, ALPM_LOG_DEBUG, "response code: %ld\n", payload->respcode);
453 			if(payload->respcode >= 400) {
454 				payload->unlink_on_fail = 1;
455 				if(!payload->errors_ok) {
456 					/* non-translated message is same as libcurl */
457 					snprintf(error_buffer, sizeof(error_buffer),
458 							"The requested URL returned error: %ld", payload->respcode);
459 					_alpm_log(handle, ALPM_LOG_ERROR,
460 							_("failed retrieving file '%s' from %s : %s\n"),
461 							payload->remote_name, hostname, error_buffer);
462 				}
463 				goto cleanup;
464 			}
465 			break;
466 		case CURLE_ABORTED_BY_CALLBACK:
467 			/* handle the interrupt accordingly */
468 			if(dload_interrupted == ABORT_OVER_MAXFILESIZE) {
469 				payload->curlerr = CURLE_FILESIZE_EXCEEDED;
470 				payload->unlink_on_fail = 1;
471 				handle->pm_errno = ALPM_ERR_LIBCURL;
472 				_alpm_log(handle, ALPM_LOG_ERROR,
473 						_("failed retrieving file '%s' from %s : expected download size exceeded\n"),
474 						payload->remote_name, hostname);
475 			}
476 			goto cleanup;
477 		case CURLE_COULDNT_RESOLVE_HOST:
478 			payload->unlink_on_fail = 1;
479 			handle->pm_errno = ALPM_ERR_SERVER_BAD_URL;
480 			_alpm_log(handle, ALPM_LOG_ERROR,
481 					_("failed retrieving file '%s' from %s : %s\n"),
482 					payload->remote_name, hostname, error_buffer);
483 			goto cleanup;
484 		default:
485 			/* delete zero length downloads */
486 			if(fstat(fileno(localf), &st) == 0 && st.st_size == 0) {
487 				payload->unlink_on_fail = 1;
488 			}
489 			if(!payload->errors_ok) {
490 				handle->pm_errno = ALPM_ERR_LIBCURL;
491 				_alpm_log(handle, ALPM_LOG_ERROR,
492 						_("failed retrieving file '%s' from %s : %s\n"),
493 						payload->remote_name, hostname, error_buffer);
494 			} else {
495 				_alpm_log(handle, ALPM_LOG_DEBUG,
496 						"failed retrieving file '%s' from %s : %s\n",
497 						payload->remote_name, hostname, error_buffer);
498 			}
499 			goto cleanup;
500 	}
501 
502 	/* retrieve info about the state of the transfer */
503 	curl_easy_getinfo(curl, CURLINFO_FILETIME, &remote_time);
504 	curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &remote_size);
505 	curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &bytes_dl);
506 	curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &timecond);
507 	curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
508 
509 	if(final_url != NULL) {
510 		*final_url = effective_url;
511 	}
512 
513 	/* time condition was met and we didn't download anything. we need to
514 	 * clean up the 0 byte .part file that's left behind. */
515 	if(timecond == 1 && DOUBLE_EQ(bytes_dl, 0)) {
516 		_alpm_log(handle, ALPM_LOG_DEBUG, "file met time condition\n");
517 		ret = 1;
518 		unlink(payload->tempfile_name);
519 		goto cleanup;
520 	}
521 
522 	/* remote_size isn't necessarily the full size of the file, just what the
523 	 * server reported as remaining to download. compare it to what curl reported
524 	 * as actually being transferred during curl_easy_perform() */
525 	if(!DOUBLE_EQ(remote_size, -1) && !DOUBLE_EQ(bytes_dl, -1) &&
526 			!DOUBLE_EQ(bytes_dl, remote_size)) {
527 		handle->pm_errno = ALPM_ERR_RETRIEVE;
528 		_alpm_log(handle, ALPM_LOG_ERROR, _("%s appears to be truncated: %jd/%jd bytes\n"),
529 				payload->remote_name, (intmax_t)bytes_dl, (intmax_t)remote_size);
530 		goto cleanup;
531 	}
532 
533 	if(payload->trust_remote_name) {
534 		if(payload->content_disp_name) {
535 			/* content-disposition header has a better name for our file */
536 			free(payload->destfile_name);
537 			payload->destfile_name = get_fullpath(localpath,
538 				get_filename(payload->content_disp_name), "");
539 		} else {
540 			const char *effective_filename = strrchr(effective_url, '/');
541 			if(effective_filename && strlen(effective_filename) > 2) {
542 				effective_filename++;
543 
544 				/* if destfile was never set, we wrote to a tempfile. even if destfile is
545 				 * set, we may have followed some redirects and the effective url may
546 				 * have a better suggestion as to what to name our file. in either case,
547 				 * refactor destfile to this newly derived name. */
548 				if(!payload->destfile_name || strcmp(effective_filename,
549 							strrchr(payload->destfile_name, '/') + 1) != 0) {
550 					free(payload->destfile_name);
551 					payload->destfile_name = get_fullpath(localpath, effective_filename, "");
552 				}
553 			}
554 		}
555 	}
556 
557 	ret = 0;
558 
559 cleanup:
560 	if(localf != NULL) {
561 		fclose(localf);
562 		utimes_long(payload->tempfile_name, remote_time);
563 	}
564 
565 	if(ret == 0) {
566 		const char *realname = payload->tempfile_name;
567 		if(payload->destfile_name) {
568 			realname = payload->destfile_name;
569 			if(rename(payload->tempfile_name, payload->destfile_name)) {
570 				_alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"),
571 						payload->tempfile_name, payload->destfile_name, strerror(errno));
572 				ret = -1;
573 			}
574 		}
575 		if(ret != -1 && final_file) {
576 			STRDUP(*final_file, strrchr(realname, '/') + 1,
577 					RET_ERR(handle, ALPM_ERR_MEMORY, -1));
578 		}
579 	}
580 
581 	if((ret == -1 || dload_interrupted) && payload->unlink_on_fail &&
582 			payload->tempfile_name) {
583 		unlink(payload->tempfile_name);
584 	}
585 
586 	/* restore the old signal handlers */
587 	unmask_signal(SIGINT, &orig_sig_int);
588 	unmask_signal(SIGPIPE, &orig_sig_pipe);
589 	/* if we were interrupted, trip the old handler */
590 	if(dload_interrupted == ABORT_SIGINT) {
591 		raise(SIGINT);
592 	}
593 
594 	return ret;
595 }
596 #endif
597 
598 /** Download a file given by a URL to a local directory.
599  * Does not overwrite an existing file if the download fails.
600  * @param payload the payload context
601  * @param localpath the directory to save the file in
602  * @param final_file the real name of the downloaded file (may be NULL)
603  * @return 0 on success, -1 on error (pm_errno is set accordingly if errors_ok == 0)
604  */
_alpm_download(struct dload_payload * payload,const char * localpath,char ** final_file,const char ** final_url)605 int _alpm_download(struct dload_payload *payload, const char *localpath,
606 		char **final_file, const char **final_url)
607 {
608 	alpm_handle_t *handle = payload->handle;
609 
610 	if(handle->fetchcb == NULL) {
611 #ifdef HAVE_LIBCURL
612 		return curl_download_internal(payload, localpath, final_file, final_url);
613 #else
614 		/* work around unused warnings when building without libcurl */
615 		(void)final_file;
616 		(void)final_url;
617 		RET_ERR(handle, ALPM_ERR_EXTERNAL_DOWNLOAD, -1);
618 #endif
619 	} else {
620 		int ret = handle->fetchcb(payload->fileurl, localpath, payload->force);
621 		if(ret == -1 && !payload->errors_ok) {
622 			RET_ERR(handle, ALPM_ERR_EXTERNAL_DOWNLOAD, -1);
623 		}
624 		return ret;
625 	}
626 }
627 
filecache_find_url(alpm_handle_t * handle,const char * url)628 static char *filecache_find_url(alpm_handle_t *handle, const char *url)
629 {
630 	const char *filebase = strrchr(url, '/');
631 
632 	if(filebase == NULL) {
633 		return NULL;
634 	}
635 
636 	filebase++;
637 	if(*filebase == '\0') {
638 		return NULL;
639 	}
640 
641 	return _alpm_filecache_find(handle, filebase);
642 }
643 
644 /** Fetch a remote pkg. */
alpm_fetch_pkgurl(alpm_handle_t * handle,const char * url)645 char SYMEXPORT *alpm_fetch_pkgurl(alpm_handle_t *handle, const char *url)
646 {
647 	char *filepath;
648 	const char *cachedir, *final_pkg_url = NULL;
649 	char *final_file = NULL;
650 	struct dload_payload payload;
651 	int ret = 0;
652 
653 	CHECK_HANDLE(handle, return NULL);
654 	ASSERT(url, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL));
655 
656 	/* find a valid cache dir to download to */
657 	cachedir = _alpm_filecache_setup(handle);
658 
659 	memset(&payload, 0, sizeof(struct dload_payload));
660 
661 	/* attempt to find the file in our pkgcache */
662 	filepath = filecache_find_url(handle, url);
663 	if(filepath == NULL) {
664 		STRDUP(payload.fileurl, url, RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
665 		payload.allow_resume = 1;
666 		payload.handle = handle;
667 		payload.trust_remote_name = 1;
668 
669 		/* download the file */
670 		ret = _alpm_download(&payload, cachedir, &final_file, &final_pkg_url);
671 		_alpm_dload_payload_reset(&payload);
672 		if(ret == -1) {
673 			_alpm_log(handle, ALPM_LOG_WARNING, _("failed to download %s\n"), url);
674 			free(final_file);
675 			return NULL;
676 		}
677 		_alpm_log(handle, ALPM_LOG_DEBUG, "successfully downloaded %s\n", url);
678 	}
679 
680 	/* attempt to download the signature */
681 	if(ret == 0 && final_pkg_url && (handle->siglevel & ALPM_SIG_PACKAGE)) {
682 		char *sig_filepath, *sig_final_file = NULL;
683 		size_t len;
684 
685 		len = strlen(final_pkg_url) + 5;
686 		MALLOC(payload.fileurl, len, free(final_file); RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
687 		snprintf(payload.fileurl, len, "%s.sig", final_pkg_url);
688 
689 		sig_filepath = filecache_find_url(handle, payload.fileurl);
690 		if(sig_filepath == NULL) {
691 			payload.handle = handle;
692 			payload.trust_remote_name = 1;
693 			payload.force = 1;
694 			payload.errors_ok = (handle->siglevel & ALPM_SIG_PACKAGE_OPTIONAL);
695 
696 			/* set hard upper limit of 16KiB */
697 			payload.max_size = 16 * 1024;
698 
699 			ret = _alpm_download(&payload, cachedir, &sig_final_file, NULL);
700 			if(ret == -1 && !payload.errors_ok) {
701 				_alpm_log(handle, ALPM_LOG_WARNING,
702 						_("failed to download %s\n"), payload.fileurl);
703 				/* Warn now, but don't return NULL. We will fail later during package
704 				 * load time. */
705 			} else if(ret == 0) {
706 				_alpm_log(handle, ALPM_LOG_DEBUG,
707 						"successfully downloaded %s\n", payload.fileurl);
708 			}
709 			FREE(sig_final_file);
710 		}
711 		free(sig_filepath);
712 		_alpm_dload_payload_reset(&payload);
713 	}
714 
715 	/* we should be able to find the file the second time around */
716 	if(filepath == NULL) {
717 		filepath = _alpm_filecache_find(handle, final_file);
718 	}
719 	free(final_file);
720 
721 	return filepath;
722 }
723 
_alpm_dload_payload_reset(struct dload_payload * payload)724 void _alpm_dload_payload_reset(struct dload_payload *payload)
725 {
726 	ASSERT(payload, return);
727 
728 	FREE(payload->remote_name);
729 	FREE(payload->tempfile_name);
730 	FREE(payload->destfile_name);
731 	FREE(payload->content_disp_name);
732 	FREE(payload->fileurl);
733 	memset(payload, '\0', sizeof(*payload));
734 }
735 
_alpm_dload_payload_reset_for_retry(struct dload_payload * payload)736 void _alpm_dload_payload_reset_for_retry(struct dload_payload *payload)
737 {
738 	ASSERT(payload, return);
739 
740 	FREE(payload->fileurl);
741 	payload->initial_size += payload->prevprogress;
742 	payload->prevprogress = 0;
743 	payload->unlink_on_fail = 0;
744 	payload->cb_initialized = 0;
745 }
746