Repository navigation
Expand file tree
/
Copy pathapt.go
More file actions
285 lines (260 loc) · 7.74 KB
/
Copy pathapt.go
File metadata and controls
285 lines (260 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"github.com/sysward/sysward-agent/logging"
"os"
// "strconv"
"strings"
)
type DebianPackageManager struct{}
func (pm DebianPackageManager) UpdatePackage(pkg string) error {
out, err := runner.Run("apt-get",
"install",
"-y",
"-o",
fmt.Sprintf("Dpkg::Options::=--force-confdef"),
"-o",
fmt.Sprintf("Dpkg::Options::=--force-confold"),
pkg,
)
if os.Getenv("DEBUG") == "true" {
logging.LogMsg(string(out))
}
if err != nil {
err = errors.New(string(out) + err.Error())
}
return err
}
func (pm DebianPackageManager) HoldPackage(pkg string) error {
out, err := runner.Run("apt-mark", "hold", pkg)
logging.LogMsg(string(out))
if err != nil {
err = errors.New(string(out) + err.Error())
}
return err
}
func (pm DebianPackageManager) UnholdPackage(pkg string) error {
out, err := runner.Run("apt-mark", "unhold", pkg)
logging.LogMsg(string(out))
if err != nil {
err = errors.New(string(out) + err.Error())
}
return err
}
func (pm DebianPackageManager) GetSourcesList() []Source {
out, _ := runner.Run("grep", "-h", "^deb", "/etc/apt/sources.list", "/etc/apt/sources.list.d/*")
out_arr := strings.Split(strings.TrimSpace(string(out)), "\n")
sources := make([]Source, len(out_arr))
for index, o := range out_arr {
x := strings.Split(o, " ")
src := false
if x[0] == "deb-src" {
src = true
}
sources[index] = Source{x[1], src, x[2:]}
}
return sources
}
func (pm DebianPackageManager) GetChangelog(package_name string) string {
changelog, _ := runner.Run("apt-get", "changelog", package_name)
changelog_encoded := base64.StdEncoding.EncodeToString([]byte(changelog))
return changelog_encoded
}
func (pm DebianPackageManager) BuildInstalledPackageList() []string {
installed, _ := runner.Run("dpkg", "--get-selections")
installed_arr := strings.Split(string(installed), "\n")
packages := []string{}
for _, line := range installed_arr {
x := strings.Split(line, "\u0009")
if x[0] == "" {
continue
}
packages = append(packages, x[0])
}
return packages
}
// dpkgQueryFormat asks dpkg-query for every field BuildPackageList needs in
// one call: installed version, source package, architecture, priority, section.
const dpkgQueryFormat = "${Version}\t${source:Package}\t${Architecture}\t${Priority}\t${Section}\n"
type dpkgPackageInfo struct {
Version string
Source string
Arch string
Priority string
Section string
}
// parseDpkgQuery parses dpkgQueryFormat output. Only the first line is used
// (a Multi-Arch: same package installed for several architectures yields one
// line per architecture). An empty source falls back to the package name.
func parseDpkgQuery(name string, out []byte) dpkgPackageInfo {
line := strings.SplitN(strings.TrimLeft(string(out), "\n"), "\n", 2)[0]
fields := strings.Split(line, "\t")
get := func(i int) string {
if i < len(fields) {
return strings.TrimSpace(fields[i])
}
return ""
}
info := dpkgPackageInfo{
Version: get(0),
Source: get(1),
Arch: get(2),
Priority: get(3),
Section: get(4),
}
if info.Source == "" {
info.Source = strings.SplitN(name, ":", 2)[0]
}
return info
}
type aptInstLine struct {
Name string
Candidate string
Security bool
}
// parseAptInstLine parses one "Inst" line of `apt-get -s upgrade`:
//
// Inst libssl3 [3.0.2-0ubuntu1.15] (3.0.2-0ubuntu1.18 Ubuntu:22.04/jammy-updates, Ubuntu:22.04/jammy-security [amd64])
//
// The parenthesised part holds the candidate version, then one or more
// comma-separated "Label:Version/Archive" releases it is available from,
// then the architecture. The "[old]" part is absent for a new install.
func parseAptInstLine(line string) (aptInstLine, bool) {
fields := strings.Fields(line)
if len(fields) < 3 || fields[0] != "Inst" {
return aptInstLine{}, false
}
open := strings.Index(line, "(")
end := strings.LastIndex(line, ")")
if open < 0 || end < open {
return aptInstLine{}, false
}
inner := strings.TrimSpace(line[open+1 : end])
if i := strings.LastIndex(inner, " ["); i >= 0 {
inner = inner[:i]
}
parts := strings.SplitN(inner, " ", 2)
inst := aptInstLine{
Name: strings.Split(fields[1], "/")[0],
Candidate: parts[0],
}
if len(parts) == 2 {
for _, release := range strings.Split(parts[1], ",") {
if isAptSecurityRelease(strings.TrimSpace(release)) {
inst.Security = true
}
}
}
return inst, true
}
// isAptSecurityRelease reports whether a "Label:Version/Archive" release
// is a security archive: Ubuntu "Ubuntu:22.04/jammy-security", Debian 11+
// "Debian-Security:12/stable-security" or "Debian:12/bookworm-security", and
// pre-11 Debian whose label alone says so ("Debian-Security:10/oldstable").
func isAptSecurityRelease(release string) bool {
release = strings.ToLower(release)
label, archive := release, release
if i := strings.Index(release, ":"); i >= 0 {
label = release[:i]
}
if i := strings.LastIndex(release, "/"); i >= 0 {
archive = release[i+1:]
}
return strings.HasSuffix(label, "-security") ||
archive == "security" ||
strings.HasSuffix(archive, "-security")
}
func (pm DebianPackageManager) BuildPackageList() []OsPackage {
var packages []OsPackage
out, err := runner.RunBytes("apt-get", "-s", "upgrade")
if err != nil {
fmt.Println("Error:", err)
return nil
}
// Parse the output
lines := bytes.Split(out, []byte("\n"))
for _, line := range lines {
inst, ok := parseAptInstLine(string(line))
if !ok {
continue
}
name := inst.Name
dpkgOut, err := runner.RunBytes("dpkg-query", "-W", "-f="+dpkgQueryFormat, name)
if err != nil {
fmt.Println("Error:", err)
continue
}
info := parseDpkgQuery(name, dpkgOut)
pkg := OsPackage{Name: name,
Current_version: info.Version,
Candidate_version: inst.Candidate,
Priority: info.Priority,
Section: info.Section,
Security: inst.Security,
Source: info.Source,
Arch: info.Arch,
InstalledEVR: info.Version,
}
packages = append(packages, pkg)
}
return packages
}
func (pm DebianPackageManager) UpdatePackageLists() error {
_, err := runner.Run("apt-get", "update")
return err
}
func (pm DebianPackageManager) UpdateCounts() Updates {
packages := pm.BuildPackageList()
security := 0
regular := 0
for _, p := range packages {
if p.Security {
security++
} else {
regular++
}
}
return Updates{regular, security}
}
// dpkgInventoryFormat lists every package with the fields the server needs for
// CVE lookup. db:Status-Abbrev is last so removed-but-configured packages
// ("rc") can be skipped.
const dpkgInventoryFormat = "${Package}\t${Version}\t${source:Package}\t${Architecture}\t${db:Status-Abbrev}\n"
// BuildInstalledInventory returns every installed package with version, source
// package and architecture, collected with a single dpkg-query call.
func (pm DebianPackageManager) BuildInstalledInventory() []InstalledPackage {
out, err := runner.RunBytes("dpkg-query", "-W", "-f="+dpkgInventoryFormat)
if err != nil && len(out) == 0 {
return []InstalledPackage{}
}
return parseDpkgInventory(out)
}
func parseDpkgInventory(out []byte) []InstalledPackage {
pkgs := []InstalledPackage{}
for _, line := range strings.Split(string(out), "\n") {
f := strings.Split(line, "\t")
if len(f) < 5 || strings.TrimSpace(f[0]) == "" || strings.TrimSpace(f[1]) == "" {
continue
}
// only fully installed packages ("ii"/"hi"); skip rc/un/etc.
status := strings.TrimSpace(f[4])
if !strings.HasPrefix(status, "ii") && !strings.HasPrefix(status, "hi") {
continue
}
name := strings.TrimSpace(f[0])
src := strings.TrimSpace(f[2])
if src == "" {
src = name
}
pkgs = append(pkgs, InstalledPackage{
Name: name,
Version: strings.TrimSpace(f[1]),
Source: src,
Arch: strings.TrimSpace(f[3]),
})
}
return pkgs
}