|
| 1 | +/** Manually-constructed bindings to libpq |
| 2 | +
|
| 3 | +Because pgrx is for extensions which run in the Postgres server, it rarely needs access to libpq. |
| 4 | +However, some server-side extensions need to interact with the reality that clients exist. |
| 5 | +Unfortunately, doing that means acknowledging that clients need authentication and authorization, |
| 6 | +areas of concern that are far beyond what pgrx wants to involve itself with or be responsible for. |
| 7 | +
|
| 8 | +We define some types and signatures here to allow a minimal amount of usage of items from libpq, |
| 9 | +while largely rejecting the notion that we should involve ourselves in security-laden concerns. |
| 10 | +
|
| 11 | +*/ |
| 12 | + |
| 13 | +pub mod be { |
| 14 | + |
| 15 | + unsafe extern "C" { |
| 16 | + pub static mut MyProcPort: *mut Port; |
| 17 | + } |
| 18 | + |
| 19 | + /// #define SCRAM_MAX_KEY_LEN PG_SHA256_DIGEST_LENGTH |
| 20 | + /// #define PG_SHA256_DIGEST_LENGTH 32 |
| 21 | + const SCRAM_MAX_KEY_LEN: usize = 32; |
| 22 | + |
| 23 | + #[repr(C)] |
| 24 | + pub struct Port { |
| 25 | + pub sock: crate::pgsocket, |
| 26 | + pub noblock: bool, |
| 27 | + pub proto: crate::ProtocolVersion, |
| 28 | + pub laddr: crate::SockAddr, |
| 29 | + pub raddr: crate::SockAddr, |
| 30 | + pub remote_host: *mut core::ffi::c_char, |
| 31 | + pub remote_hostname: *mut core::ffi::c_char, |
| 32 | + pub remote_hostname_resolv: core::ffi::c_int, |
| 33 | + pub remote_hostname_errcode: core::ffi::c_int, |
| 34 | + pub remote_port: *mut core::ffi::c_char, |
| 35 | + #[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15", feature = "pg16"))] |
| 36 | + pub canAcceptConnections: core::ffi::c_uint, |
| 37 | + #[cfg(feature = "pg18")] |
| 38 | + pub local_host: [core::ffi::c_char; 64], |
| 39 | + pub database_name: *mut core::ffi::c_char, |
| 40 | + pub user_name: *mut core::ffi::c_char, |
| 41 | + pub cmdline_options: *mut core::ffi::c_char, |
| 42 | + pub guc_options: *mut core::ffi::c_char, |
| 43 | + pub application_name: *mut core::ffi::c_char, |
| 44 | + |
| 45 | + // The remainder is for completeness, so Rust sees Port's layout as correctly as possible. |
| 46 | + // Ideally we would use `extern type` so the remainder of this was seen as of unknown size. |
| 47 | + // An alternative is to simply treat them as private fields, so we do. |
| 48 | + HbaLine: *mut core::ffi::c_void, |
| 49 | + |
| 50 | + #[cfg(any(feature = "pg14", feature = "pg15"))] |
| 51 | + authn_id: *const core::ffi::c_char, |
| 52 | + |
| 53 | + default_keepalives_idle: core::ffi::c_int, |
| 54 | + default_keepalives_interval: core::ffi::c_int, |
| 55 | + default_keepalives_count: core::ffi::c_int, |
| 56 | + default_tcp_user_timeout: core::ffi::c_int, |
| 57 | + keepalives_idle: core::ffi::c_int, |
| 58 | + keepalives_interval: core::ffi::c_int, |
| 59 | + keepalives_count: core::ffi::c_int, |
| 60 | + tcp_user_timeout: core::ffi::c_int, |
| 61 | + |
| 62 | + #[cfg(feature = "pg18")] |
| 63 | + scram_ClientKey: [u8; SCRAM_MAX_KEY_LEN], |
| 64 | + #[cfg(feature = "pg18")] |
| 65 | + scram_ServerKey: [u8; SCRAM_MAX_KEY_LEN], |
| 66 | + #[cfg(feature = "pg18")] |
| 67 | + has_scram_keys: bool, |
| 68 | + |
| 69 | + // as if ENABLE_GSS == false && ENABLE_SSPI == false |
| 70 | + gss: *mut core::ffi::c_void, |
| 71 | + |
| 72 | + ssl_in_use: bool, |
| 73 | + peer_cn: *mut core::ffi::c_char, |
| 74 | + peer_dn: *mut core::ffi::c_char, |
| 75 | + peer_cert_valid: bool, |
| 76 | + |
| 77 | + #[cfg(any(feature = "pg18", feature = "pg17"))] |
| 78 | + alpn_used: bool, |
| 79 | + #[cfg(feature = "pg18")] |
| 80 | + last_read_was_eof: bool, |
| 81 | + |
| 82 | + // NOTE: 5 fields remain on PG17, but two are `#ifdef USE_OPENSSL` in PG17, so treat all |
| 83 | + // as conditioned on PG18, even if that is not strictly accurate for PG17 |
| 84 | + |
| 85 | + // as if USE_OPENSSL == false && ENABLE_SSPI == false |
| 86 | + #[cfg(feature = "pg18")] |
| 87 | + ssl: *mut core::ffi::c_void, |
| 88 | + #[cfg(feature = "pg18")] |
| 89 | + peer: *mut core::ffi::c_void, |
| 90 | + |
| 91 | + #[cfg(feature = "pg18")] |
| 92 | + raw_buf: *mut core::ffi::c_char, |
| 93 | + #[cfg(feature = "pg18")] |
| 94 | + raw_buf_consumed: isize, |
| 95 | + #[cfg(feature = "pg18")] |
| 96 | + raw_buf_remaining: isize, |
| 97 | + } |
| 98 | +} |
0 commit comments