s4-socket: Make sure unix socket addresses are null terminated.
[Samba/id10ts.git] / source4 / lib / socket / socket_unix.c
blob4755e796a4a9e99f4ca067c5e8af8ca31086840e
1 /*
2 Unix SMB/CIFS implementation.
4 unix domain socket functions
6 Copyright (C) Stefan Metzmacher 2004
7 Copyright (C) Andrew Tridgell 2004-2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "lib/socket/socket.h"
25 #include "system/network.h"
26 #include "system/filesys.h"
28 _PUBLIC_ const struct socket_ops *socket_unixdom_ops(enum socket_type type);
32 approximate errno mapping
34 static NTSTATUS unixdom_error(int ernum)
36 return map_nt_error_from_unix_common(ernum);
39 static NTSTATUS unixdom_init(struct socket_context *sock)
41 int type;
43 switch (sock->type) {
44 case SOCKET_TYPE_STREAM:
45 type = SOCK_STREAM;
46 break;
47 case SOCKET_TYPE_DGRAM:
48 type = SOCK_DGRAM;
49 break;
50 default:
51 return NT_STATUS_INVALID_PARAMETER;
54 sock->fd = socket(PF_UNIX, type, 0);
55 if (sock->fd == -1) {
56 return map_nt_error_from_unix_common(errno);
58 sock->private_data = NULL;
60 sock->backend_name = "unix";
62 smb_set_close_on_exec(sock->fd);
64 return NT_STATUS_OK;
67 static void unixdom_close(struct socket_context *sock)
69 close(sock->fd);
72 static NTSTATUS unixdom_connect_complete(struct socket_context *sock, uint32_t flags)
74 int error=0, ret;
75 socklen_t len = sizeof(error);
77 /* check for any errors that may have occurred - this is needed
78 for non-blocking connect */
79 ret = getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, &error, &len);
80 if (ret == -1) {
81 return map_nt_error_from_unix_common(errno);
83 if (error != 0) {
84 return map_nt_error_from_unix_common(error);
87 if (!(flags & SOCKET_FLAG_BLOCK)) {
88 ret = set_blocking(sock->fd, false);
89 if (ret == -1) {
90 return map_nt_error_from_unix_common(errno);
94 sock->state = SOCKET_STATE_CLIENT_CONNECTED;
96 return NT_STATUS_OK;
99 static NTSTATUS unixdom_connect(struct socket_context *sock,
100 const struct socket_address *my_address,
101 const struct socket_address *srv_address,
102 uint32_t flags)
104 int ret;
106 if (srv_address->sockaddr) {
107 ret = connect(sock->fd, srv_address->sockaddr, srv_address->sockaddrlen);
108 } else {
109 struct sockaddr_un srv_addr;
110 if (strlen(srv_address->addr)+1 > sizeof(srv_addr.sun_path)) {
111 return NT_STATUS_OBJECT_PATH_INVALID;
114 ZERO_STRUCT(srv_addr);
115 srv_addr.sun_family = AF_UNIX;
116 snprintf(srv_addr.sun_path, sizeof(srv_addr.sun_path), "%s", srv_address->addr);
118 ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
120 if (ret == -1) {
121 return unixdom_error(errno);
124 return unixdom_connect_complete(sock, flags);
127 static NTSTATUS unixdom_listen(struct socket_context *sock,
128 const struct socket_address *my_address,
129 int queue_size, uint32_t flags)
131 struct sockaddr_un my_addr;
132 int ret;
134 /* delete if it already exists */
135 if (my_address->addr) {
136 unlink(my_address->addr);
139 if (my_address->sockaddr) {
140 ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
141 } else if (my_address->addr == NULL) {
142 return NT_STATUS_INVALID_PARAMETER;
143 } else {
144 if (strlen(my_address->addr)+1 > sizeof(my_addr.sun_path)) {
145 return NT_STATUS_OBJECT_PATH_INVALID;
149 ZERO_STRUCT(my_addr);
150 my_addr.sun_family = AF_UNIX;
151 snprintf(my_addr.sun_path, sizeof(my_addr.sun_path), "%s", my_address->addr);
153 ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
155 if (ret == -1) {
156 return unixdom_error(errno);
159 if (sock->type == SOCKET_TYPE_STREAM) {
160 ret = listen(sock->fd, queue_size);
161 if (ret == -1) {
162 return unixdom_error(errno);
166 if (!(flags & SOCKET_FLAG_BLOCK)) {
167 ret = set_blocking(sock->fd, false);
168 if (ret == -1) {
169 return unixdom_error(errno);
173 sock->state = SOCKET_STATE_SERVER_LISTEN;
174 sock->private_data = (void *)talloc_strdup(sock, my_address->addr);
176 return NT_STATUS_OK;
179 static NTSTATUS unixdom_accept(struct socket_context *sock,
180 struct socket_context **new_sock)
182 struct sockaddr_un cli_addr;
183 socklen_t cli_addr_len = sizeof(cli_addr);
184 int new_fd;
186 if (sock->type != SOCKET_TYPE_STREAM) {
187 return NT_STATUS_INVALID_PARAMETER;
190 new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
191 if (new_fd == -1) {
192 return unixdom_error(errno);
195 if (!(sock->flags & SOCKET_FLAG_BLOCK)) {
196 int ret = set_blocking(new_fd, false);
197 if (ret == -1) {
198 close(new_fd);
199 return map_nt_error_from_unix_common(errno);
203 smb_set_close_on_exec(new_fd);
205 (*new_sock) = talloc(NULL, struct socket_context);
206 if (!(*new_sock)) {
207 close(new_fd);
208 return NT_STATUS_NO_MEMORY;
211 /* copy the socket_context */
212 (*new_sock)->type = sock->type;
213 (*new_sock)->state = SOCKET_STATE_SERVER_CONNECTED;
214 (*new_sock)->flags = sock->flags;
216 (*new_sock)->fd = new_fd;
218 (*new_sock)->private_data = NULL;
219 (*new_sock)->ops = sock->ops;
220 (*new_sock)->backend_name = sock->backend_name;
222 return NT_STATUS_OK;
225 static NTSTATUS unixdom_recv(struct socket_context *sock, void *buf,
226 size_t wantlen, size_t *nread)
228 ssize_t gotlen;
230 *nread = 0;
232 gotlen = recv(sock->fd, buf, wantlen, 0);
233 if (gotlen == 0) {
234 return NT_STATUS_END_OF_FILE;
235 } else if (gotlen == -1) {
236 return unixdom_error(errno);
239 *nread = gotlen;
241 return NT_STATUS_OK;
244 static NTSTATUS unixdom_send(struct socket_context *sock,
245 const DATA_BLOB *blob, size_t *sendlen)
247 ssize_t len;
249 *sendlen = 0;
251 len = send(sock->fd, blob->data, blob->length, 0);
252 if (len == -1) {
253 return unixdom_error(errno);
256 *sendlen = len;
258 return NT_STATUS_OK;
262 static NTSTATUS unixdom_sendto(struct socket_context *sock,
263 const DATA_BLOB *blob, size_t *sendlen,
264 const struct socket_address *dest)
266 ssize_t len;
267 *sendlen = 0;
269 if (dest->sockaddr) {
270 len = sendto(sock->fd, blob->data, blob->length, 0,
271 dest->sockaddr, dest->sockaddrlen);
272 } else {
273 struct sockaddr_un srv_addr;
275 if (strlen(dest->addr)+1 > sizeof(srv_addr.sun_path)) {
276 return NT_STATUS_OBJECT_PATH_INVALID;
279 ZERO_STRUCT(srv_addr);
280 srv_addr.sun_family = AF_UNIX;
281 snprintf(srv_addr.sun_path, sizeof(srv_addr.sun_path), "%s", dest->addr);
283 len = sendto(sock->fd, blob->data, blob->length, 0,
284 (struct sockaddr *)&srv_addr, sizeof(srv_addr));
286 if (len == -1) {
287 return map_nt_error_from_unix_common(errno);
290 *sendlen = len;
292 return NT_STATUS_OK;
296 static NTSTATUS unixdom_set_option(struct socket_context *sock,
297 const char *option, const char *val)
299 return NT_STATUS_OK;
302 static char *unixdom_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
304 return talloc_strdup(mem_ctx, "LOCAL/unixdom");
307 static struct socket_address *unixdom_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
309 struct sockaddr_in *peer_addr;
310 socklen_t len = sizeof(*peer_addr);
311 struct socket_address *peer;
312 int ret;
314 peer = talloc(mem_ctx, struct socket_address);
315 if (!peer) {
316 return NULL;
319 peer->family = sock->backend_name;
320 peer_addr = talloc(peer, struct sockaddr_in);
321 if (!peer_addr) {
322 talloc_free(peer);
323 return NULL;
326 peer->sockaddr = (struct sockaddr *)peer_addr;
328 ret = getpeername(sock->fd, peer->sockaddr, &len);
329 if (ret == -1) {
330 talloc_free(peer);
331 return NULL;
334 peer->sockaddrlen = len;
336 peer->port = 0;
337 peer->addr = talloc_strdup(peer, "LOCAL/unixdom");
338 if (!peer->addr) {
339 talloc_free(peer);
340 return NULL;
343 return peer;
346 static struct socket_address *unixdom_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
348 struct sockaddr_in *local_addr;
349 socklen_t len = sizeof(*local_addr);
350 struct socket_address *local;
351 int ret;
353 local = talloc(mem_ctx, struct socket_address);
354 if (!local) {
355 return NULL;
358 local->family = sock->backend_name;
359 local_addr = talloc(local, struct sockaddr_in);
360 if (!local_addr) {
361 talloc_free(local);
362 return NULL;
365 local->sockaddr = (struct sockaddr *)local_addr;
367 ret = getsockname(sock->fd, local->sockaddr, &len);
368 if (ret == -1) {
369 talloc_free(local);
370 return NULL;
373 local->sockaddrlen = len;
375 local->port = 0;
376 local->addr = talloc_strdup(local, "LOCAL/unixdom");
377 if (!local->addr) {
378 talloc_free(local);
379 return NULL;
382 return local;
385 static int unixdom_get_fd(struct socket_context *sock)
387 return sock->fd;
390 static NTSTATUS unixdom_pending(struct socket_context *sock, size_t *npending)
392 int value = 0;
393 if (ioctl(sock->fd, FIONREAD, &value) == 0) {
394 *npending = value;
395 return NT_STATUS_OK;
397 return map_nt_error_from_unix_common(errno);
400 static const struct socket_ops unixdom_ops = {
401 .name = "unix",
402 .fn_init = unixdom_init,
403 .fn_connect = unixdom_connect,
404 .fn_connect_complete = unixdom_connect_complete,
405 .fn_listen = unixdom_listen,
406 .fn_accept = unixdom_accept,
407 .fn_recv = unixdom_recv,
408 .fn_send = unixdom_send,
409 .fn_sendto = unixdom_sendto,
410 .fn_close = unixdom_close,
411 .fn_pending = unixdom_pending,
413 .fn_set_option = unixdom_set_option,
415 .fn_get_peer_name = unixdom_get_peer_name,
416 .fn_get_peer_addr = unixdom_get_peer_addr,
417 .fn_get_my_addr = unixdom_get_my_addr,
419 .fn_get_fd = unixdom_get_fd
422 _PUBLIC_ const struct socket_ops *socket_unixdom_ops(enum socket_type type)
424 return &unixdom_ops;