aboutsummaryrefslogtreecommitdiff
path: root/cmd/acme/config.go
blob: eca811741dd433b494712bc24c10ab3f421c73c1 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main

import (
	"errors"
	"os"
	"path"
	"strings"
	"time"

	"dim13.org/acme"

	"github.com/BurntSushi/toml"
)

const defKeySize = 2048

type Config struct {
	Defaults defaults
	Provider map[string]*provider
	Account  map[string]*account
	Hook     map[string]*hook
	Desire   map[string]*desire
}

type defaults struct {
	Gracetime duration
	Listen    string
	ListenTLS string
	Provider  string
	Account   string
	Basedir   string
	Size      int
}

type provider struct {
	Directory string
	*acme.Provider
}

type account struct {
	Mail  string
	Phone string
	Size  int
	Key   string
	*acme.Account
	acme.Contacts
}

type hook struct {
	CMD string
}

type desire struct {
	Provider string
	Account  string
	Altnames []string
	Size     int
	Key      string
	Cert     string
	Webroot  string
	Hooks    []string
	provider *provider
	account  *account
	*acme.Desire
}

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

func LoadConfig(fname string) (*Config, error) {
	c := &Config{}
	_, err := toml.DecodeFile(fname, c)
	if err != nil {
		return nil, err
	}
	// apply defaults
	if c.Defaults.Size == 0 {
		c.Defaults.Size = defKeySize
	}
	if c.Defaults.Listen == "" {
		c.Defaults.Listen = "localhost:8080"
	}
	if c.Defaults.ListenTLS == "" {
		c.Defaults.ListenTLS = c.Defaults.Listen
	}
	for k, v := range c.Account {
		if v.Size == 0 {
			v.Size = c.Defaults.Size
		}
		if v.Mail == "" {
			return nil, errNoMail
		}
		if v.Key == "" {
			return nil, errNoKey
		}
		if c.Defaults.Basedir != "" {
			v.Key = path.Join(c.Defaults.Basedir, v.Key)
		}
		c.Account[k] = v
	}
	for k, v := range c.Desire {
		if v.Provider == "" {
			if c.Defaults.Provider != "" {
				v.Provider = c.Defaults.Provider
			} else {
				return nil, errNoProvider
			}
		}
		v.provider = c.Provider[v.Provider]
		if v.Account == "" {
			if c.Defaults.Account != "" {
				v.Account = c.Defaults.Account
			} else {
				return nil, errNoAccount
			}
		}
		v.account = c.Account[v.Account]
		if v.Size == 0 {
			v.Size = c.Defaults.Size
		}
		if v.Key == "" {
			return nil, errNoKey
		}
		if v.Cert == "" {
			return nil, errNoCert
		}
		if c.Defaults.Basedir != "" {
			v.Key = path.Join(c.Defaults.Basedir, v.Key)
			v.Cert = path.Join(c.Defaults.Basedir, v.Cert)
		}
		switch len(v.Altnames) {
		case 0:
			return nil, errNoAltNames
		case 1:
			an := v.Altnames[0]
			if strings.HasPrefix(an, "www.") {
				v.Altnames = append(v.Altnames, an[4:])
			}
		}
		c.Desire[k] = v
	}
	return c, nil
}

type PrivKey interface {
	KeyPath() string
	KeySize() int
	HasKey() bool
}

type Cert interface {
	CertPath() string
	HasCert() bool
}

func exists(fname string) bool {
	if _, err := os.Stat(fname); os.IsNotExist(err) {
		return false
	}
	return true
}

func (d desire) CertPath() string { return d.Cert }
func (d desire) KeyPath() string  { return d.Key }
func (d desire) KeySize() int     { return d.Size }
func (d desire) HasKey() bool     { return exists(d.Key) }
func (d desire) HasCert() bool    { return exists(d.Cert) }

func (a account) KeyPath() string { return a.Key }
func (a account) KeySize() int    { return a.Size }
func (a account) HasKey() bool    { return exists(a.Key) }

type duration struct{ time.Duration }

func (d *duration) UnmarshalText(s []byte) error {
	var err error
	d.Duration, err = time.ParseDuration(string(s))
	return err
}