[c9f5490] | 1 | #!/usr/bin/perl -w |
---|
| 2 | |
---|
| 3 | # Kevin Lahey, lahey@isi.edu |
---|
| 4 | # July 11, 2007 |
---|
| 5 | |
---|
| 6 | # Set up ssh tunnel infrastructure for federation: |
---|
| 7 | # |
---|
[a098fab] | 8 | # * Parse the configuration file provided. |
---|
| 9 | # |
---|
| 10 | # * Figure out whether we're the initiator or the reciever; if we're |
---|
| 11 | # the receiver, we just need to set up ssh keys and exit. |
---|
| 12 | # |
---|
| 13 | # * Pick out the experimental interface, remove the IP address |
---|
[c9f5490] | 14 | # |
---|
[a098fab] | 15 | # * Create a layer 2 ssh tunnel, set up bridging, and hang loose. |
---|
[c9f5490] | 16 | |
---|
| 17 | use strict; |
---|
| 18 | use Getopt::Std; |
---|
| 19 | use POSIX qw(strftime); |
---|
| 20 | use Sys::Hostname; |
---|
[a098fab] | 21 | use IO::File; |
---|
[c9f5490] | 22 | |
---|
| 23 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
| 24 | my $SSH = "/usr/local/bin/ssh"; |
---|
[a098fab] | 25 | my $NC = "/usr/bin/nc"; |
---|
| 26 | my $SSH_PORT = 22; |
---|
[c9f5490] | 27 | |
---|
[a098fab] | 28 | sub setup_bridging; |
---|
| 29 | sub setup_tunnel_cfg; |
---|
| 30 | sub parse_config; |
---|
[c9f5490] | 31 | |
---|
[a098fab] | 32 | # Option use is as follows: |
---|
| 33 | # -f filename containing config file |
---|
| 34 | # -d turn on debugging output |
---|
| 35 | # -r remotely invoked |
---|
| 36 | # (if remotely invoked the last two args are the tun interface |
---|
| 37 | # number and the remote address of the tunnel) |
---|
[c9f5490] | 38 | |
---|
[a098fab] | 39 | my $usage = "Usage: fed-tun.pl [-r] [-d] [-f config-filename] [count addr]\n"; |
---|
[c9f5490] | 40 | |
---|
[a098fab] | 41 | my %opts; |
---|
[cd3eceb] | 42 | my @expected_opts = qw(active tunnelcfg bossname fsname type |
---|
| 43 | peer pubkeys privkeys); |
---|
[a098fab] | 44 | my $filename; |
---|
| 45 | my $remote; |
---|
| 46 | my $debug = 1; |
---|
[c9f5490] | 47 | my $count; |
---|
| 48 | my $addr; |
---|
[cd3eceb] | 49 | my $active; |
---|
| 50 | my $type; |
---|
| 51 | my $tunnelcfg; |
---|
| 52 | my $ssh_port_fwds = ""; |
---|
[c9f5490] | 53 | |
---|
[a098fab] | 54 | if ($#ARGV != 0 && !getopts('df:r', \%opts)) { |
---|
[c9f5490] | 55 | die "$usage"; |
---|
| 56 | } |
---|
| 57 | |
---|
[a098fab] | 58 | if (defined($opts{'d'})) { |
---|
| 59 | $debug = 1; |
---|
| 60 | } |
---|
[c9f5490] | 61 | |
---|
[a098fab] | 62 | if (defined($opts{'f'})) { |
---|
| 63 | $filename = $opts{'f'}; |
---|
[c9f5490] | 64 | } |
---|
| 65 | |
---|
| 66 | if (defined($opts{'r'})) { |
---|
| 67 | $remote = 1; |
---|
| 68 | die "$usage" if ($#ARGV != 1); |
---|
| 69 | |
---|
| 70 | $count = pop @ARGV; |
---|
| 71 | $addr = pop @ARGV; |
---|
| 72 | } |
---|
| 73 | |
---|
[a098fab] | 74 | die "$usage" if (!defined($remote) && !defined($filename)); |
---|
[c9f5490] | 75 | |
---|
[a098fab] | 76 | if (defined($filename)) { |
---|
| 77 | &parse_config("$filename", \%opts) || |
---|
| 78 | die "Cannot read config file $filename: $!\n"; |
---|
[c9f5490] | 79 | |
---|
[cd3eceb] | 80 | foreach my $opt (@expected_opts) { |
---|
| 81 | warn "Missing $opt option\n" if (!defined($opts{$opt})); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | $active = 1 if ($opts{'active'} =~ /true/i); |
---|
| 85 | $tunnelcfg = 1 if ($opts{'tunnelcfg'} =~ /true/i); |
---|
| 86 | $type = $opts{'type'}; |
---|
| 87 | $type =~ tr/A-Z/a-z/; |
---|
[c9f5490] | 88 | |
---|
[cd3eceb] | 89 | if (defined($opts{'fsname'})) { |
---|
[a098fab] | 90 | $ssh_port_fwds = "-R :139:$opts{'fsname'}:139 "; |
---|
[cd3eceb] | 91 | } |
---|
[c9f5490] | 92 | |
---|
[cd3eceb] | 93 | if (defined($opts{'bossname'})) { |
---|
[a098fab] | 94 | $ssh_port_fwds .= "-R :7777:$opts{'bossname'}:7777 "; |
---|
[cd3eceb] | 95 | } |
---|
[c9f5490] | 96 | |
---|
[cd3eceb] | 97 | $ssh_port_fwds = "" if ($opts{'type'} eq 'experiment'); |
---|
[c9f5490] | 98 | |
---|
[cd3eceb] | 99 | print "ssh_port_fwds = $ssh_port_fwds\n" if ($debug); |
---|
| 100 | } |
---|
[c9f5490] | 101 | |
---|
[a098fab] | 102 | # Need these to make the Ethernet tap and bridge to work... |
---|
[c9f5490] | 103 | |
---|
[a098fab] | 104 | system("kldload /boot/kernel/if_bridge.ko"); |
---|
| 105 | system("kldload /boot/kernel/if_tap.ko"); |
---|
[c9f5490] | 106 | |
---|
[cd3eceb] | 107 | if ($tunnelcfg && !$remote) { |
---|
[a098fab] | 108 | # Most Emulab-like testbeds use globally-routable addresses on the |
---|
| 109 | # control net; at DETER, we isolate the control net from the Internet. |
---|
| 110 | # On DETER-like testbeds, we need to create special tunneling nodes |
---|
| 111 | # with external access. Set up the external addresses as necessary. |
---|
| 112 | &setup_tunnel_cfg(%opts); |
---|
[c9f5490] | 113 | } |
---|
| 114 | |
---|
| 115 | if (!$remote) { |
---|
[a098fab] | 116 | system("umask 077 && cp $opts{'privkeys'} /root/.ssh/id_rsa"); |
---|
| 117 | system("umask 077 && cp $opts{'pubkeys'} /root/.ssh/id_rsa.pub"); |
---|
| 118 | system("umask 077 && cat $opts{'pubkeys'} >> /root/.ssh/authorized_keys"); |
---|
| 119 | } |
---|
[c9f5490] | 120 | |
---|
[cd3eceb] | 121 | if ($active) { |
---|
[a098fab] | 122 | # If we're the initiator, open up a separate tunnel to the remote |
---|
| 123 | # host for each of the different experiment net interfaces on this |
---|
| 124 | # machine. Execute this startup script on the far end, but with |
---|
| 125 | # the -r option to indicate that it's getting invoked remotely and |
---|
| 126 | # we should just handle the remote-end tasks. |
---|
[c9f5490] | 127 | |
---|
[a098fab] | 128 | # Set up synchronization, so that the various user machines won't try to |
---|
| 129 | # contact boss before the tunnels are set up. (Thanks jjh!) |
---|
[c9f5490] | 130 | |
---|
[a098fab] | 131 | do { |
---|
| 132 | system("$NC -z $opts{'peer'} $SSH_PORT"); |
---|
| 133 | } until (!$?); |
---|
[c9f5490] | 134 | |
---|
| 135 | # XXX: Do we need to clear out previously created bridge interfaces? |
---|
| 136 | |
---|
| 137 | my $count = 0; |
---|
[a098fab] | 138 | my @SSHCMD; |
---|
[c9f5490] | 139 | |
---|
| 140 | open(IFFILE, "/var/emulab/boot/ifmap") || die "couldn't open ifmap\n"; |
---|
| 141 | while (<IFFILE>) { |
---|
| 142 | my @a = split(' '); |
---|
| 143 | my $iface = $a[0]; |
---|
| 144 | my $addr = $a[1]; |
---|
| 145 | my $bridge = "bridge" . $count; |
---|
| 146 | my $tun = "tap" . $count; |
---|
| 147 | |
---|
| 148 | print "Found $iface, $addr, to bridge on $bridge\n" if ($debug); |
---|
| 149 | |
---|
[a098fab] | 150 | # Note that we're going to fire off an ssh which will never return; |
---|
| 151 | # we need the connection to stay up to keep the tunnel up. |
---|
| 152 | # In order to check for problems, we open it this way and read |
---|
| 153 | # the expected single line of output when the tunnel is connected. |
---|
[c9f5490] | 154 | |
---|
[a098fab] | 155 | open($SSHCMD[$count], "$SSH -w $count:$count $ssh_port_fwds -o \"StrictHostKeyChecking no\" $opts{'peer'} \"./fed-tun.pl -r $addr $count\" |") or die "Failed to run ssh"; |
---|
[c9f5490] | 156 | |
---|
[a098fab] | 157 | my $check = <$SSHCMD[$count]>; # Make sure something ran... |
---|
[c9f5490] | 158 | |
---|
[a098fab] | 159 | &setup_bridging($tun, $bridge, $iface, $addr); |
---|
[c9f5490] | 160 | $count++; |
---|
[a098fab] | 161 | $ssh_port_fwds = ""; # only do this on the first connection |
---|
[c9f5490] | 162 | } |
---|
| 163 | close(IFFILE); |
---|
[a098fab] | 164 | } elsif ($remote) { |
---|
[c9f5490] | 165 | # We're on the remote system; for now, we just grab and use |
---|
| 166 | # the one experimental interface. Later, we'll actually look |
---|
| 167 | # at the address passed to match up the appropriate interfaces. |
---|
| 168 | |
---|
| 169 | my $iter = 0; |
---|
| 170 | |
---|
| 171 | open(IFFILE, "/var/emulab/boot/ifmap") || die "couldn't open ifmap\n"; |
---|
| 172 | while (<IFFILE>) { |
---|
| 173 | die "Argh, too many experimental interfaces!" if ($iter > 0); |
---|
| 174 | my @a = split(' '); |
---|
| 175 | my $iface = $a[0]; |
---|
| 176 | my $addr = $a[1]; |
---|
| 177 | my $bridge = "bridge" . $count; |
---|
| 178 | my $tun = "tap" . $count; |
---|
| 179 | |
---|
[a098fab] | 180 | &setup_bridging($tun, $bridge, $iface, $addr); |
---|
[c9f5490] | 181 | $iter++; |
---|
| 182 | } |
---|
| 183 | close(IFFILE); |
---|
[a098fab] | 184 | |
---|
| 185 | print "Remote connection all set up!\n"; # Trigger other end with output |
---|
| 186 | } else { |
---|
| 187 | print "inactive end of a connection, finishing" if ($debug); |
---|
[c9f5490] | 188 | } |
---|
| 189 | |
---|
| 190 | print "all done!\n" if ($debug); |
---|
| 191 | exit; |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | # Set up the bridging for the new stuff... |
---|
| 195 | |
---|
[a098fab] | 196 | sub setup_bridging($; $; $; $) { |
---|
[c9f5490] | 197 | my ($tun, $bridge, $iface, $addr) = @_; |
---|
| 198 | |
---|
| 199 | print "Got new $tun\n" if ($debug); |
---|
| 200 | print "setting up $bridge with $iface and nuking $addr\n" if ($debug); |
---|
| 201 | |
---|
| 202 | system("ifconfig $bridge create"); |
---|
| 203 | system("ifconfig $iface delete $addr"); |
---|
| 204 | system("ifconfig $bridge addm $iface up"); |
---|
| 205 | system("ifconfig $bridge addm $tun"); |
---|
| 206 | } |
---|
[a098fab] | 207 | |
---|
| 208 | # Set up tunnel info for DETER-like testbeds. |
---|
| 209 | |
---|
| 210 | sub setup_tunnel_cfg { |
---|
| 211 | my (%opts) = @_; |
---|
| 212 | my $tunnel_iface = "em0"; # XXX |
---|
| 213 | my $tunnel_ip; |
---|
| 214 | my $tunnel_mask; |
---|
| 215 | my $tunnel_mac; |
---|
| 216 | my $tunnel_router; |
---|
| 217 | |
---|
| 218 | print "Opening $TMCC tunnelip\n" if ($debug); |
---|
| 219 | |
---|
| 220 | open(TMCD, "$TMCC tunnelip |") || die "tmcc failed\n"; |
---|
| 221 | print "Opened $TMCC tunnelip\n" if ($debug); |
---|
| 222 | while (<TMCD>) { |
---|
| 223 | print "got one line from tmcc\n" if ($debug); |
---|
| 224 | print if ($debug); |
---|
| 225 | if (/^TUNNELIP=([0-9.]*) TUNNELMASK=([0-9.]*) TUNNELMAC=(\w*) TUNNELROUTER=([0-9.]*)$/) { |
---|
| 226 | $tunnel_ip = $1; |
---|
| 227 | $tunnel_mask = $2; |
---|
| 228 | $tunnel_mac = $3; |
---|
| 229 | $tunnel_router = $4; |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | close(TMCD); |
---|
| 233 | |
---|
| 234 | die "Unable to determine tunnel node configuration information" |
---|
| 235 | if (!defined($tunnel_router)); |
---|
| 236 | |
---|
| 237 | print "tunnel options: ip=$tunnel_ip mask=$tunnel_mask mac=$tunnel_mac router=$tunnel_router\n" if ($debug); |
---|
| 238 | |
---|
| 239 | # Sadly, we ignore the tunnel mac for now -- we should eventually |
---|
| 240 | # use it to determine which interface to use, just like the |
---|
| 241 | # Emulab startup scripts. |
---|
| 242 | |
---|
| 243 | system("ifconfig $tunnel_iface $tunnel_ip" . |
---|
| 244 | ($tunnel_mask ? " netmask $tunnel_mask" : "")); |
---|
| 245 | warn "configuration of tunnel interface failed" if ($?); |
---|
| 246 | |
---|
| 247 | system("route add $opts{'peer'} $tunnel_router"); |
---|
| 248 | warn "configuration routes via tunnel interface failed" if ($?); |
---|
| 249 | |
---|
| 250 | print "setup_tunnel_cfg done\n" if ($debug); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | # Trick config-file parsing code from Ted Faber: |
---|
| 254 | |
---|
| 255 | # Parse the config file. The format is a colon-separated parameter name |
---|
| 256 | # followed by the value of that parameter to the end of the line. This parses |
---|
| 257 | # that format and puts the parameters into the referenced hash. Parameter |
---|
| 258 | # names are mapped to lower case, parameter values are unchanged. Returns 0 on |
---|
| 259 | # failure (e.g. file open) and 1 on success. |
---|
| 260 | sub parse_config { |
---|
| 261 | my($file, $href) = @_; |
---|
| 262 | my($fh) = new IO::File($file); |
---|
| 263 | |
---|
| 264 | unless ($fh) { |
---|
| 265 | warn "Can't open $file: $!\n"; |
---|
| 266 | return 0; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | while (<$fh>) { |
---|
| 270 | next if /^\s*#/ || /^\s*$/; # Skip comments & blanks |
---|
| 271 | chomp; |
---|
| 272 | /^([^:]+):\s*(.*)/ && do { |
---|
| 273 | my($key) = $1; |
---|
| 274 | |
---|
| 275 | $key =~ tr/A-Z/a-z/; |
---|
| 276 | $href->{$key} = $2; |
---|
| 277 | next; |
---|
| 278 | }; |
---|
| 279 | warn "Unparasble line in $file: $_\n"; |
---|
| 280 | } |
---|
| 281 | $fh->close(); # It will close when it goes out of scope, but... |
---|
| 282 | return 1; |
---|
| 283 | } |
---|