2023-12-07 10:04:14 +03:00
|
|
|
use crate::{args::Args, server::Response, utils::unix_now};
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
use anyhow::{anyhow, bail, Result};
|
2024-07-22 03:02:32 +03:00
|
|
|
use base64::{engine::general_purpose::STANDARD, Engine as _};
|
2022-06-04 19:09:21 +03:00
|
|
|
use headers::HeaderValue;
|
2023-12-07 10:04:14 +03:00
|
|
|
use hyper::{header::WWW_AUTHENTICATE, Method};
|
2023-06-01 13:52:05 +03:00
|
|
|
use indexmap::IndexMap;
|
2022-06-04 19:09:21 +03:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use md5::Context;
|
2023-06-01 13:52:05 +03:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2022-06-04 19:09:21 +03:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-06-19 17:53:51 +03:00
|
|
|
const REALM: &str = "DUFS";
|
2023-11-03 15:36:23 +03:00
|
|
|
const DIGEST_AUTH_TIMEOUT: u32 = 604800; // 7 days
|
2022-06-04 19:09:21 +03:00
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref NONCESTARTHASH: Context = {
|
|
|
|
let mut h = Context::new();
|
|
|
|
h.consume(Uuid::new_v4().as_bytes());
|
|
|
|
h.consume(std::process::id().to_be_bytes());
|
|
|
|
h
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-26 23:24:25 +03:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2022-06-19 06:26:03 +03:00
|
|
|
pub struct AccessControl {
|
2023-11-04 13:12:58 +03:00
|
|
|
use_hashed_password: bool,
|
2023-06-01 13:52:05 +03:00
|
|
|
users: IndexMap<String, (String, AccessPaths)>,
|
2024-01-15 15:53:59 +03:00
|
|
|
anonymous: Option<AccessPaths>,
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
|
|
|
|
2023-11-04 11:58:19 +03:00
|
|
|
impl Default for AccessControl {
|
|
|
|
fn default() -> Self {
|
|
|
|
AccessControl {
|
2023-11-04 13:12:58 +03:00
|
|
|
use_hashed_password: false,
|
2023-11-04 11:58:19 +03:00
|
|
|
users: IndexMap::new(),
|
2024-01-15 15:53:59 +03:00
|
|
|
anonymous: Some(AccessPaths::new(AccessPerm::ReadWrite)),
|
2023-11-04 11:58:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 06:26:03 +03:00
|
|
|
impl AccessControl {
|
2023-06-01 13:52:05 +03:00
|
|
|
pub fn new(raw_rules: &[&str]) -> Result<Self> {
|
2022-06-19 06:26:03 +03:00
|
|
|
if raw_rules.is_empty() {
|
2023-11-04 11:58:19 +03:00
|
|
|
return Ok(Default::default());
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
2023-12-21 12:28:13 +03:00
|
|
|
let new_raw_rules = split_rules(raw_rules);
|
2023-11-04 13:12:58 +03:00
|
|
|
let mut use_hashed_password = false;
|
2023-12-23 13:36:46 +03:00
|
|
|
let mut annoy_paths = None;
|
|
|
|
let mut account_paths_pairs = vec![];
|
2023-11-26 17:15:49 +03:00
|
|
|
for rule in &new_raw_rules {
|
2023-12-23 13:36:46 +03:00
|
|
|
let (account, paths) =
|
|
|
|
split_account_paths(rule).ok_or_else(|| anyhow!("Invalid auth `{rule}`"))?;
|
2023-11-26 17:15:49 +03:00
|
|
|
if account.is_empty() {
|
2023-12-23 13:36:46 +03:00
|
|
|
if annoy_paths.is_some() {
|
|
|
|
bail!("Invalid auth, no duplicate anonymous rules");
|
|
|
|
}
|
|
|
|
annoy_paths = Some(paths)
|
2023-11-26 17:15:49 +03:00
|
|
|
} else if let Some((user, pass)) = account.split_once(':') {
|
2023-06-01 13:52:05 +03:00
|
|
|
if user.is_empty() || pass.is_empty() {
|
2023-12-23 13:36:46 +03:00
|
|
|
bail!("Invalid auth `{rule}`");
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
2023-12-23 13:36:46 +03:00
|
|
|
account_paths_pairs.push((user, pass, paths));
|
2023-06-01 13:52:05 +03:00
|
|
|
}
|
|
|
|
}
|
2024-01-15 15:53:59 +03:00
|
|
|
let mut anonymous = None;
|
2023-12-23 13:36:46 +03:00
|
|
|
if let Some(paths) = annoy_paths {
|
|
|
|
let mut access_paths = AccessPaths::default();
|
|
|
|
access_paths.merge(paths);
|
2024-01-15 15:53:59 +03:00
|
|
|
anonymous = Some(access_paths);
|
2023-12-23 13:36:46 +03:00
|
|
|
}
|
|
|
|
let mut users = IndexMap::new();
|
|
|
|
for (user, pass, paths) in account_paths_pairs.into_iter() {
|
2024-01-15 15:53:59 +03:00
|
|
|
let mut access_paths = anonymous.clone().unwrap_or_default();
|
2023-12-23 13:36:46 +03:00
|
|
|
access_paths
|
|
|
|
.merge(paths)
|
|
|
|
.ok_or_else(|| anyhow!("Invalid auth `{user}:{pass}@{paths}"))?;
|
|
|
|
if pass.starts_with("$6$") {
|
|
|
|
use_hashed_password = true;
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
2023-12-23 13:36:46 +03:00
|
|
|
users.insert(user.to_string(), (pass.to_string(), access_paths));
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
2023-12-23 13:36:46 +03:00
|
|
|
|
2023-11-04 13:12:58 +03:00
|
|
|
Ok(Self {
|
|
|
|
use_hashed_password,
|
|
|
|
users,
|
2024-01-15 15:53:59 +03:00
|
|
|
anonymous,
|
2023-11-04 13:12:58 +03:00
|
|
|
})
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
|
|
|
|
2023-06-05 06:40:31 +03:00
|
|
|
pub fn exist(&self) -> bool {
|
|
|
|
!self.users.is_empty()
|
2023-02-21 07:42:40 +03:00
|
|
|
}
|
|
|
|
|
2022-06-19 06:26:03 +03:00
|
|
|
pub fn guard(
|
|
|
|
&self,
|
|
|
|
path: &str,
|
|
|
|
method: &Method,
|
|
|
|
authorization: Option<&HeaderValue>,
|
2024-05-11 11:18:18 +03:00
|
|
|
guard_options: bool,
|
2023-06-01 13:52:05 +03:00
|
|
|
) -> (Option<String>, Option<AccessPaths>) {
|
2024-12-11 03:57:30 +03:00
|
|
|
if self.users.is_empty() {
|
|
|
|
return (None, Some(AccessPaths::new(AccessPerm::ReadWrite)));
|
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
if let Some(authorization) = authorization {
|
2023-11-03 15:36:23 +03:00
|
|
|
if let Some(user) = get_auth_user(authorization) {
|
2023-06-01 13:52:05 +03:00
|
|
|
if let Some((pass, paths)) = self.users.get(&user) {
|
|
|
|
if method == Method::OPTIONS {
|
|
|
|
return (Some(user), Some(AccessPaths::new(AccessPerm::ReadOnly)));
|
|
|
|
}
|
2023-11-03 15:36:23 +03:00
|
|
|
if check_auth(authorization, method.as_str(), &user, pass).is_some() {
|
2023-06-01 13:52:05 +03:00
|
|
|
return (Some(user), paths.find(path, !is_readonly_method(method)));
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-19 05:25:11 +03:00
|
|
|
|
|
|
|
return (None, None);
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
|
2024-05-11 11:18:18 +03:00
|
|
|
if !guard_options && method == Method::OPTIONS {
|
2023-06-01 13:52:05 +03:00
|
|
|
return (None, Some(AccessPaths::new(AccessPerm::ReadOnly)));
|
|
|
|
}
|
|
|
|
|
2024-01-15 15:53:59 +03:00
|
|
|
if let Some(paths) = self.anonymous.as_ref() {
|
2023-06-01 13:52:05 +03:00
|
|
|
return (None, paths.find(path, !is_readonly_method(method)));
|
|
|
|
}
|
|
|
|
|
|
|
|
(None, None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
|
|
|
pub struct AccessPaths {
|
|
|
|
perm: AccessPerm,
|
|
|
|
children: IndexMap<String, AccessPaths>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AccessPaths {
|
|
|
|
pub fn new(perm: AccessPerm) -> Self {
|
|
|
|
Self {
|
|
|
|
perm,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn perm(&self) -> AccessPerm {
|
|
|
|
self.perm
|
|
|
|
}
|
|
|
|
|
2023-12-23 13:36:46 +03:00
|
|
|
pub fn set_perm(&mut self, perm: AccessPerm) {
|
2024-01-17 06:31:26 +03:00
|
|
|
if !perm.indexonly() {
|
2023-12-23 13:36:46 +03:00
|
|
|
self.perm = perm;
|
2023-06-01 13:52:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 13:36:46 +03:00
|
|
|
pub fn merge(&mut self, paths: &str) -> Option<()> {
|
|
|
|
for item in paths.trim_matches(',').split(',') {
|
|
|
|
let (path, perm) = match item.split_once(':') {
|
|
|
|
None => (item, AccessPerm::ReadOnly),
|
|
|
|
Some((path, "ro")) => (path, AccessPerm::ReadOnly),
|
|
|
|
Some((path, "rw")) => (path, AccessPerm::ReadWrite),
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
self.add(path, perm);
|
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add(&mut self, path: &str, perm: AccessPerm) {
|
2023-06-01 13:52:05 +03:00
|
|
|
let path = path.trim_matches('/');
|
|
|
|
if path.is_empty() {
|
|
|
|
self.set_perm(perm);
|
|
|
|
} else {
|
|
|
|
let parts: Vec<&str> = path.split('/').collect();
|
|
|
|
self.add_impl(&parts, perm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_impl(&mut self, parts: &[&str], perm: AccessPerm) {
|
|
|
|
let parts_len = parts.len();
|
|
|
|
if parts_len == 0 {
|
|
|
|
self.set_perm(perm);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let child = self.children.entry(parts[0].to_string()).or_default();
|
|
|
|
child.add_impl(&parts[1..], perm)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find(&self, path: &str, writable: bool) -> Option<AccessPaths> {
|
|
|
|
let parts: Vec<&str> = path
|
|
|
|
.trim_matches('/')
|
|
|
|
.split('/')
|
|
|
|
.filter(|v| !v.is_empty())
|
|
|
|
.collect();
|
|
|
|
let target = self.find_impl(&parts, self.perm)?;
|
|
|
|
if writable && !target.perm().readwrite() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_impl(&self, parts: &[&str], perm: AccessPerm) -> Option<AccessPaths> {
|
2024-01-17 06:31:26 +03:00
|
|
|
let perm = if !self.perm.indexonly() {
|
2023-12-21 12:28:13 +03:00
|
|
|
self.perm
|
|
|
|
} else {
|
|
|
|
perm
|
|
|
|
};
|
2023-06-01 13:52:05 +03:00
|
|
|
if parts.is_empty() {
|
2024-01-17 06:31:26 +03:00
|
|
|
if perm.indexonly() {
|
2023-06-01 13:52:05 +03:00
|
|
|
return Some(self.clone());
|
|
|
|
} else {
|
|
|
|
return Some(AccessPaths::new(perm));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let child = match self.children.get(parts[0]) {
|
|
|
|
Some(v) => v,
|
|
|
|
None => {
|
2024-01-17 06:31:26 +03:00
|
|
|
if perm.indexonly() {
|
2023-06-01 13:52:05 +03:00
|
|
|
return None;
|
|
|
|
} else {
|
|
|
|
return Some(AccessPaths::new(perm));
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
};
|
|
|
|
child.find_impl(&parts[1..], perm)
|
|
|
|
}
|
|
|
|
|
2023-12-21 12:28:13 +03:00
|
|
|
pub fn child_names(&self) -> Vec<&String> {
|
2023-06-01 13:52:05 +03:00
|
|
|
self.children.keys().collect()
|
|
|
|
}
|
|
|
|
|
2023-12-21 12:28:13 +03:00
|
|
|
pub fn child_paths(&self, base: &Path) -> Vec<PathBuf> {
|
2024-01-17 06:31:26 +03:00
|
|
|
if !self.perm().indexonly() {
|
2023-06-01 13:52:05 +03:00
|
|
|
return vec![base.to_path_buf()];
|
|
|
|
}
|
|
|
|
let mut output = vec![];
|
2023-12-21 12:28:13 +03:00
|
|
|
self.child_paths_impl(&mut output, base);
|
2023-06-01 13:52:05 +03:00
|
|
|
output
|
|
|
|
}
|
|
|
|
|
2023-12-21 12:28:13 +03:00
|
|
|
fn child_paths_impl(&self, output: &mut Vec<PathBuf>, base: &Path) {
|
2023-06-01 13:52:05 +03:00
|
|
|
for (name, child) in self.children.iter() {
|
|
|
|
let base = base.join(name);
|
2024-01-17 06:31:26 +03:00
|
|
|
if child.perm().indexonly() {
|
2023-12-21 12:28:13 +03:00
|
|
|
child.child_paths_impl(output, &base);
|
2023-06-01 13:52:05 +03:00
|
|
|
} else {
|
|
|
|
output.push(base)
|
|
|
|
}
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:52:05 +03:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
|
|
|
|
pub enum AccessPerm {
|
|
|
|
#[default]
|
2024-01-17 06:31:26 +03:00
|
|
|
IndexOnly,
|
2022-06-19 06:26:03 +03:00
|
|
|
ReadOnly,
|
2023-06-02 13:38:59 +03:00
|
|
|
ReadWrite,
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:52:05 +03:00
|
|
|
impl AccessPerm {
|
2024-01-17 06:31:26 +03:00
|
|
|
pub fn indexonly(&self) -> bool {
|
|
|
|
self == &AccessPerm::IndexOnly
|
2023-12-23 13:36:46 +03:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:52:05 +03:00
|
|
|
pub fn readwrite(&self) -> bool {
|
|
|
|
self == &AccessPerm::ReadWrite
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 10:04:14 +03:00
|
|
|
pub fn www_authenticate(res: &mut Response, args: &Args) -> Result<()> {
|
|
|
|
if args.auth.use_hashed_password {
|
|
|
|
let basic = HeaderValue::from_str(&format!("Basic realm=\"{}\"", REALM))?;
|
|
|
|
res.headers_mut().insert(WWW_AUTHENTICATE, basic);
|
2023-11-04 13:12:58 +03:00
|
|
|
} else {
|
|
|
|
let nonce = create_nonce()?;
|
2023-12-07 10:04:14 +03:00
|
|
|
let digest = HeaderValue::from_str(&format!(
|
|
|
|
"Digest realm=\"{}\", nonce=\"{}\", qop=\"auth\"",
|
|
|
|
REALM, nonce
|
|
|
|
))?;
|
|
|
|
let basic = HeaderValue::from_str(&format!("Basic realm=\"{}\"", REALM))?;
|
|
|
|
res.headers_mut().append(WWW_AUTHENTICATE, digest);
|
|
|
|
res.headers_mut().append(WWW_AUTHENTICATE, basic);
|
|
|
|
}
|
|
|
|
Ok(())
|
2022-06-19 06:26:03 +03:00
|
|
|
}
|
|
|
|
|
2023-11-03 15:36:23 +03:00
|
|
|
pub fn get_auth_user(authorization: &HeaderValue) -> Option<String> {
|
|
|
|
if let Some(value) = strip_prefix(authorization.as_bytes(), b"Basic ") {
|
2024-07-22 03:02:32 +03:00
|
|
|
let value: Vec<u8> = STANDARD.decode(value).ok()?;
|
2023-11-03 15:36:23 +03:00
|
|
|
let parts: Vec<&str> = std::str::from_utf8(&value).ok()?.split(':').collect();
|
|
|
|
Some(parts[0].to_string())
|
|
|
|
} else if let Some(value) = strip_prefix(authorization.as_bytes(), b"Digest ") {
|
|
|
|
let digest_map = to_headermap(value).ok()?;
|
|
|
|
let username = digest_map.get(b"username".as_ref())?;
|
|
|
|
std::str::from_utf8(username).map(|v| v.to_string()).ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2022-06-04 19:09:21 +03:00
|
|
|
}
|
|
|
|
|
2023-11-03 15:36:23 +03:00
|
|
|
pub fn check_auth(
|
|
|
|
authorization: &HeaderValue,
|
|
|
|
method: &str,
|
|
|
|
auth_user: &str,
|
|
|
|
auth_pass: &str,
|
|
|
|
) -> Option<()> {
|
|
|
|
if let Some(value) = strip_prefix(authorization.as_bytes(), b"Basic ") {
|
2024-07-22 03:02:32 +03:00
|
|
|
let value: Vec<u8> = STANDARD.decode(value).ok()?;
|
2024-09-06 16:22:28 +03:00
|
|
|
let (user, pass) = std::str::from_utf8(&value).ok()?.split_once(':')?;
|
2023-11-03 15:36:23 +03:00
|
|
|
|
2024-09-06 16:22:28 +03:00
|
|
|
if user != auth_user {
|
2023-11-03 15:36:23 +03:00
|
|
|
return None;
|
2022-06-04 19:09:21 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
|
2023-11-04 13:12:58 +03:00
|
|
|
if auth_pass.starts_with("$6$") {
|
2024-09-06 16:22:28 +03:00
|
|
|
if let Ok(()) = sha_crypt::sha512_check(pass, auth_pass) {
|
2023-11-04 13:12:58 +03:00
|
|
|
return Some(());
|
|
|
|
}
|
2024-09-06 16:22:28 +03:00
|
|
|
} else if pass == auth_pass {
|
2023-11-03 15:36:23 +03:00
|
|
|
return Some(());
|
2022-07-31 03:27:09 +03:00
|
|
|
}
|
2022-06-20 06:25:09 +03:00
|
|
|
|
2023-11-03 15:36:23 +03:00
|
|
|
None
|
|
|
|
} else if let Some(value) = strip_prefix(authorization.as_bytes(), b"Digest ") {
|
|
|
|
let digest_map = to_headermap(value).ok()?;
|
|
|
|
if let (Some(username), Some(nonce), Some(user_response)) = (
|
|
|
|
digest_map
|
|
|
|
.get(b"username".as_ref())
|
|
|
|
.and_then(|b| std::str::from_utf8(b).ok()),
|
|
|
|
digest_map.get(b"nonce".as_ref()),
|
|
|
|
digest_map.get(b"response".as_ref()),
|
|
|
|
) {
|
|
|
|
match validate_nonce(nonce) {
|
|
|
|
Ok(true) => {}
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
if auth_user != username {
|
|
|
|
return None;
|
2022-06-20 06:25:09 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
|
2023-11-03 15:36:23 +03:00
|
|
|
let mut h = Context::new();
|
|
|
|
h.consume(format!("{}:{}:{}", auth_user, REALM, auth_pass).as_bytes());
|
|
|
|
let auth_pass = format!("{:x}", h.compute());
|
2023-06-01 13:52:05 +03:00
|
|
|
|
2023-11-03 15:36:23 +03:00
|
|
|
let mut ha = Context::new();
|
|
|
|
ha.consume(method);
|
|
|
|
ha.consume(b":");
|
|
|
|
if let Some(uri) = digest_map.get(b"uri".as_ref()) {
|
|
|
|
ha.consume(uri);
|
|
|
|
}
|
|
|
|
let ha = format!("{:x}", ha.compute());
|
|
|
|
let mut correct_response = None;
|
|
|
|
if let Some(qop) = digest_map.get(b"qop".as_ref()) {
|
|
|
|
if qop == &b"auth".as_ref() || qop == &b"auth-int".as_ref() {
|
|
|
|
correct_response = Some({
|
|
|
|
let mut c = Context::new();
|
|
|
|
c.consume(&auth_pass);
|
|
|
|
c.consume(b":");
|
|
|
|
c.consume(nonce);
|
|
|
|
c.consume(b":");
|
|
|
|
if let Some(nc) = digest_map.get(b"nc".as_ref()) {
|
|
|
|
c.consume(nc);
|
2022-06-20 06:25:09 +03:00
|
|
|
}
|
2023-11-03 15:36:23 +03:00
|
|
|
c.consume(b":");
|
|
|
|
if let Some(cnonce) = digest_map.get(b"cnonce".as_ref()) {
|
|
|
|
c.consume(cnonce);
|
2022-06-20 06:25:09 +03:00
|
|
|
}
|
2023-11-03 15:36:23 +03:00
|
|
|
c.consume(b":");
|
|
|
|
c.consume(qop);
|
|
|
|
c.consume(b":");
|
|
|
|
c.consume(&*ha);
|
|
|
|
format!("{:x}", c.compute())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let correct_response = match correct_response {
|
|
|
|
Some(r) => r,
|
|
|
|
None => {
|
|
|
|
let mut c = Context::new();
|
|
|
|
c.consume(&auth_pass);
|
|
|
|
c.consume(b":");
|
|
|
|
c.consume(nonce);
|
|
|
|
c.consume(b":");
|
|
|
|
c.consume(&*ha);
|
|
|
|
format!("{:x}", c.compute())
|
2022-06-20 06:25:09 +03:00
|
|
|
}
|
2023-11-03 15:36:23 +03:00
|
|
|
};
|
|
|
|
if correct_response.as_bytes() == *user_response {
|
|
|
|
return Some(());
|
2022-06-04 19:09:21 +03:00
|
|
|
}
|
|
|
|
}
|
2023-11-03 15:36:23 +03:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
None
|
2022-06-04 19:09:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a nonce is still valid.
|
|
|
|
/// Return an error if it was never valid
|
2023-02-21 12:23:24 +03:00
|
|
|
fn validate_nonce(nonce: &[u8]) -> Result<bool> {
|
2022-06-04 19:09:21 +03:00
|
|
|
if nonce.len() != 34 {
|
2023-02-21 12:23:24 +03:00
|
|
|
bail!("invalid nonce");
|
2022-06-04 19:09:21 +03:00
|
|
|
}
|
|
|
|
//parse hex
|
|
|
|
if let Ok(n) = std::str::from_utf8(nonce) {
|
|
|
|
//get time
|
|
|
|
if let Ok(secs_nonce) = u32::from_str_radix(&n[..8], 16) {
|
|
|
|
//check time
|
2023-02-21 12:23:24 +03:00
|
|
|
let now = unix_now()?;
|
2022-06-04 19:09:21 +03:00
|
|
|
let secs_now = now.as_secs() as u32;
|
|
|
|
|
|
|
|
if let Some(dur) = secs_now.checked_sub(secs_nonce) {
|
|
|
|
//check hash
|
|
|
|
let mut h = NONCESTARTHASH.clone();
|
|
|
|
h.consume(secs_nonce.to_be_bytes());
|
|
|
|
let h = format!("{:x}", h.compute());
|
|
|
|
if h[..26] == n[8..34] {
|
2022-07-21 06:47:47 +03:00
|
|
|
return Ok(dur < DIGEST_AUTH_TIMEOUT);
|
2022-06-04 19:09:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 12:23:24 +03:00
|
|
|
bail!("invalid nonce");
|
2022-06-04 19:09:21 +03:00
|
|
|
}
|
|
|
|
|
2023-11-03 15:36:23 +03:00
|
|
|
fn is_readonly_method(method: &Method) -> bool {
|
|
|
|
method == Method::GET
|
|
|
|
|| method == Method::OPTIONS
|
|
|
|
|| method == Method::HEAD
|
|
|
|
|| method.as_str() == "PROPFIND"
|
2024-08-27 11:07:17 +03:00
|
|
|
|| method.as_str() == "CHECKAUTH"
|
|
|
|
|| method.as_str() == "LOGOUT"
|
2023-11-03 15:36:23 +03:00
|
|
|
}
|
|
|
|
|
2022-06-04 19:09:21 +03:00
|
|
|
fn strip_prefix<'a>(search: &'a [u8], prefix: &[u8]) -> Option<&'a [u8]> {
|
|
|
|
let l = prefix.len();
|
|
|
|
if search.len() < l {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if &search[..l] == prefix {
|
|
|
|
Some(&search[l..])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_headermap(header: &[u8]) -> Result<HashMap<&[u8], &[u8]>, ()> {
|
|
|
|
let mut sep = Vec::new();
|
2022-08-03 03:51:12 +03:00
|
|
|
let mut assign = Vec::new();
|
2022-06-04 19:09:21 +03:00
|
|
|
let mut i: usize = 0;
|
|
|
|
let mut esc = false;
|
|
|
|
for c in header {
|
|
|
|
match (c, esc) {
|
2022-08-03 03:51:12 +03:00
|
|
|
(b'=', false) => assign.push(i),
|
2022-06-04 19:09:21 +03:00
|
|
|
(b',', false) => sep.push(i),
|
|
|
|
(b'"', false) => esc = true,
|
|
|
|
(b'"', true) => esc = false,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
2022-12-11 10:18:44 +03:00
|
|
|
sep.push(i);
|
2022-06-04 19:09:21 +03:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
let mut ret = HashMap::new();
|
2022-08-03 03:51:12 +03:00
|
|
|
for (&k, &a) in sep.iter().zip(assign.iter()) {
|
2022-06-04 19:09:21 +03:00
|
|
|
while header[i] == b' ' {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
if a <= i || k <= 1 + a {
|
2022-12-11 10:18:44 +03:00
|
|
|
//keys and values must contain one char
|
2022-06-04 19:09:21 +03:00
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
let key = &header[i..a];
|
|
|
|
let val = if header[1 + a] == b'"' && header[k - 1] == b'"' {
|
|
|
|
//escaped
|
|
|
|
&header[2 + a..k - 1]
|
|
|
|
} else {
|
|
|
|
//not escaped
|
|
|
|
&header[1 + a..k]
|
|
|
|
};
|
|
|
|
i = 1 + k;
|
|
|
|
ret.insert(key, val);
|
|
|
|
}
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:23:24 +03:00
|
|
|
fn create_nonce() -> Result<String> {
|
|
|
|
let now = unix_now()?;
|
2022-06-04 19:09:21 +03:00
|
|
|
let secs = now.as_secs() as u32;
|
|
|
|
let mut h = NONCESTARTHASH.clone();
|
|
|
|
h.consume(secs.to_be_bytes());
|
|
|
|
|
|
|
|
let n = format!("{:08x}{:032x}", secs, h.compute());
|
2023-02-21 12:23:24 +03:00
|
|
|
Ok(n[..34].to_string())
|
2022-06-04 19:09:21 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
|
2023-11-26 17:15:49 +03:00
|
|
|
fn split_account_paths(s: &str) -> Option<(&str, &str)> {
|
2023-11-26 15:47:57 +03:00
|
|
|
let i = s.find("@/")?;
|
|
|
|
Some((&s[0..i], &s[i + 1..]))
|
|
|
|
}
|
|
|
|
|
2023-12-21 12:28:13 +03:00
|
|
|
fn split_rules(rules: &[&str]) -> Vec<String> {
|
2023-11-26 17:15:49 +03:00
|
|
|
let mut output = vec![];
|
|
|
|
for rule in rules {
|
|
|
|
let parts: Vec<&str> = rule.split('|').collect();
|
|
|
|
let mut rules_list = vec![];
|
|
|
|
let mut concated_part = String::new();
|
|
|
|
for (i, part) in parts.iter().enumerate() {
|
|
|
|
if part.contains("@/") {
|
|
|
|
concated_part.push_str(part);
|
|
|
|
let mut concated_part_tmp = String::new();
|
|
|
|
std::mem::swap(&mut concated_part_tmp, &mut concated_part);
|
|
|
|
rules_list.push(concated_part_tmp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
concated_part.push_str(part);
|
|
|
|
if i < parts.len() - 1 {
|
|
|
|
concated_part.push('|');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !concated_part.is_empty() {
|
|
|
|
rules_list.push(concated_part)
|
|
|
|
}
|
|
|
|
output.extend(rules_list);
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:52:05 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2023-11-26 15:47:57 +03:00
|
|
|
#[test]
|
2023-11-26 17:15:49 +03:00
|
|
|
fn test_split_account_paths() {
|
2023-11-26 15:47:57 +03:00
|
|
|
assert_eq!(
|
2023-11-26 17:15:49 +03:00
|
|
|
split_account_paths("user:pass@/:rw"),
|
|
|
|
Some(("user:pass", "/:rw"))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
split_account_paths("user:pass@@/:rw"),
|
|
|
|
Some(("user:pass@", "/:rw"))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
split_account_paths("user:pass@1@/:rw"),
|
2023-11-26 15:47:57 +03:00
|
|
|
Some(("user:pass@1", "/:rw"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-26 17:15:49 +03:00
|
|
|
#[test]
|
|
|
|
fn test_compact_split_rules() {
|
|
|
|
assert_eq!(
|
2023-12-21 12:28:13 +03:00
|
|
|
split_rules(&["user1:pass1@/:rw|user2:pass2@/:rw"]),
|
2023-11-26 17:15:49 +03:00
|
|
|
["user1:pass1@/:rw", "user2:pass2@/:rw"]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-12-21 12:28:13 +03:00
|
|
|
split_rules(&["user1:pa|ss1@/:rw|user2:pa|ss2@/:rw"]),
|
2023-11-26 17:15:49 +03:00
|
|
|
["user1:pa|ss1@/:rw", "user2:pa|ss2@/:rw"]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-12-21 12:28:13 +03:00
|
|
|
split_rules(&["user1:pa|ss1@/:rw|@/"]),
|
2023-11-26 17:15:49 +03:00
|
|
|
["user1:pa|ss1@/:rw", "@/"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:52:05 +03:00
|
|
|
#[test]
|
|
|
|
fn test_access_paths() {
|
|
|
|
let mut paths = AccessPaths::default();
|
|
|
|
paths.add("/dir1", AccessPerm::ReadWrite);
|
2023-12-21 12:28:13 +03:00
|
|
|
paths.add("/dir2/dir21", AccessPerm::ReadWrite);
|
|
|
|
paths.add("/dir2/dir21/dir211", AccessPerm::ReadOnly);
|
|
|
|
paths.add("/dir2/dir22", AccessPerm::ReadOnly);
|
|
|
|
paths.add("/dir2/dir22/dir221", AccessPerm::ReadWrite);
|
|
|
|
paths.add("/dir2/dir23/dir231", AccessPerm::ReadWrite);
|
2023-06-01 13:52:05 +03:00
|
|
|
assert_eq!(
|
2023-12-21 12:28:13 +03:00
|
|
|
paths.child_paths(Path::new("/tmp")),
|
2023-06-01 13:52:05 +03:00
|
|
|
[
|
|
|
|
"/tmp/dir1",
|
2023-12-21 12:28:13 +03:00
|
|
|
"/tmp/dir2/dir21",
|
|
|
|
"/tmp/dir2/dir22",
|
|
|
|
"/tmp/dir2/dir23/dir231",
|
2023-06-01 13:52:05 +03:00
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.map(PathBuf::from)
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
paths
|
|
|
|
.find("dir2", false)
|
2023-12-21 12:28:13 +03:00
|
|
|
.map(|v| v.child_paths(Path::new("/tmp/dir2"))),
|
2023-06-01 13:52:05 +03:00
|
|
|
Some(
|
2023-12-21 12:28:13 +03:00
|
|
|
[
|
|
|
|
"/tmp/dir2/dir21",
|
|
|
|
"/tmp/dir2/dir22",
|
|
|
|
"/tmp/dir2/dir23/dir231"
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.map(PathBuf::from)
|
|
|
|
.collect::<Vec<_>>()
|
2023-06-01 13:52:05 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(paths.find("dir2", true), None);
|
2023-12-21 12:28:13 +03:00
|
|
|
assert_eq!(
|
|
|
|
paths.find("dir1/file", true),
|
|
|
|
Some(AccessPaths::new(AccessPerm::ReadWrite))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
paths.find("dir2/dir21/file", true),
|
|
|
|
Some(AccessPaths::new(AccessPerm::ReadWrite))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
paths.find("dir2/dir21/dir211/file", false),
|
|
|
|
Some(AccessPaths::new(AccessPerm::ReadOnly))
|
|
|
|
);
|
|
|
|
assert_eq!(paths.find("dir2/dir21/dir211/file", true), None);
|
2023-06-02 13:38:59 +03:00
|
|
|
}
|
2023-06-01 13:52:05 +03:00
|
|
|
}
|