blob: 9782fca2ca5b7dae9b063bc101a7f10f8780689e [file] [log] [blame]
Taylor Hutt9963dc12011-03-08 09:52:23 -08001# 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
5import logging, re, time
6from autotest_lib.client.common_lib import error
Paul Stewart2ee7fdf2011-05-19 16:29:23 -07007from autotest_lib.server import site_linux_system
Taylor Hutt9963dc12011-03-08 09:52:23 -08008
Paul Stewart2ee7fdf2011-05-19 16:29:23 -07009class LinuxServer(site_linux_system.LinuxSystem):
Taylor Hutt9963dc12011-03-08 09:52:23 -080010 """
11 Linux Server: A machine which hosts network services.
12
13 """
14
Taylor Hutt0084cba2011-04-08 06:45:57 -070015 def __init__(self, server, wifi_ip):
Paul Stewart2ee7fdf2011-05-19 16:29:23 -070016 site_linux_system.LinuxSystem.__init__(self, server, {}, "server")
17
Taylor Hutt0084cba2011-04-08 06:45:57 -070018 self.server = server # Server host.
19 self.vpn_kind = None
20 self.wifi_ip = wifi_ip
21 self.openvpn_config = {}
Taylor Hutt9963dc12011-03-08 09:52:23 -080022
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 Hutt0084cba2011-04-08 06:45:57 -070036 l2tpipsec (StrongSwan PSK or certificates)
Taylor Hutt9963dc12011-03-08 09:52:23 -080037
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 Hutt9963dc12011-03-08 09:52:23 -080050 self.vpn_kind = params.get('kind', None)
51
Taylor Hutt9963dc12011-03-08 09:52:23 -080052 # Launch specified VPN server.
53 if self.vpn_kind is None:
Taylor Hutt0084cba2011-04-08 06:45:57 -070054 raise error.TestFail('No VPN kind specified for this test.')
Taylor Hutt9963dc12011-03-08 09:52:23 -080055 elif self.vpn_kind == 'openvpn':
Taylor Hutt0084cba2011-04-08 06:45:57 -070056 # 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 &")
64 elif self.vpn_kind == 'l2tpipsec':
65
66 configs = { "/etc/ipsec.conf" :
67 "config setup\n"
68 " charonstart=no\n"
69 " plutostart=yes\n"
70 " plutodebug=%(@plutodebug@)s\n"
71 "conn L2TP\n"
72 " keyexchange=ikev1\n"
73 " authby=psk\n"
74 " pfs=no\n"
75 " rekey=no\n"
76 " left=%(@local-listen-ip@)s\n"
77 " leftprotoport=17/1701\n"
78 " right=%%any\n"
79 " rightprotoport=17/%%any\n"
80 " auto=add\n",
81
82 "/etc/ipsec.secrets" :
83 "%(@ipsec-secrets@)s %%any : PSK \"password\"",
84
85 "/etc/xl2tpd/xl2tpd.conf" :
86 "[global]\n"
87 "\n"
88 "[lns default]\n"
89 " ip range = 192.168.1.128-192.168.1.254\n"
90 " local ip = 192.168.1.99\n"
91 " require chap = yes\n"
92 " refuse pap = yes\n"
93 " require authentication = yes\n"
94 " name = LinuxVPNserver\n"
95 " ppp debug = yes\n"
96 " pppoptfile = /etc/ppp/options.xl2tpd\n"
97 " length bit = yes\n",
98
99 "/etc/xl2tpd/l2tp-secrets" :
100 "* them l2tp-secret",
101
102 "/etc/ppp/chap-secrets" :
103 "chapuser * chapsecret *",
104
105 "/etc/ppp/options.xl2tpd" :
106 "ipcp-accept-local\n"
107 "ipcp-accept-remote\n"
108 "noccp\n"
109 "auth\n"
110 "crtscts\n"
111 "idle 1800\n"
112 "mtu 1410\n"
113 "mru 1410\n"
114 "nodefaultroute\n"
115 "debug\n"
116 "lock\n"
117 "proxyarp\n"
118 "connect-delay 5000\n"
119 }
120
121 replacements = params.get("replacements", {})
122 # These two replacements must match up to the same
123 # adapter, or a connection will not be established.
124 replacements["@local-listen-ip@"] = "%defaultroute"
125 replacements["@ipsec-secrets@"] = self.server.ip
126
127 for cfg, template in configs.iteritems():
128 contents = template % (replacements)
129 self.server.run("cat <<EOF >%s\n%s\nEOF\n" % (cfg, contents))
130
131 self.server.run("/usr/sbin/ipsec start")
132
133 # Restart xl2tpd to ensure use of newly-created config files.
134 self.server.run("sh /etc/init.d/xl2tpd restart")
Taylor Hutt9963dc12011-03-08 09:52:23 -0800135 else:
136 raise error.TestFail('(internal error): No config case '
137 'for VPN kind (%s)' % self.vpn_kind)
138
139 def vpn_server_kill(self, params):
140 """ Kill the VPN server. """
141 if self.vpn_kind is not None:
142 if self.vpn_kind == 'openvpn':
143 self.server.run("pkill /usr/sbin/openvpn")
Taylor Hutt0084cba2011-04-08 06:45:57 -0700144 elif self.vpn_kind == 'l2tpipsec':
145 self.server.run("/usr/sbin/ipsec stop")
Taylor Hutt9963dc12011-03-08 09:52:23 -0800146 else:
147 raise error.TestFail('(internal error): No kill case '
148 'for VPN kind (%s)' % self.vpn_kind)
Taylor Hutt0084cba2011-04-08 06:45:57 -0700149 self.vpn_kind = None