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

import (
	"flag"
	"fmt"
	"log"
	"time"

	"dim13.org/acme"
)

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

func load(d desire) (*acme.Signer, error) {
	key, err := d.Load()
	if err != nil {
		return nil, err
	}
	log.Println("Load", d.KeyFile)
	return acme.NewSigner(key)
}

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

	sig, err := acme.NewSigner(key)
	if err != nil {
		return nil, err
	}

	con, err := acme.NewContacts(d.Mail, d.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(sig, con, agree); err != nil {
		return nil, err
	}
	return sig, nil
}

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

func requestCert(prov *acme.Provider, sig *acme.Signer, 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(sig, sols, alt); err != nil {
			return err
		}
	}

	log.Println("Request bundle for", d.Altnames)
	cert, err := prov.Bundle(sig, 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)
	}

	if *dumpConfig {
		out, err := conf.dump()
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(string(out))
		return
	}

	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)
	}

	log.Println("Dial", conf.Directory)
	prov, err := acme.DialProvider(conf.Directory)
	if err != nil {
		log.Fatal(err)
	}

	for _, d := range conf.Desire {
		if err := loadAccount(prov, d); err != nil {
			log.Fatal(err)
		}
	}
}