From 643edd0178819d2422cd9dd5a0667217ca56ab19 Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Mon, 27 Jun 2022 14:10:29 +0200 Subject: [PATCH] ask for credentials, add postinst script --- Cargo.lock | 15 ++++++- debian/.gitkeep | 0 debian/postinst | 10 +++++ ffplayout-api/Cargo.toml | 3 +- ffplayout-api/README.md | 2 + ffplayout-api/src/utils/args_parse.rs | 3 ++ ffplayout-api/src/utils/mod.rs | 64 ++++++++++++++++++++++++--- ffplayout-engine/Cargo.toml | 16 +++---- 8 files changed, 94 insertions(+), 19 deletions(-) delete mode 100644 debian/.gitkeep create mode 100644 debian/postinst diff --git a/Cargo.lock b/Cargo.lock index 54dbe311..eb9b2aca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1027,7 +1027,7 @@ dependencies = [ [[package]] name = "ffplayout-api" -version = "0.3.1" +version = "0.3.2" dependencies = [ "actix-multipart", "actix-web", @@ -1045,6 +1045,7 @@ dependencies = [ "rand 0.8.5", "relative-path", "reqwest", + "rpassword", "sanitize-filename", "serde", "serde_json", @@ -2454,6 +2455,18 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "rpassword" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bf099a1888612545b683d2661a1940089f6c2e5a8e38979b2159da876bfd956" +dependencies = [ + "libc", + "serde", + "serde_json", + "winapi 0.3.9", +] + [[package]] name = "rustc_version" version = "0.4.0" diff --git a/debian/.gitkeep b/debian/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/debian/postinst b/debian/postinst new file mode 100644 index 00000000..b4bc4c02 --- /dev/null +++ b/debian/postinst @@ -0,0 +1,10 @@ +#DEBHELPER# + +if [ ! -d "/usr/share/ffplayout/db" ]; then + mkdir "/usr/share/ffplayout/db" + chmod 777 "/usr/share/ffplayout/db" + + /usr/bin/ffpapi -i + + chown www-data. "/usr/share/ffplayout/db/ffplayout.db" +fi diff --git a/ffplayout-api/Cargo.toml b/ffplayout-api/Cargo.toml index 452d4efd..a1288475 100644 --- a/ffplayout-api/Cargo.toml +++ b/ffplayout-api/Cargo.toml @@ -4,7 +4,7 @@ description = "Rest API for ffplayout" license = "GPL-3.0" authors = ["Jonathan Baecker jonbae77@gmail.com"] readme = "README.md" -version = "0.3.1" +version = "0.3.2" edition = "2021" [dependencies] @@ -24,6 +24,7 @@ once_cell = "1.10" rand = "0.8" relative-path = "1.6" reqwest = { version = "0.11", features = ["blocking", "json"] } +rpassword = "6.0" sanitize-filename = "0.3" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/ffplayout-api/README.md b/ffplayout-api/README.md index 48c7175a..546256a6 100644 --- a/ffplayout-api/README.md +++ b/ffplayout-api/README.md @@ -21,4 +21,6 @@ Then run the API thru the systemd service, or like: ffpapi -l 127.0.0.1:8080 ``` +If you plan to run ffpapi with systemd set permission from **/usr/share/ffplayout** and content to user **www-data:www-data**. + **For possible endpoints read: [api endpoints](/docs/api.md)** diff --git a/ffplayout-api/src/utils/args_parse.rs b/ffplayout-api/src/utils/args_parse.rs index 84d8efa5..2fe65ab4 100644 --- a/ffplayout-api/src/utils/args_parse.rs +++ b/ffplayout-api/src/utils/args_parse.rs @@ -5,6 +5,9 @@ use clap::Parser; about = "REST API for ffplayout", long_about = None)] pub struct Args { + #[clap(short, long, help = "ask for user credentials")] + pub ask: bool, + #[clap(short, long, help = "Listen on IP:PORT, like: 127.0.0.1:8080")] pub listen: Option, diff --git a/ffplayout-api/src/utils/mod.rs b/ffplayout-api/src/utils/mod.rs index b3561173..1fe1eaa3 100644 --- a/ffplayout-api/src/utils/mod.rs +++ b/ffplayout-api/src/utils/mod.rs @@ -1,7 +1,13 @@ -use std::{error::Error, fs::File, path::Path}; +use std::{ + error::Error, + fs::File, + io::{stdin, stdout, Write}, + path::Path, +}; use faccess::PathExt; use once_cell::sync::OnceCell; +use rpassword::read_password; use simplelog::*; pub mod args_parse; @@ -69,20 +75,24 @@ pub async fn init_config() { } pub fn db_path() -> Result> { - let sys_path = Path::new("/usr/share/ffplayout"); - let mut db_path = String::from("./ffplayout.db"); + let sys_path = Path::new("/usr/share/ffplayout/db"); + let mut db_path = "./ffplayout.db".to_string(); + + if sys_path.is_dir() && !sys_path.writable() { + error!("Path {} is not writable!", sys_path.display()); + } if sys_path.is_dir() && sys_path.writable() { - db_path = String::from("/usr/share/ffplayout/ffplayout.db"); + db_path = "/usr/share/ffplayout/db/ffplayout.db".to_string(); } else if Path::new("./assets").is_dir() { - db_path = String::from("./assets/ffplayout.db"); + db_path = "./assets/ffplayout.db".to_string(); } Ok(db_path) } -pub async fn run_args(args: Args) -> Result<(), i32> { - if !args.init && args.listen.is_none() && args.username.is_none() { +pub async fn run_args(mut args: Args) -> Result<(), i32> { + if !args.init && args.listen.is_none() && !args.ask && args.username.is_none() { error!("Wrong number of arguments! Run ffpapi --help for more information."); return Err(0); @@ -96,6 +106,46 @@ pub async fn run_args(args: Args) -> Result<(), i32> { return Err(0); } + if args.ask { + let mut user = String::new(); + print!("Username: "); + stdout().flush().unwrap(); + + stdin() + .read_line(&mut user) + .expect("Did not enter a correct name?"); + if let Some('\n') = user.chars().next_back() { + user.pop(); + } + if let Some('\r') = user.chars().next_back() { + user.pop(); + } + + args.username = Some(user); + + print!("Password: "); + stdout().flush().unwrap(); + let password = read_password(); + + args.password = password.ok(); + + let mut email = String::new(); + print!("EMail: "); + stdout().flush().unwrap(); + + stdin() + .read_line(&mut email) + .expect("Did not enter a correct name?"); + if let Some('\n') = email.chars().next_back() { + email.pop(); + } + if let Some('\r') = email.chars().next_back() { + email.pop(); + } + + args.email = Some(email); + } + if let Some(username) = args.username { if args.email.is_none() || args.password.is_none() { error!("Email/password missing!"); diff --git a/ffplayout-engine/Cargo.toml b/ffplayout-engine/Cargo.toml index 4c4ddcc1..313763ae 100644 --- a/ffplayout-engine/Cargo.toml +++ b/ffplayout-engine/Cargo.toml @@ -41,20 +41,16 @@ suggests = "ffmpeg" copyright = "Copyright (c) 2022, Jonathan Baecker. All rights reserved." conf-files = ["/etc/ffplayout/ffplayout.yml"] assets = [ - [ - "../target/x86_64-unknown-linux-musl/release/ffpapi", - "/usr/bin/ffpapi", - "755" - ], + ["../target/x86_64-unknown-linux-musl/release/ffpapi", "/usr/bin/", "755"], [ "../target/x86_64-unknown-linux-musl/release/ffplayout", - "/usr/bin/ffplayout", + "/usr/bin/", "755" ], - ["../assets/ffpapi.service", "/lib/systemd/system/ffpapi.service", "644"], - ["../assets/11-ffplayout", "/etc/sudoers.d/11-ffplayout", "644"], - ["../assets/ffplayout.yml", "/etc/ffplayout/ffplayout.yml", "644"], - ["../assets/logo.png", "/usr/share/ffplayout/logo.png", "644"], + ["../assets/ffpapi.service", "/lib/systemd/system/", "644"], + ["../assets/11-ffplayout", "/etc/sudoers.d/", "644"], + ["../assets/ffplayout.yml", "/etc/ffplayout/", "644"], + ["../assets/logo.png", "/usr/share/ffplayout/", "644"], ["../README.md", "/usr/share/doc/ffplayout/README", "644"], ] maintainer-scripts = "../debian/"