Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini rpm applet for busybox |
| 4 | * |
| 5 | * Copyright (C) 2001,2002 by Laurence Anderson |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 9 | //config:config RPM |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "rpm (33 kb)" |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: Mini RPM applet - queries and extracts RPM packages. |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 14 | |
Denys Vlasenko | 36184a4 | 2013-11-14 09:54:24 +0100 | [diff] [blame] | 15 | //applet:IF_RPM(APPLET(rpm, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 3fa9262 | 2017-08-10 09:30:38 +0200 | [diff] [blame] | 16 | |
Denys Vlasenko | 66620fa | 2013-11-14 09:53:52 +0100 | [diff] [blame] | 17 | //kbuild:lib-$(CONFIG_RPM) += rpm.o |
| 18 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 19 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 20 | #include "common_bufsiz.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 21 | #include "bb_archive.h" |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 22 | #include "rpm.h" |
Glenn L McGrath | 38386d7 | 2002-12-10 02:09:12 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 24 | #define RPM_CHAR_TYPE 1 |
| 25 | #define RPM_INT8_TYPE 2 |
| 26 | #define RPM_INT16_TYPE 3 |
| 27 | #define RPM_INT32_TYPE 4 |
| 28 | /* #define RPM_INT64_TYPE 5 ---- These aren't supported (yet) */ |
| 29 | #define RPM_STRING_TYPE 6 |
| 30 | #define RPM_BIN_TYPE 7 |
| 31 | #define RPM_STRING_ARRAY_TYPE 8 |
| 32 | #define RPM_I18NSTRING_TYPE 9 |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 34 | #define TAG_NAME 1000 |
| 35 | #define TAG_VERSION 1001 |
| 36 | #define TAG_RELEASE 1002 |
| 37 | #define TAG_SUMMARY 1004 |
| 38 | #define TAG_DESCRIPTION 1005 |
| 39 | #define TAG_BUILDTIME 1006 |
| 40 | #define TAG_BUILDHOST 1007 |
| 41 | #define TAG_SIZE 1009 |
| 42 | #define TAG_VENDOR 1011 |
| 43 | #define TAG_LICENSE 1014 |
| 44 | #define TAG_PACKAGER 1015 |
| 45 | #define TAG_GROUP 1016 |
| 46 | #define TAG_URL 1020 |
| 47 | #define TAG_PREIN 1023 |
| 48 | #define TAG_POSTIN 1024 |
| 49 | #define TAG_FILEFLAGS 1037 |
| 50 | #define TAG_FILEUSERNAME 1039 |
| 51 | #define TAG_FILEGROUPNAME 1040 |
| 52 | #define TAG_SOURCERPM 1044 |
| 53 | #define TAG_PREINPROG 1085 |
| 54 | #define TAG_POSTINPROG 1086 |
| 55 | #define TAG_PREFIXS 1098 |
| 56 | #define TAG_DIRINDEXES 1116 |
| 57 | #define TAG_BASENAMES 1117 |
| 58 | #define TAG_DIRNAMES 1118 |
Denys Vlasenko | 0cf64c8 | 2017-08-10 10:35:08 +0200 | [diff] [blame] | 59 | #define TAG_PAYLOADCOMPRESSOR 1125 |
| 60 | |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 62 | #define RPMFILE_CONFIG (1 << 0) |
| 63 | #define RPMFILE_DOC (1 << 1) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 64 | |
| 65 | enum rpm_functions_e { |
| 66 | rpm_query = 1, |
| 67 | rpm_install = 2, |
| 68 | rpm_query_info = 4, |
| 69 | rpm_query_package = 8, |
| 70 | rpm_query_list = 16, |
| 71 | rpm_query_list_doc = 32, |
| 72 | rpm_query_list_config = 64 |
| 73 | }; |
| 74 | |
| 75 | typedef struct { |
Eric Andersen | dfcb5b0 | 2004-01-30 22:54:20 +0000 | [diff] [blame] | 76 | uint32_t tag; /* 4 byte tag */ |
| 77 | uint32_t type; /* 4 byte type */ |
| 78 | uint32_t offset; /* 4 byte offset */ |
| 79 | uint32_t count; /* 4 byte count */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 80 | } rpm_index; |
| 81 | |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 82 | struct globals { |
| 83 | void *map; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 84 | rpm_index *mytags; |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 85 | int tagcount; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 86 | unsigned mapsize, pagesize; |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 87 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 88 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 89 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 90 | |
Denys Vlasenko | 5dfd9c4 | 2010-05-06 16:56:38 +0200 | [diff] [blame] | 91 | static void extract_cpio(int fd, const char *source_rpm) |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 92 | { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 93 | archive_handle_t *archive_handle; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 94 | |
Denys Vlasenko | 5dfd9c4 | 2010-05-06 16:56:38 +0200 | [diff] [blame] | 95 | if (source_rpm != NULL) { |
| 96 | /* Binary rpm (it was built from some SRPM), install to root */ |
| 97 | xchdir("/"); |
| 98 | } /* else: SRPM, install to current dir */ |
| 99 | |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 100 | /* Initialize */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 101 | archive_handle = init_handle(); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 102 | archive_handle->seek = seek_by_read; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 103 | archive_handle->action_data = data_extract_all; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 104 | #if 0 /* For testing (rpm -i only lists the files in internal cpio): */ |
| 105 | archive_handle->action_header = header_list; |
| 106 | archive_handle->action_data = data_skip; |
| 107 | #endif |
Denys Vlasenko | d57d626 | 2009-09-17 02:43:14 +0200 | [diff] [blame] | 108 | archive_handle->ah_flags = ARCHIVE_RESTORE_DATE | ARCHIVE_CREATE_LEADING_DIRS |
Denis Vlasenko | aa9eb1f | 2008-10-16 13:29:13 +0000 | [diff] [blame] | 109 | /* compat: overwrite existing files. |
| 110 | * try "rpm -i foo.src.rpm" few times in a row - |
| 111 | * standard rpm will not complain. |
Denys Vlasenko | 2aec773 | 2013-02-20 15:58:42 +0100 | [diff] [blame] | 112 | */ |
| 113 | | ARCHIVE_REPLACE_VIA_RENAME; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 114 | archive_handle->src_fd = fd; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 115 | /*archive_handle->offset = 0; - init_handle() did it */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 116 | |
Denys Vlasenko | 640ce3d | 2014-02-02 02:06:38 +0100 | [diff] [blame] | 117 | setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 1); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 118 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS) |
Denis Vlasenko | c14d39e | 2007-06-08 13:05:39 +0000 | [diff] [blame] | 119 | continue; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Denys Vlasenko | e5650a4 | 2017-08-10 09:41:57 +0200 | [diff] [blame] | 122 | static int rpm_gettags(const char *filename) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 123 | { |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 124 | rpm_index *tags; |
Denys Vlasenko | e5650a4 | 2017-08-10 09:41:57 +0200 | [diff] [blame] | 125 | int fd; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 126 | unsigned pass, idx; |
| 127 | unsigned storepos; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 128 | |
Denys Vlasenko | 3c9b8fe | 2017-08-10 09:56:49 +0200 | [diff] [blame] | 129 | if (!filename) { /* rpm2cpio w/o filename? */ |
| 130 | filename = bb_msg_standard_output; |
| 131 | fd = 0; |
| 132 | } else { |
| 133 | fd = xopen(filename, O_RDONLY); |
| 134 | } |
| 135 | |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 136 | storepos = xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */ |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 137 | G.tagcount = 0; |
| 138 | tags = NULL; |
| 139 | idx = 0; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 140 | /* 1st pass is the signature headers, 2nd is the main stuff */ |
| 141 | for (pass = 0; pass < 2; pass++) { |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 142 | struct rpm_header header; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 143 | unsigned cnt; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 144 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 145 | xread(fd, &header, sizeof(header)); |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 146 | if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) |
Denys Vlasenko | e5650a4 | 2017-08-10 09:41:57 +0200 | [diff] [blame] | 147 | bb_error_msg_and_die("invalid RPM header magic in '%s'", filename); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 148 | header.size = ntohl(header.size); |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 149 | cnt = ntohl(header.entries); |
| 150 | storepos += sizeof(header) + cnt * 16; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 151 | |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 152 | G.tagcount += cnt; |
Denys Vlasenko | e5650a4 | 2017-08-10 09:41:57 +0200 | [diff] [blame] | 153 | tags = xrealloc(tags, sizeof(tags[0]) * G.tagcount); |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 154 | xread(fd, &tags[idx], sizeof(tags[0]) * cnt); |
| 155 | while (cnt--) { |
| 156 | rpm_index *tag = &tags[idx]; |
| 157 | tag->tag = ntohl(tag->tag); |
| 158 | tag->type = ntohl(tag->type); |
| 159 | tag->count = ntohl(tag->count); |
| 160 | tag->offset = storepos + ntohl(tag->offset); |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 161 | if (pass == 0) |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 162 | tag->tag -= 743; |
| 163 | idx++; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 164 | } |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 165 | /* Skip padding to 8 byte boundary after reading signature headers */ |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 166 | if (pass == 0) |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 167 | while (header.size & 7) |
| 168 | header.size++; |
| 169 | /* Seek past store */ |
| 170 | storepos = xlseek(fd, header.size, SEEK_CUR); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 171 | } |
Denys Vlasenko | e5650a4 | 2017-08-10 09:41:57 +0200 | [diff] [blame] | 172 | G.mytags = tags; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 173 | |
| 174 | /* Map the store */ |
| 175 | storepos = (storepos + G.pagesize) & -(int)G.pagesize; |
| 176 | /* remember size for munmap */ |
| 177 | G.mapsize = storepos; |
| 178 | /* some NOMMU systems prefer MAP_PRIVATE over MAP_SHARED */ |
| 179 | G.map = mmap(0, storepos, PROT_READ, MAP_PRIVATE, fd, 0); |
| 180 | if (G.map == MAP_FAILED) |
Denys Vlasenko | e5650a4 | 2017-08-10 09:41:57 +0200 | [diff] [blame] | 181 | bb_perror_msg_and_die("mmap '%s'", filename); |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 182 | |
Denys Vlasenko | e5650a4 | 2017-08-10 09:41:57 +0200 | [diff] [blame] | 183 | return fd; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 186 | static int bsearch_rpmtag(const void *key, const void *item) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 187 | { |
Eric Andersen | 2cdd4d5 | 2006-01-30 18:33:12 +0000 | [diff] [blame] | 188 | int *tag = (int *)key; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 189 | rpm_index *tmp = (rpm_index *) item; |
| 190 | return (*tag - tmp->tag); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 193 | static int rpm_getcount(int tag) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 194 | { |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 195 | rpm_index *found; |
| 196 | found = bsearch(&tag, G.mytags, G.tagcount, sizeof(G.mytags[0]), bsearch_rpmtag); |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 197 | if (!found) |
| 198 | return 0; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 199 | return found->count; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 202 | static char *rpm_getstr(int tag, int itemindex) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 203 | { |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 204 | rpm_index *found; |
| 205 | found = bsearch(&tag, G.mytags, G.tagcount, sizeof(G.mytags[0]), bsearch_rpmtag); |
| 206 | if (!found || itemindex >= found->count) |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 207 | return NULL; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 208 | if (found->type == RPM_STRING_TYPE |
| 209 | || found->type == RPM_I18NSTRING_TYPE |
| 210 | || found->type == RPM_STRING_ARRAY_TYPE |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 211 | ) { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 212 | int n; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 213 | char *tmpstr = (char *) G.map + found->offset; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 214 | for (n = 0; n < itemindex; n++) |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 215 | tmpstr = tmpstr + strlen(tmpstr) + 1; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 216 | return tmpstr; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 217 | } |
| 218 | return NULL; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 219 | } |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 220 | static char *rpm_getstr0(int tag) |
| 221 | { |
| 222 | return rpm_getstr(tag, 0); |
| 223 | } |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 224 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 225 | static int rpm_getint(int tag, int itemindex) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 226 | { |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 227 | rpm_index *found; |
Denys Vlasenko | 10f5f9b | 2013-02-20 15:57:39 +0100 | [diff] [blame] | 228 | char *tmpint; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 229 | |
Mike Frysinger | 19d7021 | 2005-04-23 01:43:07 +0000 | [diff] [blame] | 230 | /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ... |
| 231 | * it's ok to ignore it because tag won't be used as a pointer */ |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 232 | found = bsearch(&tag, G.mytags, G.tagcount, sizeof(G.mytags[0]), bsearch_rpmtag); |
| 233 | if (!found || itemindex >= found->count) |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 234 | return -1; |
| 235 | |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 236 | tmpint = (char *) G.map + found->offset; |
| 237 | if (found->type == RPM_INT32_TYPE) { |
Denys Vlasenko | 10f5f9b | 2013-02-20 15:57:39 +0100 | [diff] [blame] | 238 | tmpint += itemindex*4; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 239 | return ntohl(*(int32_t*)tmpint); |
| 240 | } |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 241 | if (found->type == RPM_INT16_TYPE) { |
Denys Vlasenko | 10f5f9b | 2013-02-20 15:57:39 +0100 | [diff] [blame] | 242 | tmpint += itemindex*2; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 243 | return ntohs(*(int16_t*)tmpint); |
| 244 | } |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 245 | if (found->type == RPM_INT8_TYPE) { |
Denys Vlasenko | 10f5f9b | 2013-02-20 15:57:39 +0100 | [diff] [blame] | 246 | tmpint += itemindex; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 247 | return *(int8_t*)tmpint; |
| 248 | } |
| 249 | return -1; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 252 | static void fileaction_dobackup(char *filename, int fileref) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 253 | { |
| 254 | struct stat oldfile; |
| 255 | int stat_res; |
| 256 | char *newname; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 257 | if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { |
| 258 | /* Only need to backup config files */ |
| 259 | stat_res = lstat(filename, &oldfile); |
| 260 | if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { |
| 261 | /* File already exists - really should check MD5's etc to see if different */ |
Denis Vlasenko | 9cac521 | 2006-09-09 12:24:19 +0000 | [diff] [blame] | 262 | newname = xasprintf("%s.rpmorig", filename); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 263 | copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS); |
| 264 | remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE); |
| 265 | free(newname); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 270 | static void fileaction_setowngrp(char *filename, int fileref) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 271 | { |
Denis Vlasenko | aa9eb1f | 2008-10-16 13:29:13 +0000 | [diff] [blame] | 272 | /* real rpm warns: "user foo does not exist - using <you>" */ |
| 273 | struct passwd *pw = getpwnam(rpm_getstr(TAG_FILEUSERNAME, fileref)); |
| 274 | int uid = pw ? pw->pw_uid : getuid(); /* or euid? */ |
| 275 | struct group *gr = getgrnam(rpm_getstr(TAG_FILEGROUPNAME, fileref)); |
| 276 | int gid = gr ? gr->gr_gid : getgid(); |
Denis Vlasenko | cf94446 | 2006-10-03 19:02:20 +0000 | [diff] [blame] | 277 | chown(filename, uid, gid); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 280 | static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref)) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 281 | { |
| 282 | int count = 0; |
Denis Vlasenko | f4c52b3 | 2006-12-22 15:03:50 +0000 | [diff] [blame] | 283 | while (rpm_getstr(filetag, count)) { |
| 284 | char* filename = xasprintf("%s%s", |
| 285 | rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)), |
| 286 | rpm_getstr(TAG_BASENAMES, count)); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 287 | fileaction(filename, count++); |
| 288 | free(filename); |
| 289 | } |
| 290 | } |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 291 | |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 292 | #if 0 //DEBUG |
| 293 | static void print_all_tags(void) |
| 294 | { |
| 295 | unsigned i = 0; |
| 296 | while (i < G.tagcount) { |
| 297 | rpm_index *tag = &G.mytags[i]; |
| 298 | if (tag->type == RPM_STRING_TYPE |
| 299 | || tag->type == RPM_I18NSTRING_TYPE |
| 300 | || tag->type == RPM_STRING_ARRAY_TYPE |
| 301 | ) { |
| 302 | unsigned n; |
| 303 | char *str = (char *) G.map + tag->offset; |
| 304 | |
| 305 | printf("tag[%u] %08x type %08x offset %08x count %d '%s'\n", |
| 306 | i, tag->tag, tag->type, tag->offset, tag->count, str |
| 307 | ); |
| 308 | for (n = 1; n < tag->count; n++) { |
| 309 | str += strlen(str) + 1; |
| 310 | printf("\t'%s'\n", str); |
| 311 | } |
| 312 | } |
| 313 | i++; |
| 314 | } |
| 315 | } |
| 316 | #else |
| 317 | #define print_all_tags() ((void)0) |
| 318 | #endif |
| 319 | |
| 320 | //usage:#define rpm_trivial_usage |
| 321 | //usage: "-i PACKAGE.rpm; rpm -qp[ildc] PACKAGE.rpm" |
| 322 | //usage:#define rpm_full_usage "\n\n" |
| 323 | //usage: "Manipulate RPM packages\n" |
| 324 | //usage: "\nCommands:" |
| 325 | //usage: "\n -i Install package" |
| 326 | //usage: "\n -qp Query package" |
| 327 | //usage: "\n -qpi Show information" |
| 328 | //usage: "\n -qpl List contents" |
| 329 | //usage: "\n -qpd List documents" |
| 330 | //usage: "\n -qpc List config files" |
| 331 | |
| 332 | /* RPM version 4.13.0.1: |
| 333 | * Unlike -q, -i seems to imply -p: -i, -ip and -pi work the same. |
| 334 | * OTOH, with -q order is important: "-piq FILE.rpm" works as -qp, not -qpi |
| 335 | * (IOW: shows only package name, not package info). |
| 336 | * "-iq ARG" works as -q: treats ARG as package name, not a file. |
| 337 | * |
| 338 | * "man rpm" on -l option and options implying it: |
| 339 | * -l, --list List files in package. |
| 340 | * -c, --configfiles List only configuration files (implies -l). |
| 341 | * -d, --docfiles List only documentation files (implies -l). |
| 342 | * -L, --licensefiles List only license files (implies -l). |
| 343 | * --dump Dump file information as follows (implies -l): |
| 344 | * path size mtime digest mode owner group isconfig isdoc rdev symlink |
| 345 | * -s, --state Display the states of files in the package (implies -l). |
| 346 | * The state of each file is one of normal, not installed, or replaced. |
| 347 | * |
| 348 | * Looks like we can switch to getopt32 here: in practice, people |
| 349 | * do place -q first if they intend to use it (misinterpreting "-piq" wouldn't matter). |
| 350 | */ |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 351 | int rpm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 352 | int rpm_main(int argc, char **argv) |
| 353 | { |
| 354 | int opt, func = 0; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 355 | |
Denys Vlasenko | 5da5365 | 2017-08-10 13:22:01 +0200 | [diff] [blame] | 356 | INIT_G(); |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 357 | G.pagesize = getpagesize(); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 358 | |
| 359 | while ((opt = getopt(argc, argv, "iqpldc")) != -1) { |
| 360 | switch (opt) { |
| 361 | case 'i': /* First arg: Install mode, with q: Information */ |
| 362 | if (!func) func = rpm_install; |
| 363 | else func |= rpm_query_info; |
| 364 | break; |
| 365 | case 'q': /* First arg: Query mode */ |
| 366 | if (func) bb_show_usage(); |
| 367 | func = rpm_query; |
| 368 | break; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 369 | case 'p': /* Query a package (IOW: .rpm file, we are not querying RPMDB) */ |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 370 | func |= rpm_query_package; |
| 371 | break; |
| 372 | case 'l': /* List files in a package */ |
| 373 | func |= rpm_query_list; |
| 374 | break; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 375 | case 'd': /* List doc files in a package (implies -l) */ |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 376 | func |= rpm_query_list; |
| 377 | func |= rpm_query_list_doc; |
| 378 | break; |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 379 | case 'c': /* List config files in a package (implies -l) */ |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 380 | func |= rpm_query_list; |
| 381 | func |= rpm_query_list_config; |
| 382 | break; |
| 383 | default: |
| 384 | bb_show_usage(); |
| 385 | } |
| 386 | } |
| 387 | argv += optind; |
| 388 | //argc -= optind; |
| 389 | if (!argv[0]) { |
| 390 | bb_show_usage(); |
| 391 | } |
| 392 | |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 393 | for (;;) { |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 394 | int rpm_fd; |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 395 | const char *source_rpm; |
| 396 | |
Denys Vlasenko | e5650a4 | 2017-08-10 09:41:57 +0200 | [diff] [blame] | 397 | rpm_fd = rpm_gettags(*argv); |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 398 | print_all_tags(); |
| 399 | |
| 400 | source_rpm = rpm_getstr0(TAG_SOURCERPM); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 401 | |
| 402 | if (func & rpm_install) { |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 403 | /* -i (and not -qi) */ |
| 404 | |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 405 | /* Backup any config files */ |
| 406 | loop_through_files(TAG_BASENAMES, fileaction_dobackup); |
| 407 | /* Extact the archive */ |
| 408 | extract_cpio(rpm_fd, source_rpm); |
| 409 | /* Set the correct file uid/gid's */ |
| 410 | loop_through_files(TAG_BASENAMES, fileaction_setowngrp); |
| 411 | } |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 412 | else |
| 413 | if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) { |
| 414 | /* -qp */ |
| 415 | |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 416 | if (!(func & (rpm_query_info|rpm_query_list))) { |
| 417 | /* If just a straight query, just give package name */ |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 418 | printf("%s-%s-%s\n", rpm_getstr0(TAG_NAME), rpm_getstr0(TAG_VERSION), rpm_getstr0(TAG_RELEASE)); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 419 | } |
| 420 | if (func & rpm_query_info) { |
| 421 | /* Do the nice printout */ |
| 422 | time_t bdate_time; |
| 423 | struct tm *bdate_ptm; |
| 424 | char bdatestring[50]; |
| 425 | const char *p; |
| 426 | |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 427 | printf("%-12s: %s\n", "Name" , rpm_getstr0(TAG_NAME)); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 428 | /* TODO compat: add "Epoch" here */ |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 429 | printf("%-12s: %s\n", "Version" , rpm_getstr0(TAG_VERSION)); |
| 430 | printf("%-12s: %s\n", "Release" , rpm_getstr0(TAG_RELEASE)); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 431 | /* add "Architecture" */ |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 432 | /* printf("%-12s: %s\n", "Install Date", "(not installed)"); - we don't know */ |
| 433 | printf("%-12s: %s\n", "Group" , rpm_getstr0(TAG_GROUP)); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 434 | printf("%-12s: %d\n", "Size" , rpm_getint(TAG_SIZE, 0)); |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 435 | printf("%-12s: %s\n", "License" , rpm_getstr0(TAG_LICENSE)); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 436 | /* add "Signature" */ |
| 437 | printf("%-12s: %s\n", "Source RPM" , source_rpm ? source_rpm : "(none)"); |
| 438 | bdate_time = rpm_getint(TAG_BUILDTIME, 0); |
| 439 | bdate_ptm = localtime(&bdate_time); |
| 440 | strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate_ptm); |
| 441 | printf("%-12s: %s\n", "Build Date" , bdatestring); |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 442 | printf("%-12s: %s\n", "Build Host" , rpm_getstr0(TAG_BUILDHOST)); |
| 443 | p = rpm_getstr0(TAG_PREFIXS); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 444 | printf("%-12s: %s\n", "Relocations" , p ? p : "(not relocatable)"); |
| 445 | /* add "Packager" */ |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 446 | p = rpm_getstr0(TAG_VENDOR); |
| 447 | if (p) /* rpm 4.13.0.1 does not show "(none)" for Vendor: */ |
| 448 | printf("%-12s: %s\n", "Vendor" , p); |
| 449 | p = rpm_getstr0(TAG_URL); |
| 450 | if (p) /* rpm 4.13.0.1 does not show "(none)"/"(null)" for URL: */ |
| 451 | printf("%-12s: %s\n", "URL" , p); |
| 452 | printf("%-12s: %s\n", "Summary" , rpm_getstr0(TAG_SUMMARY)); |
| 453 | printf("Description :\n%s\n", rpm_getstr0(TAG_DESCRIPTION)); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 454 | } |
| 455 | if (func & rpm_query_list) { |
| 456 | int count, it, flags; |
| 457 | count = rpm_getcount(TAG_BASENAMES); |
| 458 | for (it = 0; it < count; it++) { |
| 459 | flags = rpm_getint(TAG_FILEFLAGS, it); |
| 460 | switch (func & (rpm_query_list_doc|rpm_query_list_config)) { |
| 461 | case rpm_query_list_doc: |
| 462 | if (!(flags & RPMFILE_DOC)) continue; |
| 463 | break; |
| 464 | case rpm_query_list_config: |
| 465 | if (!(flags & RPMFILE_CONFIG)) continue; |
| 466 | break; |
| 467 | case rpm_query_list_doc|rpm_query_list_config: |
| 468 | if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue; |
| 469 | break; |
| 470 | } |
| 471 | printf("%s%s\n", |
| 472 | rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)), |
| 473 | rpm_getstr(TAG_BASENAMES, it)); |
| 474 | } |
| 475 | } |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 476 | } else { |
| 477 | /* Unsupported (help text shows what we support) */ |
| 478 | bb_show_usage(); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 479 | } |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 480 | if (!*++argv) |
| 481 | break; |
| 482 | munmap(G.map, G.mapsize); |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 483 | free(G.mytags); |
| 484 | close(rpm_fd); |
| 485 | } |
Denys Vlasenko | 9be166b | 2017-08-09 18:20:01 +0200 | [diff] [blame] | 486 | |
Denys Vlasenko | d4d4f35 | 2013-02-20 16:01:10 +0100 | [diff] [blame] | 487 | return 0; |
| 488 | } |
Denys Vlasenko | 3fa9262 | 2017-08-10 09:30:38 +0200 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * Mini rpm2cpio implementation for busybox |
| 492 | * |
| 493 | * Copyright (C) 2001 by Laurence Anderson |
| 494 | * |
| 495 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 496 | */ |
| 497 | //config:config RPM2CPIO |
| 498 | //config: bool "rpm2cpio (20 kb)" |
| 499 | //config: default y |
| 500 | //config: help |
| 501 | //config: Converts a RPM file into a CPIO archive. |
| 502 | |
| 503 | //applet:IF_RPM2CPIO(APPLET(rpm2cpio, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 504 | |
| 505 | //kbuild:lib-$(CONFIG_RPM2CPIO) += rpm.o |
| 506 | |
| 507 | //usage:#define rpm2cpio_trivial_usage |
| 508 | //usage: "PACKAGE.rpm" |
| 509 | //usage:#define rpm2cpio_full_usage "\n\n" |
| 510 | //usage: "Output a cpio archive of the rpm file" |
| 511 | |
Denys Vlasenko | 3fa9262 | 2017-08-10 09:30:38 +0200 | [diff] [blame] | 512 | /* No getopt required */ |
| 513 | int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 514 | int rpm2cpio_main(int argc UNUSED_PARAM, char **argv) |
| 515 | { |
Denys Vlasenko | 0cf64c8 | 2017-08-10 10:35:08 +0200 | [diff] [blame] | 516 | const char *str; |
Denys Vlasenko | 3c9b8fe | 2017-08-10 09:56:49 +0200 | [diff] [blame] | 517 | int rpm_fd; |
Denys Vlasenko | 3fa9262 | 2017-08-10 09:30:38 +0200 | [diff] [blame] | 518 | |
Denys Vlasenko | 5da5365 | 2017-08-10 13:22:01 +0200 | [diff] [blame] | 519 | INIT_G(); |
Denys Vlasenko | 3c9b8fe | 2017-08-10 09:56:49 +0200 | [diff] [blame] | 520 | G.pagesize = getpagesize(); |
Denys Vlasenko | 3fa9262 | 2017-08-10 09:30:38 +0200 | [diff] [blame] | 521 | |
Denys Vlasenko | 3c9b8fe | 2017-08-10 09:56:49 +0200 | [diff] [blame] | 522 | rpm_fd = rpm_gettags(argv[1]); |
Denys Vlasenko | 3fa9262 | 2017-08-10 09:30:38 +0200 | [diff] [blame] | 523 | |
| 524 | //if (SEAMLESS_COMPRESSION) - we do this at the end instead. |
| 525 | // /* We need to know whether child (gzip/bzip/etc) exits abnormally */ |
| 526 | // signal(SIGCHLD, check_errors_in_children); |
| 527 | |
Denys Vlasenko | 0cf64c8 | 2017-08-10 10:35:08 +0200 | [diff] [blame] | 528 | if (ENABLE_FEATURE_SEAMLESS_LZMA |
| 529 | && (str = rpm_getstr0(TAG_PAYLOADCOMPRESSOR)) != NULL |
| 530 | && strcmp(str, "lzma") == 0 |
| 531 | ) { |
| 532 | // lzma compression can't be detected |
| 533 | // set up decompressor without detection |
| 534 | setup_lzma_on_fd(rpm_fd); |
| 535 | } else { |
| 536 | setup_unzip_on_fd(rpm_fd, /*fail_if_not_compressed:*/ 1); |
| 537 | } |
Denys Vlasenko | 3fa9262 | 2017-08-10 09:30:38 +0200 | [diff] [blame] | 538 | |
Denys Vlasenko | 3fa9262 | 2017-08-10 09:30:38 +0200 | [diff] [blame] | 539 | if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0) |
| 540 | bb_error_msg_and_die("error unpacking"); |
| 541 | |
| 542 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 543 | close(rpm_fd); |
| 544 | } |
| 545 | |
| 546 | if (SEAMLESS_COMPRESSION) { |
| 547 | check_errors_in_children(0); |
| 548 | return bb_got_signal; |
| 549 | } |
| 550 | return EXIT_SUCCESS; |
| 551 | } |