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