diff --git a/Cargo.toml b/Cargo.toml index 882cbc9..695b7e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gemserv" -version = "0.3.2" +version = "0.4.0" authors = ["int 80h "] edition = "2018" diff --git a/README b/README index 43e566a..9d5f9f6 100644 --- a/README +++ b/README @@ -9,6 +9,7 @@ A gemini server written in rust. - User directories - Reverse proxy - Redirect + - SCGI ## Installation and running diff --git a/UPDATING b/UPDATING new file mode 100644 index 0000000..65ef155 --- /dev/null +++ b/UPDATING @@ -0,0 +1,15 @@ +20200528: + AFFECTS: CGI USERS + + The cgi variable in the configuration file changed from: + + cgi = /path/to/cgi-bin + + To: + + cgi = true + cgipath = /path/to/cgi-bin + + If cgi isn't set to true the cgipath variable will not be looked at. And + if the cgipath isn't set but cgi is true then cgi will run from + anywhere. diff --git a/src/cgi.rs b/src/cgi.rs index 0f631c4..3c74ce9 100644 --- a/src/cgi.rs +++ b/src/cgi.rs @@ -66,6 +66,13 @@ pub async fn cgi( let mut envs = envs(con.peer_addr, srv, &url); envs.insert("SCRIPT_NAME".to_string(), path.file_name().unwrap().to_str().unwrap().to_string()); + match path.parent() { + Some(p) => { + std::env::set_current_dir(p)?; + }, + None => {}, + } + let cmd = Command::new(path.to_str().unwrap()) .env_clear() .envs(&envs) diff --git a/src/config.rs b/src/config.rs index eed0893..3ded2f3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -36,7 +36,10 @@ pub struct ServerCfg { impl Config { pub fn new(file: &Path) -> Result { let fd = std::fs::read_to_string(file).unwrap(); - let config: Config = toml::from_str(&fd).unwrap(); + let config: Config = match toml::from_str(&fd) { + Ok(c) => c, + Err(e) => return Err(e), + }; return Ok(config); } pub fn to_map(&self) -> HashMap { diff --git a/src/main.rs b/src/main.rs index 4825510..d064b44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ fn get_mime(path: &PathBuf) -> String { let mut mime = "text/gemini".to_string(); let ext = match path.extension() { Some(p) => p.to_str().unwrap(), - None => return mime.to_string(), + None => return mime, }; mime = match ext { @@ -38,12 +38,12 @@ fn get_mime(path: &PathBuf) -> String { _ => { match mime_guess::from_ext(ext).first() { Some(m) => m.essence_str().to_string(), - None => mime, + None => "text/plain".to_string(), } }, }; - return mime.to_string(); + return mime; } async fn get_binary(mut con: conn::Connection, path: PathBuf, meta: String) -> io::Result<()> { @@ -263,20 +263,19 @@ async fn handle_connection( } }, None => { - if perm.mode() & 0o0111 == 0o0111 { + if meta.is_file() && perm.mode() & 0o0111 == 0o0111 { cgi::cgi(con, srv, path, url).await?; return Ok(()); } }, } } - - if perm.mode() & 0o0111 == 0o0111 { + if meta.is_file() && perm.mode() & 0o0111 == 0o0111 { logger::logger(con.peer_addr, Status::NotFound, &request); con.send_status(Status::NotFound, None).await?; return Ok(()); } - + if perm.mode() & 0o0444 != 0o0444 { logger::logger(con.peer_addr, Status::NotFound, &request); con.send_status(Status::NotFound, None).await?; @@ -310,7 +309,13 @@ fn main() -> io::Result<()> { return Ok(()); } - let cfg = config::Config::new(&p)?; + let cfg = match config::Config::new(&p) { + Ok(c) => c, + Err(e) => { + eprintln!("Config error: {}", e); + return Ok(()); + }, + }; let cmap = cfg.to_map(); let default = &cfg.server[0].hostname; println!("Serving {} vhosts", cfg.server.len());