info.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <assert.h>
  24. #include <limits.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <ar.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <dpkg/i18n.h>
  32. #include <dpkg/dpkg.h>
  33. #include <dpkg/dpkg-db.h>
  34. #include <dpkg/ar.h>
  35. #include <dpkg/myopt.h>
  36. #include "dpkg-split.h"
  37. static unsigned long unsignedlong(const char *value, const char *fn, const char *what) {
  38. unsigned long r;
  39. char *endp;
  40. r= strtoul(value,&endp,10);
  41. if (value == endp || *endp)
  42. ohshit(_("file `%.250s' is corrupt - bad digit (code %d) in %s"),fn,*endp,what);
  43. return r;
  44. }
  45. static unsigned long parseheaderlength(const char *inh, size_t len,
  46. const char *fn, const char *what) {
  47. char lintbuf[15];
  48. if (memchr(inh,0,len))
  49. ohshit(_("file `%.250s' is corrupt - %.250s length contains nulls"),fn,what);
  50. assert(sizeof(lintbuf) > len);
  51. memcpy(lintbuf,inh,len);
  52. lintbuf[len]= ' ';
  53. *strchr(lintbuf, ' ') = '\0';
  54. return unsignedlong(lintbuf,fn,what);
  55. }
  56. static char *nextline(char **ripp, const char *fn, const char *what) {
  57. char *newline, *rip;
  58. rip= *ripp;
  59. if (!rip) 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"),fn,what);
  63. *ripp= newline+1;
  64. while (newline > rip && isspace(newline[-1])) newline--;
  65. *newline = '\0';
  66. return rip;
  67. }
  68. struct partinfo *read_info(FILE *partfile, const char *fn, struct partinfo *ir) {
  69. /* returns info (nfmalloc'd) if was an archive part and we read it, 0 if it wasn't */
  70. static char *readinfobuf= NULL;
  71. static size_t readinfobuflen= 0;
  72. size_t thisilen;
  73. unsigned int templong;
  74. char magicbuf[strlen(DPKG_AR_MAGIC)], *rip, *partnums, *slash;
  75. struct ar_hdr arh;
  76. int c;
  77. struct stat stab;
  78. if (fread(magicbuf, 1, sizeof(magicbuf), partfile) != sizeof(magicbuf)) {
  79. if (ferror(partfile)) rerr(fn); else 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 = parseheaderlength(arh.ar_size, sizeof(arh.ar_size), fn,
  90. _("archive information member size"));
  91. if (thisilen >= readinfobuflen) {
  92. readinfobuflen= thisilen+1;
  93. readinfobuf= m_realloc(readinfobuf,readinfobuflen);
  94. }
  95. if (fread(readinfobuf,1,thisilen,partfile) != thisilen) rerreof(partfile,fn);
  96. if (thisilen & 1) {
  97. c= getc(partfile); if (c==EOF) rerreof(partfile,fn);
  98. if (c != '\n')
  99. ohshit(_("file `%.250s' is corrupt - bad padding character (code %d)"),fn,c);
  100. }
  101. readinfobuf[thisilen] = '\0';
  102. if (memchr(readinfobuf,0,thisilen))
  103. ohshit(_("file `%.250s' is corrupt - nulls in info section"),fn);
  104. ir->filename= fn;
  105. rip= readinfobuf;
  106. ir->fmtversion = nfstrsave(nextline(&rip, fn, _("format version number")));
  107. if (strcmp(ir->fmtversion,SPLITVERSION))
  108. ohshit(_("file `%.250s' is format version `%.250s' - you need a newer dpkg-split"),
  109. fn,ir->fmtversion);
  110. ir->package = nfstrsave(nextline(&rip, fn, _("package name")));
  111. ir->version = nfstrsave(nextline(&rip, fn, _("package version number")));
  112. ir->md5sum = nfstrsave(nextline(&rip, fn, _("package file MD5 checksum")));
  113. if (strlen(ir->md5sum) != MD5HASHLEN ||
  114. strspn(ir->md5sum, "0123456789abcdef") != MD5HASHLEN)
  115. ohshit(_("file `%.250s' is corrupt - bad MD5 checksum `%.250s'"),fn,ir->md5sum);
  116. ir->orglength = unsignedlong(nextline(&rip, fn, _("archive total size")),
  117. fn, _("archive total size"));
  118. ir->maxpartlen = unsignedlong(nextline(&rip, fn, _("archive part offset")),
  119. fn, _("archive part offset"));
  120. partnums = nextline(&rip, fn, _("archive part numbers"));
  121. slash= strchr(partnums,'/');
  122. if (!slash)
  123. ohshit(_("file '%.250s' is corrupt - no slash between archive part numbers"), fn);
  124. *slash++ = '\0';
  125. templong = unsignedlong(slash, fn, _("number of archive parts"));
  126. if (templong <= 0 || templong > INT_MAX)
  127. ohshit(_("file '%.250s' is corrupt - bad number of archive parts"), fn);
  128. ir->maxpartn= templong;
  129. templong = unsignedlong(partnums, fn, _("archive parts number"));
  130. if (templong <= 0 || templong > ir->maxpartn)
  131. ohshit(_("file '%.250s' is corrupt - bad archive part number"),fn);
  132. ir->thispartn= templong;
  133. if (fread(&arh,1,sizeof(arh),partfile) != sizeof(arh)) rerreof(partfile,fn);
  134. dpkg_ar_normalize_name(&arh);
  135. if (memcmp(arh.ar_fmag,ARFMAG,sizeof(arh.ar_fmag)))
  136. ohshit(_("file `%.250s' is corrupt - bad magic at end of second header"),fn);
  137. if (strncmp(arh.ar_name,"data",4))
  138. ohshit(_("file `%.250s' is corrupt - second member is not data member"),fn);
  139. ir->thispartlen = parseheaderlength(arh.ar_size, sizeof(arh.ar_size), fn,
  140. _("archive data member size"));
  141. ir->thispartoffset= (ir->thispartn-1)*ir->maxpartlen;
  142. if (ir->maxpartn != (ir->orglength+ir->maxpartlen-1)/ir->maxpartlen)
  143. ohshit(_("file `%.250s' is corrupt - wrong number of parts for quoted sizes"),fn);
  144. if (ir->thispartlen !=
  145. (ir->thispartn == ir->maxpartn
  146. ? ir->orglength - ir->thispartoffset : ir->maxpartlen))
  147. ohshit(_("file `%.250s' is corrupt - size is wrong for quoted part number"),fn);
  148. ir->filesize = (strlen(DPKG_AR_MAGIC) +
  149. sizeof(arh) + thisilen + (thisilen & 1) +
  150. sizeof(arh) + ir->thispartlen + (ir->thispartlen & 1));
  151. if (fstat(fileno(partfile),&stab)) ohshite(_("unable to fstat part file `%.250s'"),fn);
  152. if (S_ISREG(stab.st_mode)) {
  153. /* Don't do this check if it's coming from a pipe or something. It's
  154. * only an extra sanity check anyway.
  155. */
  156. if (stab.st_size < ir->filesize)
  157. ohshit(_("file `%.250s' is corrupt - too short"),fn);
  158. }
  159. ir->headerlen = strlen(DPKG_AR_MAGIC) +
  160. sizeof(arh) + thisilen + (thisilen & 1) + sizeof(arh);
  161. return ir;
  162. }
  163. void mustgetpartinfo(const char *filename, struct partinfo *ri) {
  164. FILE *part;
  165. part= fopen(filename,"r");
  166. if (!part) ohshite(_("cannot open archive part file `%.250s'"),filename);
  167. if (!read_info(part,filename,ri))
  168. ohshite(_("file `%.250s' is not an archive part"),filename);
  169. fclose(part);
  170. }
  171. void print_info(const struct partinfo *pi) {
  172. printf(_("%s:\n"
  173. " Part format version: %s\n"
  174. " Part of package: %s\n"
  175. " ... version: %s\n"
  176. " ... MD5 checksum: %s\n"
  177. " ... length: %lu bytes\n"
  178. " ... split every: %lu bytes\n"
  179. " Part number: %d/%d\n"
  180. " Part length: %zi bytes\n"
  181. " Part offset: %lu bytes\n"
  182. " Part file size (used portion): %lu bytes\n\n"),
  183. pi->filename,
  184. pi->fmtversion,
  185. pi->package,
  186. pi->version,
  187. pi->md5sum,
  188. pi->orglength,
  189. pi->maxpartlen,
  190. pi->thispartn,
  191. pi->maxpartn,
  192. pi->thispartlen,
  193. pi->thispartoffset,
  194. (unsigned long)pi->filesize);
  195. }
  196. void do_info(const char *const *argv) {
  197. const char *thisarg;
  198. struct partinfo *pi, ps;
  199. FILE *part;
  200. if (!*argv)
  201. badusage(_("--%s requires one or more part file arguments"),
  202. cipaction->olong);
  203. while ((thisarg= *argv++)) {
  204. part= fopen(thisarg,"r");
  205. if (!part) ohshite(_("cannot open archive part file `%.250s'"),thisarg);
  206. pi= read_info(part,thisarg,&ps);
  207. fclose(part);
  208. if (pi) {
  209. print_info(pi);
  210. } else {
  211. printf(_("file `%s' is not an archive part\n"),thisarg);
  212. }
  213. m_output(stdout, _("<standard output>"));
  214. }
  215. }