3
0

rpm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 "busybox.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 RPMTAG_NAME 1000
  22. #define RPMTAG_VERSION 1001
  23. #define RPMTAG_RELEASE 1002
  24. #define RPMTAG_SUMMARY 1004
  25. #define RPMTAG_DESCRIPTION 1005
  26. #define RPMTAG_BUILDTIME 1006
  27. #define RPMTAG_BUILDHOST 1007
  28. #define RPMTAG_SIZE 1009
  29. #define RPMTAG_VENDOR 1011
  30. #define RPMTAG_LICENSE 1014
  31. #define RPMTAG_PACKAGER 1015
  32. #define RPMTAG_GROUP 1016
  33. #define RPMTAG_URL 1020
  34. #define RPMTAG_PREIN 1023
  35. #define RPMTAG_POSTIN 1024
  36. #define RPMTAG_FILEFLAGS 1037
  37. #define RPMTAG_FILEUSERNAME 1039
  38. #define RPMTAG_FILEGROUPNAME 1040
  39. #define RPMTAG_SOURCERPM 1044
  40. #define RPMTAG_PREINPROG 1085
  41. #define RPMTAG_POSTINPROG 1086
  42. #define RPMTAG_PREFIXS 1098
  43. #define RPMTAG_DIRINDEXES 1116
  44. #define RPMTAG_BASENAMES 1117
  45. #define RPMTAG_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_getstring(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. {
  77. int opt = 0, func = 0, rpm_fd, offset;
  78. while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
  79. switch (opt) {
  80. case 'i': // First arg: Install mode, with q: Information
  81. if (!func) func |= rpm_install;
  82. else func |= rpm_query_info;
  83. break;
  84. case 'q': // First arg: Query mode
  85. if (!func) func |= rpm_query;
  86. else bb_show_usage();
  87. break;
  88. case 'p': // Query a package
  89. func |= rpm_query_package;
  90. break;
  91. case 'l': // List files in a package
  92. func |= rpm_query_list;
  93. break;
  94. case 'd': // List doc files in a package (implies list)
  95. func |= rpm_query_list;
  96. func |= rpm_query_list_doc;
  97. break;
  98. case 'c': // List config files in a package (implies list)
  99. func |= rpm_query_list;
  100. func |= rpm_query_list_config;
  101. break;
  102. default:
  103. bb_show_usage();
  104. }
  105. }
  106. if (optind == argc) bb_show_usage();
  107. while (optind < argc) {
  108. rpm_fd = xopen(argv[optind], O_RDONLY);
  109. mytags = rpm_gettags(rpm_fd, (int *) &tagcount);
  110. offset = lseek(rpm_fd, 0, SEEK_CUR);
  111. if (!mytags) { printf("Error reading rpm header\n"); exit(-1); }
  112. map = mmap(0, offset > getpagesize() ? (offset + offset % getpagesize()) : getpagesize(), PROT_READ, MAP_PRIVATE, rpm_fd, 0); // Mimimum is one page
  113. if (func & rpm_install) {
  114. loop_through_files(RPMTAG_BASENAMES, fileaction_dobackup); /* Backup any config files */
  115. extract_cpio_gz(rpm_fd); // Extact the archive
  116. loop_through_files(RPMTAG_BASENAMES, fileaction_setowngrp); /* Set the correct file uid/gid's */
  117. } else if (func & rpm_query && func & rpm_query_package) {
  118. if (!((func & rpm_query_info) || (func & rpm_query_list))) { // If just a straight query, just give package name
  119. printf("%s-%s-%s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_RELEASE, 0));
  120. }
  121. if (func & rpm_query_info) {
  122. /* Do the nice printout */
  123. time_t bdate_time;
  124. struct tm *bdate;
  125. char bdatestring[50];
  126. printf("Name : %-29sRelocations: %s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_PREFIXS, 0) ? rpm_getstring(RPMTAG_PREFIXS, 0) : "(not relocateable)");
  127. printf("Version : %-34sVendor: %s\n", rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_VENDOR, 0) ? rpm_getstring(RPMTAG_VENDOR, 0) : "(none)");
  128. bdate_time = rpm_getint(RPMTAG_BUILDTIME, 0);
  129. bdate = localtime((time_t *) &bdate_time);
  130. strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate);
  131. printf("Release : %-30sBuild Date: %s\n", rpm_getstring(RPMTAG_RELEASE, 0), bdatestring);
  132. printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstring(RPMTAG_BUILDHOST, 0));
  133. printf("Group : %-30sSource RPM: %s\n", rpm_getstring(RPMTAG_GROUP, 0), rpm_getstring(RPMTAG_SOURCERPM, 0));
  134. printf("Size : %-33dLicense: %s\n", rpm_getint(RPMTAG_SIZE, 0), rpm_getstring(RPMTAG_LICENSE, 0));
  135. printf("URL : %s\n", rpm_getstring(RPMTAG_URL, 0));
  136. printf("Summary : %s\n", rpm_getstring(RPMTAG_SUMMARY, 0));
  137. printf("Description :\n%s\n", rpm_getstring(RPMTAG_DESCRIPTION, 0));
  138. }
  139. if (func & rpm_query_list) {
  140. int count, it, flags;
  141. count = rpm_getcount(RPMTAG_BASENAMES);
  142. for (it = 0; it < count; it++) {
  143. flags = rpm_getint(RPMTAG_FILEFLAGS, it);
  144. switch ((func & rpm_query_list_doc) + (func & rpm_query_list_config))
  145. {
  146. case rpm_query_list_doc: if (!(flags & RPMFILE_DOC)) continue; break;
  147. case rpm_query_list_config: if (!(flags & RPMFILE_CONFIG)) continue; break;
  148. case rpm_query_list_doc + rpm_query_list_config: if (!((flags & RPMFILE_CONFIG) || (flags & RPMFILE_DOC))) continue; break;
  149. }
  150. printf("%s%s\n", rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, it)), rpm_getstring(RPMTAG_BASENAMES, it));
  151. }
  152. }
  153. }
  154. optind++;
  155. free (mytags);
  156. }
  157. return 0;
  158. }
  159. static void extract_cpio_gz(int fd) {
  160. archive_handle_t *archive_handle;
  161. unsigned char magic[2];
  162. /* Initialise */
  163. archive_handle = init_handle();
  164. archive_handle->seek = seek_by_read;
  165. //archive_handle->action_header = header_list;
  166. archive_handle->action_data = data_extract_all;
  167. archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
  168. archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
  169. archive_handle->src_fd = fd;
  170. archive_handle->offset = 0;
  171. xread(archive_handle->src_fd, &magic, 2);
  172. if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
  173. bb_error_msg_and_die("invalid gzip magic");
  174. }
  175. check_header_gzip(archive_handle->src_fd);
  176. xchdir("/"); // Install RPM's to root
  177. archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip);
  178. archive_handle->offset = 0;
  179. while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
  180. }
  181. static rpm_index **rpm_gettags(int fd, int *num_tags)
  182. {
  183. rpm_index **tags = xzalloc(200 * sizeof(struct rpmtag *)); /* We should never need mode than 200, and realloc later */
  184. int pass, tagindex = 0;
  185. lseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
  186. for (pass = 0; pass < 2; pass++) { /* 1st pass is the signature headers, 2nd is the main stuff */
  187. struct {
  188. char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
  189. uint8_t version; /* 1 byte version number */
  190. uint32_t reserved; /* 4 bytes reserved */
  191. uint32_t entries; /* Number of entries in header (4 bytes) */
  192. uint32_t size; /* Size of store (4 bytes) */
  193. } header;
  194. rpm_index *tmpindex;
  195. int storepos;
  196. read(fd, &header, sizeof(header));
  197. if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) return NULL; /* Invalid magic */
  198. if (header.version != 1) return NULL; /* This program only supports v1 headers */
  199. header.size = ntohl(header.size);
  200. header.entries = ntohl(header.entries);
  201. storepos = lseek(fd,0,SEEK_CUR) + header.entries * 16;
  202. while (header.entries--) {
  203. tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index));
  204. read(fd, tmpindex, sizeof(rpm_index));
  205. tmpindex->tag = ntohl(tmpindex->tag); tmpindex->type = ntohl(tmpindex->type); tmpindex->count = ntohl(tmpindex->count);
  206. tmpindex->offset = storepos + ntohl(tmpindex->offset);
  207. if (pass==0) tmpindex->tag -= 743;
  208. }
  209. lseek(fd, header.size, SEEK_CUR); /* Seek past store */
  210. if (pass==0) lseek(fd, (8 - (lseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR); /* Skip padding to 8 byte boundary after reading signature headers */
  211. }
  212. tags = realloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */
  213. *num_tags = tagindex;
  214. return tags; /* All done, leave the file at the start of the gzipped cpio archive */
  215. }
  216. static int bsearch_rpmtag(const void *key, const void *item)
  217. {
  218. int *tag = (int *)key;
  219. rpm_index **tmp = (rpm_index **) item;
  220. return (*tag - tmp[0]->tag);
  221. }
  222. static int rpm_getcount(int tag)
  223. {
  224. rpm_index **found;
  225. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  226. if (!found) return 0;
  227. else return found[0]->count;
  228. }
  229. static char *rpm_getstring(int tag, int itemindex)
  230. {
  231. rpm_index **found;
  232. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  233. if (!found || itemindex >= found[0]->count) return NULL;
  234. if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) {
  235. int n;
  236. char *tmpstr = (char *) (map + found[0]->offset);
  237. for (n=0; n < itemindex; n++) tmpstr = tmpstr + strlen(tmpstr) + 1;
  238. return tmpstr;
  239. } else return NULL;
  240. }
  241. static int rpm_getint(int tag, int itemindex)
  242. {
  243. rpm_index **found;
  244. int n, *tmpint;
  245. /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
  246. * it's ok to ignore it because tag won't be used as a pointer */
  247. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  248. if (!found || itemindex >= found[0]->count) return -1;
  249. tmpint = (int *) (map + found[0]->offset);
  250. if (found[0]->type == RPM_INT32_TYPE) {
  251. for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 4);
  252. return ntohl(*tmpint);
  253. } else if (found[0]->type == RPM_INT16_TYPE) {
  254. for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 2);
  255. return ntohs(*tmpint);
  256. } else if (found[0]->type == RPM_INT8_TYPE) {
  257. for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 1);
  258. return ntohs(*tmpint);
  259. } else return -1;
  260. }
  261. static void fileaction_dobackup(char *filename, int fileref)
  262. {
  263. struct stat oldfile;
  264. int stat_res;
  265. char *newname;
  266. if (rpm_getint(RPMTAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { /* Only need to backup config files */
  267. stat_res = lstat (filename, &oldfile);
  268. if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { /* File already exists - really should check MD5's etc to see if different */
  269. newname = xasprintf("%s.rpmorig", filename);
  270. copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
  271. remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
  272. free(newname);
  273. }
  274. }
  275. }
  276. static void fileaction_setowngrp(char *filename, int fileref)
  277. {
  278. int uid, gid;
  279. uid = bb_xgetpwnam(rpm_getstring(RPMTAG_FILEUSERNAME, fileref));
  280. gid = bb_xgetgrnam(rpm_getstring(RPMTAG_FILEGROUPNAME, fileref));
  281. chown(filename, uid, gid);
  282. }
  283. static void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
  284. {
  285. int count = 0;
  286. while (rpm_getstring(filetag, count)) {
  287. char * filename = xasprintf("%s%s",
  288. rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES,
  289. count)), rpm_getstring(RPMTAG_BASENAMES, count));
  290. fileaction(filename, count++);
  291. free(filename);
  292. }
  293. }