aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock187
-rw-r--r--Cargo.toml16
-rw-r--r--src/datastructures.rs7
-rw-r--r--src/main.rs69
4 files changed, 244 insertions, 35 deletions
diff --git a/Cargo.lock b/Cargo.lock
index c9637a7..9da9f41 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -54,6 +54,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b"
[[package]]
+name = "arc-swap"
+version = "0.4.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dabe5a181f83789739c194cbe5a897dde195078fac08568d09221fd6137a7ba8"
+
+[[package]]
name = "arrayvec"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -344,7 +350,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cgit-simple-authentication"
-version = "0.1.2"
+version = "0.1.3"
dependencies = [
"anyhow",
"base64",
@@ -352,6 +358,8 @@ dependencies = [
"env_logger",
"handlebars",
"log",
+ "log4rs",
+ "openssl",
"rand 0.7.3",
"redis",
"serde",
@@ -366,6 +374,19 @@ dependencies = [
]
[[package]]
+name = "chrono"
+version = "0.4.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
+dependencies = [
+ "libc",
+ "num-integer",
+ "num-traits",
+ "time",
+ "winapi 0.3.9",
+]
+
+[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -421,9 +442,12 @@ checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
[[package]]
name = "cpufeatures"
-version = "0.1.0"
+version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5cd5a7748210e7ec1a9696610b1015e6e31fbf58f77a160801f124bd1c36592a"
+checksum = "dec1028182c380cc45a2e2c5ec841134f2dfd0f8f5f0a5bcd68004f81b5efdf4"
+dependencies = [
+ "libc",
+]
[[package]]
name = "crc"
@@ -476,6 +500,17 @@ dependencies = [
]
[[package]]
+name = "derivative"
+version = "2.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "digest"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -931,6 +966,12 @@ dependencies = [
]
[[package]]
+name = "linked-hash-map"
+version = "0.5.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
+
+[[package]]
name = "lock_api"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -946,10 +987,44 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [
"cfg-if 1.0.0",
+ "serde",
"value-bag",
]
[[package]]
+name = "log-mdc"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a94d21414c1f4a51209ad204c1776a3d0765002c76c6abcb602a6f09f1e881c7"
+
+[[package]]
+name = "log4rs"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d1572a880d1115ff867396eee7ae2bc924554225e67a0d3c85c745b3e60ca211"
+dependencies = [
+ "anyhow",
+ "arc-swap",
+ "chrono",
+ "derivative",
+ "fnv",
+ "humantime",
+ "libc",
+ "log",
+ "log-mdc",
+ "parking_lot",
+ "regex",
+ "serde",
+ "serde-value",
+ "serde_json",
+ "serde_yaml",
+ "thiserror",
+ "thread-id",
+ "typemap",
+ "winapi 0.3.9",
+]
+
+[[package]]
name = "maplit"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1083,6 +1158,16 @@ dependencies = [
]
[[package]]
+name = "num-integer"
+version = "0.1.44"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
+dependencies = [
+ "autocfg",
+ "num-traits",
+]
+
+[[package]]
name = "num-traits"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1140,6 +1225,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de"
[[package]]
+name = "openssl-src"
+version = "111.15.0+1.1.1k"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b1a5f6ae2ac04393b217ea9f700cd04fa9bf3d93fae2872069f3d15d908af70a"
+dependencies = [
+ "cc",
+]
+
+[[package]]
name = "openssl-sys"
version = "0.9.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1148,11 +1242,21 @@ dependencies = [
"autocfg",
"cc",
"libc",
+ "openssl-src",
"pkg-config",
"vcpkg",
]
[[package]]
+name = "ordered-float"
+version = "2.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b50b8919aecb97e5ee9aceef27e24f39c46b11831130f4a6b7b091ec5de0de12"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
name = "parking"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1298,9 +1402,9 @@ dependencies = [
[[package]]
name = "quick-error"
-version = "2.0.0"
+version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3ac73b1112776fc109b2e61909bc46c7e1bf0d7f690ffb1676553acce16d5cda"
+checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3"
[[package]]
name = "quote"
@@ -1436,9 +1540,9 @@ dependencies = [
[[package]]
name = "regex"
-version = "1.5.3"
+version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ce5f1ceb7f74abbce32601642fcf8e8508a8a8991e0621c7d750295b9095702b"
+checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
@@ -1515,6 +1619,16 @@ dependencies = [
]
[[package]]
+name = "serde-value"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c"
+dependencies = [
+ "ordered-float",
+ "serde",
+]
+
+[[package]]
name = "serde_derive"
version = "1.0.125"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1538,6 +1652,18 @@ dependencies = [
]
[[package]]
+name = "serde_yaml"
+version = "0.8.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "15654ed4ab61726bf918a39cb8d98a2e2995b002387807fa6ba58fdf7f59bb23"
+dependencies = [
+ "dtoa",
+ "linked-hash-map",
+ "serde",
+ "yaml-rust",
+]
+
+[[package]]
name = "sha-1"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1811,6 +1937,16 @@ dependencies = [
]
[[package]]
+name = "time"
+version = "0.1.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
+dependencies = [
+ "libc",
+ "winapi 0.3.9",
+]
+
+[[package]]
name = "tinyvec"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1919,6 +2055,21 @@ dependencies = [
]
[[package]]
+name = "traitobject"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"
+
+[[package]]
+name = "typemap"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "653be63c80a3296da5551e1bfd2cca35227e13cdd08c6668903ae2f4f77aa1f6"
+dependencies = [
+ "unsafe-any",
+]
+
+[[package]]
name = "typenum"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1973,10 +2124,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e"
[[package]]
+name = "unsafe-any"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f30360d7979f5e9c6e6cea48af192ea8fab4afb3cf72597154b8f08935bc9c7f"
+dependencies = [
+ "traitobject",
+]
+
+[[package]]
name = "url"
-version = "2.2.1"
+version = "2.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9ccd964113622c8e9322cfac19eb1004a07e636c545f325da085d5cdde6f1f8b"
+checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c"
dependencies = [
"form_urlencoded",
"idna",
@@ -2182,3 +2342,12 @@ name = "wyz"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214"
+
+[[package]]
+name = "yaml-rust"
+version = "0.4.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
+dependencies = [
+ "linked-hash-map",
+]
diff --git a/Cargo.toml b/Cargo.toml
index 4bb3f9e..a91d4af 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,11 +1,11 @@
[package]
name = "cgit-simple-authentication"
-version = "0.1.2"
+version = "0.1.3"
authors = ["KunoiSayami <[email protected]>"]
edition = "2018"
[dependencies]
-log = { version = "0.4", features = ["max_level_trace", "release_max_level_trace"] }
+log = { version = "0.4", features = ["max_level_trace", "release_max_level_debug"] }
env_logger = "0.8"
tokio = { version = "1", features = ["full"] }
serde_json = "1"
@@ -18,7 +18,11 @@ rand = "0.7"
clap = "2.33"
handlebars = "3.4"
url = "2.1"
-redis = { version = "0.17.0", features = ["tokio-comp"] }
-sha2 = "0.9.3"
-base64 = "0.13.0"
-simple-logging = "2.0.2" \ No newline at end of file
+redis = { version = "0.17", features = ["tokio-comp"] }
+sha2 = "0.9"
+base64 = "0.13"
+simple-logging = "2"
+log4rs = "1"
+
+[target.aarch64-unknown-linux-musl.dependencies]
+openssl = { version = "0.10", features = ["vendored"] } \ No newline at end of file
diff --git a/src/datastructures.rs b/src/datastructures.rs
index 0438b70..2a19087 100644
--- a/src/datastructures.rs
+++ b/src/datastructures.rs
@@ -35,6 +35,7 @@ pub struct Config {
pub cookie_ttl: u64,
database: String,
//access_node: hashmap,
+ pub bypass_root: bool,
}
impl Default for Config {
@@ -42,6 +43,7 @@ impl Default for Config {
Self {
cookie_ttl: DEFAULT_COOKIE_TTL,
database: DEFAULT_DATABASE_LOCATION.to_string(),
+ bypass_root: false,
}
}
}
@@ -55,6 +57,7 @@ impl Config {
let file = read_to_string(path).unwrap_or_default();
let mut cookie_ttl: u64 = DEFAULT_COOKIE_TTL;
let mut database: &str = "/etc/cgit/auth.db";
+ let mut bypass_root: bool = false;
for line in file.lines() {
let line = line.trim();
if !line.contains('=') || !line.starts_with("cgit-simple-auth-") {
@@ -66,16 +69,18 @@ impl Config {
} else {
line.split_once('=').unwrap()
};
- let key_name = key.split_once("auth-").unwrap().1;
+ let key_name = key.split_once("auth-").unwrap().1.trim();
match key_name {
"cookie-ttl" => cookie_ttl = value.parse().unwrap_or(DEFAULT_COOKIE_TTL),
"database" => database = value,
+ "bypass-root" => bypass_root = value.to_lowercase().eq("true"),
_ => {}
}
}
Self {
cookie_ttl,
database: database.to_string(),
+ bypass_root,
}
}
diff --git a/src/main.rs b/src/main.rs
index 699d34f..9d6f78e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -25,6 +25,9 @@ use crate::datastructures::{Config, FormData};
use anyhow::Result;
use clap::{App, Arg, ArgMatches, SubCommand};
use handlebars::Handlebars;
+use log4rs::append::file::FileAppender;
+use log4rs::config::{Appender, Root};
+use log4rs::encode::pattern::PatternEncoder;
use rand::Rng;
use redis::AsyncCommands;
use serde::Serialize;
@@ -70,15 +73,20 @@ struct Meta<'a> {
redirect: &'a str,
}
-// Processing the `authenticate-basic` called by cgit.
-fn cmd_authenticate_basic(_matches: &ArgMatches, _cfg: Config) -> Result<()> {
- unimplemented!()
-}
-
// Processing the `authenticate-cookie` called by cgit.
async fn cmd_authenticate_cookie(matches: &ArgMatches<'_>, cfg: Config) -> Result<bool> {
let cookies = matches.value_of("http-cookie").unwrap_or("");
+ let mut bypass = false;
+
+ if cfg.bypass_root && matches.value_of("current-url").unwrap_or("").eq("/") {
+ bypass = true;
+ }
+
+ if bypass {
+ return Ok(true);
+ }
+
if cookies.is_empty() {
return Ok(false);
}
@@ -173,7 +181,7 @@ async fn cmd_authenticate_post(matches: &ArgMatches<'_>, cfg: Config) -> Result<
log::error!("{:?}", e)
}
- if ret.unwrap() {
+ if ret.unwrap_or(false) {
let key = format!("{}_{}", get_current_timestamp(), rand_int());
let value = rand_str(COOKIE_LENGTH);
@@ -192,12 +200,7 @@ async fn cmd_authenticate_post(matches: &ArgMatches<'_>, cfg: Config) -> Result<
.value_of("https")
.map_or(false, |x| matches!(x, "yes" | "on" | "1"));
let domain = matches.value_of("http-host").unwrap_or("*");
- let location = matches
- .value_of("current-url")
- .unwrap_or("/")
- .split('?')
- .next()
- .unwrap();
+ let location = matches.value_of("http-referer").unwrap_or("/");
let cookie_suffix = if is_secure { "; secure" } else { "" };
println!("Status: 302 Found");
println!("Cache-Control: no-cache, no-store");
@@ -210,7 +213,6 @@ async fn cmd_authenticate_post(matches: &ArgMatches<'_>, cfg: Config) -> Result<
println!("Status: 403 Forbidden");
println!("Cache-Control: no-cache, no-store");
}
- println!();
Ok(())
}
@@ -265,6 +267,7 @@ async fn async_main(arg_matches: ArgMatches<'_>, cfg: Config) -> Result<i32> {
}
("authenticate-post", Some(matches)) => {
cmd_authenticate_post(matches, cfg).await?;
+ println!();
}
("body", Some(matches)) => {
cmd_body(matches, cfg).await;
@@ -277,26 +280,54 @@ async fn async_main(arg_matches: ArgMatches<'_>, cfg: Config) -> Result<i32> {
}
_ => {}
}
- log::debug!("exit");
Ok(0)
}
fn main() -> Result<()> {
- simple_logging::log_to_file("/tmp/auth.log", log::LevelFilter::Debug)?;
+ let logfile = FileAppender::builder()
+ .encoder(Box::new(PatternEncoder::new(
+ "{d(%H:%M:%S)}-{M}-{l} - {m}\n",
+ )))
+ .build("/tmp/output.log")?;
+
+ let config = log4rs::Config::builder()
+ .appender(Appender::builder().build("logfile", Box::new(logfile)))
+ .logger(log4rs::config::Logger::builder().build("sqlx::query", log::LevelFilter::Warn))
+ .logger(
+ log4rs::config::Logger::builder().build("handlebars::render", log::LevelFilter::Warn),
+ )
+ .logger(
+ log4rs::config::Logger::builder().build("handlebars::context", log::LevelFilter::Warn),
+ )
+ .build(
+ Root::builder()
+ .appender("logfile")
+ .build(log::LevelFilter::Debug),
+ )?;
+
+ log4rs::init_config(config)?;
+ //simple_logging::log_to_file("/tmp/auth.log", log::LevelFilter::Debug)?;
- log::debug!("{}", env::args().collect::<Vec<String>>().join(" "));
+ log::debug!(
+ "{}",
+ env::args()
+ .enumerate()
+ .map(|(nth, arg)| format!("[{}]={}", nth, arg))
+ .collect::<Vec<String>>()
+ .join(" ")
+ );
// Sub-arguments for each command, see cgi defines.
let sub_args = &[
- Arg::with_name("http-cookie").required(true),
+ Arg::with_name("http-cookie").required(true), // 2
Arg::with_name("request-method").required(true),
Arg::with_name("query-string").required(true),
- Arg::with_name("http-referer").required(true),
+ Arg::with_name("http-referer").required(true), // 5
Arg::with_name("path-info").required(true),
Arg::with_name("http-host").required(true),
Arg::with_name("https").required(true),
Arg::with_name("repo").required(true),
- Arg::with_name("page").required(true),
+ Arg::with_name("page").required(true), // 10
Arg::with_name("current-url").required(true),
Arg::with_name("login-url").required(true),
];