Skip to content

Commit 02206cc

Browse files
committed
Merge branch 'develop'
2 parents 05d8cb3 + 381a663 commit 02206cc

File tree

29 files changed

+455
-33
lines changed

29 files changed

+455
-33
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
### 1.0.5 - Bugfix, finish hook, persistent interface, no firewall ...
4+
5+
- Fixed bug when running hooks (#3)
6+
- Added **finish** hook (which runs just before container exit)
7+
- Added **persistent interface** option, so interface is persistently present on device (if using host networking mode) and firewall setup rules are executed **only once** (no ip tables mess) (#1)
8+
- Logging chaned to stdout, no more log file by default
9+
- Added **firewall disable** feature to disable all firewall related modifications
10+
- Added `ìp6tables` & more permissions to *ip utils*
11+
- Run OpenVPN only if config is present in `/config/openvpn/server` else **sleep forever** until config was setup & **CONTAINER RESTART**
12+
313
### 1.0.4 - IPv6 docs, improved wizards
414

515
- Added instructions for IPv6 configuration

CONTRIBUTING.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,17 @@ Sections:
5151
hook # Example hook configs
5252
module # Modules for openvpn
5353
hooks # Put your custom scripts in one of subfolders
54+
auth # On authentication (needs to be enabled in config)
55+
client-connect # Client connected
56+
client-disconnect # Client disconnected
57+
down # After interface is down
58+
finish # Deinit container
5459
init # Init container
60+
learn-address
5561
route-up # After routes are added
5662
route-pre-down # Before routes are removed
5763
up # After interface is up
58-
down # After interface is down
59-
client-connect # Client connected
60-
client-disconnect # Client disconnected
61-
learn-address
6264
tls-verify # Check certificate
63-
auth # On authentication (needs to be enabled in config)
6465
system.conf # System OpenVPN config file (do not edit, unless instructed)
6566
include-server.conf # File that includes all server configuration files (automatically generated)
6667
donotdelete # Leave this file alone, if deleted it triggers full setup

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,18 @@ ENV PATH="/app/bin:$PATH" \
5555
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/main/" >> /etc/apk/repositories && \
5656
apk add --no-cache \
5757
# Core packages
58-
bash sudo iptables git openvpn easy-rsa && \
58+
bash sudo iptables ip6tables git openvpn easy-rsa && \
5959
# Link easy-rsa in bin directory
6060
ln -s ${EASYRSA}/easyrsa /usr/local/bin && \
6161
# Link python3 also as python
6262
ln -s /usr/bin/python3 /usr/bin/python && \
6363
# Remove any temporary files created by apk
6464
rm -rf /tmp/* /var/tmp/* /var/cache/apk/* /var/cache/distfiles/* && \
6565
# Add permission for network management to user abc
66-
echo "abc ALL=(ALL) NOPASSWD: /sbin/ip, /sbin/iptables" >> /etc/sudoers
66+
echo "abc ALL=(ALL) NOPASSWD: /sbin/ip, /sbin/ip6tables, /sbin/ip6tables-compat, /sbin/ip6tables-compat-restore, /sbin/ip6tables-compat-save, /sbin/ip6tables-restore, /sbin/ip6tables-restore-translate, \
67+
/sbin/ip6tables-save, /sbin/ip6tables-translate, /sbin/iptables, /sbin/iptables-compat, /sbin/iptables-compat-restore, /sbin/iptables-compat-save, \
68+
/sbin/iptables-restore, /sbin/iptables-restore-translate, /sbin/iptables-save, /sbin/iptables-translate, /sbin/route" \
69+
>> /etc/sudoers.d/abc
6770

6871
# Add repo files to image
6972
COPY root/ /

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ services:
7070
|:-----------:|:----------:|
7171
|`-e PUID=1000`|for UserID - see below for explanation|
7272
|`-e PGID=1000`|for GroupID - see below for explanation|
73+
|`-e OVPN_NFW=true`|Disable any firewall related rules to be created, modified ... (must be implemented in example)|
74+
|`-e OVPN_PERINT=false`|Disable persistent TUN interface|
7375
|`-v /config`|All the config files including OpenVPNs reside here|
7476

7577
See also: [EasyRSA](https://github.com/OpenVPN/easy-rsa/blob/master/doc/EasyRSA-Advanced.md)
@@ -146,7 +148,6 @@ For more infromation see:
146148

147149
- [OpenVPN troubleshoot guide](https://community.openvpn.net/openvpn/wiki/HOWTO#Troubleshooting)
148150

149-
150151
## Contribute
151152

152153
Feel free to contribute new features to this container, but first see [Contribute Guide](CONTRIBUTING.md).
@@ -158,7 +159,7 @@ Planed features:
158159
Wanted features (please help implement):
159160

160161
- LDAP authentication script
161-
- Google authenticator
162+
- Google authenticator
162163

163164
## Licenses
164165

@@ -167,7 +168,6 @@ Wanted features (please help implement):
167168
- [Base image](https://github.com/linuxserver/docker-baseimage-alpine)
168169
- [s6 Layer](https://github.com/just-containers/s6-overlay/blob/master/LICENSE.md)
169170

170-
171171
## Versions
172172

173173
See [CHANGELOG](CHANGELOG.md)

root/app/bin/run_hooks

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ function usage() {
1010
echo "Usage: run_hooks HOOK_NAME [ARGS]"
1111
echo ""
1212
echo "Hooks:"
13-
echo " up saasas"
14-
echo " down-pre asasss"
15-
echo " down-post asasda"
16-
echo " auth asdasd"
13+
echo " auth On OpenVPN client authentication"
14+
echo " client-connect On OpenVPN client connected"
15+
echo " client-disconnect On OpenVPN client disconnected"
16+
echo " finish On container shutdown"
17+
echo " init On container power on"
18+
echo " learn-address Client Address & Routes validation"
19+
echo " down Before/After TUN interface closed"
20+
echo " route-up After routes are added"
21+
echo " route-pre-down Before routes are removed"
22+
echo " tls-verify On OpenVPN client certificate verificaton"
23+
echo " up After TUN interface opened"
1724
}
1825

1926
# Check if hook name is set
@@ -33,8 +40,9 @@ for script in $OVPN_HOOKS/$1/*; do
3340
[ -e "$script" ] || continue
3441

3542
# Execute only executable files
36-
if [ -f "$script" ] || [ -x "$script" ] || true; then
43+
if [ -f "$script" ] && [ -x "$script" ]; then
3744
# Run script and pass additional args to hooks
45+
echo "Executing hook: $script"
3846
if [ $# -gt 2 ]; then
3947
$script ${@:2}
4048
else

root/app/lib/settings

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/with-contenv bash
2+
3+
#
4+
# Settings functions
5+
#
6+
7+
#
8+
# Checks if TUN interface is supposed to be persistant
9+
# @return 1 if persistant, 0 if not
10+
#
11+
function intPersistant() {
12+
if [ ! -n "$OVPN_PERINT" ] || ([ "$OVPN_PERINT" != "true" ] && [ "$OVPN_PERINT" != "1" ]); then
13+
return 0 # Not persistant by default
14+
else
15+
return 1 # Persistant
16+
fi
17+
}
18+
19+
#
20+
# Checks if we use firewall rules
21+
# @return 1 if yes, 0 if not
22+
#
23+
function useFW() {
24+
if [ ! -n "$OVPN_NFW" ] || ([ "$OVPN_NFW" != "true" ] && [ "$OVPN_NFW" != "1" ]); then
25+
return 1 # yes by default
26+
else
27+
return 0 # No
28+
fi
29+
}
30+
31+
#
32+
# Checks if TUN interface exists already
33+
# @return 0 if found, 1 if not found
34+
#
35+
function intTunExists() {
36+
RES=`cat /proc/net/dev | grep tun0`
37+
if [ -n "$RES" ]; then
38+
return 0 # Found
39+
else
40+
return 1 # Not found
41+
fi
42+
}

root/app/lib/utils

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/with-contenv bash
22

33
#
44
# Additional functions
@@ -45,4 +45,16 @@ function arrayContains() {
4545

4646
# Element not found
4747
return 0
48+
}
49+
50+
#
51+
# Function that makes sure, that script runs only once
52+
# @param $1 ID
53+
#
54+
function run_once() {
55+
if [ -f "$1" ]; then # Check if file (as flag) exists
56+
exit 0
57+
fi
58+
touch $1 # Create flag
59+
chown abc:abc $1 # Change permission
4860
}

root/defaults/example/README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,37 @@ config
3535
Readme.md # Info about example, what to configure
3636
```
3737

38+
### Hooks
39+
40+
- start hook file with
41+
42+
``` bash
43+
#!/usr/bin/with-contenv bash
44+
45+
source /app/lib/settings
46+
source /app/lib/utils
47+
```
48+
49+
- if hooks call any **firewall** related commands add after above code and before any commands
50+
51+
``` bash
52+
# Check if firewall rules are disabled
53+
useFW
54+
if [ $? -eq 0 ]; then
55+
# Don't use fw rules
56+
exit 0
57+
fi
58+
```
59+
60+
- also check the examples how persistent interface is handled, so you don't create iptables mess (running init, up script once, never call down, finish)
61+
3862
### Notes
3963
4064
- **DO NOT** use `dev` attribute, because it is set to static interface `tun0`.
4165
- **DO NOT** use any script running directives, because they are probably already set in `system.conf` (except `auth-user-pass-verify` is commented out), but use hooks directory.
4266
- **DO NOT** use log directives, because they are already set for `log` directory.
4367
- Please name your hooks as `<number>-<name>` to ensure order of execution.
68+
- If your hooks need access to container environment variables add `#!/usr/bin/with-contenv bash` at the top of the file.
4469
4570
### Wizard
4671
@@ -49,7 +74,7 @@ User will call `ovpn_enconf CONFIG_NAME [wizard args]` to load your example in s
4974
5075
Then there are two options:
5176
52-
1. User manualy configure settigns in `/config/openvpn` folder
77+
1. User manualy configure settings in `/config/openvpn` folder
5378
2. Your **wizard** script, configures files which will be copied to `/config/openvpn`
5479
- Configuration files are copied to temporary location (so they can be modified)
5580
- `wizard` script will be called with temporary location as first argument `$1` (folder has same structure as in examples)
@@ -69,4 +94,4 @@ Hooks are located in `hook` directory. Please follow hook guidelines:
6994
- What this hook does
7095
- Setttings with comments and an example settings values
7196
72-
**Note:** All hooks run as non-root user so instead of using `ip` and `iptables` use `ovpn-ip` and `ovpn-iptables`.
97+
**Note:** All hooks run as non-root user so instead of using `ip` and `iptables` use `ovpn-ip`, `ovpn-iptables`, `ovpn-ip6tables` (see [/root/usr/local/sbin](/usr/local/sbin)).

root/defaults/example/config/basic_nat/hooks/down/10-network.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
#!/bin/bash
1+
#!/usr/bin/with-contenv bash
2+
3+
source /app/lib/settings
4+
source /app/lib/utils
5+
6+
# Check if firewall rules are disabled
7+
useFW
8+
if [ $? -eq 0 ]; then
9+
# Don't use fw rules
10+
exit 0
11+
fi
12+
13+
# Don't run if interface persistent
14+
intPersistant
15+
if [ $? -eq 1 ]; then
16+
exit 0
17+
fi
218

319
#
420
# Network clear
521
#
22+
echo "Clearing OpenVPN releated firewall rules"
623

724
# Close OpenVPN port to outside
825
ovpn-iptables -D INPUT -p udp -m udp --dport $PORT -j ACCEPT -m comment --comment "Open OpenVPN port"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/with-contenv bash
2+
3+
source /app/lib/settings
4+
source /app/lib/utils
5+
6+
# Check if firewall rules are disabled
7+
useFW
8+
if [ $? -eq 0 ]; then
9+
# Don't use fw rules
10+
exit 0
11+
fi
12+
13+
# Don't run if interface persistent
14+
intPersistant
15+
if [ $? -eq 1 ]; then
16+
exit 0
17+
fi
18+
19+
#
20+
# Network clear
21+
#
22+
echo "Clearing up basic firewall rules"
23+
24+
# Accept everything from input
25+
ovpn-iptables -P INPUT ACCEPT
26+
27+
# Delete: Allow established connection
28+
ovpn-iptables -D INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Accept traffic from established connections"
29+
30+
# Delete: Allow ICMP ping request
31+
ovpn-iptables -D INPUT -p icmp --icmp-type 8 -j ACCEPT
32+
33+
# Accept all forwarded traffic
34+
ovpn-iptables -P FORWARD ACCEPT

root/defaults/example/config/basic_nat/hooks/init/10-network.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
#!/bin/bash
1+
#!/usr/bin/with-contenv bash
2+
3+
source /app/lib/settings
4+
source /app/lib/utils
5+
6+
# Check if firewall rules are disabled
7+
useFW
8+
if [ $? -eq 0 ]; then
9+
# Don't use fw rules
10+
exit 0
11+
fi
12+
13+
# Run only once if interface persistent
14+
intPersistant
15+
if [ $? -eq 1 ]; then
16+
run_once "/config/hooks/init/10-network"
17+
fi
218

319
#
420
# Network initialization
521
#
22+
echo "Setting up basic firewall rules"
623

724
#
825
# Because default iptables rules are set to ACCEPT all connection, we need to put some

root/defaults/example/config/basic_nat/hooks/up/10-network.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
#!/bin/bash
1+
#!/usr/bin/with-contenv bash
2+
3+
source /app/lib/settings
4+
source /app/lib/utils
5+
6+
# Check if firewall rules are disabled
7+
useFW
8+
if [ $? -eq 0 ]; then
9+
# Don't use fw rules
10+
exit 0
11+
fi
12+
13+
# Run only once if interface persistent
14+
intPersistant
15+
if [ $? -eq 1 ]; then
16+
run_once "/config/hooks/up/10-network"
17+
fi
218

319
#
420
# Network initialization
521
#
22+
echo "Setting up OpenVPN related firewall rules"
623

724
# Open OpenVPN port to outside
825
ovpn-iptables -A INPUT -p udp -m udp --dport $PORT -j ACCEPT -m comment --comment "Open OpenVPN port"

root/defaults/example/config/basic_nat_wlp/hooks/down/10-network.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
#!/bin/bash
1+
#!/usr/bin/with-contenv bash
2+
3+
source /app/lib/settings
4+
source /app/lib/utils
5+
6+
# Check if firewall rules are disabled
7+
useFW
8+
if [ $? -eq 0 ]; then
9+
# Don't use fw rules
10+
exit 0
11+
fi
12+
13+
# Don't run if interface persistent
14+
intPersistant
15+
if [ $? -eq 1 ]; then
16+
exit 0
17+
fi
218

319
#
420
# Network clear
521
#
22+
echo "Clearing OpenVPN releated firewall rules"
623

724
# Close OpenVPN port to outside
825
ovpn-iptables -D INPUT -p udp -m udp --dport $PORT -j ACCEPT -m comment --comment "Open OpenVPN port"

0 commit comments

Comments
 (0)