summaryrefslogtreecommitdiffstats
path: root/src/libopkg/release.c
blob: 1b6e838ee8f4bee260b3c0760c677e87d11fdf68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* release.c - the opkg package management system

   Copyright (C) 2010,2011 Javier Palacios

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
*/

#include <unistd.h>
#include <ctype.h>

#include "release.h"
#include "opkg_utils.h"
#include "libbb/libbb.h"

#include "opkg_download.h"
#include "sprintf_alloc.h"

#include "release_parse.h"

#include "parse_util.h"
#include "file_util.h"

static void
release_init(release_t *release)
{
     release->name = NULL;
     release->datestring = NULL;
     release->architectures = NULL;
     release->architectures_count = 0;
     release->components = NULL;
     release->components_count = 0;
     release->complist = NULL;
     release->complist_count = 0;
}

release_t *
release_new(void)
{
     release_t *release;

     release = xcalloc(1, sizeof(release_t));
     release_init(release);

     return release;
}

void
release_deinit(release_t *release)
{
    int i;

    free(release->name);
    free(release->datestring);

    for(i = 0; i < release->architectures_count; i++){
	free(release->architectures[i]);
    }
    free(release->architectures);

    for(i = 0; i < release->components_count; i++){
	free(release->components[i]);
    }
    free(release->components);

    for(i = 0; i < release->complist_count; i++){
	free(release->complist[i]);
    }
    free(release->complist);

}

int
release_init_from_file(release_t *release, const char *filename)
{
	int err = 0;
	FILE *release_file;

	release_file = fopen(filename, "r");
	if (release_file == NULL) {
		opkg_perror(ERROR, "Failed to open %s", filename);
		return -1;
	}

	err=release_parse_from_stream(release, release_file);
	if (!err) {
		if (!release_arch_supported(release)) {
			opkg_msg(ERROR, "No valid architecture found on Release file.\n");
			err = -1;
		}
	}

	return err;
}

const char *
item_in_list(const char *comp, char **complist, const unsigned int count)
{
     int i;

     if (!complist)
	  return comp;

     for(i = 0; i < count; i++){
	  if (strcmp(comp, complist[i]) == 0)
		    return complist[i];
     }

     return NULL;
}

int
release_arch_supported(release_t *release)
{
     nv_pair_list_elt_t *l;

     list_for_each_entry(l , &conf->arch_list.head, node) {
	  nv_pair_t *nv = (nv_pair_t *)l->data;
	  if (item_in_list(nv->name, release->architectures, release->architectures_count)) {
	       opkg_msg(DEBUG, "Arch %s (priority %s) supported for dist %s.\n",
			       nv->name, nv->value, release->name);
	       return 1;
	  }
     }

     return 0;
}

int
release_comps_supported(release_t *release, const char *complist)
{
     int ret = 1;
     int i;

     if (complist) {
	  release->complist = parse_list(complist, &release->complist_count, ' ', 1);
	  for(i = 0; i < release->complist_count; i++){
	       if (!item_in_list(release->complist[i], release->components, release->components_count)) {
		    opkg_msg(ERROR, "Component %s not supported for dist %s.\n",
				    release->complist[i], release->name);
		    ret = 0;
	       }
	  }
     }

     return ret;
}

const char **
release_comps(release_t *release, unsigned int *count)
{
     char **comps = release->complist;

     if (!comps) {
	  comps = release->components;
	  *count = release->components_count;
     } else {
	  *count = release->complist_count;
     }

     return (const char **)comps;
}

