Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/utils/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,17 @@ impl SocketPath for WindowsSocketPath262 {

#[cfg(target_os = "windows")]
fn get_path(&self) -> Result<PathBuf> {
let pipe_with_username = KEEPASS_SOCKET_NAME.to_owned() + "_" + &std::env::var("USERNAME")?;
let result = PathBuf::from(format!(r#"\\.\pipe\{pipe_with_username}"#));
// KeePassXC uses Qt's `qgetenv(...)`,
// which based on C stdlib's `::getenv(...)` to get USERNAME,
// It returns different results between Rust's `std::env::var(...)`.
let username_byte = c_getenv("USERNAME")
.ok_or_else(|| anyhow!("Failed to get USERNAME from environment"))?;
let username = String::from_utf8_lossy(&username_byte);

// Construct the pipe path according to
// https://github.com/keepassxreboot/keepassxc/blob/develop/src/browser/BrowserShared.cpp
let path_string = format!(r"\\.\pipe\{KEEPASS_SOCKET_NAME}_{username}");
let result = PathBuf::from(path_string);
connectable_or_error(result)
}

Expand Down Expand Up @@ -228,3 +237,22 @@ fn connectable_or_error(path: PathBuf) -> Result<PathBuf> {
Err(e) => Err(anyhow!(e)),
}
}

#[cfg(target_os = "windows")]
fn c_getenv(name: &str) -> Option<Vec<u8>> {
use std::ffi::{CStr, CString};
use std::os::raw::c_char;

extern "C" {
fn getenv(name: *const c_char) -> *const c_char;
}

let c_name = CString::new(name).ok()?;
let ptr = unsafe { getenv(c_name.as_ptr()) };
if ptr.is_null() {
return None;
}

let c_str = unsafe { CStr::from_ptr(ptr) };
Some(c_str.to_bytes().to_vec())
}
Loading