| 1 | //! git-over-SSH transport for anvil, built on `russh` (pure Rust — no OpenSSH). |
| 2 | //! |
| 3 | //! Authenticates by public key, then handles `git-upload-pack` / |
| 4 | //! `git-receive-pack` `exec` requests by running the transport-agnostic engine |
| 5 | //! in [`anvil_git::ssh`] over the channel's byte stream. |
| 6 | //! |
| 7 | //! The SSH bind address is configurable (`[ssh] listen` in the config). |
| 8 | |
| 9 | use std::net::SocketAddr; |
| 10 | use std::path::{ |
| 11 | Path, |
| 12 | PathBuf, |
| 13 | }; |
| 14 | use std::sync::Arc; |
| 15 | use std::time::Duration; |
| 16 | |
| 17 | use anvil_core::{ |
| 18 | App, |
| 19 | Error as CoreError, |
| 20 | Result as CoreResult, |
| 21 | access, |
| 22 | repos, |
| 23 | users, |
| 24 | }; |
| 25 | use anvil_git::ssh as git_ssh; |
| 26 | use russh::keys::ssh_key::{ |
| 27 | HashAlg, |
| 28 | LineEnding, |
| 29 | PublicKey, |
| 30 | }; |
| 31 | use russh::keys::{ |
| 32 | Algorithm, |
| 33 | PrivateKey, |
| 34 | }; |
| 35 | use russh::server::{ |
| 36 | self, |
| 37 | Auth, |
| 38 | Handler, |
| 39 | Msg, |
| 40 | Server as _, |
| 41 | Session, |
| 42 | }; |
| 43 | use russh::{ |
| 44 | Channel, |
| 45 | ChannelId, |
| 46 | }; |
| 47 | use tokio::net::TcpListener; |
| 48 | |
| 49 | /// Bind to the configured SSH address and serve git over SSH until shutdown. |
| 50 | pub async fn serve(app: App) -> CoreResult<()> { |
| 51 | let listen = app.config.ssh.listen.clone(); |
| 52 | let host_key = load_or_create_host_key(&app.config.data_dir)?; |
| 53 | |
| 54 | let config = Arc::new(server::Config { |
| 55 | inactivity_timeout: Some(Duration::from_secs(3600)), |
| 56 | auth_rejection_time: Duration::from_secs(1), |
| 57 | keys: vec![host_key], |
| 58 | ..Default::default() |
| 59 | }); |
| 60 | |
| 61 | let listener = TcpListener::bind(&listen).await?; |
| 62 | tracing::info!("anvil ssh server listening on {listen}"); |
| 63 | |
| 64 | let mut server = GitSshServer { app }; |
| 65 | server |
| 66 | .run_on_socket(config, &listener) |
| 67 | .await |
| 68 | .map_err(|e| CoreError::Storage(format!("ssh server: {e}")))?; |
| 69 | Ok(()) |
| 70 | } |
| 71 | |
| 72 | /// Load the persistent ed25519 host key, generating and saving it on first run |
| 73 | /// so the server identity is stable across restarts. |
| 74 | fn load_or_create_host_key(data_dir: &Path) -> CoreResult<PrivateKey> { |
| 75 | let path = data_dir.join("ssh_host_ed25519_key"); |
| 76 | if path.exists() { |
| 77 | let pem = std::fs::read_to_string(&path)?; |
| 78 | return PrivateKey::from_openssh(&pem).map_err(|e| { |
| 79 | CoreError::Config(format!("reading ssh host key {}: {e}", path.display())) |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | let key = PrivateKey::random(&mut rand::rng(), Algorithm::Ed25519) |
| 84 | .map_err(|e| CoreError::Config(format!("generating ssh host key: {e}")))?; |
| 85 | let pem = key |
| 86 | .to_openssh(LineEnding::LF) |
| 87 | .map_err(|e| CoreError::Config(format!("encoding ssh host key: {e}")))?; |
| 88 | std::fs::write(&path, pem.as_bytes())?; |
| 89 | #[cfg(unix)] |
| 90 | { |
| 91 | use std::os::unix::fs::PermissionsExt; |
| 92 | let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)); |
| 93 | } |
| 94 | tracing::info!("generated ssh host key at {}", path.display()); |
| 95 | Ok(key) |
| 96 | } |
| 97 | |
| 98 | struct GitSshServer { |
| 99 | app: App, |
| 100 | } |
| 101 | |
| 102 | impl server::Server for GitSshServer { |
| 103 | type Handler = GitSshSession; |
| 104 | |
| 105 | fn new_client(&mut self, _peer: Option<SocketAddr>) -> GitSshSession { |
| 106 | GitSshSession { |
| 107 | app: self.app.clone(), |
| 108 | channel: None, |
| 109 | protocol_v2: false, |
| 110 | authed_user: None, |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | struct GitSshSession { |
| 116 | app: App, |
| 117 | /// The session channel, taken in `channel_open_session` and consumed by the |
| 118 | /// git protocol task in `exec_request`. |
| 119 | channel: Option<Channel<Msg>>, |
| 120 | /// Set if the client requested git protocol v2 via the `GIT_PROTOCOL` env. |
| 121 | protocol_v2: bool, |
| 122 | /// The user id authenticated by public key, set in `auth_publickey`. |
| 123 | authed_user: Option<i64>, |
| 124 | } |
| 125 | |
| 126 | impl Handler for GitSshSession { |
| 127 | type Error = russh::Error; |
| 128 | |
| 129 | async fn auth_publickey(&mut self, _user: &str, key: &PublicKey) -> Result<Auth, Self::Error> { |
| 130 | // Authenticate by matching the (signature-verified) key's fingerprint to |
| 131 | // a registered user. Unknown keys are rejected. Per-repo authorization |
| 132 | // is a later milestone; for now any authenticated user has full access. |
| 133 | let fingerprint = key.fingerprint(HashAlg::Sha256).to_string(); |
| 134 | match anvil_core::ssh_keys::find_user_id_by_fingerprint(&self.app.db, &fingerprint).await { |
| 135 | Ok(Some(user_id)) => { |
| 136 | self.authed_user = Some(user_id); |
| 137 | tracing::info!("ssh auth: accepted key {fingerprint} (user {user_id})"); |
| 138 | Ok(Auth::Accept) |
| 139 | } |
| 140 | Ok(None) => { |
| 141 | tracing::info!("ssh auth: rejected unknown key {fingerprint}"); |
| 142 | Ok(Auth::reject()) |
| 143 | } |
| 144 | Err(e) => { |
| 145 | tracing::error!("ssh auth: key lookup failed: {e}"); |
| 146 | Ok(Auth::reject()) |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | async fn channel_open_session( |
| 152 | &mut self, |
| 153 | channel: Channel<Msg>, |
| 154 | _session: &mut Session, |
| 155 | ) -> Result<bool, Self::Error> { |
| 156 | self.channel = Some(channel); |
| 157 | Ok(true) |
| 158 | } |
| 159 | |
| 160 | async fn env_request( |
| 161 | &mut self, |
| 162 | _channel: ChannelId, |
| 163 | name: &str, |
| 164 | value: &str, |
| 165 | _session: &mut Session, |
| 166 | ) -> Result<(), Self::Error> { |
| 167 | if name == "GIT_PROTOCOL" && value.split(':').any(|v| v.trim() == "version=2") { |
| 168 | self.protocol_v2 = true; |
| 169 | } |
| 170 | Ok(()) |
| 171 | } |
| 172 | |
| 173 | async fn exec_request( |
| 174 | &mut self, |
| 175 | channel_id: ChannelId, |
| 176 | data: &[u8], |
| 177 | session: &mut Session, |
| 178 | ) -> Result<(), Self::Error> { |
| 179 | let command = String::from_utf8_lossy(data); |
| 180 | let Some((service, rel)) = git_ssh::parse_command(&command) else { |
| 181 | return fail(session, channel_id, "unsupported command"); |
| 182 | }; |
| 183 | let need_write = service == anvil_git::Service::ReceivePack; |
| 184 | let path = match authorize_repo(&self.app, self.authed_user, &rel, need_write).await { |
| 185 | Ok(path) => path, |
| 186 | Err(message) => return fail(session, channel_id, message), |
| 187 | }; |
| 188 | let Some(channel) = self.channel.take() else { |
| 189 | return fail(session, channel_id, "no session channel"); |
| 190 | }; |
| 191 | |
| 192 | tracing::info!( |
| 193 | "ssh {} on {rel} (user {:?})", |
| 194 | service.as_str(), |
| 195 | self.authed_user |
| 196 | ); |
| 197 | |
| 198 | let protocol_v2 = self.protocol_v2; |
| 199 | let handle = session.handle(); |
| 200 | session.channel_success(channel_id)?; |
| 201 | |
| 202 | tokio::spawn(async move { |
| 203 | let stream = channel.into_stream(); |
| 204 | let code = match git_ssh::serve(&path, service, protocol_v2, stream).await { |
| 205 | Ok(()) => 0, |
| 206 | Err(e) => { |
| 207 | tracing::error!("git ssh {}: {e}", service.as_str()); |
| 208 | 1 |
| 209 | } |
| 210 | }; |
| 211 | let _ = handle.exit_status_request(channel_id, code).await; |
| 212 | let _ = handle.eof(channel_id).await; |
| 213 | let _ = handle.close(channel_id).await; |
| 214 | }); |
| 215 | Ok(()) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /// Write a one-line error to the channel and close it with a failure status. |
| 220 | fn fail(session: &mut Session, channel_id: ChannelId, message: &str) -> Result<(), russh::Error> { |
| 221 | let _ = session.data(channel_id, format!("{message}\n").into_bytes()); |
| 222 | session.exit_status_request(channel_id, 1)?; |
| 223 | session.close(channel_id)?; |
| 224 | Ok(()) |
| 225 | } |
| 226 | |
| 227 | /// Resolve an SSH repo path (`owner/name[.git]`) to an existing bare repo and |
| 228 | /// enforce access for the authenticated user. Returns the on-disk path or a |
| 229 | /// short error message. Rejects traversal. |
| 230 | async fn authorize_repo( |
| 231 | app: &App, |
| 232 | authed_user: Option<i64>, |
| 233 | rel: &str, |
| 234 | need_write: bool, |
| 235 | ) -> Result<PathBuf, &'static str> { |
| 236 | let (owner, repo) = rel |
| 237 | .trim_start_matches('/') |
| 238 | .split_once('/') |
| 239 | .ok_or("invalid repository path")?; |
| 240 | let name = repo.strip_suffix(".git").unwrap_or(repo); |
| 241 | let bad = |s: &str| s.is_empty() || s.contains("..") || s.contains('\\') || s.contains('/'); |
| 242 | if bad(owner) || bad(name) { |
| 243 | return Err("invalid repository path"); |
| 244 | } |
| 245 | |
| 246 | let owner_user = users::find_by_username(&app.db, owner) |
| 247 | .await |
| 248 | .ok() |
| 249 | .flatten() |
| 250 | .ok_or("repository not found")?; |
| 251 | let repo = repos::find(&app.db, owner_user.id, name) |
| 252 | .await |
| 253 | .ok() |
| 254 | .flatten() |
| 255 | .ok_or("repository not found")?; |
| 256 | |
| 257 | let viewer = match authed_user { |
| 258 | Some(id) => users::find_by_id(&app.db, id).await.ok().flatten(), |
| 259 | None => None, |
| 260 | }; |
| 261 | let allowed = if need_write { |
| 262 | access::can_write(&repo, viewer.as_ref()) |
| 263 | } else { |
| 264 | access::can_read(&repo, viewer.as_ref()) |
| 265 | }; |
| 266 | if !allowed { |
| 267 | return Err("access denied"); |
| 268 | } |
| 269 | |
| 270 | let path = anvil_core::storage::repo_path(&app.config.repositories_dir(), owner, name); |
| 271 | if !path.exists() { |
| 272 | return Err("repository not found"); |
| 273 | } |
| 274 | Ok(path) |
| 275 | } |