correct authentication

This commit is contained in:
aomizu
2025-05-29 18:18:25 +09:00
parent 085a975fb9
commit 3fe7cfd090
2 changed files with 38 additions and 2 deletions

View File

@@ -3,4 +3,4 @@
Name = "TurtleSilicon"
ID = "com.tairasu.turtlesilicon"
Version = "1.1.0"
Build = 13
Build = 17

View File

@@ -1,10 +1,12 @@
package service
import (
"bytes"
"fmt"
"log"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
@@ -121,7 +123,41 @@ func StartRosettaX87Service(myWindow fyne.Window, updateAllStatuses func()) {
// startServiceWithPassword starts the service using sudo with the provided password
func startServiceWithPassword(workingDir, executable, password string) error {
// Use sudo with the password
// First test if the password is correct with a simple sudo command
testCmd := exec.Command("sudo", "-S", "-v")
testStdin, err := testCmd.StdinPipe()
if err != nil {
return fmt.Errorf("failed to create test stdin pipe: %v", err)
}
// Capture stderr to check for authentication errors
var stderr bytes.Buffer
testCmd.Stderr = &stderr
err = testCmd.Start()
if err != nil {
return fmt.Errorf("failed to start test command: %v", err)
}
// Send the password for testing
_, err = testStdin.Write([]byte(password + "\n"))
if err != nil {
testCmd.Process.Kill()
return fmt.Errorf("failed to send test password: %v", err)
}
testStdin.Close()
// Wait for the test command to complete
err = testCmd.Wait()
if err != nil {
stderrOutput := stderr.String()
if strings.Contains(stderrOutput, "Sorry, try again") || strings.Contains(stderrOutput, "incorrect password") {
return fmt.Errorf("incorrect password")
}
return fmt.Errorf("sudo authentication failed: %v", err)
}
// If we get here, the password is correct, now start the actual service
cmd := exec.Command("sudo", "-S", executable)
cmd.Dir = workingDir