3
0

rpm.c 13 KB

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