From 18be67f3631b07a877efdbc2de58efd42f843569 Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 7 May 2024 07:54:41 -0400 Subject: [PATCH] change password file directly --- main.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index d77aba9..2a773d9 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,13 @@ package main import ( + "crypto" + "fmt" "log" "net/http" "os" - "os/exec" "path" + "strings" "text/template" "time" @@ -123,9 +125,8 @@ func main() { tmpl.Execute(w, d) return } - cm := exec.Command("/root/docker/mail/setup.sh", "email", "update", f.GetValue("email"), f.GetValue("newpassword")) - err = cm.Run() - if err != nil { + // Change the password + if !modifyPasswordFile(f.GetValue("email"), f.GetValue("newpassword")) { log.Println(err) d.Body = "Internal error" tmpl.Execute(w, d) @@ -139,3 +140,47 @@ func main() { tmpl.Execute(os.Stdout, d) http.ListenAndServe("127.0.0.1:8910", r) } + +/* +example password entry: +recreation@pinnaclelake.com|{SHA512-CRYPT}$6$uTiy7Q5xn1CIN22w$3VAElns3TFfejtdTCTJMcz8k2UPPwAmlUIoXC4NgPJRnDcgo3CnI91EsB6irgwuecCrolxAx7i4mjvTxPaAlf0 + +*/ + +// createPasswordEntry: input email and plain text password, output a password entry with sha512-crypt. + +func modifyPasswordFile(email, password string) bool { + // read the password file + // find the entry with the given email + // replace the password with the new password + // write the file back + in, err := os.ReadFile("/root/docker/mail/config/postfix-accounts") + if err != nil { + log.Println(err) + return false + } + out, err := os.Create("/root/docker/mail/config/postfix-accounts.tmp") + if err != nil { + log.Println(err) + return false + } + + lines := strings.Split(string(in), "\n") + for _, line := range lines { + if strings.HasPrefix(line, email) { + line = createPasswordEntry(email, password) + } + out.WriteString(line) + out.WriteString("\n") + } + out.Close() + os.Rename("/root/docker/mail/config/postfix-accounts.tmp", "/root/docker/mail/config/postfix-accounts") + return true +} + +func createPasswordEntry(email, password string) string { + c := crypto.SHA512.New() + c.Write([]byte(password)) + hash := c.Sum(nil) + return fmt.Sprintf("%s|{SHA512-CRYPT}$6$%s", email, hash) +}