rpm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include "libbb.h"
  10. #include "archive.h"
  11. #include "rpm.h"
  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(int fd, const char *source_rpm);
  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]) {
  111. bb_show_usage();
  112. }
  113. while (*argv) {
  114. const char *source_rpm;
  115. rpm_fd = xopen(*argv++, O_RDONLY);
  116. mytags = rpm_gettags(rpm_fd, &tagcount);
  117. if (!mytags)
  118. bb_error_msg_and_die("error reading rpm header");
  119. offset = xlseek(rpm_fd, 0, SEEK_CUR);
  120. /* Mimimum is one page */
  121. map = mmap(0, offset > pagesize ? (offset + offset % pagesize) : pagesize, PROT_READ, MAP_PRIVATE, rpm_fd, 0);
  122. source_rpm = rpm_getstr(TAG_SOURCERPM, 0);
  123. if (func & rpm_install) {
  124. /* Backup any config files */
  125. loop_through_files(TAG_BASENAMES, fileaction_dobackup);
  126. /* Extact the archive */
  127. extract_cpio(rpm_fd, source_rpm);
  128. /* Set the correct file uid/gid's */
  129. loop_through_files(TAG_BASENAMES, fileaction_setowngrp);
  130. }
  131. else if ((func & (rpm_query|rpm_query_package)) == (rpm_query|rpm_query_package)) {
  132. if (!(func & (rpm_query_info|rpm_query_list))) {
  133. /* If just a straight query, just give package name */
  134. printf("%s-%s-%s\n", rpm_getstr(TAG_NAME, 0), rpm_getstr(TAG_VERSION, 0), rpm_getstr(TAG_RELEASE, 0));
  135. }
  136. if (func & rpm_query_info) {
  137. /* Do the nice printout */
  138. time_t bdate_time;
  139. struct tm *bdate_ptm;
  140. char bdatestring[50];
  141. const char *p;
  142. p = rpm_getstr(TAG_PREFIXS, 0);
  143. if (!p) p = "(not relocateable)";
  144. printf("Name : %-29sRelocations: %s\n", rpm_getstr(TAG_NAME, 0), p);
  145. p = rpm_getstr(TAG_VENDOR, 0);
  146. if (!p) p = "(none)";
  147. printf("Version : %-34sVendor: %s\n", rpm_getstr(TAG_VERSION, 0), p);
  148. bdate_time = rpm_getint(TAG_BUILDTIME, 0);
  149. bdate_ptm = localtime(&bdate_time);
  150. strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate_ptm);
  151. printf("Release : %-30sBuild Date: %s\n", rpm_getstr(TAG_RELEASE, 0), bdatestring);
  152. printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstr(TAG_BUILDHOST, 0));
  153. printf("Group : %-30sSource RPM: %s\n", rpm_getstr(TAG_GROUP, 0), source_rpm);
  154. printf("Size : %-33dLicense: %s\n", rpm_getint(TAG_SIZE, 0), rpm_getstr(TAG_LICENSE, 0));
  155. printf("URL : %s\n", rpm_getstr(TAG_URL, 0));
  156. printf("Summary : %s\n", rpm_getstr(TAG_SUMMARY, 0));
  157. printf("Description :\n%s\n", rpm_getstr(TAG_DESCRIPTION, 0));
  158. }
  159. if (func & rpm_query_list) {
  160. int count, it, flags;
  161. count = rpm_getcount(TAG_BASENAMES);
  162. for (it = 0; it < count; it++) {
  163. flags = rpm_getint(TAG_FILEFLAGS, it);
  164. switch (func & (rpm_query_list_doc|rpm_query_list_config)) {
  165. case rpm_query_list_doc:
  166. if (!(flags & RPMFILE_DOC)) continue;
  167. break;
  168. case rpm_query_list_config:
  169. if (!(flags & RPMFILE_CONFIG)) continue;
  170. break;
  171. case rpm_query_list_doc|rpm_query_list_config:
  172. if (!(flags & (RPMFILE_CONFIG|RPMFILE_DOC))) continue;
  173. break;
  174. }
  175. printf("%s%s\n",
  176. rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, it)),
  177. rpm_getstr(TAG_BASENAMES, it));
  178. }
  179. }
  180. }
  181. free(mytags);
  182. }
  183. return 0;
  184. }
  185. static void extract_cpio(int fd, const char *source_rpm)
  186. {
  187. archive_handle_t *archive_handle;
  188. if (source_rpm != NULL) {
  189. /* Binary rpm (it was built from some SRPM), install to root */
  190. xchdir("/");
  191. } /* else: SRPM, install to current dir */
  192. /* Initialize */
  193. archive_handle = init_handle();
  194. archive_handle->seek = seek_by_read;
  195. archive_handle->action_data = data_extract_all;
  196. #if 0 /* For testing (rpm -i only lists the files in internal cpio): */
  197. archive_handle->action_header = header_list;
  198. archive_handle->action_data = data_skip;
  199. #endif
  200. archive_handle->ah_flags = ARCHIVE_RESTORE_DATE | ARCHIVE_CREATE_LEADING_DIRS
  201. /* compat: overwrite existing files.
  202. * try "rpm -i foo.src.rpm" few times in a row -
  203. * standard rpm will not complain.
  204. * (TODO? real rpm creates "file;1234" and then renames it) */
  205. | ARCHIVE_UNLINK_OLD;
  206. archive_handle->src_fd = fd;
  207. /*archive_handle->offset = 0; - init_handle() did it */
  208. setup_unzip_on_fd(archive_handle->src_fd /*, fail_if_not_detected: 1*/);
  209. while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
  210. continue;
  211. }
  212. static rpm_index **rpm_gettags(int fd, int *num_tags)
  213. {
  214. /* We should never need more than 200 (shrink via realloc later) */
  215. rpm_index **tags = xzalloc(200 * sizeof(tags[0]));
  216. int pass, tagindex = 0;
  217. xlseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
  218. /* 1st pass is the signature headers, 2nd is the main stuff */
  219. for (pass = 0; pass < 2; pass++) {
  220. struct rpm_header header;
  221. rpm_index *tmpindex;
  222. int storepos;
  223. xread(fd, &header, sizeof(header));
  224. if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER))
  225. return NULL; /* Invalid magic, or not version 1 */
  226. header.size = ntohl(header.size);
  227. header.entries = ntohl(header.entries);
  228. storepos = xlseek(fd, 0, SEEK_CUR) + header.entries * 16;
  229. while (header.entries--) {
  230. tmpindex = tags[tagindex++] = xmalloc(sizeof(*tmpindex));
  231. xread(fd, tmpindex, sizeof(*tmpindex));
  232. tmpindex->tag = ntohl(tmpindex->tag);
  233. tmpindex->type = ntohl(tmpindex->type);
  234. tmpindex->count = ntohl(tmpindex->count);
  235. tmpindex->offset = storepos + ntohl(tmpindex->offset);
  236. if (pass == 0)
  237. tmpindex->tag -= 743;
  238. }
  239. storepos = xlseek(fd, header.size, SEEK_CUR); /* Seek past store */
  240. /* Skip padding to 8 byte boundary after reading signature headers */
  241. if (pass == 0)
  242. xlseek(fd, (-storepos) & 0x7, SEEK_CUR);
  243. }
  244. /* realloc tags to save space */
  245. tags = xrealloc(tags, tagindex * sizeof(tags[0]));
  246. *num_tags = tagindex;
  247. /* All done, leave the file at the start of the gzipped cpio archive */
  248. return tags;
  249. }
  250. static int bsearch_rpmtag(const void *key, const void *item)
  251. {
  252. int *tag = (int *)key;
  253. rpm_index **tmp = (rpm_index **) item;
  254. return (*tag - tmp[0]->tag);
  255. }
  256. static int rpm_getcount(int tag)
  257. {
  258. rpm_index **found;
  259. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  260. if (!found)
  261. return 0;
  262. return found[0]->count;
  263. }
  264. static char *rpm_getstr(int tag, int itemindex)
  265. {
  266. rpm_index **found;
  267. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  268. if (!found || itemindex >= found[0]->count)
  269. return NULL;
  270. if (found[0]->type == RPM_STRING_TYPE
  271. || found[0]->type == RPM_I18NSTRING_TYPE
  272. || found[0]->type == RPM_STRING_ARRAY_TYPE
  273. ) {
  274. int n;
  275. char *tmpstr = (char *) map + found[0]->offset;
  276. for (n = 0; n < itemindex; n++)
  277. tmpstr = tmpstr + strlen(tmpstr) + 1;
  278. return tmpstr;
  279. }
  280. return NULL;
  281. }
  282. static int rpm_getint(int tag, int itemindex)
  283. {
  284. rpm_index **found;
  285. int *tmpint; /* NB: using int8_t* would be easier to code */
  286. /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
  287. * it's ok to ignore it because tag won't be used as a pointer */
  288. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  289. if (!found || itemindex >= found[0]->count)
  290. return -1;
  291. tmpint = (int *) ((char *) map + found[0]->offset);
  292. if (found[0]->type == RPM_INT32_TYPE) {
  293. tmpint = (int *) ((char *) tmpint + itemindex*4);
  294. /*return ntohl(*tmpint);*/
  295. /* int can be != int32_t */
  296. return ntohl(*(int32_t*)tmpint);
  297. }
  298. if (found[0]->type == RPM_INT16_TYPE) {
  299. tmpint = (int *) ((char *) tmpint + itemindex*2);
  300. /* ??? read int, and THEN ntohs() it?? */
  301. /*return ntohs(*tmpint);*/
  302. return ntohs(*(int16_t*)tmpint);
  303. }
  304. if (found[0]->type == RPM_INT8_TYPE) {
  305. tmpint = (int *) ((char *) tmpint + itemindex);
  306. /* ??? why we don't read byte here??? */
  307. /*return ntohs(*tmpint);*/
  308. return *(int8_t*)tmpint;
  309. }
  310. return -1;
  311. }
  312. static void fileaction_dobackup(char *filename, int fileref)
  313. {
  314. struct stat oldfile;
  315. int stat_res;
  316. char *newname;
  317. if (rpm_getint(TAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) {
  318. /* Only need to backup config files */
  319. stat_res = lstat(filename, &oldfile);
  320. if (stat_res == 0 && S_ISREG(oldfile.st_mode)) {
  321. /* File already exists - really should check MD5's etc to see if different */
  322. newname = xasprintf("%s.rpmorig", filename);
  323. copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
  324. remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
  325. free(newname);
  326. }
  327. }
  328. }
  329. static void fileaction_setowngrp(char *filename, int fileref)
  330. {
  331. /* real rpm warns: "user foo does not exist - using <you>" */
  332. struct passwd *pw = getpwnam(rpm_getstr(TAG_FILEUSERNAME, fileref));
  333. int uid = pw ? pw->pw_uid : getuid(); /* or euid? */
  334. struct group *gr = getgrnam(rpm_getstr(TAG_FILEGROUPNAME, fileref));
  335. int gid = gr ? gr->gr_gid : getgid();
  336. chown(filename, uid, gid);
  337. }
  338. static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
  339. {
  340. int count = 0;
  341. while (rpm_getstr(filetag, count)) {
  342. char* filename = xasprintf("%s%s",
  343. rpm_getstr(TAG_DIRNAMES, rpm_getint(TAG_DIRINDEXES, count)),
  344. rpm_getstr(TAG_BASENAMES, count));
  345. fileaction(filename, count++);
  346. free(filename);
  347. }
  348. }