Skip to content

Commit 95edf32

Browse files
committed
Allow specifying pf_rules file to patch on init
When altering more complex setups automatically, pot's way of patching pf.conf is a bit too simplistic. By adding this flag, the user has multiple choices of modifying pf.conf in a controlled way: 1. Write to a different file that's included in pf.conf using its include keyword. 2. Write to a different file that's parsed by additional tooling to assemble a pf.conf (e.g., in automation). 3. In setups where pots modifications serve no real purpose, running `pot init -f ''` can be used to skip touching pf.conf completely. Help text intentionally shows the default to be determined by a command, not the result of it. This is the first patch of a series, with more complex ones to follow, which serve the ultimate purpose of making pot's networking more flexible/customizable using hooks, so it can be integrated into different network environments (as one size doesn't fit all).
1 parent 7cd021d commit 95edf32

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88
### Added
99
- copy-in: -c option to create missing dirs on copy-in (#172)
1010
- create: New command copy-in-flv, which is the same as copy-in, but always relative to flavourdir (#173)
11+
- init: -f option to specify pf file to patch on init (#181)
1112

1213
### Changed
1314
- start: do not write jid files to POT_TMP (#178)

share/pot/init.sh

+24-16
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77

88
init-help()
99
{
10-
echo 'pot init [-h][-v]'
10+
echo 'pot init [-h][-v] [-f pf_file]'
11+
echo ' -f pf_file : write pot anchors to this file (empty to skip),'
12+
echo ' defaults to result of `sysrc -n pf_rules`'
1113
echo ' -h print this help'
1214
echo ' -v verbose'
1315
}
1416

1517
pot-init()
1618
{
1719
local pf_file
20+
pf_file="$(sysrc -n pf_rules)"
1821
OPTIND=1
19-
while getopts "hv" _o ; do
22+
while getopts "hvf:" _o ; do
2023
case "$_o" in
24+
f) pf_file="$OPTARG"
25+
;;
2126
h)
2227
init-help
2328
${EXIT} 0
@@ -122,21 +127,24 @@ pot-init()
122127
# service syslogd restart
123128

124129
# Add pot anchors if needed
125-
pf_file="$(sysrc -n pf_rules)"
126-
if [ -r "$pf_file" ] && [ "$(grep -c '^nat-anchor pot-nat$' "$pf_file" )" -eq 1 ] && [ "$(grep -c '^rdr-anchor "pot-rdr/\*"$' "$pf_file" )" -eq 1 ] ; then
127-
_debug "pf alredy properly configured"
128-
else
129-
if [ -w "$pf_file" ]; then
130-
echo "Creating a backup of your $pf_file"
131-
cp -v "$pf_file" "$pf_file".bkp-pot
132-
# delete incomplete/broken ancory entries - just in case
133-
sed -i '' '/^nat-anchor pot-nat$/d' "$pf_file"
134-
sed -i '' '/^rdr-anchor "pot-rdr\/\*"$/d' "$pf_file"
130+
if [ -n "$pf_file" ]; then
131+
if [ -r "$pf_file" ] && [ "$(grep -c '^nat-anchor pot-nat$' "$pf_file" )" -eq 1 ] && [ "$(grep -c '^rdr-anchor "pot-rdr/\*"$' "$pf_file" )" -eq 1 ] ; then
132+
_debug "pf already properly configured"
135133
else
136-
touch "$pf_file"
134+
if [ -w "$pf_file" ]; then
135+
echo "Creating a backup of your $pf_file"
136+
cp -v "$pf_file" "$pf_file".bkp-pot
137+
# delete incomplete/broken ancory entries - just in case
138+
sed -i '' '/^nat-anchor pot-nat$/d' "$pf_file"
139+
sed -i '' '/^rdr-anchor "pot-rdr\/\*"$/d' "$pf_file"
140+
else
141+
touch "$pf_file"
142+
fi
143+
echo "auto-magically editing your $pf_file"
144+
printf "%s\n" 0a "nat-anchor pot-nat" "rdr-anchor \"pot-rdr/*\"" . x | ex "$pf_file"
145+
echo "Please, check that your PF configuration file $pf_file is still valid!"
137146
fi
138-
echo "auto-magically editing your $pf_file"
139-
printf "%s\n" 0a "nat-anchor pot-nat" "rdr-anchor \"pot-rdr/*\"" . x | ex "$pf_file"
140-
echo "Please, check that your PF configuration file $pf_file is still valid!"
147+
else
148+
_debug "pf configuration skipped"
141149
fi
142150
}

0 commit comments

Comments
 (0)