From 37ac1ac797e42804d7d1507758af87783576dbfc Mon Sep 17 00:00:00 2001 From: Urko Date: Fri, 7 Jul 2023 23:55:20 +0200 Subject: [PATCH] fix change password through prosodyctl --- internal/api/handler/prosody_hdl.go | 2 -- internal/services/prosody/change_password.go | 32 ++++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/internal/api/handler/prosody_hdl.go b/internal/api/handler/prosody_hdl.go index 84e87d3..87e6b12 100644 --- a/internal/api/handler/prosody_hdl.go +++ b/internal/api/handler/prosody_hdl.go @@ -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) diff --git a/internal/services/prosody/change_password.go b/internal/services/prosody/change_password.go index 89be60f..6d989b4 100644 --- a/internal/services/prosody/change_password.go +++ b/internal/services/prosody/change_password.go @@ -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 }