|
@@ -1,15 +1,15 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
- "net/http"
|
|
|
"log"
|
|
|
+ "net/http"
|
|
|
|
|
|
"os/exec"
|
|
|
"runtime"
|
|
|
)
|
|
|
|
|
|
-func HardwarePowerInit(){
|
|
|
- if (*allow_hardware_management){
|
|
|
+func HardwarePowerInit() {
|
|
|
+ if *allow_hardware_management {
|
|
|
//Only register these paths when hardware management is enabled
|
|
|
http.HandleFunc("/system/power/shutdown", hardware_power_poweroff)
|
|
|
http.HandleFunc("/system/power/restart", hardware_power_restart)
|
|
@@ -18,17 +18,17 @@ func HardwarePowerInit(){
|
|
|
http.HandleFunc("/system/power/accessCheck", hardware_power_checkIfHardware)
|
|
|
}
|
|
|
|
|
|
-func hardware_power_checkIfHardware(w http.ResponseWriter, r *http.Request){
|
|
|
- if (*allow_hardware_management){
|
|
|
+func hardware_power_checkIfHardware(w http.ResponseWriter, r *http.Request) {
|
|
|
+ if *allow_hardware_management {
|
|
|
sendJSONResponse(w, "true")
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
sendJSONResponse(w, "false")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func hardware_power_poweroff(w http.ResponseWriter, r *http.Request) {
|
|
|
- userinfo, err := userHandler.GetUserInfoFromRequest(w,r)
|
|
|
- if err != nil{
|
|
|
+ userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
|
|
|
+ if err != nil {
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
w.Write([]byte("401 Unauthorized"))
|
|
|
return
|
|
@@ -44,6 +44,19 @@ func hardware_power_poweroff(w http.ResponseWriter, r *http.Request) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ //Double check password for this user
|
|
|
+ password, err := mv(r, "pw", true)
|
|
|
+ if err != nil {
|
|
|
+ sendErrorResponse(w, "Password Incorrect")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ passwordCorrect := authAgent.ValidateUsernameAndPassword(userinfo.Username, password)
|
|
|
+ if !passwordCorrect {
|
|
|
+ sendErrorResponse(w, "Password Incorrect")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
if runtime.GOOS == "windows" {
|
|
|
//Only allow Linux to do power operation
|
|
|
cmd := exec.Command("shutdown", "-s", "-t", "20")
|
|
@@ -81,8 +94,8 @@ func hardware_power_poweroff(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
func hardware_power_restart(w http.ResponseWriter, r *http.Request) {
|
|
|
- userinfo, err := userHandler.GetUserInfoFromRequest(w,r)
|
|
|
- if err != nil{
|
|
|
+ userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
|
|
|
+ if err != nil {
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
w.Write([]byte("401 Unauthorized"))
|
|
|
return
|