Skip to content

Commit

Permalink
Add SSH authentication key API to enable OS shell access (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
agners authored Mar 16, 2023
1 parent 0bdb8b3 commit a703490
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
labelOverlayFileSystem = "hassos-overlay"
kernelCommandLine = "/mnt/boot/cmdline.txt"
tmpKernelCommandLine = "/mnt/boot/.tmp.cmdline.txt"
sshAuthKeyFileName = "/root/.ssh/authorized_keys"
)

type system struct {
Expand Down Expand Up @@ -102,6 +103,35 @@ func (d system) ScheduleWipeDevice() (bool, *dbus.Error) {
return true, nil
}

func (d system) AddSSHAuthKey(newKey string) *dbus.Error {

file, err := os.OpenFile(sshAuthKeyFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
logging.Error.Printf("Failed to open SSH authentication file %s: %s", sshAuthKeyFileName, err)
return dbus.MakeFailedError(err)
}

defer file.Close()

if _, err := file.WriteString(newKey + "\n"); err != nil {
logging.Error.Printf("Failed to write SSH authentication file: %s.", err)
return dbus.MakeFailedError(err)
}

logging.Info.Printf("New SSH authentication key added for user root.")

return nil
}

func (d system) ClearSSHAuthKeys() *dbus.Error {
if err := os.Remove(sshAuthKeyFileName); err != nil && os.IsNotExist(err) {
logging.Error.Printf("Failed to delete SSH authentication file %s: %s", sshAuthKeyFileName, err)
return dbus.MakeFailedError(err)
}

return nil
}

func InitializeDBus(conn *dbus.Conn) {
d := system{
conn: conn,
Expand Down

0 comments on commit a703490

Please sign in to comment.