From d1ea00b0ee69dfb19c1fc49948bec667479efd50 Mon Sep 17 00:00:00 2001 From: BOUTELIER Sebastien <sebastien.boutelier@lis-lab.fr> Date: Tue, 31 Oct 2023 13:00:50 +0100 Subject: [PATCH] Le programme n'envoie plus directement au loghost mais renvoie les log au rsyslog local qui en fera ce qu'il veut. On retire donc l'option --loghost de la ligne de commande --- README.md | 9 +++++---- parselog.rs | 54 ++++++++++++----------------------------------------- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index b2b125c..d81bb84 100644 --- a/README.md +++ b/README.md @@ -6,16 +6,17 @@ Le programme parselog-ldap permet de construire des logs LDAP lisibles et exploitables. Il doit se lancer sur le serveur LDAP. Il ne peut pas recevoir les logs de plusieurs serveurs LDAP car il pourrait y avoir plusieurs fois le même numéro de connexion et donc des logs inconsistants. -Le programme prend en entrée un port sur lequel il va écouter en TCP et un couple IP/Port sur lequel il va envoyer en TCP les logs construits et sur local5 les logs qui ne sont pas des opérations. Pour cela, il faudra passer en argument du programme ces paramètres: +Le programme prend en entrée un port sur lequel il va écouter en TCP sur 127.0.0.1 et va renvoyer les logs construits sur local5.info les logs qui ne sont pas des opérations sur local5.debug. Il faudra passer en argument du programme le port d'écoute TCP: ``` -parselog-ldap --listen=8080 --loghost=172.26.66.74:4514 +parselog-ldap --listen=8080 ``` -Il faudra ensuite indiquer au rsyslog local de traiter les logs envoyés sur local5 et d'envoyer ceux du serveur LDAP sur le démon, c'est à dire s'il écoute sur le port 8080 d'envoyer en TCP sur 127.0.0.1:8080. Avec rsyslog, exemple d'un fichier /etc/rsyslog.d/ldap.conf: +Il faudra ensuite indiquer au rsyslog local d'envoyer les log LDAP sur le port d'écoute du démon et traiter les logs envoyés sur local5.info et local5.debug. Avec rsyslog, exemple d'un fichier /etc/rsyslog.d/ldap.conf: ``` -local5.* -/var/log/ldap/ldap.log +local5.debug;local5.!info -/var/log/ldap/ldap.log +local5.info @@LOGHOST:PORT local4.* @@127.0.0.1:8080 :programname, isequal, "slapd" stop ``` diff --git a/parselog.rs b/parselog.rs index 88d2c59..7498b1c 100644 --- a/parselog.rs +++ b/parselog.rs @@ -9,7 +9,6 @@ use gethostname::gethostname; mod hashmap; -static LOGHOST: OnceLock<String> = OnceLock::new(); static HOSTNAME: OnceLock<Option<String>> = OnceLock::new(); static REGEXP_CLOSE: OnceLock<Regex> = OnceLock::new(); static REGEXP_CMD: OnceLock<Regex> = OnceLock::new(); @@ -19,9 +18,8 @@ static REGEXP_OTHERLOG: OnceLock<Regex> = OnceLock::new(); static REGEXP_RESULT: OnceLock<Regex> = OnceLock::new(); fn help() { - println!("Usage: parselog-ldap --listen=NUMBER --loghost=IP:PORT"); + println!("Usage: parselog-ldap --listen=NUMBER"); println!(" NUMBER: Le port sur lequel on reçoit les logs."); - println!(" IP:PORT: La socket sur laquelle on envoie les logs."); } fn getport()->Result<String,String> { @@ -37,19 +35,6 @@ fn getport()->Result<String,String> { process::exit(1); } -fn getloghost()->Result<String,String> { - let regexp_arg = Regex::new(r"--loghost=(?<ipport>[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5})").unwrap(); - for argument in env::args() { - let Some(caps) = regexp_arg.captures(&argument) else { - continue; - }; - return Ok(caps["ipport"].to_string()); - } - println!("Loghost invalide"); - help(); - process::exit(1); -} - fn com_accept(line: String){ let Some(caps) = REGEXP_CONNECT.get().expect("unitialized Regexp").captures(&line) else { return; @@ -112,7 +97,7 @@ fn com_result(line: String){ } } hashmap::removeop(key.clone()); - sendlog(linelog); + sendlog(linelog,false); } fn create_log(line: String){ @@ -155,27 +140,8 @@ fn com_default(line: String){ } -fn sendlog(line: String){ - let formatter = Formatter3164 { - facility: Facility::LOG_USER, - hostname: HOSTNAME.get().unwrap().clone(), - process: "slapd".into(), - pid: 6666, - }; - - match syslog::tcp(formatter,&LOGHOST.get().unwrap_or(&"0".to_string()).to_string()) { - Err(e) => println!("impossible to connect to syslog: {:?}", e), - Ok(mut writer) => { - writer.info(line).expect("could not write message"); - } - } -} +fn sendlog(line: String,otherlog: bool){ -fn sendotherlog(line: String){ - let Some(caps) = REGEXP_OTHERLOG.get().expect("unitialized Regexp").captures(&line) else { - return; - }; - let log = &caps["log"].to_string(); let formatter = Formatter3164 { facility: Facility::LOG_LOCAL5, hostname: None, @@ -186,7 +152,13 @@ fn sendotherlog(line: String){ match syslog::unix(formatter) { Err(e) => println!("impossible to connect to syslog: {:?}", e), Ok(mut writer) => { - writer.info(log).expect("could not write message"); + if otherlog { + let Some(caps) = REGEXP_OTHERLOG.get().expect("unitialized Regexp").captures(&line) else { return; }; + let log = &caps["log"].to_string(); + writer.debug(log).expect("could not write message"); + } else { + writer.info(line).expect("could not write message"); + } } } } @@ -196,7 +168,7 @@ fn launch<R: BufRead>(reader: R) { let line = l.unwrap(); //println!("LINE: {}",line); let Some(caps) = REGEXP_LAUNCH.get().expect("unitialized Regexp").captures(&line) else { - sendotherlog(line.to_string()); + sendlog(line.to_string(),true); return; }; match &caps["command"]{ @@ -215,14 +187,12 @@ fn launch<R: BufRead>(reader: R) { "SRCH" =>create_log(line.to_string()), "TLS" =>{}, "UNBIND" =>create_log(line.to_string()), - _ =>sendotherlog(line.to_string()), + _ =>sendlog(line.to_string(),true), } } } fn main() { - let loghost = getloghost().unwrap(); - let _ = LOGHOST.set(loghost); let port = getport().unwrap(); let hostname = Some(gethostname().into_string().unwrap()); let _ = HOSTNAME.set(hostname); -- GitLab