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 (
|
2023-11-02 18:28:07 +00:00
|
|
|
"errors"
|
2023-08-24 19:12:08 +00:00
|
|
|
"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.
|
2023-11-02 18:28:07 +00:00
|
|
|
// The returned authResponse indicates if the user is authorized,
|
|
|
|
// and if additional steps are needed to authenticate the user.
|
|
|
|
// If the user is authenticated, but not authorized to use the client, an error is returned.
|
|
|
|
func authorizeSynology(r *http.Request) (resp authResponse, err error) {
|
|
|
|
if !hasSynoToken(r) {
|
|
|
|
return authResponse{OK: false, AuthNeeded: synoAuth}, nil
|
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 {
|
2023-11-02 18:28:07 +00:00
|
|
|
return resp, fmt.Errorf("auth: %v: %s", err, out)
|
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 {
|
2023-11-02 18:28:07 +00:00
|
|
|
return resp, err
|
2023-08-24 19:46:51 +00:00
|
|
|
}
|
|
|
|
if !isAdmin {
|
2023-11-02 18:28:07 +00:00
|
|
|
return resp, errors.New("not a member of administrators group")
|
2023-08-24 19:46:51 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 18:28:07 +00:00
|
|
|
return authResponse{OK: true}, nil
|
2023-08-24 19:46:51 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 18:28:07 +00:00
|
|
|
// hasSynoToken returns true if the request include a SynoToken used for synology auth.
|
|
|
|
func hasSynoToken(r *http.Request) bool {
|
2023-08-24 19:12:08 +00:00
|
|
|
if r.Header.Get("X-Syno-Token") != "" {
|
2023-11-02 18:28:07 +00:00
|
|
|
return true
|
2023-08-24 19:12:08 +00:00
|
|
|
}
|
|
|
|
if r.URL.Query().Get("SynoToken") != "" {
|
2023-11-02 18:28:07 +00:00
|
|
|
return true
|
2023-08-24 19:12:08 +00:00
|
|
|
}
|
|
|
|
if r.Method == "POST" && r.FormValue("SynoToken") != "" {
|
2023-11-02 18:28:07 +00:00
|
|
|
return true
|
2023-08-24 19:12:08 +00:00
|
|
|
}
|
2023-11-02 18:28:07 +00:00
|
|
|
return false
|
2023-08-24 19:12:08 +00:00
|
|
|
}
|