]> Cypherpunks.ru repositories - govpn.git/commitdiff
Pass parameters to scripts through environment variables
authorSergey Matveev <stargrave@stargrave.org>
Mon, 7 Mar 2016 11:39:18 +0000 (14:39 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Mon, 7 Mar 2016 12:08:50 +0000 (15:08 +0300)
16 files changed:
VERSION
doc/client.texi
doc/glossary.texi
doc/news.ru.texi
doc/news.texi
doc/scripts.texi [new file with mode: 0644]
doc/server.texi
src/cypherpunks.ru/govpn/cmd/govpn-client/main.go
src/cypherpunks.ru/govpn/cmd/govpn-client/tcp.go
src/cypherpunks.ru/govpn/cmd/govpn-client/udp.go
src/cypherpunks.ru/govpn/cmd/govpn-server/common.go
src/cypherpunks.ru/govpn/cmd/govpn-server/main.go
src/cypherpunks.ru/govpn/cmd/govpn-server/tcp.go
src/cypherpunks.ru/govpn/cmd/govpn-server/udp.go
src/cypherpunks.ru/govpn/common.go
utils/addroute.sh

diff --git a/VERSION b/VERSION
index 2df33d76977064e686bccc1b213c1c9186354aba..760606e1ffff82566e95fa1bfa840ca7d1024dd5 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-5.6
+5.7
index 5b0ae2b7fda2723894f76a765641ae736c656f95..b6d8aab2a544863e63c7ef6f7cff7af539172b7d 100644 (file)
@@ -51,8 +51,9 @@ Set @ref{CPR} in KiB/sec.
 Enable @ref{Encless, encryptionless mode}.
 
 @item -up
-Optional path to script that will be executed after connection is
-established. Interface name will be given to it as a first argument.
+Optional path to @ref{Scripts, script} that will be executed after
+connection is established. Interface name will be given to it as a first
+argument.
 
 @item -down
 Same as @option{-up} above, but it is executed when connection is lost,
@@ -66,8 +67,8 @@ solicitation:
 @verbatim
 client% cat > up.sh <<EOF
 #!/bin/sh
-dhclient $1
-rtsol $1
+dhclient $GOVPN_IFACE
+rtsol $GOVPN_IFACE
 EOF
 client% chmod +x up.sh
 @end verbatim
index fd865cdbde5552ade5d2bdaadd59dc7752495a87..8fed1674d4070f9c0dd4da1ddb9d9dd915427565 100644 (file)
@@ -8,6 +8,7 @@
 * Timeout::
 * Time synchronization: Timesync.
 * Network transport: Network.
+* Scripts::
 * Proxy::
 * Maximum Transmission Unit: MTU.
 * Statistics: Stats.
@@ -23,6 +24,7 @@
 @include timeout.texi
 @include timesync.texi
 @include netproto.texi
+@include scripts.texi
 @include proxy.texi
 @include mtu.texi
 @include stats.texi
index 15219a64de96e194608fa62161c61e53911bf011..aee4aebb418baa943fc13c3f1a574a905b5516f8 100644 (file)
@@ -1,6 +1,13 @@
 @node Новости
 @section Новости
 
+@node Релиз 5.7
+@subsection Релиз 5.7
+@itemize
+@item Имя TAP интерфейса и адрес удалённой стороны теперь передаются в
+up- и down- скрипты через переменные окружения.
+@end itemize
+
 @node Релиз 5.6
 @subsection Релиз 5.6
 @itemize
index 99edbe60397bbc862bb75dce04f2668819de920f..c226c0cef9faac71c100b12e98ffa1d7915c43cc 100644 (file)
@@ -3,6 +3,13 @@
 
 See also this page @ref{Новости, on russian}.
 
+@node Release 5.7
+@section Release 5.7
+@itemize
+@item TAP interface name and remote peer's address are passed to up- and
+down- scripts through environment variables.
+@end itemize
+
 @node Release 5.6
 @section Release 5.6
 @itemize
diff --git a/doc/scripts.texi b/doc/scripts.texi
new file mode 100644 (file)
index 0000000..7b2e3c0
--- /dev/null
@@ -0,0 +1,17 @@
+@node Scripts
+@subsection Scripts
+
+Up- and down- scripts used as a hook executed when connection is either
+established or lost. Following environment variables are set during
+their execution:
+
+@table @code
+
+@item GOVPN_REMOTE
+Remote peer's address. In client mode it is server's address.
+
+@item GOVPN_IFACE
+TAP interface name. In server mode this can be empty: that means that
+script must output its name as the first line to stdout.
+
+@end table
index 1784a9bb4858390d276b5354ba083c1dc3f45c24..b3c641e00fe7b2234978926eec7d0326aa57ce98 100644 (file)
@@ -40,13 +40,13 @@ stargrave: {                        <-- Peer human readable name
 
 At least one of either @code{iface} or @code{up} must be specified. If
 you specify @code{iface}, then it will be forcefully used to determine
-what TAP interface will be used. If it is not specified, then up-script
-must output interface's name to stdout (first output line).
+what TAP interface will be used. If it is not specified, then
+up-@ref{Scripts, script} must output interface's name to stdout
+(first output line).
 
 For example up-script can be just @code{echo tap10}, or more advanced
 like the following one:
 
-
 @verbatim
 #!/bin/sh
 $tap=$(ifconfig tap create)
index 10da96929ed6f6786c9e4b0000bb009b7e03ea10..032a288c78734013b106243b6a140f7dde0af7b9 100644 (file)
@@ -156,5 +156,5 @@ MainCycle:
                close(rehandshaking)
                close(termination)
        }
-       govpn.ScriptCall(*downPath, *ifaceName)
+       govpn.ScriptCall(*downPath, *ifaceName, *remoteAddr)
 }
