summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/unbound/config/parse.go
blob: 99a632d50fc341089d31a11e72694eea34bb6d20 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package config

import (
	"bufio"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"strings"
)

type option struct{ name, value string }

const (
	optInclude         = "include"
	optIncludeToplevel = "include-toplevel"
	optCumulative      = "statistics-cumulative"
	optEnable          = "control-enable"
	optInterface       = "control-interface"
	optPort            = "control-port"
	optUseCert         = "control-use-cert"
	optKeyFile         = "control-key-file"
	optCertFile        = "control-cert-file"
)

func isOptionUsed(opt option) bool {
	switch opt.name {
	case optInclude,
		optIncludeToplevel,
		optCumulative,
		optEnable,
		optInterface,
		optPort,
		optUseCert,
		optKeyFile,
		optCertFile:
		return true
	}
	return false
}

// TODO:
// If also using chroot, using full path names for the included files works, relative pathnames for the included names
// work if the directory where the daemon is started equals its chroot/working directory or is specified before
// the include statement with  directory:  dir.

// Parse parses Unbound configuration files into UnboundConfig.
// It follows logic described in the 'man unbound.conf':
//   - Files can be included using the 'include:' directive. It can appear anywhere, it accepts a single file name as argument.
//   - Processing continues as if the text  from  the included file was copied into the config file at that point.
//   - Wildcards can be used to include multiple files.
//
// It stops processing on any error: syntax error, recursive include, glob matches directory etc.
func Parse(entryPath string) (*UnboundConfig, error) {
	options, err := parse(entryPath, nil)
	if err != nil {
		return nil, err
	}
	return fromOptions(options), nil
}

func parse(filename string, visited map[string]bool) ([]option, error) {
	if visited == nil {
		visited = make(map[string]bool)
	}
	if visited[filename] {
		return nil, fmt.Errorf("'%s' already visited", filename)
	}
	visited[filename] = true

	f, err := open(filename)
	if err != nil {
		return nil, err
	}
	defer func() { _ = f.Close() }()

	var options []option
	sc := bufio.NewScanner(f)

	for sc.Scan() {
		line := strings.TrimSpace(sc.Text())
		if line == "" || strings.HasPrefix(line, "#") {
			continue
		}

		opt, err := parseLine(line)
		if err != nil {
			return nil, fmt.Errorf("file '%s', error on parsing line '%s': %v", filename, line, err)
		}

		if !isOptionUsed(opt) {
			continue
		}

		if opt.name != optInclude && opt.name != optIncludeToplevel {
			options = append(options, opt)
			continue
		}

		filenames, err := globInclude(opt.value)
		if err != nil {
			return nil, err
		}

		for _, name := range filenames {
			opts, err := parse(name, visited)
			if err != nil {
				return nil, err
			}
			options = append(options, opts...)
		}
	}
	return options, nil
}

func globInclude(include string) ([]string, error) {
	if isGlobPattern(include) {
		return filepath.Glob(include)
	}
	return []string{include}, nil
}

func parseLine(line string) (option, error) {
	parts := strings.Split(line, ":")
	if len(parts) < 2 {
		return option{}, errors.New("bad syntax")
	}
	key, value := cleanKeyValue(parts[0], parts[1])
	return option{name: key, value: value}, nil
}

func cleanKeyValue(key, value string) (string, string) {
	if i := strings.IndexByte(value, '#'); i > 0 {
		value = value[:i-1]
	}
	key = strings.TrimSpace(key)
	value = strings.Trim(strings.TrimSpace(value), "\"'")
	return key, value
}

func isGlobPattern(value string) bool {
	magicChars := `*?[`
	if runtime.GOOS != "windows" {
		magicChars = `*?[\`
	}
	return strings.ContainsAny(value, magicChars)
}

func open(filename string) (*os.File, error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	fi, err := f.Stat()
	if err != nil {
		return nil, err
	}
	if !fi.Mode().IsRegular() {
		return nil, fmt.Errorf("'%s' is not a regular file", filename)
	}
	return f, nil
}