Got it actually checking against a yaml file
This commit is contained in:
+43
-6
@@ -4,11 +4,11 @@ use std::net::Shutdown;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::os::unix::net::{UnixListener, UnixStream};
|
||||
use std::path::PathBuf;
|
||||
use std::thread;
|
||||
use std::{fs, thread};
|
||||
|
||||
extern crate yaml_rust;
|
||||
|
||||
use yaml_rust::{YamlEmitter, YamlLoader};
|
||||
use yaml_rust::{yaml::Hash, Yaml, YamlLoader};
|
||||
|
||||
const RESPONSE_NO: [u8; 4] = [0x0, 0x2, b'N', b'O'];
|
||||
const RESPONSE_OK: [u8; 4] = [0x0, 0x2, b'O', b'K'];
|
||||
@@ -80,20 +80,57 @@ fn write_string(s: &str, stream: &mut UnixStream) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_auth(mut stream: UnixStream) -> Result<(), Error> {
|
||||
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> {
|
||||
let userid = read_string(&mut stream)?;
|
||||
let password = read_string(&mut stream)?;
|
||||
let service = read_string(&mut stream)?;
|
||||
let _realm = read_string(&mut stream)?;
|
||||
|
||||
stream.write_all(&RESPONSE_NO)?;
|
||||
stream.shutdown(Shutdown::Both)?;
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_client(stream: UnixStream) {
|
||||
if let Err(error) = check_auth(stream) {
|
||||
if let Err(error) = communicate(stream) {
|
||||
eprint!("{}", error);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user