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