Skip to content

Commit 4c168a1

Browse files
Initial upload
1 parent 427ea2f commit 4c168a1

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

add-host.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
host=""
4+
cango=false
5+
spath="/home/$(whoami)"
6+
7+
while (( $# >= 1 )); do
8+
case $1 in
9+
-h) host=$2 cango=true;;
10+
*) break;
11+
esac;
12+
shift
13+
done
14+
15+
if [[ "$cango" = 'false' ]]; then
16+
echo "-h is a required argument. Please supply it.";
17+
exit;
18+
fi
19+
20+
if ! [[ -d $spath/.getssl/$host ]]; then
21+
getssl -c $host
22+
fi
23+
mkdir -p $spath/pfscl/md5s
24+
touch $spath/.getssl/$host/$host.crt
25+
md5sum $spath/.getssl/$host/$host.crt > $spath/pfscl/md5s/$host.crt.md5
26+

pfconfedit.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# This script will only edit the config.xml file.
2+
# It will not do anything else. Copying or moving the certificates is done by a seperate script.
3+
# You are free to use or modify this for whatever purpose. Just let me know how much time it saved you :)
4+
import base64
5+
import xmltodict
6+
import secrets
7+
import argparse
8+
9+
parser = argparse.ArgumentParser()
10+
parser.add_argument("--publickey", help = "Specify the location of the public key file", required=True)
11+
parser.add_argument("--privatekey", help= " Specify the location the private key file", required=True)
12+
parser.add_argument("--config", help = 'Specify the location of the downloaded pfSense config', required=True)
13+
args = parser.parse_args()
14+
15+
pf_config_file = args.config
16+
pub_key_file = args.publickey
17+
priv_key_file = args.privatekey
18+
19+
def read_files():
20+
"""Load in pf config file and TLS pub and priv keys"""
21+
with open(pf_config_file, 'r', encoding='utf-8') as conf_file:
22+
r_xml = conf_file.read()
23+
with open(pub_key_file, 'r', encoding='utf-8') as pk:
24+
public_key = pk.read()
25+
with open(priv_key_file, 'r', encoding='utf-8') as pk:
26+
private_key = pk.read()
27+
return r_xml, public_key, private_key
28+
29+
def update_config(certs, public_key, private_key):
30+
"""Update the pf config file with new TLS certs. Also change the cert the web configurator uses."""
31+
gen_refid = secrets.token_hex(13)[:13]
32+
if type(certs) == dict:
33+
34+
# Just incase our originally generated cert id already exist.
35+
while gen_refid == pf_conf['pfsense']['cert']['refid']:
36+
gen_refid = secrets.token_hex(13)[:13]
37+
38+
pf_conf['pfsense']['cert'] = [pf_conf['pfsense']['cert']]
39+
40+
# This is our new certificate. We wont delete the old ones.
41+
# Maybe in the future we will.
42+
pf_conf['pfsense']['cert'].append({
43+
'refid': gen_refid,
44+
'descr': f'LE - {gen_refid}',
45+
'crt': base64.b64encode(public_key.encode('utf-8')).decode('utf-8'),
46+
'prv': base64.b64encode(private_key.encode('utf-8')).decode('utf-8')
47+
})
48+
else:
49+
# Get list of existing cert ids
50+
cert_ids = [cert['refid'] for cert in certs]
51+
52+
while gen_refid in cert_ids:
53+
gen_refid = secrets.token_hex(13)[:13]
54+
pf_conf['pfsense']['cert'].append({
55+
'refid': gen_refid,
56+
'descr': f'LE - {gen_refid}',
57+
'crt': base64.b64encode(public_key.encode('utf-8')).decode('utf-8'),
58+
'prv': base64.b64encode(private_key.encode('utf-8')).decode('utf-8')
59+
})
60+
61+
# Finally, we tell pfsense that it should use our new TLS certificate for the web configurator.
62+
pf_conf['pfsense']['system']['webgui']['ssl-certref'] = gen_refid
63+
64+
65+
r_xml, public_key, private_key = read_files()
66+
67+
pf_conf = xmltodict.parse(r_xml)
68+
69+
update_config(pf_conf['pfsense']['cert'], public_key, private_key)
70+
71+
# Convert the dict to xml and write to disk.
72+
xml_format = xmltodict.unparse(pf_conf, pretty=True)
73+
with open(pf_config_file, 'w', encoding='utf-8') as new_xml:
74+
new_xml.write(xml_format)

watcher.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
spath="/home/vmguest"
4+
sshkey_path="/home/$spath/.secrets"
5+
sshkey="adminkey"
6+
7+
for md5 in "$spath/pfscl/md5s/"*
8+
do
9+
output=$(md5sum -c --quiet "$md5")
10+
if [[ -z "$output" ]]; then
11+
:
12+
else
13+
host=$(echo "$md5" | cut -d'.' -f 1,2,3 | awk -F/ '{print $NF}')
14+
scp -i $sshkey_path/$sshkey admin@$host:/cf/conf/config.xml $host-config.xml
15+
time=$(date +%F_%R:%S_%:::z)
16+
cp $host-config.xml $spath/pfscl/config-archive/$host/$host-config-$time.xml
17+
python3 $spath/pfscl/pfconfedit.py --publickey $spath/.getssl/$host/$host.crt --privatekey $spath/.getssl/$host/$host.key --config $spath/pfscl/$host-config.xml
18+
sleep 2
19+
scp -i $sshkey_path/$sshkey $spath/pfscl/$host-config.xml admin@$host:/cf/conf/config.xml
20+
ssh -i $sshkey_path/$sshkey admin@$host rm /tmp/config.cache
21+
mkdir -p $spath/pfscl/config-archive/$host
22+
time=$(date +%F_%R:%S_%:::z)
23+
mv $host-config.xml $spath/pfscl/config-archive/$host/$host-config-$time.xml
24+
md5sum $spath/.getssl/$host/$host.crt > $spath/pfscl/md5s/$host.crt.md5
25+
fi
26+
done

0 commit comments

Comments
 (0)