Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(604)

Side by Side Diff: remoting/host/gnubby_auth_handler_posix.cc

Issue 138753005: Add gnubby authentication to remoting host (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address latest review comments Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium 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 #include "remoting/host/gnubby_auth_handler.h"
6
7 #include "base/file_util.h"
8 #include "base/json/json_reader.h"
9 #include "base/lazy_instance.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "net/socket/unix_domain_socket_posix.h"
14 #include "remoting/base/logging.h"
15 #include "remoting/host/gnubby_util.h"
16 #include "remoting/proto/control.pb.h"
17 #include "remoting/protocol/client_stub.h"
18
19 namespace remoting {
20
21 namespace {
22
23 class GnubbyAuthHandlerPosix : public GnubbyAuthHandler,
24 public base::NonThreadSafe,
25 public net::StreamListenSocket::Delegate {
26 public:
27 explicit GnubbyAuthHandlerPosix(protocol::ClientStub* client_stub);
28 virtual ~GnubbyAuthHandlerPosix();
29
30 private:
31 // GnubbyAuthHandler interface.
32 virtual void DeliverClientMessage(const std::string& message) OVERRIDE;
33 virtual void DeliverHostDataMessage(int connection_id,
34 const std::string& data) const OVERRIDE;
35
36 // StreamListenSocket::Delegate interface.
37 virtual void DidAccept(net::StreamListenSocket* server,
38 scoped_ptr<net::StreamListenSocket> socket) OVERRIDE;
39 virtual void DidRead(net::StreamListenSocket* socket,
40 const char* data,
41 int len) OVERRIDE;
42 virtual void DidClose(net::StreamListenSocket* socket) OVERRIDE;
43
44 // Create socket for authorization.
45 void CreateAuthorizationSocket();
46
47 // Socket used to listen for authorization requests.
48 scoped_ptr<net::StreamListenSocket> auth_socket_;
49
50 // The last assigned gnubby connection id.
51 int last_connection_id_;
52
53 // Socket and connection id pairs used to process gnubbyd requests
54 typedef std::vector<std::pair<net::StreamListenSocket*, int> > ActiveSockets;
55 ActiveSockets active_sockets_;
56
57 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerPosix);
58 };
59
60 // The name of the socket to listen for gnubby requests on.
61 base::LazyInstance<base::FilePath>::Leaky g_gnubby_socket_name =
62 LAZY_INSTANCE_INITIALIZER;
63
64 // STL predicate to match by a StreamListenSocket pointer.
65 class CompareSocket {
66 public:
67 explicit CompareSocket(net::StreamListenSocket* socket) : socket_(socket) {}
68
69 bool operator()(const std::pair<net::StreamListenSocket*, int> element)
70 const {
71 return socket_ == element.first;
72 }
73
74 private:
75 net::StreamListenSocket* socket_;
76 };
77
78 // STL predicate to match by a connection id.
79 class CompareConnection {
80 public:
81 explicit CompareConnection(int connection_id)
82 : connection_id_(connection_id) {}
83
84 bool operator()(const std::pair<net::StreamListenSocket*, int> element)
85 const {
86 return connection_id_ == element.second;
87 }
88
89 private:
90 int connection_id_;
91 };
92
93 } // namespace
94
95 GnubbyAuthHandlerPosix::GnubbyAuthHandlerPosix(
96 protocol::ClientStub* client_stub)
97 : GnubbyAuthHandler(client_stub), last_connection_id_(0) {}
98
99 GnubbyAuthHandlerPosix::~GnubbyAuthHandlerPosix() {
100 STLDeleteContainerPairFirstPointers(active_sockets_.begin(),
101 active_sockets_.end());
102 }
103
104 // static
105 scoped_ptr<GnubbyAuthHandler> GnubbyAuthHandler::Create(
106 protocol::ClientStub* client_stub) {
107 return scoped_ptr<GnubbyAuthHandler>(new GnubbyAuthHandlerPosix(client_stub));
108 }
109
110 // static
111 void GnubbyAuthHandler::SetGnubbySocketName(
112 const base::FilePath& gnubby_socket_name) {
113 g_gnubby_socket_name.Get() = gnubby_socket_name;
114 }
115
116 void GnubbyAuthHandlerPosix::DeliverClientMessage(const std::string& message) {
117 DCHECK(CalledOnValidThread());
118
119 scoped_ptr<base::Value> value(base::JSONReader::Read(message));
120 base::ListValue* list = NULL;
121 bool result = value->GetAsList(&list);
Sergey Ulanov 2014/02/11 08:20:38 value may be NULL here if |message| is not valid J
psj 2014/02/12 09:01:01 Ack
122
123 std::string auth_message_type;
124 result = result && list->GetString(0, &auth_message_type);
125 if (result && auth_message_type == "control") {
126 std::string payload;
127 result = list->GetString(1, &payload);
128 if (result && payload == "auth-v1") {
129 CreateAuthorizationSocket();
130 }
131 } else if (result && auth_message_type == "data") {
132 std::string connection_id_str;
133 std::string payload;
134 result = list->GetString(1, &connection_id_str);
135 result = result && list->GetString(2, &payload);
136 if (result) {
137 int connection_id = 0;
138 base::StringToInt(connection_id_str, &connection_id);
139
140 ActiveSockets::iterator iter =
141 std::find_if(active_sockets_.begin(),
142 active_sockets_.end(),
143 CompareConnection(connection_id));
144 if (iter != active_sockets_.end()) {
145 HOST_LOG << "Sending gnubby response";
146
147 std::string reply;
148 GetGnubbyResponseFromJson(payload, &reply);
149
150 iter->first->Send(reply);
151 }
152 }
153 }
154 }
155
156 void GnubbyAuthHandlerPosix::DeliverHostDataMessage(int connection_id,
157 const std::string& data)
158 const {
159 DCHECK(CalledOnValidThread());
160
161 protocol::ExtensionMessage message;
162 message.set_type("gnubby-auth");
163 message.set_data("data " + base::IntToString(connection_id) + " " + data);
164
165 client_stub_->DeliverHostMessage(message);
166 }
167
168 void GnubbyAuthHandlerPosix::DidAccept(
169 net::StreamListenSocket* server,
170 scoped_ptr<net::StreamListenSocket> socket) {
171 DCHECK(CalledOnValidThread());
172
173 active_sockets_.push_back(
174 std::make_pair(socket.release(), ++last_connection_id_));
175 }
176
177 void GnubbyAuthHandlerPosix::DidRead(net::StreamListenSocket* socket,
178 const char* data,
179 int len) {
180 DCHECK(CalledOnValidThread());
181
182 ActiveSockets::iterator iter = std::find_if(
183 active_sockets_.begin(), active_sockets_.end(), CompareSocket(socket));
184 if (iter != active_sockets_.end()) {
185 std::string json;
186 if (GetJsonFromGnubbyRequest(data, len, &json)) {
Sergey Ulanov 2014/02/11 08:20:38 Given that the |socket| is a stream socket can it
psj 2014/02/12 09:01:01 No idea. The message easily fits in the socket rea
Sergey Ulanov 2014/02/14 07:31:59 Right. So it looks like this code will work proper
psj 2014/02/15 00:01:33 Yes, I already implemented this. See active_reques
187 HOST_LOG << "Received gnubby request";
188 DeliverHostDataMessage(iter->second, json);
189 } else {
190 HOST_LOG << "Could not decode blob";
Sergey Ulanov 2014/02/11 08:20:38 Maybe "Could not decode gnubby request" to make it
psj 2014/02/12 09:01:01 Done.
191 }
192 }
193 }
194
195 void GnubbyAuthHandlerPosix::DidClose(net::StreamListenSocket* socket) {
196 DCHECK(CalledOnValidThread());
197
198 ActiveSockets::iterator iter = std::find_if(
199 active_sockets_.begin(), active_sockets_.end(), CompareSocket(socket));
200 if (iter != active_sockets_.end()) {
201 delete iter->first;
202 active_sockets_.erase(iter);
203 }
204 }
205
206 void GnubbyAuthHandlerPosix::CreateAuthorizationSocket() {
207 DCHECK(CalledOnValidThread());
208
209 if (!g_gnubby_socket_name.Get().empty()) {
210 // If the file already exists, a socket in use error is returned.
211 base::DeleteFile(g_gnubby_socket_name.Get(), false);
212
213 HOST_LOG << "Listening for gnubby requests on "
214 << g_gnubby_socket_name.Get().value();
215
216 auth_socket_ = net::UnixDomainSocket::CreateAndListen(
217 g_gnubby_socket_name.Get().value(),
218 this,
219 net::UnixDomainSocket::NoAuthentication());
Sergey Ulanov 2014/02/11 08:20:38 Is it acceptable to allow other users on this mach
psj 2014/02/12 09:01:01 Done.
220 if (!auth_socket_.get()) {
221 HOST_LOG << "Failed to open socket for gnubby requests";
222 }
223 } else {
224 HOST_LOG << "No gnubby socket name specified";
225 }
226 }
227
228 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698