blob: a80f799a64210f761bf93685ca116fda7b9ce6ce [file] [log] [blame]
Glenn L McGrathb72a7352002-12-10 00:17:22 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini rpm applet for busybox
4 *
5 * Copyright (C) 2001,2002 by Laurence Anderson
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Glenn L McGrathb72a7352002-12-10 00:17:22 +000022#include <stdio.h>
23#include <unistd.h>
24#include <signal.h>
25#include <stdlib.h>
26#include <fcntl.h>
27#include <netinet/in.h> /* For ntohl & htonl function */
28#include <string.h> /* For strncmp */
29#include <sys/mman.h> /* For mmap */
30#include <time.h> /* For ctime */
31
Glenn L McGrath38386d72002-12-10 02:09:12 +000032#include "busybox.h"
33#include "unarchive.h"
34
Glenn L McGrathb72a7352002-12-10 00:17:22 +000035#define RPM_HEADER_MAGIC "\216\255\350"
36#define RPM_CHAR_TYPE 1
37#define RPM_INT8_TYPE 2
38#define RPM_INT16_TYPE 3
39#define RPM_INT32_TYPE 4
40/* #define RPM_INT64_TYPE 5 ---- These aren't supported (yet) */
41#define RPM_STRING_TYPE 6
42#define RPM_BIN_TYPE 7
43#define RPM_STRING_ARRAY_TYPE 8
44#define RPM_I18NSTRING_TYPE 9
45
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000046#define RPMTAG_NAME 1000
Glenn L McGrathb72a7352002-12-10 00:17:22 +000047#define RPMTAG_VERSION 1001
48#define RPMTAG_RELEASE 1002
49#define RPMTAG_SUMMARY 1004
50#define RPMTAG_DESCRIPTION 1005
51#define RPMTAG_BUILDTIME 1006
52#define RPMTAG_BUILDHOST 1007
53#define RPMTAG_SIZE 1009
54#define RPMTAG_VENDOR 1011
55#define RPMTAG_LICENSE 1014
56#define RPMTAG_PACKAGER 1015
57#define RPMTAG_GROUP 1016
58#define RPMTAG_URL 1020
59#define RPMTAG_PREIN 1023
60#define RPMTAG_POSTIN 1024
61#define RPMTAG_FILEFLAGS 1037
62#define RPMTAG_FILEUSERNAME 1039
63#define RPMTAG_FILEGROUPNAME 1040
64#define RPMTAG_SOURCERPM 1044
65#define RPMTAG_PREINPROG 1085
66#define RPMTAG_POSTINPROG 1086
67#define RPMTAG_PREFIXS 1098
68#define RPMTAG_DIRINDEXES 1116
69#define RPMTAG_BASENAMES 1117
70#define RPMTAG_DIRNAMES 1118
71#define RPMFILE_CONFIG (1 << 0)
72#define RPMFILE_DOC (1 << 1)
73
74enum rpm_functions_e {
75 rpm_query = 1,
76 rpm_install = 2,
77 rpm_query_info = 4,
78 rpm_query_package = 8,
79 rpm_query_list = 16,
80 rpm_query_list_doc = 32,
81 rpm_query_list_config = 64
82};
83
84typedef struct {
Eric Andersendfcb5b02004-01-30 22:54:20 +000085 uint32_t tag; /* 4 byte tag */
86 uint32_t type; /* 4 byte type */
87 uint32_t offset; /* 4 byte offset */
88 uint32_t count; /* 4 byte count */
Glenn L McGrathb72a7352002-12-10 00:17:22 +000089} rpm_index;
90
91static void *map;
92static rpm_index **mytags;
93static int tagcount;
94
95void extract_cpio_gz(int fd);
96rpm_index **rpm_gettags(int fd, int *num_tags);
97int bsearch_rpmtag(const void *key, const void *item);
98char *rpm_getstring(int tag, int itemindex);
99int rpm_getint(int tag, int itemindex);
100int rpm_getcount(int tag);
101void exec_script(int progtag, int datatag, char *prefix);
102void fileaction_dobackup(char *filename, int fileref);
103void fileaction_setowngrp(char *filename, int fileref);
104void fileaction_list(char *filename, int itemno);
105void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref));
106
107int rpm_main(int argc, char **argv)
108{
109 int opt = 0, func = 0, rpm_fd, offset;
110
111 while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
112 switch (opt) {
113 case 'i': // First arg: Install mode, with q: Information
114 if (!func) func |= rpm_install;
115 else func |= rpm_query_info;
116 break;
117 case 'q': // First arg: Query mode
118 if (!func) func |= rpm_query;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 else bb_show_usage();
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000120 break;
121 case 'p': // Query a package
122 func |= rpm_query_package;
123 break;
124 case 'l': // List files in a package
125 func |= rpm_query_list;
126 break;
127 case 'd': // List doc files in a package (implies list)
128 func |= rpm_query_list;
129 func |= rpm_query_list_doc;
130 break;
131 case 'c': // List config files in a package (implies list)
132 func |= rpm_query_list;
133 func |= rpm_query_list_config;
134 break;
135 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 bb_show_usage();
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000137 }
138 }
139
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 if (optind == argc) bb_show_usage();
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000141 while (optind < argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142 rpm_fd = bb_xopen(argv[optind], O_RDONLY);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000143 mytags = rpm_gettags(rpm_fd, (int *) &tagcount);
144 offset = lseek(rpm_fd, 0, SEEK_CUR);
145 if (!mytags) { printf("Error reading rpm header\n"); exit(-1); }
Mike Frysinger5990efb2006-01-04 07:31:19 +0000146 map = mmap(0, offset > getpagesize() ? (offset + offset % getpagesize()) : getpagesize(), PROT_READ, MAP_PRIVATE, rpm_fd, 0); // Mimimum is one page
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000147 if (func & rpm_install) {
148 loop_through_files(RPMTAG_BASENAMES, fileaction_dobackup); /* Backup any config files */
149 extract_cpio_gz(rpm_fd); // Extact the archive
150 loop_through_files(RPMTAG_BASENAMES, fileaction_setowngrp); /* Set the correct file uid/gid's */
151 } else if (func & rpm_query && func & rpm_query_package) {
152 if (!((func & rpm_query_info) || (func & rpm_query_list))) { // If just a straight query, just give package name
153 printf("%s-%s-%s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_RELEASE, 0));
154 }
155 if (func & rpm_query_info) {
156 /* Do the nice printout */
157 time_t bdate_time;
158 struct tm *bdate;
159 char bdatestring[50];
160 printf("Name : %-29sRelocations: %s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_PREFIXS, 0) ? rpm_getstring(RPMTAG_PREFIXS, 0) : "(not relocateable)");
161 printf("Version : %-34sVendor: %s\n", rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_VENDOR, 0) ? rpm_getstring(RPMTAG_VENDOR, 0) : "(none)");
162 bdate_time = rpm_getint(RPMTAG_BUILDTIME, 0);
163 bdate = localtime((time_t *) &bdate_time);
164 strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate);
165 printf("Release : %-30sBuild Date: %s\n", rpm_getstring(RPMTAG_RELEASE, 0), bdatestring);
166 printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstring(RPMTAG_BUILDHOST, 0));
167 printf("Group : %-30sSource RPM: %s\n", rpm_getstring(RPMTAG_GROUP, 0), rpm_getstring(RPMTAG_SOURCERPM, 0));
168 printf("Size : %-33dLicense: %s\n", rpm_getint(RPMTAG_SIZE, 0), rpm_getstring(RPMTAG_LICENSE, 0));
169 printf("URL : %s\n", rpm_getstring(RPMTAG_URL, 0));
170 printf("Summary : %s\n", rpm_getstring(RPMTAG_SUMMARY, 0));
171 printf("Description :\n%s\n", rpm_getstring(RPMTAG_DESCRIPTION, 0));
172 }
173 if (func & rpm_query_list) {
174 int count, it, flags;
175 count = rpm_getcount(RPMTAG_BASENAMES);
176 for (it = 0; it < count; it++) {
177 flags = rpm_getint(RPMTAG_FILEFLAGS, it);
178 switch ((func & rpm_query_list_doc) + (func & rpm_query_list_config))
179 {
180 case rpm_query_list_doc: if (!(flags & RPMFILE_DOC)) continue; break;
181 case rpm_query_list_config: if (!(flags & RPMFILE_CONFIG)) continue; break;
182 case rpm_query_list_doc + rpm_query_list_config: if (!((flags & RPMFILE_CONFIG) || (flags & RPMFILE_DOC))) continue; break;
183 }
184 printf("%s%s\n", rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, it)), rpm_getstring(RPMTAG_BASENAMES, it));
185 }
186 }
187 }
188 optind++;
189 free (mytags);
190 }
191 return 0;
192}
193
194void extract_cpio_gz(int fd) {
195 archive_handle_t *archive_handle;
196 unsigned char magic[2];
197
198 /* Initialise */
199 archive_handle = init_handle();
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000200 archive_handle->seek = seek_by_char;
201 //archive_handle->action_header = header_list;
202 archive_handle->action_data = data_extract_all;
203 archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
204 archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
205 archive_handle->src_fd = fd;
206 archive_handle->offset = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000207
Manuel Novoa III cad53642003-03-19 09:13:01 +0000208 bb_xread_all(archive_handle->src_fd, &magic, 2);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000209 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000210 bb_error_msg_and_die("Invalid gzip magic");
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000211 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000212 check_header_gzip(archive_handle->src_fd);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000213 chdir("/"); // Install RPM's to root
214
Glenn L McGrath5699b852003-11-15 23:19:05 +0000215 archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip);
216 archive_handle->offset = 0;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000217 while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000218}
219
220
221rpm_index **rpm_gettags(int fd, int *num_tags)
222{
223 rpm_index **tags = calloc(200, sizeof(struct rpmtag *)); /* We should never need mode than 200, and realloc later */
224 int pass, tagindex = 0;
225 lseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
226
227 for (pass = 0; pass < 2; pass++) { /* 1st pass is the signature headers, 2nd is the main stuff */
228 struct {
229 char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
Eric Andersendfcb5b02004-01-30 22:54:20 +0000230 uint8_t version; /* 1 byte version number */
231 uint32_t reserved; /* 4 bytes reserved */
232 uint32_t entries; /* Number of entries in header (4 bytes) */
233 uint32_t size; /* Size of store (4 bytes) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000234 } header;
235 rpm_index *tmpindex;
236 int storepos;
237
238 read(fd, &header, sizeof(header));
239 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) return NULL; /* Invalid magic */
240 if (header.version != 1) return NULL; /* This program only supports v1 headers */
241 header.size = ntohl(header.size);
242 header.entries = ntohl(header.entries);
243 storepos = lseek(fd,0,SEEK_CUR) + header.entries * 16;
244
245 while (header.entries--) {
Mike Frysinger1eef0c42005-08-16 05:32:42 +0000246 tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000247 read(fd, tmpindex, sizeof(rpm_index));
248 tmpindex->tag = ntohl(tmpindex->tag); tmpindex->type = ntohl(tmpindex->type); tmpindex->count = ntohl(tmpindex->count);
249 tmpindex->offset = storepos + ntohl(tmpindex->offset);
250 if (pass==0) tmpindex->tag -= 743;
251 }
252 lseek(fd, header.size, SEEK_CUR); /* Seek past store */
253 if (pass==0) lseek(fd, (8 - (lseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR); /* Skip padding to 8 byte boundary after reading signature headers */
254 }
255 tags = realloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */
256 *num_tags = tagindex;
257 return tags; /* All done, leave the file at the start of the gzipped cpio archive */
258}
259
260int bsearch_rpmtag(const void *key, const void *item)
261{
262 rpm_index **tmp = (rpm_index **) item;
Mike Frysinger19d70212005-04-23 01:43:07 +0000263 /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
264 * it's ok to ignore it because this isn't a 'real' pointer */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000265 return ((int) key - tmp[0]->tag);
266}
267
268int rpm_getcount(int tag)
269{
270 rpm_index **found;
Mike Frysinger19d70212005-04-23 01:43:07 +0000271 /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
272 * it's ok to ignore it because tag won't be used as a pointer */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000273 found = bsearch((void *) tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
274 if (!found) return 0;
275 else return found[0]->count;
276}
277
278char *rpm_getstring(int tag, int itemindex)
279{
280 rpm_index **found;
Mike Frysinger19d70212005-04-23 01:43:07 +0000281 /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
282 * it's ok to ignore it because tag won't be used as a pointer */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000283 found = bsearch((void *) tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
284 if (!found || itemindex >= found[0]->count) return NULL;
285 if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) {
286 int n;
287 char *tmpstr = (char *) (map + found[0]->offset);
288 for (n=0; n < itemindex; n++) tmpstr = tmpstr + strlen(tmpstr) + 1;
289 return tmpstr;
290 } else return NULL;
291}
292
293int rpm_getint(int tag, int itemindex)
294{
295 rpm_index **found;
296 int n, *tmpint;
Mike Frysinger19d70212005-04-23 01:43:07 +0000297 /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
298 * it's ok to ignore it because tag won't be used as a pointer */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000299 found = bsearch((void *) tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
300 if (!found || itemindex >= found[0]->count) return -1;
301 tmpint = (int *) (map + found[0]->offset);
302 if (found[0]->type == RPM_INT32_TYPE) {
303 for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 4);
304 return ntohl(*tmpint);
305 } else if (found[0]->type == RPM_INT16_TYPE) {
306 for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 2);
307 return ntohs(*tmpint);
308 } else if (found[0]->type == RPM_INT8_TYPE) {
309 for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 1);
310 return ntohs(*tmpint);
311 } else return -1;
312}
313
314void fileaction_dobackup(char *filename, int fileref)
315{
316 struct stat oldfile;
317 int stat_res;
318 char *newname;
319 if (rpm_getint(RPMTAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { /* Only need to backup config files */
320 stat_res = lstat (filename, &oldfile);
321 if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { /* File already exists - really should check MD5's etc to see if different */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000322 newname = bb_xstrdup(filename);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000323 newname = strcat(newname, ".rpmorig");
324 copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
325 remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
326 free(newname);
327 }
328 }
329}
330
331void fileaction_setowngrp(char *filename, int fileref)
332{
333 int uid, gid;
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000334 uid = bb_xgetpwnam(rpm_getstring(RPMTAG_FILEUSERNAME, fileref));
335 gid = bb_xgetgrnam(rpm_getstring(RPMTAG_FILEGROUPNAME, fileref));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000336 chown (filename, uid, gid);
337}
338
339void fileaction_list(char *filename, int fileref)
340{
341 printf("%s\n", filename);
342}
343
344void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
345{
346 int count = 0;
347 char *filename, *tmp_dirname, *tmp_basename;
348 while (rpm_getstring(filetag, count)) {
349 tmp_dirname = rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, count)); /* 1st put on the directory */
350 tmp_basename = rpm_getstring(RPMTAG_BASENAMES, count);
351 filename = xmalloc(strlen(tmp_basename) + strlen(tmp_dirname) + 1);
352 strcpy(filename, tmp_dirname); /* First the directory name */
353 strcat(filename, tmp_basename); /* then the filename */
354 fileaction(filename, count++);
355 free(filename);
356 }
357}