Skip to content

Commit b0ff2a2

Browse files
authored
Merge pull request #1 from sdslabs/logger
Completes core feature binaries
2 parents e88882a + 6ec008f commit b0ff2a2

19 files changed

+566
-50
lines changed

Cargo.lock

+122
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,21 @@ toml = "0.5.3"
1212
reqwest = "0.9.20"
1313
base64 = "0.10.1"
1414
rust-crypto = "0.2.36"
15+
simplelog = "^0.7.3"
16+
log = "^0.4.8"
1517

1618
[[bin]]
1719
name = "pam_sudo"
1820
path = "src/sudo/main.rs"
1921

22+
[[bin]]
23+
name = "pam_su"
24+
path = "src/su/main.rs"
25+
26+
[[bin]]
27+
name = "pam_ssh"
28+
path = "src/ssh/main.rs"
29+
2030
[[bin]]
2131
name = "auth_keys_cmd"
2232
path = "src/auth_keys_cmd/main.rs"

install.sh

-6
This file was deleted.

install/edit-sshd-config.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
watchdog_config = """
3+
# SDSLabs Watchdog configuration START
4+
5+
UsePAM yes
6+
PasswordAuthentication no
7+
AuthorizedKeysCommand /opt/watchdog/bin/auth_keys_cmd %u %h %t %f %k
8+
AuthorizedKeysCommandUser root
9+
10+
# SDSLabs Watchdog configuration END
11+
"""
12+
13+
14+
modified_keys = [
15+
'AuthorizedKeysCommand',
16+
'AuthorizedKeysCommandUser',
17+
'PasswordAuthentication',
18+
'UsePAM'
19+
]
20+
21+
inside_watchdog_config = False
22+
23+
def process_line(line):
24+
global inside_watchdog_config
25+
26+
if inside_watchdog_config and line == "# SDSLabs Watchdog configuration END\n":
27+
inside_watchdog_config = False
28+
return ''
29+
30+
if inside_watchdog_config:
31+
return ''
32+
33+
if line == "# SDSLabs Watchdog configuration START\n":
34+
inside_watchdog_config = True
35+
return ''
36+
37+
l = line.strip()
38+
i = l.find('#')
39+
if i != -1:
40+
l = l[:i]
41+
if len(l) == 0:
42+
return line
43+
i = l.find(' ')
44+
j = l.find('\t')
45+
if i == -1 and j != -1:
46+
i = j
47+
elif j == -1 and i != -1:
48+
pass
49+
elif j == -1 and i == -1:
50+
return line
51+
else:
52+
i = min(i, j)
53+
key = l[:i]
54+
value = l[i+1:].strip()
55+
if key in modified_keys:
56+
# comment this line
57+
return '# Watchdog: Commenting the line below out\n#' + line
58+
else:
59+
return line
60+
61+
def main():
62+
iput = open("/etc/ssh/sshd_config")
63+
oput = open("tmp_sshd_config", "w")
64+
lines = iput.readlines()
65+
for l in lines:
66+
oputline = process_line(l)
67+
oput.write(oputline)
68+
69+
oput.write(watchdog_config)
70+
71+
iput.close()
72+
oput.close()
73+
74+
75+
main()

