s4-socket: Make sure unix socket addresses are null terminated.
[gd/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         ssize_t len;
267         *sendlen = 0;
268                 
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;
274                 
275                 if (strlen(dest->addr)+1 > sizeof(srv_addr.sun_path)) {
276                         return NT_STATUS_OBJECT_PATH_INVALID;
277                 }
278                 
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);
282
283                 len = sendto(sock->fd, blob->data, blob->length, 0, 
284                              (struct sockaddr *)&srv_addr, sizeof(srv_addr));
285         }
286         if (len == -1) {
287                 return map_nt_error_from_unix_common(errno);
288         }       
289
290         *sendlen = len;
291
292         return NT_STATUS_OK;
293 }
294
295
296 static NTSTATUS unixdom_set_option(struct socket_context *sock, 
297                                    const char *option, const char *val)
298 {
299         return NT_STATUS_OK;
300 }
301
302 static char *unixdom_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
303 {
304         return talloc_strdup(mem_ctx, "LOCAL/unixdom");
305 }
306
307 static struct socket_address *unixdom_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
308 {
309         struct sockaddr_in *peer_addr;
310         socklen_t len = sizeof(*peer_addr);
311         struct socket_address *peer;
312         int ret;
313
314         peer = talloc(mem_ctx, struct socket_address);
315         if (!peer) {
316                 return NULL;
317         }
318         
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;
324         }
325
326         peer->sockaddr = (struct sockaddr *)peer_addr;
327
328         ret = getpeername(sock->fd, peer->sockaddr, &len);
329         if (ret == -1) {
330                 talloc_free(peer);
331                 return NULL;
332         }
333
334         peer->sockaddrlen = len;
335
336         peer->port = 0;
337         peer->addr = talloc_strdup(peer, "LOCAL/unixdom");
338         if (!peer->addr) {
339                 talloc_free(peer);
340                 return NULL;
341         }
342
343         return peer;
344 }
345
346 static struct socket_address *unixdom_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
347 {
348         struct sockaddr_in *local_addr;
349         socklen_t len = sizeof(*local_addr);
350         struct socket_address *local;
351         int ret;
352         
353         local = talloc(mem_ctx, struct socket_address);
354         if (!local) {
355                 return NULL;
356         }
357         
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;
363         }
364
365         local->sockaddr = (struct sockaddr *)local_addr;
366
367         ret = getsockname(sock->fd, local->sockaddr, &len);
368         if (ret == -1) {
369                 talloc_free(local);
370                 return NULL;
371         }
372
373         local->sockaddrlen = len;
374
375         local->port = 0;
376         local->addr = talloc_strdup(local, "LOCAL/unixdom");
377         if (!local->addr) {
378                 talloc_free(local);
379                 return NULL;
380         }
381
382         return local;
383 }
384
385 static int unixdom_get_fd(struct socket_context *sock)
386 {
387         return sock->fd;
388 }
389
390 static NTSTATUS unixdom_pending(struct socket_context *sock, size_t *npending)
391 {
392         int value = 0;
393         if (ioctl(sock->fd, FIONREAD, &value) == 0) {
394                 *npending = value;
395                 return NT_STATUS_OK;
396         }
397         return map_nt_error_from_unix_common(errno);
398 }
399
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,
412
413         .fn_set_option          = unixdom_set_option,
414
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,
418
419         .fn_get_fd              = unixdom_get_fd
420 };
421
422 _PUBLIC_ const struct socket_ops *socket_unixdom_ops(enum socket_type type)
423 {
424         return &unixdom_ops;
425 }