fix change password through prosodyctl

This commit is contained in:
Urko 2023-07-07 23:55:20 +02:00
parent cc3c3b07b2
commit 37ac1ac797
2 changed files with 26 additions and 8 deletions

View File

@ -2,7 +2,6 @@ package handler
import (
"fmt"
"log"
"gitea.urkob.com/urko/prosody-password/internal/services/fail2ban"
"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", ""),
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 {
for _, ip := range c.IPs() {
handler.fail2banSrv.FailedAttempt(ip)

View File

@ -6,7 +6,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"log"
"io"
"os/exec"
"strconv"
@ -18,13 +18,11 @@ func (p *Prosody) ChangePassword(user string, currentPwd string, newPwd string)
if err != nil {
return fmt.Errorf("p.loadAccount %w", err)
}
log.Printf("loadAccount %+v", *acc)
iterationCount, err := strconv.Atoi(acc.IterationCount)
if err != nil {
return fmt.Errorf("strconv.Atoi %w", err)
}
log.Println("user , currentPwd , newPwd ", user, currentPwd, newPwd)
storedKey, err := hashPassword(currentPwd, acc.Salt, iterationCount)
if err != nil {
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")
}
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 {
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
}