77679c275f288ae4d4987621951ac02288e7f58e
[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 "libbb.h"
11 #include "unarchive.h"
12 #include "rpm.h"
13
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
50 #define RPMFILE_CONFIG          (1 << 0)
51 #define RPMFILE_DOC             (1 << 1)
52
53 enum rpm_functions_e {
54         rpm_query = 1,
55         rpm_install = 2,
56         rpm_query_info = 4,
57         rpm_query_package = 8,
58         rpm_query_list = 16,
59         rpm_query_list_doc = 32,
60         rpm_query_list_config = 64
61 };
62
63 typedef struct {
64         uint32_t tag; /* 4 byte tag */
65         uint32_t type; /* 4 byte type */
66         uint32_t offset; /* 4 byte offset */
67         uint32_t count; /* 4 byte count */
68 } rpm_index;
69
70 static void *map;
71 static rpm_index **mytags;
72 static int tagcount;
73
74 static void extract_cpio_gz(int fd);
75 static rpm_index **rpm_gettags(int fd, int *num_tags);
76 static int bsearch_rpmtag(const void *key, const void *item);
77 static char *rpm_getstr(int tag, int itemindex);
78 static int rpm_getint(int tag, int itemindex);
79 static int rpm_getcount(int tag);
80 static void fileaction_dobackup(char *filename, int fileref);
81 static void fileaction_setowngrp(char *filename, int fileref);
82 static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref));
83
84 int rpm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
85 int rpm_main(int argc, char **argv)
86 {
87         int opt = 0, func = 0, rpm_fd, offset;
88         const int pagesize = getpagesize();
89
90         while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
91                 switch (opt) {
92                 case 'i': /* First arg: Install mode, with q: Information */
93                         if (!func) func = rpm_install;
94                         else func |= rpm_query_info;
95                         break;
96                 case 'q': /* First arg: Query mode */
97                         if (func) bb_show_usage();
98                         func = rpm_query;
99                         break;
100                 case 'p': /* Query a package */
101                         func |= rpm_query_package;
102                         break;
103                 case 'l': /* List files in a package */
104                         func |= rpm_query_list;
105                         break;
106                 case 'd': /* List doc files in a package (implies list) */
107                         func |= rpm_query_list;
108                         func |= rpm_query_list_doc;
109                         break;
110                 case 'c': /* List config files in a package (implies list) */
111                         func |= rpm_query_list;
112                         func |= rpm_query_list_config;
113                         break;
114                 default:
115                         bb_show_usage();
116                 }
117         }
118         argv += optind;
119         //argc -= optind;
120         if (!argv[0]) {
121                 bb_show_usage();
122         }
123
124         while (*argv) {
125                 rpm_fd = xopen(*argv++, O_RDONLY);
126                 mytags = rpm_gettags(rpm_fd, &tagcount);
127                 if (!mytags)
128                         bb_error_msg_and_die("error reading rpm header");
129                 offset = xlseek(rpm_fd, 0, SEEK_CUR);
130                 /* Mimimum is one page */
131                 map = mmap(0, offset > pagesize ? (offset + offset % pagesize) : pagesize, PROT_READ, MAP_PRIVATE, rpm_fd, 0);
132
133                 if (func & rpm_install) {
134                         /* Backup any config files */
135                         loop_through_files(TAG_BASENAMES, fileaction_dobackup);
136                         /* Extact the archive */
137                         extract_cpio_gz(rpm_fd);
138                         /* Set the correct file uid/gid's */
139                         loop_through_files(TAG_BASENAMES, fileaction_setowngrp);
140                 }
141                 else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) {
142                         if (!(func & (rpm_query_info|rpm_query_list))) {
143                                 /* If just a straight query, just give package name */
144                                 printf("%s-%s-%s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_RELEASE, 0));
145                         }
146                         if (func & rpm_query_info) {
147                                 /* Do the nice printout */
148                                 time_t bdate_time;
149                                 struct tm *bdate_ptm;
150                                 char bdatestring[50];
151                                 const char *p;
152
153                                 p = rpm_getstr(TAG_PREFIXS, 0);
154                                 if (!p) p = "(not relocateable)";
155                                 printf("Name        : %-29sRelocations: %s\n", rpm_getstr(TAG_NAME, 0), p);
156                                 p = rpm_getstr(TAG_VENDOR, 0);
157                                 if (!p) p = "(none)";
158                                 printf("Version     : %-34sVendor: %s\n", rpm_getstr(TAG_VERSION, 0), p);
159                                 bdate_time = rpm_getint(TAG_BUILDTIME, 0);
160                                 bdate_ptm = localtime(&bdate_time);
161                                 strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate_ptm);
162                                 printf("Release     : %-30sBuild Date: %s\n", rpm_getstr(TAG_RELEASE, 0), bdatestring);
163                                 printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstr(TAG_BUILDHOST, 0));
164                                 printf("Group       : %-30sSource RPM: %s\n", rpm_getstr(TAG_GROUP, 0), rpm_getstr(TAG_SOURCERPM, 0));
165                                 printf("Size        : %-33dLicense: %s\n", rpm_getint(TAG_SIZE, 0), rpm_getstr(TAG_LICENSE, 0));
166                                 printf("URL         : %s\n", rpm_getstr(TAG_URL, 0));
167                                 printf("Summary     : %s\n", rpm_getstr(TAG_SUMMARY, 0));
168                                 printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0));
169                         }
170                         if (func & rpm_query_list) {
171                                 int count, it, flags;
172                                 count = rpm_getcount(TAG_BASENAMES);
173                                 for (it = 0; it < count; it++) {
174                                         flags = rpm_getint(TAG_FILEFLAGS, it);
175                                         switch (func & (rpm_query_list_doc|rpm_query_list_config)) {
176                                         case rpm_query_list_doc:
177                                                 if (!(flags & RPMFILE_DOC)) continue;
178                                                 break;
179                                         case rpm_query_list_config:
180                                                 if (!(flags & RPMFILE_CONFIG)) continue;
181                                                 break;
182                                         case rpm_query_list_doc|rpm_query_list_config:
183                                                 if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue;
184                                                 break;
185                                         }
186                                         printf("%s%s\n",
187                                                 rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)),
188                                                 rpm_getstr(TAG_BASENAMES, it));
189                                 }
190                         }
191                 }
192                 free(mytags);
193         }
194         return 0;
195 }
196
197 static void extract_cpio_gz(int fd)
198 {
199         archive_handle_t *archive_handle;
200
201         /* Initialize */
202         archive_handle = init_handle();
203         archive_handle->seek = seek_by_read;
204         archive_handle->action_data = data_extract_all;
205 #if 0 /* For testing (rpm -i only lists the files in internal cpio): */
206         archive_handle->action_header = header_list;
207         archive_handle->action_data = data_skip;
208 #endif
209         archive_handle->ah_flags = ARCHIVE_RESTORE_DATE | ARCHIVE_CREATE_LEADING_DIRS
210                 /* compat: overwrite existing files.
211                  * try "rpm -i foo.src.rpm" few times in a row -
212                  * standard rpm will not complain.
213                  * (TODO? real rpm creates "file;1234" and then renames it) */
214                 | ARCHIVE_UNLINK_OLD;
215         archive_handle->src_fd = fd;
216         /*archive_handle->offset = 0; - init_handle() did it */
217
218         xchdir("/"); /* Install RPM's to root */
219         setup_unzip_on_fd(archive_handle->src_fd /*, fail_if_not_detected: 1*/);
220         while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
221                 continue;
222 }
223
224 static rpm_index **rpm_gettags(int fd, int *num_tags)
225 {
226         /* We should never need more than 200 (shrink via realloc later) */
227         rpm_index **tags = xzalloc(200 * sizeof(tags[0]));
228         int pass, tagindex = 0;
229
230         xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
231
232         /* 1st pass is the signature headers, 2nd is the main stuff */
233         for (pass = 0; pass < 2; pass++) {
234                 struct rpm_header header;
235                 rpm_index *tmpindex;
236                 int storepos;
237
238                 xread(fd, &header, sizeof(header));
239                 if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER))
240                         return NULL; /* Invalid magic, or not version 1 */
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(*tmpindex));
247                         xread(fd, tmpindex, sizeof(*tmpindex));
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                 storepos = 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, (-storepos) & 0x7, SEEK_CUR);
259         }
260         /* realloc tags to save space */
261         tags = xrealloc(tags, tagindex * sizeof(tags[0]));
262         *num_tags = tagindex;
263         /* All done, leave the file at the start of the gzipped cpio archive */
264         return tags;
265 }
266
267 static int bsearch_rpmtag(const void *key, const void *item)
268 {
269         int *tag = (int *)key;
270         rpm_index **tmp = (rpm_index **) item;
271         return (*tag - tmp[0]->tag);
272 }
273
274 static int rpm_getcount(int tag)
275 {
276         rpm_index **found;
277         found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
278         if (!found)
279                 return 0;
280         return found[0]->count;
281 }
282
283 static char *rpm_getstr(int tag, int itemindex)
284 {
285         rpm_index **found;
286         found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
287         if (!found || itemindex >= found[0]->count)
288                 return NULL;
289         if (found[0]->type == RPM_STRING_TYPE
290          || found[0]->type == RPM_I18NSTRING_TYPE
291          || found[0]->type == RPM_STRING_ARRAY_TYPE
292         ) {
293                 int n;
294                 char *tmpstr = (char *) map + found[0]->offset;
295                 for (n = 0; n < itemindex; n++)
296                         tmpstr = tmpstr + strlen(tmpstr) + 1;
297                 return tmpstr;
298         }
299         return NULL;
300 }
301
302 static int rpm_getint(int tag, int itemindex)
303 {
304         rpm_index **found;
305         int *tmpint; /* NB: using int8_t* would be easier to code */
306
307         /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
308          * it's ok to ignore it because tag won't be used as a pointer */
309         found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
310         if (!found || itemindex >= found[0]->count)
311                 return -1;
312
313         tmpint = (int *) ((char *) map + found[0]->offset);
314
315         if (found[0]->type == RPM_INT32_TYPE) {
316                 tmpint = (int *) ((char *) tmpint + itemindex*4);
317                 /*return ntohl(*tmpint);*/
318                 /* int can be != int32_t */
319                 return ntohl(*(int32_t*)tmpint);
320         }
321         if (found[0]->type == RPM_INT16_TYPE) {
322                 tmpint = (int *) ((char *) tmpint + itemindex*2);
323                 /* ??? read int, and THEN ntohs() it?? */
324                 /*return ntohs(*tmpint);*/
325                 return ntohs(*(int16_t*)tmpint);
326         }
327         if (found[0]->type == RPM_INT8_TYPE) {
328                 tmpint = (int *) ((char *) tmpint + itemindex);
329                 /* ??? why we don't read byte here??? */
330                 /*return ntohs(*tmpint);*/
331                 return *(int8_t*)tmpint;
332         }
333         return -1;
334 }
335
336 static void fileaction_dobackup(char *filename, int fileref)
337 {
338         struct stat oldfile;
339         int stat_res;
340         char *newname;
341         if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) {
342                 /* Only need to backup config files */
343                 stat_res = lstat(filename, &oldfile);
344                 if (stat_res == 0 && S_ISREG(oldfile.st_mode)) {
345                         /* File already exists  - really should check MD5's etc to see if different */
346                         newname = xasprintf("%s.rpmorig", filename);
347                         copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
348                         remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
349                         free(newname);
350                 }
351         }
352 }
353
354 static void fileaction_setowngrp(char *filename, int fileref)
355 {
356         /* real rpm warns: "user foo does not exist - using <you>" */
357         struct passwd *pw = getpwnam(rpm_getstr(TAG_FILEUSERNAME, fileref));
358         int uid = pw ? pw->pw_uid : getuid(); /* or euid? */
359         struct group *gr = getgrnam(rpm_getstr(TAG_FILEGROUPNAME, fileref));
360         int gid = gr ? gr->gr_gid : getgid();
361         chown(filename, uid, gid);
362 }
363
364 static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
365 {
366         int count = 0;
367         while (rpm_getstr(filetag, count)) {
368                 char* filename = xasprintf("%s%s",
369                         rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)),
370                         rpm_getstr(TAG_BASENAMES, count));
371                 fileaction(filename, count++);
372                 free(filename);
373         }
374 }