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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
// This file is part of the adequate Debian-native package, and is available
// under the Expat license. For the full terms please see debian/copyright.
package main
import (
"encoding/xml"
"fmt"
"log"
"os"
osUser "os/user"
"path"
"path/filepath"
"sort"
"strings"
)
const (
INVALID_SYSTEMD_USER_GROUP_TAG = "invalid-systemd-user-or-group"
INVALID_DBUS_USER_GROUP_TAG = "invalid-dbus-user-or-group"
INVALID_SYSV_INIT_USER_GROUP_TAG = "invalid-sysvinit-user-or-group"
)
var systemIDTags = []string{
INVALID_SYSTEMD_USER_GROUP_TAG,
INVALID_DBUS_USER_GROUP_TAG,
INVALID_SYSV_INIT_USER_GROUP_TAG,
}
// systemID holds a unix user or group name.
type systemID struct {
value string
kind idType
}
func (s systemID) isValid() bool {
if s.kind == unsetIDType || s.value == "" {
log.Fatalf("Unexpected systemID value: %#v", s)
}
switch s.kind {
case userType:
_, err := osUser.Lookup(s.value)
return err == nil
case groupType:
_, err := osUser.LookupGroup(s.value)
return err == nil
}
return true // should never get here
}
type idType int
const (
unsetIDType idType = iota
userType
groupType
)
func (id idType) String() string {
switch id {
case userType:
return "user"
case groupType:
return "group"
}
return ""
}
type invalidIDErr struct {
pkg string
tag string
path string
id systemID
}
func (c invalidIDErr) Error() string {
if c == (invalidIDErr{}) {
return ""
}
return fmt.Sprintf("%s: %s %s %s=%s", c.pkg, c.tag, c.path, c.id.kind, c.id.value)
}
type systemIDChecker struct {
tagsToEmit map[string]bool
doNotEmitAnyTag bool
}
func newSystemIDChecker(tags tagFilter) systemIDChecker {
emit := make(map[string]bool)
var emitAny bool
for _, t := range systemIDTags {
emit[t] = tags.shouldEmit(t)
if tags.shouldEmit(t) {
emitAny = true
}
}
return systemIDChecker{
tagsToEmit: emit,
doNotEmitAnyTag: !emitAny,
}
}
func (cc systemIDChecker) check(pkg2files map[string][]string) []error {
if cc.doNotEmitAnyTag {
return nil
}
// build map from file to pkg
filePkg := make(map[string]string)
for pkg, files := range pkg2files {
for _, f := range files {
filePkg[f] = pkg
}
}
var errs []error
for _, systemType := range []struct {
tag string
loader func(map[string]string) []fileIDPair
}{
{INVALID_SYSTEMD_USER_GROUP_TAG, loadSystemdIDs},
{INVALID_DBUS_USER_GROUP_TAG, loadDbusIDs},
{INVALID_SYSV_INIT_USER_GROUP_TAG, loadSysvinitIDs},
} {
if !cc.tagsToEmit[systemType.tag] {
continue
}
for _, pair := range systemType.loader(filePkg) {
if pair.id.isValid() {
continue
}
pkg, ok := filePkg[pair.path]
if !ok {
// Likely due to an uninstalled, non-purged package.
continue
}
errs = append(errs, invalidIDErr{
pkg: pkg,
tag: systemType.tag,
path: pair.path,
id: pair.id,
})
}
}
sort.Slice(errs, func(i, j int) bool {
return (errs[i].(invalidIDErr).pkg < errs[j].(invalidIDErr).pkg &&
errs[i].(invalidIDErr).tag < errs[j].(invalidIDErr).tag &&
errs[i].(invalidIDErr).id.kind < errs[j].(invalidIDErr).id.kind)
})
return errs
}
type fileIDPair struct {
path string
id systemID
}
// systemd
func loadSystemdIDs(fileAcceptList map[string]string) []fileIDPair {
var res []fileIDPair
files := glob("/usr/lib/systemd/system/*.service")
for _, f := range files {
if _, ok := fileAcceptList[f]; !ok {
// Skip, unless a file belongs to a package we've been
// asked to check.
continue
}
buf, err := os.ReadFile(f)
if err != nil {
continue
}
res = append(res, extractSystemdIDsFromFile(f, buf)...)
}
return res
}
func extractSystemdIDsFromFile(filename string, contents []byte) []fileIDPair {
var res []fileIDPair
var inServiceSection bool
for _, line := range strings.Split(string(contents), "\n") {
if strings.TrimSpace(line) == "[Service]" {
inServiceSection = true
} else if strings.HasPrefix(line, "[") {
inServiceSection = false
}
if !inServiceSection {
continue
}
// systemd.syntax(7): [..] configuration entries in the style
// key=value. Whitespace immediately before or after the "=" is
// ignored. Empty lines and lines starting with "#" or ";" are
// ignored [..]
var candidate string
var kind idType
if strings.HasPrefix(line, "User=") {
u := strings.Split(line, "=")
if len(u) >= 2 {
candidate, kind = u[1], userType
}
} else if strings.HasPrefix(line, "Group=") {
g := strings.Split(line, "=")
if len(g) >= 2 {
candidate, kind = g[1], groupType
}
} else {
continue
}
// Strip leading and trailing space, single and double
// quotes, and do not attempt to validate values with %
// specifiers (see systemd.unit(5)) or with quoted characters.
if v := strings.TrimSpace(strings.Trim(candidate, `"'`)); v != "" &&
!strings.Contains(v, "%") && !strings.Contains(v, `\`) {
res = append(res, fileIDPair{filename, systemID{value: candidate, kind: kind}})
}
}
return res
}
// dbus
func loadDbusIDs(fileAcceptList map[string]string) []fileIDPair {
var res []fileIDPair
files := glob("/etc/dbus-*/*/*.conf")
for _, f := range files {
if _, ok := fileAcceptList[f]; !ok {
// Skip, unless a file belongs to a package we've been
// asked to check.
continue
}
buf, err := os.ReadFile(f)
if err != nil {
continue
}
cfg := &dbusConfig{}
if err := xml.Unmarshal(buf, cfg); err != nil {
continue
}
for _, p := range cfg.Policy {
if p.User != "" {
res = append(res, fileIDPair{f, systemID{value: p.User, kind: userType}})
}
if p.Group != "" {
res = append(res, fileIDPair{f, systemID{value: p.Group, kind: groupType}})
}
}
}
return res
}
type dbusConfig struct {
Policy []Policy `xml:"policy"`
}
type Policy struct {
User string `xml:"user,attr"`
Group string `xml:"group,attr"`
}
// sysvinit
func loadSysvinitIDs(fileAcceptList map[string]string) []fileIDPair {
var res []fileIDPair
files := glob("/etc/init.d/*")
for _, f := range files {
if _, ok := fileAcceptList[f]; !ok {
// Skip, unless a file belongs to a package we've been
// asked to check.
continue
}
buf, err := os.ReadFile(f)
if err != nil {
continue
}
res = append(res, extractSysvinitIDsFromFile(f, buf)...)
}
return res
}
func extractSysvinitIDsFromFile(filename string, contents []byte) []fileIDPair {
var res []fileIDPair
for _, line := range strings.Split(string(contents), "\n") {
if i := strings.Index(line, "#"); i != -1 {
line = line[:i]
}
tokens := strings.Split(line, "=")
if len(tokens) != 2 {
continue
}
var kind idType
switch k := strings.TrimSpace(tokens[0]); {
// Assume that variables with a USER/GROUP suffix are used to
// specify the user/group under which a service should run.
case strings.HasSuffix(k, "USER"):
kind = userType
case strings.HasSuffix(k, "GROUP"):
kind = groupType
default:
continue
}
v := strings.Trim(strings.TrimSpace(tokens[1]), `"'`)
if v == "" || strings.Contains(v, "$") {
continue // let's not bother with interpolating shell variables ...
}
res = append(res, fileIDPair{filename, systemID{value: v, kind: kind}})
}
return res
}
// shared funcs
func glob(pat string) []string {
files, err := filepath.Glob(pat)
if err == path.ErrBadPattern {
log.Fatalf("Bad glob pattern %q; please file a bug", pat)
}
return files
}
|