]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_tcp.c
Merge from branch 2-1
[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         /* Immediateyl mark globally that we're a child now */
131         parent_or_child = 1;
132
133         /* reset signals */
134         server_reset_signal();
135
136 #ifndef DEBUGGING
137         /* install an alarm to deal with non-responsive connections */
138         newact.sa_handler = timeout_handler;
139         sigemptyset(&newact.sa_mask);
140         newact.sa_flags = 0;
141         sigemptyset(&oldact.sa_mask);
142         oldact.sa_flags = 0;
143         setitimer(ITIMER_PROF, &itimer, NULL);
144
145         if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
146             (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
147             LOG(log_error, logtype_dsi, "dsi_tcp_open: %s", strerror(errno));
148             exit(EXITERR_SYS);
149         }
150 #endif
151
152         /* read in commands. this is similar to dsi_receive except
153          * for the fact that we do some sanity checking to prevent
154          * delinquent connections from causing mischief. */
155
156         /* read in the first two bytes */
157         len = dsi_stream_read(dsi, block, 2);
158         if (!len ) {
159             /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
160             exit(EXITERR_CLNT);
161         }
162         if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
163             LOG(log_error, logtype_dsi, "dsi_tcp_open: invalid header");
164             exit(EXITERR_CLNT);
165         }
166
167         /* read in the rest of the header */
168         stored = 2;
169         while (stored < DSI_BLOCKSIZ) {
170             len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
171             if (len > 0)
172                 stored += len;
173             else {
174                 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
175                 exit(EXITERR_CLNT);
176             }
177         }
178
179         dsi->header.dsi_flags = block[0];
180         dsi->header.dsi_command = block[1];
181         memcpy(&dsi->header.dsi_requestID, block + 2,
182                sizeof(dsi->header.dsi_requestID));
183         memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
184         memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
185         memcpy(&dsi->header.dsi_reserved, block + 12,
186                sizeof(dsi->header.dsi_reserved));
187         dsi->clientID = ntohs(dsi->header.dsi_requestID);
188
189         /* make sure we don't over-write our buffers. */
190         dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
191
192         stored = 0;
193         while (stored < dsi->cmdlen) {
194             len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
195             if (len > 0)
196                 stored += len;
197             else {
198                 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
199                 exit(EXITERR_CLNT);
200             }
201         }
202
203         /* stop timer and restore signal handler */
204 #ifndef DEBUGGING
205         memset(&timer, 0, sizeof(timer));
206         setitimer(ITIMER_REAL, &timer, NULL);
207         sigaction(SIGALRM, &oldact, NULL);
208 #endif
209
210         LOG(log_info, logtype_dsi, "AFP/TCP session from %s:%u",
211             getip_string((struct sockaddr *)&dsi->client),
212             getip_port((struct sockaddr *)&dsi->client));
213     }
214
215     /* send back our pid */
216     return pid;
217 }
218
219 /* get it from the interface list */
220 #ifndef IFF_SLAVE
221 #define IFF_SLAVE 0
222 #endif
223
224 static void guess_interface(DSI *dsi, const char *hostname, const char *port)
225 {
226     int fd;
227     char **start, **list;
228     struct ifreq ifr;
229     struct sockaddr_in *sa = (struct sockaddr_in *)&dsi->server;
230
231     start = list = getifacelist();
232     if (!start)
233         return;
234         
235     fd = socket(PF_INET, SOCK_STREAM, 0);
236
237     while (list && *list) {
238         strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
239         list++;
240
241
242         if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
243             continue;
244
245         if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
246             continue;
247
248         if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
249             continue;
250
251         if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
252             continue;
253
254         memset(&dsi->server, 0, sizeof(struct sockaddr_storage));
255         sa->sin_family = AF_INET;
256         sa->sin_port = htons(atoi(port));
257         sa->sin_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
258
259         LOG(log_info, logtype_dsi, "dsi_tcp: '%s:%s' on interface '%s' will be used instead.",
260             getip_string((struct sockaddr *)&dsi->server), port, ifr.ifr_name);
261         goto iflist_done;
262     }
263     LOG(log_info, logtype_dsi, "dsi_tcp (Chooser will not select afp/tcp) "
264         "Check to make sure %s is in /etc/hosts and the correct domain is in "
265         "/etc/resolv.conf: %s", hostname, strerror(errno));
266
267 iflist_done:
268     close(fd);
269     freeifacelist(start);
270 }
271
272
273 #ifndef AI_NUMERICSERV
274 #define AI_NUMERICSERV 0
275 #endif
276
277 /* this needs to accept passed in addresses */
278 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
279                  const char *port, const int proxy)
280 {
281     int                ret;
282     int                flag;
283     struct addrinfo    hints, *servinfo, *p;
284
285     dsi->protocol = DSI_TCPIP;
286
287     /* Prepare hint for getaddrinfo */
288     memset(&hints, 0, sizeof hints);
289 #if !defined(FREEBSD)
290     hints.ai_family = AF_UNSPEC;
291 #endif
292     hints.ai_socktype = SOCK_STREAM;
293     hints.ai_flags = AI_NUMERICSERV;
294
295     if ( ! address) {
296         hints.ai_flags |= AI_PASSIVE;
297 #if defined(FREEBSD)
298         hints.ai_family = AF_INET6;
299 #endif
300     } else {
301         hints.ai_flags |= AI_NUMERICHOST;
302 #if defined(FREEBSD)
303         hints.ai_family = AF_UNSPEC;
304 #endif
305     }
306     if ((ret = getaddrinfo(address ? address : NULL, port ? port : "548", &hints, &servinfo)) != 0) {
307         LOG(log_error, logtype_dsi, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret));
308         return 0;
309     }
310
311     /* create a socket */
312     if (proxy)
313         dsi->serversock = -1;
314     else {
315         /* loop through all the results and bind to the first we can */
316         for (p = servinfo; p != NULL; p = p->ai_next) {
317             if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
318                 LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno));
319                 continue;
320             }
321
322             /*
323              * Set some socket options:
324              * SO_REUSEADDR deals w/ quick close/opens
325              * TCP_NODELAY diables Nagle
326              */
327 #ifdef SO_REUSEADDR
328             flag = 1;
329             setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
330 #endif
331 #if defined(FREEBSD) && defined(IPV6_BINDV6ONLY)
332             int on = 0;
333             setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on));
334 #endif
335
336 #ifdef USE_TCP_NODELAY
337 #ifndef SOL_TCP
338 #define SOL_TCP IPPROTO_TCP
339 #endif
340             flag = 1;
341             setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
342 #endif /* USE_TCP_NODELAY */
343             
344             if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) {
345                 close(dsi->serversock);
346                 LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno));
347                 continue;
348             }
349
350             if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) {
351                 close(dsi->serversock);
352                 LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno));
353                 continue;
354             }
355             
356             break;
357         }
358
359         if (p == NULL)  {
360             LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket");
361             freeaddrinfo(servinfo);
362             return 0;
363         }
364
365         /* Copy struct sockaddr to struct sockaddr_storage */
366         memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
367         freeaddrinfo(servinfo);
368     } /* if (proxy) */
369
370     /* Point protocol specific functions to tcp versions */
371     dsi->proto_open = dsi_tcp_open;
372     dsi->proto_close = dsi_tcp_close;
373
374     /* get real address for GetStatus. */
375
376     if (address) {
377         /* address is a parameter, use it 'as is' */
378         return 1;
379     }
380
381     /* Prepare hint for getaddrinfo */
382     memset(&hints, 0, sizeof hints);
383     hints.ai_family = AF_UNSPEC;
384     hints.ai_socktype = SOCK_STREAM;
385
386     if ((ret = getaddrinfo(hostname, port ? port : "548", &hints, &servinfo)) != 0) {
387         LOG(log_info, logtype_dsi, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(ret));
388         goto interfaces;
389     }
390
391     for (p = servinfo; p != NULL; p = p->ai_next) {
392         if (p->ai_family == AF_INET) { // IPv4
393             struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
394             if ( (ipv4->sin_addr.s_addr & htonl(0x7f000000)) != htonl(0x7f000000) )
395                 break;
396         } else { // IPv6
397             struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
398             unsigned char ipv6loopb[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
399             if ((memcmp(ipv6->sin6_addr.s6_addr, ipv6loopb, 16)) != 0)
400                 break;
401         }
402     }
403
404     if (p) {
405         /* Store found address in dsi->server */
406         memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
407         freeaddrinfo(servinfo);
408         return 1;
409     }
410     LOG(log_info, logtype_dsi, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
411     freeaddrinfo(servinfo);
412
413 interfaces:
414     guess_interface(dsi, hostname, port ? port : "548");
415     return 1;
416 }
417