carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package hostbox
4
5import (
6 "path/filepath"
7 "strings"
8
9 "github.com/carlosframework/platform/internal/rootstate"
10)
11
12func censusVendorUnitLink(root, path string) bool {
13 rel, err := filepath.Rel(root, path)
14 if err != nil || !filepath.IsLocal(rel) {
15 return false
16 }
17 parts := strings.Split(rel, string(filepath.Separator))
18 if len(parts) > 2 || (len(parts) == 2 && !strings.HasSuffix(parts[0], ".wants") && !strings.HasSuffix(parts[0], ".requires")) {
19 return false
20 }
21 for _, part := range parts {
22 if strings.HasPrefix(part, "carlos") {
23 return false
24 }
25 }
26 target, err := rootstate.ReadTrustedLink(path)
27 if err != nil {
28 return false
29 }
30 if !filepath.IsAbs(target) {
31 target = filepath.Join(filepath.Dir(path), target)
32 }
33 target = filepath.Clean(target)
34 if filepath.Dir(target) != "/usr/lib/systemd/system" && filepath.Dir(target) != "/lib/systemd/system" {
35 return false
36 }
37 if strings.HasPrefix(filepath.Base(target), "carlos") {
38 return false
39 }
40 resolved, err := filepath.EvalSymlinks(target)
41 if err != nil || filepath.Dir(resolved) != "/usr/lib/systemd/system" && filepath.Dir(resolved) != "/lib/systemd/system" {
42 return false
43 }
44 f, err := rootstate.OpenTrustedFile(resolved)
45 if err != nil {
46 return false
47 }
48 return f.Close() == nil
49}
50