1
|
|
|
/* Vuls - Vulnerability Scanner |
2
|
|
|
Copyright (C) 2016 Future Corporation , Japan. |
3
|
|
|
|
4
|
|
|
This program is free software: you can redistribute it and/or modify |
5
|
|
|
it under the terms of the GNU General Public License as published by |
6
|
|
|
the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
(at your option) any later version. |
8
|
|
|
|
9
|
|
|
This program is distributed in the hope that it will be useful, |
10
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
GNU General Public License for more details. |
13
|
|
|
|
14
|
|
|
You should have received a copy of the GNU General Public License |
15
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
package scan |
19
|
|
|
|
20
|
|
|
import ( |
21
|
|
|
"bufio" |
22
|
|
|
"encoding/json" |
23
|
|
|
"fmt" |
24
|
|
|
"net" |
25
|
|
|
"regexp" |
26
|
|
|
"strings" |
27
|
|
|
"time" |
28
|
|
|
|
29
|
|
|
"github.com/future-architect/vuls/config" |
30
|
|
|
"github.com/future-architect/vuls/models" |
31
|
|
|
"github.com/k0kubun/pp" |
32
|
|
|
"github.com/sirupsen/logrus" |
33
|
|
|
) |
34
|
|
|
|
35
|
|
|
type base struct { |
36
|
|
|
ServerInfo config.ServerInfo |
37
|
|
|
Distro config.Distro |
38
|
|
|
Platform models.Platform |
39
|
|
|
osPackages |
40
|
|
|
|
41
|
|
|
log *logrus.Entry |
42
|
|
|
errs []error |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
func (l *base) scanWp() (err error) { |
46
|
|
|
if len(l.ServerInfo.WpPath) == 0 && len(l.ServerInfo.WpToken) == 0 { |
47
|
|
|
return nil |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
var unsecures models.VulnInfo |
51
|
|
|
if unsecures, err = detectWp(l.ServerInfo); err != nil { |
52
|
|
|
l.log.Errorf("Failed to scan wordpress: %s", err) |
53
|
|
|
return err |
54
|
|
|
} |
55
|
|
|
l.VulnInfos[unsecures.CveID] = unsecures |
56
|
|
|
|
57
|
|
|
return err |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
//CveInfos hoge |
61
|
|
|
type CveInfos struct { |
62
|
|
|
ReleaseDate string `json:"release_date"` |
63
|
|
|
ChangelogURL string `json:"changelog_url"` |
64
|
|
|
Status string `json:"status"` |
65
|
|
|
Vulnerabilities []CveInfo `json:"vulnerabilities"` |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
//CveInfo hoge |
69
|
|
|
type CveInfo struct { |
70
|
|
|
ID int `json:"id"` |
71
|
|
|
Title string `json:"title"` |
72
|
|
|
CreatedAt string `json:"created_at"` |
73
|
|
|
UpdatedAt string `json:"updated_at"` |
74
|
|
|
PublishedDate string `json:"Published_date"` |
75
|
|
|
VulnType string `json:"Vuln_type"` |
76
|
|
|
References []References `json:"references"` |
77
|
|
|
FixedIn string `json:"fixed_in"` |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
//References hoge |
81
|
|
|
type References struct { |
82
|
|
|
URL string `json:"url"` |
83
|
|
|
Cve string `json:"cve"` |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
func detectWp(c config.ServerInfo) (rs models.VulnInfo, err error) { |
87
|
|
|
cmd := fmt.Sprintf("wp core version --path=%s", c.WpPath) |
88
|
|
|
|
89
|
|
|
var coreVersion string |
90
|
|
|
if r := exec(c, cmd, noSudo); r.isSuccess() { |
91
|
|
|
tmp := strings.Split(r.Stdout, ".") |
92
|
|
|
coreVersion = strings.Join(tmp, "") |
93
|
|
|
coreVersion = strings.TrimRight(coreVersion, "\r\n") |
94
|
|
|
pp.Print(coreVersion) |
95
|
|
|
} |
96
|
|
|
cmd = fmt.Sprintf("curl -H 'Authorization: Token token=%s' https://wpvulndb.com/api/v3/wordpresses/%s", c.WpToken, coreVersion) |
97
|
|
|
if r := exec(c, cmd, noSudo); r.isSuccess() { |
98
|
|
|
animals := map[string]CveInfos{} |
99
|
|
|
err = json.Unmarshal([]byte(r.Stdout), &animals) |
100
|
|
|
pp.Print(animals) |
101
|
|
|
} |
102
|
|
|
return |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
func (l *base) exec(cmd string, sudo bool) execResult { |
106
|
|
|
return exec(l.ServerInfo, cmd, sudo, l.log) |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
func (l *base) setServerInfo(c config.ServerInfo) { |
110
|
|
|
l.ServerInfo = c |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
func (l *base) getServerInfo() config.ServerInfo { |
114
|
|
|
return l.ServerInfo |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
func (l *base) setDistro(fam, rel string) { |
118
|
|
|
d := config.Distro{ |
119
|
|
|
Family: fam, |
120
|
|
|
Release: rel, |
121
|
|
|
} |
122
|
|
|
l.Distro = d |
123
|
|
|
|
124
|
|
|
s := l.getServerInfo() |
125
|
|
|
s.Distro = d |
126
|
|
|
l.setServerInfo(s) |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
func (l *base) getDistro() config.Distro { |
130
|
|
|
return l.Distro |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
func (l *base) setPlatform(p models.Platform) { |
134
|
|
|
l.Platform = p |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
func (l *base) getPlatform() models.Platform { |
138
|
|
|
return l.Platform |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
func (l *base) runningKernel() (release, version string, err error) { |
142
|
|
|
r := l.exec("uname -r", noSudo) |
143
|
|
|
if !r.isSuccess() { |
144
|
|
|
return "", "", fmt.Errorf("Failed to SSH: %s", r) |
145
|
|
|
} |
146
|
|
|
release = strings.TrimSpace(r.Stdout) |
147
|
|
|
|
148
|
|
|
switch l.Distro.Family { |
149
|
|
|
case config.Debian: |
150
|
|
|
r := l.exec("uname -a", noSudo) |
151
|
|
|
if !r.isSuccess() { |
152
|
|
|
return "", "", fmt.Errorf("Failed to SSH: %s", r) |
153
|
|
|
} |
154
|
|
|
ss := strings.Fields(r.Stdout) |
155
|
|
|
if 6 < len(ss) { |
156
|
|
|
version = ss[6] |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
return |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
func (l *base) allContainers() (containers []config.Container, err error) { |
163
|
|
|
switch l.ServerInfo.ContainerType { |
164
|
|
|
case "", "docker": |
165
|
|
|
stdout, err := l.dockerPs("-a --format '{{.ID}} {{.Names}} {{.Image}}'") |
166
|
|
|
if err != nil { |
167
|
|
|
return containers, err |
168
|
|
|
} |
169
|
|
|
return l.parseDockerPs(stdout) |
170
|
|
|
case "lxd": |
171
|
|
|
stdout, err := l.lxdPs("-c n") |
172
|
|
|
if err != nil { |
173
|
|
|
return containers, err |
174
|
|
|
} |
175
|
|
|
return l.parseLxdPs(stdout) |
176
|
|
|
case "lxc": |
177
|
|
|
stdout, err := l.lxcPs("-1") |
178
|
|
|
if err != nil { |
179
|
|
|
return containers, err |
180
|
|
|
} |
181
|
|
|
return l.parseLxcPs(stdout) |
182
|
|
|
default: |
183
|
|
|
return containers, fmt.Errorf( |
184
|
|
|
"Not supported yet: %s", l.ServerInfo.ContainerType) |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
func (l *base) runningContainers() (containers []config.Container, err error) { |
189
|
|
|
switch l.ServerInfo.ContainerType { |
190
|
|
|
case "", "docker": |
191
|
|
|
stdout, err := l.dockerPs("--format '{{.ID}} {{.Names}} {{.Image}}'") |
192
|
|
|
if err != nil { |
193
|
|
|
return containers, err |
194
|
|
|
} |
195
|
|
|
return l.parseDockerPs(stdout) |
196
|
|
|
case "lxd": |
197
|
|
|
stdout, err := l.lxdPs("volatile.last_state.power=RUNNING -c n") |
198
|
|
|
if err != nil { |
199
|
|
|
return containers, err |
200
|
|
|
} |
201
|
|
|
return l.parseLxdPs(stdout) |
202
|
|
|
case "lxc": |
203
|
|
|
stdout, err := l.lxcPs("-1 --running") |
204
|
|
|
if err != nil { |
205
|
|
|
return containers, err |
206
|
|
|
} |
207
|
|
|
return l.parseLxcPs(stdout) |
208
|
|
|
default: |
209
|
|
|
return containers, fmt.Errorf( |
210
|
|
|
"Not supported yet: %s", l.ServerInfo.ContainerType) |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
func (l *base) exitedContainers() (containers []config.Container, err error) { |
215
|
|
|
switch l.ServerInfo.ContainerType { |
216
|
|
|
case "", "docker": |
217
|
|
|
stdout, err := l.dockerPs("--filter 'status=exited' --format '{{.ID}} {{.Names}} {{.Image}}'") |
218
|
|
|
if err != nil { |
219
|
|
|
return containers, err |
220
|
|
|
} |
221
|
|
|
return l.parseDockerPs(stdout) |
222
|
|
|
case "lxd": |
223
|
|
|
stdout, err := l.lxdPs("volatile.last_state.power=STOPPED -c n") |
224
|
|
|
if err != nil { |
225
|
|
|
return containers, err |
226
|
|
|
} |
227
|
|
|
return l.parseLxdPs(stdout) |
228
|
|
|
case "lxc": |
229
|
|
|
stdout, err := l.lxcPs("-1 --stopped") |
230
|
|
|
if err != nil { |
231
|
|
|
return containers, err |
232
|
|
|
} |
233
|
|
|
return l.parseLxcPs(stdout) |
234
|
|
|
default: |
235
|
|
|
return containers, fmt.Errorf( |
236
|
|
|
"Not supported yet: %s", l.ServerInfo.ContainerType) |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
func (l *base) dockerPs(option string) (string, error) { |
241
|
|
|
cmd := fmt.Sprintf("docker ps %s", option) |
242
|
|
|
r := l.exec(cmd, noSudo) |
243
|
|
|
if !r.isSuccess() { |
244
|
|
|
return "", fmt.Errorf("Failed to SSH: %s", r) |
245
|
|
|
} |
246
|
|
|
return r.Stdout, nil |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
func (l *base) lxdPs(option string) (string, error) { |
250
|
|
|
cmd := fmt.Sprintf("lxc list %s", option) |
251
|
|
|
r := l.exec(cmd, noSudo) |
252
|
|
|
if !r.isSuccess() { |
253
|
|
|
return "", fmt.Errorf("failed to SSH: %s", r) |
254
|
|
|
} |
255
|
|
|
return r.Stdout, nil |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
func (l *base) lxcPs(option string) (string, error) { |
259
|
|
|
cmd := fmt.Sprintf("lxc-ls %s 2>/dev/null", option) |
260
|
|
|
r := l.exec(cmd, sudo) |
261
|
|
|
if !r.isSuccess() { |
262
|
|
|
return "", fmt.Errorf("failed to SSH: %s", r) |
263
|
|
|
} |
264
|
|
|
return r.Stdout, nil |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
func (l *base) parseDockerPs(stdout string) (containers []config.Container, err error) { |
268
|
|
|
lines := strings.Split(stdout, "\n") |
269
|
|
|
for _, line := range lines { |
270
|
|
|
fields := strings.Fields(line) |
271
|
|
|
if len(fields) == 0 { |
272
|
|
|
break |
273
|
|
|
} |
274
|
|
|
if len(fields) != 3 { |
275
|
|
|
return containers, fmt.Errorf("Unknown format: %s", line) |
276
|
|
|
} |
277
|
|
|
containers = append(containers, config.Container{ |
278
|
|
|
ContainerID: fields[0], |
279
|
|
|
Name: fields[1], |
280
|
|
|
Image: fields[2], |
281
|
|
|
}) |
282
|
|
|
} |
283
|
|
|
return |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
func (l *base) parseLxdPs(stdout string) (containers []config.Container, err error) { |
287
|
|
|
lines := strings.Split(stdout, "\n") |
288
|
|
|
for i, line := range lines[3:] { |
289
|
|
|
if i%2 == 1 { |
290
|
|
|
continue |
291
|
|
|
} |
292
|
|
|
fields := strings.Fields(strings.Replace(line, "|", " ", -1)) |
293
|
|
|
if len(fields) == 0 { |
294
|
|
|
break |
295
|
|
|
} |
296
|
|
|
if len(fields) != 1 { |
297
|
|
|
return containers, fmt.Errorf("Unknown format: %s", line) |
298
|
|
|
} |
299
|
|
|
containers = append(containers, config.Container{ |
300
|
|
|
ContainerID: fields[0], |
301
|
|
|
Name: fields[0], |
302
|
|
|
}) |
303
|
|
|
} |
304
|
|
|
return |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
func (l *base) parseLxcPs(stdout string) (containers []config.Container, err error) { |
308
|
|
|
lines := strings.Split(stdout, "\n") |
309
|
|
|
for _, line := range lines { |
310
|
|
|
fields := strings.Fields(line) |
311
|
|
|
if len(fields) == 0 { |
312
|
|
|
break |
313
|
|
|
} |
314
|
|
|
containers = append(containers, config.Container{ |
315
|
|
|
ContainerID: fields[0], |
316
|
|
|
Name: fields[0], |
317
|
|
|
}) |
318
|
|
|
} |
319
|
|
|
return |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
// ip executes ip command and returns IP addresses |
323
|
|
|
func (l *base) ip() ([]string, []string, error) { |
324
|
|
|
// e.g. |
325
|
|
|
// 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000\ link/ether 52:54:00:2a:86:4c brd ff:ff:ff:ff:ff:ff |
326
|
|
|
// 2: eth0 inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 |
327
|
|
|
// 2: eth0 inet6 fe80::5054:ff:fe2a:864c/64 scope link \ valid_lft forever preferred_lft forever |
328
|
|
|
r := l.exec("/sbin/ip -o addr", noSudo) |
329
|
|
|
if !r.isSuccess() { |
330
|
|
|
return nil, nil, fmt.Errorf("Failed to detect IP address: %v", r) |
331
|
|
|
} |
332
|
|
|
ipv4Addrs, ipv6Addrs := l.parseIP(r.Stdout) |
333
|
|
|
return ipv4Addrs, ipv6Addrs, nil |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
// parseIP parses the results of ip command |
337
|
|
|
func (l *base) parseIP(stdout string) (ipv4Addrs []string, ipv6Addrs []string) { |
338
|
|
|
lines := strings.Split(stdout, "\n") |
339
|
|
|
for _, line := range lines { |
340
|
|
|
fields := strings.Fields(line) |
341
|
|
|
if len(fields) < 4 { |
342
|
|
|
continue |
343
|
|
|
} |
344
|
|
|
ip, _, err := net.ParseCIDR(fields[3]) |
345
|
|
|
if err != nil { |
346
|
|
|
continue |
347
|
|
|
} |
348
|
|
|
if !ip.IsGlobalUnicast() { |
349
|
|
|
continue |
350
|
|
|
} |
351
|
|
|
if ipv4 := ip.To4(); ipv4 != nil { |
352
|
|
|
ipv4Addrs = append(ipv4Addrs, ipv4.String()) |
353
|
|
|
} else { |
354
|
|
|
ipv6Addrs = append(ipv6Addrs, ip.String()) |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
return |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
func (l *base) detectPlatform() { |
361
|
|
|
if l.getServerInfo().Mode.IsOffline() { |
362
|
|
|
l.setPlatform(models.Platform{Name: "unknown"}) |
363
|
|
|
return |
364
|
|
|
} |
365
|
|
|
ok, instanceID, err := l.detectRunningOnAws() |
366
|
|
|
if err != nil { |
367
|
|
|
l.setPlatform(models.Platform{Name: "other"}) |
368
|
|
|
return |
369
|
|
|
} |
370
|
|
|
if ok { |
371
|
|
|
l.setPlatform(models.Platform{ |
372
|
|
|
Name: "aws", |
373
|
|
|
InstanceID: instanceID, |
374
|
|
|
}) |
375
|
|
|
return |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
//TODO Azure, GCP... |
379
|
|
|
l.setPlatform(models.Platform{Name: "other"}) |
380
|
|
|
return |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
func (l *base) detectRunningOnAws() (ok bool, instanceID string, err error) { |
384
|
|
|
if r := l.exec("type curl", noSudo); r.isSuccess() { |
385
|
|
|
cmd := "curl --max-time 1 --noproxy 169.254.169.254 http://169.254.169.254/latest/meta-data/instance-id" |
386
|
|
|
r := l.exec(cmd, noSudo) |
387
|
|
|
if r.isSuccess() { |
388
|
|
|
id := strings.TrimSpace(r.Stdout) |
389
|
|
|
if !l.isAwsInstanceID(id) { |
390
|
|
|
return false, "", nil |
391
|
|
|
} |
392
|
|
|
return true, id, nil |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
switch r.ExitStatus { |
396
|
|
|
case 28, 7: |
397
|
|
|
// Not running on AWS |
398
|
|
|
// 7 Failed to connect to host. |
399
|
|
|
// 28 operation timeout. |
400
|
|
|
return false, "", nil |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
if r := l.exec("type wget", noSudo); r.isSuccess() { |
405
|
|
|
cmd := "wget --tries=3 --timeout=1 --no-proxy -q -O - http://169.254.169.254/latest/meta-data/instance-id" |
406
|
|
|
r := l.exec(cmd, noSudo) |
407
|
|
|
if r.isSuccess() { |
408
|
|
|
id := strings.TrimSpace(r.Stdout) |
409
|
|
|
if !l.isAwsInstanceID(id) { |
410
|
|
|
return false, "", nil |
411
|
|
|
} |
412
|
|
|
return true, id, nil |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
switch r.ExitStatus { |
416
|
|
|
case 4, 8: |
417
|
|
|
// Not running on AWS |
418
|
|
|
// 4 Network failure |
419
|
|
|
// 8 Server issued an error response. |
420
|
|
|
return false, "", nil |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
return false, "", fmt.Errorf( |
424
|
|
|
"Failed to curl or wget to AWS instance metadata on %s. container: %s", |
425
|
|
|
l.ServerInfo.ServerName, l.ServerInfo.Container.Name) |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/resource-ids.html |
429
|
|
|
var awsInstanceIDPattern = regexp.MustCompile(`^i-[0-9a-f]+$`) |
430
|
|
|
|
431
|
|
|
func (l *base) isAwsInstanceID(str string) bool { |
432
|
|
|
return awsInstanceIDPattern.MatchString(str) |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
func (l *base) convertToModel() models.ScanResult { |
436
|
|
|
ctype := l.ServerInfo.ContainerType |
437
|
|
|
if l.ServerInfo.Container.ContainerID != "" && ctype == "" { |
438
|
|
|
ctype = "docker" |
439
|
|
|
} |
440
|
|
|
container := models.Container{ |
441
|
|
|
ContainerID: l.ServerInfo.Container.ContainerID, |
442
|
|
|
Name: l.ServerInfo.Container.Name, |
443
|
|
|
Image: l.ServerInfo.Container.Image, |
444
|
|
|
Type: ctype, |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
errs := []string{} |
448
|
|
|
for _, e := range l.errs { |
449
|
|
|
errs = append(errs, fmt.Sprintf("%s", e)) |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
return models.ScanResult{ |
453
|
|
|
JSONVersion: models.JSONVersion, |
454
|
|
|
ServerName: l.ServerInfo.ServerName, |
455
|
|
|
ScannedAt: time.Now(), |
456
|
|
|
ScanMode: l.ServerInfo.Mode.String(), |
457
|
|
|
Family: l.Distro.Family, |
458
|
|
|
Release: l.Distro.Release, |
459
|
|
|
Container: container, |
460
|
|
|
Platform: l.Platform, |
461
|
|
|
IPv4Addrs: l.ServerInfo.IPv4Addrs, |
462
|
|
|
IPv6Addrs: l.ServerInfo.IPv6Addrs, |
463
|
|
|
ScannedCves: l.VulnInfos, |
464
|
|
|
RunningKernel: l.Kernel, |
465
|
|
|
Packages: l.Packages, |
466
|
|
|
SrcPackages: l.SrcPackages, |
467
|
|
|
Optional: l.ServerInfo.Optional, |
468
|
|
|
Errors: errs, |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
func (l *base) setErrs(errs []error) { |
473
|
|
|
l.errs = errs |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
func (l *base) getErrs() []error { |
477
|
|
|
return l.errs |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
const ( |
481
|
|
|
systemd = "systemd" |
482
|
|
|
upstart = "upstart" |
483
|
|
|
sysVinit = "init" |
484
|
|
|
) |
485
|
|
|
|
486
|
|
|
// https://unix.stackexchange.com/questions/196166/how-to-find-out-if-a-system-uses-sysv-upstart-or-systemd-initsystem |
487
|
|
|
func (l *base) detectInitSystem() (string, error) { |
488
|
|
|
var f func(string) (string, error) |
489
|
|
|
f = func(cmd string) (string, error) { |
490
|
|
|
r := l.exec(cmd, sudo) |
491
|
|
|
if !r.isSuccess() { |
492
|
|
|
return "", fmt.Errorf("Failed to stat %s: %s", cmd, r) |
493
|
|
|
} |
494
|
|
|
scanner := bufio.NewScanner(strings.NewReader(r.Stdout)) |
495
|
|
|
scanner.Scan() |
496
|
|
|
line := strings.TrimSpace(scanner.Text()) |
497
|
|
|
if strings.Contains(line, "systemd") { |
498
|
|
|
return systemd, nil |
499
|
|
|
} else if strings.Contains(line, "upstart") { |
500
|
|
|
return upstart, nil |
501
|
|
|
} else if strings.Contains(line, "File: ‘/proc/1/exe’ -> ‘/sbin/init’") || |
502
|
|
|
strings.Contains(line, "File: `/proc/1/exe' -> `/sbin/init'") { |
503
|
|
|
return f("stat /sbin/init") |
504
|
|
|
} else if line == "File: ‘/sbin/init’" || |
505
|
|
|
line == "File: `/sbin/init'" { |
506
|
|
|
r := l.exec("/sbin/init --version", noSudo) |
507
|
|
|
if r.isSuccess() { |
508
|
|
|
if strings.Contains(r.Stdout, "upstart") { |
509
|
|
|
return upstart, nil |
510
|
|
|
} |
511
|
|
|
} |
512
|
|
|
return sysVinit, nil |
513
|
|
|
} |
514
|
|
|
return "", fmt.Errorf("Failed to detect a init system: %s", line) |
515
|
|
|
} |
516
|
|
|
return f("stat /proc/1/exe") |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
func (l *base) detectServiceName(pid string) (string, error) { |
520
|
|
|
cmd := fmt.Sprintf("systemctl status --quiet --no-pager %s", pid) |
521
|
|
|
r := l.exec(cmd, noSudo) |
522
|
|
|
if !r.isSuccess() { |
523
|
|
|
return "", fmt.Errorf("Failed to stat %s: %s", cmd, r) |
524
|
|
|
} |
525
|
|
|
return l.parseSystemctlStatus(r.Stdout), nil |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
func (l *base) parseSystemctlStatus(stdout string) string { |
529
|
|
|
scanner := bufio.NewScanner(strings.NewReader(stdout)) |
530
|
|
|
scanner.Scan() |
531
|
|
|
line := scanner.Text() |
532
|
|
|
ss := strings.Fields(line) |
533
|
|
|
if len(ss) < 2 || strings.HasPrefix(line, "Failed to get unit for PID") { |
534
|
|
|
return "" |
535
|
|
|
} |
536
|
|
|
return ss[1] |
537
|
|
|
} |
538
|
|
|
|