2023-08-24 19:12:08 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
// synology.go contains handlers and logic, such as authentication,
|
|
|
|
// that is specific to running the web client on Synology.
|
|
|
|
|
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"tailscale.com/util/groupmember"
|
|
|
|
)
|
|
|
|
|
2023-08-24 19:46:51 +00:00
|
|
|
// authorizeSynology authenticates the logged-in Synology user and verifies
|
2023-09-26 19:57:40 +00:00
|
|
|
// that they are authorized to use the web client.
|
|
|
|
// It reports true if the request is authorized to continue, and false otherwise.
|
|
|
|
// authorizeSynology manages writing out any relevant authorization errors to the
|
|
|
|
// ResponseWriter itself.
|
|
|
|
func authorizeSynology(w http.ResponseWriter, r *http.Request) (ok bool) {
|
2023-08-24 19:46:51 +00:00
|
|
|
if synoTokenRedirect(w, r) {
|
2023-09-26 19:57:40 +00:00
|
|
|
return false
|
2023-08-24 19:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// authenticate the Synology user
|
|
|
|
cmd := exec.Command("/usr/syno/synoman/webman/modules/authenticate.cgi")
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("auth: %v: %s", err, out), http.StatusUnauthorized)
|
2023-09-26 19:57:40 +00:00
|
|
|
return false
|
2023-08-24 19:46:51 +00:00
|
|
|
}
|
|
|
|
user := strings.TrimSpace(string(out))
|
|
|
|
|
|
|
|
// check if the user is in the administrators group
|
|
|
|
isAdmin, err := groupmember.IsMemberOfGroup("administrators", user)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusForbidden)
|
2023-09-26 19:57:40 +00:00
|
|
|
return false
|
2023-08-24 19:46:51 +00:00
|
|
|
}
|
|
|
|
if !isAdmin {
|
|
|
|
http.Error(w, "not a member of administrators group", http.StatusForbidden)
|
2023-09-26 19:57:40 +00:00
|
|
|
return false
|
2023-08-24 19:46:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 19:57:40 +00:00
|
|
|
return true
|
2023-08-24 19:46:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 19:12:08 +00:00
|
|
|
func synoTokenRedirect(w http.ResponseWriter, r *http.Request) bool {
|
|
|
|
if r.Header.Get("X-Syno-Token") != "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r.URL.Query().Get("SynoToken") != "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r.Method == "POST" && r.FormValue("SynoToken") != "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// We need a SynoToken for authenticate.cgi.
|
|
|
|
// So we tell the client to get one.
|
|
|
|
_, _ = fmt.Fprint(w, synoTokenRedirectHTML)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-08-24 19:46:51 +00:00
|
|
|
const synoTokenRedirectHTML = `<html>
|
2023-08-24 19:12:08 +00:00
|
|
|
Redirecting with session token...
|
|
|
|
<script>
|
2023-08-24 19:46:51 +00:00
|
|
|
fetch("/webman/login.cgi")
|
|
|
|
.then(r => r.json())
|
|
|
|
.then(data => {
|
|
|
|
u = new URL(window.location)
|
|
|
|
u.searchParams.set("SynoToken", data.SynoToken)
|
|
|
|
document.location = u
|
|
|
|
})
|
2023-08-24 19:12:08 +00:00
|
|
|
</script>
|
|
|
|
`
|