4
0
raspi-alpine-builder/system.go

152 lines
3.8 KiB
Go
Raw Normal View History

2019-11-29 22:12:38 +01:00
package alpine_builder
import (
2020-01-05 19:49:53 +01:00
"bufio"
2019-11-29 22:12:38 +01:00
"fmt"
"io/ioutil"
"os"
"os/exec"
2020-01-05 19:49:53 +01:00
"path"
"path/filepath"
"sort"
2019-11-29 22:12:38 +01:00
"strings"
"github.com/GehirnInc/crypt"
_ "github.com/GehirnInc/crypt/sha256_crypt"
)
// system configurations
var systemShadow = "/data/etc/shadow"
var systemDropbearConfig = "/data/etc/dropbear/dropbear.conf"
2019-12-15 17:38:55 +01:00
var systemDropbearRestart = "rc-service dropbear restart"
2019-11-29 22:12:38 +01:00
var systemShutdown = "poweroff"
var systemReboot = "reboot"
2020-01-05 19:49:53 +01:00
var systemZoneinfo = "/usr/share/zoneinfo/"
var systemLocaltimeFile = "/data/etc/localtime"
2019-11-29 22:12:38 +01:00
// SystemSetRootPassword update shadow file
func SystemSetRootPassword(password string) error {
crypter := crypt.SHA256.New()
// generate password line
hash, err := crypter.Generate([]byte(password), nil)
if err != nil {
return fmt.Errorf("failed to generate hash: %w", err)
}
2019-12-15 16:29:06 +01:00
line := fmt.Sprintf("root:%s:0:0:::::\n", hash)
2019-11-29 22:12:38 +01:00
// write shadow file
return ioutil.WriteFile(systemShadow, []byte(line), os.ModePerm)
}
2019-12-10 18:57:01 +01:00
// 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)
}
2019-12-15 16:24:44 +01:00
return !strings.Contains(string(data), "127.0.0.1:22"), nil
2019-12-10 18:57:01 +01:00
}
2019-11-29 22:12:38 +01:00
// SystemEnableSSH server
func SystemEnableSSH() error {
err := ioutil.WriteFile(systemDropbearConfig, []byte("DROPBEAR_OPTS=\"\""), os.ModePerm)
if err != nil {
return fmt.Errorf("failed to write ssh config: %w", err)
}
cmdSplit := strings.Split(systemDropbearRestart, " ")
cmd := exec.Command(cmdSplit[0], cmdSplit[1:]...)
err = cmd.Run()
if err != nil {
return fmt.Errorf("failed to restart ssh server: %w", err)
}
return nil
}
// SystemDisableSSH server
func SystemDisableSSH() error {
err := ioutil.WriteFile(systemDropbearConfig, []byte("DROPBEAR_OPTS=\"-p 127.0.0.1:22\""), os.ModePerm)
if err != nil {
return fmt.Errorf("failed to write ssh config: %w", err)
}
cmdSplit := strings.Split(systemDropbearRestart, " ")
cmd := exec.Command(cmdSplit[0], cmdSplit[1:]...)
err = cmd.Run()
if err != nil {
return fmt.Errorf("failed to restart ssh server: %w", err)
}
return nil
}
2019-12-01 21:20:32 +01:00
// SystemShutdown start shutdown of system
2019-11-29 22:12:38 +01:00
func SystemShutdown() error {
cmd := exec.Command(systemShutdown)
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to start system shutdown: %w", err)
}
return nil
}
2019-12-01 21:20:32 +01:00
// SystemReboot start reboot of system
2019-11-29 22:12:38 +01:00
func SystemReboot() error {
cmd := exec.Command(systemReboot)
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to start system reboot: %w", err)
}
return nil
}
2020-01-05 19:49:53 +01:00
// SystemListTimeZones available on system
func SystemListTimeZones() ([]string, error) {
// load zone info tab
file, err := os.Open(path.Join(systemZoneinfo, "zone1970.tab"))
if err != nil {
return nil, err
}
defer file.Close()
// parse file
zones := make([]string, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") {
continue
}
lineSplit := strings.Split(line, "\t")
if len(lineSplit) < 3 {
continue
}
zones = append(zones, strings.TrimSpace(lineSplit[2]))
}
zones = append(zones, "Etc/UTC")
sort.Strings(zones)
return zones, nil
}
// SystemSetTimeZone for operating system
func SystemSetTimeZone(name string) error {
if _, err := os.Stat(path.Join(systemZoneinfo, name)); err != nil {
return fmt.Errorf("invalid time zone given: %s", name)
}
_ = os.Remove(systemLocaltimeFile)
return os.Symlink(path.Join(systemZoneinfo, name), systemLocaltimeFile)
}
// SystemGetTimeZone currently set for operating system
func SystemGetTimeZone() (string, error) {
link, err := os.Readlink(systemLocaltimeFile)
if err != nil {
return "", fmt.Errorf("failed to get local time zone")
}
return filepath.Rel(systemZoneinfo, link)
}