OLD | NEW |
(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 "net/socket/unix_domain_server_socket_posix.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <sys/socket.h> |
| 9 #include <sys/un.h> |
| 10 #include <unistd.h> |
| 11 |
| 12 #include "base/logging.h" |
| 13 #include "net/base/net_errors.h" |
| 14 #include "net/socket/socket_libevent.h" |
| 15 #include "net/socket/unix_domain_client_socket_posix.h" |
| 16 |
| 17 namespace net { |
| 18 |
| 19 UnixDomainServerSocket::UnixDomainServerSocket( |
| 20 const AuthCallback& auth_callback, |
| 21 bool use_abstract_namespace) |
| 22 : auth_callback_(auth_callback), |
| 23 use_abstract_namespace_(use_abstract_namespace) { |
| 24 } |
| 25 |
| 26 UnixDomainServerSocket::~UnixDomainServerSocket() { |
| 27 } |
| 28 |
| 29 // static |
| 30 bool UnixDomainServerSocket::GetPeerIds(SocketDescriptor socket, |
| 31 uid_t* user_id, |
| 32 gid_t* group_id) { |
| 33 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 34 struct ucred user_cred; |
| 35 socklen_t len = sizeof(user_cred); |
| 36 if (getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &user_cred, &len) < 0) |
| 37 return false; |
| 38 *user_id = user_cred.uid; |
| 39 *group_id = user_cred.gid; |
| 40 #else |
| 41 if (getpeereid(socket, user_id, group_id) < 0) |
| 42 return false; |
| 43 #endif |
| 44 return true; |
| 45 } |
| 46 |
| 47 int UnixDomainServerSocket::Listen(const IPEndPoint& address, int backlog) { |
| 48 NOTIMPLEMENTED(); |
| 49 return ERR_NOT_IMPLEMENTED; |
| 50 } |
| 51 |
| 52 int UnixDomainServerSocket::ListenWithAddressAndPort( |
| 53 const std::string& unix_domain_path, |
| 54 int port_unused, |
| 55 int backlog) { |
| 56 DCHECK(!listen_socket_); |
| 57 |
| 58 SockaddrStorage address; |
| 59 if (!UnixDomainClientSocket::FillAddress(unix_domain_path, |
| 60 use_abstract_namespace_, |
| 61 &address)) { |
| 62 return ERR_ADDRESS_INVALID; |
| 63 } |
| 64 |
| 65 listen_socket_.reset(new SocketLibevent); |
| 66 int rv = listen_socket_->Open(AF_UNIX); |
| 67 if (rv != OK) |
| 68 return rv; |
| 69 |
| 70 rv = listen_socket_->Bind(address); |
| 71 if (rv != OK) { |
| 72 PLOG(ERROR) |
| 73 << "Could not bind unix domain socket to " << unix_domain_path |
| 74 << (use_abstract_namespace_ ? " (with abstract namespace)" : ""); |
| 75 return rv; |
| 76 } |
| 77 |
| 78 return listen_socket_->Listen(backlog); |
| 79 } |
| 80 |
| 81 int UnixDomainServerSocket::GetLocalAddress(IPEndPoint* address) const { |
| 82 NOTIMPLEMENTED(); |
| 83 return ERR_NOT_IMPLEMENTED; |
| 84 } |
| 85 |
| 86 int UnixDomainServerSocket::Accept(scoped_ptr<StreamSocket>* socket, |
| 87 const CompletionCallback& callback) { |
| 88 DCHECK(socket); |
| 89 DCHECK(!callback.is_null()); |
| 90 DCHECK(listen_socket_); |
| 91 DCHECK(!accept_socket_); |
| 92 |
| 93 while (true) { |
| 94 int rv = listen_socket_->Accept( |
| 95 &accept_socket_, |
| 96 base::Bind(&UnixDomainServerSocket::AcceptCompleted, |
| 97 base::Unretained(this), socket, callback)); |
| 98 if (rv != OK) |
| 99 return rv; |
| 100 if (AuthenticateAndGetStreamSocket(socket)) |
| 101 return OK; |
| 102 } |
| 103 } |
| 104 |
| 105 void UnixDomainServerSocket::AcceptCompleted(scoped_ptr<StreamSocket>* socket, |
| 106 const CompletionCallback& callback, |
| 107 int rv) { |
| 108 if (rv != OK) { |
| 109 callback.Run(rv); |
| 110 return; |
| 111 } |
| 112 |
| 113 if (AuthenticateAndGetStreamSocket(socket)) { |
| 114 callback.Run(OK); |
| 115 return; |
| 116 } |
| 117 |
| 118 // Accepts another socket because authentication error should be transparent |
| 119 // to the caller. |
| 120 rv = Accept(socket, callback); |
| 121 if (rv != ERR_IO_PENDING) |
| 122 callback.Run(rv); |
| 123 } |
| 124 |
| 125 bool UnixDomainServerSocket::AuthenticateAndGetStreamSocket( |
| 126 scoped_ptr<StreamSocket>* socket) { |
| 127 DCHECK(accept_socket_); |
| 128 |
| 129 uid_t user_id; |
| 130 gid_t group_id; |
| 131 if (!GetPeerIds(accept_socket_->socket_fd(), &user_id, &group_id) || |
| 132 !auth_callback_.Run(user_id, group_id)) { |
| 133 accept_socket_.reset(); |
| 134 return false; |
| 135 } |
| 136 |
| 137 socket->reset(new UnixDomainClientSocket(accept_socket_.Pass())); |
| 138 return true; |
| 139 } |
| 140 |
| 141 } // namespace net |
OLD | NEW |