Remove bb_ prefixes from xfuncs.c (and a few other places), consolidate
[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 RPMTAG_NAME                             1000
25 #define RPMTAG_VERSION                  1001
26 #define RPMTAG_RELEASE                  1002
27 #define RPMTAG_SUMMARY                  1004
28 #define RPMTAG_DESCRIPTION              1005
29 #define RPMTAG_BUILDTIME                1006
30 #define RPMTAG_BUILDHOST                1007
31 #define RPMTAG_SIZE                     1009
32 #define RPMTAG_VENDOR                   1011
33 #define RPMTAG_LICENSE                  1014
34 #define RPMTAG_PACKAGER                 1015
35 #define RPMTAG_GROUP                    1016
36 #define RPMTAG_URL                      1020
37 #define RPMTAG_PREIN                    1023
38 #define RPMTAG_POSTIN                   1024
39 #define RPMTAG_FILEFLAGS                1037
40 #define RPMTAG_FILEUSERNAME             1039
41 #define RPMTAG_FILEGROUPNAME            1040
42 #define RPMTAG_SOURCERPM                1044
43 #define RPMTAG_PREINPROG                1085
44 #define RPMTAG_POSTINPROG               1086
45 #define RPMTAG_PREFIXS                  1098
46 #define RPMTAG_DIRINDEXES               1116
47 #define RPMTAG_BASENAMES                1117
48 #define RPMTAG_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 void extract_cpio_gz(int fd);
74 rpm_index **rpm_gettags(int fd, int *num_tags);
75 int bsearch_rpmtag(const void *key, const void *item);
76 char *rpm_getstring(int tag, int itemindex);
77 int rpm_getint(int tag, int itemindex);
78 int rpm_getcount(int tag);
79 void exec_script(int progtag, int datatag, char *prefix);
80 void fileaction_dobackup(char *filename, int fileref);
81 void fileaction_setowngrp(char *filename, int fileref);
82 void fileaction_list(char *filename, int itemno);
83 void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref));
84
85 int rpm_main(int argc, char **argv)
86 {
87         int opt = 0, func = 0, rpm_fd, offset;
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) func |= rpm_query;
97                         else bb_show_usage();
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
118         if (optind == argc) bb_show_usage();
119         while (optind < argc) {
120                 rpm_fd = xopen(argv[optind], O_RDONLY);
121                 mytags = rpm_gettags(rpm_fd, (int *) &tagcount);
122                 offset = lseek(rpm_fd, 0, SEEK_CUR);
123                 if (!mytags) { printf("Error reading rpm header\n"); exit(-1); }
124                 map = mmap(0, offset > getpagesize() ? (offset + offset % getpagesize()) : getpagesize(), PROT_READ, MAP_PRIVATE, rpm_fd, 0); // Mimimum is one page
125                 if (func & rpm_install) {
126                         loop_through_files(RPMTAG_BASENAMES, fileaction_dobackup); /* Backup any config files */
127                         extract_cpio_gz(rpm_fd); // Extact the archive
128                         loop_through_files(RPMTAG_BASENAMES, fileaction_setowngrp); /* Set the correct file uid/gid's */
129                 } else if (func & rpm_query && func & rpm_query_package) {
130                         if (!((func & rpm_query_info) || (func & rpm_query_list))) { // If just a straight query, just give package name
131                                 printf("%s-%s-%s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_RELEASE, 0));
132                         }
133                         if (func & rpm_query_info) {
134                                 /* Do the nice printout */
135                                 time_t bdate_time;
136                                 struct tm *bdate;
137                                 char bdatestring[50];
138                                 printf("Name        : %-29sRelocations: %s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_PREFIXS, 0) ? rpm_getstring(RPMTAG_PREFIXS, 0) : "(not relocateable)");
139                                 printf("Version     : %-34sVendor: %s\n", rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_VENDOR, 0) ? rpm_getstring(RPMTAG_VENDOR, 0) : "(none)");
140                                 bdate_time = rpm_getint(RPMTAG_BUILDTIME, 0);
141                                 bdate = localtime((time_t *) &bdate_time);
142                                 strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate);
143                                 printf("Release     : %-30sBuild Date: %s\n", rpm_getstring(RPMTAG_RELEASE, 0), bdatestring);
144                                 printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstring(RPMTAG_BUILDHOST, 0));
145                                 printf("Group       : %-30sSource RPM: %s\n", rpm_getstring(RPMTAG_GROUP, 0), rpm_getstring(RPMTAG_SOURCERPM, 0));
146                                 printf("Size        : %-33dLicense: %s\n", rpm_getint(RPMTAG_SIZE, 0), rpm_getstring(RPMTAG_LICENSE, 0));
147                                 printf("URL         : %s\n", rpm_getstring(RPMTAG_URL, 0));
148                                 printf("Summary     : %s\n", rpm_getstring(RPMTAG_SUMMARY, 0));
149                                 printf("Description :\n%s\n", rpm_getstring(RPMTAG_DESCRIPTION, 0));
150                         }
151                         if (func & rpm_query_list) {
152                                 int count, it, flags;
153                                 count = rpm_getcount(RPMTAG_BASENAMES);
154                                 for (it = 0; it < count; it++) {
155                                         flags = rpm_getint(RPMTAG_FILEFLAGS, it);
156                                         switch ((func & rpm_query_list_doc) + (func & rpm_query_list_config))
157                                         {
158                                                 case rpm_query_list_doc: if (!(flags & RPMFILE_DOC)) continue; break;
159                                                 case rpm_query_list_config: if (!(flags & RPMFILE_CONFIG)) continue; break;
160                                                 case rpm_query_list_doc + rpm_query_list_config: if (!((flags & RPMFILE_CONFIG) || (flags & RPMFILE_DOC))) continue; break;
161                                         }
162                                         printf("%s%s\n", rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, it)), rpm_getstring(RPMTAG_BASENAMES, it));
163                                 }
164                         }
165                 }
166                 optind++;
167                 free (mytags);
168         }
169         return 0;
170 }
171
172 void extract_cpio_gz(int fd) {
173         archive_handle_t *archive_handle;
174         unsigned char magic[2];
175
176         /* Initialise */
177         archive_handle = init_handle();
178         archive_handle->seek = seek_by_char;
179         //archive_handle->action_header = header_list;
180         archive_handle->action_data = data_extract_all;
181         archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
182         archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
183         archive_handle->src_fd = fd;
184         archive_handle->offset = 0;
185
186         xread(archive_handle->src_fd, &magic, 2);
187         if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
188                 bb_error_msg_and_die("Invalid gzip magic");
189         }
190         check_header_gzip(archive_handle->src_fd);
191         xchdir("/"); // Install RPM's to root
192
193         archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip);
194         archive_handle->offset = 0;
195         while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
196 }
197
198
199 rpm_index **rpm_gettags(int fd, int *num_tags)
200 {
201         rpm_index **tags = xzalloc(200 * sizeof(struct rpmtag *)); /* We should never need mode than 200, and realloc later */
202         int pass, tagindex = 0;
203         lseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
204
205         for (pass = 0; pass < 2; pass++) { /* 1st pass is the signature headers, 2nd is the main stuff */
206                 struct {
207                         char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
208                         uint8_t version; /* 1 byte version number */
209                         uint32_t reserved; /* 4 bytes reserved */
210                         uint32_t entries; /* Number of entries in header (4 bytes) */
211                         uint32_t size; /* Size of store (4 bytes) */
212                 } header;
213                 rpm_index *tmpindex;
214                 int storepos;
215
216                 read(fd, &header, sizeof(header));
217                 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) return NULL; /* Invalid magic */
218                 if (header.version != 1) return NULL; /* This program only supports v1 headers */
219                 header.size = ntohl(header.size);
220                 header.entries = ntohl(header.entries);
221                 storepos = lseek(fd,0,SEEK_CUR) + header.entries * 16;
222
223                 while (header.entries--) {
224                         tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index));
225                         read(fd, tmpindex, sizeof(rpm_index));
226                         tmpindex->tag = ntohl(tmpindex->tag); tmpindex->type = ntohl(tmpindex->type); tmpindex->count = ntohl(tmpindex->count);
227                         tmpindex->offset = storepos + ntohl(tmpindex->offset);
228                         if (pass==0) tmpindex->tag -= 743;
229                 }
230                 lseek(fd, header.size, SEEK_CUR); /* Seek past store */
231                 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 */
232         }
233         tags = realloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */
234         *num_tags = tagindex;
235         return tags; /* All done, leave the file at the start of the gzipped cpio archive */
236 }
237
238 int bsearch_rpmtag(const void *key, const void *item)
239 {
240         int *tag = (int *)key;
241         rpm_index **tmp = (rpm_index **) item;
242         return (*tag - tmp[0]->tag);
243 }
244
245 int rpm_getcount(int tag)
246 {
247         rpm_index **found;
248         found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
249         if (!found) return 0;
250         else return found[0]->count;
251 }
252
253 char *rpm_getstring(int tag, int itemindex)
254 {
255         rpm_index **found;
256         found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
257         if (!found || itemindex >= found[0]->count) return NULL;
258         if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) {
259                 int n;
260                 char *tmpstr = (char *) (map + found[0]->offset);
261                 for (n=0; n < itemindex; n++) tmpstr = tmpstr + strlen(tmpstr) + 1;
262                 return tmpstr;
263         } else return NULL;
264 }
265
266 int rpm_getint(int tag, int itemindex)
267 {
268         rpm_index **found;
269         int n, *tmpint;
270         /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
271          * it's ok to ignore it because tag won't be used as a pointer */
272         found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
273         if (!found || itemindex >= found[0]->count) return -1;
274         tmpint = (int *) (map + found[0]->offset);
275         if (found[0]->type == RPM_INT32_TYPE) {
276                 for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 4);
277                 return ntohl(*tmpint);
278         } else if (found[0]->type == RPM_INT16_TYPE) {
279                 for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 2);
280                 return ntohs(*tmpint);
281         } else if (found[0]->type == RPM_INT8_TYPE) {
282                 for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 1);
283                 return ntohs(*tmpint);
284         } else return -1;
285 }
286
287 void fileaction_dobackup(char *filename, int fileref)
288 {
289         struct stat oldfile;
290         int stat_res;
291         char *newname;
292         if (rpm_getint(RPMTAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { /* Only need to backup config files */
293                 stat_res = lstat (filename, &oldfile);
294                 if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { /* File already exists  - really should check MD5's etc to see if different */
295                         newname = xstrdup(filename);
296                         newname = strcat(newname, ".rpmorig");
297                         copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
298                         remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
299                         free(newname);
300                 }
301         }
302 }
303
304 void fileaction_setowngrp(char *filename, int fileref)
305 {
306         int uid, gid;
307         uid = bb_xgetpwnam(rpm_getstring(RPMTAG_FILEUSERNAME, fileref));
308         gid = bb_xgetgrnam(rpm_getstring(RPMTAG_FILEGROUPNAME, fileref));
309         chown (filename, uid, gid);
310 }
311
312 void fileaction_list(char *filename, int ATTRIBUTE_UNUSED fileref)
313 {
314         printf("%s\n", filename);
315 }
316
317 void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
318 {
319         int count = 0;
320         while (rpm_getstring(filetag, count)) {
321                 char * filename = xasprintf("%s%s",
322                         rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES,
323                         count)), rpm_getstring(RPMTAG_BASENAMES, count));
324                 fileaction(filename, count++);
325                 free(filename);
326         }
327 }