install/install.sh

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
# Install all the files at right place
4+
mkdir -p /opt/watchdog/bin
5+
6+
cp ../target/debug/pam_ssh /opt/watchdog/bin/pam_ssh
7+
chown root /opt/watchdog/bin/pam_ssh
8+
chgrp root /opt/watchdog/bin/pam_ssh
9+
chmod 700 /opt/watchdog/bin/pam_ssh
10+
11+
cp ../target/debug/pam_su /opt/watchdog/bin/pam_su
12+
chown root /opt/watchdog/bin/pam_su
13+
chgrp root /opt/watchdog/bin/pam_su
14+
chmod 700 /opt/watchdog/bin/pam_su
15+
16+
cp ../target/debug/pam_sudo /opt/watchdog/bin/pam_sudo
17+
chown root /opt/watchdog/bin/pam_sudo
18+
chgrp root /opt/watchdog/bin/pam_sudo
19+
chmod 700 /opt/watchdog/bin/pam_sudo
20+
21+
cp ../target/debug/auth_keys_cmd /opt/watchdog/bin/auth_keys_cmd
22+
chown root /opt/watchdog/bin/auth_keys_cmd
23+
chgrp root /opt/watchdog/bin/auth_keys_cmd
24+
chmod 700 /opt/watchdog/bin/auth_keys_cmd
25+
26+
cp ../config.toml /opt/watchdog/config.toml
27+
28+
# edit `sshd_config` file
29+
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.watchdog.bak
30+
python3 edit-sshd-config.py
31+
cp tmp_sshd_config /etc/ssh/sshd_config
32+
rm tmp_sshd_config
33+
service sshd restart
34+
35+
# installing pam_exec lines
36+
python3 pam-install-sudo.py
37+
python3 pam-install-su.py
38+
python3 pam-install-ssh.py
39+
40+
cp tmp_sudo /etc/pam.d/sudo
41+
cp tmp_su /etc/pam.d/su
42+
cp tmp_ssh /etc/pam.d/sshd
43+
44+
rm tmp_sudo
45+
rm tmp_su
46+
rm tmp_ssh

install/pam-install-ssh.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
watchdog_config = """
3+
# SDSLabs Watchdog configuration START
4+
5+
session optional pam_exec.so seteuid /opt/watchdog/bin/pam_ssh
6+
7+
# SDSLabs Watchdog configuration END
8+
"""
9+
10+
inside_watchdog_config = False
11+
12+
def process_line(line):
13+
global inside_watchdog_config
14+
15+
if inside_watchdog_config and line == "# SDSLabs Watchdog configuration END\n":
16+
inside_watchdog_config = False
17+
return ''
18+
19+
if inside_watchdog_config:
20+
return ''
21+
22+
if line == "# SDSLabs Watchdog configuration START\n":
23+
inside_watchdog_config = True
24+
return ''
25+
26+
return line
27+
28+
def main():
29+
iput = open("/etc/pam.d/sshd")
30+
oput = open("tmp_ssh", "w")
31+
lines = iput.readlines()
32+
for l in lines:
33+
oputline = process_line(l)
34+
oput.write(oputline)
35+
36+
oput.write(watchdog_config)
37+
38+
iput.close()
39+
oput.close()
40+
41+
42+
main()

install/pam-install-su.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
watchdog_config = """
3+
# SDSLabs Watchdog configuration START
4+
5+
session optional pam_exec.so seteuid /opt/watchdog/bin/pam_su
6+
7+
# SDSLabs Watchdog configuration END
8+
"""
9+
10+
inside_watchdog_config = False
11+
12+
def process_line(line):
13+
global inside_watchdog_config
14+
15+
if inside_watchdog_config and line == "# SDSLabs Watchdog configuration END\n":
16+
inside_watchdog_config = False
17+
return ''
18+
19+
if inside_watchdog_config:
20+
return ''
21+
22+
if line == "# SDSLabs Watchdog configuration START\n":
23+
inside_watchdog_config = True
24+
return ''
25+
26+
return line
27+
28+
def main():
29+
iput = open("/etc/pam.d/su")
30+
oput = open("tmp_su", "w")
31+
lines = iput.readlines()
32+
for l in lines:
33+
oputline = process_line(l)
34+
oput.write(oputline)
35+
36+
oput.write(watchdog_config)
37+
38+
iput.close()
39+
oput.close()
40+
41+
42+
main()

