Skip to content
This repository has been archived by the owner on Mar 20, 2022. It is now read-only.

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1te909 committed Dec 9, 2020
1 parent 51a91c3 commit 9e9a4bb
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 276 deletions.
147 changes: 8 additions & 139 deletions agent/agent_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"time"
"unsafe"

"github.com/capnspacehook/taskmaster"
ps "github.com/elastic/go-sysinfo"
"github.com/go-resty/resty/v2"
"github.com/gonutz/w32"
Expand Down Expand Up @@ -647,8 +646,8 @@ func (a *WindowsAgent) SendSoftware() {
}

func (a *WindowsAgent) UninstallCleanup() {
CleanupSchedTasks()
a.CleanupAgentUpdates()
CleanupSchedTasks()
}

// ShowStatus prints windows service status
Expand Down Expand Up @@ -741,90 +740,16 @@ func (a *WindowsAgent) AgentUpdate(url, inno, version string) {
a.Logger.Errorln(err)
return
}
innoLogFile := filepath.Join(dir, "tacticalrmm.txt")
code := fmt.Sprintf(`
@echo off
ping 127.0.0.1 -n 3
"%s" stop tacticalagent
"%s" stop checkrunner
"%s" stop tacticalrpc
ping 127.0.0.1 -n 2
taskkill /F /IM tacticalrmm.exe
ping 127.0.0.1 -n 2
"%s" /VERYSILENT /SUPPRESSMSGBOXES /LOG="%s"
ping 127.0.0.1 -n 45
"%s" start tacticalagent
ping 127.0.0.1 -n 5
"%s" start checkrunner
ping 127.0.0.1 -n 2
"%s" start tacticalrpc
`, a.Nssm, a.Nssm, a.Nssm, updater, innoLogFile, a.Nssm, a.Nssm, a.Nssm)

content := []byte(code)

tmpfn, _ := ioutil.TempFile(dir, "*.bat")
if _, err := tmpfn.Write(content); err != nil {
a.Logger.Errorln(err)
return
}
a.Logger.Debugln(tmpfn.Name())

conn, err := taskmaster.Connect()
if err != nil {
a.Logger.Errorln(err)
return
}
defer conn.Disconnect()

now := time.Now()
def := conn.NewTaskDefinition()
trigger := taskmaster.TimeTrigger{
TaskTrigger: taskmaster.TaskTrigger{
Enabled: true,
StartBoundary: time.Date(1975, 1, 1, 1, 0, 0, 0, now.Location()),
EndBoundary: now.Add(10 * time.Minute),
},
}
def.AddTrigger(trigger)
action := taskmaster.ExecAction{
Path: tmpfn.Name(),
}
def.AddAction(action)
def.Principal.RunLevel = taskmaster.TASK_RUNLEVEL_HIGHEST
def.Principal.LogonType = taskmaster.TASK_LOGON_SERVICE_ACCOUNT
def.Principal.UserID = "SYSTEM"
def.Settings.AllowDemandStart = true
def.Settings.AllowHardTerminate = true
def.Settings.DontStartOnBatteries = false
def.Settings.Enabled = true
def.Settings.MultipleInstances = taskmaster.TASK_INSTANCES_IGNORE_NEW
def.Settings.StopIfGoingOnBatteries = false
def.Settings.WakeToRun = true
def.Settings.DeleteExpiredTaskAfter = "PT15M"

ctask, _, err := conn.CreateTask("\\TacticalRMM_agentupdate", def, true)
if err != nil {
a.Logger.Errorln(err)
return
}
ctask.Release()
time.Sleep(1 * time.Second)
CMD("schtasks", []string{"/Change", "/TN", "TacticalRMM_fixmesh", "/DISABLE"}, 10, false)
CMD("schtasks", []string{"/Change", "/TN", "TacticalRMM_sync", "/DISABLE"}, 10, false)
time.Sleep(1 * time.Second)

