|
1 | 1 | use std::borrow::Cow; |
2 | 2 | use std::env::var_os; |
3 | 3 | use std::fs::File; |
4 | | -use std::io::{BufRead, BufReader}; |
| 4 | +use std::io::{BufRead, Read}; |
5 | 5 | use std::path::PathBuf; |
6 | 6 |
|
7 | | -/// try to load a password from the various pgpass file locations |
8 | | -pub fn load_password( |
9 | | - host: &str, |
10 | | - port: u16, |
11 | | - username: &str, |
12 | | - database: Option<&str>, |
13 | | -) -> Option<String> { |
14 | | - let custom_file = var_os("PGPASSFILE"); |
15 | | - if let Some(file) = custom_file { |
16 | | - if let Some(password) = |
17 | | - load_password_from_file(PathBuf::from(file), host, port, username, database) |
18 | | - { |
19 | | - return Some(password); |
| 7 | +/// PostgreSQL passfile content. |
| 8 | +#[derive(Clone, Debug, Default)] |
| 9 | +pub struct PGPassFile(String); |
| 10 | + |
| 11 | +impl PGPassFile { |
| 12 | + /// Loads the first valid passfile discovered. |
| 13 | + /// |
| 14 | + /// Loading is attempted in the following order: |
| 15 | + /// 1. Path given via the `PGPASSFILE` environment variable. |
| 16 | + /// 2. Default path (`~/.pgpass` on Linux and |
| 17 | + /// `%APPDATA%/postgres/pgpass.conf` on Windows) |
| 18 | + /// |
| 19 | + /// If loading of any file fails, the function proceeds to the next. |
| 20 | + /// Returns `None` in case no file can be loaded. |
| 21 | + pub fn load() -> Option<Self> { |
| 22 | + let custom_file = var_os("PGPASSFILE"); |
| 23 | + if let Some(file) = custom_file { |
| 24 | + if let Some(password) = Self::load_from_file(PathBuf::from(file)) { |
| 25 | + return Some(password); |
| 26 | + } |
20 | 27 | } |
21 | | - } |
22 | 28 |
|
23 | | - #[cfg(not(target_os = "windows"))] |
24 | | - let default_file = home::home_dir().map(|path| path.join(".pgpass")); |
25 | | - #[cfg(target_os = "windows")] |
26 | | - let default_file = { |
27 | | - use etcetera::BaseStrategy; |
28 | | - |
29 | | - etcetera::base_strategy::Windows::new() |
30 | | - .ok() |
31 | | - .map(|basedirs| basedirs.data_dir().join("postgres").join("pgpass.conf")) |
32 | | - }; |
33 | | - load_password_from_file(default_file?, host, port, username, database) |
34 | | -} |
| 29 | + #[cfg(not(target_os = "windows"))] |
| 30 | + let default_file = home::home_dir().map(|path| path.join(".pgpass")); |
| 31 | + #[cfg(target_os = "windows")] |
| 32 | + let default_file = { |
| 33 | + use etcetera::BaseStrategy; |
| 34 | + |
| 35 | + etcetera::base_strategy::Windows::new() |
| 36 | + .ok() |
| 37 | + .map(|basedirs| basedirs.data_dir().join("postgres").join("pgpass.conf")) |
| 38 | + }; |
| 39 | + Self::load_from_file(default_file?) |
| 40 | + } |
35 | 41 |
|
36 | | -/// try to extract a password from a pgpass file |
37 | | -fn load_password_from_file( |
38 | | - path: PathBuf, |
39 | | - host: &str, |
40 | | - port: u16, |
41 | | - username: &str, |
42 | | - database: Option<&str>, |
43 | | -) -> Option<String> { |
44 | | - let file = File::open(&path) |
45 | | - .map_err(|e| { |
46 | | - match e.kind() { |
47 | | - std::io::ErrorKind::NotFound => { |
48 | | - tracing::debug!( |
49 | | - path = %path.display(), |
50 | | - "`.pgpass` file not found", |
51 | | - ); |
52 | | - } |
53 | | - _ => { |
54 | | - tracing::warn!( |
55 | | - path = %path.display(), |
56 | | - "Failed to open `.pgpass` file: {e:?}", |
57 | | - ); |
58 | | - } |
59 | | - }; |
60 | | - }) |
61 | | - .ok()?; |
62 | | - |
63 | | - #[cfg(target_os = "linux")] |
64 | | - { |
65 | | - use std::os::unix::fs::PermissionsExt; |
66 | | - |
67 | | - // check file permissions on linux |
68 | | - |
69 | | - let metadata = file.metadata().ok()?; |
70 | | - let permissions = metadata.permissions(); |
71 | | - let mode = permissions.mode(); |
72 | | - if mode & 0o77 != 0 { |
73 | | - tracing::warn!( |
74 | | - path = %path.display(), |
75 | | - permissions = format!("{mode:o}"), |
76 | | - "Ignoring path. Permissions are not strict enough", |
77 | | - ); |
78 | | - return None; |
| 42 | + /// Returns the PostgreSQL passfile loaded from the given path. |
| 43 | + fn load_from_file(path: PathBuf) -> Option<Self> { |
| 44 | + let mut file = File::open(&path) |
| 45 | + .map_err(|e| { |
| 46 | + match e.kind() { |
| 47 | + std::io::ErrorKind::NotFound => { |
| 48 | + tracing::debug!( |
| 49 | + path = %path.display(), |
| 50 | + "`.pgpass` file not found", |
| 51 | + ); |
| 52 | + } |
| 53 | + _ => { |
| 54 | + tracing::warn!( |
| 55 | + path = %path.display(), |
| 56 | + "Failed to open `.pgpass` file: {e:?}", |
| 57 | + ); |
| 58 | + } |
| 59 | + }; |
| 60 | + }) |
| 61 | + .ok()?; |
| 62 | + |
| 63 | + #[cfg(target_os = "linux")] |
| 64 | + { |
| 65 | + use std::os::unix::fs::PermissionsExt; |
| 66 | + |
| 67 | + // check file permissions on linux |
| 68 | + |
| 69 | + let metadata = file.metadata().ok()?; |
| 70 | + let permissions = metadata.permissions(); |
| 71 | + let mode = permissions.mode(); |
| 72 | + if mode & 0o77 != 0 { |
| 73 | + tracing::warn!( |
| 74 | + path = %path.display(), |
| 75 | + permissions = format!("{mode:o}"), |
| 76 | + "Ignoring path. Permissions are not strict enough", |
| 77 | + ); |
| 78 | + return None; |
| 79 | + } |
79 | 80 | } |
| 81 | + |
| 82 | + let mut passfile = Self::default(); |
| 83 | + file.read_to_string(&mut passfile.0).ok()?; |
| 84 | + |
| 85 | + Some(passfile) |
80 | 86 | } |
81 | 87 |
|
82 | | - let reader = BufReader::new(file); |
83 | | - load_password_from_reader(reader, host, port, username, database) |
| 88 | + /// Returns the password matched by the given parameters. |
| 89 | + pub fn password_if_matching( |
| 90 | + &self, |
| 91 | + hostname: &str, |
| 92 | + port: u16, |
| 93 | + database: Option<&str>, |
| 94 | + username: &str, |
| 95 | + ) -> Option<String> { |
| 96 | + load_password_from_reader(self.0.as_bytes(), hostname, port, username, database) |
| 97 | + } |
84 | 98 | } |
85 | 99 |
|
86 | 100 | fn load_password_from_reader( |
|
0 commit comments