install/pam-install-sudo.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
watchdog_config = """
3+
# SDSLabs Watchdog configuration START
4+
5+
session optional pam_exec.so seteuid /opt/watchdog/bin/pam_sudo
6+
7+
# SDSLabs Watchdog configuration END
8+
"""
9+
10+
inside_watchdog_config = False
11+
12+
def process_line(line):
13+
global inside_watchdog_config
14+
15+
if inside_watchdog_config and line == "# SDSLabs Watchdog configuration END\n":
16+
inside_watchdog_config = False
17+
return ''
18+
19+
if inside_watchdog_config:
20+
return ''
21+
22+
if line == "# SDSLabs Watchdog configuration START\n":
23+
inside_watchdog_config = True
24+
return ''
25+
26+
return line
27+
28+
def main():
29+
iput = open("/etc/pam.d/sudo")
30+
oput = open("tmp_sudo", "w")
31+
lines = iput.readlines()
32+
for l in lines:
33+
oputline = process_line(l)
34+
oput.write(oputline)
35+
36+
oput.write(watchdog_config)
37+
38+
iput.close()
39+
oput.close()
40+
41+
42+
main()

src/auth_keys_cmd/main.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
extern crate log;
2+
extern crate watchdog;
3+
4+
use log::error;
15
use std::env;
26
use std::fs;
3-
// use std::io::prelude::*;
4-
5-
extern crate watchdog;
67

78
fn main() {
89
let config = watchdog::config::read_config();
10+
watchdog::init::init(&config);
11+
912
let args: Vec<_> = env::args().collect();
1013

1114
let ssh_host_username = &args[1];
@@ -17,7 +20,14 @@ fn main() {
1720
ssh_host_username, ssh_key
1821
);
1922

20-
fs::write(&config.temp_env_file, data).expect("unable to write temp env file");
23+
let res = fs::write(&config.temp_env_file, data);
24+
match res {
25+
Ok(b) => b,
26+
Err(_) => {
27+
error!("Cannot write temporary environment file. Please check if the watchdog `auth_keys_cmd` is run by the root user");
28+
std::process::exit(1);
29+
}
30+
}
2131

2232
println!("{}", ssh_key);
2333
} else {

src/config.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ pub struct Config {
99
pub keyhouse_base_url: String,
1010
pub temp_env_file: String,
1111
pub watchdog_base_url: String,
12+
pub error_log_file: String,
1213
}
1314

1415
pub fn read_config() -> Config {
15-
let toml_str = fs::read_to_string("/home/kanav/projects/watchdog-rs/config.toml")
16+
let toml_str = fs::read_to_string("/opt/watchdog/config.toml")
1617
.expect("Error reading the config.toml file.");
1718
let config: Config = toml::from_str(&toml_str).unwrap();
1819
return config;

src/environment.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub struct TempEnvirontment {
77
pub ssh_key: String,
88
}
99

10-
pub fn read_temp_env(path: String) -> TempEnvirontment {
11-
let toml_str = fs::read_to_string(&path).expect("Error reading the environment toml file.");
10+
pub fn read_temp_env(path: &String) -> TempEnvirontment {
11+
let toml_str = fs::read_to_string(path).expect("Error reading the environment toml file.");
1212
let env: TempEnvirontment = toml::from_str(&toml_str).unwrap();
1313
return env;
1414
}

src/init.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
extern crate simplelog;
2+
3+
use crate::config;
4+
use log::error;
5+
use simplelog::*;
6+
use std::fs::OpenOptions;
7+
8+
pub fn init(config: &config::Config) {
9+
init_logger(&config);
10+
}
11+
12+
pub fn init_logger(config: &config::Config) {
13+
let log_file = match OpenOptions::new()
14+
.create_new(true)
15+
.read(true)
16+
.append(true)
17+
.open(&config.error_log_file)
18+
{
19+
Ok(f) => f,
20+
Err(_) => {
21+
error!("Watchdog: Couldn't open log file");
22+
panic!("Watchdog: Couldn't open log file");
23+
}
24+
};
25+
26+
let _res = match CombinedLogger::init(vec![WriteLogger::new(
27+
LevelFilter::Info,
28+
Config::default(),
29+
log_file,
30+
)]) {
31+
Ok(_) => {}
32+
Err(_) => error!("Watchdog: Couldnt start logger for some reason"),
33+
};
34+
}

0 commit comments

Comments
 (0)