chiark / gitweb /
Merge branch 'master' of http://db.debian.org/git/debian.org
[dsa-metapackages.git] / portforwarder-ssh-wrap
1 #!/bin/bash
2
3 # Copyright (c) 2009,2010 Peter Palfrader
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 set -e
25 set -u
26
27
28 MYLOGNAME="`basename "$0"`[$$]"
29
30 usage() {
31         echo "local Usage: $0 <source-host> <local-bind> <allowed-port> [<allowed-port> ...]"
32         echo "via ssh orig command : forward-to <port>"
33 }
34 croak() {
35         logger -s -p daemon.warn -t "$MYLOGNAME" "$1"
36         exit 1
37 }
38
39
40 if [ -z "${SSH_ORIGINAL_COMMAND:-}" ] ; then
41         echo "Did not find SSH_ORIGINAL_COMMAND" 2>&1
42         exit 1
43 fi
44
45
46 if [ "${1-}" = "-h" ] || [ "${1-}" = "--help" ]; then
47         usage
48         exit 0
49 fi
50 # check/parse local command line
51 if [ "$#" -le 2 ]; then
52         usage >&2
53         exit 1
54 fi
55 host="$1"
56 shift
57 bindaddr="$1"
58 shift
59 if [[ "$bindaddr" =~ [^0-9.] ]]; then
60         echo "Invalid bindaddr spec" >&2
61         exit 1
62 fi
63 allowed_ports="$@"
64
65 # check/parse remote command line
66 set "dummy" ${SSH_ORIGINAL_COMMAND}
67 shift
68
69 # check/parse local command line
70 if [ "$#" != 2 ]; then
71         usage >&2
72         exit 1
73 fi
74 if [ "$1" != "forward-to" ]; then
75         croak "Expected forward-to as command"
76 fi
77 port="$2"
78 if [[ "$port" =~ [^0-9] ]]; then
79         croak "Invalid port spec"
80 fi
81 ok=0
82 for allowed in $allowed_ports ; do
83         if [ "$port" = "$allowed" ]; then
84                 ok=1
85                 break
86         fi
87 done
88
89 if [ "$ok" = "1" ]; then
90         logger -p daemon.info -t "$MYLOGNAME" "Forwarding to port $port for remote host $host"
91         exec /bin/nc -q 600 -s "$bindaddr" 127.0.0.1 "$port"
92         echo "Exec failed" >&2
93         exit 1
94 else
95         croak "$host requested unallowed port $port"
96 fi