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