diff --git a/src/main.rs b/src/main.rs index 71f975a..85498c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use std::fmt::{Display, Formatter, Write}; use std::{fmt, fs}; use std::fs::{read_to_string, ReadDir}; use std::path::PathBuf; -use crate::socket::socket::{Id, Tcp_Socket}; +use crate::socket::socket::{pids_to_string, Id, Tcp_Socket}; type inode = u64; @@ -112,37 +112,37 @@ pub fn format_output(combination : socket::socket::Combination, sockets : &Vec max_loc_ip_len) { curr_loc_ip_len } else { max_loc_ip_len }; max_loc_port_len = if (curr_loc_port_len > max_loc_port_len) { curr_loc_port_len } else { max_loc_port_len }; max_rem_ip_len = if (curr_rem_ip_len > max_rem_ip_len) { curr_rem_ip_len } else { max_rem_ip_len }; @@ -150,21 +150,35 @@ pub fn format_output(combination : socket::socket::Combination, sockets : &Vec max_state_len) { curr_state_len } else { max_state_len }; if combination == socket::socket::Combination::tnap{ - // max_real_uid_len = if (curr_uid_) + max_real_uid_len = if (curr_real_uid_len > max_real_uid_len) { curr_real_uid_len } else { max_real_uid_len }; + max_eff_uid_len = if (curr_eff_uid_len > max_eff_uid_len) { curr_eff_uid_len } else { max_eff_uid_len }; + max_real_gid_len = if (curr_real_gid_len > max_real_gid_len) { curr_real_gid_len } else { max_real_gid_len }; + max_eff_gid_len = if (curr_eff_gid_len > max_eff_gid_len) { curr_eff_gid_len } else { max_eff_gid_len }; + max_pids_len = if (curr_pids_len > max_pids_len) { curr_pids_len } else { max_pids_len }; } }); + max_loc_ip_len = max_loc_ip_len + 3; - max_loc_port_len = max_loc_port_len + 3; max_rem_ip_len = max_rem_ip_len + 3; + max_real_uid_len = max_real_uid_len + 3; + max_real_gid_len = max_real_gid_len + 3; + max_pids_len = max_pids_len + 3; + match combination { socket::socket::Combination::tna => { - ret_str.push_str(&format!("{:>max_state_len$} {:>max_loc_ip_len$}:{:max_rem_ip_len$}:{:max_state_len$} {:>max_loc_ip_len$}:{:max_rem_ip_len$}:{:max_state_len$} {:>max_loc_ip_len$}:{:max_rem_ip_len$}:{: { + ret_str.push_str(&format!("{:>max_state_len$} {:>max_loc_ip_len$}:{:max_rem_ip_len$}:{:max_real_uid_len$}|{:max_real_gid_len$}|{:max_pids_len$}\n", header_state , header_loc_ip , header_loc_port, header_rem_ip,header_rem_port, header_uid_real, header_uid_eff, header_gid_real, header_gid_eff, header_pids )); + sockets.clone().iter().for_each(|x| { + let (real_uid, eff_uid) = x.get_real_eff_uid_string(); + let (real_gid,eff_gid) = x.get_real_eff_gid_String(); + ret_str.push_str(&format!("{:>max_state_len$} {:>max_loc_ip_len$}:{:max_rem_ip_len$}:{:max_real_uid_len$}|{:max_real_gid_len$}|{:max_pids_len$}\n", x.get_state().to_string(), x.get_loc_ip_string(), x.get_loc_port_string(), x.get_rem_ip_string(), x.get_rem_port_string(), real_uid, eff_uid , real_gid , eff_gid, pids_to_string(&x.pids))) + }); } } ret_str @@ -212,5 +226,5 @@ fn main() { let socket_patt : Regex = Regex::new(r"socket:\[([0-9]+)\]").expect("kannst kein regex?? xd"); let pid_pattern = Regex::new(r"^[1-4]?[0-9]{1,6}$").expect("kannst kein regex??"); let sockets = get_all_tcp_sockets(&pid_pattern, &socket_patt); - println!("{}", format_output(socket::socket::Combination::tna, &sockets)); + println!("{}", format_output(socket::socket::Combination::tnap, &sockets)); } diff --git a/src/socket.rs b/src/socket.rs index 0cb78ba..e456766 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -1,33 +1,28 @@ - pub mod socket { - use std::cmp::PartialEq; + use crate::pids; use regex::Regex; + use std::cmp::PartialEq; use std::collections::HashSet; use std::fmt::Display; use std::fs; use std::hash::{Hash, Hasher}; use std::os::unix::fs::MetadataExt; - use crate::pids; - #[derive(PartialEq)] - #[derive(Clone)] - #[derive(Debug)] + #[derive(PartialEq, Clone, Debug)] pub struct Peer { pub ip_address: String, pub port: usize, } - #[derive(PartialEq, Copy)] - #[derive(Clone)] - #[derive(Debug)] + #[derive(PartialEq, Copy, Clone, Debug)] pub struct Id { - pub real : usize, - pub effective : usize, - pub saved_set : usize, - pub fs : usize, + pub real: usize, + pub effective: usize, + pub saved_set: usize, + pub fs: usize, } impl Id { - pub fn new(real : usize, effective : usize, saved_set : usize, fs : usize) -> Id { + pub fn new(real: usize, effective: usize, saved_set: usize, fs: usize) -> Id { Id { real, effective, @@ -36,7 +31,7 @@ pub mod socket { } } pub fn create_def() -> Id { - Id::new(0,0,0,0) + Id::new(0, 0, 0, 0) } } @@ -48,7 +43,7 @@ pub mod socket { } } - pub fn build_from_hex(hex_peer : &str) -> Peer { + pub fn build_from_hex(hex_peer: &str) -> Peer { let mut split = hex_peer.split(":"); let hex_ip = split.nth(0).unwrap(); let hex_port = split.nth(0).unwrap(); @@ -90,15 +85,16 @@ pub mod socket { self.clone().ip_address } - pub fn get_port_string(&self) -> String{ - if self.port == 0 { String::from("*")} else {self.port.to_string()} + pub fn get_port_string(&self) -> String { + if self.port == 0 { + String::from("*") + } else { + self.port.to_string() + } } - } - #[derive(PartialEq)] - #[derive(Clone)] - #[derive(Debug)] + #[derive(PartialEq, Clone, Debug)] pub enum Tcp_SocketState { Established, SYN_Sent, @@ -118,7 +114,7 @@ pub mod socket { } impl Tcp_SocketState { - pub fn new(index : usize) -> Tcp_SocketState { + pub fn new(index: usize) -> Tcp_SocketState { match index { 1 => Tcp_SocketState::Established, 2 => Tcp_SocketState::SYN_Sent, @@ -159,13 +155,16 @@ pub mod socket { } } - pub fn pids_to_string(pids : pids) -> String { + pub fn pids_to_string(pids: &HashSet) -> String { let mut ret_str = String::from(""); pids.iter().for_each(|x| { ret_str.push_str(&x.to_string()); ret_str.push_str(", "); }); - ret_str.drain(ret_str.len()-2..ret_str.len()); + let ret_str_len = ret_str.len(); + if (ret_str_len > 2) { + ret_str.drain(ret_str.len() - 2..ret_str.len()); + } ret_str } @@ -178,8 +177,8 @@ pub mod socket { impl Eq for Tcp_Socket {} impl Hash for Tcp_Socket { - fn hash(&self, state: &mut H) -> (){ - let mut state = self.inode * 31; + fn hash(&self, state: &mut H) -> () { + let mut state = self.inode * 31; state = state + 11; } } @@ -189,20 +188,28 @@ pub mod socket { peer_loc: Peer, peer_rem: Peer, pub pids: HashSet, - state : Tcp_SocketState, - user : Id, - group : Id, - pub inode : u64 + state: Tcp_SocketState, + user: Id, + group: Id, + pub inode: u64, } - #[derive(Clone,Copy, PartialEq)] - pub enum Combination{ + #[derive(Clone, Copy, PartialEq)] + pub enum Combination { tnap, tna, } impl Tcp_Socket { - pub fn new(peer_loc : Peer, peer_rem : Peer, pids : HashSet, state : Tcp_SocketState, user : Id, group : Id, inode : u64) -> Tcp_Socket { + pub fn new( + peer_loc: Peer, + peer_rem: Peer, + pids: HashSet, + state: Tcp_SocketState, + user: Id, + group: Id, + inode: u64, + ) -> Tcp_Socket { Tcp_Socket { peer_loc, peer_rem, @@ -219,48 +226,56 @@ pub mod socket { path.push_str(&pid.to_string()); path.push_str("/net/tcp"); - let inode = fs::metadata(&path) - .expect("Cannot read metadata") - .ino(); + let inode = fs::metadata(&path).expect("Cannot read metadata").ino(); inode } - pub fn add_pid(socket : &mut Tcp_Socket, pid : u64) -> () { + pub fn add_pid(socket: &mut Tcp_Socket, pid: u64) -> () { socket.pids.insert(pid); } - pub fn add_uid_gid(socket : &mut Tcp_Socket, uid : Id, gid : Id) -> (){ + pub fn add_uid_gid(socket: &mut Tcp_Socket, uid: Id, gid: Id) -> () { socket.user = uid; socket.group = gid; } - pub fn check_inode_seen(seen_inodes : &HashSet, pid : u64) -> bool { + pub fn check_inode_seen(seen_inodes: &HashSet, pid: u64) -> bool { let inode = Tcp_Socket::get_tcp_file_inode(pid); seen_inodes.contains(&inode) } - pub fn add_inode(seen_inodes : &mut HashSet, pid: u64){ - let inode = Tcp_Socket::get_tcp_file_inode(pid); - seen_inodes.insert(inode); + pub fn add_inode(seen_inodes: &mut HashSet, pid: u64) { + let inode = Tcp_Socket::get_tcp_file_inode(pid); + seen_inodes.insert(inode); } - pub fn get_loc_ip_string(&self) -> String{ + pub fn get_loc_ip_string(&self) -> String { self.peer_loc.get_ip_string() } - pub fn get_loc_port_string(&self) -> String{ + pub fn get_loc_port_string(&self) -> String { self.peer_loc.get_port_string() } - pub fn get_rem_ip_string(&self) -> String{ + pub fn get_rem_ip_string(&self) -> String { self.peer_rem.get_ip_string() } - pub fn get_rem_port_string(&self) -> String{ + pub fn get_rem_port_string(&self) -> String { self.peer_rem.get_port_string() } - pub fn to_string (&self, combination: Combination) -> String { + pub fn get_real_eff_uid_string(&self) -> (String,String) { + (self.user.real.to_string(),self.user.effective.to_string()) + } + + pub fn get_real_eff_gid_String(&self) -> (String,String) { + (self.group.real.to_string(),self.group.effective.to_string()) + } + + + + pub fn to_string(&self, combination: Combination) -> String { let mut ret_str = String::from(""); match combination { Combination::tnap => { @@ -274,9 +289,8 @@ pub mod socket { ret_str.push_str(&self.group.real.to_string()); ret_str.push('|'); ret_str.push_str(&self.group.effective.to_string()); - ret_str.push_str(&pids_to_string(self.clone().pids)); + ret_str.push_str(&pids_to_string(&self.clone().pids)); ret_str - } Combination::tna => { //state rem_ip:rem_port loc_ip:loc_port @@ -285,27 +299,34 @@ pub mod socket { ret_str.push_str(&self.peer_loc.ip_address); ret_str.push(':'); let loc_port_str = self.peer_loc.port.clone().to_string(); - ret_str.push_str(if self.peer_loc.port == 0 { "*" } else { &loc_port_str }); + ret_str.push_str(if self.peer_loc.port == 0 { + "*" + } else { + &loc_port_str + }); ret_str.push(' '); ret_str.push_str(&self.peer_rem.ip_address); ret_str.push(':'); let rem_port_str = self.peer_rem.port.clone().to_string(); - ret_str.push_str(if self.peer_rem.port == 0 { "*" } else { &rem_port_str }); + ret_str.push_str(if self.peer_rem.port == 0 { + "*" + } else { + &rem_port_str + }); ret_str.push('\n'); ret_str } } - } - pub fn get_state(&self) -> Tcp_SocketState{ + pub fn get_state(&self) -> Tcp_SocketState { self.state.clone() } fn get_all_unique_socket_infos() -> String { let pattern = Regex::new(r"^[1-4]?[0-9]{1,6}$").expect("kannst kein regex??"); - let contents = - fs::read_dir("/proc/").expect("upsi 4 (proc kann nicht gelesen werden, Berechtigung?)"); + let contents = fs::read_dir("/proc/") + .expect("upsi 4 (proc kann nicht gelesen werden, Berechtigung?)"); let mut seen_inodes = HashSet::::new(); let mut complete_file: String = String::new(); for dir in contents { @@ -321,62 +342,88 @@ pub mod socket { let mut path = dir.path().into_os_string().into_string().expect("gehtnd"); path.push_str("/net/tcp"); let pid = &path.split("/").nth(2).expect("gesplittert"); - if !Tcp_Socket::check_inode_seen(&seen_inodes, pid.parse::().unwrap()) { - Tcp_Socket::add_inode(& mut seen_inodes, pid.parse::().unwrap()); + if !Tcp_Socket::check_inode_seen(&seen_inodes, pid.parse::().unwrap()) + { + Tcp_Socket::add_inode(&mut seen_inodes, pid.parse::().unwrap()); let test = &fs::read_to_string(path) .expect("Could not read proc/pid/net/tcp") .lines() .skip(1) - // .map(|x| socket::convert_line(x.to_string())) + // .map(|x| socket::convert_line(x.to_string())) .map(|x| x.to_owned()) .collect::>() .join("\n"); complete_file.push_str(test); - } - else{ + } else { continue; } - }, + } Err(_) => continue, - }; + }; } - // complete_file - complete_file + // complete_file + complete_file } - } } #[cfg(test)] mod tests { - use crate::socket::socket::Combination; use super::*; + use crate::socket::socket::Combination; fn init_sockets() -> Vec { let mut sockets = Vec::::new(); - let root_uid = socket::Id::new(0,0,0,0); + let root_uid = socket::Id::new(0, 0, 0, 0); let root_gid = root_uid.clone(); - let mux_uid = socket::Id::new(1000,1000,1000,1000); + let mux_uid = socket::Id::new(1000, 1000, 1000, 1000); let mux_gid = mux_uid.clone(); - let sudo_mux_uid = socket::Id::new(1000,0,0,0); + let sudo_mux_uid = socket::Id::new(1000, 0, 0, 0); let sudo_mux_gid = root_gid.clone(); - // sockets.push(socket::Socket::new("93.83.160.12", 21345, "0.0.0.0", 0, Vec::::new(), socket::SocketState::Listening, root_uid, root_gid, 123123)); - // sockets.push(socket::Socket::new("195.201.90.97", 8843, "0.0.0.0", 443, Vec::::new(), socket::SocketState::Established, mux_uid, mux_gid, 218389)); - // sockets.push(socket::Socket::new("255.255.255.255", 193193, "0.0.0.0", 8080, Vec::::new(), socket::SocketState::Listening, sudo_mux_uid, sudo_mux_gid, 1301011)); + // sockets.push(socket::Socket::new("93.83.160.12", 21345, "0.0.0.0", 0, Vec::::new(), socket::SocketState::Listening, root_uid, root_gid, 123123)); + // sockets.push(socket::Socket::new("195.201.90.97", 8843, "0.0.0.0", 443, Vec::::new(), socket::SocketState::Established, mux_uid, mux_gid, 218389)); + // sockets.push(socket::Socket::new("255.255.255.255", 193193, "0.0.0.0", 8080, Vec::::new(), socket::SocketState::Listening, sudo_mux_uid, sudo_mux_gid, 1301011)); sockets } #[test] fn test_build_from_hex() -> () { let hex_val = "0100007F"; - assert_eq!(socket::Peer::build_from_hex("0100007F:F168"), socket::Peer { ip_address: "127.0.0.1".to_owned(), port: 61800 }); - assert_eq!(socket::Peer::build_from_hex("980FB23E:ACCE"), socket::Peer { ip_address: "62.178.15.152".to_owned(), port: 44238 }); - assert_eq!(socket::Peer::build_from_hex("00000000:30C8"), socket::Peer { ip_address: "0.0.0.0".to_owned(), port: 12488 }); + assert_eq!( + socket::Peer::build_from_hex("0100007F:F168"), + socket::Peer { + ip_address: "127.0.0.1".to_owned(), + port: 61800 + } + ); + assert_eq!( + socket::Peer::build_from_hex("980FB23E:ACCE"), + socket::Peer { + ip_address: "62.178.15.152".to_owned(), + port: 44238 + } + ); + assert_eq!( + socket::Peer::build_from_hex("00000000:30C8"), + socket::Peer { + ip_address: "0.0.0.0".to_owned(), + port: 12488 + } + ); } #[test] fn test_to_string() -> () { let sockets = init_sockets(); - assert_eq!("93.83.160.12:21345 0.0.0.0:*\n", sockets[0].clone().to_string(Combination::tna)); - assert_eq!("195.201.90.97:8843 0.0.0.0:443\n", sockets[1].clone().to_string(Combination::tna)); - assert_eq!("255.255.255.255:193193 0.0.0.0:8080\n", sockets[2].clone().to_string(Combination::tna)); + assert_eq!( + "93.83.160.12:21345 0.0.0.0:*\n", + sockets[0].clone().to_string(Combination::tna) + ); + assert_eq!( + "195.201.90.97:8843 0.0.0.0:443\n", + sockets[1].clone().to_string(Combination::tna) + ); + assert_eq!( + "255.255.255.255:193193 0.0.0.0:8080\n", + sockets[2].clone().to_string(Combination::tna) + ); } }