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