63702508fd99413397a34070a1c63105f83a1822
[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 = d*100/t;
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     printf ("\r%3d%% |", p);
64     for (i = 1; i < 73; i++)
65     {
66         if (i <= p * 0.73)
67             printf ("=");
68         else
69             printf ("-");
70     }
71     printf ("|");
72     fflush(stdout);
73     return 0;
74 }
75
76 int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name)
77 {
78     int err = 0;
79
80     char *src_basec = strdup(src);
81     char *src_base = basename(src_basec);
82     char *tmp_file_location;
83
84     opkg_message(conf,OPKG_NOTICE,"Downloading %s\n", src);
85         
86     fflush(stdout);
87     
88     if (str_starts_with(src, "file:")) {
89         int ret;
90         const char *file_src = src + 5;
91         opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
92         ret = file_copy(src + 5, dest_file_name);
93         opkg_message(conf,OPKG_INFO,"Done\n");
94         return ret;
95     }
96
97     sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
98     err = unlink(tmp_file_location);
99     if (err && errno != ENOENT) {
100         opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
101                 __FUNCTION__, tmp_file_location, strerror(errno));
102         free(tmp_file_location);
103         return errno;
104     }
105
106     if (conf->http_proxy) {
107         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
108         setenv("http_proxy", conf->http_proxy, 1);
109     }
110     if (conf->ftp_proxy) {
111         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
112         setenv("ftp_proxy", conf->ftp_proxy, 1);
113     }
114     if (conf->no_proxy) {
115         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
116         setenv("no_proxy", conf->no_proxy, 1);
117     }
118
119     /* XXX: BUG rewrite to use execvp or else busybox's internal wget -Jamey 7/23/2002 */ 
120 #if 0
121     sprintf_alloc(&cmd, "wget --passive-ftp %s %s%s %s%s %s -P %s %s",
122                   (conf->http_proxy || conf->ftp_proxy) ? "--proxy=on" : "",
123                   conf->proxy_user ? "--proxy-user=" : "",
124                   conf->proxy_user ? conf->proxy_user : "",
125                   conf->proxy_passwd ? "--proxy-passwd=" : "",
126                   conf->proxy_passwd ? conf->proxy_passwd : "",
127                   conf->verbose_wget ? "" : "-q",
128                   conf->tmp_dir,
129                   src);
130     err = xsystem(cmd);
131     if (err) {
132         if (err != -1) {
133             opkg_message(conf,OPKG_ERROR, "%s: ERROR: Command failed with return value %d: `%s'\n",
134                     __FUNCTION__, err, cmd);
135         } 
136         unlink(tmp_file_location);
137         free(tmp_file_location);
138         free(src_basec);
139         free(cmd);
140         return EINVAL;
141     }
142     free(cmd);
143 #endif
144     CURL *curl;
145     CURLcode res;
146     FILE * file = fopen (tmp_file_location, "w");
147
148     curl = curl_easy_init ();
149     if (curl)
150     {
151         curl_easy_setopt (curl, CURLOPT_URL, src);
152         curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
153         curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0);
154         curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, src);
155         curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
156         curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
157         if (conf->http_proxy || conf->ftp_proxy)
158         {
159             char *userpwd;
160             sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
161                     conf->proxy_passwd);
162             curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
163             free (userpwd);
164         }
165         res = curl_easy_perform (curl);
166         curl_easy_cleanup (curl);
167         fclose (file);
168         if (res)
169             return res;
170
171     }
172     else
173         return -1;
174
175     printf ("\n");
176
177     err = file_move(tmp_file_location, dest_file_name);
178
179     free(tmp_file_location);
180     free(src_basec);
181
182     if (err) {
183         return err;
184     }
185
186     return 0;
187 }
188
189 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
190 {
191     int err;
192     char *url;
193
194     if (pkg->src == NULL) {
195         opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
196                 pkg->name, pkg->parent->name);
197         return -1;
198     }
199
200     sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
201
202     /* XXX: BUG: The pkg->filename might be something like
203        "../../foo.ipk". While this is correct, and exactly what we
204        want to use to construct url above, here we actually need to
205        use just the filename part, without any directory. */
206     sprintf_alloc(&pkg->local_filename, "%s/%s", dir, pkg->filename);
207
208     err = opkg_download(conf, url, pkg->local_filename);
209     free(url);
210
211     return err;
212 }
213
214 /*
215  * Downloads file from url, installs in package database, return package name. 
216  */
217 int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **namep)
218 {
219      int err = 0;
220      pkg_t *pkg;
221      pkg = pkg_new();
222      if (pkg == NULL)
223           return ENOMEM;
224
225      if (str_starts_with(url, "http://")
226          || str_starts_with(url, "ftp://")) {
227           char *tmp_file;
228           char *file_basec = strdup(url);
229           char *file_base = basename(file_basec);
230
231           sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
232           err = opkg_download(conf, url, tmp_file);
233           if (err)
234                return err;
235
236           err = pkg_init_from_file(pkg, tmp_file);
237           if (err)
238                return err;
239           pkg->local_filename = strdup(tmp_file);
240
241           free(tmp_file);
242           free(file_basec);
243
244      } else if (strcmp(&url[strlen(url) - 4], OPKG_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 (char *text_file, char *sig_file)
281 {
282     int status = -1;
283     gpgme_ctx_t ctx;
284     gpgme_data_t sig, text;
285     gpgme_error_t err = -1;
286     gpgme_verify_result_t result;
287     gpgme_signature_t s;
288     
289     err = gpgme_new (&ctx);
290
291     if (err)
292         return -1;
293
294     err = gpgme_data_new_from_file (&sig, sig_file, 1); 
295     if (err)
296         return -1;
297
298     err = gpgme_data_new_from_file (&text, text_file, 1); 
299     if (err)
300         return -1;
301
302     err = gpgme_op_verify (ctx, sig, text, NULL);
303
304     result = gpgme_op_verify_result (ctx);
305
306     /* see if any of the signitures matched */
307     s = result->signatures;
308     while (s)
309     {
310         status = gpg_err_code (s->status);
311         if (status == GPG_ERR_NO_ERROR)
312             break;
313         s = s->next;
314     }
315
316     gpgme_data_release (sig);
317     gpgme_data_release (text);
318     gpgme_release (ctx);
319
320     return status;
321 }