Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 203775 - net-misc/vpnc: Add support for scripts executed before/after a vpn connection is established/destroyed
Summary: net-misc/vpnc: Add support for scripts executed before/after a vpn connection...
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: New packages (show other bugs)
Hardware: All Linux
: High enhancement
Assignee: Christian Faulhammer (RETIRED)
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-12-30 14:37 UTC by Thomas Fischer
Modified: 2008-01-14 07:13 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
Patch for /etc/init.d/vpnc or net-misc/vpnc/files/vpnc-1.init (vpnc-init-scripts.diff,1.08 KB, patch)
2007-12-30 14:40 UTC, Thomas Fischer
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Fischer 2007-12-30 14:37:56 UTC
It would be nice to have support to execute scripts when establishing or destroying a vpn connected. E.g. I'd like to restore the default route after establishing the vpn connection, as only relevant traffic should be routed over the tunnel.

Example scripts executed as shown in the attached patch von /etc/init.d/vpnc would be (given that the init script is called vpnc.mynetwork):

mynetwork-preup.sh:
#!/bin/sh
route -n | grep -E '^0.0.0.0 ' | cut -c 17-32 >/var/tmp/defaultgw

mynetwork-postup.sh:
#!/bin/sh
route del -net 0.0.0.0 netmask 0.0.0.0 dev tun1
route add default gw $(cat /var/tmp/defaultgw)
route add -net 123.123.0.0 netmask 255.255.0.0 dev tun1

The patch is quite simple and should disturbe existing setups (except for return codes as $?, please check).

Reproducible: Always

Steps to Reproduce:
Comment 1 Thomas Fischer 2007-12-30 14:40:51 UTC
Created attachment 139631 [details, diff]
Patch for /etc/init.d/vpnc or net-misc/vpnc/files/vpnc-1.init

This patch inserts shell code to execute scripts before/after establishing/destroying a vpn connection. The scripts' names depend on the connection name (as given by the init script's name). The scripts location is /etc/vpnc/scripts.d
Comment 2 Christian Faulhammer (RETIRED) gentoo-dev 2007-12-30 20:19:04 UTC
This looks like an ideal addition...I will add it after some local testing.
Comment 3 Christian Faulhammer (RETIRED) gentoo-dev 2008-01-02 11:31:44 UTC
Thomas, if you are bored you can start writing an extension of http://www.gentoo.org/doc/en/vpnc-howto.xml to document your change.  Simply attach a patch here.
Comment 4 Christian Faulhammer (RETIRED) gentoo-dev 2008-01-02 12:11:14 UTC
A note about your patch: 1) Bash is not allowed in initscripts to be able to be compatible to other shells (POSIX is the standard).  2) You forgot to define PREDOWNSCRIPT 

Nevertheless, works fine here.
Comment 5 Thomas Fischer 2008-01-10 15:33:27 UTC
I noticed that there has been some documentation added to Gentoo's vpnc documentation regarding my pre/post scripts. I wrote some more text describing an example how to used these scripts.
Can someone proof-read my text and add it to the documentation (maybe below the table in subsection "Start vpnc on boot"), please?

--8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--

<p>
These custom-made scripts can be used to setup a user-defined routing for the vpnc connection.
The examples below show how to setup the routing table so that only connections to 123.234.x.x are routed over the VPN and all other connections use the default gateway.
The example uses work-preup.sh to save the current default gateway before starting vpnc (which resets the default gateway using the VPN connection).
Once vpnc has been started, work-postup.sh deletes this new default gateway, restores the old default gateway and sets the route for all connections to 123.234.x.x to use the vpnc connection.
</p>

<pre caption="/etc/vpnc/scripts.d/work-preup.sh">
#!/bin/sh
route -n | grep -E '^0.0.0.0 ' | cut -c 17-32 >/var/tmp/defaultgw
</pre>

<pre caption="/etc/vpnc/scripts.d/work-postup.sh">
#!/bin/sh
route del -net 0.0.0.0 netmask 0.0.0.0 dev tun1
route add default gw $(cat /var/tmp/defaultgw)
route add -net 123.234.0.0 netmask 255.255.0.0 dev tun1
</pre>

<p>
The example scripts assume that the vpnc connection uses tun1 as tun device. You can set the device name in the connection's configuration file.
</p>

<pre caption="/etc/vpnc/work.conf">
Interface name tun1
IPSec gateway vpn.mywork.com
Pidfile /var/run/vpnc.work.pid
</pre>
Comment 6 Christian Faulhammer (RETIRED) gentoo-dev 2008-01-10 16:13:36 UTC
(In reply to comment #5)
> I noticed that there has been some documentation added to Gentoo's vpnc
> documentation regarding my pre/post scripts. I wrote some more text describing
> an example how to used these scripts.

 Great.

> Can someone proof-read my text and add it to the documentation (maybe below the
> table in subsection "Start vpnc on boot"), please?

 This is always done by the doc team, so you could open a bug with your patch attached, they would take care of it.  The text looks good, by the way and I will prepare a patch and submit it to your documentation maintainers.
Comment 7 Christian Faulhammer (RETIRED) gentoo-dev 2008-01-14 07:13:27 UTC
Documentation change requested with patch in bug 205738, thanks.