rpm.c 16 KB

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