index dd353649ad4db37ee0f2a1b0b673b7e7d71b2a76..4b11eac1091f8c5cca3bd458d95230fc1ba89b7e 100644 (file)
@@ -83,7 +83,7 @@ HandshakeCycle:
                log.Println("Handshake completed")
                knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
                if firstUpCall {
-                       go govpn.ScriptCall(*upPath, *ifaceName)
+                       go govpn.ScriptCall(*upPath, *ifaceName, *remoteAddr)
                        firstUpCall = false
                }
                hs.Zero()
index c70cf46c1ea2c3e8a22b5f8fdded82ef43b7a243..acacbd15473b435abbada466ec37f8030e66ff85 100644 (file)
@@ -89,7 +89,7 @@ MainCycle:
                log.Println("Handshake completed")
                knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
                if firstUpCall {
-                       go govpn.ScriptCall(*upPath, *ifaceName)
+                       go govpn.ScriptCall(*upPath, *ifaceName, *remoteAddr)
                        firstUpCall = false
                }
                hs.Zero()
index eac8a98097b10ac1c4c11f2ca7e55ab6505072c3..f7435dddcd766a63337e23e9762fd20d5ec5cfdb 100644 (file)
@@ -66,10 +66,10 @@ Processor:
        heartbeat.Stop()
 }
 
