3aa5440242ffc90f9839eaf10912f622dbe043c8
[bbaumbach/samba-autobuild/.git] / source4 / lib / socket / socket_unix.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    unix domain socket functions
5
6    Copyright (C) Stefan Metzmacher 2004
7    Copyright (C) Andrew Tridgell 2004-2005
8    
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.
13    
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.
18    
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/>.
21 */
22
23 #include "includes.h"
24 #include "lib/socket/socket.h"
25 #include "system/network.h"
26 #include "system/filesys.h"
27
28 _PUBLIC_ const struct socket_ops *socket_unixdom_ops(enum socket_type type);
29
30
31 /*
32   approximate errno mapping
33 */
34 static NTSTATUS unixdom_error(int ernum)
35 {
36         return map_nt_error_from_unix_common(ernum);
37 }
38
39 static NTSTATUS unixdom_init(struct socket_context *sock)
40 {
41         int type;
42
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;
52         }
53
54         sock->fd = socket(PF_UNIX, type, 0);
55         if (sock->fd == -1) {
56                 return map_nt_error_from_unix_common(errno);
57         }
58         sock->private_data = NULL;
59
60         sock->backend_name = "unix";
61
62         smb_set_close_on_exec(sock->fd);
63
64         return NT_STATUS_OK;
65 }
66
67 static void unixdom_close(struct socket_context *sock)
68 {
69         close(sock->fd);
70 }
71
72 static NTSTATUS unixdom_connect_complete(struct socket_context *sock, uint32_t flags)
73 {
74         int error=0, ret;
75         socklen_t len = sizeof(error);
76
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);
82         }
83         if (error != 0) {
84                 return map_nt_error_from_unix_common(error);
85         }
86
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);
91                 }
92         }
93
94         sock->state = SOCKET_STATE_CLIENT_CONNECTED;
95
96         return NT_STATUS_OK;
97 }
98
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)
103 {
104         int ret;
105
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;
112                 }
113                 
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);
117
118                 ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
119         }
120         if (ret == -1) {
121                 return unixdom_error(errno);
122         }
123
124         return unixdom_connect_complete(sock, flags);
125 }
126
127 static NTSTATUS unixdom_listen(struct socket_context *sock,
128                                const struct socket_address *my_address, 
129                                int queue_size, uint32_t flags)
130 {
131         struct sockaddr_un my_addr;
132         int ret;
133
134         /* delete if it already exists */
135         if (my_address->addr) {
136                 unlink(my_address->addr);
137         }
138
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;
146                 }
147                 
148                 
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);
152
153                 ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
154         }
155         if (ret == -1) {
156                 return unixdom_error(errno);
157         }
158
159         if (sock->type == SOCKET_TYPE_STREAM) {
160                 ret = listen(sock->fd, queue_size);
161                 if (ret == -1) {
162                         return unixdom_error(errno);
163                 }
164         }
165
166         if (!(flags & SOCKET_FLAG_BLOCK)) {
167                 ret = set_blocking(sock->fd, false);
168                 if (ret == -1) {
169                         return unixdom_error(errno);
170                 }
171         }
172
173         sock->state = SOCKET_STATE_SERVER_LISTEN;
174         sock->private_data = (void *)talloc_strdup(sock, my_address->addr);
175
176         return NT_STATUS_OK;
177 }
178
179 static NTSTATUS unixdom_accept(struct socket_context *sock, 
180                                struct socket_context **new_sock)
181 {
182         struct sockaddr_un cli_addr;
183         socklen_t cli_addr_len = sizeof(cli_addr);
184         int new_fd;
185
186         if (sock->type != SOCKET_TYPE_STREAM) {
187                 return NT_STATUS_INVALID_PARAMETER;
188         }
189
190         new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
191         if (new_fd == -1) {
192                 return unixdom_error(errno);
193         }
194
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);
200                 }
201         }
202
203         smb_set_close_on_exec(new_fd);
204
205         (*new_sock) = talloc(NULL, struct socket_context);
206         if (!(*new_sock)) {
207                 close(new_fd);
208                 return NT_STATUS_NO_MEMORY;
209         }
210
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;
215
216         (*new_sock)->fd                 = new_fd;
217
218         (*new_sock)->private_data       = NULL;
219         (*new_sock)->ops                = sock->ops;
220         (*new_sock)->backend_name       = sock->backend_name;
221
222         return NT_STATUS_OK;
223 }
224
225 static NTSTATUS unixdom_recv(struct socket_context *sock, void *buf, 
226                              size_t wantlen, size_t *nread)
227 {
228         ssize_t gotlen;
229
230         *nread = 0;
231
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);
237         }
238
239         *nread = gotlen;
240
241         return NT_STATUS_OK;
242 }
243
244 static NTSTATUS unixdom_send(struct socket_context *sock,
245                              const DATA_BLOB *blob, size_t *sendlen)
246 {
247         ssize_t len;
248
249         *sendlen = 0;
250
251         len = send(sock->fd, blob->data, blob->length, 0);
252         if (len == -1) {
253                 return unixdom_error(errno);
254         }       
255
256         *sendlen = len;
257
258         return NT_STATUS_OK;
259 }
260
261
262 static NTSTATUS unixdom_sendto(struct socket_context *sock, 
263                                const DATA_BLOB *blob, size_t *sendlen, 
264                                const struct socket_address *dest)
265 {
266         struct sockaddr_un srv_addr;
267         const struct sockaddr *sa;
268         socklen_t sa_len;
269         ssize_t len;
270
271         *sendlen = 0;
272
273         if (dest->sockaddr) {
274                 sa = dest->sockaddr;
275                 sa_len = dest->sockaddrlen;
276         } else {
277                 if (strlen(dest->addr)+1 > sizeof(srv_addr.sun_path)) {
278                         return NT_STATUS_OBJECT_PATH_INVALID;
279                 }
280
281                 ZERO_STRUCT(srv_addr);
282                 srv_addr.sun_family = AF_UNIX;
283                 snprintf(srv_addr.sun_path, sizeof(srv_addr.sun_path), "%s",
284                          dest->addr);
285                 sa = (struct sockaddr *) &srv_addr;
286                 sa_len = sizeof(srv_addr);
287         }
288
289         len = sendto(sock->fd, blob->data, blob->length, 0, sa, sa_len);
290
291         /* retry once */
292         if (len == -1 && errno == EMSGSIZE) {
293                 /* round up in 1K increments */
294                 int bufsize = ((blob->length + 1023) & (~1023));
295                 if (setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF, &bufsize,
296                                sizeof(bufsize)) == -1)
297                 {
298                         return map_nt_error_from_unix_common(EMSGSIZE);
299                 }
300                 len = sendto(sock->fd, blob->data, blob->length, 0, sa, sa_len);
301         }
302
303         if (len == -1) {
304                 return map_nt_error_from_unix_common(errno);
305         }       
306
307         *sendlen = len;
308
309         return NT_STATUS_OK;
310 }
311
312
313 static NTSTATUS unixdom_set_option(struct socket_context *sock, 
314                                    const char *option, const char *val)
315 {
316         return NT_STATUS_OK;
317 }
318
319 static char *unixdom_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
320 {
321         return talloc_strdup(mem_ctx, "LOCAL/unixdom");
322 }
323
324 static struct socket_address *unixdom_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
325 {
326         struct sockaddr_un *peer_addr;
327         socklen_t len = sizeof(*peer_addr);
328         struct socket_address *peer;
329         int ret;
330
331         peer = talloc(mem_ctx, struct socket_address);
332         if (!peer) {
333                 return NULL;
334         }
335         
336         peer->family = sock->backend_name;
337         peer_addr = talloc(peer, struct sockaddr_un);
338         if (!peer_addr) {
339                 talloc_free(peer);
340                 return NULL;
341         }
342
343         peer->sockaddr = (struct sockaddr *)peer_addr;
344
345         ret = getpeername(sock->fd, peer->sockaddr, &len);
346         if (ret == -1) {
347                 talloc_free(peer);
348                 return NULL;
349         }
350
351         peer->sockaddrlen = len;
352
353         peer->port = 0;
354         peer->addr = talloc_strdup(peer, "LOCAL/unixdom");
355         if (!peer->addr) {
356                 talloc_free(peer);
357                 return NULL;
358         }
359
360         return peer;
361 }
362
363 static struct socket_address *unixdom_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
364 {
365         struct sockaddr_in *local_addr;
366         socklen_t len = sizeof(*local_addr);
367         struct socket_address *local;
368         int ret;
369         
370         local = talloc(mem_ctx, struct socket_address);
371         if (!local) {
372                 return NULL;
373         }
374         
375         local->family = sock->backend_name;
376         local_addr = talloc(local, struct sockaddr_in);
377         if (!local_addr) {
378                 talloc_free(local);
379                 return NULL;
380         }
381
382         local->sockaddr = (struct sockaddr *)local_addr;
383
384         ret = getsockname(sock->fd, local->sockaddr, &len);
385         if (ret == -1) {
386                 talloc_free(local);
387                 return NULL;
388         }
389
390         local->sockaddrlen = len;
391
392         local->port = 0;
393         local->addr = talloc_strdup(local, "LOCAL/unixdom");
394         if (!local->addr) {
395                 talloc_free(local);
396                 return NULL;
397         }
398
399         return local;
400 }
401
402 static int unixdom_get_fd(struct socket_context *sock)
403 {
404         return sock->fd;
405 }
406
407 static NTSTATUS unixdom_pending(struct socket_context *sock, size_t *npending)
408 {
409         int value = 0;
410         if (ioctl(sock->fd, FIONREAD, &value) == 0) {
411                 *npending = value;
412                 return NT_STATUS_OK;
413         }
414         return map_nt_error_from_unix_common(errno);
415 }
416
417 static const struct socket_ops unixdom_ops = {
418         .name                   = "unix",
419         .fn_init                = unixdom_init,
420         .fn_connect             = unixdom_connect,
421         .fn_connect_complete    = unixdom_connect_complete,
422         .fn_listen              = unixdom_listen,
423         .fn_accept              = unixdom_accept,
424         .fn_recv                = unixdom_recv,
425         .fn_send                = unixdom_send,
426         .fn_sendto              = unixdom_sendto,
427         .fn_close               = unixdom_close,
428         .fn_pending             = unixdom_pending,
429
430         .fn_set_option          = unixdom_set_option,
431
432         .fn_get_peer_name       = unixdom_get_peer_name,
433         .fn_get_peer_addr       = unixdom_get_peer_addr,
434         .fn_get_my_addr         = unixdom_get_my_addr,
435
436         .fn_get_fd              = unixdom_get_fd
437 };
438
439 _PUBLIC_ const struct socket_ops *socket_unixdom_ops(enum socket_type type)
440 {
441         return &unixdom_ops;
442 }