2020-08-01 15:31:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-08-16 12:36:54 +00:00
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2020-08-01 15:31:08 +00:00
|
|
|
)
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) StartPWR() {
|
|
|
|
app.info.Println("Starting password reset daemon")
|
|
|
|
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) {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("Failed to start password reset daemon: Directory \"%s\" doesn't exist", path)
|
2020-08-01 15:31:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("Couldn't initialise password reset daemon")
|
2020-08-01 15:31:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
done := make(chan bool)
|
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 {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("Failed to start password reset daemon: %s", err)
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(data, &pwr)
|
|
|
|
if len(pwr.Pin) == 0 || err != nil {
|
|
|
|
return
|
|
|
|
}
|
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) {
|
2020-11-02 00:53:08 +00:00
|
|
|
user, status, err := app.jf.UserByName(pwr.Username, false)
|
2020-08-01 15:31:08 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("Failed to get users from Jellyfin: Code %d", status)
|
|
|
|
app.debug.Printf("Error: %s", err)
|
2020-08-01 15:31:08 +00:00
|
|
|
return
|
|
|
|
}
|
2020-08-16 12:36:54 +00:00
|
|
|
app.storage.loadEmails()
|
2020-09-16 10:55:35 +00:00
|
|
|
var address string
|
2020-09-16 18:19:04 +00:00
|
|
|
uid := user["Id"]
|
|
|
|
if uid == nil {
|
|
|
|
app.err.Printf("Couldn't get user ID for user \"%s\"", pwr.Username)
|
|
|
|
app.debug.Printf("user maplength: %d", len(user))
|
|
|
|
return
|
|
|
|
}
|
2020-09-16 10:55:35 +00:00
|
|
|
addr, ok := app.storage.emails[user["Id"].(string)]
|
|
|
|
if !ok || addr == nil {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("Couldn't find email for user \"%s\". Make sure it's set", pwr.Username)
|
2020-08-01 15:31:08 +00:00
|
|
|
return
|
|
|
|
}
|
2020-09-16 10:55:35 +00:00
|
|
|
address = addr.(string)
|
2020-09-13 20:18:47 +00:00
|
|
|
msg, err := app.email.constructReset(pwr, app)
|
|
|
|
if err != nil {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("Failed to construct password reset email for %s", pwr.Username)
|
2020-09-13 20:07:15 +00:00
|
|
|
app.debug.Printf("%s: Error: %s", pwr.Username, err)
|
2021-02-18 14:58:53 +00:00
|
|
|
} else if err := app.email.send(msg, address); err != nil {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("Failed to send password reset email to \"%s\"", address)
|
2020-09-13 20:07:15 +00:00
|
|
|
app.debug.Printf("%s: Error: %s", pwr.Username, err)
|
2020-08-01 15:31:08 +00:00
|
|
|
} else {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.info.Printf("Sent password reset email to \"%s\"", address)
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("Password reset for user \"%s\" has already expired (%s). Check your time settings.", pwr.Username, pwr.Expiry)
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
case err, ok := <-watcher.Errors:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("Password reset daemon: %s", err)
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|