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

import (
	"flag"
	"log"

	"dim13.org/acme"
)

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

func main() {
	flag.Parse()

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

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

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

		for _, v := range v.Account {
			log.Println("Load", v.KeyFile)
			key, err := v.Load()
			if err != nil {
				key, err = acme.NewKey(v.KeySize)
				if err != nil {
					log.Fatal(err)
				}
				if err := v.Save(key); err != nil {
					log.Fatal(err)
				}
			}

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

			con, err := acme.NewContacts(v.Mail, v.Phone)
			if err != nil {
				log.Fatal(err)
			}

			log.Println("Register", con)
			err = prov.Register(acc, con)
			if err != nil {
				log.Fatal(err)
			}

			for _, v := range v.Domain {
				c, err := v.Load()
				if err != nil {
					log.Println(err)
				}
				if c.Leaf != nil && !conf.renew(c.Leaf) && !*forceRenew {
					log.Println("valid until", c.Leaf.NotAfter, "sipping")
					continue
				}

				key, err := acme.NewKey(v.KeySize)
				if err != nil {
					log.Fatal(err)
				}
				des := acme.NewDesire(key, v.Altnames)
				if v.Webroot != "" {
					sol := acme.NewWebrootSolver(v.Webroot)
					des.RegisterSolver(sol)
				} else if httpSol != nil {
					des.RegisterSolver(httpSol)
				}
				if tlsSol != nil {
					des.RegisterSolver(tlsSol)
				}
				if !des.HasSolver() {
					log.Fatal("no solver")
				}

				log.Println("Authorize", v.Altnames)
				if err := prov.Authorize(acc, des); err != nil {
					log.Fatal(err)
				}

				log.Println("Request bundle for", v.Altnames)
				cert, err := prov.Bundle(acc, des)
				if err != nil {
					log.Fatal(err)
				}

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