Skip to content

Commit 3de3177

Browse files
make schtasks package windows only
1 parent f422385 commit 3de3177

19 files changed

+107
-83
lines changed

codecov.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ignore:
1111
- run_profile.go
1212
- syslog.go
1313
- syslog_windows.go
14+
- schtasks/permission.go
1415
- "**/mocks/*.go"
1516

1617
codecov:

schedule/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func AddHandlerProvider(provider HandlerProvider) {
5353
func lookupBinary(name, binary string) error {
5454
found, err := exec.LookPath(binary)
5555
if err != nil || found == "" {
56-
return fmt.Errorf("it doesn't look like %s is installed on your system (cannot find %q in path)", name, binary)
56+
return fmt.Errorf("cannot find %q: please ensure %s is installed and available on this system", binary, name)
5757
}
5858
return nil
5959
}

schedule/handler_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ type HandlerWindows struct {
1616
config SchedulerConfig
1717
}
1818

19-
// Init a connection to the task scheduler
19+
// Init only checks the schtask.exe tool is available
2020
func (h *HandlerWindows) Init() error {
21-
return schtasks.Init()
21+
return lookupBinary("schtasks", "schtasks.exe")
2222
}
2323

2424
// Close does nothing with this implementation

schtasks/actions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build windows
2+
13
package schtasks
24

35
type Actions struct {

schtasks/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build windows
2+
13
package schtasks
24

35
type Config struct {

schtasks/constants.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//go:build windows
2+
3+
package schtasks
4+
5+
const (
6+
dateFormat = "2006-01-02T15:04:05-07:00"
7+
maxTriggers = 60
8+
author = "Author"
9+
taskSchema = "http://schemas.microsoft.com/windows/2004/02/mit/task"
10+
taskSchemaVersion = "1.4"
11+
defaultPriority = 8
12+
binaryPath = "schtasks.exe"
13+
tasksPathPrefix = `\resticprofile backup\`
14+
// From: https://learn.microsoft.com/en-us/windows/win32/secauthz/security-descriptor-string-format
15+
// O:owner_sid
16+
// G:group_sid
17+
// D:dacl_flags(string_ace1)(string_ace2)... (string_acen) <---
18+
// S:sacl_flags(string_ace1)(string_ace2)... (string_acen)
19+
// With flag:
20+
// "AI" SDDL_AUTO_INHERITED
21+
// From: https://learn.microsoft.com/en-us/windows/win32/secauthz/ace-strings
22+
// - first field:
23+
// "A" SDDL_ACCESS_ALLOWED
24+
// - third field
25+
// "FA" SDDL_FILE_ALL FILE_GENERIC_ALL
26+
// "FR" SDDL_FILE_READ FILE_GENERIC_READ
27+
// "FW" SDDL_FILE_WRITE FILE_GENERIC_WRITE
28+
// "FX" SDDL_FILE_EXECUTE FILE_GENERIC_EXECUTE
29+
// From: https://learn.microsoft.com/en-us/windows/win32/secauthz/sid-strings
30+
// "AU" SDDL_AUTHENTICATED_USERS
31+
// "BA" SDDL_BUILTIN_ADMINISTRATORS
32+
// "LS" SDDL_LOCAL_SERVICE
33+
// "SY" SDDL_LOCAL_SYSTEM
34+
securityDescriptor = "D:AI(A;;FA;;;BA)(A;;FA;;;SY)(A;;FRFX;;;LS)(A;;FR;;;AU)"
35+
)

schtasks/errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build windows
2+
13
package schtasks
24

35
import "errors"

schtasks/permission.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build windows
2+
13
package schtasks
24

35
import (

schtasks/principal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build windows
2+
13
package schtasks
24

35
const serviceAccount = "S-1-5-18"

schtasks/schtasks.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build windows
2+
13
package schtasks
24

35
import (
@@ -11,10 +13,6 @@ import (
1113
"strings"
1214
)
1315

14-
const (
15-
binaryPath = "schtasks.exe"
16-
)
17-
1816
func getRegisteredTasks() ([]string, error) {
1917
raw, err := listRegisteredTasks()
2018
if err != nil {
@@ -26,7 +24,7 @@ func getRegisteredTasks() ([]string, error) {
2624
}
2725
list := make([]string, 0)
2826
for _, taskLine := range all {
29-
if strings.HasPrefix(taskLine[0], tasksPath) {
27+
if len(taskLine) > 0 && strings.HasPrefix(taskLine[0], tasksPathPrefix) {
3028
list = append(list, taskLine[0])
3129
}
3230
}
@@ -72,9 +70,14 @@ func createTask(taskName, filename, username, password string) (string, error) {
7270
return "", ErrEmptyTaskName
7371
}
7472
params := []string{"/create", "/tn", taskName, "/xml", filename}
73+
74+
if len(password) > 0 && len(username) == 0 {
75+
return "", errors.New("username is required when specifying a password")
76+
}
7577
if len(password) > 0 {
7678
params = append(params, "/ru", username, "/rp", password)
7779
}
80+
7881
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
7982
cmd := exec.Command(binaryPath, params...)
8083
cmd.Stdout = stdout

0 commit comments

Comments
 (0)