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