59123d25babfa2d05cf373ead8ac04ec857af95a
[project/opkg-lede.git] / libopkg / opkg_download.c
1 /* vi: set noexpandtab sw=4 sts=4: */
2 /* opkg_download.c - the opkg package management system
3
4 Carl D. Worth
5
6 Copyright (C) 2001 University of Southern California
7 Copyright (C) 2008 OpenMoko Inc
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18 */
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <libgen.h>
23
24 #include "opkg_download.h"
25 #include "opkg_message.h"
26
27 #include "sprintf_alloc.h"
28 #include "xsystem.h"
29 #include "file_util.h"
30 #include "opkg_defines.h"
31 #include "libbb/libbb.h"
32
33 static int str_starts_with(const char *str, const char *prefix)
34 {
35 return (strncmp(str, prefix, strlen(prefix)) == 0);
36 }
37
38 int opkg_verify_integrity(pkg_t *pkg, const char *filename)
39 {
40 int err = 0;
41 char *file_md5, *pkg_md5;
42 char *file_sha256, *pkg_sha256;
43 struct stat pkg_stat;
44 long long int pkg_expected_size;
45
46 /* Check file size */
47 err = lstat(filename, &pkg_stat);
48
49 if (err) {
50 opkg_msg(ERROR, "Failed to stat %s: %s\n",
51 filename, strerror(errno));
52 return err;
53 }
54
55 pkg_expected_size = pkg_get_int(pkg, PKG_SIZE);
56
57 if (pkg_expected_size > 0 && pkg_stat.st_size != pkg_expected_size) {
58 if (!conf->force_checksum) {
59 opkg_msg(ERROR,
60 "Package size mismatch: %s is %lld bytes, expecting %lld bytes\n",
61 pkg->name, (long long int)pkg_stat.st_size, pkg_expected_size);
62 return -1;
63 } else {
64 opkg_msg(NOTICE,
65 "Ignored %s size mismatch.\n",
66 pkg->name);
67 }
68 }
69
70 /* Check for md5 values */
71 pkg_md5 = pkg_get_md5(pkg);
72 if (pkg_md5) {
73 file_md5 = file_md5sum_alloc(filename);
74 if (file_md5 && strcmp(file_md5, pkg_md5)) {
75 if (!conf->force_checksum) {
76 opkg_msg(ERROR, "Package %s md5sum mismatch. "
77 "Either the opkg or the package index are corrupt. "
78 "Try 'opkg update'.\n", pkg->name);
79 free(file_md5);
80 return -1;
81 } else {
82 opkg_msg(NOTICE,
83 "Ignored %s md5sum mismatch.\n",
84 pkg->name);
85 }
86 }
87 if (file_md5)
88 free(file_md5);
89 }
90
91 /* Check for sha256 value */
92 pkg_sha256 = pkg_get_sha256(pkg);
93 if (pkg_sha256) {
94 file_sha256 = file_sha256sum_alloc(filename);
95 if (file_sha256 && strcmp(file_sha256, pkg_sha256)) {
96 if (!conf->force_checksum) {
97 opkg_msg(ERROR,
98 "Package %s sha256sum mismatch. "
99 "Either the opkg or the package index are corrupt. "
100 "Try 'opkg update'.\n", pkg->name);
101 free(file_sha256);
102 return -1;
103 } else {
104 opkg_msg(NOTICE,
105 "Ignored %s sha256sum mismatch.\n",
106 pkg->name);
107 }
108 }
109 if (file_sha256)
110 free(file_sha256);
111 }
112
113 return err;
114 }
115
116 int
117 opkg_download(const char *src, const char *dest_file_name,
118 const short hide_error)
119 {
120 int err = 0;
121
122 char *src_basec = xstrdup(src);
123 char *src_base = basename(src_basec);
124 char *tmp_file_location;
125
126 opkg_msg(NOTICE, "Downloading %s\n", src);
127
128 if (str_starts_with(src, "file:")) {
129 char *file_src = urldecode_path(src + 5);
130 opkg_msg(INFO, "Copying %s to %s...", file_src, dest_file_name);
131 err = file_copy(file_src, dest_file_name);
132 opkg_msg(INFO, "Done.\n");
133 free(src_basec);
134 free(file_src);
135 return err;
136 }
137
138 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
139 free(src_basec);
140 err = unlink(tmp_file_location);
141 if (err && errno != ENOENT) {
142 opkg_perror(ERROR, "Failed to unlink %s", tmp_file_location);
143 free(tmp_file_location);
144 return -1;
145 }
146
147 if (conf->http_proxy) {
148 opkg_msg(DEBUG,
149 "Setting environment variable: http_proxy = %s.\n",
150 conf->http_proxy);
151 setenv("http_proxy", conf->http_proxy, 1);
152 }
153 if (conf->ftp_proxy) {
154 opkg_msg(DEBUG,
155 "Setting environment variable: ftp_proxy = %s.\n",
156 conf->ftp_proxy);
157 setenv("ftp_proxy", conf->ftp_proxy, 1);
158 }
159 if (conf->no_proxy) {
160 opkg_msg(DEBUG,
161 "Setting environment variable: no_proxy = %s.\n",
162 conf->no_proxy);
163 setenv("no_proxy", conf->no_proxy, 1);
164 }
165
166 {
167 int res;
168 const char *argv[11];
169 int i = 0;
170
171 argv[i++] = "wget";
172 argv[i++] = "-q";
173 if (conf->no_check_certificate) {
174 argv[i++] = "--no-check-certificate";
175 }
176 if (conf->http_timeout) {
177 argv[i++] = "--timeout";
178 argv[i++] = conf->http_timeout;
179 }
180 if (conf->http_proxy || conf->ftp_proxy) {
181 argv[i++] = "-Y";
182 argv[i++] = "on";
183 }
184 argv[i++] = "-O";
185 argv[i++] = tmp_file_location;
186 argv[i++] = src;
187 argv[i++] = NULL;
188 res = xsystem(argv);
189
190 if (res) {
191 opkg_msg(ERROR,
192 "Failed to download %s, wget returned %d.\n",
193 src, res);
194 if (res == 4)
195 opkg_msg(ERROR,
196 "Check your network settings and connectivity.\n\n");
197 free(tmp_file_location);
198 return -1;
199 }
200 }
201
202 err = file_move(tmp_file_location, dest_file_name);
203
204 free(tmp_file_location);
205
206 return err;
207 }
208
209 static char* get_cache_filename(const char *dest_file_name)
210 {
211 char *cache_name;
212 char *filename = strrchr(dest_file_name, '/');
213 if (filename)
214 cache_name = xstrdup(filename + 1); // strip leading '/'
215 else
216 cache_name = xstrdup(dest_file_name);
217 return cache_name;
218 }
219
220 static int
221 opkg_download_cache(const char *src, const char *dest_file_name)
222 {
223 char *cache_name, *cache_location;
224 int err = 0;
225
226 if (!conf->cache || str_starts_with(src, "file:")) {
227 err = opkg_download(src, dest_file_name, 0);
228 goto out1;
229 }
230
231 if (!file_is_dir(conf->cache)) {
232 opkg_msg(ERROR, "%s is not a directory.\n", conf->cache);
233 err = 1;
234 goto out1;
235 }
236
237 cache_name = get_cache_filename(dest_file_name);
238 sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
239 if (file_exists(cache_location))
240 opkg_msg(NOTICE, "Copying %s.\n", cache_location);
241 else {
242 err = opkg_download(src, cache_location, 0);
243 if (err) {
244 (void)unlink(cache_location);
245 goto out2;
246 }
247 }
248
249 err = file_copy(cache_location, dest_file_name);
250
251 out2:
252 free(cache_location);
253 free(cache_name);
254 out1:
255 return err;
256 }
257
258 int opkg_download_pkg(pkg_t * pkg, const char *dir)
259 {
260 int err;
261 char *url;
262 char *local_filename;
263 char *stripped_filename;
264 char *urlencoded_path;
265 char *filename;
266 char *cache_name;
267 char *cache_location;
268
269 if (pkg->src == NULL) {
270 opkg_msg(ERROR,
271 "Package %s is not available from any configured src.\n",
272 pkg->name);
273 return -1;
274 }
275
276 filename = pkg_get_string(pkg, PKG_FILENAME);
277
278 if (filename == NULL) {
279 opkg_msg(ERROR,
280 "Package %s does not have a valid filename field.\n",
281 pkg->name);
282 return -1;
283 }
284
285 urlencoded_path = urlencode_path(filename);
286 sprintf_alloc(&url, "%s/%s", pkg->src->value, urlencoded_path);
287 free(urlencoded_path);
288
289 /* The filename might be something like
290 "../../foo.opk". While this is correct, and exactly what we
291 want to use to construct url above, here we actually need to
292 use just the filename part, without any directory. */
293
294 stripped_filename = strrchr(filename, '/');
295 if (!stripped_filename)
296 stripped_filename = filename;
297
298 sprintf_alloc(&local_filename, "%s/%s", dir, stripped_filename);
299 pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
300
301 /* Invalidate/remove cached package if it has an incorrect checksum. */
302 if (conf->cache) {
303 cache_name = get_cache_filename(local_filename);
304 sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
305 free(cache_name);
306 if (file_exists(cache_location)) {
307 err = opkg_verify_integrity(pkg, cache_location);
308 if (err) {
309 opkg_msg(NOTICE,
310 "Removing %s from cache because it has incorrect checksum.\n",
311 pkg->name);
312 unlink(cache_location);
313 }
314 }
315 free(cache_location);
316 }
317
318 err = opkg_download_cache(url, local_filename);
319 free(url);
320
321 return err;
322 }
323
324 /*
325 * Downloads file from url, installs in package database, return package name.
326 */
327 int opkg_prepare_url_for_install(const char *url, char **namep)
328 {
329 int err = 0;
330 pkg_t *pkg;
331 abstract_pkg_t *ab_pkg;
332
333 pkg = pkg_new();
334
335 if (str_starts_with(url, "http://")
336 || str_starts_with(url, "ftp://")) {
337 char *tmp_file;
338 char *file_basec = xstrdup(url);
339 char *file_base = basename(file_basec);
340
341 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
342 err = opkg_download(url, tmp_file, 0);
343 if (err)
344 return err;
345
346 err = pkg_init_from_file(pkg, tmp_file);
347 if (err)
348 return err;
349
350 free(tmp_file);
351 free(file_basec);
352
353 } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
354 || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
355 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
356
357 err = pkg_init_from_file(pkg, url);
358 if (err)
359 return err;
360 opkg_msg(DEBUG2, "Package %s provided by hand (%s).\n",
361 pkg->name, pkg_get_string(pkg, PKG_LOCAL_FILENAME));
362 pkg->provided_by_hand = 1;
363
364 } else {
365 ab_pkg = ensure_abstract_pkg_by_name(url);
366
367 if (!(ab_pkg->state_flag & SF_NEED_DETAIL)) {
368 opkg_msg(DEBUG, "applying abpkg flag to %s\n", ab_pkg->name);
369 ab_pkg->state_flag |= SF_NEED_DETAIL;
370 }
371
372 pkg_deinit(pkg);
373 free(pkg);
374 return 0;
375 }
376
377 pkg->dest = conf->default_dest;
378 pkg->state_want = SW_INSTALL;
379 pkg->state_flag |= SF_PREFER;
380 hash_insert_pkg(pkg, 1);
381
382 if (namep) {
383 *namep = xstrdup(pkg->name);
384 }
385 return 0;
386 }
387
388 int opkg_verify_file(char *text_file, char *sig_file)
389 {
390 #if defined HAVE_USIGN
391 const char *argv[] = { conf->verify_program, "verify", sig_file,
392 text_file, NULL };
393
394 return xsystem(argv) ? -1 : 0;
395 #else
396 /* mute `unused variable' warnings. */
397 (void)sig_file;
398 (void)text_file;
399 (void)conf;
400 return 0;
401 #endif
402 }