rpm.c 16 KB

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