aboutsummaryrefslogtreecommitdiff
path: root/cmd/acme/config.go
blob: 15c3403d0dfc0d798a6bd676ab4d9869c89494ea (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
package main

import (
	"errors"
	"io/ioutil"
	"os/user"
	"path"
	"strings"
	"time"

	"gopkg.in/yaml.v2"
)

const defKeySize = 2048

type Config struct {
	Gracetime time.Duration
	Listen    string
	ListenTLS string
	BaseDir   string
	KeySize   int
	Provider  map[string]string
	Account   map[string]account
	Desire    map[string]desire
	Hook      map[string]string
}

type account struct {
	Mail    string
	Phone   string
	KeySize int
	KeyFile string
}

type desire struct {
	Provider string
	Account  string
	Altnames []string
	KeySize  int
	KeyFile  string
	CrtFile  string
	Webroot  string
	Hook     []string
}

var (
	errNoProvider = errors.New("no provider specified")
	errNoAccount  = errors.New("no account specified")
	errNoKey      = errors.New("no key file specified")
	errNoCrt      = errors.New("no crt file specified")
	errNoAltNames = errors.New("no altnames specified")
	errNoMail     = errors.New("no mail specified")
)

func LoadConfig(fname string) (*Config, error) {
	conf, err := ioutil.ReadFile(fname)
	if err != nil {
		return nil, err
	}
	c := &Config{}
	err = yaml.Unmarshal(conf, c)
	if err != nil {
		return nil, err
	}
	if strings.HasPrefix(c.BaseDir, "~") {
		usr, err := user.Current()
		if err != nil {
			return nil, err
		}
		c.BaseDir = path.Join(usr.HomeDir, c.BaseDir[1:])
	}
	// apply defaults
	if c.KeySize == 0 {
		c.KeySize = defKeySize
	}
	for k, v := range c.Account {
		if v.KeySize == 0 {
			v.KeySize = c.KeySize
		}
		if v.Mail == "" {
			return nil, errNoMail
		}
		if v.KeyFile == "" {
			return nil, errNoKey
		}
		if c.BaseDir != "" {
			v.KeyFile = path.Join(c.BaseDir, v.KeyFile)
		}
		c.Account[k] = v
	}
	for k, v := range c.Desire {
		if v.Provider == "" {
			return nil, errNoProvider
		}
		if v.Account == "" {
			return nil, errNoAccount
		}
		if v.KeySize == 0 {
			v.KeySize = c.KeySize
		}
		if v.KeyFile == "" {
			return nil, errNoKey
		}
		if v.CrtFile == "" {
			return nil, errNoCrt
		}
		if c.BaseDir != "" {
			v.KeyFile = path.Join(c.BaseDir, v.KeyFile)
			v.CrtFile = path.Join(c.BaseDir, v.CrtFile)
		}
		v.Altnames = checkWWW(v.Altnames)
		c.Desire[k] = v
	}
	return c, nil
}

func checkWWW(altnames []string) []string {
	ch := make(chan string)
	go func(ch chan string, s []string) {
		for _, an := range s {
			if strings.HasPrefix(an, "www.") {
				ch <- an[4:]
			}
		}
		close(ch)
	}(ch, altnames)
	has := func(s string) bool {
		for _, an := range altnames {
			if an == s {
				return true
			}
		}
		return false
	}
	for d := range ch {
		if !has(d) {
			altnames = append(altnames, d)
		}
	}
	return altnames
}