1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use alloc::{string::String, vec::Vec};
#[cfg(feature = "std")]
use alloc::vec;
#[cfg(feature = "std")]
use std::{env, fs::File, io::Read};
#[cfg(all(feature = "async", not(feature = "tokio-support")))]
use blocking::{unblock, Unblock};
#[cfg(feature = "async")]
use futures_lite::{AsyncRead, AsyncReadExt};
#[cfg(feature = "tokio-support")]
use tokio_util::compat::TokioAsyncReadCompatExt as _;
#[derive(Default, Debug)]
pub struct AuthInfo {
pub name: String,
pub data: Vec<u8>,
pub family: u16,
pub address: Vec<u8>,
pub number: Vec<u8>,
}
#[cfg(feature = "std")]
#[inline]
fn counted_string(bytes: &mut &[u8]) -> Option<Vec<u8>> {
if bytes.len() < 2 {
log::error!("Auth did not contain length bytes");
return None;
}
let length: [u8; 2] = [bytes[0], bytes[1]];
let length = u16::from_be_bytes(length) as usize;
if bytes.len() < 2 + length {
log::error!("Auth did not contain string bytes");
return None;
}
let res = (&bytes[2..length + 2]).to_vec();
*bytes = &bytes[length + 2..];
Some(res)
}
#[cfg(feature = "std")]
impl AuthInfo {
#[inline]
fn from_buffer(s: &mut &[u8]) -> Option<Self> {
if s.len() < 2 {
log::error!("Auth did not contain family bytes");
return None;
}
let family: [u8; 2] = [s[0], s[1]];
let family = u16::from_be_bytes(family);
let mut cursor = &s[2..];
let address = counted_string(&mut cursor)?;
let number = counted_string(&mut cursor)?;
let name = match String::from_utf8(counted_string(&mut cursor)?) {
Ok(name) => name,
Err(e) => {
log::warn!("Name was not valid UTF-8, doing substitution.");
let mut name = e.into_bytes();
name.retain(|b| *b < 128);
match String::from_utf8(name) {
Ok(name) => name,
Err(_) => return None,
}
}
};
let data = counted_string(&mut cursor)?;
*s = &s[10 + address.len() + number.len() + name.len() + data.len()..];
Some(AuthInfo {
name,
data,
family,
address,
number,
})
}
#[inline]
fn many_from_buffer(mut s: &[u8]) -> Option<Vec<Self>> {
let mut res = vec![];
while !s.is_empty() {
res.push(Self::from_buffer(&mut s)?);
}
Some(res)
}
#[inline]
#[must_use]
pub fn from_stream<R: Read>(reader: &mut R) -> Option<Vec<Self>> {
let mut buffer = Vec::with_capacity(128);
let _ = reader.read_to_end(&mut buffer).ok()?;
Self::many_from_buffer(&buffer)
}
#[cfg(feature = "async")]
#[inline]
#[must_use]
pub async fn from_stream_async<R: AsyncRead + Unpin>(reader: &mut R) -> Option<Vec<Self>> {
let mut buffer = Vec::with_capacity(128);
let _ = reader.read_to_end(&mut buffer).await.ok()?;
Self::many_from_buffer(&buffer)
}
#[inline]
#[must_use]
pub fn from_xauthority() -> Option<Vec<Self>> {
let fname = env::var_os("XAUTHORITY")?;
let mut file = File::open(&fname).ok()?;
Self::from_stream(&mut file)
}
#[cfg(feature = "async")]
#[inline]
#[must_use]
pub async fn from_xauthority_async() -> Option<Vec<Self>> {
let fname = env::var_os("XAUTHORITY")?;
cfg_if::cfg_if! {
if #[cfg(feature = "tokio-support")] {
let mut file = tokio::fs::File::open(&fname).await.ok()?.compat();
Self::from_stream_async(&mut file).await
} else {
let file = unblock(move || File::open(&fname)).await.ok()?;
let mut file = Unblock::new(file);
Self::from_stream_async(&mut file).await
}
}
}
#[inline]
pub(crate) fn get() -> Self {
if cfg!(test) {
return Default::default();
}
if let Some(mut v) = Self::from_xauthority() {
if v.is_empty() {
Default::default()
} else {
v.remove(0)
}
} else {
log::error!("Failed to get AuthInfo from XAUTHORITY, using empty auth info");
Default::default()
}
}
#[cfg(feature = "async")]
#[inline]
pub(crate) async fn get_async() -> Self {
if cfg!(test) {
return Default::default();
}
match Self::from_xauthority_async().await {
Some(mut v) => {
if v.is_empty() {
Default::default()
} else {
v.remove(0)
}
}
None => Default::default(),
}
}
}
#[cfg(not(feature = "std"))]
impl AuthInfo {
#[inline]
pub(crate) fn get() -> Self {
Default::default()
}
}