mur_common/
keychain_acl.rs1use std::path::Path;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum Trust {
24 Trusted,
26 Missing,
28}
29
30#[derive(Debug, thiserror::Error)]
31pub enum AclError {
32 #[error("keychain item {service}/{account} does not exist")]
33 NotFound { service: String, account: String },
34 #[error("{call} failed (OSStatus {status})")]
35 Os { call: &'static str, status: i32 },
36 #[error("keychain item has no app list to add to")]
37 NoAppList,
38 #[error("keychain ACLs are only managed on macOS")]
39 Unsupported,
40}
41
42#[cfg(any(target_os = "macos", test))]
53fn decide(acls: &[Option<Vec<Vec<u8>>>], target: &[u8]) -> Trust {
54 let target = trim_nul(target);
55 let listed = acls.iter().any(|acl| match acl {
56 None => true,
57 Some(apps) => apps.iter().any(|a| trim_nul(a) == target),
58 });
59 if listed {
60 Trust::Trusted
61 } else {
62 Trust::Missing
63 }
64}
65
66#[cfg(any(target_os = "macos", test))]
68fn trim_nul(b: &[u8]) -> &[u8] {
69 let end = b.iter().rposition(|&c| c != 0).map_or(0, |i| i + 1);
70 &b[..end]
71}
72
73pub fn check(service: &str, account: &str, app: &Path) -> Result<Trust, AclError> {
76 if crate::secret::keychain_blocked() {
77 return Err(AclError::Unsupported);
78 }
79 imp::check(service, account, app)
80}
81
82pub fn ensure(service: &str, account: &str, app: &Path) -> Result<bool, AclError> {
86 if crate::secret::keychain_blocked() {
87 return Err(AclError::Unsupported);
88 }
89 imp::ensure(service, account, app)
90}
91
92#[cfg(not(target_os = "macos"))]
93mod imp {
94 use super::*;
95 pub(super) fn check(_: &str, _: &str, _: &Path) -> Result<Trust, AclError> {
96 Err(AclError::Unsupported)
97 }
98 pub(super) fn ensure(_: &str, _: &str, _: &Path) -> Result<bool, AclError> {
99 Err(AclError::Unsupported)
100 }
101}
102
103#[cfg(target_os = "macos")]
104#[path = "keychain_acl_macos.rs"]
105mod imp;
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110
111 fn apps(list: &[&str]) -> Option<Vec<Vec<u8>>> {
112 Some(list.iter().map(|s| s.as_bytes().to_vec()).collect())
113 }
114
115 #[test]
116 fn a_listed_app_is_trusted_with_or_without_the_nul() {
117 let acls = [apps(&["/bin/mur\0", "/usr/bin/security"])];
118 assert_eq!(decide(&acls, b"/bin/mur"), Trust::Trusted);
119 assert_eq!(decide(&acls, b"/bin/mur\0"), Trust::Trusted);
120 }
121
122 #[test]
123 fn an_unlisted_app_is_missing() {
124 let acls = [apps(&["/bin/mur"])];
125 assert_eq!(decide(&acls, b"/bin/mur-agent-runtime"), Trust::Missing);
126 assert_eq!(decide(&[], b"/bin/mur"), Trust::Missing);
127 }
128
129 #[test]
130 fn an_acl_without_an_app_list_lets_anyone_read() {
131 assert_eq!(decide(&[apps(&["/x"]), None], b"/y"), Trust::Trusted);
132 }
133
134 #[test]
135 fn trim_only_drops_trailing_nuls() {
136 assert_eq!(trim_nul(b"a\0b\0\0"), b"a\0b");
137 assert_eq!(trim_nul(b"\0"), b"");
138 }
139
140 #[test]
141 fn blocked_keychain_never_touches_the_os() {
142 if crate::secret::keychain_blocked() {
144 assert!(matches!(
145 check("s", "a", Path::new("/x")),
146 Err(AclError::Unsupported)
147 ));
148 assert!(matches!(
149 ensure("s", "a", Path::new("/x")),
150 Err(AclError::Unsupported)
151 ));
152 }
153 }
154}