info.c 8.2 KB

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