Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import logging, re, time |
| 6 | from autotest_lib.client.common_lib import error |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 7 | from autotest_lib.server import site_linux_system |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 8 | |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 9 | class LinuxServer(site_linux_system.LinuxSystem): |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 10 | """ |
| 11 | Linux Server: A machine which hosts network services. |
| 12 | |
| 13 | """ |
| 14 | |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 15 | def __init__(self, server, wifi_ip): |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 16 | site_linux_system.LinuxSystem.__init__(self, server, {}, "server") |
| 17 | |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 18 | self.server = server # Server host. |
| 19 | self.vpn_kind = None |
| 20 | self.wifi_ip = wifi_ip |
| 21 | self.openvpn_config = {} |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 22 | |
| 23 | def vpn_server_config(self, params): |
| 24 | """ Configure & launch the server side of the VPN. |
| 25 | |
| 26 | Parameters, in 'params': |
| 27 | |
| 28 | kind : required |
| 29 | |
| 30 | The kind of VPN which should be configured and |
| 31 | launched. |
| 32 | |
| 33 | Valid values: |
| 34 | |
| 35 | openvpn |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 36 | l2tpipsec (StrongSwan PSK or certificates) |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 37 | |
| 38 | config: required |
| 39 | |
| 40 | The configuration information associated with |
| 41 | the VPN server. |
| 42 | |
| 43 | This is a dict which contains key/value pairs |
| 44 | representing the VPN's configuration. |
| 45 | |
| 46 | The values stored in the 'config' param must all be |
| 47 | supported by the specified VPN kind. |
| 48 | """ |
| 49 | self.vpn_server_kill({}) # Must be first. Relies on self.vpn_kind. |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 50 | self.vpn_kind = params.get('kind', None) |
| 51 | |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 52 | # Launch specified VPN server. |
| 53 | if self.vpn_kind is None: |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 54 | raise error.TestFail('No VPN kind specified for this test.') |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 55 | elif self.vpn_kind == 'openvpn': |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 56 | # Read config information & create server configuration file. |
| 57 | for k, v in params.get('config', {}).iteritems(): |
| 58 | self.openvpn_config[k] = v |
| 59 | self.server.run("cat <<EOF >/tmp/vpn-server.conf\n%s\nEOF\n" % |
| 60 | ('\n'.join( "%s %s" % kv for kv in |
| 61 | self.openvpn_config.iteritems()))) |
| 62 | self.server.run("/usr/sbin/openvpn " |
| 63 | "--config /tmp/vpn-server.conf &") |
James Simonsen | 4c154f0 | 2011-05-26 15:50:00 -0700 | [diff] [blame] | 64 | elif self.vpn_kind in ('l2tpipsec-psk', 'l2tpipsec-cert'): |
| 65 | configs = { |
| 66 | "/etc/xl2tpd/xl2tpd.conf" : |
| 67 | "[global]\n" |
| 68 | "\n" |
| 69 | "[lns default]\n" |
| 70 | " ip range = 192.168.1.128-192.168.1.254\n" |
| 71 | " local ip = 192.168.1.99\n" |
| 72 | " require chap = yes\n" |
| 73 | " refuse pap = yes\n" |
| 74 | " require authentication = yes\n" |
| 75 | " name = LinuxVPNserver\n" |
| 76 | " ppp debug = yes\n" |
| 77 | " pppoptfile = /etc/ppp/options.xl2tpd\n" |
| 78 | " length bit = yes\n", |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 79 | |
James Simonsen | 4c154f0 | 2011-05-26 15:50:00 -0700 | [diff] [blame] | 80 | "/etc/xl2tpd/l2tp-secrets" : |
| 81 | "* them l2tp-secret", |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 82 | |
James Simonsen | 4c154f0 | 2011-05-26 15:50:00 -0700 | [diff] [blame] | 83 | "/etc/ppp/chap-secrets" : |
| 84 | "chapuser * chapsecret *", |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 85 | |
James Simonsen | 4c154f0 | 2011-05-26 15:50:00 -0700 | [diff] [blame] | 86 | "/etc/ppp/options.xl2tpd" : |
| 87 | "ipcp-accept-local\n" |
| 88 | "ipcp-accept-remote\n" |
| 89 | "noccp\n" |
| 90 | "auth\n" |
| 91 | "crtscts\n" |
| 92 | "idle 1800\n" |
| 93 | "mtu 1410\n" |
| 94 | "mru 1410\n" |
| 95 | "nodefaultroute\n" |
| 96 | "debug\n" |
| 97 | "lock\n" |
| 98 | "proxyarp\n" |
| 99 | "connect-delay 5000\n" |
| 100 | } |
| 101 | config_choices = { |
| 102 | 'l2tpipsec-psk': { |
| 103 | "/etc/ipsec.conf" : |
| 104 | "config setup\n" |
| 105 | " charonstart=no\n" |
| 106 | " plutostart=yes\n" |
| 107 | " plutodebug=%(@plutodebug@)s\n" |
Ken Mixter | 2c2ac6d | 2011-08-24 16:37:54 -0700 | [diff] [blame] | 108 | " plutostderrlog=/var/log/pluto.log\n" |
James Simonsen | 4c154f0 | 2011-05-26 15:50:00 -0700 | [diff] [blame] | 109 | "conn L2TP\n" |
| 110 | " keyexchange=ikev1\n" |
| 111 | " authby=psk\n" |
| 112 | " pfs=no\n" |
| 113 | " rekey=no\n" |
| 114 | " left=%(@local-listen-ip@)s\n" |
| 115 | " leftprotoport=17/1701\n" |
| 116 | " right=%%any\n" |
| 117 | " rightprotoport=17/%%any\n" |
| 118 | " auto=add\n", |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 119 | |
James Simonsen | 4c154f0 | 2011-05-26 15:50:00 -0700 | [diff] [blame] | 120 | "/etc/ipsec.secrets" : |
| 121 | "%(@ipsec-secrets@)s %%any : PSK \"password\"", |
| 122 | }, |
| 123 | 'l2tpipsec-cert': { |
| 124 | "/etc/ipsec.conf" : |
| 125 | "config setup\n" |
| 126 | " charonstart=no\n" |
| 127 | " plutostart=yes\n" |
| 128 | " plutodebug=%(@plutodebug@)s\n" |
Ken Mixter | 2c2ac6d | 2011-08-24 16:37:54 -0700 | [diff] [blame] | 129 | " plutostderrlog=/var/log/pluto.log\n" |
James Simonsen | 4c154f0 | 2011-05-26 15:50:00 -0700 | [diff] [blame] | 130 | "conn L2TP\n" |
| 131 | " keyexchange=ikev1\n" |
| 132 | " left=%(@local-listen-ip@)s\n" |
| 133 | " leftcert=server.crt\n" |
| 134 | " leftid=\"C=US, ST=California, L=Mountain View, " |
| 135 | "CN=chromelab-wifi-testbed-server.mtv.google.com\"\n" |
| 136 | " leftprotoport=17/1701\n" |
| 137 | " right=%%any\n" |
| 138 | " rightca=\"C=US, ST=California, L=Mountain View, " |
| 139 | "CN=chromelab-wifi-testbed-root.mtv.google.com\"\n" |
| 140 | " rightprotoport=17/%%any\n" |
| 141 | " auto=add\n" |
| 142 | " pfs=no\n", |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 143 | |
James Simonsen | 4c154f0 | 2011-05-26 15:50:00 -0700 | [diff] [blame] | 144 | "/etc/ipsec.secrets" : ": RSA server.key \"\"\n", |
| 145 | }, |
| 146 | } |
| 147 | configs.update(config_choices[self.vpn_kind]) |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 148 | |
| 149 | replacements = params.get("replacements", {}) |
| 150 | # These two replacements must match up to the same |
| 151 | # adapter, or a connection will not be established. |
| 152 | replacements["@local-listen-ip@"] = "%defaultroute" |
| 153 | replacements["@ipsec-secrets@"] = self.server.ip |
| 154 | |
| 155 | for cfg, template in configs.iteritems(): |
| 156 | contents = template % (replacements) |
| 157 | self.server.run("cat <<EOF >%s\n%s\nEOF\n" % (cfg, contents)) |
| 158 | |
Ken Mixter | 2c2ac6d | 2011-08-24 16:37:54 -0700 | [diff] [blame] | 159 | self.server.run("/usr/sbin/ipsec restart") |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 160 | |
| 161 | # Restart xl2tpd to ensure use of newly-created config files. |
| 162 | self.server.run("sh /etc/init.d/xl2tpd restart") |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 163 | else: |
| 164 | raise error.TestFail('(internal error): No config case ' |
| 165 | 'for VPN kind (%s)' % self.vpn_kind) |
| 166 | |
| 167 | def vpn_server_kill(self, params): |
| 168 | """ Kill the VPN server. """ |
| 169 | if self.vpn_kind is not None: |
| 170 | if self.vpn_kind == 'openvpn': |
| 171 | self.server.run("pkill /usr/sbin/openvpn") |
James Simonsen | 4c154f0 | 2011-05-26 15:50:00 -0700 | [diff] [blame] | 172 | elif self.vpn_kind in ('l2tpipsec-psk', 'l2tpipsec-cert'): |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 173 | self.server.run("/usr/sbin/ipsec stop") |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 174 | else: |
| 175 | raise error.TestFail('(internal error): No kill case ' |
| 176 | 'for VPN kind (%s)' % self.vpn_kind) |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 177 | self.vpn_kind = None |