From 1ea1259aadb4568a802557ad4e203e8dd169e68b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20B=C3=B6hmke?= Date: Tue, 10 Dec 2019 18:57:01 +0100 Subject: [PATCH] added SystemSSHEnabled --- system.go | 10 ++++++++++ system_test.go | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/system.go b/system.go index 4b52167..7634a7a 100644 --- a/system.go +++ b/system.go @@ -33,6 +33,16 @@ func SystemSetRootPassword(password string) error { return ioutil.WriteFile(systemShadow, []byte(line), os.ModePerm) } +// SystemSSHEnabled returns true if server is enabled +func SystemSSHEnabled() (bool, error) { + data, err := ioutil.ReadFile(systemDropbearConfig) + if err != nil { + return false, fmt.Errorf("failed to read ssh config: %w", err) + } + + return strings.Contains(string(data), "127.0.0.1:22"), nil +} + // SystemEnableSSH server func SystemEnableSSH() error { err := ioutil.WriteFile(systemDropbearConfig, []byte("DROPBEAR_OPTS=\"\""), os.ModePerm) diff --git a/system_test.go b/system_test.go index 79b4d64..bdf39c8 100644 --- a/system_test.go +++ b/system_test.go @@ -34,6 +34,25 @@ func TestSystemSetRootPassword(t *testing.T) { _ = os.Remove(systemShadow) } +func TestSystemSSHEnabled(t *testing.T) { + ass := assert.New(t) + + _, err := SystemSSHEnabled() + ass.EqualError(err, "failed to read ssh config: open test_dropbear: no such file or directory") + + ass.NoError(ioutil.WriteFile(systemDropbearConfig, []byte("test"), os.ModePerm)) + value, err := SystemSSHEnabled() + ass.NoError(err) + ass.False(value) + + ass.NoError(ioutil.WriteFile(systemDropbearConfig, []byte("DROPBEAR_OPTS=\"-p 127.0.0.1:22\""), os.ModePerm)) + value, err = SystemSSHEnabled() + ass.NoError(err) + ass.True(value) + + _ = os.Remove(systemDropbearConfig) +} + func TestSystemEnableSSH(t *testing.T) { ass := assert.New(t)