sed: a communal variable managed to slip past 'size'
[oweals/busybox.git] / archival / rpm.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini rpm applet for busybox
4  *
5  * Copyright (C) 2001,2002 by Laurence Anderson
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include "busybox.h"
11 #include "unarchive.h"
12
13 #define RPM_HEADER_MAGIC        "\216\255\350"
14 #define RPM_CHAR_TYPE           1
15 #define RPM_INT8_TYPE           2
16 #define RPM_INT16_TYPE          3
17 #define RPM_INT32_TYPE          4
18 /* #define RPM_INT64_TYPE       5   ---- These aren't supported (yet) */
19 #define RPM_STRING_TYPE         6
20 #define RPM_BIN_TYPE            7
21 #define RPM_STRING_ARRAY_TYPE   8
22 #define RPM_I18NSTRING_TYPE     9
23
24 #define TAG_NAME                1000
25 #define TAG_VERSION             1001
26 #define TAG_RELEASE             1002
27 #define TAG_SUMMARY             1004
28 #define TAG_DESCRIPTION         1005
29 #define TAG_BUILDTIME           1006
30 #define TAG_BUILDHOST           1007
31 #define TAG_SIZE                1009
32 #define TAG_VENDOR              1011
33 #define TAG_LICENSE             1014
34 #define TAG_PACKAGER            1015
35 #define TAG_GROUP               1016
36 #define TAG_URL                 1020
37 #define TAG_PREIN               1023
38 #define TAG_POSTIN              1024
39 #define TAG_FILEFLAGS           1037
40 #define TAG_FILEUSERNAME        1039
41 #define TAG_FILEGROUPNAME       1040
42 #define TAG_SOURCERPM           1044
43 #define TAG_PREINPROG           1085
44 #define TAG_POSTINPROG          1086
45 #define TAG_PREFIXS             1098
46 #define TAG_DIRINDEXES          1116
47 #define TAG_BASENAMES           1117
48 #define TAG_DIRNAMES            1118
49 #define RPMFILE_CONFIG          (1 << 0)
50 #define RPMFILE_DOC             (1 << 1)
51
52 enum rpm_functions_e {
53         rpm_query = 1,
54         rpm_install = 2,
55         rpm_query_info = 4,
56         rpm_query_package = 8,
57         rpm_query_list = 16,
58         rpm_query_list_doc = 32,
59         rpm_query_list_config = 64
60 };
61
62 typedef struct {
63         uint32_t tag; /* 4 byte tag */
64         uint32_t type; /* 4 byte type */
65         uint32_t offset; /* 4 byte offset */
66         uint32_t count; /* 4 byte count */
67 } rpm_index;
68
69 static void *map;
70 static rpm_index **mytags;
71 static int tagcount;
72
73 static void extract_cpio_gz(int fd);
74 static rpm_index **rpm_gettags(int fd, int *num_tags);
75 static int bsearch_rpmtag(const void *key, const void *item);
76 static char *rpm_getstr(int tag, int itemindex);
77 static int rpm_getint(int tag, int itemindex);
78 static int rpm_getcount(int tag);
79 static void fileaction_dobackup(char *filename, int fileref);
80 static void fileaction_setowngrp(char *filename, int fileref);
81 static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref));
82
83 int rpm_main(int argc, char **argv);
84 int rpm_main(int argc, char **argv)
85 {
86         int opt = 0, func = 0, rpm_fd, offset;
87         const int pagesize = getpagesize();
88
89         while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
90                 switch (opt) {
91                 case 'i': /* First arg: Install mode, with q: Information */
92                         if (!func) func = rpm_install;
93                         else func |= rpm_query_info;
94                         break;
95                 case 'q': /* First arg: Query mode */
96                         if (func) bb_show_usage();
97                         func = rpm_query;
98                         break;
99                 case 'p': /* Query a package */
100                         func |= rpm_query_package;
101                         break;
102                 case 'l': /* List files in a package */
103                         func |= rpm_query_list;
104                         break;
105                 case 'd': /* List doc files in a package (implies list) */
106                         func |= rpm_query_list;
107                         func |= rpm_query_list_doc;
108                         break;
109                 case 'c': /* List config files in a package (implies list) */
110                         func |= rpm_query_list;
111                         func |= rpm_query_list_config;
112                         break;
113                 default:
114                         bb_show_usage();
115                 }
116         }
117         argv += optind;
118         argc -= optind;
119         if (!argc) bb_show_usage();
120
121         while (*argv) {
122                 rpm_fd = xopen(*argv++, O_RDONLY);
123                 mytags = rpm_gettags(rpm_fd, &tagcount);
124                 if (!mytags)
125                         bb_error_msg_and_die("error reading rpm header");
126                 offset = xlseek(rpm_fd, 0, SEEK_CUR);
127                 /* Mimimum is one page */
128                 map = mmap(0, offset > pagesize ? (offset + offset % pagesize) : pagesize, PROT_READ, MAP_PRIVATE, rpm_fd, 0);
129
130                 if (func & rpm_install) {
131                         /* Backup any config files */
132                         loop_through_files(TAG_BASENAMES, fileaction_dobackup);
133                         /* Extact the archive */
134                         extract_cpio_gz(rpm_fd);
135                         /* Set the correct file uid/gid's */
136                         loop_through_files(TAG_BASENAMES, fileaction_setowngrp);
137                 }
138                 else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) {
139                         if (!(func & (rpm_query_info|rpm_query_list))) {
140                                 /* If just a straight query, just give package name */
141                                 printf("%s-%s-%s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_RELEASE, 0));
142                         }
143                         if (func & rpm_query_info) {
144                                 /* Do the nice printout */
145                                 time_t bdate_time;
146                                 struct tm *bdate;
147                                 char bdatestring[50];
148                                 printf("Name        : %-29sRelocations: %s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_PREFIXS, 0) ? rpm_getstr(TAG_PREFIXS, 0) : "(not relocateable)");
149                                 printf("Version     : %-34sVendor: %s\n", rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_VENDOR, 0) ? rpm_getstr(TAG_VENDOR, 0) : "(none)");
150                                 bdate_time = rpm_getint(TAG_BUILDTIME, 0);
151                                 bdate = localtime((time_t *) &bdate_time);
152                                 strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate);
153                                 printf("Release     : %-30sBuild Date: %s\n", rpm_getstr(TAG_RELEASE, 0), bdatestring);
154                                 printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstr(TAG_BUILDHOST, 0));
155                                 printf("Group       : %-30sSource RPM: %s\n", rpm_getstr(TAG_GROUP, 0), rpm_getstr(TAG_SOURCERPM, 0));
156                                 printf("Size        : %-33dLicense: %s\n", rpm_getint(TAG_SIZE, 0), rpm_getstr(TAG_LICENSE, 0));
157                                 printf("URL         : %s\n", rpm_getstr(TAG_URL, 0));
158                                 printf("Summary     : %s\n", rpm_getstr(TAG_SUMMARY, 0));
159                                 printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0));
160                         }
161                         if (func & rpm_query_list) {
162                                 int count, it, flags;
163                                 count = rpm_getcount(TAG_BASENAMES);
164                                 for (it = 0; it < count; it++) {
165                                         flags = rpm_getint(TAG_FILEFLAGS, it);
166                                         switch (func & (rpm_query_list_doc|rpm_query_list_config)) {
167                                         case rpm_query_list_doc:
168                                                 if (!(flags & RPMFILE_DOC)) continue;
169                                                 break;
170                                         case rpm_query_list_config:
171                                                 if (!(flags & RPMFILE_CONFIG)) continue;
172                                                 break;
173                                         case rpm_query_list_doc|rpm_query_list_config:
174                                                 if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue;
175                                                 break;
176                                         }
177                                         printf("%s%s\n",
178                                                 rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)),
179                                                 rpm_getstr(TAG_BASENAMES, it));
180                                 }
181                         }
182                 }
183                 free(mytags);
184         }
185         return 0;
186 }
187
188 static void extract_cpio_gz(int fd) {
189         archive_handle_t *archive_handle;
190         unsigned char magic[2];
191
192         /* Initialise */
193         archive_handle = init_handle();
194         archive_handle->seek = seek_by_read;
195         //archive_handle->action_header = header_list;
196         archive_handle->action_data = data_extract_all;
197         archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
198         archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
199         archive_handle->src_fd = fd;
200         archive_handle->offset = 0;
201
202         xread(archive_handle->src_fd, &magic, 2);
203         if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
204                 bb_error_msg_and_die("invalid gzip magic");
205         }
206         check_header_gzip_or_die(archive_handle->src_fd);
207         xchdir("/"); /* Install RPM's to root */
208
209         archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip);
210         archive_handle->offset = 0;
211         while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
212                 /* loop */;
213 }
214
215
216 static rpm_index **rpm_gettags(int fd, int *num_tags)
217 {
218         /* We should never need mode than 200, and realloc later */
219         rpm_index **tags = xzalloc(200 * sizeof(struct rpmtag *));
220         int pass, tagindex = 0;
221
222         xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
223
224         /* 1st pass is the signature headers, 2nd is the main stuff */
225         for (pass = 0; pass < 2; pass++) {
226                 struct {
227                         char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
228                         uint8_t version; /* 1 byte version number */
229                         uint32_t reserved; /* 4 bytes reserved */
230                         uint32_t entries; /* Number of entries in header (4 bytes) */
231                         uint32_t size; /* Size of store (4 bytes) */
232                 } header;
233                 rpm_index *tmpindex;
234                 int storepos;
235
236                 xread(fd, &header, sizeof(header));
237                 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0)
238                         return NULL; /* Invalid magic */
239                 if (header.version != 1)
240                         return NULL; /* This program only supports v1 headers */
241                 header.size = ntohl(header.size);
242                 header.entries = ntohl(header.entries);
243                 storepos = xlseek(fd,0,SEEK_CUR) + header.entries * 16;
244
245                 while (header.entries--) {
246                         tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index));
247                         xread(fd, tmpindex, sizeof(rpm_index));
248                         tmpindex->tag = ntohl(tmpindex->tag);
249                         tmpindex->type = ntohl(tmpindex->type);
250                         tmpindex->count = ntohl(tmpindex->count);
251                         tmpindex->offset = storepos + ntohl(tmpindex->offset);
252                         if (pass==0)
253                                 tmpindex->tag -= 743;
254                 }
255                 xlseek(fd, header.size, SEEK_CUR); /* Seek past store */
256                 /* Skip padding to 8 byte boundary after reading signature headers */
257                 if (pass==0)
258                         xlseek(fd, (8 - (xlseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR);
259         }
260         tags = xrealloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */
261         *num_tags = tagindex;
262         return tags; /* All done, leave the file at the start of the gzipped cpio archive */
263 }
264
265 static int bsearch_rpmtag(const void *key, const void *item)
266 {
267         int *tag = (int *)key;
268         rpm_index **tmp = (rpm_index **) item;
269         return (*tag - tmp[0]->tag);
270 }
271
272 static int rpm_getcount(int tag)
273 {
274         rpm_index **found;
275         found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
276         if (!found)
277                 return 0;
278         return found[0]->count;
279 }
280
281 static char *rpm_getstr(int tag, int itemindex)
282 {
283         rpm_index **found;
284         found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
285         if (!found || itemindex >= found[0]->count)
286                 return NULL;
287         if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) {
288                 int n;
289                 char *tmpstr = (char *) (map + found[0]->offset);
290                 for (n=0; n < itemindex; n++)
291                         tmpstr = tmpstr + strlen(tmpstr) + 1;
292                 return tmpstr;
293         }
294         return NULL;
295 }
296
297 static int rpm_getint(int tag, int itemindex)
298 {
299         rpm_index **found;
300         int *tmpint; /* NB: using int8_t* would be easier to code */
301
302         /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
303          * it's ok to ignore it because tag won't be used as a pointer */
304         found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
305         if (!found || itemindex >= found[0]->count)
306                 return -1;
307
308         tmpint = (int *) (map + found[0]->offset);
309
310         if (found[0]->type == RPM_INT32_TYPE) {
311                 tmpint = (int *) ((char *) tmpint + itemindex*4);
312                 /*return ntohl(*tmpint);*/
313                 /* int can be != int32_t */
314                 return ntohl(*(int32_t*)tmpint);
315         }
316         if (found[0]->type == RPM_INT16_TYPE) {
317                 tmpint = (int *) ((char *) tmpint + itemindex*2);
318                 /* ??? read int, and THEN ntohs() it?? */
319                 /*return ntohs(*tmpint);*/
320                 return ntohs(*(int16_t*)tmpint);
321         }
322         if (found[0]->type == RPM_INT8_TYPE) {
323                 tmpint = (int *) ((char *) tmpint + itemindex);
324                 /* ??? why we don't read byte here??? */
325                 /*return ntohs(*tmpint);*/
326                 return *(int8_t*)tmpint;
327         }
328         return -1;
329 }
330
331 static void fileaction_dobackup(char *filename, int fileref)
332 {
333         struct stat oldfile;
334         int stat_res;
335         char *newname;
336         if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) {
337                 /* Only need to backup config files */
338                 stat_res = lstat(filename, &oldfile);
339                 if (stat_res == 0 && S_ISREG(oldfile.st_mode)) {
340                         /* File already exists  - really should check MD5's etc to see if different */
341                         newname = xasprintf("%s.rpmorig", filename);
342                         copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
343                         remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
344                         free(newname);
345                 }
346         }
347 }
348
349 static void fileaction_setowngrp(char *filename, int fileref)
350 {
351         int uid, gid;
352         uid = xuname2uid(rpm_getstr(TAG_FILEUSERNAME, fileref));
353         gid = xgroup2gid(rpm_getstr(TAG_FILEGROUPNAME, fileref));
354         chown(filename, uid, gid);
355 }
356
357 static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
358 {
359         int count = 0;
360         while (rpm_getstr(filetag, count)) {
361                 char* filename = xasprintf("%s%s",
362                         rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)),
363                         rpm_getstr(TAG_BASENAMES, count));
364                 fileaction(filename, count++);
365                 free(filename);
366         }
367 }