From f99a5a5ad71d943a94273e3c3d80ab95695a05d8 Mon Sep 17 00:00:00 2001 From: HikariKnight <2557889+HikariKnight@users.noreply.github.com> Date: Mon, 10 Apr 2023 01:40:43 +0200 Subject: [PATCH] Write in the ability to elevate to sudo --- pkg/command/command.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pkg/command/command.go b/pkg/command/command.go index 26978c6..2070c1b 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -2,8 +2,12 @@ package command import ( "bytes" + "encoding/base64" "io" "os/exec" + "time" + + "github.com/HikariKnight/ls-iommu/pkg/errorcheck" ) func Run(binary string, args ...string) ([]string, error) { @@ -21,6 +25,7 @@ func Run(binary string, args ...string) ([]string, error) { output, _ := io.ReadAll(&stdout) outerr, _ := io.ReadAll(&stderr) + // Get the output outputs := []string{} outputs = append(outputs, string(output)) outputs = append(outputs, string(outerr)) @@ -28,3 +33,32 @@ func Run(binary string, args ...string) ([]string, error) { // Return our list of items return outputs, err } + +// This functions runs the command "sudo -Sk -- echo", this forces sudo +// to re-authenticate and lets us enter the password to STDIN +// giving us the ability to run sudo commands +func Elevate(password string) { + // Do a simple sudo command to just authenticate with sudo + cmd := exec.Command("sudo", "-Sk", "--", "echo") + + // Wait for 500ms, if the password is correct, sudo will return immediately + cmd.WaitDelay = 500 * time.Millisecond + + // Open STDIN + stdin, err := cmd.StdinPipe() + errorcheck.ErrorCheck(err, "Failed to get sudo STDIN") + + // Start the authentication + cmd.Start() + + // Get the passed password + pw, _ := base64.StdEncoding.DecodeString(password) + _, err = stdin.Write([]byte(string(pw) + "\n")) + errorcheck.ErrorCheck(err, "Failed at typing to STDIN") + // Clear the password + pw = nil + stdin.Close() + + err = cmd.Wait() + errorcheck.ErrorCheck(err) +}