]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_tcp.c
Merge
[netatalk.git] / libatalk / dsi / dsi_tcp.c
1 /*
2  * $Id: dsi_tcp.c,v 1.25 2009-12-08 22:34:37 didg Exp $
3  *
4  * Copyright (c) 1997, 1998 Adrian Sun (asun@zoology.washington.edu)
5  * All rights reserved. See COPYRIGHT.
6  *
7  * this provides both proto_open() and proto_close() to account for
8  * protocol specific initialization and shutdown procedures. all the
9  * read/write stuff is done in dsi_stream.c.  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif /* HAVE_CONFIG_H */
14
15 #define USE_TCP_NODELAY
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif /* HAVE_UNISTD_H */
23 #include <errno.h>
24 #ifdef HAVE_NETDB_H
25 #include <netdb.h>
26 #endif /* HAVE_NETDB_H */
27 #include <sys/types.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30
31 #ifdef HAVE_STDINT_H
32 #include <stdint.h>
33 #endif /* HAVE_STDINT_H */
34
35 #include <sys/ioctl.h>
36 #ifdef TRU64
37 #include <sys/mbuf.h>
38 #include <net/route.h>
39 #endif /* TRU64 */
40 #include <net/if.h>
41 #include <netinet/tcp.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44
45 #include <signal.h>
46 #include <atalk/logger.h>
47
48 #ifdef __svr4__
49 #include <sys/sockio.h>
50 #endif /* __svr4__ */
51
52 #ifdef TCPWRAP
53 #include <tcpd.h>
54 int allow_severity = log_info;
55 int deny_severity = log_warning;
56 #endif /* TCPWRAP */
57
58 #include <atalk/dsi.h>
59 #include <atalk/compat.h>
60 #include <atalk/util.h>
61 #include <netatalk/endian.h>
62 #include "dsi_private.h"
63
64 #define min(a,b)  ((a) < (b) ? (a) : (b))
65
66 #ifndef DSI_TCPMAXPEND
67 #define DSI_TCPMAXPEND      20       /* max # of pending connections */
68 #endif /* DSI_TCPMAXPEND */
69
70 #ifndef DSI_TCPTIMEOUT
71 #define DSI_TCPTIMEOUT      120     /* timeout in seconds for connections */
72 #endif /* ! DSI_TCPTIMEOUT */
73
74
75 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
76 #ifndef SOCKLEN_T
77 #define SOCKLEN_T unsigned int
78 #endif /* ! SOCKLEN_T */
79
80 static void dsi_tcp_close(DSI *dsi)
81 {
82     if (dsi->socket == -1)
83         return;
84
85     close(dsi->socket);
86     dsi->socket = -1;
87 }
88
89 /* alarm handler for tcp_open */
90 static void timeout_handler(int sig _U_)
91 {
92     LOG(log_error, logtype_dsi, "dsi_tcp_open: connection timed out");
93     exit(EXITERR_CLNT);
94 }
95
96 static struct itimerval itimer;
97 /* accept the socket and do a little sanity checking */
98 static int dsi_tcp_open(DSI *dsi)
99 {
100     pid_t pid;
101     SOCKLEN_T len;
102
103     len = sizeof(dsi->client);
104     dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client, &len);
105
106 #ifdef TCPWRAP
107     {
108         struct request_info req;
109         request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL);
110         fromhost(&req);
111         if (!hosts_access(&req)) {
112             LOG(deny_severity, logtype_dsi, "refused connect from %s", eval_client(&req));
113             close(dsi->socket);
114             errno = ECONNREFUSED;
115             dsi->socket = -1;
116         }
117     }
118 #endif /* TCPWRAP */
119
120     if (dsi->socket < 0)
121         return -1;
122
123     getitimer(ITIMER_PROF, &itimer);
124     if (0 == (pid = fork()) ) { /* child */
125         static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
126         struct sigaction newact, oldact;
127         u_int8_t block[DSI_BLOCKSIZ];
128         size_t stored;
129
130         /* reset signals */
131         server_reset_signal();
132
133 #ifndef DEBUGGING
134         /* install an alarm to deal with non-responsive connections */
135         newact.sa_handler = timeout_handler;
136         sigemptyset(&newact.sa_mask);
137         newact.sa_flags = 0;
138         sigemptyset(&oldact.sa_mask);
139         oldact.sa_flags = 0;
140         setitimer(ITIMER_PROF, &itimer, NULL);
141
142         if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
143             (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
144             LOG(log_error, logtype_dsi, "dsi_tcp_open: %s", strerror(errno));
145             exit(EXITERR_SYS);
146         }
147 #endif
148
149         /* read in commands. this is similar to dsi_receive except
150          * for the fact that we do some sanity checking to prevent
151          * delinquent connections from causing mischief. */
152
153         /* read in the first two bytes */
154         len = dsi_stream_read(dsi, block, 2);
155         if (!len ) {
156             /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
157             exit(EXITERR_CLNT);
158         }
159         if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
160             LOG(log_error, logtype_dsi, "dsi_tcp_open: invalid header");
161             exit(EXITERR_CLNT);
162         }
163
164         /* read in the rest of the header */
165         stored = 2;
166         while (stored < DSI_BLOCKSIZ) {
167             len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
168             if (len > 0)
169                 stored += len;
170             else {
171                 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
172                 exit(EXITERR_CLNT);
173             }
174         }
175
176         dsi->header.dsi_flags = block[0];
177         dsi->header.dsi_command = block[1];
178         memcpy(&dsi->header.dsi_requestID, block + 2,
179                sizeof(dsi->header.dsi_requestID));
180         memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
181         memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
182         memcpy(&dsi->header.dsi_reserved, block + 12,
183                sizeof(dsi->header.dsi_reserved));
184         dsi->clientID = ntohs(dsi->header.dsi_requestID);
185
186         /* make sure we don't over-write our buffers. */
187         dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
188
189         stored = 0;
190         while (stored < dsi->cmdlen) {
191             len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
192             if (len > 0)
193                 stored += len;
194             else {
195                 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
196                 exit(EXITERR_CLNT);
197             }
198         }
199
200         /* stop timer and restore signal handler */
201 #ifndef DEBUGGING
202         memset(&timer, 0, sizeof(timer));
203         setitimer(ITIMER_REAL, &timer, NULL);
204         sigaction(SIGALRM, &oldact, NULL);
205 #endif
206
207         LOG(log_info, logtype_dsi, "AFP/TCP session from %s:%u",
208             getip_string((struct sockaddr *)&dsi->client),
209             getip_port((struct sockaddr *)&dsi->client));
210     }
211
212     /* send back our pid */
213     return pid;
214 }
215
216 /* get it from the interface list */
217 #ifndef IFF_SLAVE
218 #define IFF_SLAVE 0
219 #endif
220
221 static void guess_interface(DSI *dsi, const char *hostname, const char *port)
222 {
223     int fd;
224     char **start, **list;
225     struct ifreq ifr;
226     struct sockaddr_in *sa = (struct sockaddr_in *)&dsi->server;
227
228     start = list = getifacelist();
229     if (!start)
230         return;
231         
232     fd = socket(PF_INET, SOCK_STREAM, 0);
233
234     while (list && *list) {
235         strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
236         list++;
237
238
239         if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
240             continue;
241
242         if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
243             continue;
244
245         if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
246             continue;
247
248         if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
249             continue;
250
251         memset(&dsi->server, 0, sizeof(struct sockaddr_storage));
252         sa->sin_family = AF_INET;
253         sa->sin_port = htons(atoi(port));
254         sa->sin_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
255
256         LOG(log_info, logtype_dsi, "dsi_tcp: '%s:%s' on interface '%s' will be used instead.",
257             getip_string((struct sockaddr *)&dsi->server), port, ifr.ifr_name);
258         goto iflist_done;
259     }
260     LOG(log_info, logtype_dsi, "dsi_tcp (Chooser will not select afp/tcp) "
261         "Check to make sure %s is in /etc/hosts and the correct domain is in "
262         "/etc/resolv.conf: %s", hostname, strerror(errno));
263
264 iflist_done:
265     close(fd);
266     freeifacelist(start);
267 }
268
269
270 #ifndef AI_NUMERICSERV
271 #define AI_NUMERICSERV 0
272 #endif
273
274 /* this needs to accept passed in addresses */
275 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
276                  const char *port, const int proxy)
277 {
278     int                ret;
279     int                flag;
280     struct addrinfo    hints, *servinfo, *p;
281
282     dsi->protocol = DSI_TCPIP;
283
284     /* Prepare hint for getaddrinfo */
285     memset(&hints, 0, sizeof hints);
286 #if !defined(FREEBSD)
287     hints.ai_family = AF_UNSPEC;
288 #endif
289     hints.ai_socktype = SOCK_STREAM;
290     hints.ai_flags = AI_NUMERICSERV;
291
292     if ( ! address) {
293         hints.ai_flags |= AI_PASSIVE;
294 #if defined(FREEBSD)
295         hints.ai_family = AF_INET6;
296 #endif
297     } else {
298         hints.ai_flags |= AI_NUMERICHOST;
299 #if defined(FREEBSD)
300         hints.ai_family = AF_UNSPEC;
301 #endif
302     }
303     if ((ret = getaddrinfo(address ? address : NULL, port ? port : "548", &hints, &servinfo)) != 0) {
304         LOG(log_error, logtype_dsi, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret));
305         return 0;
306     }
307
308     /* create a socket */
309     if (proxy)
310         dsi->serversock = -1;
311     else {
312         /* loop through all the results and bind to the first we can */
313         for (p = servinfo; p != NULL; p = p->ai_next) {
314             if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
315                 LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno));
316                 continue;
317             }
318
319             /*
320              * Set some socket options:
321              * SO_REUSEADDR deals w/ quick close/opens
322              * TCP_NODELAY diables Nagle
323              */
324 #ifdef SO_REUSEADDR
325             flag = 1;
326             setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
327 #endif
328 #if defined(FREEBSD) && defined(IPV6_BINDV6ONLY)
329             int on = 0;
330             setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on));
331 #endif
332
333 #ifdef USE_TCP_NODELAY
334 #ifndef SOL_TCP
335 #define SOL_TCP IPPROTO_TCP
336 #endif
337             flag = 1;
338             setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
339 #endif /* USE_TCP_NODELAY */
340             
341             if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) {
342                 close(dsi->serversock);
343                 LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno));
344                 continue;
345             }
346
347             if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) {
348                 close(dsi->serversock);
349                 LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno));
350                 continue;
351             }
352             
353             break;
354         }
355
356         if (p == NULL)  {
357             LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket");
358             freeaddrinfo(servinfo);
359             return 0;
360         }
361
362         /* Copy struct sockaddr to struct sockaddr_storage */
363         memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
364         freeaddrinfo(servinfo);
365     } /* if (proxy) */
366
367     /* Point protocol specific functions to tcp versions */
368     dsi->proto_open = dsi_tcp_open;
369     dsi->proto_close = dsi_tcp_close;
370
371     /* get real address for GetStatus. */
372
373     if (address) {
374         /* address is a parameter, use it 'as is' */
375         return 1;
376     }
377
378     /* Prepare hint for getaddrinfo */
379     memset(&hints, 0, sizeof hints);
380     hints.ai_family = AF_UNSPEC;
381     hints.ai_socktype = SOCK_STREAM;
382
383     if ((ret = getaddrinfo(hostname, port ? port : "548", &hints, &servinfo)) != 0) {
384         LOG(log_info, logtype_dsi, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(ret));
385         goto interfaces;
386     }
387
388     for (p = servinfo; p != NULL; p = p->ai_next) {
389         if (p->ai_family == AF_INET) { // IPv4
390             struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
391             if ( (ipv4->sin_addr.s_addr & htonl(0x7f000000)) != htonl(0x7f000000) )
392                 break;
393         } else { // IPv6
394             struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
395             unsigned char ipv6loopb[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
396             if ((memcmp(ipv6->sin6_addr.s6_addr, ipv6loopb, 16)) != 0)
397                 break;
398         }
399     }
400
401     if (p) {
402         /* Store found address in dsi->server */
403         memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
404         freeaddrinfo(servinfo);
405         return 1;
406     }
407     LOG(log_info, logtype_dsi, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
408     freeaddrinfo(servinfo);
409
410 interfaces:
411     guess_interface(dsi, hostname, port ? port : "548");
412     return 1;
413 }
414