use std::io;
use std::net::Shutdown;
use std::ops::{Deref, Range};
use std::os::fd::{AsRawFd, BorrowedFd};
use std::time::Duration;
use giong::net::{TcpStream, UnixStream};
use giong::RecvBuf;
use giong_tls::TlsStream;
use socket2::{SockRef, TcpKeepalive};
use crate::auth::{md5_password, Scram};
use crate::config::{Config, Host};
use crate::error::{DbError, Error, ErrorKind};
use crate::protocol::{self, frame, parse_backend, Backend};
const KEEPALIVE_IDLE: Duration = Duration::from_secs(30);
const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(10);
const KEEPALIVE_RETRIES: u32 = 3;
const USER_TIMEOUT: Duration = Duration::from_secs(60);
pub(crate) enum Stream {
Tcp(TcpStream),
Unix(UnixStream),
Tls(Box<TlsStream>),
}
pub(crate) enum Chunk {
Pooled(RecvBuf),
Owned(Vec<u8>),
}
impl Deref for Chunk {
type Target = [u8];
fn deref(&self) -> &[u8] {
match self {
Chunk::Pooled(b) => b,
Chunk::Owned(v) => v,
}
}
}
impl Stream {
pub(crate) async fn recv(&self) -> io::Result<Chunk> {
match self {
Stream::Tcp(s) => s.recv().await.map(Chunk::Pooled),
Stream::Unix(s) => s.recv().await.map(Chunk::Pooled),
Stream::Tls(s) => s.recv().await.map(Chunk::Owned),
}
}
pub(crate) async fn write_all(&self, buf: Vec<u8>) -> (io::Result<()>, Vec<u8>) {
match self {
Stream::Tcp(s) => s.write_all(buf).await,
Stream::Unix(s) => s.write_all(buf).await,
Stream::Tls(s) => s.write_all(buf).await,
}
}
pub(crate) fn shutdown(&self, how: Shutdown) {
let _ = match self {
Stream::Tcp(s) => s.shutdown(how),
Stream::Unix(s) => s.shutdown(how),
Stream::Tls(s) => s.tcp().shutdown(how),
};
}
}
pub(crate) struct ReadBuf {
data: Vec<u8>,
start: usize,
skip: usize,
}
impl ReadBuf {
pub(crate) fn new() -> Self {
Self {
data: Vec::with_capacity(8192),
start: 0,
skip: 0,
}
}
pub(crate) fn extend(&mut self, mut chunk: &[u8]) {
if self.skip > 0 {
let n = self.skip.min(chunk.len());
self.skip -= n;
chunk = &chunk[n..];
}
if self.start == self.data.len() {
self.data.clear();
self.start = 0;
} else if self.start > 0 && self.start >= self.data.len() / 2 {
self.data.drain(..self.start);
self.start = 0;
}
self.data.extend_from_slice(chunk);
}
pub(crate) fn peek(&self) -> Result<Option<(u8, usize)>, Error> {
frame(&self.data[self.start..])
}
pub(crate) fn body(&self, len: usize) -> Option<Range<usize>> {
(self.data.len() - self.start >= len).then(|| self.start + 5..self.start + len)
}
pub(crate) fn bytes(&self, range: Range<usize>) -> &[u8] {
&self.data[range]
}
pub(crate) fn consume(&mut self, len: usize) {
self.start += len;
}
pub(crate) fn skip(&mut self, len: usize) {
let here = (self.data.len() - self.start).min(len);
self.start += here;
self.skip = len - here;
}
}
pub(crate) struct Startup {
pub stream: Stream,
pub buf: ReadBuf,
pub params: Vec<(String, String)>,
pub key: Option<(i32, i32)>,
pub status: u8,
}
fn connect_error(config: &Config, message: impl std::fmt::Display, e: io::Error) -> Error {
Error::io(
ErrorKind::Connect,
format!("cannot connect to {}: {message}", config.target()),
e,
)
}
pub(crate) async fn open(config: &Config) -> Result<Stream, Error> {
match &config.host {
Host::Unix(dir) => {
let path = config.socket_path(dir);
let stream = UnixStream::connect(&path).await.map_err(|e| {
connect_error(
config,
"is the server running, and is this its socket directory?",
e,
)
})?;
Ok(Stream::Unix(stream))
}
Host::Tcp(host) => {
let tls = config.tls_connector()?;
let tcp = TcpStream::connect((host.as_str(), config.port))
.await
.map_err(|e| connect_error(config, "is the server running and reachable?", e))?;
tune(&tcp).map_err(|e| connect_error(config, "cannot set socket options", e))?;
let Some(connector) = tls else {
return Ok(Stream::Tcp(tcp));
};
let mut request = Vec::with_capacity(8);
protocol::ssl_request(&mut request);
let (sent, _) = tcp.write_all(request).await;
sent.map_err(|e| connect_error(config, "sending the TLS request failed", e))?;
let answer = tcp.recv().await.map_err(|e| {
connect_error(
config,
"waiting for the answer to the TLS request failed",
e,
)
})?;
match &answer[..] {
b"S" => {}
b"N" => {
return Err(Error::new(
ErrorKind::Connect,
format!(
"the server at {} does not accept TLS (ssl is off in its \
postgresql.conf); turn TLS on there, or add sslmode=disable to the \
URL if the network is trusted",
config.target()
),
))
}
[] => {
return Err(Error::new(
ErrorKind::Connect,
format!(
"the server at {} closed the connection when asked for TLS",
config.target()
),
))
}
_ => {
return Err(Error::protocol(format!(
"the server at {} answered the TLS request with {} bytes instead of one",
config.target(),
answer.len()
)))
}
}
drop(answer);
let tls = connector.connect(host, tcp).await.map_err(|e| {
connect_error(config, format!("the TLS handshake with {host} failed"), e)
})?;
Ok(Stream::Tls(Box::new(tls)))
}
}
}
fn tune(tcp: &TcpStream) -> io::Result<()> {
tcp.set_nodelay(true)?;
let fd = unsafe { BorrowedFd::borrow_raw(tcp.as_raw_fd()) };
let sock = SockRef::from(&fd);
sock.set_tcp_keepalive(
&TcpKeepalive::new()
.with_time(KEEPALIVE_IDLE)
.with_interval(KEEPALIVE_INTERVAL)
.with_retries(KEEPALIVE_RETRIES),
)?;
sock.set_tcp_user_timeout(Some(USER_TIMEOUT))
}
async fn next_message(stream: &Stream, buf: &mut ReadBuf) -> Result<(u8, Range<usize>), Error> {
loop {
if let Some((tag, len)) = buf.peek()? {
if len > 1 << 20 {
return Err(Error::protocol(format!(
"the server sent a {len}-byte message while the connection was being set up"
)));
}
if let Some(body) = buf.body(len) {
buf.consume(len);
return Ok((tag, body));
}
}
let chunk = stream
.recv()
.await
.map_err(|e| Error::io(ErrorKind::Connect, "reading from the server failed", e))?;
if chunk.is_empty() {
return Err(Error::new(
ErrorKind::Connect,
"the server closed the connection while it was being set up",
));
}
buf.extend(&chunk);
}
}
async fn send(stream: &Stream, out: Vec<u8>) -> Result<(), Error> {
let (r, _) = stream.write_all(out).await;
r.map_err(|e| Error::io(ErrorKind::Connect, "writing to the server failed", e))
}
fn startup_error(config: &Config, body: &[u8]) -> Error {
match DbError::parse(body) {
Ok(db) => {
let kind = if db.code().starts_with("28") {
ErrorKind::Auth
} else {
ErrorKind::Connect
};
Error::with_db(
kind,
format!("the server at {} refused the connection", config.target()),
db,
)
}
Err(e) => e,
}
}
pub(crate) async fn startup(stream: Stream, config: &Config) -> Result<Startup, Error> {
let user = config.user.as_deref().ok_or_else(|| {
Error::config(
"no user to log in as; put it in the URL (postgres://USER@host/db) or call Config::user",
)
})?;
let timeout_ms = config
.statement_timeout
.as_micros()
.div_ceil(1000)
.to_string();
let mut params: Vec<(&str, &str)> = vec![
("user", user),
("client_encoding", "UTF8"),
("statement_timeout", &timeout_ms),
];
if let Some(db) = &config.dbname {
params.push(("database", db));
}
if let Some(app) = &config.application_name {
params.push(("application_name", app));
}
for (name, value) in &config.params {
params.push((name, value));
}
let mut out = Vec::new();
protocol::startup(&mut out, ¶ms)?;
send(&stream, out).await?;
let encrypted = matches!(stream, Stream::Tls(_));
let mut buf = ReadBuf::new();
let mut scram: Option<Scram> = None;
let password = || {
config.password.as_deref().ok_or_else(|| {
Error::new(
ErrorKind::Auth,
format!(
"the server asks for a password for user {user:?}, but none was given; put it \
in the URL (postgres://{user}:PASSWORD@host/db) or call Config::password"
),
)
})
};
loop {
let (tag, range) = next_message(&stream, &mut buf).await?;
let mut out = Vec::new();
match parse_backend(tag, buf.bytes(range))? {
Backend::AuthenticationOk => break,
Backend::AuthenticationCleartextPassword => {
if !encrypted && !config.is_local() {
return Err(Error::new(
ErrorKind::Auth,
format!(
"the server at {} asks for the password in clear text over a \
connection without TLS; refusing, since anyone on the network could \
read it. Turn on TLS (sslmode=verify-full), or use scram-sha-256 in \
the server's pg_hba.conf",
config.target()
),
));
}
let mut data = password()?.as_bytes().to_vec();
data.push(0);
protocol::password(&mut out, &data)?;
}
Backend::AuthenticationMd5Password(salt) => {
let mut data = md5_password(user, password()?, salt).into_bytes();
data.push(0);
protocol::password(&mut out, &data)?;
}
Backend::AuthenticationSasl(mechanisms) => {
if !mechanisms.contains(&"SCRAM-SHA-256") {
return Err(Error::new(
ErrorKind::Auth,
format!(
"the server offers only the SASL mechanisms {mechanisms:?}; \
giong-postgres supports SCRAM-SHA-256"
),
));
}
let s = Scram::new(password()?)?;
protocol::sasl_initial_response(&mut out, "SCRAM-SHA-256", &s.client_first())?;
scram = Some(s);
}
Backend::AuthenticationSaslContinue(data) => {
let s = scram.as_mut().ok_or_else(|| {
Error::protocol("the server continued a SASL exchange that was never started")
})?;
protocol::password(&mut out, &s.client_final(data)?)?;
}
Backend::AuthenticationSaslFinal(data) => {
scram
.as_mut()
.ok_or_else(|| {
Error::protocol(
"the server finished a SASL exchange that was never started",
)
})?
.finish(data)?;
}
Backend::AuthenticationOther(code) => {
let method = match code {
2 => "Kerberos V5",
6 => "SCM credentials",
7 | 8 => "GSSAPI",
9 => "SSPI",
_ => "an unknown method",
};
return Err(Error::new(
ErrorKind::Auth,
format!(
"the server asks for {method} authentication (code {code}), which \
giong-postgres does not support; use scram-sha-256 in pg_hba.conf"
),
));
}
Backend::ErrorResponse(body) => return Err(startup_error(config, body)),
Backend::NoticeResponse | Backend::NegotiateProtocolVersion => {}
_ => {
return Err(Error::protocol(format!(
"the server sent an unexpected {:?} message during authentication",
tag as char
)))
}
}
if !out.is_empty() {
send(&stream, out).await?;
}
}
if scram.as_ref().is_some_and(|s| !s.is_done()) {
return Err(Error::new(
ErrorKind::Auth,
"the server ended SCRAM authentication without proving that it knows the password; \
refusing to continue",
));
}
let mut params = Vec::new();
let mut key = None;
loop {
let (tag, range) = next_message(&stream, &mut buf).await?;
match parse_backend(tag, buf.bytes(range))? {
Backend::KeyData {
process_id,
secret_key,
} => key = Some((process_id, secret_key)),
Backend::ParameterStatus { name, value } => {
params.push((name.to_owned(), value.to_owned()));
}
Backend::ReadyForQuery(status) => {
return Ok(Startup {
stream,
buf,
params,
key,
status,
})
}
Backend::ErrorResponse(body) => return Err(startup_error(config, body)),
Backend::NoticeResponse | Backend::NegotiateProtocolVersion => {}
_ => {
return Err(Error::protocol(format!(
"the server sent an unexpected {:?} message after authentication",
tag as char
)))
}
}
}
}
pub(crate) async fn connect(config: &Config) -> Result<Startup, Error> {
let limit = config.connect_timeout;
let attempt = async { startup(open(config).await?, config).await };
match giong::time::timeout(limit, attempt).await {
Ok(result) => result,
Err(_) => Err(Error::new(
ErrorKind::Connect,
format!(
"connecting to {} took longer than the connect timeout ({limit:?}); check that \
the server is up and reachable, or raise Config::connect_timeout",
config.target()
),
)),
}
}
pub(crate) async fn cancel(config: &Config, key: (i32, i32)) -> Result<(), Error> {
let attempt = async {
let stream = open(config).await?;
let mut out = Vec::with_capacity(16);
protocol::cancel_request(&mut out, key.0, key.1);
send(&stream, out).await?;
loop {
match stream.recv().await {
Ok(chunk) if !chunk.is_empty() => continue,
_ => return Ok(()),
}
}
};
let limit = config.connect_timeout;
giong::time::timeout(limit, attempt)
.await
.unwrap_or_else(|_| {
Err(Error::new(
ErrorKind::Connect,
format!(
"sending the cancel request to {} took longer than the connect timeout ({limit:?})",
config.target()
),
))
})
}