Copy settings over to lightdm with --greeter flag.
This commit is contained in:
1 parent
ab14244404
commit
2c6ab2ce76
10 files changed
+666
-3
No files matched your search
@@ -0,0 +1,75 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"git.stormux.org/storm/cthulhu/internal/config"
|
||||
)
|
||||
|
||||
func greeter_account(name string) (*user.User, error) {
|
||||
if name != "lightdm" {
|
||||
return nil, fmt.Errorf("unsupported greeter %q; supported: lightdm", name)
|
||||
}
|
||||
account, err := user.Lookup(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("find %s greeter account: %w", name, err)
|
||||
}
|
||||
if account.Uid == "0" || !filepath.IsAbs(account.HomeDir) || account.HomeDir == "/" {
|
||||
return nil, fmt.Errorf("%s greeter account needs a non-root UID and an absolute home directory other than /", name)
|
||||
}
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func copy_greeter_settings(ctx context.Context, settings config.Settings, account *user.User, stdout, stderr io.Writer) error {
|
||||
if account.Uid != strconv.Itoa(os.Geteuid()) {
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
return fmt.Errorf("find Cthulhu executable: %w", err)
|
||||
}
|
||||
command, err := greeter_copy_command(ctx, executable, settings, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// sudo reads authentication from the terminal, leaving stdin available
|
||||
// for the validated settings. Only the greeter account writes its files.
|
||||
fmt.Fprintf(stdout, "Copying settings for %s; sudo authentication may be required.\n", account.Username)
|
||||
command.Stdout, command.Stderr = stdout, stderr
|
||||
if err := command.Run(); err != nil {
|
||||
return fmt.Errorf("copy greeter settings as %s: %w", account.Username, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Deliberately ignore the invoking user's XDG_DATA_HOME. This is the
|
||||
// destination account's default, matching an ordinary Cthulhu startup.
|
||||
path := filepath.Join(account.HomeDir, ".local", "share", "cthulhu", "user-settings-go.toml")
|
||||
if err := config.WriteGreeterSettings(path, settings); err != nil {
|
||||
return fmt.Errorf("copy greeter settings: %w", err)
|
||||
}
|
||||
fmt.Fprintf(stdout, "Copied greeter settings to %s. They take effect when the greeter reader next starts.\n", path)
|
||||
return nil
|
||||
}
|
||||
|
||||
func greeter_copy_command(ctx context.Context, executable string, settings config.Settings, account *user.User) (*exec.Cmd, error) {
|
||||
contents, err := config.GreeterSettings(settings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Re-enter only the copy path, never the screen reader. Sending the
|
||||
// snapshot over stdin avoids exposing the source file to another user.
|
||||
command := exec.CommandContext(ctx, "sudo", "--user", account.Username, "--",
|
||||
executable, "--greeter", "lightdm", "--greeter-settings-stdin")
|
||||
command.Stdin = bytes.NewReader(contents)
|
||||
return command, nil
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.stormux.org/storm/cthulhu/internal/config"
|
||||
)
|
||||
|
||||
func TestGreeterCopyUsesDestinationHomeAndPreservesSource(t *testing.T) {
|
||||
t.Setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/nonexistent/session-bus")
|
||||
directory := t.TempDir()
|
||||
source := filepath.Join(directory, "source.toml")
|
||||
contents := []byte("[general]\nenableEchoByWord = true\n")
|
||||
if err := os.WriteFile(source, contents, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
settings, err := config.Load(source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
account := &user.User{Username: "Username", Uid: strconv.Itoa(os.Geteuid()), HomeDir: filepath.Join(directory, "greeter-home")}
|
||||
t.Setenv("XDG_DATA_HOME", filepath.Join(directory, "source-data"))
|
||||
var output bytes.Buffer
|
||||
if err := copy_greeter_settings(context.Background(), settings, account, &output, &output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target := filepath.Join(account.HomeDir, ".local", "share", "cthulhu", "user-settings-go.toml")
|
||||
copied, err := config.Load(target)
|
||||
if err != nil || !copied.Loaded || copied.ActiveProfile != "greeter" || !copied.EnableEchoByWord {
|
||||
t.Fatalf("copied settings = %#v, error %v", copied, err)
|
||||
}
|
||||
info, err := os.Stat(target)
|
||||
if err != nil || info.Mode().Perm() != 0o600 {
|
||||
t.Fatalf("destination mode = %v, error %v", info, err)
|
||||
}
|
||||
unchanged, err := os.ReadFile(source)
|
||||
if err != nil || !bytes.Equal(contents, unchanged) {
|
||||
t.Fatalf("source changed: %q, error %v", unchanged, err)
|
||||
}
|
||||
if !strings.Contains(output.String(), target) {
|
||||
t.Fatalf("missing destination confirmation: %q", output.String())
|
||||
}
|
||||
// A second copy must replace settings without leaving a stale value.
|
||||
settings.EnableEchoByWord = false
|
||||
if err := copy_greeter_settings(context.Background(), settings, account, &output, &output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
copied, err = config.Load(target)
|
||||
if err != nil || copied.EnableEchoByWord {
|
||||
t.Fatalf("repeated copy retained old settings: %#v, %v", copied, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGreeterCopyRequestsOnlyDestinationAccountPrivileges(t *testing.T) {
|
||||
settings := config.DefaultSettings()
|
||||
settings.Path = "/private/source/settings.toml"
|
||||
settings.EnableEchoByWord = true
|
||||
account := &user.User{Username: "Username"}
|
||||
command, err := greeter_copy_command(context.Background(), "/usr/bin/cthulhu", settings, account)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{"sudo", "--user", "Username", "--", "/usr/bin/cthulhu", "--greeter", "lightdm", "--greeter-settings-stdin"}
|
||||
if !reflect.DeepEqual(command.Args, want) {
|
||||
t.Fatalf("copy arguments = %q, want %q", command.Args, want)
|
||||
}
|
||||
contents, err := io.ReadAll(command.Stdin)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snapshot := filepath.Join(t.TempDir(), "snapshot.toml")
|
||||
if err := os.WriteFile(snapshot, contents, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
copied, err := config.Load(snapshot)
|
||||
if err != nil || copied.ActiveProfile != "greeter" || !copied.EnableEchoByWord {
|
||||
t.Fatalf("sudo stdin snapshot = %#v, error %v", copied, err)
|
||||
}
|
||||
}
|
||||
+28
-2
@@ -26,6 +26,7 @@ import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"os/user"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -65,6 +66,8 @@ func run(ctx context.Context, arguments []string, stdout, stderr io.Writer) (res
|
||||
replace := options.Bool("replace", false, "stop the running Cthulhu instance before starting")
|
||||
shortReplace := options.Bool("r", false, "stop the running Cthulhu instance before starting")
|
||||
checkConfig := options.Bool("check-config", false, "validate configuration and exit")
|
||||
greeterStdin := options.Bool("greeter-settings-stdin", false, "receive an internal greeter settings snapshot")
|
||||
greeter := options.String("greeter", "", "copy the saved active profile to a greeter account and exit (supported: lightdm)")
|
||||
shortSettings := options.Bool("s", false, "open the preferences window at startup")
|
||||
settingsFlag := options.Bool("settings", false, "open the preferences window at startup")
|
||||
preferencesUI := options.Bool("preferences-ui", false, "run the internal preferences helper (private pipe protocol)")
|
||||
@@ -102,7 +105,7 @@ func run(ctx context.Context, arguments []string, stdout, stderr io.Writer) (res
|
||||
options.VisitAll(func(option *flag.Flag) {
|
||||
switch option.Name {
|
||||
case "preferences-ui", "notification-list-ui", "speech-history-ui", "shortcut-list-ui",
|
||||
"clipboard-read", "clipboard-watch", "ocr-capture", "ocr-confirm", "tentacle-guardian":
|
||||
"clipboard-read", "clipboard-watch", "ocr-capture", "ocr-confirm", "tentacle-guardian", "greeter-settings-stdin":
|
||||
return
|
||||
}
|
||||
visibleOptions.Var(option.Value, option.Name, option.Usage)
|
||||
@@ -155,6 +158,20 @@ func run(ctx context.Context, arguments []string, stdout, stderr io.Writer) (res
|
||||
if *ocrCapture {
|
||||
return ocrcapture.Run(ctx, os.Stdin, stdout)
|
||||
}
|
||||
var greeterAccount *user.User
|
||||
if *greeterStdin && (*greeter == "" || *configPath != "") {
|
||||
return errors.New("--greeter-settings-stdin requires --greeter and cannot be combined with --config")
|
||||
}
|
||||
if *greeter != "" {
|
||||
var err error
|
||||
greeterAccount, err = greeter_account(*greeter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if os.Geteuid() == 0 && *configPath == "" {
|
||||
return errors.New("run --greeter as your normal user; it requests sudo authentication when needed (root must supply --config FILE)")
|
||||
}
|
||||
}
|
||||
logger := slog.New(slog.DiscardHandler)
|
||||
if *debugFile != "" {
|
||||
fileWriter, err := logging.OpenFile(*debugFile, nil)
|
||||
@@ -177,7 +194,13 @@ func run(ctx context.Context, arguments []string, stdout, stderr io.Writer) (res
|
||||
"build", version.RuntimeIdentity(), "atspi_version", atspi.Version(),
|
||||
"session_type", os.Getenv("XDG_SESSION_TYPE"), "session_desktop", os.Getenv("XDG_SESSION_DESKTOP"),
|
||||
"x11_display_available", os.Getenv("DISPLAY") != "", "wayland_display_available", os.Getenv("WAYLAND_DISPLAY") != "")
|
||||
settings, err := config.Load(*configPath)
|
||||
var settings config.Settings
|
||||
var err error
|
||||
if *greeterStdin {
|
||||
settings, err = config.ReadGreeterSettings(os.Stdin)
|
||||
} else {
|
||||
settings, err = config.Load(*configPath)
|
||||
}
|
||||
if err != nil {
|
||||
logger.Error("load configuration", "error", err)
|
||||
return err
|
||||
@@ -192,6 +215,9 @@ func run(ctx context.Context, arguments []string, stdout, stderr io.Writer) (res
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if greeterAccount != nil {
|
||||
return copy_greeter_settings(ctx, settings, greeterAccount, stdout, stderr)
|
||||
}
|
||||
if *replace || *shortReplace {
|
||||
if err := replaceRunning(ctx); err != nil {
|
||||
return fmt.Errorf("replace running Cthulhu: %w", err)
|
||||
|
||||
@@ -90,6 +90,18 @@ func TestCheckConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnsupportedGreeterDoesNotStartOrReplaceReader(t *testing.T) {
|
||||
t.Setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/nonexistent/session-bus")
|
||||
var output bytes.Buffer
|
||||
err := run(context.Background(), []string{"--greeter", "unsupported", "--replace"}, &output, &output)
|
||||
if err == nil || !strings.Contains(err.Error(), "unsupported greeter") {
|
||||
t.Fatalf("unsupported greeter error = %v", err)
|
||||
}
|
||||
if output.Len() != 0 {
|
||||
t.Fatalf("unexpected output: %q", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckVersionedConfig(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
|
||||
Reference in new issue
Block a user