blob: d40cdf5e04fc3ca9a8eaefd48c137f4d99f0c4ee [file] [log] [blame]
Darin Petkov1c115202012-03-22 15:35:47 +01001// Copyright (c) 2012 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#ifndef SHILL_OPENVPN_MANAGEMENT_SERVER_
6#define SHILL_OPENVPN_MANAGEMENT_SERVER_
7
Darin Petkov46463022012-03-29 14:57:32 +02008#include <string>
9#include <vector>
10
Darin Petkov1c115202012-03-22 15:35:47 +010011#include <base/basictypes.h>
Darin Petkov78f63262012-03-26 01:30:24 +020012#include <base/cancelable_callback.h>
13#include <base/memory/weak_ptr.h>
Darin Petkov271fe522012-03-27 13:47:29 +020014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkov1c115202012-03-22 15:35:47 +010015
16namespace shill {
17
Darin Petkov78f63262012-03-26 01:30:24 +020018class EventDispatcher;
Darin Petkov683942b2012-03-27 18:00:04 +020019class GLib;
Darin Petkov78f63262012-03-26 01:30:24 +020020class InputData;
21class IOHandler;
Darin Petkov1c115202012-03-22 15:35:47 +010022class OpenVPNDriver;
Darin Petkov78f63262012-03-26 01:30:24 +020023class Sockets;
Darin Petkov1c115202012-03-22 15:35:47 +010024
25class OpenVPNManagementServer {
26 public:
Darin Petkov683942b2012-03-27 18:00:04 +020027 OpenVPNManagementServer(OpenVPNDriver *driver, GLib *glib);
Darin Petkov1c115202012-03-22 15:35:47 +010028 virtual ~OpenVPNManagementServer();
29
Darin Petkov46463022012-03-29 14:57:32 +020030 // Returns false on failure. On success, returns true and appends management
31 // interface openvpn options to |options|.
32 virtual bool Start(EventDispatcher *dispatcher,
33 Sockets *sockets,
34 std::vector<std::string> *options);
Darin Petkov78f63262012-03-26 01:30:24 +020035
Darin Petkov46463022012-03-29 14:57:32 +020036 virtual void Stop();
Darin Petkov1c115202012-03-22 15:35:47 +010037
38 private:
Darin Petkov271fe522012-03-27 13:47:29 +020039 friend class OpenVPNManagementServerTest;
40 FRIEND_TEST(OpenVPNManagementServerTest, OnInput);
41 FRIEND_TEST(OpenVPNManagementServerTest, OnReady);
42 FRIEND_TEST(OpenVPNManagementServerTest, OnReadyAcceptFail);
Darin Petkove0d5dd12012-04-04 16:10:48 +020043 FRIEND_TEST(OpenVPNManagementServerTest, ParseNeedPasswordTag);
Darin Petkov683942b2012-03-27 18:00:04 +020044 FRIEND_TEST(OpenVPNManagementServerTest, PerformStaticChallenge);
45 FRIEND_TEST(OpenVPNManagementServerTest, PerformStaticChallengeNoCreds);
Darin Petkov0440b9b2012-04-17 16:11:56 +020046 FRIEND_TEST(OpenVPNManagementServerTest, ProcessFailedPasswordMessage);
Darin Petkov271fe522012-03-27 13:47:29 +020047 FRIEND_TEST(OpenVPNManagementServerTest, ProcessInfoMessage);
48 FRIEND_TEST(OpenVPNManagementServerTest, ProcessMessage);
Darin Petkov683942b2012-03-27 18:00:04 +020049 FRIEND_TEST(OpenVPNManagementServerTest, ProcessNeedPasswordMessageAuthSC);
Darin Petkove0d5dd12012-04-04 16:10:48 +020050 FRIEND_TEST(OpenVPNManagementServerTest, ProcessNeedPasswordMessageTPMToken);
51 FRIEND_TEST(OpenVPNManagementServerTest, ProcessNeedPasswordMessageUnknown);
Darin Petkov271fe522012-03-27 13:47:29 +020052 FRIEND_TEST(OpenVPNManagementServerTest, ProcessStateMessage);
53 FRIEND_TEST(OpenVPNManagementServerTest, Send);
Darin Petkov683942b2012-03-27 18:00:04 +020054 FRIEND_TEST(OpenVPNManagementServerTest, SendPassword);
Darin Petkov271fe522012-03-27 13:47:29 +020055 FRIEND_TEST(OpenVPNManagementServerTest, SendState);
Darin Petkov683942b2012-03-27 18:00:04 +020056 FRIEND_TEST(OpenVPNManagementServerTest, SendUsername);
Darin Petkov271fe522012-03-27 13:47:29 +020057 FRIEND_TEST(OpenVPNManagementServerTest, Start);
58 FRIEND_TEST(OpenVPNManagementServerTest, Stop);
Darin Petkove0d5dd12012-04-04 16:10:48 +020059 FRIEND_TEST(OpenVPNManagementServerTest, SupplyTPMToken);
60 FRIEND_TEST(OpenVPNManagementServerTest, SupplyTPMTokenNoPIN);
Darin Petkov271fe522012-03-27 13:47:29 +020061
Darin Petkov78f63262012-03-26 01:30:24 +020062 // IO handler callbacks.
63 void OnReady(int fd);
64 void OnInput(InputData *data);
65
66 void Send(const std::string &data);
67 void SendState(const std::string &state);
Darin Petkov683942b2012-03-27 18:00:04 +020068 void SendUsername(const std::string &tag, const std::string &username);
69 void SendPassword(const std::string &tag, const std::string &password);
Darin Petkov78f63262012-03-26 01:30:24 +020070
71 void ProcessMessage(const std::string &message);
Darin Petkov271fe522012-03-27 13:47:29 +020072 bool ProcessInfoMessage(const std::string &message);
73 bool ProcessNeedPasswordMessage(const std::string &message);
74 bool ProcessFailedPasswordMessage(const std::string &message);
75 bool ProcessStateMessage(const std::string &message);
Darin Petkov78f63262012-03-26 01:30:24 +020076
Darin Petkove0d5dd12012-04-04 16:10:48 +020077 void PerformStaticChallenge(const std::string &tag);
78 void SupplyTPMToken(const std::string &tag);
79
80 static std::string ParseNeedPasswordTag(const std::string &message);
Darin Petkov683942b2012-03-27 18:00:04 +020081
Darin Petkov1c115202012-03-22 15:35:47 +010082 OpenVPNDriver *driver_;
Darin Petkov683942b2012-03-27 18:00:04 +020083 GLib *glib_;
Darin Petkov78f63262012-03-26 01:30:24 +020084 base::WeakPtrFactory<OpenVPNManagementServer> weak_ptr_factory_;
85 base::Callback<void(int)> ready_callback_;
86 base::Callback<void(InputData *)> input_callback_;
87
88 Sockets *sockets_;
89 int socket_;
90 scoped_ptr<IOHandler> ready_handler_;
91 EventDispatcher *dispatcher_;
92 int connected_socket_;
93 scoped_ptr<IOHandler> input_handler_;
Darin Petkov1c115202012-03-22 15:35:47 +010094
95 DISALLOW_COPY_AND_ASSIGN(OpenVPNManagementServer);
96};
97
98} // namespace shill
99
100#endif // SHILL_OPENVPN_MANAGEMENT_SERVER_