Add config validation, remove command, status filtering, and unlock method display
config check: validates UUID format, recognized methods, keyfile consistency and existence. Reports all issues with alias context. remove: deletes a device from config by alias. Inverse of add. status: --mounted, --unlocked, --locked flags filter the device table. Flags combine as OR. mount/unlock: display which method succeeded and key slot used, e.g. "(fido2, key slot 1)". cryptsetup Open now runs with -v and parses "Key slot N unlocked" from stderr via io.MultiWriter. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,31 +1,49 @@
|
||||
package cryptsetup
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.wntrmute.dev/kyle/arca/internal/verbose"
|
||||
)
|
||||
|
||||
// OpenResult holds information about a successful cryptsetup open.
|
||||
type OpenResult struct {
|
||||
KeySlot string // e.g., "1", or "" if not parsed
|
||||
}
|
||||
|
||||
var keySlotPattern = regexp.MustCompile(`Key slot (\d+) unlocked`)
|
||||
|
||||
// Open opens a LUKS device using cryptsetup with token-based unlock.
|
||||
func Open(devicePath, mapperName string) error {
|
||||
args := withTokenPluginEnv([]string{"cryptsetup", "open", devicePath, mapperName, "--token-only"})
|
||||
// Returns info about which key slot was used.
|
||||
func Open(devicePath, mapperName string) (OpenResult, error) {
|
||||
args := withTokenPluginEnv([]string{"cryptsetup", "open", devicePath, mapperName, "--token-only", "-v"})
|
||||
args = withPrivilege(args)
|
||||
|
||||
verbose.Printf("exec: %s", strings.Join(args, " "))
|
||||
|
||||
var buf bytes.Buffer
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stderr = io.MultiWriter(os.Stderr, &buf)
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("cryptsetup open --token-only: %w", err)
|
||||
return OpenResult{}, fmt.Errorf("cryptsetup open --token-only: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
var result OpenResult
|
||||
if m := keySlotPattern.FindStringSubmatch(buf.String()); len(m) > 1 {
|
||||
result.KeySlot = m[1]
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Close closes a LUKS mapping.
|
||||
|
||||
Reference in New Issue
Block a user