rpm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 tarball for details.
  8. */
  9. #include "libbb.h"
  10. #include "unarchive.h"
  11. #define RPM_HEADER_MAGIC "\216\255\350"
  12. #define RPM_CHAR_TYPE 1
  13. #define RPM_INT8_TYPE 2
  14. #define RPM_INT16_TYPE 3
  15. #define RPM_INT32_TYPE 4
  16. /* #define RPM_INT64_TYPE 5 ---- These aren't supported (yet) */
  17. #define RPM_STRING_TYPE 6
  18. #define RPM_BIN_TYPE 7
  19. #define RPM_STRING_ARRAY_TYPE 8
  20. #define RPM_I18NSTRING_TYPE 9
  21. #define TAG_NAME 1000
  22. #define TAG_VERSION 1001
  23. #define TAG_RELEASE 1002
  24. #define TAG_SUMMARY 1004
  25. #define TAG_DESCRIPTION 1005
  26. #define TAG_BUILDTIME 1006
  27. #define TAG_BUILDHOST 1007
  28. #define TAG_SIZE 1009
  29. #define TAG_VENDOR 1011
  30. #define TAG_LICENSE 1014
  31. #define TAG_PACKAGER 1015
  32. #define TAG_GROUP 1016
  33. #define TAG_URL 1020
  34. #define TAG_PREIN 1023
  35. #define TAG_POSTIN 1024
  36. #define TAG_FILEFLAGS 1037
  37. #define TAG_FILEUSERNAME 1039
  38. #define TAG_FILEGROUPNAME 1040
  39. #define TAG_SOURCERPM 1044
  40. #define TAG_PREINPROG 1085
  41. #define TAG_POSTINPROG 1086
  42. #define TAG_PREFIXS 1098
  43. #define TAG_DIRINDEXES 1116
  44. #define TAG_BASENAMES 1117
  45. #define TAG_DIRNAMES 1118
  46. #define RPMFILE_CONFIG (1 << 0)
  47. #define RPMFILE_DOC (1 << 1)
  48. enum rpm_functions_e {
  49. rpm_query = 1,
  50. rpm_install = 2,
  51. rpm_query_info = 4,
  52. rpm_query_package = 8,
  53. rpm_query_list = 16,
  54. rpm_query_list_doc = 32,
  55. rpm_query_list_config = 64
  56. };
  57. typedef struct {
  58. uint32_t tag; /* 4 byte tag */
  59. uint32_t type; /* 4 byte type */
  60. uint32_t offset; /* 4 byte offset */
  61. uint32_t count; /* 4 byte count */
  62. } rpm_index;
  63. static void *map;
  64. static rpm_index **mytags;
  65. static int tagcount;
  66. static void extract_cpio_gz(int fd);
  67. static rpm_index **rpm_gettags(int fd, int *num_tags);
  68. static int bsearch_rpmtag(const void *key, const void *item);
  69. static char *rpm_getstr(int tag, int itemindex);
  70. static int rpm_getint(int tag, int itemindex);
  71. static int rpm_getcount(int tag);
  72. static void fileaction_dobackup(char *filename, int fileref);
  73. static void fileaction_setowngrp(char *filename, int fileref);
  74. static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref));
  75. int rpm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  76. int rpm_main(int argc, char **argv)
  77. {
  78. int opt = 0, func = 0, rpm_fd, offset;
  79. const int pagesize = getpagesize();
  80. while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
  81. switch (opt) {
  82. case 'i': /* First arg: Install mode, with q: Information */
  83. if (!func) func = rpm_install;
  84. else func |= rpm_query_info;
  85. break;
  86. case 'q': /* First arg: Query mode */
  87. if (func) bb_show_usage();
  88. func = rpm_query;
  89. break;
  90. case 'p': /* Query a package */
  91. func |= rpm_query_package;
  92. break;
  93. case 'l': /* List files in a package */
  94. func |= rpm_query_list;
  95. break;
  96. case 'd': /* List doc files in a package (implies list) */
  97. func |= rpm_query_list;
  98. func |= rpm_query_list_doc;
  99. break;
  100. case 'c': /* List config files in a package (implies list) */
  101. func |= rpm_query_list;
  102. func |= rpm_query_list_config;
  103. break;
  104. default:
  105. bb_show_usage();
  106. }
  107. }
  108. argv += optind;
  109. argc -= optind;
  110. if (!argc) bb_show_usage();
  111. while (*argv) {
  112. rpm_fd = xopen(*argv++, O_RDONLY);
  113. mytags = rpm_gettags(rpm_fd, &tagcount);
  114. if (!mytags)
  115. bb_error_msg_and_die("error reading rpm header");
  116. offset = xlseek(rpm_fd, 0, SEEK_CUR);
  117. /* Mimimum is one page */
  118. map = mmap(0, offset > pagesize ? (offset + offset % pagesize) : pagesize, PROT_READ, MAP_PRIVATE, rpm_fd, 0);
  119. if (func & rpm_install) {
  120. /* Backup any config files */
  121. loop_through_files(TAG_BASENAMES, fileaction_dobackup);
  122. /* Extact the archive */
  123. extract_cpio_gz(rpm_fd);
  124. /* Set the correct file uid/gid's */
  125. loop_through_files(TAG_BASENAMES, fileaction_setowngrp);
  126. }
  127. else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) {
  128. if (!(func & (rpm_query_info|rpm_query_list))) {
  129. /* If just a straight query, just give package name */
  130. printf("%s-%s-%s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_RELEASE, 0));
  131. }
  132. if (func & rpm_query_info) {
  133. /* Do the nice printout */
  134. time_t bdate_time;
  135. struct tm *bdate;
  136. char bdatestring[50];
  137. printf("Name : %-29sRelocations: %s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_PREFIXS, 0) ? rpm_getstr(TAG_PREFIXS, 0) : "(not relocateable)");
  138. printf("Version : %-34sVendor: %s\n", rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_VENDOR, 0) ? rpm_getstr(TAG_VENDOR, 0) : "(none)");
  139. bdate_time = rpm_getint(TAG_BUILDTIME, 0);
  140. bdate = localtime((time_t *) &bdate_time);
  141. strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate);
  142. printf("Release : %-30sBuild Date: %s\n", rpm_getstr(TAG_RELEASE, 0), bdatestring);
  143. printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstr(TAG_BUILDHOST, 0));
  144. printf("Group : %-30sSource RPM: %s\n", rpm_getstr(TAG_GROUP, 0), rpm_getstr(TAG_SOURCERPM, 0));
  145. printf("Size : %-33dLicense: %s\n", rpm_getint(TAG_SIZE, 0), rpm_getstr(TAG_LICENSE, 0));
  146. printf("URL : %s\n", rpm_getstr(TAG_URL, 0));
  147. printf("Summary : %s\n", rpm_getstr(TAG_SUMMARY, 0));
  148. printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0));
  149. }
  150. if (func & rpm_query_list) {
  151. int count, it, flags;
  152. count = rpm_getcount(TAG_BASENAMES);
  153. for (it = 0; it < count; it++) {
  154. flags = rpm_getint(TAG_FILEFLAGS, it);
  155. switch (func & (rpm_query_list_doc|rpm_query_list_config)) {
  156. case rpm_query_list_doc:
  157. if (!(flags & RPMFILE_DOC)) continue;
  158. break;
  159. case rpm_query_list_config:
  160. if (!(flags & RPMFILE_CONFIG)) continue;
  161. break;
  162. case rpm_query_list_doc|rpm_query_list_config:
  163. if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue;
  164. break;
  165. }
  166. printf("%s%s\n",
  167. rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)),
  168. rpm_getstr(TAG_BASENAMES, it));
  169. }
  170. }
  171. }
  172. free(mytags);
  173. }
  174. return 0;
  175. }
  176. static void extract_cpio_gz(int fd)
  177. {
  178. archive_handle_t *archive_handle;
  179. unsigned char magic[2];
  180. #if BB_MMU
  181. USE_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
  182. enum { xformer_prog = 0 };
  183. #else
  184. enum { xformer = 0 };
  185. const char *xformer_prog;
  186. #endif
  187. /* Initialize */
  188. archive_handle = init_handle();
  189. archive_handle->seek = seek_by_read;
  190. //archive_handle->action_header = header_list;
  191. archive_handle->action_data = data_extract_all;
  192. archive_handle->ah_flags = ARCHIVE_PRESERVE_DATE | ARCHIVE_CREATE_LEADING_DIRS;
  193. archive_handle->src_fd = fd;
  194. /*archive_handle->offset = 0; - init_handle() did it */
  195. // TODO: open_zipped does the same
  196. xread(archive_handle->src_fd, &magic, 2);
  197. #if BB_MMU
  198. xformer = unpack_gz_stream;
  199. #else
  200. xformer_prog = "gunzip";
  201. #endif
  202. if (magic[0] != 0x1f || magic[1] != 0x8b) {
  203. if (!ENABLE_FEATURE_SEAMLESS_BZ2
  204. || magic[0] != 'B' || magic[1] != 'Z'
  205. ) {
  206. bb_error_msg_and_die("no gzip"
  207. USE_FEATURE_SEAMLESS_BZ2("/bzip2")
  208. " magic");
  209. }
  210. #if BB_MMU
  211. xformer = unpack_bz2_stream;
  212. #else
  213. xformer_prog = "bunzip2";
  214. #endif
  215. } else {
  216. #if !BB_MMU
  217. /* NOMMU version of open_transformer execs an external unzipper that should
  218. * have the file position at the start of the file */
  219. xlseek(archive_handle->src_fd, 0, SEEK_SET);
  220. #endif
  221. }
  222. xchdir("/"); /* Install RPM's to root */
  223. open_transformer(archive_handle->src_fd, xformer, xformer_prog);
  224. archive_handle->offset = 0;
  225. while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
  226. continue;
  227. }
  228. static rpm_index **rpm_gettags(int fd, int *num_tags)
  229. {
  230. /* We should never need mode than 200, and realloc later */
  231. rpm_index **tags = xzalloc(200 * sizeof(struct rpmtag *));
  232. int pass, tagindex = 0;
  233. xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
  234. /* 1st pass is the signature headers, 2nd is the main stuff */
  235. for (pass = 0; pass < 2; pass++) {
  236. struct {
  237. char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
  238. uint8_t version; /* 1 byte version number */
  239. uint32_t reserved; /* 4 bytes reserved */
  240. uint32_t entries; /* Number of entries in header (4 bytes) */
  241. uint32_t size; /* Size of store (4 bytes) */
  242. } header;
  243. rpm_index *tmpindex;
  244. int storepos;
  245. xread(fd, &header, sizeof(header));
  246. if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0)
  247. return NULL; /* Invalid magic */
  248. if (header.version != 1)
  249. return NULL; /* This program only supports v1 headers */
  250. header.size = ntohl(header.size);
  251. header.entries = ntohl(header.entries);
  252. storepos = xlseek(fd,0,SEEK_CUR) + header.entries * 16;
  253. while (header.entries--) {
  254. tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index));
  255. xread(fd, tmpindex, sizeof(rpm_index));
  256. tmpindex->tag = ntohl(tmpindex->tag);
  257. tmpindex->type = ntohl(tmpindex->type);
  258. tmpindex->count = ntohl(tmpindex->count);
  259. tmpindex->offset = storepos + ntohl(tmpindex->offset);
  260. if (pass == 0)
  261. tmpindex->tag -= 743;
  262. }
  263. xlseek(fd, header.size, SEEK_CUR); /* Seek past store */
  264. /* Skip padding to 8 byte boundary after reading signature headers */
  265. if (pass == 0)
  266. xlseek(fd, (8 - (xlseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR);
  267. }
  268. tags = xrealloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */
  269. *num_tags = tagindex;
  270. return tags; /* All done, leave the file at the start of the gzipped cpio archive */
  271. }
  272. static int bsearch_rpmtag(const void *key, const void *item)
  273. {
  274. int *tag = (int *)key;
  275. rpm_index **tmp = (rpm_index **) item;
  276. return (*tag - tmp[0]->tag);
  277. }
  278. static int rpm_getcount(int tag)
  279. {
  280. rpm_index **found;
  281. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  282. if (!found)
  283. return 0;
  284. return found[0]->count;
  285. }
  286. static char *rpm_getstr(int tag, int itemindex)
  287. {
  288. rpm_index **found;
  289. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  290. if (!found || itemindex >= found[0]->count)
  291. return NULL;
  292. if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) {
  293. int n;
  294. char *tmpstr = (char *) (map + found[0]->offset);
  295. for (n=0; n < itemindex; n++)
  296. tmpstr = tmpstr + strlen(tmpstr) + 1;
  297. return tmpstr;
  298. }
  299. return NULL;
  300. }
  301. static int rpm_getint(int tag, int itemindex)
  302. {
  303. rpm_index **found;
  304. int *tmpint; /* NB: using int8_t* would be easier to code */
  305. /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
  306. * it's ok to ignore it because tag won't be used as a pointer */
  307. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  308. if (!found || itemindex >= found[0]->count)
  309. return -1;
  310. tmpint = (int *) (map + found[0]->offset);
  311. if (found[0]->type == RPM_INT32_TYPE) {
  312. tmpint = (int *) ((char *) tmpint + itemindex*4);
  313. /*return ntohl(*tmpint);*/
  314. /* int can be != int32_t */
  315. return ntohl(*(int32_t*)tmpint);
  316. }
  317. if (found[0]->type == RPM_INT16_TYPE) {
  318. tmpint = (int *) ((char *) tmpint + itemindex*2);
  319. /* ??? read int, and THEN ntohs() it?? */
  320. /*return ntohs(*tmpint);*/
  321. return ntohs(*(int16_t*)tmpint);
  322. }
  323. if (found[0]->type == RPM_INT8_TYPE) {
  324. tmpint = (int *) ((char *) tmpint + itemindex);
  325. /* ??? why we don't read byte here??? */
  326. /*return ntohs(*tmpint);*/
  327. return *(int8_t*)tmpint;
  328. }
  329. return -1;
  330. }
  331. static void fileaction_dobackup(char *filename, int fileref)
  332. {
  333. struct stat oldfile;
  334. int stat_res;
  335. char *newname;
  336. if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) {
  337. /* Only need to backup config files */
  338. stat_res = lstat(filename, &oldfile);
  339. if (stat_res == 0 && S_ISREG(oldfile.st_mode)) {
  340. /* File already exists - really should check MD5's etc to see if different */
  341. newname = xasprintf("%s.rpmorig", filename);
  342. copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
  343. remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
  344. free(newname);
  345. }
  346. }
  347. }
  348. static void fileaction_setowngrp(char *filename, int fileref)
  349. {
  350. int uid, gid;
  351. uid = xuname2uid(rpm_getstr(TAG_FILEUSERNAME, fileref));
  352. gid = xgroup2gid(rpm_getstr(TAG_FILEGROUPNAME, fileref));
  353. chown(filename, uid, gid);
  354. }
  355. static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
  356. {
  357. int count = 0;
  358. while (rpm_getstr(filetag, count)) {
  359. char* filename = xasprintf("%s%s",
  360. rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)),
  361. rpm_getstr(TAG_BASENAMES, count));
  362. fileaction(filename, count++);
  363. free(filename);
  364. }
  365. }