Give the Sidecar Host API one authoritative catalog in pkg/pluginproto: every host/* method's status, required permissions, handshake feature and request and result shapes. The host, pkg/pluginsdk and plugins share those types, so a field can no longer exist on one side only, and permissions come from the catalog instead of from each handler. Tests gate that every served method is catalogued, every stable catalogued method is served, and every catalogued permission is one the host recognizes. Open the interface up instead of adding consent gates. Typing a secret setting is no longer reserved for reims.agent: any plugin that holds qmp:write and that the user authorized for the VM can have the host type the plugin's own setting, which the plugin never reads back. Restoring a snapshot works on any authorized VM, a plugin names its own clones, a held key may carry modifiers, and a screenshot width outside the supported range is brought into range rather than refused. Deleting a VM stays limited to the temporary VMs a plugin created. Record the keys and pointer buttons each plugin holds per VM and lift them when the plugin's process exits, its scope is disposed, the VM stops or it stops calling, so a guest never stays stuck with a modifier down. Requests from plugins that declare pluginApi 1.3 or newer are decoded strictly and answer an undefined field with JSON-RPC -32602, while older plugins keep tolerant decoding. Add typed VM and console methods to pkg/pluginsdk so a plugin no longer needs its own JSON-RPC client. A method the contract does not catalog, or one the host did not advertise in the handshake, is refused before it is sent, and the host's JSON-RPC error code reaches the caller.
206 lines
6.1 KiB
Go
206 lines
6.1 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os/user"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
"github.com/Nwflower/reims-go/pkg/pluginproto"
|
|
)
|
|
|
|
// elevatePromptTimeout is how long a confirmation waits for the person.
|
|
var elevatePromptTimeout = 2 * time.Minute
|
|
|
|
// ElevationPrompt is what the desktop window shows before a plugin's
|
|
// elevation request goes on to the Windows UAC prompt.
|
|
type ElevationPrompt struct {
|
|
ID string `json:"id"`
|
|
PluginID string `json:"plugin_id"`
|
|
PluginName string `json:"plugin_name"`
|
|
Publisher string `json:"publisher,omitempty"`
|
|
Trust string `json:"trust,omitempty"`
|
|
Action string `json:"action"`
|
|
NeedsReboot bool `json:"needs_reboot,omitempty"`
|
|
NeedsLogoff bool `json:"needs_logoff,omitempty"`
|
|
}
|
|
|
|
type elevationRunner func(ctx context.Context, action, targetUser string) (host.ElevationResult, error)
|
|
|
|
func (a *App) elevateHostHandlers(mgr *plugin.Manager, pluginID string) map[string]plugin.RPCRequestHandler {
|
|
if mgr == nil || mgr.Broker() == nil {
|
|
return nil
|
|
}
|
|
broker := mgr.Broker()
|
|
return map[string]plugin.RPCRequestHandler{
|
|
"host/elevate.request": a.hostMethod(mgr, pluginID, "host/elevate.request", func(ctx context.Context, raw json.RawMessage) (any, error) {
|
|
return a.requestElevation(ctx, mgr, broker, mgr.Registry(), pluginID, raw)
|
|
}),
|
|
}
|
|
}
|
|
|
|
// requestElevation runs one plugin elevation request: check the permission and
|
|
// the closed action list, ask the person in the desktop window, and only after
|
|
// a yes run the action (which raises the Windows UAC prompt). A no,
|
|
// a timeout or a missing window ends the request before anything runs.
|
|
func (a *App) requestElevation(ctx context.Context, kernel hostMethodHost, broker *plugin.Broker, reg *plugin.Registry, pluginID string, raw json.RawMessage) (any, error) {
|
|
req, err := decodeHostParams[pluginproto.ElevateParams](kernel, pluginID, "host/elevate.request", raw)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spec, ok := host.LookupElevationAction(req.Action)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unsupported elevation action %q", req.Action)
|
|
}
|
|
|
|
a.mu.Lock()
|
|
emit := a.elevateEmit
|
|
dismiss := a.elevateDismiss
|
|
runner := a.elevateRunner
|
|
winCtx := a.ctx
|
|
hasWindow := winCtx != nil
|
|
locale := i18n.EffectiveLocale(i18n.Auto)
|
|
if a.cfg != nil {
|
|
locale = i18n.EffectiveLocale(a.cfg.Locale)
|
|
}
|
|
a.mu.Unlock()
|
|
if emit == nil && !hasWindow {
|
|
broker.Audit(pluginID, "ELEVATE_UNAVAILABLE", spec.Action)
|
|
return nil, errors.New("elevation_unavailable: the desktop window is required to confirm elevation")
|
|
}
|
|
|
|
// One confirmation and one servicing run at a time.
|
|
a.elevatePromptMu.Lock()
|
|
defer a.elevatePromptMu.Unlock()
|
|
|
|
prompt := ElevationPrompt{
|
|
PluginID: pluginID, PluginName: pluginID, Action: spec.Action,
|
|
NeedsReboot: spec.NeedsReboot, NeedsLogoff: spec.NeedsLogoff,
|
|
}
|
|
if info, found := reg.Info(pluginID); found {
|
|
if name := strings.TrimSpace(info.Name.Get(string(locale))); name != "" {
|
|
prompt.PluginName = name
|
|
}
|
|
prompt.Publisher = info.Publisher
|
|
prompt.Trust = string(info.Trust)
|
|
}
|
|
|
|
ch := make(chan bool, 1)
|
|
a.elevateMu.Lock()
|
|
if a.elevatePending == nil {
|
|
a.elevatePending = map[string]chan bool{}
|
|
}
|
|
a.elevateSeq++
|
|
prompt.ID = fmt.Sprintf("elevate-%d", a.elevateSeq)
|
|
a.elevatePending[prompt.ID] = ch
|
|
a.elevateMu.Unlock()
|
|
dismissed := false
|
|
clearPrompt := func() {
|
|
a.elevateMu.Lock()
|
|
delete(a.elevatePending, prompt.ID)
|
|
a.elevateMu.Unlock()
|
|
if dismissed {
|
|
return
|
|
}
|
|
dismissed = true
|
|
if dismiss != nil {
|
|
dismiss(prompt.ID)
|
|
} else if emit == nil {
|
|
a.emit("reims:elevate-resolved", prompt.ID)
|
|
}
|
|
}
|
|
defer clearPrompt()
|
|
|
|
if emit != nil {
|
|
emit(prompt)
|
|
} else {
|
|
a.showMainWindow()
|
|
a.emit("reims:elevate-request", prompt)
|
|
}
|
|
|
|
timer := time.NewTimer(elevatePromptTimeout)
|
|
defer timer.Stop()
|
|
select {
|
|
case approved, open := <-ch:
|
|
clearPrompt()
|
|
if !open || !approved {
|
|
broker.Audit(pluginID, "ELEVATE_DENIED", spec.Action)
|
|
return nil, errors.New("elevation_denied: the user declined the elevation request")
|
|
}
|
|
case <-ctx.Done():
|
|
clearPrompt()
|
|
broker.Audit(pluginID, "ELEVATE_CANCELED", spec.Action)
|
|
return nil, fmt.Errorf("elevation_denied: %w", ctx.Err())
|
|
case <-timer.C:
|
|
clearPrompt()
|
|
broker.Audit(pluginID, "ELEVATE_TIMEOUT", spec.Action)
|
|
return nil, errors.New("elevation_denied: the confirmation timed out")
|
|
}
|
|
broker.Audit(pluginID, "ELEVATE_APPROVED", spec.Action)
|
|
|
|
targetUser := ""
|
|
if spec.Action == host.ElevateHyperVAddAdminGroup {
|
|
cur, err := user.Current()
|
|
if err != nil {
|
|
broker.Audit(pluginID, "ELEVATE_ERROR", spec.Action)
|
|
return nil, fmt.Errorf("find the current account: %w", err)
|
|
}
|
|
targetUser = cur.Username
|
|
}
|
|
if runner == nil {
|
|
runner = host.RunElevatedAction
|
|
}
|
|
res, err := runner(ctx, spec.Action, targetUser)
|
|
if err != nil {
|
|
if errors.Is(err, host.ErrElevationDeclined) {
|
|
broker.Audit(pluginID, "ELEVATE_UAC_DECLINED", spec.Action)
|
|
return nil, errors.New("elevation_denied: the Windows UAC prompt was declined")
|
|
}
|
|
broker.Audit(pluginID, "ELEVATE_ERROR", spec.Action)
|
|
return nil, err
|
|
}
|
|
broker.Audit(pluginID, "ELEVATE_OK", spec.Action)
|
|
return pluginproto.ElevateResult{
|
|
OK: res.OK,
|
|
Action: res.Action,
|
|
ExitCode: res.ExitCode,
|
|
RebootRequired: res.RebootRequired,
|
|
LogoffRequired: res.LogoffRequired,
|
|
}, nil
|
|
}
|
|
|
|
// ResolveElevationRequest delivers the person's answer to a pending plugin
|
|
// elevation request. An expired or answered request is ignored.
|
|
func (a *App) ResolveElevationRequest(id string, approved bool) error {
|
|
a.elevateMu.Lock()
|
|
ch, ok := a.elevatePending[id]
|
|
delete(a.elevatePending, id)
|
|
a.elevateMu.Unlock()
|
|
if !ok {
|
|
return nil
|
|
}
|
|
select {
|
|
case ch <- approved:
|
|
default:
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// cancelPendingElevations answers every waiting request with a no, so
|
|
// shutting down never leaves a plugin waiting on a dialog.
|
|
func (a *App) cancelPendingElevations() {
|
|
a.elevateMu.Lock()
|
|
pending := a.elevatePending
|
|
a.elevatePending = nil
|
|
a.elevateMu.Unlock()
|
|
for _, ch := range pending {
|
|
close(ch)
|
|
}
|
|
}
|