Patch from rwhitby to follow 302 redirects properly.
[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 #include "config.h"
20 #include <curl/curl.h>
21 #ifdef HAVE_GPGME
22 #include <gpgme.h>
23 #endif
24
25 #include "includes.h"
26 #include "opkg_download.h"
27 #include "opkg_message.h"
28 #include "opkg_state.h"
29
30 #include "sprintf_alloc.h"
31 #include "xsystem.h"
32 #include "file_util.h"
33 #include "str_util.h"
34 #include "opkg_defines.h"
35
36 int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name, curl_progress_func cb, void *data)
37 {
38 int err = 0;
39
40 char *src_basec = strdup(src);
41 char *src_base = basename(src_basec);
42 char *tmp_file_location;
43
44 opkg_message(conf,OPKG_NOTICE,"Downloading %s\n", src);
45
46 if (str_starts_with(src, "file:")) {
47 int ret;
48 const char *file_src = src + 5;
49 opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
50 ret = file_copy(src + 5, dest_file_name);
51 opkg_message(conf,OPKG_INFO,"Done\n");
52 free(src_basec);
53 return ret;
54 }
55
56 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
57 err = unlink(tmp_file_location);
58 if (err && errno != ENOENT) {
59 opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
60 __FUNCTION__, tmp_file_location, strerror(errno));
61 free(tmp_file_location);
62 free(src_basec);
63 return errno;
64 }
65
66 if (conf->http_proxy) {
67 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
68 setenv("http_proxy", conf->http_proxy, 1);
69 }
70 if (conf->ftp_proxy) {
71 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
72 setenv("ftp_proxy", conf->ftp_proxy, 1);
73 }
74 if (conf->no_proxy) {
75 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
76 setenv("no_proxy", conf->no_proxy, 1);
77 }
78
79 CURL *curl;
80 CURLcode res;
81 FILE * file = fopen (tmp_file_location, "w");
82
83 curl = curl_easy_init ();
84 if (curl)
85 {
86 curl_easy_setopt (curl, CURLOPT_URL, src);
87 curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
88 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, (cb == NULL));
89 if (cb)
90 {
91 curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, data);
92 curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, cb);
93 }
94 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
95 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
96 if (conf->http_proxy || conf->ftp_proxy)
97 {
98 char *userpwd;
99 sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
100 conf->proxy_passwd);
101 curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
102 free (userpwd);
103 }
104 res = curl_easy_perform (curl);
105 fclose (file);
106 if (res)
107 {
108 long error_code;
109 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
110 opkg_message(conf, OPKG_ERROR, "Failed to download %s, error %d\n", src, error_code);
111 free(tmp_file_location);
112 free(src_basec);
113 curl_easy_cleanup (curl);
114 return res;
115 }
116 curl_easy_cleanup (curl);
117
118 }
119 else
120 {
121 free(tmp_file_location);
122 free(src_basec);
123 return -1;
124 }
125
126 err = file_move(tmp_file_location, dest_file_name);
127
128 free(tmp_file_location);
129 free(src_basec);
130
131 if (err) {
132 return err;
133 }
134
135 return 0;
136 }
137
138 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
139 {
140 int err;
141 char *url;
142 char *pkgid;
143 char *stripped_filename;
144
145 if (pkg->src == NULL) {
146 opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
147 pkg->name, pkg->parent->name);
148 return -1;
149 }
150
151 sprintf_alloc (&pkgid, "%s;%s;%s;", pkg->name, pkg->version, pkg->architecture);
152 opkg_set_current_state (conf, OPKG_STATE_DOWNLOADING_PKG, pkgid);
153 free (pkgid);
154
155 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
156
157 /* XXX: BUG: The pkg->filename might be something like
158 "../../foo.opk". While this is correct, and exactly what we
159 want to use to construct url above, here we actually need to
160 use just the filename part, without any directory. */
161
162 stripped_filename = strrchr(pkg->filename, '/');
163 if ( ! stripped_filename )
164 stripped_filename = pkg->filename;
165
166 sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename);
167
168 err = opkg_download(conf, url, pkg->local_filename, NULL, NULL);
169 free(url);
170
171 opkg_set_current_state (conf, OPKG_STATE_NONE, NULL);
172 return err;
173 }
174
175 /*
176 * Downloads file from url, installs in package database, return package name.
177 */
178 int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **namep)
179 {
180 int err = 0;
181 pkg_t *pkg;
182 pkg = pkg_new();
183 if (pkg == NULL)
184 return ENOMEM;
185
186 if (str_starts_with(url, "http://")
187 || str_starts_with(url, "ftp://")) {
188 char *tmp_file;
189 char *file_basec = strdup(url);
190 char *file_base = basename(file_basec);
191
192 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
193 err = opkg_download(conf, url, tmp_file, NULL, NULL);
194 if (err)
195 return err;
196
197 err = pkg_init_from_file(pkg, tmp_file);
198 if (err)
199 return err;
200 pkg->local_filename = strdup(tmp_file);
201
202 free(tmp_file);
203 free(file_basec);
204
205 } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
206 || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
207 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
208
209 err = pkg_init_from_file(pkg, url);
210 if (err)
211 return err;
212 pkg->local_filename = strdup(url);
213 opkg_message(conf, OPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
214 pkg->provided_by_hand = 1;
215
216 } else {
217 pkg_deinit(pkg);
218 free(pkg);
219 return 0;
220 }
221
222 if (!pkg->architecture) {
223 opkg_message(conf, OPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
224 return -EINVAL;
225 }
226
227 pkg->dest = conf->default_dest;
228 pkg->state_want = SW_INSTALL;
229 pkg->state_flag |= SF_PREFER;
230 pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
231 if ( pkg == NULL ){
232 fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
233 return 0;
234 }
235 if (namep) {
236 *namep = strdup(pkg->name);
237 }
238 return 0;
239 }
240
241 int
242 opkg_verify_file (opkg_conf_t *conf, char *text_file, char *sig_file)
243 {
244 #ifdef HAVE_GPGME
245 int status = -1;
246 gpgme_ctx_t ctx;
247 gpgme_data_t sig, text, key;
248 gpgme_error_t err = -1;
249 gpgme_verify_result_t result;
250 gpgme_signature_t s;
251 char *trusted_path = NULL;
252
253 err = gpgme_new (&ctx);
254
255 if (err)
256 return -1;
257
258 sprintf_alloc(&trusted_path, "%s/%s", conf->offline_root, "/etc/opkg/trusted.gpg");
259 err = gpgme_data_new_from_file (&key, trusted_path, 1);
260 free (trusted_path);
261 if (err)
262 {
263 return -1;
264 }
265 err = gpgme_op_import (ctx, key);
266 if (err)
267 {
268 gpgme_data_release (key);
269 return -1;
270 }
271 gpgme_data_release (key);
272
273 err = gpgme_data_new_from_file (&sig, sig_file, 1);
274 if (err)
275 {
276 gpgme_release (ctx);
277 return -1;
278 }
279
280 err = gpgme_data_new_from_file (&text, text_file, 1);
281 if (err)
282 {
283 gpgme_data_release (sig);
284 gpgme_release (ctx);
285 return -1;
286 }
287
288 err = gpgme_op_verify (ctx, sig, text, NULL);
289
290 result = gpgme_op_verify_result (ctx);
291 if (!result)
292 return -1;
293
294 /* see if any of the signitures matched */
295 s = result->signatures;
296 while (s)
297 {
298 status = gpg_err_code (s->status);
299 if (status == GPG_ERR_NO_ERROR)
300 break;
301 s = s->next;
302 }
303
304
305 gpgme_data_release (sig);
306 gpgme_data_release (text);
307 gpgme_release (ctx);
308
309 return status;
310 #else
311 opkg_message (conf, OPKG_NOTICE, "Signature check for %s was skipped because GPG support was not enabled in this build\n");
312 return 0;
313 #endif
314 }