aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/batch/files.go61
-rw-r--r--cmd/batch/main.go2
2 files changed, 55 insertions, 8 deletions
diff --git a/cmd/batch/files.go b/cmd/batch/files.go
index cc4cad5..fee7a1a 100644
--- a/cmd/batch/files.go
+++ b/cmd/batch/files.go
@@ -3,26 +3,73 @@ package main
import (
"crypto/tls"
"crypto/x509"
+ "io"
+ "log"
+ "os"
"path"
"path/filepath"
+
+ "dim13.org/acme"
)
-func scanFiles(dir string) ([]tls.Certificate, error) {
- var certs []tls.Certificate
+type Cert struct {
+ tls.Certificate
+ keyFile string
+ crtFile string
+}
+
+func loadFiles(crtFile, keyFile string) (Cert, error) {
+ crt, err := tls.LoadX509KeyPair(crtFile, keyFile)
+ if err != nil {
+ return Cert{}, err
+ }
+ crt.Leaf, err = x509.ParseCertificate(crt.Certificate[0])
+ if err != nil {
+ return Cert{}, err
+ }
+ return Cert{Certificate: crt, keyFile: keyFile, crtFile: crtFile}, nil
+}
+
+func newFile(fname string, mode os.FileMode) (io.WriteCloser, error) {
+ os.Rename(fname, fname[:len(fname)-4]+".old")
+ return os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
+}
+
+func (c Cert) saveFiles() error {
+ fd, err := newFile(c.keyFile, 0600)
+ if err != nil {
+ return err
+ }
+ defer fd.Close()
+ if err := acme.SaveKey(fd, c.Certificate.PrivateKey); err != nil {
+ return err
+ }
+ fd, err = newFile(c.crtFile, 0644)
+ if err != nil {
+ return err
+ }
+ defer fd.Close()
+ for _, crt := range c.Certificate.Certificate {
+ if err := acme.SaveCert(fd, crt); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func scanFiles(dir string) ([]Cert, error) {
+ var certs []Cert
keys, err := filepath.Glob(path.Join(dir, "private", "*.key"))
if err != nil {
return nil, err
}
for _, k := range keys {
c := filepath.Join(dir, "certs", filepath.Base(k[:len(k)-4])+".pem")
- crt, err := tls.LoadX509KeyPair(c, k)
+ crt, err := loadFiles(c, k)
if err != nil {
+ log.Println(err)
continue
}
- crt.Leaf, err = x509.ParseCertificate(crt.Certificate[0])
- if err != nil {
- return nil, err
- }
certs = append(certs, crt)
}
return certs, nil
diff --git a/cmd/batch/main.go b/cmd/batch/main.go
index 208bc34..f0a75f0 100644
--- a/cmd/batch/main.go
+++ b/cmd/batch/main.go
@@ -8,7 +8,7 @@ func main() {
log.Fatal(err)
}
for _, c := range crt {
- log.Printf("%+v\n", c.Leaf)
+ log.Printf("%+v\n", c)
}
if len(*emails) > 0 && len(*domains) > 0 {
log.Println(*emails)