1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
// -*- Mode: Go; indent-tabs-mode: t -*-
//go:build !nosecboot
/*
* Copyright (C) 2025 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package secboot
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
sb "github.com/snapcore/secboot"
"github.com/snapcore/snapd/systemd"
)
type systemdAuthRequestor struct {
}
func (r *systemdAuthRequestor) askPassword(sourceDevicePath, msg, credentialName string) (string, error) {
enableCredential := true
err := systemd.EnsureAtLeast(249)
if systemd.IsSystemdTooOld(err) {
enableCredential = false
}
var args []string
args = append(args, "--icon", "drive-harddisk")
args = append(args, "--id", filepath.Base(os.Args[0])+":"+sourceDevicePath)
if enableCredential {
args = append(args, fmt.Sprintf("--credential=snapd.%s", credentialName))
}
args = append(args, msg)
cmd := exec.Command(
"systemd-ask-password",
args...,
)
out := new(bytes.Buffer)
cmd.Stdout = out
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("cannot execute systemd-ask-password: %v", err)
}
result, err := out.ReadString('\n')
if err != nil {
// The only error returned from bytes.Buffer.ReadString is io.EOF.
return "", errors.New("systemd-ask-password output is missing terminating newline")
}
return strings.TrimRight(result, "\n"), nil
}
func (r *systemdAuthRequestor) RequestPassphrase(volumeName, sourceDevicePath string) (string, error) {
msg := fmt.Sprintf("Please enter the passphrase for volume %s for device %s", volumeName, sourceDevicePath)
return r.askPassword(sourceDevicePath, msg, "passphrase")
}
func (r *systemdAuthRequestor) RequestRecoveryKey(volumeName, sourceDevicePath string) (sb.RecoveryKey, error) {
msg := fmt.Sprintf("Please enter the recovery key for volume %s for device %s", volumeName, sourceDevicePath)
passphrase, err := r.askPassword(sourceDevicePath, msg, "recovery")
if err != nil {
return sb.RecoveryKey{}, err
}
key, err := sb.ParseRecoveryKey(passphrase)
if err != nil {
return sb.RecoveryKey{}, fmt.Errorf("cannot parse recovery key: %w", err)
}
return key, nil
}
func NewSystemdAuthRequestor() sb.AuthRequestor {
return &systemdAuthRequestor{}
}
|