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
343
344
345
346
347
348
349
350
351
352
|
/* unpack the payload of RPM package to the current directory
*
* File name: rpmextract.c
* Date: 2009/12/18
* Author: Martin Sivak <msivak at redhat dot com>
*
* Copyright (C) 2009 Red Hat, Inc. All rights reserved.
*
* 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 of the
* License, 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rpm/rpmlib.h> /* rpmReadPackageFile .. */
#include <rpm/rpmtag.h>
#include <rpm/rpmio.h>
#include <rpm/rpmpgp.h>
#include <rpm/rpmts.h>
#include <stdio.h>
#include <archive.h>
#include <archive_entry.h>
#include "loader.h"
#include "rpmextract.h"
#include "../pyanaconda/isys/log.h"
/*
* internal structure to pass to libarchive callbacks
*/
struct cpio_mydata {
FD_t gzdi;
char *buffer;
};
/*
* libarchive callbacks
*/
ssize_t rpm_myread(struct archive *a, void *client_data, const void **buff)
{
struct cpio_mydata *mydata = client_data;
*buff = mydata->buffer;
return Fread(mydata->buffer, 1, BUFFERSIZE, mydata->gzdi);
}
int rpm_myclose(struct archive *a, void *client_data)
{
struct cpio_mydata *mydata = client_data;
if (mydata->gzdi > 0)
Fclose(mydata->gzdi);
return ARCHIVE_OK;
}
/* read data from RPM header */
const char * headerGetString(Header h, rpmTag tag)
{
const char *res = NULL;
struct rpmtd_s td;
if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
if (rpmtdCount(&td) == 1) {
res = rpmtdGetString(&td);
}
rpmtdFreeData(&td);
}
return res;
}
/*
* explode source RPM into the current directory
* use filters to skip packages and files we do not need
*/
int explodeRPM(const char *source,
filterfunc filter,
dependencyfunc provides,
dependencyfunc deps,
void* userptr)
{
char buffer[BUFFERSIZE+1]; /* make space for trailing \0 */
FD_t fdi;
Header h;
char * rpmio_flags = NULL;
rpmRC rc;
FD_t gzdi;
struct archive *cpio;
struct archive_entry *cpio_entry;
struct cpio_mydata cpio_mydata;
rpmts ts;
rpmVSFlags vsflags;
const char *compr;
if (strcmp(source, "-") == 0)
fdi = fdDup(STDIN_FILENO);
else
fdi = Fopen(source, "r.ufdio");
if (Ferror(fdi)) {
const char *srcname = (strcmp(source, "-") == 0) ? "<stdin>" : source;
logMessage(ERROR, "%s: %s\n", srcname, Fstrerror(fdi));
return EXIT_FAILURE;
}
rpmReadConfigFiles(NULL, NULL);
/* Initialize RPM transaction */
ts = rpmtsCreate();
vsflags = 0;
/* Do not check digests, signatures or headers */
vsflags |= _RPMVSF_NODIGESTS;
vsflags |= _RPMVSF_NOSIGNATURES;
vsflags |= RPMVSF_NOHDRCHK;
(void) rpmtsSetVSFlags(ts, vsflags);
rc = rpmReadPackageFile(ts, fdi, "rpm2dir", &h);
ts = rpmtsFree(ts);
switch (rc) {
case RPMRC_OK:
case RPMRC_NOKEY:
case RPMRC_NOTTRUSTED:
break;
case RPMRC_NOTFOUND:
logMessage(ERROR, "%s is not an RPM package", source);
return EXIT_FAILURE;
break;
case RPMRC_FAIL:
default:
logMessage(ERROR, "error reading header from %s package\n", source);
return EXIT_FAILURE;
break;
}
/* Retrieve all dependencies and run them through deps function */
while (deps) {
struct rpmtd_s tddep;
struct rpmtd_s tdver;
const char *depname;
const char *depversion;
if (!headerGet(h, RPMTAG_PROVIDES, &tddep, HEADERGET_MINMEM))
break;
if (!headerGet(h, RPMTAG_PROVIDEVERSION, &tdver, HEADERGET_MINMEM)){
rpmtdFreeData(&tddep);
break;
}
/* iterator */
while ((depname = rpmtdNextString(&tddep))) {
depversion = rpmtdNextString(&tdver);
if (deps(depname, depversion, userptr)) {
rpmtdFreeData(&tddep);
rpmtdFreeData(&tdver);
Fclose(fdi);
return EXIT_BADDEPS;
}
}
rpmtdFreeData(&tddep);
rpmtdFreeData(&tdver);
break;
}
/* Retrieve all provides and run them through provides function */
while (provides) {
struct rpmtd_s tddep;
struct rpmtd_s tdver;
const char *depname;
const char *depversion;
int found = 0;
if (!headerGet(h, RPMTAG_PROVIDES, &tddep, HEADERGET_MINMEM))
break;
if (!headerGet(h, RPMTAG_PROVIDEVERSION, &tdver, HEADERGET_MINMEM)){
rpmtdFreeData(&tddep);
break;
}
/* iterator */
while ((depname = rpmtdNextString(&tddep))) {
depversion = rpmtdNextString(&tdver);
if (!provides(depname, depversion, userptr)) {
found++;
}
}
rpmtdFreeData(&tddep);
rpmtdFreeData(&tdver);
if (found<=0){
Fclose(fdi);
return EXIT_BADDEPS;
}
break;
}
/* Retrieve type of payload compression. */
compr = headerGetString(h, RPMTAG_PAYLOADCOMPRESSOR);
if (compr && strcmp(compr, "gzip")) {
checked_asprintf(&rpmio_flags, "r.%sdio", compr);
}
else {
checked_asprintf(&rpmio_flags, "r.gzdio");
}
/* Open uncompressed cpio stream */
gzdi = Fdopen(fdi, rpmio_flags);
free(rpmio_flags);
if (gzdi == NULL) {
logMessage(ERROR, "cannot re-open payload: %s", Fstrerror(gzdi));
return EXIT_FAILURE;
}
/* initialize cpio decompressor */
cpio = archive_read_new();
if (cpio==NULL) {
Fclose(gzdi);
return -1;
}
cpio_mydata.gzdi = gzdi;
cpio_mydata.buffer = buffer;
archive_read_support_compression_all(cpio);
archive_read_support_format_all(cpio);
rc = archive_read_open(cpio, &cpio_mydata, NULL, rpm_myread, rpm_myclose);
/* check the status of archive_open */
if (rc != ARCHIVE_OK){
Fclose(gzdi);
return -1;
}
/* read all files in cpio archive */
while ((rc = archive_read_next_header(cpio, &cpio_entry)) == ARCHIVE_OK){
const struct stat *fstat;
int64_t fsize;
const char* filename;
int needskip = 1; /* do we need to read the data to get to the next header? */
int offset = 0;
int towrite = 0;
filename = archive_entry_pathname(cpio_entry);
fstat = archive_entry_stat(cpio_entry);
fsize = archive_entry_size(cpio_entry);
/* Strip leading slashes */
while (filename[offset] == '/')
offset+=1;
/* Strip leading ./ */
while (filename[offset] == '.' && filename[offset+1] == '/')
offset+=2;
/* Other file type - we do not care except special cases */
if (!S_ISREG(fstat->st_mode))
towrite = 1;
else
towrite = 2;
if (filter && filter(filename+offset, fstat, userptr)) {
/* filter this file */
towrite = 0;
}
/* Create directories */
char* dirname = strdup(filename+offset);
/* If the dup fails, let's hope the dirs already exist */
if (dirname){
char* dirptr = dirname;
while (dirptr && *dirptr) {
dirptr = strchr(dirptr, '/');
if (dirptr) {
*dirptr = 0;
mkdir(dirname, 0700);
*dirptr = '/';
dirptr++;
}
}
free(dirname);
}
/* Regular file */
if (towrite>=2) {
FILE *fdout = fopen(filename+offset, "w");
if (fdout==NULL){
rc = 33;
break;
}
rc = archive_read_data_into_fd(cpio, fileno(fdout));
if (rc!=ARCHIVE_OK) {
/* XXX We didn't get the file.. well.. */
needskip = 0;
} else {
needskip = 0;
}
fclose(fdout);
}
/* symlink, we assume that the path contained in symlink
* is shorter than BUFFERSIZE */
while (towrite && S_ISLNK(fstat->st_mode)) {
char symlinkbuffer[BUFFERSIZE-1];
needskip = 0;
if ((rc = archive_read_data(cpio, symlinkbuffer, fsize))!=ARCHIVE_OK) {
/* XXX We didn't get the file.. well.. */
break;
}
if (symlink(buffer, filename+offset)) {
logMessage(ERROR, "Failed to create symlink %s -> %s", filename+offset, buffer);
}
break;
}
if(needskip)
archive_read_data_skip(cpio);
}
rc = archive_read_finish(cpio); /* Also closes the RPM stream using callback */
return rc != ARCHIVE_OK;
}
|