]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_tcp.c
5f6b6eb49dbd5a09ce3ab7fb7ef23be832259f95
[netatalk.git] / libatalk / dsi / dsi_tcp.c
1 /*
2  * $Id: dsi_tcp.c,v 1.15 2009-10-13 22:55: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_default, "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,
105                        &len);
106
107 #ifdef TCPWRAP
108   {
109     struct request_info req;
110     request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL);
111     fromhost(&req);
112     if (!hosts_access(&req)) {
113       LOG(deny_severity, logtype_default, "refused connect from %s", eval_client(&req));
114       close(dsi->socket);
115       errno = ECONNREFUSED;
116       dsi->socket = -1;
117     }
118   }
119 #endif /* TCPWRAP */
120
121   if (dsi->socket < 0)
122     return -1;
123
124   getitimer(ITIMER_PROF, &itimer);
125   if (0 == (pid = fork()) ) { /* child */
126     static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
127     struct sigaction newact, oldact;
128     u_int8_t block[DSI_BLOCKSIZ];
129     size_t stored;
130
131     /* reset signals */
132     server_reset_signal();
133
134 #ifndef DEBUGGING
135     /* install an alarm to deal with non-responsive connections */
136     newact.sa_handler = timeout_handler;
137     sigemptyset(&newact.sa_mask);
138     newact.sa_flags = 0;
139     sigemptyset(&oldact.sa_mask);
140     oldact.sa_flags = 0;
141     setitimer(ITIMER_PROF, &itimer, NULL);
142
143     if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
144         (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
145         LOG(log_error, logtype_default, "dsi_tcp_open: %s", strerror(errno));
146         exit(EXITERR_SYS);
147     }
148 #endif
149     
150     /* read in commands. this is similar to dsi_receive except
151      * for the fact that we do some sanity checking to prevent
152      * delinquent connections from causing mischief. */
153     
154     /* read in the first two bytes */
155     len = dsi_stream_read(dsi, block, 2);
156     if (!len ) {
157       /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
158       exit(EXITERR_CLNT);
159     }
160     if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
161       LOG(log_error, logtype_default, "dsi_tcp_open: invalid header");
162       exit(EXITERR_CLNT);
163     }      
164     
165     /* read in the rest of the header */
166     stored = 2;
167     while (stored < DSI_BLOCKSIZ) {
168       len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
169       if (len > 0)
170         stored += len;
171       else {
172         LOG(log_error, logtype_default, "dsi_tcp_open: stream_read: %s", strerror(errno));
173         exit(EXITERR_CLNT);
174       }
175     }
176     
177     dsi->header.dsi_flags = block[0];
178     dsi->header.dsi_command = block[1];
179     memcpy(&dsi->header.dsi_requestID, block + 2, 
180            sizeof(dsi->header.dsi_requestID));
181     memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
182     memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
183     memcpy(&dsi->header.dsi_reserved, block + 12,
184            sizeof(dsi->header.dsi_reserved));
185     dsi->clientID = ntohs(dsi->header.dsi_requestID);
186     
187     /* make sure we don't over-write our buffers. */
188     dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
189     
190     stored = 0;
191     while (stored < dsi->cmdlen) {
192       len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
193       if (len > 0)
194         stored += len;
195       else {
196         LOG(log_error, logtype_default, "dsi_tcp_open: stream_read: %s", strerror(errno));
197         exit(EXITERR_CLNT);
198       }
199     }
200     
201     /* stop timer and restore signal handler */
202 #ifndef DEBUGGING
203     memset(&timer, 0, sizeof(timer));
204     setitimer(ITIMER_REAL, &timer, NULL);
205     sigaction(SIGALRM, &oldact, NULL);
206 #endif
207     LOG(log_info, logtype_default,"ASIP session:%u(%d) from %s:%u(%d)", 
208            ntohs(dsi->server.sin_port), dsi->serversock, 
209            inet_ntoa(dsi->client.sin_addr), ntohs(dsi->client.sin_port),
210            dsi->socket);
211   }
212   
213   /* send back our pid */
214   return pid;
215 }
216
217 /* this needs to accept passed in addresses */
218 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
219                  const u_int16_t ipport, const int proxy)
220 {
221   struct servent     *service;
222   struct hostent     *host;
223   int                port;
224
225   dsi->protocol = DSI_TCPIP;
226
227   /* create a socket */
228   if (proxy)
229     dsi->serversock = -1;
230   else if ((dsi->serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
231     return 0;
232       
233   /* find port */
234   if (ipport)
235     port = htons(ipport);
236   else if ((service = getservbyname("afpovertcp", "tcp")))
237     port = service->s_port;
238   else
239     port = htons(DSI_AFPOVERTCP_PORT);
240
241   /* find address */
242   if (!address) 
243     dsi->server.sin_addr.s_addr = htonl(INADDR_ANY);
244   else if (inet_aton(address, &dsi->server.sin_addr) == 0) {
245     LOG(log_info, logtype_default, "dsi_tcp: invalid address (%s)", address);
246     return 0;
247   }
248
249   dsi->server.sin_family = AF_INET;
250   dsi->server.sin_port = port;
251
252   if (!proxy) {
253     /* this deals w/ quick close/opens */    
254 #ifdef SO_REUSEADDR
255     port = 1;
256     setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &port, sizeof(port));
257 #endif
258
259 #ifdef USE_TCP_NODELAY 
260
261 #ifndef SOL_TCP
262 #define SOL_TCP IPPROTO_TCP
263 #endif 
264
265     port = 1;
266     setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &port, sizeof(port));
267 #endif /* USE_TCP_NODELAY */
268
269     /* now, bind the socket and set it up for listening */
270     if ((bind(dsi->serversock, (struct sockaddr *) &dsi->server, 
271               sizeof(dsi->server)) < 0) || 
272         (listen(dsi->serversock, DSI_TCPMAXPEND) < 0)) {
273       close(dsi->serversock);
274       return 0;
275     }
276   }
277
278   /* Point protocol specific functions to tcp versions */
279   dsi->proto_open = dsi_tcp_open;
280   dsi->proto_close = dsi_tcp_close;
281
282   /* get real address for GetStatus. we'll go through the list of 
283    * interfaces if necessary. */
284
285   if (address) {
286       /* address is a parameter, use it 'as is' */
287       return 1;
288   }
289   
290   if (!(host = gethostbyname(hostname)) ) { /* we can't resolve the name */
291
292       LOG(log_info, logtype_default, "dsi_tcp: cannot resolve hostname '%s'", hostname);
293       if (proxy) {
294          /* give up we have nothing to advertise */
295          return 0;
296       }
297   }
298   else {
299       if (( ((struct in_addr *) host->h_addr)->s_addr & htonl(0x7F000000) ) !=  htonl(0x7F000000)) { /* FIXME ugly check */
300           dsi->server.sin_addr.s_addr = ((struct in_addr *) host->h_addr)->s_addr;
301           return 1;
302       }
303       LOG(log_info, logtype_default, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
304   }
305   {
306       char **start, **list;
307       struct ifreq ifr;
308
309       /* get it from the interface list */
310       start = list = getifacelist();
311       while (list && *list) {
312           strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
313           list++;
314
315 #ifndef IFF_SLAVE
316 #define IFF_SLAVE 0
317 #endif
318
319           if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
320             continue;
321
322           if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
323             continue;
324
325           if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
326             continue;
327
328           if (ioctl(dsi->serversock, SIOCGIFADDR, &ifr) < 0)
329             continue;
330         
331           dsi->server.sin_addr.s_addr = 
332             ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
333           LOG(log_info, logtype_default, "dsi_tcp: '%s' on interface '%s' will be used instead.",
334                inet_ntoa(dsi->server.sin_addr), ifr.ifr_name);
335           goto iflist_done;
336       }
337       LOG(log_info, logtype_default, "dsi_tcp (Chooser will not select afp/tcp) \
338 Check to make sure %s is in /etc/hosts and the correct domain is in \
339 /etc/resolv.conf: %s", hostname, strerror(errno));
340
341 iflist_done:
342       if (start)
343           freeifacelist(start);
344   }
345
346   return 1;
347
348 }
349