-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
322 lines (267 loc) · 8.35 KB
/
main.go
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
package main
import (
"os"
"fmt"
"sync"
"github.com/miekg/dns"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"github.com/apex/log"
"github.com/apex/log/handlers/text"
)
const MINAGE = "MinAge"
const MAXAGE = "MaxAge"
const MINVALID = "MinValid"
const MAXVALID = "MaxValid"
const DEFAULT_MINAGE int = 4 * 60 * 60 // 4 hours
const DEFAULT_MAXAGE int = 4 * 24 * 60 * 60 // 4 days
const DEFAULT_MINVALID int = 21 * 24 * 60 * 60 // 21 days
const DEFAULT_MAXVALID int = 30 * 24 * 60 * 60 // 30 days
const NSEC3_MAXITERATIONS = "MaxNsec3Iterations"
const NSEC3_OPTOUTOK = "Nsec3OptOutOk"
const DEFAULT_NSEC3_MAXITERATIONS int = 10
const DEFAULT_NSEC3_OPTOUTOK bool = false
const CHECK_NSEC = "CheckNSEC"
const CHECK_NSEC3 = "CheckNSEC3"
const CHECK_RRSIG = "CheckRRSIG"
const VERBOSE = "verbose"
const VERBOSE_QUIET int = 0
const VERBOSE_ERROR int = 1
const VERBOSE_WARNING int = 2
const VERBOSE_INFO int = 3
const VERBOSE_DEBUG int = 4
const VERBOSE_TRACE int = 5
const MULTISIGNER = "MultiSigner"
const DEFAULT_MULTISIGNER bool = false
type Cache map[string]map[string][]dns.RR
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "umpy [-v] [--nsec] [--nsec3] [--norrsig] [-f <config file>] [--now <timestamp>] [<zone file name>]",
Version: "0.0.1a",
Short: "Validate DNSSEC information in a zone file",
Long: `umpy validates DNSSEC information in a zone file.`,
Run: func(cmd *cobra.Command, args []string) { run(args) },
DisableFlagsInUseLine: true,
Args: cobra.MaximumNArgs(1),
}
func main() {
// default logging to STDERR
log.SetHandler(text.New(os.Stderr))
// Use flags for viper values
viper.BindPFlags(pflag.CommandLine)
if err := rootCmd.Execute(); err != nil {
log.Error(err.Error())
os.Exit(1)
}
}
func init() {
// Set default log handler
log.SetHandler(text.New(os.Stderr))
cobra.OnInitialize(initConfig)
rootCmd.Flags().CountP(VERBOSE, "v", "repeat for more verbose printouts")
rootCmd.Flags().Bool("nsec", true, "validate nsec chain")
rootCmd.Flags().Bool("nsec3", true, "validate nsec3 chain")
rootCmd.Flags().Bool("norrsig", false, "disable rrsig validation")
rootCmd.Flags().String("now", "", "reference time for signature validation")
rootCmd.Flags().StringP("config", "f", "", "config file (default is $HOME/.umpy)")
// Use flags for viper values
viper.BindPFlags(rootCmd.Flags())
}
// initConfig reads in config file and ENV variables if set.sdfsdf
func initConfig() {
// init log level
switch viper.GetInt(VERBOSE) {
case VERBOSE_QUIET: log.SetLevel(log.FatalLevel)
case VERBOSE_ERROR: log.SetLevel(log.ErrorLevel)
case VERBOSE_WARNING: log.SetLevel(log.WarnLevel)
case VERBOSE_INFO: log.SetLevel(log.InfoLevel)
case VERBOSE_DEBUG: log.SetLevel(log.DebugLevel)
default: log.SetLevel(log.ErrorLevel)
}
// Set defaults
//
// default log loglevel
viper.SetDefault(VERBOSE, VERBOSE_QUIET)
// Default signature life times
viper.SetDefault(MINAGE, DEFAULT_MINAGE)
viper.SetDefault(MAXAGE, DEFAULT_MAXAGE)
viper.SetDefault(MINVALID, DEFAULT_MINVALID)
viper.SetDefault(MAXVALID, DEFAULT_MAXVALID)
// Allow recommended digest types
viper.SetDefault("SHA1", false)
viper.SetDefault("SHA256", true)
viper.SetDefault("GOST94", false)
viper.SetDefault("SHA384", true)
viper.SetDefault("SHA512", true)
// Allow recommended algorithms
viper.SetDefault("RSASHA256", true)
viper.SetDefault("RSASHA512", true)
viper.SetDefault("ECDSAP256SHA256", true)
viper.SetDefault("ECDSAP384SHA384", true)
viper.SetDefault("ED25519", true)
viper.SetDefault("ED448", true)
// Which checks should be performed
viper.SetDefault(CHECK_RRSIG, true)
// Default values for NSEC3 checks
viper.SetDefault(NSEC3_MAXITERATIONS, DEFAULT_NSEC3_MAXITERATIONS)
viper.SetDefault(NSEC3_OPTOUTOK, DEFAULT_NSEC3_OPTOUTOK)
// Multisigner
viper.SetDefault(MULTISIGNER, DEFAULT_MULTISIGNER)
// Find home directory.
home, err := homedir.Dir()
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
// Search config in home directory with name ".umpy" (without extension).
viper.SetConfigName(".umpy")
viper.SetConfigType("yaml")
viper.AddConfigPath(home)
viper.AddConfigPath(".")
// config file specified om command line
if viper.GetString("config") != "" {
// Use config file from the flag.
viper.SetConfigFile(viper.GetString("config"))
}
// read in environment variables that match
viper.SetEnvPrefix("UMPY")
viper.AutomaticEnv()
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
if len(viper.GetString("config")) > 0 {
log.Fatalf("Error reading config file: '%s' %s",viper.GetString("config"), err.Error())
}
log.Info(err.Error())
} else {
log.Debugf("Using config file: %s", viper.ConfigFileUsed())
}
// special handling of dnssec timing data
// user specified strings need to be converted to int
for _, opt := range []string{MINAGE, MAXAGE, MINVALID, MAXVALID} {
if viper.GetInt(opt) == 0 { // this indicates there is no int value
value, parserOk := stringToTTL(viper.GetString(opt))
if !parserOk {
log.Errorf("Could not parse config %s value %s\n", opt, viper.GetString(opt))
os.Exit(5)
}
viper.Set(opt, value)
}
}
}
func run(args []string) {
//
// Take care of command line arguments
//
if viper.GetBool("norrsig") {
viper.Set(CHECK_RRSIG, false)
}
if viper.IsSet("nsec") {
viper.Set(CHECK_NSEC, viper.GetBool("nsec"))
}
if viper.IsSet("nsec3") {
viper.Set(CHECK_NSEC3, viper.GetBool("nsec3"))
}
//
// ZONE FILE
//
log.Debug("Start reading zone file")
var zonef *os.File
if len(args) > 0 {
var err error
zonef, err = os.Open(args[0])
if err != nil {
log.Errorf("Could not open zonefile %s", args[0])
log.Error(err.Error())
os.Exit(5)
}
} else {
zonef = os.Stdin
}
origin, cache := readZonefile(zonef)
// close the file we opened
if len(args) > 0 {
zonef.Close()
}
log.Debug("Zone file successfully read.")
if len(cache) == 0 {
log.Info("Zone file empty.")
os.Exit(1)
}
// check for NSEC chain
if !viper.IsSet(CHECK_NSEC) {
foundNSEC := hasNSEC(cache)
viper.Set(CHECK_NSEC, foundNSEC)
}
if viper.GetBool(CHECK_NSEC) {
log.Debug("NSEC records will be checked.")
} else {
log.Debug("NSEC records will not be checked.")
}
// check for NSEC3 chain
if !viper.IsSet(CHECK_NSEC3) {
foundNSEC3 := hasNSEC3(cache)
viper.Set(CHECK_NSEC3, foundNSEC3)
if viper.GetInt("verbose") >= VERBOSE_DEBUG {
}
}
if viper.GetBool(CHECK_NSEC3) {
log.Debug("NSEC3 records will be checked.")
} else {
log.Debug("NSEC3 records will not be checked.")
}
/******************************************************
START CHECKING
******************************************************/
var wg sync.WaitGroup
results := make(chan Result)
go RunTest("DNSKEY", cache, origin, checkDNSKEY, &wg, results)
go RunTest("DS", cache, origin, checkDS, &wg, results)
go RunTest("CDS", cache, origin, checkCDS, &wg, results)
go RunTest("CDNSKEY", cache, origin, checkCDNSKEY, &wg, results)
go RunTest("SOA", cache, origin, checkSOA, &wg, results)
wg.Add(5)
// RRSIG
if viper.GetBool(CHECK_RRSIG) {
go RunTest("RRSIG", cache, origin, checkRRSIG, &wg, results)
wg.Add(1)
}
// NSEC
if viper.GetBool(CHECK_NSEC) {
go RunTest("NSEC", cache, origin, checkNSEC, &wg, results)
wg.Add(1)
}
// NSEC3
if viper.GetBool(CHECK_NSEC3) {
go RunTest("NSEC3PARAM", cache, origin, checkNSEC3PARAM, &wg, results)
go RunTest("NSEC3", cache, origin, checkNSEC3, &wg, results)
wg.Add(2)
}
/* -------------- DONE WITH ALL CHECKS -------------- */
var result *Result = &Result{}
go func() {
for r := range results {
result.Add(r)
}
wg.Done()
}()
wg.Wait()
close(results)
log.Infof("Total %d erros and %d warnings", result.errors, result.warnings)
/* -------------- SET EXIT CODE -------------- */
if result.errors > 0 {
os.Exit(5)
}
if result.warnings > 0 {
os.Exit(1)
}
os.Exit(0)
}
func RunTest(name string, cache Cache, origin string, f func(Cache, string) Result, wg *sync.WaitGroup, c chan Result) {
defer log.Trace(fmt.Sprintf("Testing %s", name)).Stop(nil)
r := f(cache, origin)
log.Infof("Test %s reported %d warnings and %d errors.", name, r.warnings, r.errors)
c <- r
wg.Done()
}