aboutsummaryrefslogtreecommitdiff
path: root/config.go
blob: 1e6944cb2733afb8dde1980918d1a7c1f63be9e6 (plain) (blame)
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
package main

import (
	"encoding/json"
	"errors"
	"os"
	"time"
)

var ErrInsecure = errors.New("config has insecure permissions (need 0?00)")

type config struct {
	User      string
	Passwd    string
	Link4     string
	Link6     string
	Interval  time.Duration
	PrefixLen int
	Records4  []int
	Records6  []int
}

func (c *config) parse(location string) error {
	info, err := os.Stat(location)
	if err != nil {
		return err
	}

	if info.Mode().Perm()&0077 > 0 {
		return ErrInsecure
	}

	f, err := os.Open(location)
	if err != nil {
		return err
	}
	defer f.Close()

	return json.NewDecoder(f).Decode(c)
}