rpm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 (*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->flags |= ARCHIVE_PRESERVE_DATE;
  193. archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
  194. archive_handle->src_fd = fd;
  195. archive_handle->offset = 0;
  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_RPM_BZ2
  204. && (magic[0] == 0x42) && (magic[1] == 0x5a)) {
  205. #if BB_MMU
  206. xformer = unpack_bz2_stream;
  207. #else
  208. xformer_prog = "bunzip2";
  209. #endif
  210. /* We can do better, need modifying unpack_bz2_stream to not require
  211. * first 2 bytes. Not very hard to do... I mean, TODO :) */
  212. xlseek(archive_handle->src_fd, -2, SEEK_CUR);
  213. } else
  214. bb_error_msg_and_die("no gzip"
  215. USE_FEATURE_RPM_BZ2("/bzip")
  216. " magic");
  217. } else {
  218. #if !BB_MMU
  219. /* NOMMU version of open_transformer execs an external unzipper that should
  220. * have the file position at the start of the file */
  221. xlseek(archive_handle->src_fd, 0, SEEK_SET);
  222. #endif
  223. }
  224. xchdir("/"); /* Install RPM's to root */
  225. archive_handle->src_fd = open_transformer(archive_handle->src_fd, xformer, xformer_prog);
  226. archive_handle->offset = 0;
  227. while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
  228. continue;
  229. }
  230. static rpm_index **rpm_gettags(int fd, int *num_tags)
  231. {
  232. /* We should never need mode than 200, and realloc later */
  233. rpm_index **tags = xzalloc(200 * sizeof(struct rpmtag *));
  234. int pass, tagindex = 0;
  235. xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
  236. /* 1st pass is the signature headers, 2nd is the main stuff */
  237. for (pass = 0; pass < 2; pass++) {
  238. struct {
  239. char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
  240. uint8_t version; /* 1 byte version number */
  241. uint32_t reserved; /* 4 bytes reserved */
  242. uint32_t entries; /* Number of entries in header (4 bytes) */
  243. uint32_t size; /* Size of store (4 bytes) */
  244. } header;
  245. rpm_index *tmpindex;
  246. int storepos;
  247. xread(fd, &header, sizeof(header));
  248. if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0)
  249. return NULL; /* Invalid magic */
  250. if (header.version != 1)
  251. return NULL; /* This program only supports v1 headers */
  252. header.size = ntohl(header.size);
  253. header.entries = ntohl(header.entries);
  254. storepos = xlseek(fd,0,SEEK_CUR) + header.entries * 16;
  255. while (header.entries--) {
  256. tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index));
  257. xread(fd, tmpindex, sizeof(rpm_index));
  258. tmpindex->tag = ntohl(tmpindex->tag);
  259. tmpindex->type = ntohl(tmpindex->type);
  260. tmpindex->count = ntohl(tmpindex->count);
  261. tmpindex->offset = storepos + ntohl(tmpindex->offset);
  262. if (pass==0)
  263. tmpindex->tag -= 743;
  264. }
  265. xlseek(fd, header.size, SEEK_CUR); /* Seek past store */
  266. /* Skip padding to 8 byte boundary after reading signature headers */
  267. if (pass==0)
  268. xlseek(fd, (8 - (xlseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR);
  269. }
  270. tags = xrealloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */
  271. *num_tags = tagindex;
  272. return tags; /* All done, leave the file at the start of the gzipped cpio archive */
  273. }
  274. static int bsearch_rpmtag(const void *key, const void *item)
  275. {
  276. int *tag = (int *)key;
  277. rpm_index **tmp = (rpm_index **) item;
  278. return (*tag - tmp[0]->tag);
  279. }
  280. static int rpm_getcount(int tag)
  281. {
  282. rpm_index **found;
  283. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  284. if (!found)
  285. return 0;
  286. return found[0]->count;
  287. }
  288. static char *rpm_getstr(int tag, int itemindex)
  289. {
  290. rpm_index **found;
  291. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  292. if (!found || itemindex >= found[0]->count)
  293. return NULL;
  294. if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) {
  295. int n;
  296. char *tmpstr = (char *) (map + found[0]->offset);
  297. for (n=0; n < itemindex; n++)
  298. tmpstr = tmpstr + strlen(tmpstr) + 1;
  299. return tmpstr;
  300. }
  301. return NULL;
  302. }
  303. static int rpm_getint(int tag, int itemindex)
  304. {
  305. rpm_index **found;
  306. int *tmpint; /* NB: using int8_t* would be easier to code */
  307. /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
  308. * it's ok to ignore it because tag won't be used as a pointer */
  309. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  310. if (!found || itemindex >= found[0]->count)
  311. return -1;
  312. tmpint = (int *) (map + found[0]->offset);
  313. if (found[0]->type == RPM_INT32_TYPE) {
  314. tmpint = (int *) ((char *) tmpint + itemindex*4);
  315. /*return ntohl(*tmpint);*/
  316. /* int can be != int32_t */
  317. return ntohl(*(int32_t*)tmpint);
  318. }
  319. if (found[0]->type == RPM_INT16_TYPE) {
  320. tmpint = (int *) ((char *) tmpint + itemindex*2);
  321. /* ??? read int, and THEN ntohs() it?? */
  322. /*return ntohs(*tmpint);*/
  323. return ntohs(*(int16_t*)tmpint);
  324. }
  325. if (found[0]->type == RPM_INT8_TYPE) {
  326. tmpint = (int *) ((char *) tmpint + itemindex);
  327. /* ??? why we don't read byte here??? */
  328. /*return ntohs(*tmpint);*/
  329. return *(int8_t*)tmpint;
  330. }
  331. return -1;
  332. }
  333. static void fileaction_dobackup(char *filename, int fileref)
  334. {
  335. struct stat oldfile;
  336. int stat_res;
  337. char *newname;
  338. if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) {
  339. /* Only need to backup config files */
  340. stat_res = lstat(filename, &oldfile);
  341. if (stat_res == 0 && S_ISREG(oldfile.st_mode)) {
  342. /* File already exists - really should check MD5's etc to see if different */
  343. newname = xasprintf("%s.rpmorig", filename);
  344. copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
  345. remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
  346. free(newname);
  347. }
  348. }
  349. }
  350. static void fileaction_setowngrp(char *filename, int fileref)
  351. {
  352. int uid, gid;
  353. uid = xuname2uid(rpm_getstr(TAG_FILEUSERNAME, fileref));
  354. gid = xgroup2gid(rpm_getstr(TAG_FILEGROUPNAME, fileref));
  355. chown(filename, uid, gid);
  356. }
  357. static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
  358. {
  359. int count = 0;
  360. while (rpm_getstr(filetag, count)) {
  361. char* filename = xasprintf("%s%s",
  362. rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)),
  363. rpm_getstr(TAG_BASENAMES, count));
  364. fileaction(filename, count++);
  365. free(filename);
  366. }
  367. }