info.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/ar.h>
  38. #include <dpkg/options.h>
  39. #include "dpkg-split.h"
  40. static intmax_t
  41. parse_intmax(const char *value, const char *fn, const char *what)
  42. {
  43. intmax_t r;
  44. char *endp;
  45. errno = 0;
  46. r = strtoimax(value, &endp, 10);
  47. if (value == endp || *endp)
  48. ohshit(_("file '%.250s' is corrupt - bad digit (code %d) in %s"),
  49. fn, *endp, what);
  50. if (r < 0 || errno == ERANGE)
  51. ohshit(_("file '%s' is corrupt; out of range integer in %s"), fn, what);
  52. return r;
  53. }
  54. static char *nextline(char **ripp, const char *fn, const char *what) {
  55. char *newline, *rip;
  56. rip= *ripp;
  57. if (!rip)
  58. ohshit(_("file '%.250s' is corrupt - %.250s missing"), fn, what);
  59. newline= strchr(rip,'\n');
  60. if (!newline)
  61. ohshit(_("file '%.250s' is corrupt - missing newline after %.250s"),
  62. fn, what);
  63. *ripp= newline+1;
  64. while (newline > rip && c_isspace(newline[-1]))
  65. newline--;
  66. *newline = '\0';
  67. return rip;
  68. }
  69. /**
  70. * Read a deb-split part archive.
  71. *
  72. * @return Part info (nfmalloc'd) if was an archive part and we read it,
  73. * NULL if it wasn't.
  74. */
  75. struct partinfo *read_info(FILE *partfile, const char *fn, struct partinfo *ir) {
  76. static char *readinfobuf= NULL;
  77. static size_t readinfobuflen= 0;
  78. size_t thisilen;
  79. intmax_t templong;
  80. char magicbuf[sizeof(DPKG_AR_MAGIC) - 1], *rip, *partnums, *slash;
  81. const char *err;
  82. struct ar_hdr arh;
  83. int c;
  84. struct stat stab;
  85. if (fread(magicbuf, 1, sizeof(magicbuf), partfile) != sizeof(magicbuf)) {
  86. if (ferror(partfile))
  87. ohshite(_("error reading %.250s"), fn);
  88. else
  89. return NULL;
  90. }
  91. if (memcmp(magicbuf, DPKG_AR_MAGIC, sizeof(magicbuf)))
  92. return NULL;
  93. if (fread(&arh,1,sizeof(arh),partfile) != sizeof(arh)) rerreof(partfile,fn);
  94. dpkg_ar_normalize_name(&arh);
  95. if (strncmp(arh.ar_name, PARTMAGIC, sizeof(arh.ar_name)) != 0)
  96. return NULL;
  97. if (dpkg_ar_member_is_illegal(&arh))
  98. ohshit(_("file '%.250s' is corrupt - bad magic at end of first header"), fn);
  99. thisilen = dpkg_ar_member_get_size(fn, &arh);
  100. if (thisilen >= readinfobuflen) {
  101. readinfobuflen= thisilen+1;
  102. readinfobuf= m_realloc(readinfobuf,readinfobuflen);
  103. }
  104. if (fread(readinfobuf,1,thisilen,partfile) != thisilen) rerreof(partfile,fn);
  105. if (thisilen & 1) {
  106. c= getc(partfile); if (c==EOF) rerreof(partfile,fn);
  107. if (c != '\n')
  108. ohshit(_("file '%.250s' is corrupt - bad padding character (code %d)"),
  109. fn, c);
  110. }
  111. readinfobuf[thisilen] = '\0';
  112. if (memchr(readinfobuf,0,thisilen))
  113. ohshit(_("file '%.250s' is corrupt - nulls in info section"), fn);
  114. ir->filename= fn;
  115. rip= readinfobuf;
  116. err = deb_version_parse(&ir->fmtversion,
  117. nextline(&rip, fn, _("format version number")));
  118. if (err)
  119. ohshit(_("file '%.250s' has invalid format version: %s"), fn, err);
  120. if (ir->fmtversion.major != 2)
  121. ohshit(_("file '%.250s' is format version %d.%d; get a newer dpkg-split"),
  122. fn, ir->fmtversion.major, ir->fmtversion.minor);
  123. ir->package = nfstrsave(nextline(&rip, fn, _("package name")));
  124. ir->version = nfstrsave(nextline(&rip, fn, _("package version number")));
  125. ir->md5sum = nfstrsave(nextline(&rip, fn, _("package file MD5 checksum")));
  126. if (strlen(ir->md5sum) != MD5HASHLEN ||
  127. strspn(ir->md5sum, "0123456789abcdef") != MD5HASHLEN)
  128. ohshit(_("file '%.250s' is corrupt - bad MD5 checksum '%.250s'"),
  129. fn, ir->md5sum);
  130. ir->orglength = parse_intmax(nextline(&rip, fn, _("archive total size")),
  131. fn, _("archive total size"));
  132. ir->maxpartlen = parse_intmax(nextline(&rip, fn, _("archive part offset")),
  133. fn, _("archive part offset"));
  134. partnums = nextline(&rip, fn, _("archive part numbers"));
  135. slash= strchr(partnums,'/');
  136. if (!slash)
  137. ohshit(_("file '%.250s' is corrupt - no slash between archive part numbers"), fn);
  138. *slash++ = '\0';
  139. templong = parse_intmax(slash, fn, _("number of archive parts"));
  140. if (templong <= 0 || templong > INT_MAX)
  141. ohshit(_("file '%.250s' is corrupt - bad number of archive parts"), fn);
  142. ir->maxpartn= templong;
  143. templong = parse_intmax(partnums, fn, _("archive parts number"));
  144. if (templong <= 0 || templong > ir->maxpartn)
  145. ohshit(_("file '%.250s' is corrupt - bad archive part number"), fn);
  146. ir->thispartn= templong;
  147. /* If the package was created with dpkg 1.16.1 or later it will include
  148. * the architecture. */
  149. if (*rip != '\0')
  150. ir->arch = nfstrsave(nextline(&rip, fn, _("package architecture")));
  151. else
  152. ir->arch = NULL;
  153. if (fread(&arh,1,sizeof(arh),partfile) != sizeof(arh)) rerreof(partfile,fn);
  154. dpkg_ar_normalize_name(&arh);
  155. if (dpkg_ar_member_is_illegal(&arh))
  156. ohshit(_("file '%.250s' is corrupt - bad magic at end of second header"), fn);
  157. if (strncmp(arh.ar_name,"data",4))
  158. ohshit(_("file '%.250s' is corrupt - second member is not data member"), fn);
  159. ir->thispartlen = dpkg_ar_member_get_size(fn, &arh);
  160. ir->thispartoffset= (ir->thispartn-1)*ir->maxpartlen;
  161. if (ir->maxpartn != (ir->orglength+ir->maxpartlen-1)/ir->maxpartlen)
  162. ohshit(_("file '%.250s' is corrupt - wrong number of parts for quoted sizes"), fn);
  163. if (ir->thispartlen !=
  164. (ir->thispartn == ir->maxpartn
  165. ? ir->orglength - ir->thispartoffset : ir->maxpartlen))
  166. ohshit(_("file '%.250s' is corrupt - size is wrong for quoted part number"), fn);
  167. ir->filesize = (strlen(DPKG_AR_MAGIC) +
  168. sizeof(arh) + thisilen + (thisilen & 1) +
  169. sizeof(arh) + ir->thispartlen + (ir->thispartlen & 1));
  170. if (fstat(fileno(partfile), &stab))
  171. ohshite(_("unable to fstat part file '%.250s'"), fn);
  172. if (S_ISREG(stab.st_mode)) {
  173. /* Don't do this check if it's coming from a pipe or something. It's
  174. * only an extra sanity check anyway. */
  175. if (stab.st_size < ir->filesize)
  176. ohshit(_("file '%.250s' is corrupt - too short"), fn);
  177. }
  178. ir->headerlen = strlen(DPKG_AR_MAGIC) +
  179. sizeof(arh) + thisilen + (thisilen & 1) + sizeof(arh);
  180. return ir;
  181. }
  182. void mustgetpartinfo(const char *filename, struct partinfo *ri) {
  183. FILE *part;
  184. part= fopen(filename,"r");
  185. if (!part)
  186. ohshite(_("cannot open archive part file '%.250s'"), filename);
  187. if (!read_info(part,filename,ri))
  188. ohshite(_("file '%.250s' is not an archive part"), filename);
  189. fclose(part);
  190. }
  191. void print_info(const struct partinfo *pi) {
  192. printf(_("%s:\n"
  193. " Part format version: %d.%d\n"
  194. " Part of package: %s\n"
  195. " ... version: %s\n"
  196. " ... architecture: %s\n"
  197. " ... MD5 checksum: %s\n"
  198. " ... length: %jd bytes\n"
  199. " ... split every: %jd bytes\n"
  200. " Part number: %d/%d\n"
  201. " Part length: %jd bytes\n"
  202. " Part offset: %jd bytes\n"
  203. " Part file size (used portion): %jd bytes\n\n"),
  204. pi->filename,
  205. pi->fmtversion.major, pi->fmtversion.minor,
  206. pi->package,
  207. pi->version,
  208. pi->arch ? pi->arch : C_("architecture", "<unknown>"),
  209. pi->md5sum,
  210. (intmax_t)pi->orglength,
  211. (intmax_t)pi->maxpartlen,
  212. pi->thispartn,
  213. pi->maxpartn,
  214. (intmax_t)pi->thispartlen,
  215. (intmax_t)pi->thispartoffset,
  216. (intmax_t)pi->filesize);
  217. }
  218. int
  219. do_info(const char *const *argv)
  220. {
  221. const char *thisarg;
  222. struct partinfo *pi, ps;
  223. FILE *part;
  224. if (!*argv)
  225. badusage(_("--%s requires one or more part file arguments"),
  226. cipaction->olong);
  227. while ((thisarg= *argv++)) {
  228. part= fopen(thisarg,"r");
  229. if (!part)
  230. ohshite(_("cannot open archive part file '%.250s'"), thisarg);
  231. pi= read_info(part,thisarg,&ps);
  232. fclose(part);
  233. if (pi) {
  234. print_info(pi);
  235. } else {
  236. printf(_("file '%s' is not an archive part\n"), thisarg);
  237. }
  238. m_output(stdout, _("<standard output>"));
  239. }
  240. return 0;
  241. }