Merge pull request #724 from mahkoh/jorth/dbus-challenge-response
dbus: use challenge-response authentication
This commit is contained in:
commit
6727e8d0d3
5 changed files with 26 additions and 40 deletions
|
|
@ -98,6 +98,8 @@ pub enum DbusError {
|
|||
ReadError(#[source] IoUringError),
|
||||
#[error("timeout")]
|
||||
IoUringError(#[source] Box<IoUringError>),
|
||||
#[error("Server did not send auth challenge")]
|
||||
NoChallenge,
|
||||
#[error("Server did not accept our authentication")]
|
||||
Auth,
|
||||
#[error("Array length is not a multiple of the element size")]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use {
|
||||
crate::{
|
||||
dbus::{DbusError, DbusSocket, incoming::handle_incoming, outgoing::handle_outgoing},
|
||||
utils::{buf::Buf, errorfmt::ErrorFmt, hex},
|
||||
utils::{buf::Buf, errorfmt::ErrorFmt},
|
||||
},
|
||||
std::{ops::Deref, rc::Rc},
|
||||
};
|
||||
|
|
@ -52,34 +52,33 @@ impl Auth {
|
|||
}
|
||||
|
||||
async fn handle_auth(&mut self) -> Result<(), DbusError> {
|
||||
let uid = hex::to_hex(&uapi::getuid().to_string());
|
||||
let mut out_buf = Buf::new(128);
|
||||
{
|
||||
let buf = out_buf
|
||||
.write_fmt(format_args!("\0AUTH EXTERNAL {}\r\n", uid))
|
||||
.unwrap();
|
||||
self.write_buf(buf).await?;
|
||||
// dbus-broker hard codes this initial burst of messages
|
||||
const AUTH: &str = "\
|
||||
\0\
|
||||
AUTH EXTERNAL\r\n\
|
||||
DATA\r\n\
|
||||
NEGOTIATE_UNIX_FD\r\n\
|
||||
BEGIN\r\n\
|
||||
";
|
||||
let out_buf = Buf::from_slice(AUTH.as_bytes());
|
||||
self.write_buf(out_buf).await?;
|
||||
let mut line;
|
||||
macro_rules! read_cmd {
|
||||
() => {{
|
||||
line = self.readline().await?;
|
||||
let (cmd, _) = line_to_cmd(&line);
|
||||
cmd
|
||||
}};
|
||||
}
|
||||
let line = self.readline().await?;
|
||||
let (cmd, _) = line_to_cmd(&line);
|
||||
if cmd != "OK" {
|
||||
if read_cmd!() != "DATA" {
|
||||
return Err(DbusError::NoChallenge);
|
||||
}
|
||||
if read_cmd!() != "OK" {
|
||||
return Err(DbusError::Auth);
|
||||
}
|
||||
{
|
||||
let buf = out_buf
|
||||
.write_fmt(format_args!("NEGOTIATE_UNIX_FD\r\n"))
|
||||
.unwrap();
|
||||
self.write_buf(buf).await?;
|
||||
}
|
||||
let line = self.readline().await?;
|
||||
let (cmd, _) = line_to_cmd(&line);
|
||||
if cmd != "AGREE_UNIX_FD" {
|
||||
if read_cmd!() != "AGREE_UNIX_FD" {
|
||||
return Err(DbusError::UnixFd);
|
||||
}
|
||||
{
|
||||
let buf = out_buf.write_fmt(format_args!("BEGIN\r\n")).unwrap();
|
||||
self.write_buf(buf).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ pub mod free_list;
|
|||
pub mod geometric_decay;
|
||||
pub mod gfx_api_ext;
|
||||
pub mod hash_map_ext;
|
||||
pub mod hex;
|
||||
pub mod line_logger;
|
||||
pub mod linkedlist;
|
||||
pub mod log_on_drop;
|
||||
|
|
|
|||
|
|
@ -145,6 +145,7 @@ impl Buf {
|
|||
unsafe { self.storage.as_ptr().add(self.range.start as _) }
|
||||
}
|
||||
|
||||
#[expect(dead_code)]
|
||||
pub fn write_fmt(&mut self, args: Arguments) -> Result<Self, io::Error> {
|
||||
let cap = self.len();
|
||||
let mut buf = self.deref_mut();
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
pub fn to_hex(b: &str) -> String {
|
||||
let mut s = String::with_capacity(b.len() * 2);
|
||||
for &b in b.as_bytes() {
|
||||
s.push(nibble_to_hex(b >> 4) as char);
|
||||
s.push(nibble_to_hex(b & 7) as char);
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn nibble_to_hex(n: u8) -> u8 {
|
||||
match n {
|
||||
n @ 0..=9 => b'0' + n,
|
||||
n => b'a' + n,
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue