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