int
release_download(release_t *release, pkg_src_t *dist, char *lists_dir, char *tmpdir)
{
     int ret = 0;
     unsigned int ncomp;
     const char **comps = release_comps(release, &ncomp);
     nv_pair_list_elt_t *l;
     int i;

     for(i = 0; i < ncomp; i++){
	  int err = 0;
	  char *prefix;

	  sprintf_alloc(&prefix, "%s/dists/%s/%s/binary", dist->value, dist->name,
			comps[i]);

	  list_for_each_entry(l , &conf->arch_list.head, node) {
	       char *url;
	       char *tmp_file_name, *list_file_name;
	       char *subpath = NULL;

	       nv_pair_t *nv = (nv_pair_t *)l->data;

	       sprintf_alloc(&list_file_name, "%s/%s-%s-%s", lists_dir, dist->name, comps[i], nv->name);

	       sprintf_alloc(&tmp_file_name, "%s/%s-%s-%s%s", tmpdir, dist->name, comps[i], nv->name, ".gz");

	       sprintf_alloc(&subpath, "%s/binary-%s/%s", comps[i], nv->name, dist->gzip ? "Packages.gz" : "Packages");

	       if (dist->gzip) {
	       sprintf_alloc(&url, "%s-%s/Packages.gz", prefix, nv->name);
	       err = opkg_download(url, tmp_file_name, NULL, NULL, 1);
	       if (!err) {
		    err = release_verify_file(release, tmp_file_name, subpath);
		    if (err) {
			 unlink (tmp_file_name);
			 unlink (list_file_name);
		    }
	       }
	       if (!err) {
		    FILE *in, *out;
		    opkg_msg(NOTICE, "Inflating %s.\n", url);
		    in = fopen (tmp_file_name, "r");
		    out = fopen (list_file_name, "w");
		    if (in && out) {
			 err = unzip (in, out);
			 if (err)
			      opkg_msg(INFO, "Corrumpt file at %s.\n", url);
		    } else
			 err = 1;
		    if (in)
			 fclose (in);
		    if (out)
			 fclose (out);
		    unlink (tmp_file_name);
	       }
	       free(url);
	       }

	       if (err) {
		    sprintf_alloc(&url, "%s-%s/Packages", prefix, nv->name);
		    err = opkg_download(url, list_file_name, NULL, NULL, 1);
		    if (!err) {
			 err = release_verify_file(release, tmp_file_name, subpath);
			 if (err)
			      unlink (list_file_name);
		    }
		    free(url);
	       }

	       free(tmp_file_name);
	       free(list_file_name);
	  }

	  if(err)
	       ret = 1;

	  free(prefix);
     }

     return ret;
}

int
release_get_size(release_t *release, const char *pathname)
{
     const cksum_t *cksum;

     if (release->md5sums) {
	  cksum = cksum_list_find(release->md5sums, pathname);
	  return cksum->size;
     }

#ifdef HAVE_SHA256
     if (release->sha256sums) {
	  cksum = cksum_list_find(release->sha256sums, pathname);
	  return cksum->size;
     }
#endif

     return -1;
}

const char *
release_get_md5(release_t *release, const char *pathname)
{
     const cksum_t *cksum;

     if (release->md5sums) {
	  cksum = cksum_list_find(release->md5sums, pathname);
	  return cksum->value;
     }

     return '\0';
}

#ifdef HAVE_SHA256
const char *
release_get_sha256(release_t *release, const char *pathname)
{
     const cksum_t *cksum;

     if (release->sha256sums) {
	  cksum = cksum_list_find(release->sha256sums, pathname);
	  return cksum->value;
     }

     return '\0';
}
#endif

int
release_verify_file(release_t *release, const char* file_name, const char *pathname)
{
     struct stat f_info;
     char *f_md5 = NULL;
     const char *md5 = release_get_md5(release, pathname);
#ifdef HAVE_SHA256
     char *f_sha256 = NULL;
     const char *sha256 = release_get_sha256(release, pathname);
#endif
     int ret = 0;

     if (stat(file_name, &f_info) || (f_info.st_size!=release_get_size(release, pathname))) {
	  opkg_msg(ERROR, "Size verification failed for %s - %s.\n", release->name, pathname);
	  ret = 1;
     } else {

     f_md5 = file_md5sum_alloc(file_name);
#ifdef HAVE_SHA256
     f_sha256 = file_sha256sum_alloc(file_name);
#endif

     if (md5 && strcmp(md5, f_md5)) {
	  opkg_msg(ERROR, "MD5 verification failed for %s - %s.\n", release->name, pathname);
	  ret = 1;
#ifdef HAVE_SHA256
     } else if (sha256 && strcmp(sha256, f_sha256)) {
	  opkg_msg(ERROR, "SHA256 verification failed for %s - %s.\n", release->name, pathname);
	  ret = 1;
#endif
     }

     }

     free(f_md5);
#ifdef HAVE_SHA256
     free(f_sha256);
#endif

     return ret;
}