change password file directly
This commit is contained in:
parent
c4cbab2f38
commit
18be67f363
53
main.go
53
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue