2019-09-16 06:52:58 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sys/windows/svc"
|
|
|
|
"golang.org/x/sys/windows/svc/eventlog"
|
|
|
|
"golang.org/x/sys/windows/svc/mgr"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2020-07-29 19:56:56 +00:00
|
|
|
"github.com/drakkan/sftpgo/ftpd"
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpd"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2019-09-16 06:52:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-06-22 17:11:53 +00:00
|
|
|
serviceName = "SFTPGo"
|
|
|
|
serviceDesc = "Full featured and highly configurable SFTP server"
|
|
|
|
rotateLogCmd = svc.Cmd(128)
|
|
|
|
acceptRotateLog = svc.Accepted(rotateLogCmd)
|
2019-09-16 06:52:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Status defines service status
|
|
|
|
type Status uint8
|
|
|
|
|
|
|
|
// Supported values for service status
|
|
|
|
const (
|
|
|
|
StatusUnknown Status = iota
|
|
|
|
StatusRunning
|
|
|
|
StatusStopped
|
|
|
|
StatusPaused
|
|
|
|
StatusStartPending
|
|
|
|
StatusPausePending
|
|
|
|
StatusContinuePending
|
|
|
|
StatusStopPending
|
|
|
|
)
|
|
|
|
|
|
|
|
type WindowsService struct {
|
|
|
|
Service Service
|
|
|
|
isInteractive bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Status) String() string {
|
|
|
|
switch s {
|
|
|
|
case StatusRunning:
|
|
|
|
return "running"
|
|
|
|
case StatusStopped:
|
|
|
|
return "stopped"
|
|
|
|
case StatusStartPending:
|
|
|
|
return "start pending"
|
|
|
|
case StatusPausePending:
|
|
|
|
return "pause pending"
|
|
|
|
case StatusPaused:
|
|
|
|
return "paused"
|
|
|
|
case StatusContinuePending:
|
|
|
|
return "continue pending"
|
|
|
|
case StatusStopPending:
|
|
|
|
return "stop pending"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WindowsService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
|
2020-06-22 17:11:53 +00:00
|
|
|
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptParamChange | acceptRotateLog
|
2019-09-16 06:52:58 +00:00
|
|
|
changes <- svc.Status{State: svc.StartPending}
|
|
|
|
if err := s.Service.Start(); err != nil {
|
|
|
|
return true, 1
|
|
|
|
}
|
|
|
|
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
c := <-r
|
|
|
|
switch c.Cmd {
|
|
|
|
case svc.Interrogate:
|
|
|
|
logger.Debug(logSender, "", "Received service interrogate request, current status: %v", c.CurrentStatus)
|
|
|
|
changes <- c.CurrentStatus
|
|
|
|
case svc.Stop, svc.Shutdown:
|
|
|
|
logger.Debug(logSender, "", "Received service stop request")
|
|
|
|
changes <- svc.Status{State: svc.StopPending}
|
|
|
|
s.Service.Stop()
|
|
|
|
break loop
|
2020-02-02 21:20:39 +00:00
|
|
|
case svc.ParamChange:
|
|
|
|
logger.Debug(logSender, "", "Received reload request")
|
2020-04-30 12:23:55 +00:00
|
|
|
err := dataprovider.ReloadConfig()
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "error reloading dataprovider configuration: %v", err)
|
|
|
|
}
|
|
|
|
err = httpd.ReloadTLSCertificate()
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "error reloading TLS certificate: %v", err)
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ftpd.ReloadTLSCertificate()
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "error reloading FTPD TLS certificate: %v", err)
|
|
|
|
}
|
2020-06-22 17:11:53 +00:00
|
|
|
case rotateLogCmd:
|
|
|
|
logger.Debug(logSender, "", "Received log file rotation request")
|
|
|
|
err := logger.RotateLogFile()
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "error rotating log file: %v", err)
|
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
default:
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WindowsService) RunService() error {
|
2019-09-16 16:11:35 +00:00
|
|
|
exePath, err := s.getExePath()
|
2019-09-16 06:52:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
isIntSess, err := svc.IsAnInteractiveSession()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.isInteractive = isIntSess
|
2019-09-16 16:11:35 +00:00
|
|
|
dir := filepath.Dir(exePath)
|
2019-09-16 06:52:58 +00:00
|
|
|
if err = os.Chdir(dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if s.isInteractive {
|
|
|
|
return s.Start()
|
|
|
|
}
|
|
|
|
return svc.Run(serviceName, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WindowsService) Start() error {
|
|
|
|
m, err := mgr.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer m.Disconnect()
|
|
|
|
service, err := m.OpenService(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not access service: %v", err)
|
|
|
|
}
|
|
|
|
defer service.Close()
|
|
|
|
err = service.Start()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not start service: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-02 21:20:39 +00:00
|
|
|
func (s *WindowsService) Reload() error {
|
|
|
|
m, err := mgr.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer m.Disconnect()
|
|
|
|
service, err := m.OpenService(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not access service: %v", err)
|
|
|
|
}
|
|
|
|
defer service.Close()
|
|
|
|
_, err = service.Control(svc.ParamChange)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not send control=%d: %v", svc.ParamChange, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:11:53 +00:00
|
|
|
func (s *WindowsService) RotateLogFile() error {
|
|
|
|
m, err := mgr.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer m.Disconnect()
|
|
|
|
service, err := m.OpenService(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not access service: %v", err)
|
|
|
|
}
|
|
|
|
defer service.Close()
|
|
|
|
_, err = service.Control(rotateLogCmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not send control=%d: %v", rotateLogCmd, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-16 06:52:58 +00:00
|
|
|
func (s *WindowsService) Install(args ...string) error {
|
2019-09-16 16:11:35 +00:00
|
|
|
exePath, err := s.getExePath()
|
2019-09-16 06:52:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m, err := mgr.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer m.Disconnect()
|
|
|
|
service, err := m.OpenService(serviceName)
|
|
|
|
if err == nil {
|
|
|
|
service.Close()
|
|
|
|
return fmt.Errorf("service %s already exists", serviceName)
|
|
|
|
}
|
|
|
|
config := mgr.Config{
|
|
|
|
DisplayName: serviceName,
|
|
|
|
Description: serviceDesc,
|
|
|
|
StartType: mgr.StartAutomatic}
|
2019-09-16 16:11:35 +00:00
|
|
|
service, err = m.CreateService(serviceName, exePath, config, args...)
|
2019-09-16 06:52:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer service.Close()
|
|
|
|
err = eventlog.InstallAsEventCreate(serviceName, eventlog.Error|eventlog.Warning|eventlog.Info)
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "exists") {
|
|
|
|
service.Delete()
|
|
|
|
return fmt.Errorf("SetupEventLogSource() failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
recoveryActions := []mgr.RecoveryAction{
|
2019-09-16 07:22:27 +00:00
|
|
|
{
|
2019-09-16 06:52:58 +00:00
|
|
|
Type: mgr.ServiceRestart,
|
|
|
|
Delay: 0,
|
|
|
|
},
|
2019-09-16 07:22:27 +00:00
|
|
|
{
|
2019-09-16 06:52:58 +00:00
|
|
|
Type: mgr.ServiceRestart,
|
|
|
|
Delay: 60 * time.Second,
|
|
|
|
},
|
2019-09-16 07:22:27 +00:00
|
|
|
{
|
2020-03-02 08:58:36 +00:00
|
|
|
Type: mgr.NoAction,
|
2019-09-16 06:52:58 +00:00
|
|
|
},
|
|
|
|
}
|
2020-03-02 08:58:36 +00:00
|
|
|
err = service.SetRecoveryActions(recoveryActions, uint32(3600))
|
2019-09-16 06:52:58 +00:00
|
|
|
if err != nil {
|
|
|
|
service.Delete()
|
|
|
|
return fmt.Errorf("unable to set recovery actions: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WindowsService) Uninstall() error {
|
|
|
|
m, err := mgr.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer m.Disconnect()
|
|
|
|
service, err := m.OpenService(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("service %s is not installed", serviceName)
|
|
|
|
}
|
|
|
|
defer service.Close()
|
|
|
|
err = service.Delete()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = eventlog.Remove(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("RemoveEventLogSource() failed: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WindowsService) Stop() error {
|
|
|
|
m, err := mgr.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer m.Disconnect()
|
|
|
|
service, err := m.OpenService(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not access service: %v", err)
|
|
|
|
}
|
|
|
|
defer service.Close()
|
|
|
|
status, err := service.Control(svc.Stop)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not send control=%d: %v", svc.Stop, err)
|
|
|
|
}
|
|
|
|
timeout := time.Now().Add(10 * time.Second)
|
|
|
|
for status.State != svc.Stopped {
|
|
|
|
if timeout.Before(time.Now()) {
|
|
|
|
return fmt.Errorf("timeout waiting for service to go to state=%d", svc.Stopped)
|
|
|
|
}
|
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
status, err = service.Query()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not retrieve service status: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WindowsService) Status() (Status, error) {
|
|
|
|
m, err := mgr.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return StatusUnknown, err
|
|
|
|
}
|
|
|
|
defer m.Disconnect()
|
|
|
|
service, err := m.OpenService(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return StatusUnknown, fmt.Errorf("could not access service: %v", err)
|
|
|
|
}
|
|
|
|
defer service.Close()
|
|
|
|
status, err := service.Query()
|
|
|
|
if err != nil {
|
|
|
|
return StatusUnknown, fmt.Errorf("could not query service status: %v", err)
|
|
|
|
}
|
|
|
|
switch status.State {
|
|
|
|
case svc.StartPending:
|
|
|
|
return StatusStartPending, nil
|
|
|
|
case svc.Running:
|
|
|
|
return StatusRunning, nil
|
|
|
|
case svc.PausePending:
|
|
|
|
return StatusPausePending, nil
|
|
|
|
case svc.Paused:
|
|
|
|
return StatusPaused, nil
|
|
|
|
case svc.ContinuePending:
|
|
|
|
return StatusContinuePending, nil
|
|
|
|
case svc.StopPending:
|
|
|
|
return StatusStopPending, nil
|
|
|
|
case svc.Stopped:
|
|
|
|
return StatusStopped, nil
|
|
|
|
default:
|
|
|
|
return StatusUnknown, fmt.Errorf("unknown status %v", status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:11:35 +00:00
|
|
|
func (s *WindowsService) getExePath() (string, error) {
|
2019-09-16 06:52:58 +00:00
|
|
|
return os.Executable()
|
|
|
|
}
|