task, err := conn.GetRegisteredTask("\\TacticalRMM_agentupdate")
if err != nil {
a.Logger.Errorln(err)
return
}
defer task.Release()
_, err = task.Run()
if err != nil {
a.Logger.Errorln(err)
innoLogFile := filepath.Join(dir, "tacticalrmm.txt")
args := []string{"/C", updater, "/VERYSILENT", "/SUPPRESSMSGBOXES", fmt.Sprintf("/LOG=%s", innoLogFile)}
a.Logger.Debugln(strings.Join(args, " "))
cmd := exec.Command("cmd.exe", args...)
cmd.SysProcAttr = &windows.SysProcAttr{
CreationFlags: windows.DETACHED_PROCESS | windows.CREATE_NEW_PROCESS_GROUP,
}
cmd.Start()
}

func (a *WindowsAgent) AgentUninstall() {
Expand Down Expand Up @@ -864,50 +789,6 @@ func (a *WindowsAgent) CleanupAgentUpdates() {
}
}

// CleanupPythonAgent cleans up files from the old python agent if this is an upgrade
func (a *WindowsAgent) CleanupPythonAgent() {
cderr := os.Chdir(a.ProgramDir)
if cderr != nil {
return
}

fileMatches := []string{
"*.dll",
"*.pyd",
"base_library*",
"*.manifest",
"onit.ico",
"VERSION",
}

for _, match := range fileMatches {
files, err := filepath.Glob(match)
if err == nil {
for _, f := range files {
os.Remove(f)
}
}
}

folderMatches := []string{
"certifi",
"Include",
"lib2to3",
"psutil",
"tcl",
"tk",
}

for _, match := range folderMatches {
folders, err := filepath.Glob(match)
if err == nil {
for _, f := range folders {
os.RemoveAll(f)
}
}
}
}

func (a *WindowsAgent) InstallSalt() {
rClient := resty.New()
rClient.SetCloseConnection(true)
Expand Down Expand Up @@ -1007,16 +888,4 @@ func (a *WindowsAgent) InstallSalt() {
}
}
a.Logger.Infoln("Salt was installed.")
DeleteSchedTask("TacticalRMM_installsalt")
}

// for older agents
func (a *WindowsAgent) CreateSyncTask() {
tasks := ListSchedTasks()
for _, task := range tasks {
if task == "TacticalRMM_sync" {
return
}
}
a.CreateInternalTask("TacticalRMM_sync", "-m sync", "15", 6)
}
25 changes: 5 additions & 20 deletions agent/install_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"
"time"

