rpm.c 13 KB

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