change password file directly

This commit is contained in:
josh 2024-05-07 07:54:41 -04:00
parent c4cbab2f38
commit 18be67f363
1 changed files with 49 additions and 4 deletions

53
main.go
View File

@ -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)
}