checkpasswd: fix -other service file
[userv-utils] / ipif / udptunnel
CommitLineData
46ab1c83 1#!/usr/bin/perl
5124214b 2# Encrypting VPN tunnel for use with userv-ipif.
46ab1c83 3#
5124214b 4# This comment is reference documentation. See ipif/INSTALL for the
5# installation instructions and setup tutorial.
8f1bfa81 6#
46ab1c83 7# usage:
5124214b 8# To make a tunnel between two machines `alice' and `bob',
9# on `alice', the active endpoint, run:
10#
11# udptunnel
12# [ -l[<alice-command/arg>] ... .
9d4e63db 13# | -e <encryption-mech>[/<encryption-parameter>...]
5124214b 14# | -m (`masquerade support': bob gets `Wait' instead of our addr/port)
15# | -d (`dump keys': when no peer, spew keys rather than reading them;
16# we always send keys to our peer if there is one)
09966b49 17# | -Dcrypto (debug crypto - use with care, prints keys, packets &c on screen!)
9d4e63db 18# | -f<path-to-udptunnel-forwarder>
1fb3cba0 19# ...
20# ]
5124214b 21# <alice-phys-addr>,<alice-phys-port>
22# <bob-phys-addr>,<bob-phys-port>
23# <alice-virt-addr>,<bob-virt-addr>,<mtu>,<proto>
92bbf44b 24# <keepalive>,<timeout>[,<reannounce>]
5124214b 25# <alice-priv-nets> <bob-priv-nets>
26# [ <bob-command> [<bob-args> ...] ]
46ab1c83 27#
5124214b 28# This will run udptunnel-forwarder on alice, and use <bob-command>
29# (usually an ssh invocation) to run udptunnel appropriately on bob.
30# Key material will be generated by alice and fed to udptunnel on bob
31# via <bob-command>'s stdin, and the physical address and port on bob
32# will be (if so configured) returned via <bob-command>'s stdout.
1fb3cba0 33#
5124214b 34# The tunnel will stay up until one of the subprocesses involved dies,
35# or the keepalive timeout expires. If you want the tunnel to remain
36# up permanently, you must arrange to invoke it repeatedly (eg, from
37# inittab). See INSTALL.
1fb3cba0 38#
5124214b 39# <proto> may be slip or cslip
46ab1c83 40#
5124214b 41# <mtu> will be the MTU of the tunnel interfaces; it is best if this
42# is enough smaller than the path MTU between the physical interfaces
43# that the encapsulated packets will fit without fragmentation.
44#
45# Any <..-addr> supplied to udptunnel may also be hostname; these will
46# all be looked up on alice and IP addresses passed to bob.
47#
48# The `local' physical address and ports (ie, alice's own details),
49# may have these special values:
50# `Any' choose one ourselves and do not print it (the port chosen
51# will be supplied to bob)
52# `Print' choose one ourselves and print both port and addr
53# (this is not usually useful specified directly; it's
54# used by udptunnel when it invokes itself on bob via
55# <bob-command>, to have its other self print the
56# relevant value.
57#
58# The `remote' physical address and port (ie, on alice, bob's details),
59# may also have the special values:
60# `Command' wait for <bob-command> to tell us the values (this is
61# usually the right choice on alice for at least the
62# port). <bob-command> must be specified (ie, this
63# only makes sense on alice).
64# `Wait' alice will wait to receive a packet from bob and use
65# whatever address and port it came from
66#
67# These special values are case-sensitive. When alice runs udptunnel
68# on bob they are automatically translated to appropriate other values
69# in the arguments to bob's udptunnel.
70#
71# If <bob-command> is specified it should run udptunnel at the
72# bob end; it will be invoked as
73# <bob-command> [ <bob-args> ... ]
74# [ <-e arguments passed along> ]
75# <bob-phys-addr'>,<bob-phys-port'>
76# <alice-phys-addr'>,<alice-phys-port'>
77# <bob-virt-addr>,<alice-virt-addr>,<mtu>,<proto>
78# <keepalive>,<timeout>[,<reannounce>]
79# <bob-priv-nets> <alice-priv-nets>
80#
81# If it was given Print for <bob-phys-foo'>, udptunnel's first stdout
82# output will be the real <bob-phys-addr>,<bob-phys-port> pair. It
83# may then produce more stdout which, if any, will be forwarded to the
84# local end's stdout as debugging info.
85#
86# After this, if any encryption was specified, the encryption key
87# material will be fed into its stdin. See the documentation in the
88# mech-*.c files for details of the parameters. udptunnel on alice
89# will arrange to feed the keys fd of udptunnel-forwarder into the
90# stdin of the udptunnel on bob.
91#
92# <bob-phys-foo'> is as follows:
93# <bob-phys-foo> <bob-phys-foo'>
94# actual addr/port that addr/port
95# `Command' `Print'
96# `Wait' `Any'
97#
98# <alice-phys-foo'> is as follows:
99# <alice-phys-foo> <alice-phys-foo'> <alice-phys-foo'>
100# (-m not specified) (-m specified)
9d4e63db 101# actual addr/port that addr/port `Wait'
9d4e63db 102# `Print' the chosen address `Wait'
31dcbf98 103# `Any' `Wait' for addr, `Wait'
104# chosen port for port
9d4e63db 105#
5124214b 106# In each case udptunnel will run userv ipif locally, as
107# userv root ipif <local-virt-addr>,<remote-virt-addr>,<mtu>,<proto>
108# <remote-priv-nets>
9d4e63db 109# or, if -l was given, userv root ipif is replaced with the argument(s)
110# following -l option(s) until `.'.
111#
5124214b 112# udptunnel will also run udptunnel-forwarder with appropriate options.
9d4e63db 113#
114# recommended encryption parameters are:
115# -e nonce (prepend 32 bit counter)
116# -e timestamp/<max-skew>/<max-age> (prepend 32 bit time_t, and check on receipt)
117# -e pkcs5/8 (pad as per PKCS#5 to 8-byte boundary)
118# -e blowfish-cbcmac/128 (prepend CBC MAC with random IV and 128 bit key)
119# -e blowfish-cbc/128 (encrypt with CBC, random IV and 128 bit key)
5124214b 120# where <max-skew> is perhaps 10 and <max-age> perhaps 30. If your
121# clocks are not sufficiently well synchronised, you could replace
122# `-e nonce -e timestamp/...' with just `-e sequence'. Do not just
123# remove `-e timestamp/...'.
46ab1c83 124
9d4e63db 125# Copyright (C) 1999-2000 Ian Jackson
caa68336 126#
127# This is free software; you can redistribute it and/or modify it
128# under the terms of the GNU General Public License as published by
129# the Free Software Foundation; either version 2 of the License, or
130# (at your option) any later version.
131#
132# This program is distributed in the hope that it will be useful, but
133# WITHOUT ANY WARRANTY; without even the implied warranty of
134# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
135# General Public License for more details.
136#
137# You should have received a copy of the GNU General Public License
138# along with userv-utils; if not, write to the Free Software
139# Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
140#
141# $Id$
142
46ab1c83 143use Socket;
144use POSIX;
145use Fcntl;
146
147$progname= $0; $progname =~ s,.*/,,;
148$|=1;
149
150chomp($hostname= `uname -n`);
151$? and die "$progname: cannot get hostname (uname failed with code $?)\n";
152
153sub quit ($) { die "$progname - $hostname: fatal error: $_[0]\n"; }
154sub debug ($) { print "$progname - $hostname: debug: $_[0]\n"; }
9d4e63db 155sub fail ($) { quit("unexpected system call failure: $_[0]: $!"); }
46ab1c83 156sub warning ($) { warn "$progname - $hostname: $_[0]\n"; }
157
158sub eat_addr_port ($) {
159 my ($x) = @_;
1fb3cba0 160 @ARGV or quit("<addr>,<port> missing");
46ab1c83 161 $_= shift(@ARGV);
9d4e63db 162 (m/^$x,/i && m/^[a-z]/ || m/,$x$/i && m/,[a-z]/)
163 and warning("$_: use Mixed Case for special values");
164 m/^([0-9a-z][0-9a-z-+.]+|$x)\,(\d+|$x)$/
165 or quit("$_: <host/addr>,<port> bad syntax".
166 (m/[A-Z]/ ? ' (use lowercase for hostnames)' : ''));
46ab1c83 167 return ($1,$2);
168}
169sub conv_host_addr ($) {
9d4e63db 170 my ($s,$r,@h) = @_;
171 return INADDR_ANY() if $s =~ m/^[A-Z][a-z]/;
172 return $r if defined($r= inet_aton($s));
173 @h= gethostbyname($s) or quit("$s: cannot get address");
174 $h[2] eq &AF_INET or quit("$s: address is not IPv4");
175 @h < 5 or quit("$s: name maps to no addresses");
176 $r= $h[4];
177 @h == 5 or warning("$s: name has several addresses, using ".inet_ntoa($r));
46ab1c83 178 return $r;
179}
217afbe6 180sub conv_port_number ($) {
46ab1c83 181 my ($s,$r) = @_;
9d4e63db 182 return 0 if $s =~ m/^[A-Z][a-z]/;
183 $r= $s+0;
184 $r>0 && $r<65536 or quit("$s: port out of range");
46ab1c83 185 return $r;
186}
9d4e63db 187sub show_addr ($) {
188 my ($s,@s) = @_;
189 @s= unpack_sockaddr_in($s);
190 return inet_ntoa($s[1]);
191}
192sub show_port ($) {
46ab1c83 193 my ($s,@s) = @_;
194 @s= unpack_sockaddr_in($s);
9d4e63db 195 return $s[0];
196}
197sub show_addr_port ($) {
198 my ($s) = @_;
199 return show_addr($s).','.show_port($s);
46ab1c83 200}
9d4e63db 201sub arg_value ($$) {
0f4b558c 202 my ($val,$opt) = @_;
9d4e63db 203 $_= '-';
204 return $val if length $val;
205 @ARGV or quit("$opt needs value");
206 return shift @ARGV;
207}
208
abe75cda 209@lcmd= ();
9d4e63db 210@encryption= ();
211$masq= 0;
212$dump= 0;
213$fcmd= 'udptunnel-forwarder';
09966b49 214$xfwdopts= '';
abe75cda 215
216while ($ARGV[0] =~ m/^-/) {
217 $_= shift @ARGV;
9d4e63db 218 last if m/^--?$/;
219 while (!m/^-$/) {
220 if (s/^-l//) {
221 push @lcmd,$_ if length;
222 while (@ARGV && ($_= shift @ARGV) ne '.') { push @lcmd, $_; }
223 $_= '-'
224 } elsif (s/^-f//) {
225 $fcmd= arg_value($_,'-f');
226 } elsif (s/^-e//) {
227 $encrarg= arg_value($_,'-e');
0f4b558c 228 push @remoteopts, "-e$encrarg";
229 @thisencryption= split m#/#, $encrarg;
ed509ebd 230 $thisencryption[0] =~ s/^/\|/;
0f4b558c 231 push @encryption, @thisencryption;
9d4e63db 232 } elsif (s/^-m/-/) {
233 $masq= 1;
234 } elsif (s/^-d/-/) {
235 $dump= 1;
09966b49 236 } elsif (s/^-Dcrypto$/-/) {
237 $xfwdopts.= 'K';
0f4b558c 238 push @remoteopts, '-Dcrypto';
9d4e63db 239 } else {
240 quit("unknown option \`$_'");
241 }
abe75cda 242 }
243}
244
9d4e63db 245# Variables \$[lr]a?p?(|s|d|r)
246# Local/Remote Address&/Port
247# actualvalue/Specified/Displaypassdown/fromRemote/passtoForwarder
248#
249($las,$lps)= eat_addr_port('Print|Any');
46ab1c83 250$la= conv_host_addr($las);
251$lp= conv_port_number($lps);
252$ls= pack_sockaddr_in $lp,$la;
253
9d4e63db 254($ras,$rps)= eat_addr_port('Wait|Command');
255$ra= conv_host_addr($ras);
46ab1c83 256$rp= conv_port_number($rps);
9d4e63db 257$rs= pack_sockaddr_in $rp,$ra;
46ab1c83 258
259$_= shift @ARGV;
260m/^([.0-9]+),([.0-9]+),(\d+),(slip|cslip)$/
261 or quit("lvaddr,rvaddr,mtu,proto missing or bad syntax or proto not [c]slip");
217afbe6 262($lva,$rva,$mtu,$proto) = ($1,$2,$3,$4);
46ab1c83 263
264$_= shift @ARGV;
92bbf44b 265if (m/^(\d+),(\d+)$/) {
266 ($keepalive,$timeout,$reannounce)= ($1+0,$2+0,0);
267 $ka_to_ra= "$keepalive,$timeout";
268} elsif (m/^(\d+),(\d+),(\d+)$/) {
269 ($keepalive,$timeout,$reannounce)= ($1+0,$2+0,$3);
270 "$keepalive,$timeout",
271 $ka_to_ra= "$keepalive,$timeout,$reannounce";
272} else {
273 quit("keepalive,timeout missing or bad syntax");
274}
46ab1c83 275$keepalive && ($timeout > $keepalive*2) or quit("timeout must be < 2*keepalive")
276 if $timeout;
277
9d4e63db 278# Variables \$[lr]exn
279# Local/Remote Extra Nets
280$lexn= shift @ARGV;
281$rexn= shift @ARGV;
46ab1c83 282
283defined($udp= getprotobyname('udp')) or fail("getprotobyname udp");
284
285socket(L,PF_INET,SOCK_DGRAM,$udp) or fail("socket");
286bind(L,$ls) or quit("bind failed: $!");
287defined($ls= getsockname(L)) or fail("getsockname");
9d4e63db 288$lad= show_addr($ls);
289$lpd= show_port($ls);
290$lapd= "$lad,$lpd";
291
292print "$lapd\n" or fail("print addr/port") if ($las eq 'Print' || $lps eq 'Print');
46ab1c83 293
9d4e63db 294$rapcmd= ($ras eq 'Command' || $rps eq 'Command');
295quit("need remote-command if Command for remote addr/port") if $rapcmd && !@ARGV;
296
297sub xform_remote ($$) {
298 my ($showed,$spec) = @_;
299 return 'Print' if $spec eq 'Command';
300 return 'Any' if $spec eq 'Wait';
301 return $showed;
302}
303
304if (@ARGV) {
305 warning("-d specified with remote command, ignoring") if $dump;
306 $dump= 1;
307
308 $rad= xform_remote(show_addr($rs),$ras);
309 $rpd= xform_remote(show_port($rs),$rps);
310 @rcmd= (@ARGV,
0f4b558c 311 @remoteopts,
9d4e63db 312 "$rad,$rpd",
31dcbf98 313 $masq ? 'Wait,Wait' : $las eq 'Any' ? "Wait,$lpd" : $lapd,
9d4e63db 314 "$rva,$lva,$mtu,$proto",
92bbf44b 315 $ka_to_ra,
9d4e63db 316 $rexn, $lexn);
217afbe6 317 debug("remote command @rcmd");
9d4e63db 318
319 if ($rapcmd) {
320 pipe(RAPREAD,RCMDREADSUB) or fail("pipe");
46ab1c83 321 }
0f4b558c 322 pipe(RCMDWRITESUB,DUMPKEYS) or fail("pipe");
9d4e63db 323 defined($c_rcmd= fork) or fail("fork for remote");
324 if (!$c_rcmd) {
0f4b558c 325 open STDIN, "<&RCMDWRITESUB" or fail("reopen stdin for remote command");
9d4e63db 326 open STDOUT, ">&RCMDREADSUB" or fail("reopen stdout for remote command")
327 if $rapcmd;
328 close RAPREAD if $rapcmd;
329 close DUMPKEYS;
330 close RCMDWRITESUB;
331 close RCMDREADSUB;
332 close L;
333 exec @rcmd; fail("failed to execute remote command $rcmd[0]");
46ab1c83 334 }
9d4e63db 335 close RCMDWRITESUB;
336
337 if ($rapcmd) {
338 close RCMDREADSUB if $rapcmd;
4ffb4653 339 $_= '';
340 while (!m/\n/) {
341 $!=0;
342 defined($nread= sysread(RAPREAD,$_,1,length))
343 or fail("read from remote command");
344 if (!$nread) {
345 close DUMPKEYS;
346 close RAPREAD;
347 waitpid $c_rcmd,0 or fail("wait for remote command");
348 quit($? ? "remote command failed (code $?)" :
349 "no details received from remote");
350 }
351 }
352 chomp;
353 m/^([.0-9]+)\,(\d+)$/ or quit("invalid details from remote end: \`$_'");
354 ($rar,$rpr) = ($1,$2);
355 $ra= conv_host_addr($rar);
356 $rp= conv_port_number($rpr);
9d4e63db 357
358 defined($c_catremdebug= fork) or fail("fork for cat remote debug");
359 if (!$c_catremdebug) {
360 open(STDIN,"<&RAPREAD") or fail("redirect remote debug");
9d4e63db 361 close DUMPKEYS;
362 close L;
363 exec "cat"; fail("execute cat");
364 }
365 close RAPREAD;
46ab1c83 366 }
9d4e63db 367} elsif ($dump) {
368 open DUMPKEYS, ">&STDOUT" or fail("reopen stdout for key material");
369 $dump= 1;
46ab1c83 370} else {
9d4e63db 371 open DUMPKEYS, "<&STDIN" or fail("reopen stdout for key material");
46ab1c83 372}
373
374$rs= pack_sockaddr_in $rp,$ra;
46ab1c83 375
9d4e63db 376if ($ras eq 'Wait' || $rps eq 'Wait') {
377 @rapf= ('');
378 $rapd= ('Wait,Wait');
379} else {
380 @rapf= (show_addr($rs), show_port($rs));
381 $rapd= show_addr_port($rs);
382}
abe75cda 383@lcmd= qw(userv root ipif) unless @lcmd;
384
9d4e63db 385debug("using remote $rapd local $lapd");
386push @lcmd, ("$lva,$rva,$mtu,$proto",$lexn);
387debug("local command @lcmd.");
46ab1c83 388
389pipe(UR,UW) or fail("up pipe");
390pipe(DR,DW) or fail("down pipe");
391
9d4e63db 392defined($c_lcmd= fork) or fail("fork for local command");
393if (!$c_lcmd) {
46ab1c83 394 close UR; close DW;
217afbe6 395 open(STDIN,"<&DR") or fail("reopen stdin for packets");
396 open(STDOUT,">&UW") or fail("reopen stdout for packets");
abe75cda 397 exec @lcmd;
398 quit("cannot execute $lcmd[0]: $!");
46ab1c83 399}
400close UW;
401close DR;
402
09966b49 403$xfwdopts.= 'w' if $dump;
404
405@fcmd= ($fcmd, $xfwdopts,
406 fileno(L), fileno(DW), fileno(UR), fileno(DUMPKEYS),
92bbf44b 407 $mtu, $keepalive, $timeout, $reannounce,
9d4e63db 408 @rapf,
9d4e63db 409 @encryption);
410debug("forwarding command @fcmd.");
46ab1c83 411
9d4e63db 412defined($c_fwd= fork) or fail("fork for udptunnel-forwarder");
413if (!$c_fwd) {
0f4b558c 414 foreach $fd (qw(L DW UR DUMPKEYS)) {
9d4e63db 415 fcntl($fd, F_SETFD, 0) or fail("set no-close-on-exec $fd");
416 }
417 exec @fcmd; fail("cannot execute $fcmd[0]");
46ab1c83 418}
419
9d4e63db 420close L;
421close DUMPKEYS;
422close UR;
423close DW;
46ab1c83 424
9d4e63db 425%procs= ($c_fwd, 'forwarder',
426 $c_lcmd, 'local command');
427$procs{$c_rcmd}= 'remote command' if $c_rcmd;
428$procs{$c_catremdebug}= 'debug cat' if $c_catremdebug;
46ab1c83 429
9d4e63db 430$estatus= 0;
217afbe6 431
9d4e63db 432while (keys %procs) {
433 ($c= wait) >0 or
434 fail("wait failed (expecting ". join('; ',keys %procs). ")");
0f4b558c 435 $status= $?;
436 warning("unexpected child reaped: pid $c, code $status"), next
9d4e63db 437 unless exists $procs{$c};
438 $str= $procs{$c};
439 delete $procs{$c};
0f4b558c 440 $status ? warning("subprocess $str failed with code $status")
9d4e63db 441 : debug("subprocess $str finished");
442 if ($c==$c_lcmd || $c==$c_fwd || $c==$c_rcmd) {
443 kill 15, grep (exists $procs{$_}, $c_fwd, $c_rcmd);
46ab1c83 444 }
9d4e63db 445 $estatus=1 unless $c == $c_catremdebug;
46ab1c83 446}
9d4e63db 447
448debug("all processes terminated, exiting with status $estatus");
449
450exit $estatus;