info.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * dpkg-split - splitting and joining of multipart *.deb archives
  3. * info.c - information about split archives
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #include <limits.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. //#include <ar.h>
  29. #include <inttypes.h>
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <dpkg/i18n.h>
  34. #include <dpkg/c-ctype.h>
  35. #include <dpkg/dpkg.h>
  36. #include <dpkg/dpkg-db.h>
  37. #include <dpkg/fdio.h>
  38. #include <dpkg/ar.h>
  39. #include <dpkg/options.h>
  40. #include "dpkg-split.h"
  41. static intmax_t
  42. parse_intmax(const char *value, const char *fn, const char *what)
  43. {
  44. intmax_t r;
  45. char *endp;
  46. errno = 0;
  47. r = strtoimax(value, &endp, 10);
  48. if (value == endp || *endp)
  49. ohshit(_("file '%.250s' is corrupt - bad digit (code %d) in %s"),
  50. fn, *endp, what);
  51. if (r < 0 || errno == ERANGE)
  52. ohshit(_("file '%s' is corrupt; out of range integer in %s"), fn, what);
  53. return r;
  54. }
  55. static char *nextline(char **ripp, const char *fn, const char *what) {
  56. char *newline, *rip;
  57. rip= *ripp;
  58. if (!rip)
  59. ohshit(_("file '%.250s' is corrupt - %.250s missing"), fn, what);
  60. newline= strchr(rip,'\n');
  61. if (!newline)
  62. ohshit(_("file '%.250s' is corrupt - missing newline after %.250s"),
  63. fn, what);
  64. *ripp= newline+1;
  65. while (newline > rip && c_isspace(newline[-1]))
  66. newline--;
  67. *newline = '\0';
  68. return rip;
  69. }
  70. /**
  71. * Read a deb-split part archive.
  72. *
  73. * @return Part info (nfmalloc'd) if was an archive part and we read it,
  74. * NULL if it wasn't.
  75. */
  76. struct partinfo *
  77. read_info(struct dpkg_ar *ar, struct partinfo *ir)
  78. {
  79. static char *readinfobuf= NULL;
  80. static size_t readinfobuflen= 0;
  81. size_t thisilen;
  82. intmax_t templong;
  83. char magicbuf[sizeof(DPKG_AR_MAGIC) - 1], *rip, *partnums, *slash;
  84. const char *err;
  85. struct dpkg_ar_hdr arh;
  86. ssize_t rc;
  87. rc = fd_read(ar->fd, magicbuf, sizeof(magicbuf));
  88. if (rc != sizeof(magicbuf)) {
  89. if (rc < 0)
  90. ohshite(_("error reading %.250s"), ar->name);
  91. else
  92. return NULL;
  93. }
  94. if (memcmp(magicbuf, DPKG_AR_MAGIC, sizeof(magicbuf)))
  95. return NULL;
  96. rc = fd_read(ar->fd, &arh, sizeof(arh));
  97. if (rc != sizeof(arh))
  98. read_fail(rc, ar->name, "ar header");
  99. dpkg_ar_normalize_name(&arh);
  100. if (strncmp(arh.ar_name, PARTMAGIC, sizeof(arh.ar_name)) != 0)
  101. return NULL;
  102. if (dpkg_ar_member_is_illegal(&arh))
  103. ohshit(_("file '%.250s' is corrupt - bad magic at end of first header"),
  104. ar->name);
  105. thisilen = dpkg_ar_member_get_size(ar, &arh);
  106. if (thisilen >= readinfobuflen) {
  107. readinfobuflen = thisilen + 2;
  108. readinfobuf= m_realloc(readinfobuf,readinfobuflen);
  109. }
  110. rc = fd_read(ar->fd, readinfobuf, thisilen + (thisilen & 1));
  111. if (rc != (ssize_t)(thisilen + (thisilen & 1)))
  112. read_fail(rc, ar->name, "reading header member");
  113. if (thisilen & 1) {
  114. int c = readinfobuf[thisilen + 1];
  115. if (c != '\n')
  116. ohshit(_("file '%.250s' is corrupt - bad padding character (code %d)"),
  117. ar->name, c);
  118. }
  119. readinfobuf[thisilen] = '\0';
  120. if (memchr(readinfobuf,0,thisilen))
  121. ohshit(_("file '%.250s' is corrupt - nulls in info section"), ar->name);
  122. ir->filename = ar->name;
  123. rip= readinfobuf;
  124. err = deb_version_parse(&ir->fmtversion,
  125. nextline(&rip, ar->name, _("format version number")));
  126. if (err)
  127. ohshit(_("file '%.250s' has invalid format version: %s"), ar->name, err);
  128. if (ir->fmtversion.major != 2)
  129. ohshit(_("file '%.250s' is format version %d.%d; get a newer dpkg-split"),
  130. ar->name, ir->fmtversion.major, ir->fmtversion.minor);
  131. ir->package = nfstrsave(nextline(&rip, ar->name, _("package name")));
  132. ir->version = nfstrsave(nextline(&rip, ar->name, _("package version number")));
  133. ir->md5sum = nfstrsave(nextline(&rip, ar->name, _("package file MD5 checksum")));
  134. if (strlen(ir->md5sum) != MD5HASHLEN ||
  135. strspn(ir->md5sum, "0123456789abcdef") != MD5HASHLEN)
  136. ohshit(_("file '%.250s' is corrupt - bad MD5 checksum '%.250s'"),
  137. ar->name, ir->md5sum);
  138. ir->orglength = parse_intmax(nextline(&rip, ar->name, _("archive total size")),
  139. ar->name, _("archive total size"));
  140. ir->maxpartlen = parse_intmax(nextline(&rip, ar->name, _("archive part offset")),
  141. ar->name, _("archive part offset"));
  142. partnums = nextline(&rip, ar->name, _("archive part numbers"));
  143. slash= strchr(partnums,'/');
  144. if (!slash)
  145. ohshit(_("file '%.250s' is corrupt - no slash between archive part numbers"), ar->name);
  146. *slash++ = '\0';
  147. templong = parse_intmax(slash, ar->name, _("number of archive parts"));
  148. if (templong <= 0 || templong > INT_MAX)
  149. ohshit(_("file '%.250s' is corrupt - bad number of archive parts"), ar->name);
  150. ir->maxpartn= templong;
  151. templong = parse_intmax(partnums, ar->name, _("archive parts number"));
  152. if (templong <= 0 || templong > ir->maxpartn)
  153. ohshit(_("file '%.250s' is corrupt - bad archive part number"), ar->name);
  154. ir->thispartn= templong;
  155. /* If the package was created with dpkg 1.16.1 or later it will include
  156. * the architecture. */
  157. if (*rip != '\0')
  158. ir->arch = nfstrsave(nextline(&rip, ar->name, _("package architecture")));
  159. else
  160. ir->arch = NULL;
  161. rc = fd_read(ar->fd, &arh, sizeof(arh));
  162. if (rc != sizeof(arh))
  163. read_fail(rc, ar->name, "reading data part member ar header");
  164. dpkg_ar_normalize_name(&arh);
  165. if (dpkg_ar_member_is_illegal(&arh))
  166. ohshit(_("file '%.250s' is corrupt - bad magic at end of second header"),
  167. ar->name);
  168. if (strncmp(arh.ar_name,"data",4))
  169. ohshit(_("file '%.250s' is corrupt - second member is not data member"),
  170. ar->name);
  171. ir->thispartlen = dpkg_ar_member_get_size(ar, &arh);
  172. ir->thispartoffset= (ir->thispartn-1)*ir->maxpartlen;
  173. if (ir->maxpartn != (ir->orglength+ir->maxpartlen-1)/ir->maxpartlen)
  174. ohshit(_("file '%.250s' is corrupt - wrong number of parts for quoted sizes"),
  175. ar->name);
  176. if (ir->thispartlen !=
  177. (ir->thispartn == ir->maxpartn
  178. ? ir->orglength - ir->thispartoffset : ir->maxpartlen))
  179. ohshit(_("file '%.250s' is corrupt - size is wrong for quoted part number"),
  180. ar->name);
  181. ir->filesize = (strlen(DPKG_AR_MAGIC) +
  182. sizeof(arh) + thisilen + (thisilen & 1) +
  183. sizeof(arh) + ir->thispartlen + (ir->thispartlen & 1));
  184. if (S_ISREG(ar->mode)) {
  185. /* Don't do this check if it's coming from a pipe or something. It's
  186. * only an extra sanity check anyway. */
  187. if (ar->size < ir->filesize)
  188. ohshit(_("file '%.250s' is corrupt - too short"), ar->name);
  189. }
  190. ir->headerlen = strlen(DPKG_AR_MAGIC) +
  191. sizeof(arh) + thisilen + (thisilen & 1) + sizeof(arh);
  192. return ir;
  193. }
  194. void mustgetpartinfo(const char *filename, struct partinfo *ri) {
  195. struct dpkg_ar *part;
  196. part = dpkg_ar_open(filename);
  197. if (!part)
  198. ohshite(_("cannot open archive part file '%.250s'"), filename);
  199. if (!read_info(part, ri))
  200. ohshite(_("file '%.250s' is not an archive part"), filename);
  201. dpkg_ar_close(part);
  202. }
  203. void print_info(const struct partinfo *pi) {
  204. printf(_("%s:\n"
  205. " Part format version: %d.%d\n"
  206. " Part of package: %s\n"
  207. " ... version: %s\n"
  208. " ... architecture: %s\n"
  209. " ... MD5 checksum: %s\n"
  210. " ... length: %jd bytes\n"
  211. " ... split every: %jd bytes\n"
  212. " Part number: %d/%d\n"
  213. " Part length: %jd bytes\n"
  214. " Part offset: %jd bytes\n"
  215. " Part file size (used portion): %jd bytes\n\n"),
  216. pi->filename,
  217. pi->fmtversion.major, pi->fmtversion.minor,
  218. pi->package,
  219. pi->version,
  220. pi->arch ? pi->arch : C_("architecture", "<unknown>"),
  221. pi->md5sum,
  222. (intmax_t)pi->orglength,
  223. (intmax_t)pi->maxpartlen,
  224. pi->thispartn,
  225. pi->maxpartn,
  226. (intmax_t)pi->thispartlen,
  227. (intmax_t)pi->thispartoffset,
  228. (intmax_t)pi->filesize);
  229. }
  230. int
  231. do_info(const char *const *argv)
  232. {
  233. const char *thisarg;
  234. struct partinfo *pi, ps;
  235. struct dpkg_ar *part;
  236. if (!*argv)
  237. badusage(_("--%s requires one or more part file arguments"),
  238. cipaction->olong);
  239. while ((thisarg= *argv++)) {
  240. part = dpkg_ar_open(thisarg);
  241. if (!part)
  242. ohshite(_("cannot open archive part file '%.250s'"), thisarg);
  243. pi = read_info(part, &ps);
  244. dpkg_ar_close(part);
  245. if (pi) {
  246. print_info(pi);
  247. } else {
  248. printf(_("file '%s' is not an archive part\n"), thisarg);
  249. }
  250. m_output(stdout, _("<standard output>"));
  251. }
  252. return 0;
  253. }