"github.com/capnspacehook/taskmaster"
"github.com/go-resty/resty/v2"
"github.com/gonutz/w32"
)
Expand Down Expand Up @@ -270,10 +269,6 @@ func (a *WindowsAgent) Install(i *Installer) {
a.Logger.Debugln("Creating mesh watchdog scheduled task")
a.CreateInternalTask("TacticalRMM_fixmesh", "-m fixmesh", "60", 10)

// sync stuff every 15 min
a.Logger.Debugln("Creating sync task")
a.CreateInternalTask("TacticalRMM_sync", "-m sync", "15", 6)

a.Logger.Infoln("Installing services...")

rpcCommands := [5][]string{
Expand Down Expand Up @@ -326,25 +321,15 @@ func (a *WindowsAgent) Install(i *Installer) {

if !i.NoSalt {
st := SchedTask{
Type: "installsalt",
Name: "TacticalRMM_installsalt",
Trigger: "manual",
Type: "installsalt",
Name: "TacticalRMM_installsalt",
Trigger: "oninstall",
DeleteAfter: true,
}
success, err := a.CreateSchedTask(st)
if success {
time.Sleep(1 * time.Second)
conn, err := taskmaster.Connect()
if err != nil {
a.Logger.Errorln(err)
}
defer conn.Disconnect()

task, err := conn.GetRegisteredTask("\\TacticalRMM_installsalt")
if err != nil {
a.Logger.Errorln(err)
}
defer task.Release()
task.Run()
CMD("schtasks", []string{"/run", "/TN", "TacticalRMM_installsalt"}, 10, false)
} else {
if err != nil {
a.Logger.Errorln(err)
Expand Down
10 changes: 9 additions & 1 deletion agent/rpc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@ func (a *WindowsAgent) RunRPC() {
ret.Encode("ok")
msg.Respond(resp)
}()

case "sync":
go func() {
a.Logger.Debugln("Sending sysinfo and software")
a.Sync()
}()
case "runchecks":
go func() {
a.Logger.Debugln("Running checks")
Expand All @@ -280,6 +284,10 @@ func (a *WindowsAgent) RunRPC() {

case "agentupdate":
go func(p *NatsMsg) {
var resp []byte
ret := codec.NewEncoderBytes(&resp, new(codec.MsgpackHandle))
ret.Encode("ok")
msg.Respond(resp)
a.AgentUpdate(p.Data["url"], p.Data["inno"], p.Data["version"])
}(payload)

Expand Down
26 changes: 15 additions & 11 deletions agent/tasks_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (a *WindowsAgent) CreateInternalTask(name, args, repeat string, start int)
def.AddTrigger(dailyTrigger)

action := taskmaster.ExecAction{
Path: a.EXE,
Path: "tacticalrmm.exe",
WorkingDir: a.ProgramDir,
Args: args,
}
Expand All @@ -106,11 +106,10 @@ func (a *WindowsAgent) CreateInternalTask(name, args, repeat string, start int)
def.Settings.StopIfGoingOnBatteries = false
def.Settings.WakeToRun = true

task, success, err := conn.CreateTask(fmt.Sprintf("\\%s", name), def, true)
_, success, err := conn.CreateTask(fmt.Sprintf("\\%s", name), def, true)
if err != nil {
return false, err
}
defer task.Release()

if success {
// https://github.com/capnspacehook/taskmaster/issues/15
Expand Down Expand Up @@ -191,6 +190,14 @@ func (a *WindowsAgent) CreateSchedTask(st SchedTask) (bool, error) {
StartBoundary: time.Date(1975, 1, 1, 1, 0, 0, 0, now.Location()),
},
}
case "oninstall":
trigger = taskmaster.TimeTrigger{
TaskTrigger: taskmaster.TaskTrigger{
Enabled: true,
StartBoundary: time.Date(1975, 1, 1, 1, 0, 0, 0, now.Location()),
EndBoundary: time.Now().Add(10 * time.Minute),
},
}
}

def.AddTrigger(trigger)
Expand Down Expand Up @@ -231,12 +238,11 @@ func (a *WindowsAgent) CreateSchedTask(st SchedTask) (bool, error) {
def.Settings.DeleteExpiredTaskAfter = "PT15M"
}

task, success, err := conn.CreateTask(fmt.Sprintf("\\%s", st.Name), def, true)
_, success, err := conn.CreateTask(fmt.Sprintf("\\%s", st.Name), def, true)
if err != nil {
a.Logger.Errorln(err)
return false, err
}
defer task.Release()

return success, nil
}
Expand Down Expand Up @@ -266,12 +272,10 @@ func EnableSchedTask(st SchedTask) error {
if err != nil {
return err
}
defer task.Release()

def := task.Definition
def.Settings.Enabled = st.Enabled
task.Definition.Settings.Enabled = st.Enabled

_, err = conn.UpdateTask(task.Path, def)
_, err = conn.UpdateTask(task.Path, task.Definition)
if err != nil {
return err
}
Expand All @@ -292,11 +296,11 @@ func CleanupSchedTasks() {
}

for _, task := range tasks {
defer task.Release()
if strings.HasPrefix(task.Name, "TacticalRMM_") {
conn.DeleteTask(fmt.Sprintf("\\%s", task.Name))
}
}
tasks.Release()
}

func ListSchedTasks() []string {
Expand All @@ -314,9 +318,9 @@ func ListSchedTasks() []string {
}

for _, task := range tasks {
defer task.Release()
ret = append(ret, task.Name)
}
tasks.Release()
return ret
}

Expand Down
Loading

0 comments on commit 9e9a4bb

Please sign in to comment.