rpm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <stdio.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include <stdlib.h>
  13. #include <fcntl.h>
  14. #include <netinet/in.h> /* For ntohl & htonl function */
  15. #include <string.h> /* For strncmp */
  16. #include <sys/mman.h> /* For mmap */
  17. #include <time.h> /* For ctime */
  18. #include "busybox.h"
  19. #include "unarchive.h"
  20. #define RPM_HEADER_MAGIC "\216\255\350"
  21. #define RPM_CHAR_TYPE 1
  22. #define RPM_INT8_TYPE 2
  23. #define RPM_INT16_TYPE 3
  24. #define RPM_INT32_TYPE 4
  25. /* #define RPM_INT64_TYPE 5 ---- These aren't supported (yet) */
  26. #define RPM_STRING_TYPE 6
  27. #define RPM_BIN_TYPE 7
  28. #define RPM_STRING_ARRAY_TYPE 8
  29. #define RPM_I18NSTRING_TYPE 9
  30. #define RPMTAG_NAME 1000
  31. #define RPMTAG_VERSION 1001
  32. #define RPMTAG_RELEASE 1002
  33. #define RPMTAG_SUMMARY 1004
  34. #define RPMTAG_DESCRIPTION 1005
  35. #define RPMTAG_BUILDTIME 1006
  36. #define RPMTAG_BUILDHOST 1007
  37. #define RPMTAG_SIZE 1009
  38. #define RPMTAG_VENDOR 1011
  39. #define RPMTAG_LICENSE 1014
  40. #define RPMTAG_PACKAGER 1015
  41. #define RPMTAG_GROUP 1016
  42. #define RPMTAG_URL 1020
  43. #define RPMTAG_PREIN 1023
  44. #define RPMTAG_POSTIN 1024
  45. #define RPMTAG_FILEFLAGS 1037
  46. #define RPMTAG_FILEUSERNAME 1039
  47. #define RPMTAG_FILEGROUPNAME 1040
  48. #define RPMTAG_SOURCERPM 1044
  49. #define RPMTAG_PREINPROG 1085
  50. #define RPMTAG_POSTINPROG 1086
  51. #define RPMTAG_PREFIXS 1098
  52. #define RPMTAG_DIRINDEXES 1116
  53. #define RPMTAG_BASENAMES 1117
  54. #define RPMTAG_DIRNAMES 1118
  55. #define RPMFILE_CONFIG (1 << 0)
  56. #define RPMFILE_DOC (1 << 1)
  57. enum rpm_functions_e {
  58. rpm_query = 1,
  59. rpm_install = 2,
  60. rpm_query_info = 4,
  61. rpm_query_package = 8,
  62. rpm_query_list = 16,
  63. rpm_query_list_doc = 32,
  64. rpm_query_list_config = 64
  65. };
  66. typedef struct {
  67. uint32_t tag; /* 4 byte tag */
  68. uint32_t type; /* 4 byte type */
  69. uint32_t offset; /* 4 byte offset */
  70. uint32_t count; /* 4 byte count */
  71. } rpm_index;
  72. static void *map;
  73. static rpm_index **mytags;
  74. static int tagcount;
  75. void extract_cpio_gz(int fd);
  76. rpm_index **rpm_gettags(int fd, int *num_tags);
  77. int bsearch_rpmtag(const void *key, const void *item);
  78. char *rpm_getstring(int tag, int itemindex);
  79. int rpm_getint(int tag, int itemindex);
  80. int rpm_getcount(int tag);
  81. void exec_script(int progtag, int datatag, char *prefix);
  82. void fileaction_dobackup(char *filename, int fileref);
  83. void fileaction_setowngrp(char *filename, int fileref);
  84. void fileaction_list(char *filename, int itemno);
  85. void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref));
  86. int rpm_main(int argc, char **argv)
  87. {
  88. int opt = 0, func = 0, rpm_fd, offset;
  89. while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
  90. switch (opt) {
  91. case 'i': // First arg: Install mode, with q: Information
  92. if (!func) func |= rpm_install;
  93. else func |= rpm_query_info;
  94. break;
  95. case 'q': // First arg: Query mode
  96. if (!func) func |= rpm_query;
  97. else bb_show_usage();
  98. break;
  99. case 'p': // Query a package
  100. func |= rpm_query_package;
  101. break;
  102. case 'l': // List files in a package
  103. func |= rpm_query_list;
  104. break;
  105. case 'd': // List doc files in a package (implies list)
  106. func |= rpm_query_list;
  107. func |= rpm_query_list_doc;
  108. break;
  109. case 'c': // List config files in a package (implies list)
  110. func |= rpm_query_list;
  111. func |= rpm_query_list_config;
  112. break;
  113. default:
  114. bb_show_usage();
  115. }
  116. }
  117. if (optind == argc) bb_show_usage();
  118. while (optind < argc) {
  119. rpm_fd = bb_xopen(argv[optind], O_RDONLY);
  120. mytags = rpm_gettags(rpm_fd, (int *) &tagcount);
  121. offset = lseek(rpm_fd, 0, SEEK_CUR);
  122. if (!mytags) { printf("Error reading rpm header\n"); exit(-1); }
  123. map = mmap(0, offset > getpagesize() ? (offset + offset % getpagesize()) : getpagesize(), PROT_READ, MAP_PRIVATE, rpm_fd, 0); // Mimimum is one page
  124. if (func & rpm_install) {
  125. loop_through_files(RPMTAG_BASENAMES, fileaction_dobackup); /* Backup any config files */
  126. extract_cpio_gz(rpm_fd); // Extact the archive
  127. loop_through_files(RPMTAG_BASENAMES, fileaction_setowngrp); /* Set the correct file uid/gid's */
  128. } else if (func & rpm_query && func & rpm_query_package) {
  129. if (!((func & rpm_query_info) || (func & rpm_query_list))) { // If just a straight query, just give package name
  130. printf("%s-%s-%s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_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_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_PREFIXS, 0) ? rpm_getstring(RPMTAG_PREFIXS, 0) : "(not relocateable)");
  138. printf("Version : %-34sVendor: %s\n", rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_VENDOR, 0) ? rpm_getstring(RPMTAG_VENDOR, 0) : "(none)");
  139. bdate_time = rpm_getint(RPMTAG_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_getstring(RPMTAG_RELEASE, 0), bdatestring);
  143. printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstring(RPMTAG_BUILDHOST, 0));
  144. printf("Group : %-30sSource RPM: %s\n", rpm_getstring(RPMTAG_GROUP, 0), rpm_getstring(RPMTAG_SOURCERPM, 0));
  145. printf("Size : %-33dLicense: %s\n", rpm_getint(RPMTAG_SIZE, 0), rpm_getstring(RPMTAG_LICENSE, 0));
  146. printf("URL : %s\n", rpm_getstring(RPMTAG_URL, 0));
  147. printf("Summary : %s\n", rpm_getstring(RPMTAG_SUMMARY, 0));
  148. printf("Description :\n%s\n", rpm_getstring(RPMTAG_DESCRIPTION, 0));
  149. }
  150. if (func & rpm_query_list) {
  151. int count, it, flags;
  152. count = rpm_getcount(RPMTAG_BASENAMES);
  153. for (it = 0; it < count; it++) {
  154. flags = rpm_getint(RPMTAG_FILEFLAGS, it);
  155. switch ((func & rpm_query_list_doc) + (func & rpm_query_list_config))
  156. {
  157. case rpm_query_list_doc: if (!(flags & RPMFILE_DOC)) continue; break;
  158. case rpm_query_list_config: if (!(flags & RPMFILE_CONFIG)) continue; break;
  159. case rpm_query_list_doc + rpm_query_list_config: if (!((flags & RPMFILE_CONFIG) || (flags & RPMFILE_DOC))) continue; break;
  160. }
  161. printf("%s%s\n", rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, it)), rpm_getstring(RPMTAG_BASENAMES, it));
  162. }
  163. }
  164. }
  165. optind++;
  166. free (mytags);
  167. }
  168. return 0;
  169. }
  170. void extract_cpio_gz(int fd) {
  171. archive_handle_t *archive_handle;
  172. unsigned char magic[2];
  173. /* Initialise */
  174. archive_handle = init_handle();
  175. archive_handle->seek = seek_by_char;
  176. //archive_handle->action_header = header_list;
  177. archive_handle->action_data = data_extract_all;
  178. archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
  179. archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
  180. archive_handle->src_fd = fd;
  181. archive_handle->offset = 0;
  182. bb_xread_all(archive_handle->src_fd, &magic, 2);
  183. if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
  184. bb_error_msg_and_die("Invalid gzip magic");
  185. }
  186. check_header_gzip(archive_handle->src_fd);
  187. chdir("/"); // Install RPM's to root
  188. archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip);
  189. archive_handle->offset = 0;
  190. while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
  191. }
  192. rpm_index **rpm_gettags(int fd, int *num_tags)
  193. {
  194. rpm_index **tags = calloc(200, sizeof(struct rpmtag *)); /* We should never need mode than 200, and realloc later */
  195. int pass, tagindex = 0;
  196. lseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
  197. for (pass = 0; pass < 2; pass++) { /* 1st pass is the signature headers, 2nd is the main stuff */
  198. struct {
  199. char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
  200. uint8_t version; /* 1 byte version number */
  201. uint32_t reserved; /* 4 bytes reserved */
  202. uint32_t entries; /* Number of entries in header (4 bytes) */
  203. uint32_t size; /* Size of store (4 bytes) */
  204. } header;
  205. rpm_index *tmpindex;
  206. int storepos;
  207. read(fd, &header, sizeof(header));
  208. if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) return NULL; /* Invalid magic */
  209. if (header.version != 1) return NULL; /* This program only supports v1 headers */
  210. header.size = ntohl(header.size);
  211. header.entries = ntohl(header.entries);
  212. storepos = lseek(fd,0,SEEK_CUR) + header.entries * 16;
  213. while (header.entries--) {
  214. tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index));
  215. read(fd, tmpindex, sizeof(rpm_index));
  216. tmpindex->tag = ntohl(tmpindex->tag); tmpindex->type = ntohl(tmpindex->type); tmpindex->count = ntohl(tmpindex->count);
  217. tmpindex->offset = storepos + ntohl(tmpindex->offset);
  218. if (pass==0) tmpindex->tag -= 743;
  219. }
  220. lseek(fd, header.size, SEEK_CUR); /* Seek past store */
  221. 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 */
  222. }
  223. tags = realloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */
  224. *num_tags = tagindex;
  225. return tags; /* All done, leave the file at the start of the gzipped cpio archive */
  226. }
  227. int bsearch_rpmtag(const void *key, const void *item)
  228. {
  229. int *tag = (int *)key;
  230. rpm_index **tmp = (rpm_index **) item;
  231. return (*tag - tmp[0]->tag);
  232. }
  233. int rpm_getcount(int tag)
  234. {
  235. rpm_index **found;
  236. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  237. if (!found) return 0;
  238. else return found[0]->count;
  239. }
  240. char *rpm_getstring(int tag, int itemindex)
  241. {
  242. rpm_index **found;
  243. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  244. if (!found || itemindex >= found[0]->count) return NULL;
  245. if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) {
  246. int n;
  247. char *tmpstr = (char *) (map + found[0]->offset);
  248. for (n=0; n < itemindex; n++) tmpstr = tmpstr + strlen(tmpstr) + 1;
  249. return tmpstr;
  250. } else return NULL;
  251. }
  252. int rpm_getint(int tag, int itemindex)
  253. {
  254. rpm_index **found;
  255. int n, *tmpint;
  256. /* gcc throws warnings here when sizeof(void*)!=sizeof(int) ...
  257. * it's ok to ignore it because tag won't be used as a pointer */
  258. found = bsearch(&tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
  259. if (!found || itemindex >= found[0]->count) return -1;
  260. tmpint = (int *) (map + found[0]->offset);
  261. if (found[0]->type == RPM_INT32_TYPE) {
  262. for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 4);
  263. return ntohl(*tmpint);
  264. } else if (found[0]->type == RPM_INT16_TYPE) {
  265. for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 2);
  266. return ntohs(*tmpint);
  267. } else if (found[0]->type == RPM_INT8_TYPE) {
  268. for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 1);
  269. return ntohs(*tmpint);
  270. } else return -1;
  271. }
  272. void fileaction_dobackup(char *filename, int fileref)
  273. {
  274. struct stat oldfile;
  275. int stat_res;
  276. char *newname;
  277. if (rpm_getint(RPMTAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { /* Only need to backup config files */
  278. stat_res = lstat (filename, &oldfile);
  279. if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { /* File already exists - really should check MD5's etc to see if different */
  280. newname = bb_xstrdup(filename);
  281. newname = strcat(newname, ".rpmorig");
  282. copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
  283. remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
  284. free(newname);
  285. }
  286. }
  287. }
  288. void fileaction_setowngrp(char *filename, int fileref)
  289. {
  290. int uid, gid;
  291. uid = bb_xgetpwnam(rpm_getstring(RPMTAG_FILEUSERNAME, fileref));
  292. gid = bb_xgetgrnam(rpm_getstring(RPMTAG_FILEGROUPNAME, fileref));
  293. chown (filename, uid, gid);
  294. }
  295. void fileaction_list(char *filename, int ATTRIBUTE_UNUSED fileref)
  296. {
  297. printf("%s\n", filename);
  298. }
  299. void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
  300. {
  301. int count = 0;
  302. char *filename, *tmp_dirname, *tmp_basename;
  303. while (rpm_getstring(filetag, count)) {
  304. tmp_dirname = rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, count)); /* 1st put on the directory */
  305. tmp_basename = rpm_getstring(RPMTAG_BASENAMES, count);
  306. filename = xmalloc(strlen(tmp_basename) + strlen(tmp_dirname) + 1);
  307. strcpy(filename, tmp_dirname); /* First the directory name */
  308. strcat(filename, tmp_basename); /* then the filename */
  309. fileaction(filename, count++);
  310. free(filename);
  311. }
  312. }