info.c 8.9 KB

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