Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/unix_domain_socket_posix.h" | 5 #include "net/socket/unix_domain_socket_posix.h" |
| 6 | 6 |
| 7 #include <cstring> | |
| 8 #include <string> | |
| 9 | |
| 10 #include <errno.h> | 7 #include <errno.h> |
| 11 #include <sys/socket.h> | 8 #include <sys/socket.h> |
| 12 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 13 #include <sys/types.h> | 10 #include <sys/types.h> |
| 14 #include <sys/un.h> | 11 #include <sys/un.h> |
| 15 #include <unistd.h> | 12 #include <unistd.h> |
| 16 | 13 |
| 14 #include <cstring> | |
| 15 #include <string> | |
| 16 | |
| 17 #include "base/bind.h" | 17 #include "base/bind.h" |
| 18 #include "base/callback.h" | 18 #include "base/callback.h" |
| 19 #include "base/posix/eintr_wrapper.h" | 19 #include "base/posix/eintr_wrapper.h" |
| 20 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
| 21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 23 #include "net/base/net_util.h" | 23 #include "net/base/net_util.h" |
| 24 #include "net/socket/socket_descriptor.h" | 24 #include "net/socket/socket_descriptor.h" |
| 25 #include "net/socket/unix_domain_client_socket_posix.h" | |
| 25 | 26 |
| 26 namespace net { | 27 namespace net { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 bool NoAuthenticationCallback(uid_t, gid_t) { | 31 int CreateAndBind(const std::string& socket_path, |
| 31 return true; | 32 bool use_abstract_namespace, |
| 32 } | 33 SocketDescriptor* socket_fd) { |
| 34 SockaddrStorage address; | |
| 35 if (!UnixDomainClientSocket::FillAddress(socket_path, | |
| 36 use_abstract_namespace, | |
| 37 &address)) | |
| 38 return ERR_ADDRESS_INVALID; | |
|
mmenke
2014/07/11 20:44:05
nit: Use braces when the condition takes up more
byungchul
2014/07/14 17:49:23
Done.
| |
| 33 | 39 |
| 34 bool GetPeerIds(int socket, uid_t* user_id, gid_t* group_id) { | 40 SocketDescriptor s = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0); |
|
mmenke
2014/07/11 20:44:05
nit: The name "s" violates the google naming guid
byungchul
2014/07/14 17:49:23
Done.
| |
| 35 #if defined(OS_LINUX) || defined(OS_ANDROID) | 41 if (s == kInvalidSocket) |
| 36 struct ucred user_cred; | 42 return errno ? MapSystemError(errno) : ERR_UNEXPECTED; |
| 37 socklen_t len = sizeof(user_cred); | 43 |
| 38 if (getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &user_cred, &len) == -1) | 44 if (bind(s, address.addr, address.addr_len) < 0) { |
| 39 return false; | 45 int rv = MapSystemError(errno); |
| 40 *user_id = user_cred.uid; | 46 close(s); |
| 41 *group_id = user_cred.gid; | 47 PLOG(ERROR) << "Could not bind unix domain socket to " << socket_path |
| 42 #else | 48 << (use_abstract_namespace ? " (with abstract namespace)" : ""); |
| 43 if (getpeereid(socket, user_id, group_id) == -1) | 49 return rv; |
| 44 return false; | 50 } |
| 45 #endif | 51 |
| 46 return true; | 52 DCHECK(socket_fd); |
|
mmenke
2014/07/11 20:44:05
nit: Know this was here before, but since we're c
byungchul
2014/07/14 17:49:23
Done.
| |
| 53 *socket_fd = s; | |
| 54 return OK; | |
| 47 } | 55 } |
| 48 | 56 |
| 49 } // namespace | 57 } // namespace |
| 50 | 58 |
| 51 // static | 59 // static |
| 52 UnixDomainSocket::AuthCallback UnixDomainSocket::NoAuthentication() { | 60 scoped_ptr<UnixDomainListenSocket> |
| 53 return base::Bind(NoAuthenticationCallback); | 61 UnixDomainListenSocket::CreateAndListenInternal( |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 scoped_ptr<UnixDomainSocket> UnixDomainSocket::CreateAndListenInternal( | |
| 58 const std::string& path, | 62 const std::string& path, |
| 59 const std::string& fallback_path, | 63 const std::string& fallback_path, |
| 60 StreamListenSocket::Delegate* del, | 64 StreamListenSocket::Delegate* del, |
| 61 const AuthCallback& auth_callback, | 65 const AuthCallback& auth_callback, |
| 62 bool use_abstract_namespace) { | 66 bool use_abstract_namespace) { |
| 63 SocketDescriptor s = CreateAndBind(path, use_abstract_namespace); | 67 SocketDescriptor socket_fd = kInvalidSocket; |
| 64 if (s == kInvalidSocket && !fallback_path.empty()) | 68 int rv = CreateAndBind(path, use_abstract_namespace, &socket_fd); |
| 65 s = CreateAndBind(fallback_path, use_abstract_namespace); | 69 if (rv != OK && !fallback_path.empty()) |
| 66 if (s == kInvalidSocket) | 70 rv = CreateAndBind(fallback_path, use_abstract_namespace, &socket_fd); |
| 67 return scoped_ptr<UnixDomainSocket>(); | 71 if (rv != OK) |
| 68 scoped_ptr<UnixDomainSocket> sock( | 72 return scoped_ptr<UnixDomainListenSocket>(); |
| 69 new UnixDomainSocket(s, del, auth_callback)); | 73 scoped_ptr<UnixDomainListenSocket> sock( |
| 74 new UnixDomainListenSocket(socket_fd, del, auth_callback)); | |
| 70 sock->Listen(); | 75 sock->Listen(); |
| 71 return sock.Pass(); | 76 return sock.Pass(); |
| 72 } | 77 } |
| 73 | 78 |
| 74 // static | 79 // static |
| 75 scoped_ptr<UnixDomainSocket> UnixDomainSocket::CreateAndListen( | 80 scoped_ptr<UnixDomainListenSocket> UnixDomainListenSocket::CreateAndListen( |
| 76 const std::string& path, | 81 const std::string& path, |
| 77 StreamListenSocket::Delegate* del, | 82 StreamListenSocket::Delegate* del, |
| 78 const AuthCallback& auth_callback) { | 83 const AuthCallback& auth_callback) { |
| 79 return CreateAndListenInternal(path, "", del, auth_callback, false); | 84 return CreateAndListenInternal(path, "", del, auth_callback, false); |
| 80 } | 85 } |
| 81 | 86 |
| 82 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) | 87 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) |
| 83 // static | 88 // static |
| 84 scoped_ptr<UnixDomainSocket> | 89 scoped_ptr<UnixDomainListenSocket> |
| 85 UnixDomainSocket::CreateAndListenWithAbstractNamespace( | 90 UnixDomainListenSocket::CreateAndListenWithAbstractNamespace( |
| 86 const std::string& path, | 91 const std::string& path, |
| 87 const std::string& fallback_path, | 92 const std::string& fallback_path, |
| 88 StreamListenSocket::Delegate* del, | 93 StreamListenSocket::Delegate* del, |
| 89 const AuthCallback& auth_callback) { | 94 const AuthCallback& auth_callback) { |
| 90 return | 95 return |
| 91 CreateAndListenInternal(path, fallback_path, del, auth_callback, true); | 96 CreateAndListenInternal(path, fallback_path, del, auth_callback, true); |
| 92 } | 97 } |
| 93 #endif | 98 #endif |
| 94 | 99 |
| 95 UnixDomainSocket::UnixDomainSocket( | 100 UnixDomainListenSocket::UnixDomainListenSocket( |
| 96 SocketDescriptor s, | 101 SocketDescriptor s, |
| 97 StreamListenSocket::Delegate* del, | 102 StreamListenSocket::Delegate* del, |
| 98 const AuthCallback& auth_callback) | 103 const AuthCallback& auth_callback) |
| 99 : StreamListenSocket(s, del), | 104 : StreamListenSocket(s, del), |
| 100 auth_callback_(auth_callback) {} | 105 auth_callback_(auth_callback) {} |
| 101 | 106 |
| 102 UnixDomainSocket::~UnixDomainSocket() {} | 107 UnixDomainListenSocket::~UnixDomainListenSocket() {} |
| 103 | 108 |
| 104 // static | 109 void UnixDomainListenSocket::Accept() { |
| 105 SocketDescriptor UnixDomainSocket::CreateAndBind(const std::string& path, | |
| 106 bool use_abstract_namespace) { | |
| 107 sockaddr_un addr; | |
| 108 static const size_t kPathMax = sizeof(addr.sun_path); | |
| 109 if (use_abstract_namespace + path.size() + 1 /* '\0' */ > kPathMax) | |
| 110 return kInvalidSocket; | |
| 111 const SocketDescriptor s = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0); | |
| 112 if (s == kInvalidSocket) | |
| 113 return kInvalidSocket; | |
| 114 memset(&addr, 0, sizeof(addr)); | |
| 115 addr.sun_family = AF_UNIX; | |
| 116 socklen_t addr_len; | |
| 117 if (use_abstract_namespace) { | |
| 118 // Convert the path given into abstract socket name. It must start with | |
| 119 // the '\0' character, so we are adding it. |addr_len| must specify the | |
| 120 // length of the structure exactly, as potentially the socket name may | |
| 121 // have '\0' characters embedded (although we don't support this). | |
| 122 // Note that addr.sun_path is already zero initialized. | |
| 123 memcpy(addr.sun_path + 1, path.c_str(), path.size()); | |
| 124 addr_len = path.size() + offsetof(struct sockaddr_un, sun_path) + 1; | |
| 125 } else { | |
| 126 memcpy(addr.sun_path, path.c_str(), path.size()); | |
| 127 addr_len = sizeof(sockaddr_un); | |
| 128 } | |
| 129 if (bind(s, reinterpret_cast<sockaddr*>(&addr), addr_len)) { | |
| 130 LOG(ERROR) << "Could not bind unix domain socket to " << path; | |
| 131 if (use_abstract_namespace) | |
| 132 LOG(ERROR) << " (with abstract namespace enabled)"; | |
| 133 if (IGNORE_EINTR(close(s)) < 0) | |
| 134 LOG(ERROR) << "close() error"; | |
| 135 return kInvalidSocket; | |
| 136 } | |
| 137 return s; | |
| 138 } | |
| 139 | |
| 140 void UnixDomainSocket::Accept() { | |
| 141 SocketDescriptor conn = StreamListenSocket::AcceptSocket(); | 110 SocketDescriptor conn = StreamListenSocket::AcceptSocket(); |
| 142 if (conn == kInvalidSocket) | 111 if (conn == kInvalidSocket) |
| 143 return; | 112 return; |
| 144 uid_t user_id; | 113 uid_t user_id; |
| 145 gid_t group_id; | 114 gid_t group_id; |
| 146 if (!GetPeerIds(conn, &user_id, &group_id) || | 115 if (!UnixDomainServerSocket::GetPeerIds(conn, &user_id, &group_id) || |
| 147 !auth_callback_.Run(user_id, group_id)) { | 116 !auth_callback_.Run(user_id, group_id)) { |
| 148 if (IGNORE_EINTR(close(conn)) < 0) | 117 if (IGNORE_EINTR(close(conn)) < 0) |
| 149 LOG(ERROR) << "close() error"; | 118 LOG(ERROR) << "close() error"; |
| 150 return; | 119 return; |
| 151 } | 120 } |
| 152 scoped_ptr<UnixDomainSocket> sock( | 121 scoped_ptr<UnixDomainListenSocket> sock( |
| 153 new UnixDomainSocket(conn, socket_delegate_, auth_callback_)); | 122 new UnixDomainListenSocket(conn, socket_delegate_, auth_callback_)); |
| 154 // It's up to the delegate to AddRef if it wants to keep it around. | 123 // It's up to the delegate to AddRef if it wants to keep it around. |
| 155 sock->WatchSocket(WAITING_READ); | 124 sock->WatchSocket(WAITING_READ); |
| 156 socket_delegate_->DidAccept(this, sock.PassAs<StreamListenSocket>()); | 125 socket_delegate_->DidAccept(this, sock.PassAs<StreamListenSocket>()); |
| 157 } | 126 } |
| 158 | 127 |
| 159 UnixDomainSocketFactory::UnixDomainSocketFactory( | 128 UnixDomainListenSocketFactory::UnixDomainListenSocketFactory( |
| 160 const std::string& path, | 129 const std::string& path, |
| 161 const UnixDomainSocket::AuthCallback& auth_callback) | 130 const UnixDomainListenSocket::AuthCallback& auth_callback) |
| 162 : path_(path), | 131 : path_(path), |
| 163 auth_callback_(auth_callback) {} | 132 auth_callback_(auth_callback) {} |
| 164 | 133 |
| 165 UnixDomainSocketFactory::~UnixDomainSocketFactory() {} | 134 UnixDomainListenSocketFactory::~UnixDomainListenSocketFactory() {} |
| 166 | 135 |
| 167 scoped_ptr<StreamListenSocket> UnixDomainSocketFactory::CreateAndListen( | 136 scoped_ptr<StreamListenSocket> UnixDomainListenSocketFactory::CreateAndListen( |
| 168 StreamListenSocket::Delegate* delegate) const { | 137 StreamListenSocket::Delegate* delegate) const { |
| 169 return UnixDomainSocket::CreateAndListen( | 138 return UnixDomainListenSocket::CreateAndListen( |
| 170 path_, delegate, auth_callback_).PassAs<StreamListenSocket>(); | 139 path_, delegate, auth_callback_).PassAs<StreamListenSocket>(); |
| 171 } | 140 } |
| 172 | 141 |
| 173 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) | 142 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) |
| 174 | 143 |
| 175 UnixDomainSocketWithAbstractNamespaceFactory:: | 144 UnixDomainListenSocketWithAbstractNamespaceFactory:: |
| 176 UnixDomainSocketWithAbstractNamespaceFactory( | 145 UnixDomainListenSocketWithAbstractNamespaceFactory( |
| 177 const std::string& path, | 146 const std::string& path, |
| 178 const std::string& fallback_path, | 147 const std::string& fallback_path, |
| 179 const UnixDomainSocket::AuthCallback& auth_callback) | 148 const UnixDomainListenSocket::AuthCallback& auth_callback) |
| 180 : UnixDomainSocketFactory(path, auth_callback), | 149 : UnixDomainListenSocketFactory(path, auth_callback), |
| 181 fallback_path_(fallback_path) {} | 150 fallback_path_(fallback_path) {} |
| 182 | 151 |
| 183 UnixDomainSocketWithAbstractNamespaceFactory:: | 152 UnixDomainListenSocketWithAbstractNamespaceFactory:: |
| 184 ~UnixDomainSocketWithAbstractNamespaceFactory() {} | 153 ~UnixDomainListenSocketWithAbstractNamespaceFactory() {} |
| 185 | 154 |
| 186 scoped_ptr<StreamListenSocket> | 155 scoped_ptr<StreamListenSocket> |
| 187 UnixDomainSocketWithAbstractNamespaceFactory::CreateAndListen( | 156 UnixDomainListenSocketWithAbstractNamespaceFactory::CreateAndListen( |
| 188 StreamListenSocket::Delegate* delegate) const { | 157 StreamListenSocket::Delegate* delegate) const { |
| 189 return UnixDomainSocket::CreateAndListenWithAbstractNamespace( | 158 return UnixDomainListenSocket::CreateAndListenWithAbstractNamespace( |
| 190 path_, fallback_path_, delegate, auth_callback_) | 159 path_, fallback_path_, delegate, auth_callback_) |
| 191 .PassAs<StreamListenSocket>(); | 160 .PassAs<StreamListenSocket>(); |
| 192 } | 161 } |
| 193 | 162 |
| 194 #endif | 163 #endif |
| 195 | 164 |
| 196 } // namespace net | 165 } // namespace net |
| OLD | NEW |