aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-03-01 14:54:26 +0100
committerDimitri Sokolyuk <demon@dim13.org>2016-03-01 14:54:26 +0100
commit22b6e21cbb304403d8ce9d14060c6a876cf2c0e8 (patch)
tree1971d8bf6e60e1541faba169f066abfe1d4e2e98
parentfb2e95cde5b0376522bad6a5d54fcc4d021c2dd6 (diff)
Cleanup file operations
-rw-r--r--account.go4
-rw-r--r--cmd/acme/config.go2
-rw-r--r--crypto.go21
-rw-r--r--desire.go22
-rw-r--r--solve_webroot.go6
5 files changed, 18 insertions, 37 deletions
diff --git a/account.go b/account.go
index 9831db7..a5a3886 100644
--- a/account.go
+++ b/account.go
@@ -91,10 +91,10 @@ func (a *Account) KeyAuth(token string) (string, error) {
}
func (a *Account) Save(fname string) error {
- fd, err := CreatePrivFile(fname)
+ fd, err := CreateFile(fname, 0600)
if err != nil {
return err
}
defer fd.Close()
- return saveKey(fd, a.key)
+ return SaveKey(fd, a.key)
}
diff --git a/cmd/acme/config.go b/cmd/acme/config.go
index 40bb3c1..8149393 100644
--- a/cmd/acme/config.go
+++ b/cmd/acme/config.go
@@ -138,7 +138,7 @@ func LoadConfig(fname string) (*Config, error) {
dom.KeyFile = path.Join(keyPath, replace(d)+".key")
}
if dom.CrtFile == "" {
- dom.CrtFile = path.Join(crtPath, replace(d)+".crt")
+ dom.CrtFile = path.Join(crtPath, replace(d)+".pem")
}
if c.BaseDir != "" {
dom.KeyFile = path.Join(c.BaseDir, dom.KeyFile)
diff --git a/crypto.go b/crypto.go
index ae8227c..5ed81c0 100644
--- a/crypto.go
+++ b/crypto.go
@@ -10,7 +10,7 @@ import (
"io"
"io/ioutil"
"os"
- "path"
+ "syscall"
)
const (
@@ -46,7 +46,7 @@ func LoadKey(r io.Reader) (crypto.PrivateKey, error) {
}
}
-func saveKey(w io.Writer, key crypto.PrivateKey) error {
+func SaveKey(w io.Writer, key crypto.PrivateKey) error {
var block *pem.Block
switch k := key.(type) {
case *rsa.PrivateKey:
@@ -88,18 +88,11 @@ func LoadCerts(r io.Reader) ([]*x509.Certificate, error) {
return x509.ParseCertificates(block.Bytes)
}
-func CreatePrivFile(fname string) (io.WriteCloser, error) {
- return createFile(fname, 0700)
-}
-
-func CreatePubFile(fname string) (io.WriteCloser, error) {
- return createFile(fname, 0755)
-}
-
-func createFile(fname string, mode os.FileMode) (io.WriteCloser, error) {
- if err := os.MkdirAll(path.Dir(fname), mode); err != nil {
- return nil, err
+func CreateFile(fname string, mode os.FileMode) (io.WriteCloser, error) {
+ err := os.Rename(fname, fname+".bak")
+ if nerr, ok := err.(*os.LinkError); ok && nerr.Err != syscall.ENOENT {
+ return nil, nerr
}
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
- return os.OpenFile(fname, flags, mode&0644)
+ return os.OpenFile(fname, flags, mode)
}
diff --git a/desire.go b/desire.go
index 4de3b64..838b59b 100644
--- a/desire.go
+++ b/desire.go
@@ -7,8 +7,6 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
- "os"
- "syscall"
)
type Desire struct {
@@ -37,14 +35,6 @@ func (d *Desire) HasSolver() bool {
return len(d.solver) > 0
}
-func backup(fname string) error {
- err := os.Rename(fname, fname+".bak")
- if nerr, ok := err.(*os.LinkError); ok && nerr.Err == syscall.ENOENT {
- return nil
- }
- return err
-}
-
func (d *Desire) Save(cert, key string) error {
if err := d.saveKey(key); err != nil {
return err
@@ -53,22 +43,16 @@ func (d *Desire) Save(cert, key string) error {
}
func (d *Desire) saveKey(fname string) error {
- if err := backup(fname); err != nil {
- return err
- }
- fd, err := CreatePrivFile(fname)
+ fd, err := CreateFile(fname, 0600)
if err != nil {
return err
}
defer fd.Close()
- return saveKey(fd, d.cert.PrivateKey)
+ return SaveKey(fd, d.cert.PrivateKey)
}
func (d *Desire) saveCert(fname string) error {
- if err := backup(fname); err != nil {
- return err
- }
- fd, err := CreatePubFile(fname)
+ fd, err := CreateFile(fname, 0644)
if err != nil {
return err
}
diff --git a/solve_webroot.go b/solve_webroot.go
index dc3c878..096ca4a 100644
--- a/solve_webroot.go
+++ b/solve_webroot.go
@@ -19,7 +19,11 @@ func NewWebrootSolver(webroot string) Solver {
func (s *webrootSolver) Solve(ch Challenge) error {
s.file = path.Join(s.webroot, WellKnown, ch.Token)
log.Println("solve webroot", s.file)
- fd, err := CreatePubFile(s.file)
+ err := os.MkdirAll(path.Dir(s.file), 0755)
+ if err != nil {
+ return err
+ }
+ fd, err := CreateFile(s.file, 0644)
if err != nil {
return err
}