-func callUp(peerId *govpn.PeerId) (string, error) {
+func callUp(peerId *govpn.PeerId, remoteAddr string) (string, error) {
        ifaceName := confs[*peerId].Iface
        if confs[*peerId].Up != "" {
-               result, err := govpn.ScriptCall(confs[*peerId].Up, "")
+               result, err := govpn.ScriptCall(confs[*peerId].Up, ifaceName, remoteAddr)
                if err != nil {
                        log.Println("Script", confs[*peerId].Up, "call failed", err)
                        return "", err
index 886b8a812b365dae21960e36232e4d18b0e6618f..0c385c74436a77af75672c155d4a9cdf66fb6663 100644 (file)
@@ -115,6 +115,7 @@ MainCycle:
                                        go govpn.ScriptCall(
                                                confs[*ps.peer.Id].Down,
                                                ps.tap.Name,
+                                               ps.peer.Addr,
                                        )
                                        ps.terminator <- struct{}{}
                                }
index 417fcb1601f9fac8c67509ca5693a2b447891656..6ca6967f60af4c892b50d59242ddf273a17d7154 100644 (file)
@@ -115,7 +115,7 @@ func handleTCP(conn net.Conn) {
                        kpLock.Unlock()
                        log.Println("Rehandshake processed:", peer.Id.String())
                } else {
-                       ifaceName, err := callUp(peer.Id)
+                       ifaceName, err := callUp(peer.Id, peer.Addr)
                        if err != nil {
                                peer = nil
                                break
index 73f4d6fbc7e79cf678f50aab5730d661099dd994..d9197cfbbe0e52c8721ffb27bf4f7b818b1abe0c 100644 (file)
@@ -135,7 +135,7 @@ func startUDP() {
                                log.Println("Rehandshake processed:", peer.Id.String())
                        } else {
                                go func(addr string, peer *govpn.Peer) {
-                                       ifaceName, err := callUp(peer.Id)
+                                       ifaceName, err := callUp(peer.Id, peer.Addr)
                                        if err != nil {
                                                return
                                        }
index dc7fbe0a08f059ddd80f0baa4ad5a8727df60ac2..98137c1c3bc4e5e91744c1c8fb22b87c12afb613 100644 (file)
@@ -30,6 +30,9 @@ const (
        EtherSize      = 14
        MTUMax         = 9000 + EtherSize + 1
        MTUDefault     = 1500 + EtherSize + 1
+
+       ENV_IFACE  = "GOVPN_IFACE"
+       ENV_REMOTE = "GOVPN_REMOTE"
 )
 
 var (
@@ -40,14 +43,17 @@ var (
 // You have to specify path to it and (inteface name as a rule) something
 // that will be the first argument when calling it. Function will return
 // it's output and possible error.
-func ScriptCall(path, ifaceName string) ([]byte, error) {
+func ScriptCall(path, ifaceName, remoteAddr string) ([]byte, error) {
        if path == "" {
                return nil, nil
        }
        if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
                return nil, err
        }
-       out, err := exec.Command(path, ifaceName).CombinedOutput()
+       cmd := exec.Command(path)
+       cmd.Env = append(cmd.Env, ENV_IFACE+"="+ifaceName)
+       cmd.Env = append(cmd.Env, ENV_REMOTE+"="+remoteAddr)
+       out, err := cmd.CombinedOutput()
        if err != nil {
                log.Println("Script error", path, err, string(out))
        }
index a8d85cd4226f5f05b6561f8d37d3a9dbbec6d586..e0ad70b886a11d2944ff10f9df3602f26a0c1054 100755 (executable)
@@ -6,34 +6,34 @@
 # List of parameters passed through environment
 # - reason               -- why this script is called:
 #                           pre-init, connect, disconnect
-# - VPNGATEWAY           -- public address of vpn gateway
-# - TAPDEV               -- tap device
+# - GOVPN_REMOTE         -- public address of VPN gateway
+# - GOVPN_IFACE          -- tap device
 # - INTERNAL_IP4_ADDRESS -- e.g. 172.0.0.2/24
 # - INTERNAL_IP4_GATEWAY -- e.g. 172.0.0.1
 
 
 set_up_dev() {
-  ip tuntap add dev $TAPDEV mode tap
+  ip tuntap add dev $GOVPN_IFACE mode tap
 }
 
 
 tear_down_dev() {
-  ip tuntap del dev $TAPDEV mode tap
+  ip tuntap del dev $GOVPN_IFACE mode tap
 }
 
 
 do_connect() {
   local OLDGW=$(ip route show 0/0 | sed 's/^default//')
-  ip link set dev $TAPDEV up
-  ip addr add $INTERNAL_IP4_ADDRESS dev $TAPDEV
-  ip route add $VPNGATEWAY $OLDGW
-  ip route add 0/1 via $INTERNAL_IP4_GATEWAY dev $TAPDEV
-  ip route add 128/1 via $INTERNAL_IP4_GATEWAY dev $TAPDEV
+  ip link set dev $GOVPN_IFACE up
+  ip addr add $INTERNAL_IP4_ADDRESS dev $GOVPN_IFACE
+  ip route add $GOVPN_REMOTE $OLDGW
+  ip route add 0/1 via $INTERNAL_IP4_GATEWAY dev $GOVPN_IFACE
+  ip route add 128/1 via $INTERNAL_IP4_GATEWAY dev $GOVPN_IFACE
 }
 
 
 do_disconnect() {
-  ip route del $VPNGATEWAY
+  ip route del $GOVPN_REMOTE
 }