Skip to content
Snippets Groups Projects
Commit d1ea00b0 authored by Sebastien Boutelier's avatar Sebastien Boutelier
Browse files

Le programme n'envoie plus directement au loghost mais renvoie les log au...

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
parent cc2a90d0
No related branches found
No related tags found
No related merge requests found
...@@ -6,16 +6,17 @@ ...@@ -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 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 local4.* @@127.0.0.1:8080
:programname, isequal, "slapd" stop :programname, isequal, "slapd" stop
``` ```
......
...@@ -9,7 +9,6 @@ use gethostname::gethostname; ...@@ -9,7 +9,6 @@ use gethostname::gethostname;
mod hashmap; mod hashmap;
static LOGHOST: OnceLock<String> = OnceLock::new();
static HOSTNAME: OnceLock<Option<String>> = OnceLock::new(); static HOSTNAME: OnceLock<Option<String>> = OnceLock::new();
static REGEXP_CLOSE: OnceLock<Regex> = OnceLock::new(); static REGEXP_CLOSE: OnceLock<Regex> = OnceLock::new();
static REGEXP_CMD: OnceLock<Regex> = OnceLock::new(); static REGEXP_CMD: OnceLock<Regex> = OnceLock::new();
...@@ -19,9 +18,8 @@ static REGEXP_OTHERLOG: OnceLock<Regex> = OnceLock::new(); ...@@ -19,9 +18,8 @@ static REGEXP_OTHERLOG: OnceLock<Regex> = OnceLock::new();
static REGEXP_RESULT: OnceLock<Regex> = OnceLock::new(); static REGEXP_RESULT: OnceLock<Regex> = OnceLock::new();
fn help() { 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!(" 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> { fn getport()->Result<String,String> {
...@@ -37,19 +35,6 @@ fn getport()->Result<String,String> { ...@@ -37,19 +35,6 @@ fn getport()->Result<String,String> {
process::exit(1); 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){ fn com_accept(line: String){
let Some(caps) = REGEXP_CONNECT.get().expect("unitialized Regexp").captures(&line) else { let Some(caps) = REGEXP_CONNECT.get().expect("unitialized Regexp").captures(&line) else {
return; return;
...@@ -112,7 +97,7 @@ fn com_result(line: String){ ...@@ -112,7 +97,7 @@ fn com_result(line: String){
} }
} }
hashmap::removeop(key.clone()); hashmap::removeop(key.clone());
sendlog(linelog); sendlog(linelog,false);
} }
fn create_log(line: String){ fn create_log(line: String){
...@@ -155,27 +140,8 @@ fn com_default(line: String){ ...@@ -155,27 +140,8 @@ fn com_default(line: String){
} }
fn sendlog(line: String){ fn sendlog(line: String,otherlog: bool){
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 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 { let formatter = Formatter3164 {
facility: Facility::LOG_LOCAL5, facility: Facility::LOG_LOCAL5,
hostname: None, hostname: None,
...@@ -186,7 +152,13 @@ fn sendotherlog(line: String){ ...@@ -186,7 +152,13 @@ fn sendotherlog(line: String){
match syslog::unix(formatter) { match syslog::unix(formatter) {
Err(e) => println!("impossible to connect to syslog: {:?}", e), Err(e) => println!("impossible to connect to syslog: {:?}", e),
Ok(mut writer) => { 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) { ...@@ -196,7 +168,7 @@ fn launch<R: BufRead>(reader: R) {
let line = l.unwrap(); let line = l.unwrap();
//println!("LINE: {}",line); //println!("LINE: {}",line);
let Some(caps) = REGEXP_LAUNCH.get().expect("unitialized Regexp").captures(&line) else { let Some(caps) = REGEXP_LAUNCH.get().expect("unitialized Regexp").captures(&line) else {
sendotherlog(line.to_string()); sendlog(line.to_string(),true);
return; return;
}; };
match &caps["command"]{ match &caps["command"]{
...@@ -215,14 +187,12 @@ fn launch<R: BufRead>(reader: R) { ...@@ -215,14 +187,12 @@ fn launch<R: BufRead>(reader: R) {
"SRCH" =>create_log(line.to_string()), "SRCH" =>create_log(line.to_string()),
"TLS" =>{}, "TLS" =>{},
"UNBIND" =>create_log(line.to_string()), "UNBIND" =>create_log(line.to_string()),
_ =>sendotherlog(line.to_string()), _ =>sendlog(line.to_string(),true),
} }
} }
} }
fn main() { fn main() {
let loghost = getloghost().unwrap();
let _ = LOGHOST.set(loghost);
let port = getport().unwrap(); let port = getport().unwrap();
let hostname = Some(gethostname().into_string().unwrap()); let hostname = Some(gethostname().into_string().unwrap());
let _ = HOSTNAME.set(hostname); let _ = HOSTNAME.set(hostname);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment