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