Add a separate client binary, with some common code to the server.

The cargo workspace thing was a PITA to figure out, but it seems to be working.  Could maybe be simplified?
This commit is contained in:
Barry Pederson
2024-11-02 15:58:35 -07:00
parent 46ad77f19e
commit 076e48f457
16 changed files with 380 additions and 255 deletions
Generated
+165 -132
View File
@@ -3,100 +3,124 @@
version = 3
[[package]]
name = "ansi_term"
version = "0.12.1"
name = "anstream"
version = "0.6.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
checksum = "23a1e53f0f5d86382dafe1cf314783b2044280f406e7e1506368220ad11b1338"
dependencies = [
"winapi",
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "atty"
version = "0.2.14"
name = "anstyle"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
[[package]]
name = "anstyle-parse"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9"
dependencies = [
"hermit-abi",
"libc",
"winapi",
"utf8parse",
]
[[package]]
name = "bitflags"
version = "1.3.2"
name = "anstyle-query"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "clap"
version = "2.34.0"
version = "4.5.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "heck"
version = "0.3.3"
name = "clap_derive"
version = "4.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
version = "0.2.161"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"heck",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
name = "clap_lex"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
[[package]]
name = "client"
version = "0.2.0"
dependencies = [
"proc-macro2",
"quote",
"version_check",
"clap",
"common",
]
[[package]]
name = "colorchoice"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "common"
version = "0.2.0"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "proc-macro2"
version = "1.0.89"
@@ -119,59 +143,35 @@ dependencies = [
name = "saslauthd"
version = "0.2.0"
dependencies = [
"structopt",
"clap",
"common",
]
[[package]]
name = "server"
version = "0.2.0"
dependencies = [
"clap",
"common",
]
[[package]]
name = "strsim"
version = "0.8.0"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "1.0.109"
version = "2.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "unicode-ident"
version = "1.0.13"
@@ -179,47 +179,80 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]]
name = "unicode-segmentation"
version = "1.12.0"
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "unicode-width"
version = "0.1.14"
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
"windows-targets",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
+19 -2
View File
@@ -3,7 +3,24 @@ name = "saslauthd"
version = "0.2.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[workspace]
members = [
"common",
"server",
"client",
]
#[workspace.dependencies]
#clap = { version = "4.5.20", features = ["derive"] }
[dependencies]
structopt = "^0.3.26"
clap = { version = "4.5.20", features = ["derive"] }
common = { path = "common" }
[[bin]]
name = "saslauthd"
path = "server/src/main.rs"
[[bin]]
name = "saslauthd_test_client"
path = "client/src/main.rs"
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "client"
version = "0.2.0"
edition = "2021"
[dependencies]
clap = { version = "4.5.20", features = ["derive"] }
common = { path = "../common" }
+23
View File
@@ -0,0 +1,23 @@
mod options;
use crate::options::OPTIONS;
use common::{read_string, Request};
use std::os::unix::net::UnixStream;
fn main() -> std::io::Result<()> {
let mut stream = UnixStream::connect(&OPTIONS.socket_name)?;
println!("Connected to server");
let request = Request {
userid: OPTIONS.username.clone(),
password: OPTIONS.password.clone(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
request.write_to_stream(&mut stream)?;
let response = read_string(&mut stream)?;
println!("Response: {}", response);
Ok(())
}
+22
View File
@@ -0,0 +1,22 @@
use clap::Parser;
use std::path::PathBuf;
use std::sync::LazyLock;
#[derive(Parser, Clone, Debug)]
/// saslauthd test client
pub struct Opt {
#[arg(
short = 'f',
long = "socket-name",
default_value = "/tmp/saslauthd.sock"
)]
pub socket_name: PathBuf,
#[arg(default_value = "bp")]
pub username: String,
#[arg(default_value = "some-rad-password")]
pub password: String,
}
pub static OPTIONS: LazyLock<Opt> = LazyLock::new(Opt::parse);
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "common"
version = "0.2.0"
edition = "2021"
[dependencies]
+71
View File
@@ -0,0 +1,71 @@
use std::io::{Error, ErrorKind, Read, Write};
pub const RESPONSE_NO: [u8; 4] = [0x0, 0x2, b'N', b'O'];
pub const RESPONSE_OK: [u8; 4] = [0x0, 0x2, b'O', b'K'];
pub struct Request {
pub userid: String,
pub password: String,
pub service: String,
pub realm: String,
}
/// The saslauthd protocol transmits strings as
/// 16-bit unsigned network-byte-order lengths followed
/// by the string itself.
///
pub fn read_string(stream: &mut impl Read) -> 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(stream: &mut impl Write, s: &str) -> 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(())
}
impl Request {
pub fn from_stream(stream: &mut impl Read) -> Result<Self, Error> {
let userid = read_string(stream)?;
let password = read_string(stream)?;
let service = read_string(stream)?;
let realm = read_string(stream)?;
Ok(Request {
userid,
password,
service,
realm,
})
}
pub fn write_to_stream(&self, stream: &mut impl Write) -> Result<(), Error> {
write_string(stream, &self.userid)?;
write_string(stream, &self.password)?;
write_string(stream, &self.service)?;
write_string(stream, &self.realm)?;
Ok(())
}
}
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "server"
version = "0.2.0"
edition = "2021"
[dependencies]
clap = { version = "4.5.20", features = ["derive"] }
common = { path = "../common" }
+3 -3
View File
@@ -2,7 +2,7 @@ use std::fs::File;
use std::io::{BufRead, BufReader};
use crate::options::OPTIONS;
use crate::request::Request;
use common::Request;
#[derive(Clone)]
pub struct FileHandler;
@@ -24,7 +24,7 @@ impl FileHandler {
file_name.push(&request.userid);
if let Ok(file) = File::open(file_name) {
for line in BufReader::new(file).lines().flatten() {
for line in BufReader::new(file).lines().map_while(Result::ok) {
if !line.starts_with('#') && line.trim() == request.password {
return true;
}
@@ -38,7 +38,7 @@ impl FileHandler {
#[cfg(test)]
mod tests {
use crate::handler::FileHandler;
use crate::request::Request;
use common::Request;
#[test]
fn it_checks_good_password() {
+1 -2
View File
@@ -1,7 +1,6 @@
mod handler;
mod listener;
mod options;
mod request;
pub mod options;
mod server;
use crate::handler::FileHandler;
+20
View File
@@ -0,0 +1,20 @@
use std::path::PathBuf;
use clap::Parser;
use std::sync::LazyLock;
#[derive(Parser, Clone, Debug)]
/// saslauthd server/client
pub struct Opt {
#[arg(
short = 'f',
long = "socket-name",
default_value = "/tmp/saslauthd.sock"
)]
pub socket_name: PathBuf,
#[arg(short = 'p', long = "password-dir", default_value = "./passwords")]
pub password_dir: PathBuf,
}
pub static OPTIONS: LazyLock<Opt> = LazyLock::new(Opt::parse);
+34
View File
@@ -0,0 +1,34 @@
use crate::handler::FileHandler;
use common::{Request, RESPONSE_NO, RESPONSE_OK};
use std::io::{Error, Write};
use std::os::unix::net::UnixStream;
pub struct Server {
stream: UnixStream,
handler: FileHandler,
}
impl Server {
pub fn new(stream: UnixStream, handler: FileHandler) -> Result<Server, Error> {
Ok(Server { stream, handler })
}
fn communicate(&mut self) -> Result<(), Error> {
let request = Request::from_stream(&mut self.stream)?;
println!("userid: {}", request.userid);
if self.handler.check_auth(&request) {
self.stream.write_all(&RESPONSE_OK)?
} else {
self.stream.write_all(&RESPONSE_NO)?
}
Ok(())
}
pub fn handle_client(&mut self) {
if let Err(error) = self.communicate() {
eprint!("{error}");
}
}
}
-27
View File
@@ -1,27 +0,0 @@
use std::path::PathBuf;
use std::sync::LazyLock;
use structopt::StructOpt;
#[derive(StructOpt, Clone, Debug)]
#[structopt(name = "simple_saslauthd")]
/// Rust-based web crawler
pub struct Opt {
#[structopt(
short = "f",
long = "socket-name",
default_value = "/tmp/saslauthd.sock",
parse(from_os_str)
)]
pub socket_name: PathBuf,
#[structopt(
short = "p",
long = "password-dir",
default_value = "./passwords",
parse(from_os_str)
)]
pub password_dir: PathBuf,
}
pub static OPTIONS: LazyLock<Opt> = LazyLock::new(Opt::from_args);
-6
View File
@@ -1,6 +0,0 @@
pub struct Request {
pub userid: String,
pub password: String,
pub service: String,
pub realm: String,
}
-83
View File
@@ -1,83 +0,0 @@
use crate::handler::FileHandler;
use crate::request::Request;
use std::io::{Error, ErrorKind, Read, Write};
use std::os::unix::net::UnixStream;
const RESPONSE_NO: [u8; 4] = [0x0, 0x2, b'N', b'O'];
const RESPONSE_OK: [u8; 4] = [0x0, 0x2, b'O', b'K'];
pub struct Server {
stream: UnixStream,
handler: FileHandler,
}
impl Server {
pub fn new(stream: UnixStream, handler: FileHandler) -> Result<Server, Error> {
Ok(Server { stream, handler })
}
/// The saslauthd protocol transmits strings as
/// 16-bit unsigned network-byte-order lengths followed
/// by the string itself.
///
fn read_string(&mut self) -> Result<String, Error> {
let mut length_buffer = [0u8, 0u8];
self.stream.read_exact(&mut length_buffer)?;
let length = u16::from_be_bytes(length_buffer);
let mut string_buffer = vec![0u8; length as usize];
self.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(&mut self, s: &str) -> 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];
self.stream.write_all(&length_buffer)?;
self.stream.write_all(s.as_bytes())?;
Ok(())
}
*/
fn communicate(&mut self) -> Result<(), Error> {
let userid = self.read_string()?;
let password = self.read_string()?;
let service = self.read_string()?;
let realm = self.read_string()?;
let request = Request {
userid,
password,
service,
realm,
};
if self.handler.check_auth(&request) {
self.stream.write_all(&RESPONSE_OK)?
} else {
self.stream.write_all(&RESPONSE_NO)?
}
Ok(())
}
pub fn handle_client(&mut self) {
if let Err(error) = self.communicate() {
eprint!("{error}");
}
}
}