2017-01-13 23:49:02 +00:00
|
|
|
// Copyright 2017 clair authors
|
2015-11-13 19:11:28 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
// Package dpkg implements a featurefmt.Lister for dpkg packages.
|
2015-12-28 20:03:29 +00:00
|
|
|
package dpkg
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2018-10-09 21:37:16 +00:00
|
|
|
"github.com/deckarep/golang-set"
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-12-28 01:45:11 +00:00
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2017-01-13 23:49:02 +00:00
|
|
|
"github.com/coreos/clair/ext/featurefmt"
|
2016-12-28 01:45:11 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt"
|
2017-01-03 21:00:20 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
2017-01-13 23:49:02 +00:00
|
|
|
"github.com/coreos/clair/pkg/tarutil"
|
2015-11-13 19:11:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dpkgSrcCaptureRegexp = regexp.MustCompile(`Source: (?P<name>[^\s]*)( \((?P<version>.*)\))?`)
|
|
|
|
dpkgSrcCaptureRegexpNames = dpkgSrcCaptureRegexp.SubexpNames()
|
|
|
|
)
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
type lister struct{}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
func init() {
|
2018-09-19 18:31:15 +00:00
|
|
|
featurefmt.RegisterLister("dpkg", "1.0", &lister{})
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 21:37:16 +00:00
|
|
|
func valid(pkg *featurefmt.PackageInfo) bool {
|
|
|
|
return pkg.PackageName != "" && pkg.PackageVersion != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func addSourceVersion(pkg *featurefmt.PackageInfo) {
|
|
|
|
if pkg.SourceName != "" && pkg.SourceVersion == "" {
|
|
|
|
pkg.SourceVersion = pkg.PackageVersion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
func (l lister) ListFeatures(files tarutil.FilesMap) ([]database.Feature, error) {
|
2017-01-13 23:49:02 +00:00
|
|
|
f, hasFile := files["var/lib/dpkg/status"]
|
2015-11-13 19:11:28 +00:00
|
|
|
if !hasFile {
|
2017-07-26 23:22:29 +00:00
|
|
|
return []database.Feature{}, nil
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 21:37:16 +00:00
|
|
|
var (
|
|
|
|
pkg featurefmt.PackageInfo
|
|
|
|
pkgs = mapset.NewSet()
|
|
|
|
err error
|
|
|
|
)
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(string(f)))
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.HasPrefix(line, "Package: ") {
|
|
|
|
// Package line
|
|
|
|
// Defines the name of the package
|
|
|
|
|
2018-10-09 21:37:16 +00:00
|
|
|
pkg.PackageName = strings.TrimSpace(strings.TrimPrefix(line, "Package: "))
|
|
|
|
pkg.PackageVersion = ""
|
2015-12-28 20:03:29 +00:00
|
|
|
} else if strings.HasPrefix(line, "Source: ") {
|
2018-10-09 21:37:16 +00:00
|
|
|
// Source line (Optional)
|
2015-11-13 19:11:28 +00:00
|
|
|
// Gives the name of the source package
|
|
|
|
// May also specifies a version
|
|
|
|
|
|
|
|
srcCapture := dpkgSrcCaptureRegexp.FindAllStringSubmatch(line, -1)[0]
|
|
|
|
md := map[string]string{}
|
|
|
|
for i, n := range srcCapture {
|
|
|
|
md[dpkgSrcCaptureRegexpNames[i]] = strings.TrimSpace(n)
|
|
|
|
}
|
|
|
|
|
2018-10-09 21:37:16 +00:00
|
|
|
pkg.SourceName = md["name"]
|
2015-11-13 19:11:28 +00:00
|
|
|
if md["version"] != "" {
|
2016-12-28 01:45:11 +00:00
|
|
|
version := md["version"]
|
2018-10-09 21:37:16 +00:00
|
|
|
if err = versionfmt.Valid(dpkg.ParserName, version); err != nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithError(err).WithField("version", string(line[1])).Warning("could not parse package version. skipping")
|
2016-12-28 01:45:11 +00:00
|
|
|
} else {
|
2018-10-09 21:37:16 +00:00
|
|
|
pkg.SourceVersion = version
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-09 21:37:16 +00:00
|
|
|
} else if strings.HasPrefix(line, "Version: ") {
|
2015-11-13 19:11:28 +00:00
|
|
|
// Version line
|
|
|
|
// Defines the version of the package
|
|
|
|
// This version is less important than a version retrieved from a Source line
|
|
|
|
// because the Debian vulnerabilities often skips the epoch from the Version field
|
|
|
|
// which is not present in the Source version, and because +bX revisions don't matter
|
2016-12-28 01:45:11 +00:00
|
|
|
version := strings.TrimPrefix(line, "Version: ")
|
2018-10-09 21:37:16 +00:00
|
|
|
if err = versionfmt.Valid(dpkg.ParserName, version); err != nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithError(err).WithField("version", string(line[1])).Warning("could not parse package version. skipping")
|
2016-12-28 01:45:11 +00:00
|
|
|
} else {
|
2018-10-09 21:37:16 +00:00
|
|
|
pkg.PackageVersion = version
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
2017-04-11 19:44:20 +00:00
|
|
|
} else if line == "" {
|
2018-10-09 21:37:16 +00:00
|
|
|
pkg.Reset()
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 21:37:16 +00:00
|
|
|
if valid(&pkg) {
|
|
|
|
addSourceVersion(&pkg)
|
|
|
|
pkgs.Add(pkg)
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 21:37:16 +00:00
|
|
|
return featurefmt.PackageSetToFeatures(dpkg.ParserName, pkgs), nil
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
func (l lister) RequiredFilenames() []string {
|
2015-11-13 19:11:28 +00:00
|
|
|
return []string{"var/lib/dpkg/status"}
|
|
|
|
}
|