info.c 8.1 KB

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