3
0

rpm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 source tree.
  8. */
  9. //config:config RPM
  10. //config: bool "rpm"
  11. //config: default y
  12. //config: help
  13. //config: Mini RPM applet - queries and extracts RPM packages.
  14. //applet:IF_RPM(APPLET(rpm, BB_DIR_BIN, BB_SUID_DROP))
  15. //kbuild:lib-$(CONFIG_RPM) += rpm.o
  16. //usage:#define rpm_trivial_usage
  17. //usage: "-i PACKAGE.rpm; rpm -qp[ildc] PACKAGE.rpm"
  18. //usage:#define rpm_full_usage "\n\n"
  19. //usage: "Manipulate RPM packages\n"
  20. //usage: "\nCommands:"
  21. //usage: "\n -i Install package"
  22. //usage: "\n -qp Query package"
  23. //usage: "\n -qpi Show information"
  24. //usage: "\n -qpl List contents"
  25. //usage: "\n -qpd List documents"
  26. //usage: "\n -qpc List config files"
  27. #include "libbb.h"
  28. #include "common_bufsiz.h"
  29. #include "bb_archive.h"
  30. #include "rpm.h"
  31. #define RPM_CHAR_TYPE 1
  32. #define RPM_INT8_TYPE 2
  33. #define RPM_INT16_TYPE 3
  34. #define RPM_INT32_TYPE 4
  35. /* #define RPM_INT64_TYPE 5 ---- These aren't supported (yet) */
  36. #define RPM_STRING_TYPE 6
  37. #define RPM_BIN_TYPE 7
  38. #define RPM_STRING_ARRAY_TYPE 8
  39. #define RPM_I18NSTRING_TYPE 9
  40. #define TAG_NAME 1000
  41. #define TAG_VERSION 1001
  42. #define TAG_RELEASE 1002
  43. #define TAG_SUMMARY 1004
  44. #define TAG_DESCRIPTION 1005
  45. #define TAG_BUILDTIME 1006
  46. #define TAG_BUILDHOST 1007
  47. #define TAG_SIZE 1009
  48. #define TAG_VENDOR 1011
  49. #define TAG_LICENSE 1014
  50. #define TAG_PACKAGER 1015
  51. #define TAG_GROUP 1016
  52. #define TAG_URL 1020
  53. #define TAG_PREIN 1023
  54. #define TAG_POSTIN 1024
  55. #define TAG_FILEFLAGS 1037
  56. #define TAG_FILEUSERNAME 1039
  57. #define TAG_FILEGROUPNAME 1040
  58. #define TAG_SOURCERPM 1044
  59. #define TAG_PREINPROG 1085
  60. #define TAG_POSTINPROG 1086
  61. #define TAG_PREFIXS 1098
  62. #define TAG_DIRINDEXES 1116
  63. #define TAG_BASENAMES 1117
  64. #define TAG_DIRNAMES 1118
  65. #define RPMFILE_CONFIG (1 << 0)
  66. #define RPMFILE_DOC (1 << 1)
  67. enum rpm_functions_e {
  68. rpm_query = 1,
  69. rpm_install = 2,
  70. rpm_query_info = 4,
  71. rpm_query_package = 8,
  72. rpm_query_list = 16,
  73. rpm_query_list_doc = 32,
  74. rpm_query_list_config = 64
  75. };
  76. typedef struct {
  77. uint32_t tag; /* 4 byte tag */
  78. uint32_t type; /* 4 byte type */
  79. uint32_t offset; /* 4 byte offset */
  80. uint32_t count; /* 4 byte count */
  81. } rpm_index;
  82. struct globals {
  83. void *map;
  84. rpm_index **mytags;
  85. int tagcount;
  86. } FIX_ALIASING;
  87. #define G (*(struct globals*)bb_common_bufsiz1)
  88. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  89. static void extract_cpio(int fd, const char *source_rpm)
  90. {
  91. archive_handle_t *archive_handle;
  92. if (source_rpm != NULL) {
  93. /* Binary rpm (it was built from some SRPM), install to root */
  94. xchdir("/");
  95. } /* else: SRPM, install to current dir */
  96. /* Initialize */
  97. archive_handle = init_handle();
  98. archive_handle->seek = seek_by_read;
  99. archive_handle->action_data = data_extract_all;
  100. #if 0 /* For testing (rpm -i only lists the files in internal cpio): */
  101. archive_handle->action_header = header_list;
  102. archive_handle->action_data = data_skip;
  103. #endif
  104. archive_handle->ah_flags = ARCHIVE_RESTORE_DATE | ARCHIVE_CREATE_LEADING_DIRS
  105. /* compat: overwrite existing files.
  106. * try "rpm -i foo.src.rpm" few times in a row -
  107. * standard rpm will not complain.
  108. */
  109. | ARCHIVE_REPLACE_VIA_RENAME;
  110. archive_handle->src_fd = fd;
  111. /*archive_handle->offset = 0; - init_handle() did it */
  112. setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 1);
  113. while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
  114. continue;
  115. }
  116. static rpm_index **rpm_gettags(int fd, int *num_tags)
  117. {
  118. /* We should never need more than 200 (shrink via realloc later) */
  119. rpm_index **tags = xzalloc(200 * sizeof(tags[0]));
  120. int pass, tagindex = 0;
  121. xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
  122. /* 1st pass is the signature headers, 2nd is the main stuff */
  123. for (pass = 0; pass < 2; pass++) {
  124. struct rpm_header header;
  125. rpm_index *tmpindex;
  126. int storepos;
  127. xread(fd, &header, sizeof(header));
  128. if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER))
  129. return NULL; /* Invalid magic, or not version 1 */
  130. header.size = ntohl(header.size);
  131. header.entries = ntohl(header.entries);
  132. storepos = xlseek(fd, 0, SEEK_CUR) + header.entries * 16;
  133. while (header.entries--) {
  134. tmpindex = tags[tagindex++] = xmalloc(sizeof(*tmpindex));
  135. xread(fd, tmpindex, sizeof(*tmpindex));
  136. tmpindex->tag = ntohl(tmpindex->tag);
  137. tmpindex->type = ntohl(tmpindex->type);
  138. tmpindex->count = ntohl(tmpindex->count);
  139. tmpindex->offset = storepos + ntohl(tmpindex->offset);
  140. if (pass == 0)
  141. tmpindex->tag -= 743;
  142. }
  143. storepos = xlseek(fd, header.size, SEEK_CUR); /* Seek past store */
  144. /* Skip padding to 8 byte boundary after reading signature headers */
  145. if (pass == 0)
  146. xlseek(fd, (-storepos) & 0x7, SEEK_CUR);
  147. }
  148. /* realloc tags to save space */
  149. tags = xrealloc(tags, tagindex * sizeof(tags[0]));
  150. *num_tags = tagindex;
  151. /* All done, leave the file at the start of the gzipped cpio archive */
  152. return tags;
  153. }
  154. static int bsearch_rpmtag(const void *key, const void *item)
  155. {
  156. int *tag = (int *)key;
  157. rpm_index **tmp = (rpm_index **) item;
  158. return (*tag - tmp[0]->tag);
  159. }
  160. static int rpm_getcount(int tag)
  161. {
  162. rpm_index **found;
  163. found = bsearch(&tag, G.mytags, G.tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  164. if (!found)
  165. return 0;
  166. return found[0]->count;
  167. }
  168. static char *rpm_getstr(int tag, int itemindex)
  169. {
  170. rpm_index **found;
  171. found = bsearch(&tag, G.mytags, G.tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  172. if (!found || itemindex >= found[0]->count)
  173. return NULL;
  174. if (found[0]->type == RPM_STRING_TYPE
  175. || found[0]->type == RPM_I18NSTRING_TYPE
  176. || found[0]->type == RPM_STRING_ARRAY_TYPE
  177. ) {
  178. int n;
  179. char *tmpstr = (char *) G.map + found[0]->offset;
  180. for (n = 0; n < itemindex; n++)
  181. tmpstr = tmpstr + strlen(tmpstr) + 1;
  182. return tmpstr;
  183. }
  184. return NULL;
  185. }
  186. static int rpm_getint(int tag, int itemindex)
  187. {
  188. rpm_index **found;
  189. char *tmpint;
  190. /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
  191. * it's ok to ignore it because tag won't be used as a pointer */
  192. found = bsearch(&tag, G.mytags, G.tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  193. if (!found || itemindex >= found[0]->count)
  194. return -1;
  195. tmpint = (char *) G.map + found[0]->offset;
  196. if (found[0]->type == RPM_INT32_TYPE) {
  197. tmpint += itemindex*4;
  198. return ntohl(*(int32_t*)tmpint);
  199. }
  200. if (found[0]->type == RPM_INT16_TYPE) {
  201. tmpint += itemindex*2;
  202. return ntohs(*(int16_t*)tmpint);
  203. }
  204. if (found[0]->type == RPM_INT8_TYPE) {
  205. tmpint += itemindex;
  206. return *(int8_t*)tmpint;
  207. }
  208. return -1;
  209. }
  210. static void fileaction_dobackup(char *filename, int fileref)
  211. {
  212. struct stat oldfile;
  213. int stat_res;
  214. char *newname;
  215. if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) {
  216. /* Only need to backup config files */
  217. stat_res = lstat(filename, &oldfile);
  218. if (stat_res == 0 && S_ISREG(oldfile.st_mode)) {
  219. /* File already exists - really should check MD5's etc to see if different */
  220. newname = xasprintf("%s.rpmorig", filename);
  221. copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
  222. remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
  223. free(newname);
  224. }
  225. }
  226. }
  227. static void fileaction_setowngrp(char *filename, int fileref)
  228. {
  229. /* real rpm warns: "user foo does not exist - using <you>" */
  230. struct passwd *pw = getpwnam(rpm_getstr(TAG_FILEUSERNAME, fileref));
  231. int uid = pw ? pw->pw_uid : getuid(); /* or euid? */
  232. struct group *gr = getgrnam(rpm_getstr(TAG_FILEGROUPNAME, fileref));
  233. int gid = gr ? gr->gr_gid : getgid();
  234. chown(filename, uid, gid);
  235. }
  236. static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
  237. {
  238. int count = 0;
  239. while (rpm_getstr(filetag, count)) {
  240. char* filename = xasprintf("%s%s",
  241. rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)),
  242. rpm_getstr(TAG_BASENAMES, count));
  243. fileaction(filename, count++);
  244. free(filename);
  245. }
  246. }
  247. int rpm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  248. int rpm_main(int argc, char **argv)
  249. {
  250. int opt, func = 0;
  251. const unsigned pagesize = getpagesize();
  252. while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
  253. switch (opt) {
  254. case 'i': /* First arg: Install mode, with q: Information */
  255. if (!func) func = rpm_install;
  256. else func |= rpm_query_info;
  257. break;
  258. case 'q': /* First arg: Query mode */
  259. if (func) bb_show_usage();
  260. func = rpm_query;
  261. break;
  262. case 'p': /* Query a package */
  263. func |= rpm_query_package;
  264. break;
  265. case 'l': /* List files in a package */
  266. func |= rpm_query_list;
  267. break;
  268. case 'd': /* List doc files in a package (implies list) */
  269. func |= rpm_query_list;
  270. func |= rpm_query_list_doc;
  271. break;
  272. case 'c': /* List config files in a package (implies list) */
  273. func |= rpm_query_list;
  274. func |= rpm_query_list_config;
  275. break;
  276. default:
  277. bb_show_usage();
  278. }
  279. }
  280. argv += optind;
  281. //argc -= optind;
  282. if (!argv[0]) {
  283. bb_show_usage();
  284. }
  285. while (*argv) {
  286. int rpm_fd;
  287. unsigned mapsize;
  288. const char *source_rpm;
  289. rpm_fd = xopen(*argv++, O_RDONLY);
  290. G.mytags = rpm_gettags(rpm_fd, &G.tagcount);
  291. if (!G.mytags)
  292. bb_error_msg_and_die("error reading rpm header");
  293. mapsize = xlseek(rpm_fd, 0, SEEK_CUR);
  294. mapsize = (mapsize + pagesize) & -(int)pagesize;
  295. /* Some NOMMU systems prefer MAP_PRIVATE over MAP_SHARED */
  296. G.map = mmap(0, mapsize, PROT_READ, MAP_PRIVATE, rpm_fd, 0);
  297. //FIXME: error check?
  298. source_rpm = rpm_getstr(TAG_SOURCERPM, 0);
  299. if (func & rpm_install) {
  300. /* Backup any config files */
  301. loop_through_files(TAG_BASENAMES, fileaction_dobackup);
  302. /* Extact the archive */
  303. extract_cpio(rpm_fd, source_rpm);
  304. /* Set the correct file uid/gid's */
  305. loop_through_files(TAG_BASENAMES, fileaction_setowngrp);
  306. }
  307. else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) {
  308. if (!(func & (rpm_query_info|rpm_query_list))) {
  309. /* If just a straight query, just give package name */
  310. printf("%s-%s-%s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_RELEASE, 0));
  311. }
  312. if (func & rpm_query_info) {
  313. /* Do the nice printout */
  314. time_t bdate_time;
  315. struct tm *bdate_ptm;
  316. char bdatestring[50];
  317. const char *p;
  318. printf("%-12s: %s\n", "Name" , rpm_getstr(TAG_NAME, 0));
  319. /* TODO compat: add "Epoch" here */
  320. printf("%-12s: %s\n", "Version" , rpm_getstr(TAG_VERSION, 0));
  321. printf("%-12s: %s\n", "Release" , rpm_getstr(TAG_RELEASE, 0));
  322. /* add "Architecture" */
  323. printf("%-12s: %s\n", "Install Date", "(not installed)");
  324. printf("%-12s: %s\n", "Group" , rpm_getstr(TAG_GROUP, 0));
  325. printf("%-12s: %d\n", "Size" , rpm_getint(TAG_SIZE, 0));
  326. printf("%-12s: %s\n", "License" , rpm_getstr(TAG_LICENSE, 0));
  327. /* add "Signature" */
  328. printf("%-12s: %s\n", "Source RPM" , source_rpm ? source_rpm : "(none)");
  329. bdate_time = rpm_getint(TAG_BUILDTIME, 0);
  330. bdate_ptm = localtime(&bdate_time);
  331. strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate_ptm);
  332. printf("%-12s: %s\n", "Build Date" , bdatestring);
  333. printf("%-12s: %s\n", "Build Host" , rpm_getstr(TAG_BUILDHOST, 0));
  334. p = rpm_getstr(TAG_PREFIXS, 0);
  335. printf("%-12s: %s\n", "Relocations" , p ? p : "(not relocatable)");
  336. /* add "Packager" */
  337. p = rpm_getstr(TAG_VENDOR, 0);
  338. printf("%-12s: %s\n", "Vendor" , p ? p : "(none)");
  339. printf("%-12s: %s\n", "URL" , rpm_getstr(TAG_URL, 0));
  340. printf("%-12s: %s\n", "Summary" , rpm_getstr(TAG_SUMMARY, 0));
  341. printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0));
  342. }
  343. if (func & rpm_query_list) {
  344. int count, it, flags;
  345. count = rpm_getcount(TAG_BASENAMES);
  346. for (it = 0; it < count; it++) {
  347. flags = rpm_getint(TAG_FILEFLAGS, it);
  348. switch (func & (rpm_query_list_doc|rpm_query_list_config)) {
  349. case rpm_query_list_doc:
  350. if (!(flags & RPMFILE_DOC)) continue;
  351. break;
  352. case rpm_query_list_config:
  353. if (!(flags & RPMFILE_CONFIG)) continue;
  354. break;
  355. case rpm_query_list_doc|rpm_query_list_config:
  356. if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue;
  357. break;
  358. }
  359. printf("%s%s\n",
  360. rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)),
  361. rpm_getstr(TAG_BASENAMES, it));
  362. }
  363. }
  364. }
  365. munmap(G.map, mapsize);
  366. free(G.mytags);
  367. close(rpm_fd);
  368. }
  369. return 0;
  370. }