diff --git a/config.toml.example b/config.toml.example index 16037b1..c581b34 100644 --- a/config.toml.example +++ b/config.toml.example @@ -1,11 +1,11 @@ #General settings hostname = "mein-iserv.de" -session_cookie = "IServSAT=mvuvFvCZSlpvuwhk0mmNwF1NKEQypM4d; IServSession=xEPqyJO1a5nsVd34HEpbPqQGMMQKapvw" +cookie = "IServSAT=mvuvFvCZSlpvuwhk0mmNwF1NKEQypM4d; IServSession=xEPqyJO1a5nsVd34HEpbPqQGMMQKapvw" # Targeted device settings -target_ip = "10.1.1.1" -target_room = 123 -target_compilation = 1 +host_ip = "10.1.1.1" +host_room = 123 +mdm_compilation = 1 [host_filter] diff --git a/src/main.rs b/src/main.rs index f4e7e53..79b04d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,7 +87,7 @@ args_or_config! { |optional: #[clap(long, help("Numerical compilation id the newly added device should be assigned to. HINT: The id can be found at the end of the url when inspecting a compilation in the mdm admin section. If this is not specified, the new device will not be added to a collection."))] - target_compilation: u16 + mdm_compilation: u16 } macro_rules! print_banner { @@ -115,12 +115,23 @@ fn main_loop(args: &Config) -> Result<(), Box> { .send()?; let status = &resp.status(); let plain = &resp.text()?; + + if plain.contains("class=\"login-form\"") { + eprintln!("ERR: Failed to authorize. Server forwarded to login site. HINT: Check the provided cookie."); + std::process::exit(187); + } + + if status.is_client_error() || status.is_server_error() { + eprintln!("ERR: Received error status (status code: {:?})", status); + std::process::exit(187); + } + let content = squeeze( &plain, "", ); - let json: serde_json::Value = serde_json::from_str(content).expect("Failed to parse JSON data. This may be because the server did not return valid JSON data to parse."); + let json: serde_json::Value = serde_json::from_str(content).expect("ERR: Failed to parse JSON data. This may be because the server did not return valid JSON data to parse."); let json_part = &json["data"][0]["name"]["rendered"] .to_string() .replace("\\", ""); @@ -135,46 +146,59 @@ No new host: skipping (Retry in {}s) std::thread::sleep(std::time::Duration::from_secs(5)); return Ok(()); } + let host_id = squeeze(json_part, "data-host-id=\"", "\""); let device_name = squeeze(json_part, "", ""); print_banner!("1/8 Found host\nHost id: {}\nDevice name: {}\nReceived response status: {:?}", host_id, device_name, status); let dest = format!("https://{}/iserv/admin/host/edit/{}", args.hostname, &host_id); let resp = client.get(&dest).header(header.0, header.1).send()?; - //TODO Check for non 200 response let status = &resp.status(); - if !status.is_success() && *status != 300 { - eprintln!("WARN: Found invalid Status Code: {status}"); + if !status.is_success() { + eprintln!("{}, {}", "WARN: Found invalid Status Code: ".bright_yellow(), status); return Ok(()) } let plain = &resp.text()?; - std::fs::write("lol.out.html", plain)?; let mut form_ip: &str = &args.host_ip; - let form_name = squeeze_html_form(plain, "name=\"host[name]\"", "value=\"", "\""); - let form_tags = squeeze_html_form(plain, "name=\"host[tags][]\"", "value=\"", "\""); - let form_mac = squeeze_html_form(plain, "name=\"host[mac]\"", "value=\"", "\""); - let form_description = squeeze_html_form(plain, "name=\"host[description]\"", ">", "<"); - let form_token = squeeze_html_form(plain, "name=\"host[_token]\"", "value=\"", "\""); - let mut form_data = [ - ("host[name]", form_name), - ("host[tags][]", form_tags), - ("host[room]", &args.host_room.to_string()), - ("host[ip]", &form_ip), - ("host[mac]", form_mac), - ("host[internet]", "1"), - ("host[proxyEnforce]", "0"), - ("host[owner]", ""), - ("host[controllable]", "0"), - ("host[inventoryNumber]", ""), - ("host[shutdown]", ""), - ("host[description]", form_description), - ("host[image][id]", ""), - ("host[image][x]", ""), - ("host[image][y]", ""), - ("host[actions][submit]", ""), - ("host[_token]", form_token), - ]; - print_banner!("2/8 Received current host properties\nReceived response status: {:?}", status); + let mut form_ip_index = 2; + let form_name = squeeze_html_form(plain, "name=\"host[name]\"", ">", "value=\"", "\""); + let plain_inner_tags = squeeze(plain, "name=\"host[tags][]\"", ""); + let mut form_tags = Vec::new(); + squeeze_loop(&mut form_tags, plain_inner_tags, "