Skip to content

Commit

Permalink
fix: Patching of /etc/nsswitch.conf
Browse files Browse the repository at this point in the history
Signed-off-by: Steffen Vogel <[email protected]>
  • Loading branch information
stv0g committed Oct 27, 2024
1 parent 2dcbff5 commit 083af9a
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions pkg/network_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net"
"os"
"path/filepath"
"slices"
"strings"

"cunicu.li/gont/v2/internal/utils"
Expand Down Expand Up @@ -141,8 +140,12 @@ func readNSSwitchConfig(fn string) (map[string][]string, error) {

cols := strings.Split(line, ":")

if len(cols) < 2 {
continue // Skip empty lines
}

db := cols[0]
srcs := cols[1:]
srcs := strings.Fields(cols[1])

m[db] = srcs
}
Expand Down Expand Up @@ -180,12 +183,32 @@ func (n *Network) patchNSSConfFile() error {
return fmt.Errorf("failed to read nsswitch.conf: %w", err)
}

for db := range cfg {
if db == "hosts" {
cfg[db] = slices.DeleteFunc(cfg[db], func(src string) bool {
return !strings.HasPrefix(src, "resolve") && !strings.HasPrefix(src, "mymachines") && !strings.HasPrefix(src, "myhostname")
})
keepService := func(svc string) bool {
return svc != "resolve" && svc != "mymachines" && svc != "myhostname"
}

isAction := func(s string) bool {
return len(s) >= 1 && s[0] == '['
}

if fields, ok := cfg["hosts"]; ok {
var newFields []string

for i := 0; i < len(fields); i++ {
keep := keepService(fields[i])
if keep {
newFields = append(newFields, fields[i])
}

if i+1 < len(fields) && isAction(fields[i+1]) {
i++
if keep {
newFields = append(newFields, fields[i+1])
}
}
}

cfg["hosts"] = newFields
}

fn := filepath.Join(n.VarPath, "files/etc/nsswitch.conf")
Expand Down

0 comments on commit 083af9a

Please sign in to comment.