1 /*-
2  * Copyright (c) 2011-2013 Baptiste Daroussin <bapt@FreeBSD.org>
3  * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
4  * Copyright (c) 2011-2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
5  * Copyright (c) 2012-2013 Matthew Seaman <matthew@FreeBSD.org>
6  * Copyright (c) 2014 Vsevolod Stakhov <vsevolod@FreeBSD.org>
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer
15  *    in this position and unchanged.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/stat.h>
33 #include <sys/param.h>
34 #include <sys/mman.h>
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <errno.h>
41 #include <limits.h>
42 
43 #include <libgen.h>
44 
45 #include "pkg.h"
46 #include "private/event.h"
47 #include "private/utils.h"
48 #include "private/pkgdb.h"
49 #include "private/pkg.h"
50 #include "binary.h"
51 
52 extern struct pkg_ctx ctx;
53 
54 int
pkg_repo_binary_get_cached_name(struct pkg_repo * repo,struct pkg * pkg,char * dest,size_t destlen)55 pkg_repo_binary_get_cached_name(struct pkg_repo *repo, struct pkg *pkg,
56 	char *dest, size_t destlen)
57 {
58 	const char *ext = NULL;
59 	const char *packagesite;
60 	struct stat st;
61 
62 	packagesite = pkg_repo_url(repo);
63 
64 	if (strncmp(packagesite, "file:/", 6) == 0) {
65 		snprintf(dest, destlen, "%s/%s", packagesite + 6,
66 		    pkg->repopath);
67 		return (EPKG_OK);
68 	}
69 
70 	if (pkg->repopath != NULL)
71 		ext = strrchr(pkg->repopath, '.');
72 
73 	if (ext != NULL) {
74 		/*
75 		 * The real naming scheme:
76 		 * <cachedir>/<name>-<version>-<checksum>.txz
77 		 */
78 		pkg_snprintf(dest, destlen, "%S/%n-%v%S%z%S",
79 		    ctx.cachedir, pkg, pkg, PKG_HASH_SEPSTR, pkg, ext);
80 		if (stat (dest, &st) == -1 || pkg->pkgsize != st.st_size)
81 			return (EPKG_FATAL);
82 
83 	}
84 	else {
85 		pkg_snprintf(dest, destlen, "%S/%n-%v%S%z", ctx.cachedir, pkg,
86 		    pkg, PKG_HASH_SEPSTR, pkg);
87 	}
88 
89 	return (EPKG_OK);
90 }
91 
92 static int
pkg_repo_binary_create_symlink(struct pkg * pkg,const char * fname,const char * dir)93 pkg_repo_binary_create_symlink(struct pkg *pkg, const char *fname,
94 	const char *dir)
95 {
96 	const char *ext, *dest_fname;
97 	char link_dest_tmp[MAXPATHLEN], link_dest[MAXPATHLEN];
98 
99 	/* Create symlink from full pkgname */
100 	ext = strrchr(fname, '.');
101 	pkg_snprintf(link_dest, sizeof(link_dest), "%S/%n-%v%S",
102 		dir, pkg, pkg, ext ? ext : "");
103 	snprintf(link_dest_tmp, sizeof(link_dest_tmp), "%s.new", link_dest);
104 
105 	/* Ignore errors here */
106 	(void)unlink(link_dest_tmp);
107 
108 	/* Trim the path to just the filename. */
109 	if ((dest_fname = strrchr(fname, '/')) != NULL)
110 		++dest_fname;
111 	if (symlink(dest_fname, link_dest_tmp) == -1) {
112 		pkg_emit_errno("symlink", link_dest);
113 		return (EPKG_FATAL);
114 	}
115 
116 	if (rename(link_dest_tmp, link_dest) == -1) {
117 		pkg_emit_errno("rename", link_dest);
118 		unlink(link_dest_tmp);
119 		return (EPKG_FATAL);
120 	}
121 
122 	return (EPKG_OK);
123 }
124 
125 static int
pkg_repo_binary_try_fetch(struct pkg_repo * repo,struct pkg * pkg,bool already_tried,bool mirror,const char * destdir)126 pkg_repo_binary_try_fetch(struct pkg_repo *repo, struct pkg *pkg,
127 	bool already_tried, bool mirror, const char *destdir)
128 {
129 	char dest[MAXPATHLEN];
130 	char url[MAXPATHLEN];
131 	char *dir = NULL;
132 	bool fetched = false;
133 	struct stat st;
134 	const char *packagesite = NULL;
135 	ssize_t offset = -1;
136 
137 	int retcode = EPKG_OK;
138 
139 	assert((pkg->type & PKG_REMOTE) == PKG_REMOTE);
140 
141 	if (mirror) {
142 		const char *cachedir;
143 
144 		if (destdir != NULL)
145 			cachedir = destdir;
146 		else
147 			cachedir = ctx.cachedir;
148 
149 		snprintf(dest, sizeof(dest), "%s/%s", cachedir, pkg->repopath);
150 	}
151 	else
152 		pkg_repo_binary_get_cached_name(repo, pkg, dest, sizeof(dest));
153 
154 	/* If it is already in the local cachedir, dont bother to
155 	 * download it */
156 	if (stat(dest, &st) == 0) {
157 		/* try to resume */
158 		if (pkg->pkgsize > st.st_size) {
159 			offset = st.st_size;
160 			pkg_debug(1, "Resuming fetch");
161 		} else {
162 			goto checksum;
163 		}
164 	}
165 
166 	/* Create the dirs in cachedir */
167 	dir = get_dirname(xstrdup(dest));
168 	if ((retcode = mkdirs(dir)) != EPKG_OK)
169 		goto cleanup;
170 
171 	/*
172 	 * In multi-repos the remote URL is stored in pkg[PKG_REPOURL]
173 	 * For a single attached database the repository URL should be
174 	 * defined by URL.
175 	 */
176 	packagesite = pkg_repo_url(repo);
177 
178 	if (packagesite == NULL || packagesite[0] == '\0') {
179 		pkg_emit_error("URL is not defined");
180 		retcode = 1;
181 		goto cleanup;
182 	}
183 
184 	if (packagesite[strlen(packagesite) - 1] == '/')
185 		pkg_snprintf(url, sizeof(url), "%S%R", packagesite, pkg);
186 	else
187 		pkg_snprintf(url, sizeof(url), "%S/%R", packagesite, pkg);
188 
189 	if (!mirror && strncasecmp(packagesite, "file://", 7) == 0) {
190 		free(dir);
191 		return (EPKG_OK);
192 	}
193 
194 	retcode = pkg_fetch_file(repo, url, dest, 0, offset, pkg->pkgsize);
195 
196 	if (offset == -1)
197 		fetched = true;
198 
199 	if (retcode != EPKG_OK)
200 		goto cleanup;
201 
202 checksum:
203 	/*	checksum calculation is expensive, if size does not
204 		match, skip it and assume failed checksum. */
205 	if (stat(dest, &st) == -1 || pkg->pkgsize != st.st_size) {
206 		if (already_tried) {
207 			pkg_emit_error("cached package %s-%s: "
208 			    "size mismatch, cannot continue\n"
209 			    "Consider running 'pkg update -f'",
210 			    pkg->name, pkg->version);
211 			retcode = EPKG_FATAL;
212 			goto cleanup;
213 		}
214 
215 		unlink(dest);
216 		free(dir);
217 		pkg_emit_error("cached package %s-%s: "
218 		    "size mismatch, fetching from remote",
219 		    pkg->name, pkg->version);
220 		return (pkg_repo_binary_try_fetch(repo, pkg, true, mirror, destdir));
221 	}
222 	if (pkg_checksum_validate_file(dest, pkg->sum) != 0) {
223 		if (already_tried || fetched) {
224 			pkg_emit_error("%s-%s failed checksum "
225 			    "from repository", pkg->name, pkg->version);
226 			retcode = EPKG_FATAL;
227 		} else {
228 			pkg_emit_error("cached package %s-%s: "
229 			    "checksum mismatch, fetching from remote",
230 			    pkg->name, pkg->version);
231 			unlink(dest);
232 			return (pkg_repo_binary_try_fetch(repo, pkg, true, mirror, destdir));
233 		}
234 	}
235 
236 cleanup:
237 
238 	if (retcode != EPKG_OK)
239 		unlink(dest);
240 	else if (!mirror && dir != NULL) {
241 		(void)pkg_repo_binary_create_symlink(pkg, dest, dir);
242 	}
243 
244 	/* allowed even if dir is NULL */
245 	free(dir);
246 
247 	return (retcode);
248 }
249 
250 int
pkg_repo_binary_fetch(struct pkg_repo * repo,struct pkg * pkg)251 pkg_repo_binary_fetch(struct pkg_repo *repo, struct pkg *pkg)
252 {
253 	return (pkg_repo_binary_try_fetch(repo, pkg, false, false, NULL));
254 }
255 
256 int
pkg_repo_binary_mirror(struct pkg_repo * repo,struct pkg * pkg,const char * destdir)257 pkg_repo_binary_mirror(struct pkg_repo *repo, struct pkg *pkg,
258 	const char *destdir)
259 {
260 	return (pkg_repo_binary_try_fetch(repo, pkg, false, true, destdir));
261 }
262