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>
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
)
|
|
|
|
var (
|
|
ValidMethods = []string{"passphrase", "keyfile", "fido2", "tpm2"}
|
|
uuidPattern = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
|
|
)
|
|
|
|
// Validate checks the config for common issues. Returns a list of errors.
|
|
func Validate(cfg *Config) []error {
|
|
var errs []error
|
|
|
|
for alias, dev := range cfg.Devices {
|
|
if dev.UUID == "" {
|
|
errs = append(errs, fmt.Errorf("%s: missing uuid", alias))
|
|
} else if !uuidPattern.MatchString(dev.UUID) {
|
|
errs = append(errs, fmt.Errorf("%s: malformed uuid %q", alias, dev.UUID))
|
|
}
|
|
|
|
for _, m := range dev.Methods {
|
|
if !isValidMethod(m) {
|
|
errs = append(errs, fmt.Errorf("%s: unknown method %q (valid: %v)", alias, m, ValidMethods))
|
|
}
|
|
}
|
|
|
|
hasKeyfileMethod := false
|
|
for _, m := range dev.Methods {
|
|
if m == "keyfile" {
|
|
hasKeyfileMethod = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if hasKeyfileMethod && dev.Keyfile == "" {
|
|
errs = append(errs, fmt.Errorf("%s: method 'keyfile' listed but no keyfile path set", alias))
|
|
}
|
|
|
|
if dev.Keyfile != "" {
|
|
if _, err := os.Stat(dev.Keyfile); err != nil {
|
|
errs = append(errs, fmt.Errorf("%s: keyfile %q not found (may be on removable media)", alias, dev.Keyfile))
|
|
}
|
|
}
|
|
}
|
|
|
|
return errs
|
|
}
|
|
|
|
func isValidMethod(m string) bool {
|
|
for _, v := range ValidMethods {
|
|
if m == v {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|