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 |
| 7 | |
| 8 | class LinuxServer(object): |
| 9 | """ |
| 10 | Linux Server: A machine which hosts network services. |
| 11 | |
| 12 | """ |
| 13 | |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 14 | def __init__(self, server, wifi_ip): |
| 15 | self.server = server # Server host. |
| 16 | self.vpn_kind = None |
| 17 | self.wifi_ip = wifi_ip |
| 18 | self.openvpn_config = {} |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 19 | |
| 20 | def vpn_server_config(self, params): |
| 21 | """ Configure & launch the server side of the VPN. |
| 22 | |
| 23 | Parameters, in 'params': |
| 24 | |
| 25 | kind : required |
| 26 | |
| 27 | The kind of VPN which should be configured and |
| 28 | launched. |
| 29 | |
| 30 | Valid values: |
| 31 | |
| 32 | openvpn |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 33 | l2tpipsec (StrongSwan PSK or certificates) |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 34 | |
| 35 | config: required |
| 36 | |
| 37 | The configuration information associated with |
| 38 | the VPN server. |
| 39 | |
| 40 | This is a dict which contains key/value pairs |
| 41 | representing the VPN's configuration. |
| 42 | |
| 43 | The values stored in the 'config' param must all be |
| 44 | supported by the specified VPN kind. |
| 45 | """ |
| 46 | self.vpn_server_kill({}) # Must be first. Relies on self.vpn_kind. |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 47 | self.vpn_kind = params.get('kind', None) |
| 48 | |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 49 | # Launch specified VPN server. |
| 50 | if self.vpn_kind is None: |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 51 | raise error.TestFail('No VPN kind specified for this test.') |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 52 | elif self.vpn_kind == 'openvpn': |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 53 | # Read config information & create server configuration file. |
| 54 | for k, v in params.get('config', {}).iteritems(): |
| 55 | self.openvpn_config[k] = v |
| 56 | self.server.run("cat <<EOF >/tmp/vpn-server.conf\n%s\nEOF\n" % |
| 57 | ('\n'.join( "%s %s" % kv for kv in |
| 58 | self.openvpn_config.iteritems()))) |
| 59 | self.server.run("/usr/sbin/openvpn " |
| 60 | "--config /tmp/vpn-server.conf &") |
| 61 | elif self.vpn_kind == 'l2tpipsec': |
| 62 | |
| 63 | configs = { "/etc/ipsec.conf" : |
| 64 | "config setup\n" |
| 65 | " charonstart=no\n" |
| 66 | " plutostart=yes\n" |
| 67 | " plutodebug=%(@plutodebug@)s\n" |
| 68 | "conn L2TP\n" |
| 69 | " keyexchange=ikev1\n" |
| 70 | " authby=psk\n" |
| 71 | " pfs=no\n" |
| 72 | " rekey=no\n" |
| 73 | " left=%(@local-listen-ip@)s\n" |
| 74 | " leftprotoport=17/1701\n" |
| 75 | " right=%%any\n" |
| 76 | " rightprotoport=17/%%any\n" |
| 77 | " auto=add\n", |
| 78 | |
| 79 | "/etc/ipsec.secrets" : |
| 80 | "%(@ipsec-secrets@)s %%any : PSK \"password\"", |
| 81 | |
| 82 | "/etc/xl2tpd/xl2tpd.conf" : |
| 83 | "[global]\n" |
| 84 | "\n" |
| 85 | "[lns default]\n" |
| 86 | " ip range = 192.168.1.128-192.168.1.254\n" |
| 87 | " local ip = 192.168.1.99\n" |
| 88 | " require chap = yes\n" |
| 89 | " refuse pap = yes\n" |
| 90 | " require authentication = yes\n" |
| 91 | " name = LinuxVPNserver\n" |
| 92 | " ppp debug = yes\n" |
| 93 | " pppoptfile = /etc/ppp/options.xl2tpd\n" |
| 94 | " length bit = yes\n", |
| 95 | |
| 96 | "/etc/xl2tpd/l2tp-secrets" : |
| 97 | "* them l2tp-secret", |
| 98 | |
| 99 | "/etc/ppp/chap-secrets" : |
| 100 | "chapuser * chapsecret *", |
| 101 | |
| 102 | "/etc/ppp/options.xl2tpd" : |
| 103 | "ipcp-accept-local\n" |
| 104 | "ipcp-accept-remote\n" |
| 105 | "noccp\n" |
| 106 | "auth\n" |
| 107 | "crtscts\n" |
| 108 | "idle 1800\n" |
| 109 | "mtu 1410\n" |
| 110 | "mru 1410\n" |
| 111 | "nodefaultroute\n" |
| 112 | "debug\n" |
| 113 | "lock\n" |
| 114 | "proxyarp\n" |
| 115 | "connect-delay 5000\n" |
| 116 | } |
| 117 | |
| 118 | replacements = params.get("replacements", {}) |
| 119 | # These two replacements must match up to the same |
| 120 | # adapter, or a connection will not be established. |
| 121 | replacements["@local-listen-ip@"] = "%defaultroute" |
| 122 | replacements["@ipsec-secrets@"] = self.server.ip |
| 123 | |
| 124 | for cfg, template in configs.iteritems(): |
| 125 | contents = template % (replacements) |
| 126 | self.server.run("cat <<EOF >%s\n%s\nEOF\n" % (cfg, contents)) |
| 127 | |
| 128 | self.server.run("/usr/sbin/ipsec start") |
| 129 | |
| 130 | # Restart xl2tpd to ensure use of newly-created config files. |
| 131 | self.server.run("sh /etc/init.d/xl2tpd restart") |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 132 | else: |
| 133 | raise error.TestFail('(internal error): No config case ' |
| 134 | 'for VPN kind (%s)' % self.vpn_kind) |
| 135 | |
| 136 | def vpn_server_kill(self, params): |
| 137 | """ Kill the VPN server. """ |
| 138 | if self.vpn_kind is not None: |
| 139 | if self.vpn_kind == 'openvpn': |
| 140 | self.server.run("pkill /usr/sbin/openvpn") |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 141 | elif self.vpn_kind == 'l2tpipsec': |
| 142 | self.server.run("/usr/sbin/ipsec stop") |
Taylor Hutt | 9963dc1 | 2011-03-08 09:52:23 -0800 | [diff] [blame] | 143 | else: |
| 144 | raise error.TestFail('(internal error): No kill case ' |
| 145 | 'for VPN kind (%s)' % self.vpn_kind) |
Taylor Hutt | 0084cba | 2011-04-08 06:45:57 -0700 | [diff] [blame] | 146 | self.vpn_kind = None |