deb.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package deb extracts metadata from Debian packages.
  4. package deb
  5. import (
  6. "archive/tar"
  7. "bufio"
  8. "bytes"
  9. "compress/gzip"
  10. "crypto/md5"
  11. "crypto/sha1"
  12. "crypto/sha256"
  13. "errors"
  14. "fmt"
  15. "io"
  16. "os"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. )
  21. // Info is the Debian package metadata needed to integrate the package
  22. // into a repository.
  23. type Info struct {
  24. // Version is the version of the package, as reported by dpkg.
  25. Version string
  26. // Arch is the Debian CPU architecture the package is for.
  27. Arch string
  28. // Control is the entire contents of the package's control file,
  29. // with leading and trailing whitespace removed.
  30. Control []byte
  31. // MD5 is the MD5 hash of the package file.
  32. MD5 []byte
  33. // SHA1 is the SHA1 hash of the package file.
  34. SHA1 []byte
  35. // SHA256 is the SHA256 hash of the package file.
  36. SHA256 []byte
  37. }
  38. // ReadFile returns Debian package metadata from the .deb file at path.
  39. func ReadFile(path string) (*Info, error) {
  40. f, err := os.Open(path)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return Read(f)
  45. }
  46. // Read returns Debian package metadata from the .deb file in r.
  47. func Read(r io.Reader) (*Info, error) {
  48. b := bufio.NewReader(r)
  49. m5, s1, s256 := md5.New(), sha1.New(), sha256.New()
  50. summers := io.MultiWriter(m5, s1, s256)
  51. r = io.TeeReader(b, summers)
  52. t, err := findControlTar(r)
  53. if err != nil {
  54. return nil, fmt.Errorf("searching for control.tar.gz: %w", err)
  55. }
  56. control, err := findControlFile(t)
  57. if err != nil {
  58. return nil, fmt.Errorf("searching for control file in control.tar.gz: %w", err)
  59. }
  60. arch, version, err := findArchAndVersion(control)
  61. if err != nil {
  62. return nil, fmt.Errorf("extracting version and architecture from control file: %w", err)
  63. }
  64. // Exhaust the remainder of r, so that the summers see the entire file.
  65. if _, err := io.Copy(io.Discard, r); err != nil {
  66. return nil, fmt.Errorf("hashing file: %w", err)
  67. }
  68. return &Info{
  69. Version: version,
  70. Arch: arch,
  71. Control: control,
  72. MD5: m5.Sum(nil),
  73. SHA1: s1.Sum(nil),
  74. SHA256: s256.Sum(nil),
  75. }, nil
  76. }
  77. // findControlTar reads r as an `ar` archive, finds a tarball named
  78. // `control.tar.gz` within, and returns a reader for that file.
  79. func findControlTar(r io.Reader) (tarReader io.Reader, err error) {
  80. var magic [8]byte
  81. if _, err := io.ReadFull(r, magic[:]); err != nil {
  82. return nil, fmt.Errorf("reading ar magic: %w", err)
  83. }
  84. if string(magic[:]) != "!<arch>\n" {
  85. return nil, fmt.Errorf("not an ar file (bad magic %q)", magic)
  86. }
  87. for {
  88. var hdr [60]byte
  89. if _, err := io.ReadFull(r, hdr[:]); err != nil {
  90. return nil, fmt.Errorf("reading file header: %w", err)
  91. }
  92. filename := strings.TrimSpace(string(hdr[:16]))
  93. size, err := strconv.ParseInt(strings.TrimSpace(string(hdr[48:58])), 10, 64)
  94. if err != nil {
  95. return nil, fmt.Errorf("reading size of file %q: %w", filename, err)
  96. }
  97. if filename == "control.tar.gz" {
  98. return io.LimitReader(r, size), nil
  99. }
  100. // files in ar are padded out to 2 bytes.
  101. if size%2 == 1 {
  102. size++
  103. }
  104. if _, err := io.CopyN(io.Discard, r, size); err != nil {
  105. return nil, fmt.Errorf("seeking past file %q: %w", filename, err)
  106. }
  107. }
  108. }
  109. // findControlFile reads r as a tar.gz archive, finds a file named
  110. // `control` within, and returns its contents.
  111. func findControlFile(r io.Reader) (control []byte, err error) {
  112. gz, err := gzip.NewReader(r)
  113. if err != nil {
  114. return nil, fmt.Errorf("decompressing control.tar.gz: %w", err)
  115. }
  116. defer gz.Close()
  117. tr := tar.NewReader(gz)
  118. for {
  119. hdr, err := tr.Next()
  120. if err != nil {
  121. if errors.Is(err, io.EOF) {
  122. return nil, errors.New("EOF while looking for control file in control.tar.gz")
  123. }
  124. return nil, fmt.Errorf("reading tar header: %w", err)
  125. }
  126. if filepath.Clean(hdr.Name) != "control" {
  127. continue
  128. }
  129. // Found control file
  130. break
  131. }
  132. bs, err := io.ReadAll(tr)
  133. if err != nil {
  134. return nil, fmt.Errorf("reading control file: %w", err)
  135. }
  136. return bytes.TrimSpace(bs), nil
  137. }
  138. var (
  139. archKey = []byte("Architecture:")
  140. versionKey = []byte("Version:")
  141. )
  142. // findArchAndVersion extracts the architecture and version strings
  143. // from the given control file.
  144. func findArchAndVersion(control []byte) (arch string, version string, err error) {
  145. b := bytes.NewBuffer(control)
  146. for {
  147. ln, err := b.ReadBytes('\n')
  148. if err != nil {
  149. return "", "", err
  150. }
  151. if bytes.HasPrefix(ln, archKey) {
  152. arch = string(bytes.TrimSpace(ln[len(archKey):]))
  153. } else if bytes.HasPrefix(ln, versionKey) {
  154. version = string(bytes.TrimSpace(ln[len(versionKey):]))
  155. }
  156. if arch != "" && version != "" {
  157. return arch, version, nil
  158. }
  159. }
  160. }