info.c 8.6 KB

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