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