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