diff --git a/docs/content/schedules/systemd.md b/docs/content/schedules/systemd.md index 3e26f2ed3..07fdcf018 100644 --- a/docs/content/schedules/systemd.md +++ b/docs/content/schedules/systemd.md @@ -260,11 +260,20 @@ ExecStart={{ .CommandLine }} {{ end -}} {{ if .IOSchedulingPriority }}IOSchedulingPriority={{ .IOSchedulingPriority }} {{ end -}} +{{ if .User }}User={{ .User }} +{{ end -}} {{ range .Environment -}} Environment="{{ . }}" {{ end -}} +{{ if .EnvironmentFile }}EnvironmentFile={{ .EnvironmentFile }} +{{ end -}} ``` +{{% notice style="warning" %}} +The environment captured with `capture-environment` can contain secrets, so it is not written in the unit file: any user can read `Environment=` values with `systemctl show`. It is saved in a file only readable by the owner of the unit, in the service drop-in folder. +If you use your own unit template, add the `EnvironmentFile=` line above, otherwise the service doesn't get the captured environment. +{{% /notice %}} + ### Default timer file ```ini @@ -293,4 +302,5 @@ These are available for both the unit and timer templates: * OnCalendar *array of strings* * SystemdProfile *string* * Nice *integer* -* Environment *array of strings* +* Environment *array of strings* (generated variables only, e.g. `HOME`) +* EnvironmentFile *string* (file holding the captured environment, empty when nothing is captured) diff --git a/schedule/handler_darwin.go b/schedule/handler_darwin.go index c832c3b6b..97be2aef1 100644 --- a/schedule/handler_darwin.go +++ b/schedule/handler_darwin.go @@ -5,7 +5,9 @@ package schedule import ( "bytes" "context" + "errors" "fmt" + "io/fs" "os" "os/exec" "path" @@ -343,7 +345,12 @@ func (h *HandlerLaunchd) createPlistFile(launchdJob *darwin.LaunchdJob, permissi _ = h.fs.Chown(dir, user.Uid, user.Gid) } } - file, err := h.fs.Create(filename) + // the plist can contain the captured environment (secrets): only its owner can read it. + // A new file gets the right permission, and O_EXCL doesn't follow a symlink + if err = h.fs.Remove(filename); err != nil && !errors.Is(err, fs.ErrNotExist) { + return "", err + } + file, err := h.fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) if err != nil { return "", err } diff --git a/schedule/handler_darwin_test.go b/schedule/handler_darwin_test.go index 15c49d5f1..059916f3c 100644 --- a/schedule/handler_darwin_test.go +++ b/schedule/handler_darwin_test.go @@ -96,6 +96,29 @@ func TestCreateSystemPlist(t *testing.T) { assert.NoError(t, err) } +func TestCreatePlistOnlyReadableByOwner(t *testing.T) { + handler := NewHandler(SchedulerLaunchd{}).(*HandlerLaunchd) + handler.fs = afero.NewMemMapFs() + + launchdJob := &darwin.LaunchdJob{ + Label: "TestCreatePlistOnlyReadableByOwner", + EnvironmentVariables: map[string]string{"RESTIC_PASSWORD": "secret"}, + } + for _, permission := range []Permission{PermissionSystem, PermissionUserBackground} { + filename, err := getFilename(launchdJob.Label, permission) + require.NoError(t, err) + // a file from a previous version is replaced + require.NoError(t, afero.WriteFile(handler.fs, filename, []byte("old"), 0o644)) + + _, err = handler.createPlistFile(launchdJob, permission) + require.NoError(t, err) + + info, err := handler.fs.Stat(filename) + require.NoError(t, err) + assert.Equal(t, os.FileMode(0o600), info.Mode().Perm()) + } +} + func TestReadingLaunchdScheduled(t *testing.T) { calendarEvent := calendar.NewEvent(func(e *calendar.Event) { _ = e.Second.AddValue(0) diff --git a/systemd/environment_test.go b/systemd/environment_test.go new file mode 100644 index 000000000..b81946112 --- /dev/null +++ b/systemd/environment_test.go @@ -0,0 +1,136 @@ +//go:build !darwin && !windows && !openbsd && !netbsd && !freebsd + +package systemd + +import ( + "errors" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + testServiceFile = "resticprofile-backup@profile-name.service" + testEnvironmentFile = "resticprofile-backup@profile-name.service.d/resticprofile.env" +) + +func testEnvironmentConfig(environment ...string) Config { + return Config{ + CommandLine: "commandLine", + WorkingDirectory: "workdir", + Title: "name", + SubTitle: "backup", + Schedules: []string{"daily"}, + UnitType: SystemUnit, + Environment: environment, + } +} + +func TestGenerateEnvironmentFile(t *testing.T) { + t.Setenv("SUDO_USER", "") + + fs := afero.NewMemMapFs() + unit := Unit{fs: fs, user: testSudoUser} + serviceFile := filepath.Join(GetSystemDir(), testServiceFile) + environmentFile := filepath.Join(GetSystemDir(), testEnvironmentFile) + + // a file from a previous version is replaced + require.NoError(t, afero.WriteFile(fs, environmentFile, []byte("old"), 0o644)) + + config := testEnvironmentConfig( + "RESTICPROFILE_SCHEDULE_ID=profiles.yaml:backup@name", + "RESTIC_PASSWORD=pa$$ \"w`o\\rd'%h\nnext line", + "HOME=/captured", + ) + require.NoError(t, unit.Generate(config)) + + service, err := afero.ReadFile(fs, serviceFile) + require.NoError(t, err) + assert.Contains(t, string(service), "Environment=\"HOME=/root\"\nEnvironmentFile="+environmentFile+"\n") + assert.NotContains(t, string(service), "RESTIC") + + info, err := fs.Stat(environmentFile) + require.NoError(t, err) + assert.Equal(t, os.FileMode(0o600), info.Mode().Perm()) + + content, err := afero.ReadFile(fs, environmentFile) + require.NoError(t, err) + assert.Equal(t, "RESTICPROFILE_SCHEDULE_ID=\"profiles.yaml:backup@name\"\n"+ + "RESTIC_PASSWORD=\"pa\\$\\$ \\\"w\\`o\\\\rd'%h\nnext line\"\n", string(content)) + + // the generated HOME keeps precedence over the captured one + read, err := unit.Read(testServiceFile, SystemUnit) + require.NoError(t, err) + assert.Equal(t, []string{"HOME=/root", config.Environment[0], config.Environment[1]}, read.Environment) + + // nothing captured: no file + require.NoError(t, unit.Generate(testEnvironmentConfig())) + service, err = afero.ReadFile(fs, serviceFile) + require.NoError(t, err) + assert.NotContains(t, string(service), "EnvironmentFile") + _, err = fs.Stat(environmentFile) + assert.ErrorIs(t, err, os.ErrNotExist) +} + +func TestEnvironmentFileDoesNotFollowSymlink(t *testing.T) { + root := t.TempDir() + target := filepath.Join(root, "target") + require.NoError(t, os.WriteFile(target, []byte("unchanged"), 0o644)) + + link := filepath.Join(root, GetSystemDir(), testEnvironmentFile) + require.NoError(t, os.MkdirAll(filepath.Dir(link), 0o755)) + require.NoError(t, os.Symlink(target, link)) + + unit := Unit{fs: afero.NewBasePathFs(afero.NewOsFs(), root), user: testSudoUser} + require.NoError(t, unit.Generate(testEnvironmentConfig("RESTIC_PASSWORD=secret"))) + + content, err := os.ReadFile(target) + require.NoError(t, err) + assert.Equal(t, "unchanged", string(content)) + + info, err := os.Lstat(link) + require.NoError(t, err) + assert.True(t, info.Mode().IsRegular()) + assert.Equal(t, os.FileMode(0o600), info.Mode().Perm()) +} + +type failingRenameFs struct{ afero.Fs } + +func (failingRenameFs) Rename(string, string) error { return errors.New("rename failed") } + +func TestEnvironmentFileKeptOnFailure(t *testing.T) { + t.Parallel() + + fs := afero.NewMemMapFs() + environmentFile := filepath.Join(GetSystemDir(), testEnvironmentFile) + require.NoError(t, afero.WriteFile(fs, environmentFile, []byte("old"), 0o600)) + + unit := Unit{fs: failingRenameFs{fs}, user: testSudoUser} + require.Error(t, unit.writeEnvironmentFile(environmentFile, []string{"RESTIC_PASSWORD=secret"})) + + content, err := afero.ReadFile(fs, environmentFile) + require.NoError(t, err) + assert.Equal(t, "old", string(content)) + + // no temporary file left behind + entries, err := afero.ReadDir(fs, filepath.Dir(environmentFile)) + require.NoError(t, err) + assert.Len(t, entries, 1) +} + +func TestParseEnvironmentFile(t *testing.T) { + t.Parallel() + + environment := []string{"A=", "B=plain", "C=\\ \" ` $ ' %h \n end"} + content := "" + for _, env := range environment { + name, value, _ := strings.Cut(env, "=") + content += name + "=" + quoteEnvironmentValue(value) + "\n" + } + assert.Equal(t, environment, parseEnvironmentFile(content)) +} diff --git a/systemd/generate.go b/systemd/generate.go index 1277497b4..779ab31d8 100644 --- a/systemd/generate.go +++ b/systemd/generate.go @@ -4,6 +4,7 @@ package systemd import ( "bytes" + "errors" "fmt" "io" "io/fs" @@ -21,8 +22,9 @@ import ( ) const ( - defaultPermission = 0o644 - systemdSystemDir = "/etc/systemd/system/" + defaultPermission = 0o644 + environmentFileName = "resticprofile.env" + systemdSystemDir = "/etc/systemd/system/" systemdUnitDefaultTmpl = `[Unit] Description={{ .JobDescription }} @@ -45,6 +47,8 @@ ExecStart={{ .CommandLine }} {{ range .Environment -}} Environment="{{ . }}" {{ end -}} +{{ if .EnvironmentFile }}EnvironmentFile={{ .EnvironmentFile }} +{{ end -}} ` systemdTimerDefaultTmpl = `[Unit] @@ -83,6 +87,7 @@ type templateInfo struct { SystemdProfile string Nice int Environment []string + EnvironmentFile string AfterNetworkOnline bool CPUSchedulingPolicy string IOSchedulingClass int @@ -139,8 +144,7 @@ func (u Unit) Generate(config Config) error { } } - environment := slices.Clone(config.Environment) - + var environment []string if config.UnitType == SystemUnit && config.User == "" { // permission = system environment = append(environment, fmt.Sprintf("HOME=%s", u.user.SudoHomeDir)) @@ -152,6 +156,17 @@ func (u Unit) Generate(config Config) error { environment = append(environment, fmt.Sprintf("HOME=%s", u.user.UserHomeDir)) } + // Any user can read Environment= ("systemctl show"): the captured environment, which can contain secrets, + // goes to a file only readable by the owner of the unit. Generated variables keep precedence. + capturedEnvironment := collect.All(config.Environment, func(env string) bool { + return !slices.ContainsFunc(environment, func(generated string) bool { return envName(generated) == envName(env) }) + }) + environmentFilePath := filepath.Join(systemdUserDir, GetServiceFileDropInDir(config.Title, config.SubTitle), environmentFileName) + environmentFile := "" + if len(capturedEnvironment) > 0 { + environmentFile = environmentFilePath + } + policy := "" if config.Priority == constants.SchedulePriorityBackground { policy = "idle" @@ -168,6 +183,7 @@ func (u Unit) Generate(config Config) error { SystemdProfile: systemdProfile, Nice: config.Nice, Environment: environment, + EnvironmentFile: environmentFile, CPUSchedulingPolicy: policy, IOSchedulingClass: config.IOSchedulingClass, IOSchedulingPriority: config.IOSchedulingPriority, @@ -180,6 +196,9 @@ func (u Unit) Generate(config Config) error { if err != nil { return err } + if environmentFile != "" && !strings.Contains(systemdUnitTmpl, ".EnvironmentFile") { + clog.Warningf("unit template %q doesn't use {{ .EnvironmentFile }}: the service won't get the captured environment", config.UnitFile) + } unitTmpl, err := templates.New("systemd.unit").Parse(systemdUnitTmpl) if err != nil { return err @@ -221,6 +240,11 @@ func (u Unit) Generate(config Config) error { _ = u.fs.Chown(filePathName, u.user.Uid, u.user.Gid) } + // written before the drop-ins, which change the owner of the folder content for user units + if err = u.writeEnvironmentFile(environmentFilePath, capturedEnvironment); err != nil { + return err + } + existingFiles := collect.All(config.DropInFiles, u.DropInFileExists) dropIns := map[string][]string{ @@ -301,3 +325,52 @@ func (u Unit) loadTemplate(filename, defaultTmpl string) (string, error) { } return output.String(), nil } + +// writeEnvironmentFile saves the environment in a file only readable by its owner, or removes the file when there's no environment +func (u Unit) writeEnvironmentFile(filename string, environment []string) error { + if len(environment) == 0 { + if err := u.fs.Remove(filename); err != nil && !errors.Is(err, fs.ErrNotExist) { + return err + } + return nil + } + dir := filepath.Dir(filename) + if err := u.fs.MkdirAll(dir, 0o755); err != nil { + return err + } + content := strings.Builder{} + for _, env := range environment { + name, value, _ := strings.Cut(env, "=") + content.WriteString(name + "=" + quoteEnvironmentValue(value) + "\n") + } + clog.Debugf("writing %v", filename) + // the temporary file is created with 0600 permission, and the rename replaces a symlink instead of following it. + // the previous file stays in place until the new one is complete. + file, err := afero.TempFile(u.fs, dir, "."+filepath.Base(filename)+"-*") + if err != nil { + return err + } + _, err = file.WriteString(content.String()) + if closeErr := file.Close(); err == nil { + err = closeErr + } + if err == nil { + err = u.fs.Rename(file.Name(), filename) + } + if err != nil { + _ = u.fs.Remove(file.Name()) + } + return err +} + +// environmentValueEscaper escapes the only sequences systemd interprets in a double-quoted environment file value +var environmentValueEscaper = strings.NewReplacer(`\`, `\\`, `"`, `\"`, "`", "\\`", `$`, `\$`) + +func quoteEnvironmentValue(value string) string { + return `"` + environmentValueEscaper.Replace(value) + `"` +} + +func envName(env string) string { + name, _, _ := strings.Cut(env, "=") + return name +} diff --git a/systemd/read.go b/systemd/read.go index 5abb5d96d..5d48b44f5 100644 --- a/systemd/read.go +++ b/systemd/read.go @@ -6,9 +6,11 @@ import ( "bytes" "fmt" "path" + "slices" "strconv" "strings" + "github.com/creativeprojects/clog" "github.com/creativeprojects/resticprofile/constants" "github.com/spf13/afero" ) @@ -47,7 +49,7 @@ func (u Unit) Read(unit string, unitType UnitType) (*Config, error) { WorkingDirectory: getSingleValue(serviceSections, "Service", "WorkingDirectory"), CommandLine: getSingleValue(serviceSections, "Service", "ExecStart"), UnitType: unitType, - Environment: getValues(serviceSections, "Service", "Environment"), + Environment: slices.Concat(getValues(serviceSections, "Service", "Environment"), u.readEnvironmentFiles(serviceSections)), Nice: getIntegerValue(serviceSections, "Service", "Nice"), IOSchedulingClass: getIntegerValue(serviceSections, "Service", "IOSchedulingClass"), IOSchedulingPriority: getIntegerValue(serviceSections, "Service", "IOSchedulingPriority"), @@ -115,6 +117,43 @@ func getSingleValue(from map[string]map[string][]string, section, key string) st return "" } +// readEnvironmentFiles returns the environment from the files written by writeEnvironmentFile. +// A file that cannot be read (only readable by root) is skipped. +func (u Unit) readEnvironmentFiles(serviceSections map[string]map[string][]string) (environment []string) { + for _, filename := range getValues(serviceSections, "Service", "EnvironmentFile") { + content, err := afero.ReadFile(u.fs, strings.TrimPrefix(filename, "-")) + if err != nil { + clog.Debugf("cannot read environment file: %s", err) + continue + } + environment = append(environment, parseEnvironmentFile(string(content))...) + } + return +} + +// parseEnvironmentFile parses lines of NAME="value", where the value is escaped by quoteEnvironmentValue +func parseEnvironmentFile(content string) (environment []string) { + for { + name, quoted, found := strings.Cut(strings.TrimLeft(content, "\n"), `="`) + if !found { + return + } + value := strings.Builder{} + i := 0 + for ; i < len(quoted) && quoted[i] != '"'; i++ { + if quoted[i] == '\\' && i+1 < len(quoted) { + i++ + } + value.WriteByte(quoted[i]) + } + environment = append(environment, name+"="+value.String()) + if i >= len(quoted) { + return + } + content = quoted[i+1:] + } +} + func getValues(from map[string]map[string][]string, section, key string) []string { if section, found := from[section]; found { if values, found := section[key]; found { diff --git a/systemd/read_test.go b/systemd/read_test.go index a505b056b..50c7839ee 100644 --- a/systemd/read_test.go +++ b/systemd/read_test.go @@ -176,7 +176,7 @@ func TestReadSystemUnit(t *testing.T) { WorkingDirectory: tc.config.WorkingDirectory, CommandLine: tc.config.CommandLine, UnitType: tc.config.UnitType, - Environment: append(tc.config.Environment, "HOME="+homedir), + Environment: append([]string{"HOME=" + homedir}, tc.config.Environment...), Schedules: tc.config.Schedules, Priority: tc.config.Priority, User: tc.config.User,