2022-02-28 11:58:57 -08:00
|
|
|
use std::fs::{metadata, set_permissions};
|
|
|
|
|
use std::io::{Error, ErrorKind, Read, Write};
|
|
|
|
|
use std::net::Shutdown;
|
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
|
use std::os::unix::net::{UnixListener, UnixStream};
|
|
|
|
|
use std::path::PathBuf;
|
2022-02-28 14:22:16 -08:00
|
|
|
use std::{fs, thread};
|
2022-02-28 11:58:57 -08:00
|
|
|
|
|
|
|
|
extern crate yaml_rust;
|
|
|
|
|
|
2022-02-28 14:22:16 -08:00
|
|
|
use yaml_rust::{yaml::Hash, Yaml, YamlLoader};
|
2022-02-28 11:58:57 -08:00
|
|
|
|
|
|
|
|
const RESPONSE_NO: [u8; 4] = [0x0, 0x2, b'N', b'O'];
|
|
|
|
|
const RESPONSE_OK: [u8; 4] = [0x0, 0x2, b'O', b'K'];
|
|
|
|
|
|
|
|
|
|
struct SaslSock {
|
|
|
|
|
name: PathBuf,
|
|
|
|
|
listener: UnixListener,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SaslSock {
|
|
|
|
|
pub fn new(name: &str) -> Result<SaslSock, Error> {
|
|
|
|
|
let path = PathBuf::from(&name);
|
|
|
|
|
if path.exists() {
|
|
|
|
|
std::fs::remove_file(&path)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let listener = UnixListener::bind(&name)?;
|
|
|
|
|
|
|
|
|
|
let mut perms = metadata(&path)?.permissions();
|
|
|
|
|
perms.set_mode(0o0777);
|
|
|
|
|
set_permissions(&path, perms)?;
|
|
|
|
|
|
|
|
|
|
Ok(SaslSock {
|
|
|
|
|
name: PathBuf::from(&name),
|
|
|
|
|
listener,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for SaslSock {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
let _result = std::fs::remove_file(&self.name);
|
|
|
|
|
println!("Shutting down");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The saslauthd protocol transmits strings as
|
|
|
|
|
/// 16-bit unsigned network-byte-order lengths followed
|
|
|
|
|
/// by the string itself.
|
|
|
|
|
///
|
|
|
|
|
fn read_string(stream: &mut UnixStream) -> Result<String, Error> {
|
|
|
|
|
let mut length_buffer = [0u8, 0u8];
|
|
|
|
|
|
|
|
|
|
stream.read_exact(&mut length_buffer)?;
|
|
|
|
|
let length = u16::from_be_bytes(length_buffer);
|
|
|
|
|
|
|
|
|
|
let mut string_buffer = vec![0u8; length as usize];
|
|
|
|
|
|
|
|
|
|
stream.read_exact(&mut string_buffer)?;
|
|
|
|
|
|
|
|
|
|
match String::from_utf8(string_buffer) {
|
|
|
|
|
Ok(str) => Ok(str),
|
|
|
|
|
Err(_err) => Err(Error::new(ErrorKind::Other, "Invalid UTF-8 sent")),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write_string(s: &str, stream: &mut UnixStream) -> Result<(), Error> {
|
|
|
|
|
let length = s.len();
|
|
|
|
|
|
|
|
|
|
if length > 0xffff {
|
|
|
|
|
return Err(Error::new(ErrorKind::Other, "String too long to write"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let length_buffer = [(length >> 8) as u8, (length & 0xff) as u8];
|
|
|
|
|
|
|
|
|
|
stream.write_all(&length_buffer)?;
|
|
|
|
|
stream.write_all(s.as_bytes())?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 14:22:16 -08:00
|
|
|
fn check_auth(userid: &str, password: &str) -> Result<bool, Error> {
|
|
|
|
|
let yaml = fs::read_to_string("test.yml")?;
|
|
|
|
|
|
|
|
|
|
let docs =
|
|
|
|
|
YamlLoader::load_from_str(&yaml)
|
|
|
|
|
.map_err(|_| Error::new(ErrorKind::Other, "Can't parse YAML"))?;
|
|
|
|
|
|
|
|
|
|
let doc = &docs[0];
|
|
|
|
|
let users = doc["users"]
|
|
|
|
|
.as_hash()
|
|
|
|
|
.ok_or(Error::new(ErrorKind::Other, "Can't find 'users' in YAML"))?;
|
|
|
|
|
|
|
|
|
|
let passwords = users[&Yaml::from_str(&userid)].as_vec();
|
|
|
|
|
|
|
|
|
|
if let Some(passwords) = passwords {
|
|
|
|
|
// Found the passwords for the user, look for any matches
|
|
|
|
|
|
|
|
|
|
for p in passwords {
|
|
|
|
|
if let Some(str) = p.as_str() {
|
|
|
|
|
if str == password {
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn communicate(mut stream: UnixStream) -> Result<(), Error> {
|
2022-02-28 11:58:57 -08:00
|
|
|
let userid = read_string(&mut stream)?;
|
|
|
|
|
let password = read_string(&mut stream)?;
|
|
|
|
|
let service = read_string(&mut stream)?;
|
|
|
|
|
let _realm = read_string(&mut stream)?;
|
|
|
|
|
|
2022-02-28 14:22:16 -08:00
|
|
|
match check_auth(&userid, &password) {
|
|
|
|
|
Ok(true) => stream.write_all(&RESPONSE_OK)?,
|
|
|
|
|
Ok(false) => stream.write_all(&RESPONSE_NO)?,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
stream.write_all(&RESPONSE_NO)?;
|
|
|
|
|
stream.shutdown(Shutdown::Both)?;
|
|
|
|
|
return Err(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-28 11:58:57 -08:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_client(stream: UnixStream) {
|
2022-02-28 14:22:16 -08:00
|
|
|
if let Err(error) = communicate(stream) {
|
2022-02-28 11:58:57 -08:00
|
|
|
eprint!("{}", error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
|
|
|
let sock = SaslSock::new("/tmp/saslauthd.sock")?;
|
|
|
|
|
|
|
|
|
|
// accept connections and process them, spawning a new thread for each one
|
|
|
|
|
for stream in sock.listener.incoming() {
|
|
|
|
|
match stream {
|
|
|
|
|
Ok(stream) => {
|
|
|
|
|
/* connection succeeded */
|
|
|
|
|
thread::spawn(|| handle_client(stream));
|
|
|
|
|
}
|
|
|
|
|
Err(_err) => {
|
|
|
|
|
/* connection failed */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|