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