opkg: add --no-check-certificate argument
[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 <sys/wait.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <libgen.h>
24
25 #include "opkg_download.h"
26 #include "opkg_message.h"
27
28 #include "sprintf_alloc.h"
29 #include "xsystem.h"
30 #include "file_util.h"
31 #include "opkg_defines.h"
32 #include "libbb/libbb.h"
33
34 static int str_starts_with(const char *str, const char *prefix)
35 {
36 return (strncmp(str, prefix, strlen(prefix)) == 0);
37 }
38
39 int
40 opkg_download(const char *src, const char *dest_file_name,
41 const short hide_error)
42 {
43 int err = 0;
44
45 char *src_basec = xstrdup(src);
46 char *src_base = basename(src_basec);
47 char *tmp_file_location;
48
49 opkg_msg(NOTICE, "Downloading %s\n", src);
50
51 if (str_starts_with(src, "file:")) {
52 const char *file_src = src + 5;
53 opkg_msg(INFO, "Copying %s to %s...", file_src, dest_file_name);
54 err = file_copy(file_src, dest_file_name);
55 opkg_msg(INFO, "Done.\n");
56 free(src_basec);
57 return err;
58 }
59
60 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
61 free(src_basec);
62 err = unlink(tmp_file_location);
63 if (err && errno != ENOENT) {
64 opkg_perror(ERROR, "Failed to unlink %s", tmp_file_location);
65 free(tmp_file_location);
66 return -1;
67 }
68
69 if (conf->http_proxy) {
70 opkg_msg(DEBUG,
71 "Setting environment variable: http_proxy = %s.\n",
72 conf->http_proxy);
73 setenv("http_proxy", conf->http_proxy, 1);
74 }
75 if (conf->ftp_proxy) {
76 opkg_msg(DEBUG,
77 "Setting environment variable: ftp_proxy = %s.\n",
78 conf->ftp_proxy);
79 setenv("ftp_proxy", conf->ftp_proxy, 1);
80 }
81 if (conf->no_proxy) {
82 opkg_msg(DEBUG,
83 "Setting environment variable: no_proxy = %s.\n",
84 conf->no_proxy);
85 setenv("no_proxy", conf->no_proxy, 1);
86 }
87
88 {
89 int res;
90 const char *argv[9];
91 int i = 0;
92
93 argv[i++] = "wget";
94 argv[i++] = "-q";
95 if (conf->no_check_certificate) {
96 argv[i++] = "--no-check-certificate";
97 }
98 if (conf->http_proxy || conf->ftp_proxy) {
99 argv[i++] = "-Y";
100 argv[i++] = "on";
101 }
102 argv[i++] = "-O";
103 argv[i++] = tmp_file_location;
104 argv[i++] = src;
105 argv[i++] = NULL;
106 res = xsystem(argv);
107
108 if (res) {
109 opkg_msg(ERROR,
110 "Failed to download %s, wget returned %d.\n",
111 src, res);
112 if (res == 4)
113 opkg_msg(ERROR,
114 "Check your network settings and connectivity.\n\n");
115 free(tmp_file_location);
116 return -1;
117 }
118 }
119
120 err = file_move(tmp_file_location, dest_file_name);
121
122 free(tmp_file_location);
123
124 return err;
125 }
126
127 static int
128 opkg_download_cache(const char *src, const char *dest_file_name)
129 {
130 char *cache_name = xstrdup(src);
131 char *cache_location, *p;
132 int err = 0;
133
134 if (!conf->cache || str_starts_with(src, "file:")) {
135 err = opkg_download(src, dest_file_name, 0);
136 goto out1;
137 }
138
139 if (!file_is_dir(conf->cache)) {
140 opkg_msg(ERROR, "%s is not a directory.\n", conf->cache);
141 err = 1;
142 goto out1;
143 }
144
145 for (p = cache_name; *p; p++)
146 if (*p == '/')
147 *p = ','; /* looks nicer than | or # */
148
149 sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
150 if (file_exists(cache_location))
151 opkg_msg(NOTICE, "Copying %s.\n", cache_location);
152 else {
153 /* cache file with funky name not found, try simple name */
154 free(cache_name);
155 char *filename = strrchr(dest_file_name, '/');
156 if (filename)
157 cache_name = xstrdup(filename + 1); // strip leading '/'
158 else
159 cache_name = xstrdup(dest_file_name);
160 free(cache_location);
161 sprintf_alloc(&cache_location, "%s/%s", conf->cache,
162 cache_name);
163 if (file_exists(cache_location))
164 opkg_msg(NOTICE, "Copying %s.\n", cache_location);
165 else {
166 err = opkg_download(src, cache_location, 0);
167 if (err) {
168 (void)unlink(cache_location);
169 goto out2;
170 }
171 }
172 }
173
174 err = file_copy(cache_location, dest_file_name);
175
176 out2:
177 free(cache_location);
178 out1:
179 free(cache_name);
180 return err;
181 }
182
183 int opkg_download_pkg(pkg_t * pkg, const char *dir)
184 {
185 int err;
186 char *url;
187 char *local_filename;
188 char *stripped_filename;
189 char *filename;
190
191 if (pkg->src == NULL) {
192 opkg_msg(ERROR,
193 "Package %s is not available from any configured src.\n",
194 pkg->name);
195 return -1;
196 }
197
198 filename = pkg_get_string(pkg, PKG_FILENAME);
199
200 if (filename == NULL) {
201 opkg_msg(ERROR,
202 "Package %s does not have a valid filename field.\n",
203 pkg->name);
204 return -1;
205 }
206
207 sprintf_alloc(&url, "%s/%s", pkg->src->value, filename);
208
209 /* The filename might be something like
210 "../../foo.opk". While this is correct, and exactly what we
211 want to use to construct url above, here we actually need to
212 use just the filename part, without any directory. */
213
214 stripped_filename = strrchr(filename, '/');
215 if (!stripped_filename)
216 stripped_filename = filename;
217
218 sprintf_alloc(&local_filename, "%s/%s", dir, stripped_filename);
219 pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
220
221 err = opkg_download_cache(url, local_filename);
222 free(url);
223
224 return err;
225 }
226
227 /*
228 * Downloads file from url, installs in package database, return package name.
229 */
230 int opkg_prepare_url_for_install(const char *url, char **namep)
231 {
232 int err = 0;
233 pkg_t *pkg;
234 abstract_pkg_t *ab_pkg;
235
236 pkg = pkg_new();
237
238 if (str_starts_with(url, "http://")
239 || str_starts_with(url, "ftp://")) {
240 char *tmp_file;
241 char *file_basec = xstrdup(url);
242 char *file_base = basename(file_basec);
243
244 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
245 err = opkg_download(url, tmp_file, 0);
246 if (err)
247 return err;
248
249 err = pkg_init_from_file(pkg, tmp_file);
250 if (err)
251 return err;
252
253 free(tmp_file);
254 free(file_basec);
255
256 } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
257 || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
258 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
259
260 err = pkg_init_from_file(pkg, url);
261 if (err)
262 return err;
263 opkg_msg(DEBUG2, "Package %s provided by hand (%s).\n",
264 pkg->name, pkg_get_string(pkg, PKG_LOCAL_FILENAME));
265 pkg->provided_by_hand = 1;
266
267 } else {
268 ab_pkg = ensure_abstract_pkg_by_name(url);
269
270 if (!(ab_pkg->state_flag & SF_NEED_DETAIL)) {
271 opkg_msg(DEBUG, "applying abpkg flag to %s\n", ab_pkg->name);
272 ab_pkg->state_flag |= SF_NEED_DETAIL;
273 }
274
275 pkg_deinit(pkg);
276 free(pkg);
277 return 0;
278 }
279
280 pkg->dest = conf->default_dest;
281 pkg->state_want = SW_INSTALL;
282 pkg->state_flag |= SF_PREFER;
283 hash_insert_pkg(pkg, 1);
284
285 if (namep) {
286 *namep = xstrdup(pkg->name);
287 }
288 return 0;
289 }
290
291 int opkg_verify_file(char *text_file, char *sig_file)
292 {
293 #if defined HAVE_USIGN
294 int status = -1;
295 int pid;
296
297 if (conf->check_signature == 0)
298 return 0;
299
300 pid = fork();
301 if (pid < 0)
302 return -1;
303
304 if (!pid) {
305 execl("/usr/sbin/opkg-key", "opkg-key", "verify", sig_file,
306 text_file, NULL);
307 exit(255);
308 }
309
310 waitpid(pid, &status, 0);
311 if (!WIFEXITED(status) || WEXITSTATUS(status))
312 return -1;
313
314 return 0;
315 #else
316 /* mute `unused variable' warnings. */
317 (void)sig_file;
318 (void)text_file;
319 (void)conf;
320 return 0;
321 #endif
322 }