From 7081bfa8d7fb027bd78bf6fdf10b4b8b5d6bb501 Mon Sep 17 00:00:00 2001 From: HikariKnight <2557889+HikariKnight@users.noreply.github.com> Date: Tue, 11 Apr 2023 01:03:59 +0200 Subject: [PATCH] Implement Set_Kernelstub and attempt to elevate to sudo using STDIN and then attempt to remove the password from the program memory --- internal/configs/config_bootloaders.go | 11 ++++++++ internal/ui_main_events.go | 29 +++++++++++++------- internal/ui_main_functions.go | 37 +++++++++++++++++++++++++- internal/ui_main_view.go | 2 +- internal/ui_model.go | 15 +++++++++-- 5 files changed, 81 insertions(+), 13 deletions(-) diff --git a/internal/configs/config_bootloaders.go b/internal/configs/config_bootloaders.go index fa92e74..7d20d41 100644 --- a/internal/configs/config_bootloaders.go +++ b/internal/configs/config_bootloaders.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/HikariKnight/quickpassthrough/internal/logger" "github.com/HikariKnight/quickpassthrough/pkg/command" "github.com/HikariKnight/quickpassthrough/pkg/fileio" "github.com/klauspost/cpuid/v2" @@ -63,5 +64,15 @@ func Set_Cmdline(gpu_IDs []string) { // TODO2: look into grubby // TODO3: if unknown bootloader, tell user what to add a kernel arguments func Set_KernelStub() { + // Get the config + config := GetConfig() + // Get the kernel args + kernel_args := fileio.ReadFile(config.Path.CMDLINE) + + // Write to logger + logger.Printf("Running command:\nsudo kernelstub -a \"%s\"", kernel_args) + + // Run the command + command.Run("sudo", "kernelstub", "-a", kernel_args) } diff --git a/internal/ui_main_events.go b/internal/ui_main_events.go index 83a3758..4314d70 100644 --- a/internal/ui_main_events.go +++ b/internal/ui_main_events.go @@ -1,6 +1,10 @@ package internal -import tea "github.com/charmbracelet/bubbletea" +import ( + "encoding/base64" + + tea "github.com/charmbracelet/bubbletea" +) func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd @@ -8,7 +12,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: // If we are not done - if m.focused != DONE { + if m.focused != INSTALL { // Setup keybindings switch msg.String() { case "ctrl+c", "q": @@ -19,7 +23,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.width != 0 { // Process the selected item, if the return value is true then exit the application if m.processSelection() { - return m, tea.Quit + return m, tea.ExitAltScreen } } case "ctrl+z", "backspace": @@ -45,13 +49,20 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Quit case "enter": - if m.width != 0 { - // Process the selected item, if the return value is true then exit the application - if m.processSelection() { - return m, tea.Quit - } - } + // Start installation and send the password to the command + m.install( + base64.StdEncoding.EncodeToString( + []byte( + m.authDialog.Value(), + ), + ), + ) + + // Blank the password field + m.authDialog.SetValue("") } + + // Issue an UI update m.authDialog, cmd = m.authDialog.Update(msg) return m, cmd } diff --git a/internal/ui_main_functions.go b/internal/ui_main_functions.go index 60cc193..930ca83 100644 --- a/internal/ui_main_functions.go +++ b/internal/ui_main_functions.go @@ -1,10 +1,13 @@ package internal import ( + "fmt" "os" "regexp" "github.com/HikariKnight/quickpassthrough/internal/configs" + "github.com/HikariKnight/quickpassthrough/internal/logger" + "github.com/HikariKnight/quickpassthrough/pkg/command" "github.com/HikariKnight/quickpassthrough/pkg/fileio" ) @@ -115,7 +118,8 @@ func (m *model) processSelection() bool { //m.focused++ // Because we have no QuickEmu support yet, just skip USB Controller configuration - m.focused = DONE + m.focused = INSTALL + return true case INTRO: // This is an OK Dialog @@ -133,3 +137,34 @@ func (m *model) processSelection() bool { // Return false as we are not done return false } + +// This function starts the install process +// It takes 1 auth string as variable +func (m *model) install(auth string) { + // Get the config + config := configs.GetConfig() + + // Write to logger + logger.Printf("Getting authentication token by elevating with sudo once") + + // Elevate to sudo + command.Elevate(auth) + + // Write to logger + logger.Printf("Attempting to free hash from memory") + + // Blank out the variable + auth = "" + + // Based on the bootloader, setup the configuration + if config.Bootloader == "kernelstub" { + // Write to logger + logger.Printf("Configuring systemd-boot using kernelstub") + + // Configure kernelstub + configs.Set_KernelStub() + } else if config.Bootloader == "unknown" { + kernel_args := fileio.ReadFile(config.Path.CMDLINE) + fmt.Printf("Unsupported bootloader, please add the below line to your bootloaders kernel arguments\n%s", kernel_args) + } +} diff --git a/internal/ui_main_view.go b/internal/ui_main_view.go index d0b448f..dc15225 100644 --- a/internal/ui_main_view.go +++ b/internal/ui_main_view.go @@ -115,7 +115,7 @@ func (m model) View() string { view = listStyle.Render(m.lists[m.focused].View()) - case DONE: + case INSTALL: title = dialogStyle.Render( fmt.Sprint( "The configuration files have been generated and are\n", diff --git a/internal/ui_model.go b/internal/ui_model.go index 721ec49..da1b3ec 100644 --- a/internal/ui_model.go +++ b/internal/ui_model.go @@ -46,6 +46,8 @@ const ( VIDEO USB USB_GROUP + INSTALL + UNKNOWN_BOOTLOADER DONE ) @@ -95,14 +97,16 @@ func (m *model) initLists(width, height int) { defaultList, defaultList, choiceList, + choiceList, + choiceList, } // Configure offsets for sizing m.offsetx = []int{ - 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } m.offsety = []int{ - 18, 2, 3, 13, 5, 2, 3, 12, + 18, 2, 3, 13, 5, 2, 3, 12, 0, 0, } // Update the styles with the correct width @@ -169,4 +173,11 @@ func (m *model) initLists(width, height int) { } m.lists[DONE].SetItems(items) m.lists[DONE].SetSize(m.width-m.offsetx[DONE], m.height-m.offsety[DONE]) + + // Init DONE choises + items = []list.Item{ + item{title: "FINISH"}, + } + m.lists[UNKNOWN_BOOTLOADER].SetItems(items) + m.lists[UNKNOWN_BOOTLOADER].SetSize(m.width-m.offsetx[UNKNOWN_BOOTLOADER], m.height-m.offsety[UNKNOWN_BOOTLOADER]) }