fix change password through prosodyctl
This commit is contained in:
parent
cc3c3b07b2
commit
37ac1ac797
|
@ -2,7 +2,6 @@ package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"gitea.urkob.com/urko/prosody-password/internal/services/fail2ban"
|
"gitea.urkob.com/urko/prosody-password/internal/services/fail2ban"
|
||||||
"gitea.urkob.com/urko/prosody-password/internal/services/prosody"
|
"gitea.urkob.com/urko/prosody-password/internal/services/prosody"
|
||||||
|
@ -33,7 +32,6 @@ func (handler ProsodyHandler) Post(c *fiber.Ctx) error {
|
||||||
NewPassword: c.FormValue("new_password", ""),
|
NewPassword: c.FormValue("new_password", ""),
|
||||||
User: c.FormValue("user", ""),
|
User: c.FormValue("user", ""),
|
||||||
}
|
}
|
||||||
log.Println("req.User, req.CurrentPassword, req.NewPassword", req.User, req.CurrentPassword, req.NewPassword)
|
|
||||||
if err := handler.prosodyService.ChangePassword(req.User, req.CurrentPassword, req.NewPassword); err != nil {
|
if err := handler.prosodyService.ChangePassword(req.User, req.CurrentPassword, req.NewPassword); err != nil {
|
||||||
for _, ip := range c.IPs() {
|
for _, ip := range c.IPs() {
|
||||||
handler.fail2banSrv.FailedAttempt(ip)
|
handler.fail2banSrv.FailedAttempt(ip)
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -18,13 +18,11 @@ func (p *Prosody) ChangePassword(user string, currentPwd string, newPwd string)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("p.loadAccount %w", err)
|
return fmt.Errorf("p.loadAccount %w", err)
|
||||||
}
|
}
|
||||||
log.Printf("loadAccount %+v", *acc)
|
|
||||||
|
|
||||||
iterationCount, err := strconv.Atoi(acc.IterationCount)
|
iterationCount, err := strconv.Atoi(acc.IterationCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("strconv.Atoi %w", err)
|
return fmt.Errorf("strconv.Atoi %w", err)
|
||||||
}
|
}
|
||||||
log.Println("user , currentPwd , newPwd ", user, currentPwd, newPwd)
|
|
||||||
storedKey, err := hashPassword(currentPwd, acc.Salt, iterationCount)
|
storedKey, err := hashPassword(currentPwd, acc.Salt, iterationCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("hashPassword: %w", err)
|
return fmt.Errorf("hashPassword: %w", err)
|
||||||
|
@ -35,12 +33,34 @@ func (p *Prosody) ChangePassword(user string, currentPwd string, newPwd string)
|
||||||
return errors.New("password is incorrect")
|
return errors.New("password is incorrect")
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := exec.Command("/usr/bin/prosodyctl", "-c", "passwd -s 12 -scny 1").Output()
|
cmd := exec.Command("/usr/bin/prosodyctl", "passwd", user+"@"+p.plainDomain)
|
||||||
|
// Create a pipe to write to the process's standard input.
|
||||||
|
stdin, err := cmd.StdinPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("prosodcytl: %w", err)
|
return fmt.Errorf("creating stdin pipe: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the process.
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return fmt.Errorf("starting command: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the password to the process's standard input.
|
||||||
|
_, err = io.WriteString(stdin, newPwd+"\n"+newPwd+"\n")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("writing to stdin pipe: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the pipe to indicate that we're done writing.
|
||||||
|
if err := stdin.Close(); err != nil {
|
||||||
|
return fmt.Errorf("closing stdin pipe: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for the command to finish.
|
||||||
|
if err := cmd.Wait(); err != nil {
|
||||||
|
return fmt.Errorf("waiting for command: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("string(result)", string(result))
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue