rpm.c 12 KB

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