2020-08-01 15:31:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-08-12 17:53:46 +00:00
|
|
|
"errors"
|
2023-06-22 11:04:40 +00:00
|
|
|
"fmt"
|
2020-08-01 15:31:08 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-08-16 12:36:54 +00:00
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2024-08-01 19:17:05 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2020-08-01 15:31:08 +00:00
|
|
|
)
|
|
|
|
|
2021-10-13 14:04:22 +00:00
|
|
|
// GenInternalReset generates a local password reset PIN, for use with the PWR option on the Admin page.
|
|
|
|
func (app *appContext) GenInternalReset(userID string) (InternalPWR, error) {
|
|
|
|
pin := genAuthToken()
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.jf.UserByID(userID, false)
|
|
|
|
if err != nil {
|
2021-10-13 14:04:22 +00:00
|
|
|
return InternalPWR{}, err
|
|
|
|
}
|
|
|
|
pwr := InternalPWR{
|
|
|
|
PIN: pin,
|
|
|
|
Username: user.Name,
|
|
|
|
ID: userID,
|
|
|
|
Expiry: time.Now().Add(30 * time.Minute),
|
|
|
|
}
|
|
|
|
return pwr, nil
|
|
|
|
}
|
|
|
|
|
2023-06-22 11:04:40 +00:00
|
|
|
// GenResetLink generates and returns a password reset link.
|
|
|
|
func (app *appContext) GenResetLink(pin string) (string, error) {
|
2024-08-13 19:39:06 +00:00
|
|
|
url := app.ExternalURI
|
2023-06-22 11:04:40 +00:00
|
|
|
var pinLink string
|
|
|
|
if url == "" {
|
2024-08-12 17:53:46 +00:00
|
|
|
return pinLink, errors.New(lm.NoExternalHost)
|
2023-06-22 11:04:40 +00:00
|
|
|
}
|
|
|
|
// Strip /invite from end of this URL, ik it's ugly.
|
|
|
|
pinLink = fmt.Sprintf("%s/reset?pin=%s", url, pin)
|
|
|
|
return pinLink, nil
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) StartPWR() {
|
2024-08-10 18:30:14 +00:00
|
|
|
app.info.Printf(lm.StartDaemon, "PWR")
|
2020-08-16 12:36:54 +00:00
|
|
|
path := app.config.Section("password_resets").Key("watch_directory").String()
|
2020-08-01 15:31:08 +00:00
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedStartDaemon, "PWR", fmt.Sprintf(lm.PathNotFound, path))
|
2020-08-01 15:31:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedStartDaemon, "PWR", err)
|
2020-08-01 15:31:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
go pwrMonitor(app, watcher)
|
2020-08-01 15:31:08 +00:00
|
|
|
err = watcher.Add(path)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedStartDaemon, "PWR", err)
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
2023-06-11 15:35:41 +00:00
|
|
|
|
2023-06-11 18:48:03 +00:00
|
|
|
waitForRestart()
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 16:36:43 +00:00
|
|
|
// PasswordReset represents a passwordreset-xyz.json file generated by Jellyfin.
|
|
|
|
type PasswordReset struct {
|
2020-08-01 15:31:08 +00:00
|
|
|
Pin string `json:"Pin"`
|
|
|
|
Username string `json:"UserName"`
|
|
|
|
Expiry time.Time `json:"ExpirationDate"`
|
2021-10-13 14:04:22 +00:00
|
|
|
Internal bool `json:"Internal,omitempty"`
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func pwrMonitor(app *appContext, watcher *fsnotify.Watcher) {
|
2021-01-31 18:50:04 +00:00
|
|
|
if !emailEnabled {
|
|
|
|
return
|
|
|
|
}
|
2020-08-01 15:31:08 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-watcher.Events:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if event.Op&fsnotify.Write == fsnotify.Write && strings.Contains(event.Name, "passwordreset") {
|
2020-11-22 16:36:43 +00:00
|
|
|
var pwr PasswordReset
|
2021-01-31 23:12:50 +00:00
|
|
|
data, err := os.ReadFile(event.Name)
|
2020-08-01 15:31:08 +00:00
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Printf(lm.FailedReading, event.Name, err)
|
2020-08-01 15:31:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(data, &pwr)
|
|
|
|
if len(pwr.Pin) == 0 || err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Printf(lm.FailedReading, event.Name, err)
|
2023-06-11 15:35:41 +00:00
|
|
|
continue
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
2020-08-16 12:36:54 +00:00
|
|
|
app.info.Printf("New password reset for user \"%s\"", pwr.Username)
|
2020-09-15 11:00:20 +00:00
|
|
|
if currentTime := time.Now(); pwr.Expiry.After(currentTime) {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.jf.UserByName(pwr.Username, false)
|
|
|
|
if err != nil || user.ID == "" {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedGetUser, pwr.Username, lm.Jellyfin, err)
|
2020-08-01 15:31:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
uid := user.ID
|
2021-05-07 15:06:47 +00:00
|
|
|
name := app.getAddressOrName(uid)
|
|
|
|
if name != "" {
|
|
|
|
msg, err := app.email.constructReset(pwr, app, false)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedConstructPWRMessage, pwr.Username, err)
|
2021-05-07 15:06:47 +00:00
|
|
|
} else if err := app.sendByID(msg, uid); err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedSendPWRMessage, pwr.Username, name, err)
|
2021-05-07 15:06:47 +00:00
|
|
|
} else {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.SentPWRMessage, pwr.Username, name)
|
2021-05-07 15:06:47 +00:00
|
|
|
}
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.PWRExpired, pwr.Username, pwr.Expiry)
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
case err, ok := <-watcher.Errors:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedStartDaemon, "PWR", err)
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|