Copied target now gets the owner chown.

master
Zed A. Shaw 2 months ago
parent ee3da7c610
commit 9a283a64ec
  1. 35
      main.go

@ -10,6 +10,8 @@ import (
"time"
"path"
"path/filepath"
"os/user"
"strconv"
)
type Cert struct {
@ -86,7 +88,29 @@ func ParseOpts() Config {
return LoadConfig(config_file)
}
func Copy(from string, to string) {
func ChownTarget(fname string, owner string) {
u, err := user.Lookup(owner)
if err != nil {
log.Fatalf("failed to find owner %s: %v", fname, err)
}
uid, err := strconv.Atoi(u.Uid)
if err != nil {
log.Fatalf("UID %s gives bad result when calling user.Lookup()", u.Uid)
}
gid, err := strconv.Atoi(u.Gid)
if err != nil {
log.Fatalf("GID %s gives bad result when calling user.Lookup()", u.Gid)
}
err = os.Chown(fname, uid, gid)
if err != nil {
log.Fatalf("Error cannot chown file %s to user %s: %v", fname, owner, err)
}
}
func Copy(from string, to string, owner string) {
log.Println("copying from=", from, "to=", to)
src, err := os.Open(from)
@ -99,6 +123,8 @@ func Copy(from string, to string) {
_, err = io.Copy(dst, src)
ChownTarget(to, owner)
if err != nil {
log.Fatalf("failed to copy: %v", err)
}
@ -106,11 +132,10 @@ func Copy(from string, to string) {
func (cfg *Config) SyncCerts() {
log.Println("SYNC CERTS CALLED")
// copy the files
Copy(cfg.Source.PrivateKey, cfg.Target.PrivateKey)
Copy(cfg.Source.PublicKey, cfg.Target.PublicKey)
// copy the files, also changes ownership of target
Copy(cfg.Source.PrivateKey, cfg.Target.PrivateKey, cfg.Target.Owner)
Copy(cfg.Source.PublicKey, cfg.Target.PublicKey, cfg.Target.Owner)
// change the ownership
// restart the service
}

Loading…
Cancel
Save