aboutsummaryrefslogtreecommitdiff
path: root/cmd/acme/main.go
blob: 2c977c7b8db2dd4278a3fc9dbc36f95a4113b269 (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
// ACME CLI tool
package main

import (
	"flag"
	"log"
	"time"

	"dim13.org/acme"
)

var (
	confName        = flag.String("conf", "", "configuration file")
	forceRenew      = flag.Bool("force", false, "force renew")
	httpSol, tlsSol acme.Solver
)

func dialProvider(p provider) error {
	log.Println("Dial", p.Directory)
	prov, err := acme.DialProvider(p.Directory)
	if err != nil {
		return err
	}
	for _, a := range p.Account {
		if err := loadAccount(prov, a); err != nil {
			return err
		}
	}
	return nil
}

func load(a account) (*acme.Account, error) {
	key, err := a.Load()
	if err != nil {
		return nil, err
	}
	log.Println("Load", a.KeyFile)
	return acme.NewAccount(key)
}

func register(prov *acme.Provider, a account) (*acme.Account, error) {
	key, err := acme.NewKey(a.KeySize)
	if err != nil {
		return nil, err
	}
	defer a.Save(key)

	acc, err := acme.NewAccount(key)
	if err != nil {
		return nil, err
	}

	con, err := acme.NewContacts(a.Mail, a.Phone)
	if err != nil {
		return nil, err
	}

	agree := func(tos string) bool {
		log.Println("agree to", tos)
		return true
	}

	log.Println("Register", con)
	if err := prov.Register(acc, con, agree); err != nil {
		return nil, err
	}
	return acc, nil
}

func loadAccount(prov *acme.Provider, a account) error {
	acc, err := load(a)
	if err != nil {
		acc, err = register(prov, a)
		if err != nil {
			return err
		}
	}
	for _, d := range a.Domain {
		if err := requestCert(prov, acc, d); err != nil {
			return err
		}
	}
	return nil
}

func requestCert(prov *acme.Provider, acc *acme.Account, d domain) error {
	c, err := d.Load()
	if err != nil {
		c.PrivateKey, err = acme.NewKey(d.KeySize)
		if err != nil {
			return err
		}
	}
	skipValid := func() bool {
		if c.Leaf == nil || *forceRenew {
			return false
		}
		return time.Now().Add(d.Gracetime).Before(c.Leaf.NotAfter)
	}
	if skipValid() {
		log.Println("skip valid until", c.Leaf.NotAfter)
		return nil
	}

	sols := acme.NewSolvers()
	if d.Webroot != "" {
		sols.Add(acme.NewWebrootSolver(d.Webroot))
	} else {
		sols.Add(httpSol)
		sols.Add(tlsSol)
	}

	for _, alt := range d.Altnames {
		log.Println("Authorize", alt)
		if err := prov.Authorize(acc, sols, alt); err != nil {
			return err
		}
	}

	log.Println("Request bundle for", d.Altnames)
	cert, err := prov.Bundle(acc, c.PrivateKey, d.Altnames)
	if err != nil {
		return err
	}

	log.Println("Save", d.CrtFile, d.KeyFile)
	if err := d.Save(cert); err != nil {
		return err
	}

	return nil
}

func main() {
	flag.Parse()

	conf, err := LoadConfig(*confName)
	if err != nil {
		log.Fatal(err)
	}

	httpSol, err = acme.NewHTTPSolver(conf.Listen)
	if err != nil {
		log.Println("HTTP Solver", err)
	}
	tlsSol, err = acme.NewTLSSolver(conf.ListenTLS)
	if err != nil {
		log.Println("TLS Solver", err)
	}

	for _, p := range conf.Provider {
		if err := dialProvider(p); err != nil {
			log.Fatal(err)
		}
	}
}