info.c 9.0 KB

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