Lines
85.51 %
Functions
100 %
Branches
use server::command::ssh_key::AddSshKey;
use server::command::{CmdError, CmdResult};
use server::ssh_keys::parse_authorized_keys_line;
use super::{AuthMethod, Session_};
/// More than any real `authorized_keys` payload from `ssh-copy-id`.
pub(super) const MAX_UPLOAD_BYTES: usize = 64 * 1024;
/// Appends `data` unless the upload would pass [`MAX_UPLOAD_BYTES`].
pub(super) fn append_upload(buf: &mut Vec<u8>, data: &[u8]) -> bool {
let fits = buf.len().saturating_add(data.len()) <= MAX_UPLOAD_BYTES;
if fits {
buf.extend_from_slice(data);
}
fits
/// What the client may read about a failed store: the caller's own mistakes as
/// they are, any other fault only as a fixed text.
pub(super) fn client_reason(user_id: sqlx::types::Uuid, error: &CmdError) -> String {
match error {
CmdError::Args(_) | CmdError::Disabled | CmdError::NotFound(_) | CmdError::Refused(_) => {
error.to_string()
_ => {
tracing::warn!("ssh-copy-id key store failed user_id={user_id}: {error}");
"the key could not be stored".to_string()
impl Session_ {
/// True only for the ssh-copy-id bootstrap window: the session
/// authenticated with a password AND the user does not yet have
/// any registered key.
pub(super) async fn is_bootstrap_upload_allowed(&self) -> bool {
if self.auth_method != Some(AuthMethod::Password) {
return false;
let Some(uid) = self.user_id else {
};
matches!(
server::command::ssh_key::UserHasSshKey::new()
.user_id(uid)
.run()
.await,
Ok(Some(CmdResult::Bool(false)))
)
/// Parse the bytes ssh-copy-id wrote to stdin (one or more
/// `authorized_keys` lines) and register each line via
/// [`AddSshKey`]. Returns the operator-facing banner that the
/// channel writes back before closing.
pub(super) async fn process_pubkey_upload(&self, buf: &[u8]) -> String {
return "ssh-copy-id: no authenticated user; aborting.\n".to_string();
let raw = match std::str::from_utf8(buf) {
Ok(s) => s,
Err(_) => {
return "ssh-copy-id: pubkey payload is not UTF-8.\n".to_string();
let mut accepted = 0usize;
let mut errors: Vec<String> = Vec::new();
for line in raw.lines() {
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
let parsed = match parse_authorized_keys_line(trimmed) {
Ok(p) => p,
Err(e) => {
errors.push(format!("parse: {e}"));
let annotation = if parsed.comment.is_empty() {
"ssh-copy-id".to_string()
} else {
format!("ssh-copy-id ({})", parsed.comment)
match AddSshKey::new()
.key_type(parsed.key_type)
.key_blob(parsed.key_blob)
.fingerprint(parsed.fingerprint)
.annotation(annotation)
.await
{
Ok(Some(CmdResult::Uuid(_))) => accepted += 1,
Ok(_) => errors.push("server returned no key id".to_string()),
Err(e) => errors.push(client_reason(uid, &e)),
if accepted == 0 {
let detail = if errors.is_empty() {
"no usable pubkey lines".to_string()
errors.join("; ")
return format!("ssh-copy-id: no key accepted ({detail}).\n");
let user_label = self.user_name.as_deref().unwrap_or("YOUR_LOGIN");
format!(
"Identity added; password auth disabled.\r\n\
\r\n\
Suggested ~/.ssh/config entry:\r\n\
Host nomisync\r\n\
\x20 HostName {hostname}\r\n\
\x20 Port {port}\r\n\
\x20 User {user}\r\n\
\x20 IdentitiesOnly yes\r\n\
\x20 # IdentityAgent ${{SSH_AUTH_SOCK}} # uncomment if using gpg-agent\r\n\
\x20 # IdentityFile ~/.ssh/id_ed25519 # or point at your private key\r\n\
Host key fingerprint (verify on first connect):\r\n\
\x20 {fp}\r\n",
hostname = self.connect_info.hostname,
port = self.connect_info.port,
user = user_label,
fp = self.connect_info.host_fingerprint,