]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_tcp.c
Trunk-BP: Fixed signal handling.
[netatalk.git] / libatalk / dsi / dsi_tcp.c
1 /*
2  * $Id: dsi_tcp.c,v 1.5.2.1 2002-02-08 00:03:55 srittau 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 <syslog.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   syslog(LOG_ERR, "dsi_tcp_open: connection timed out");
93   exit(1);
94 }
95
96 /* accept the socket and do a little sanity checking */
97 static int dsi_tcp_open(DSI *dsi)
98 {
99   pid_t pid;
100   SOCKLEN_T len;
101
102   len = sizeof(dsi->client);
103   dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client,
104                        &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       syslog(deny_severity, "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   if ((pid = fork()) == 0) { /* child */
124     static const struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
125     struct sigaction newact, oldact;
126     u_int8_t block[DSI_BLOCKSIZ];
127     size_t stored;
128     
129     /* reset a couple signals */
130     signal(SIGTERM, SIG_DFL); 
131     signal(SIGHUP, SIG_DFL);
132
133     /* install an alarm to deal with non-responsive connections */
134     newact.sa_handler = timeout_handler;
135     sigemptyset(&newact.sa_mask);
136     newact.sa_flags = 0;
137     sigemptyset(&oldact.sa_mask);
138     oldact.sa_flags = 0;
139     if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
140         (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
141         syslog(LOG_ERR, "dsi_tcp_open: %s", strerror(errno));
142         exit(1);
143     }
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 <= 0 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
152       syslog(LOG_ERR, "dsi_tcp_open: invalid header");
153       exit(1);
154     }      
155     
156     /* read in the rest of the header */
157     stored = 2;
158     while (stored < DSI_BLOCKSIZ) {
159       len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
160       if (len > 0)
161         stored += len;
162       else {
163         syslog(LOG_ERR, "dsi_tcp_open: stream_read: %s", strerror(errno));
164         exit(1);
165       }
166     }
167     
168     dsi->header.dsi_flags = block[0];
169     dsi->header.dsi_command = block[1];
170     memcpy(&dsi->header.dsi_requestID, block + 2, 
171            sizeof(dsi->header.dsi_requestID));
172     memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
173     memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
174     memcpy(&dsi->header.dsi_reserved, block + 12,
175            sizeof(dsi->header.dsi_reserved));
176     dsi->clientID = ntohs(dsi->header.dsi_requestID);
177     
178     /* make sure we don't over-write our buffers. */
179     dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
180     
181     stored = 0;
182     while (stored < dsi->cmdlen) {
183       len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
184       if (len > 0)
185         stored += len;
186       else {
187         syslog(LOG_ERR, "dsi_tcp_open: stream_read: %s", strerror(errno));
188         exit(1);
189       }
190     }
191     
192     /* restore signal */
193     sigaction(SIGALRM, &oldact, NULL);
194
195     syslog(LOG_INFO,"ASIP session:%u(%d) from %s:%u(%d)", 
196            ntohs(dsi->server.sin_port), dsi->serversock, 
197            inet_ntoa(dsi->client.sin_addr), ntohs(dsi->client.sin_port),
198            dsi->socket);
199   }
200   
201   /* send back our pid */
202   return pid;
203 }
204
205 /* this needs to accept passed in addresses */
206 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
207                  const u_int16_t ipport, const int proxy)
208 {
209   struct servent     *service;
210   struct hostent     *host;
211   int                port;
212
213   dsi->protocol = DSI_TCPIP;
214
215   /* create a socket */
216   if (proxy)
217     dsi->serversock = -1;
218   else if ((dsi->serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
219     return 0;
220       
221   /* find port */
222   if (ipport)
223     port = htons(ipport);
224   else if ((service = getservbyname("afpovertcp", "tcp")))
225     port = service->s_port;
226   else
227     port = htons(DSI_AFPOVERTCP_PORT);
228
229   /* find address */
230   if (!address) 
231     dsi->server.sin_addr.s_addr = htonl(INADDR_ANY);
232   else if (inet_aton(address, &dsi->server.sin_addr) == 0) {
233     syslog(LOG_INFO, "dsi_tcp: invalid address (%s)", address);
234     return 0;
235   }
236
237   dsi->server.sin_family = AF_INET;
238   dsi->server.sin_port = port;
239
240   if (!proxy) {
241     /* this deals w/ quick close/opens */    
242 #ifdef SO_REUSEADDR
243     port = 1;
244     setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &port, sizeof(port));
245 #endif /* SO_REUSEADDR */
246
247 #ifdef USE_TCP_NODELAY 
248 #ifndef SOL_TCP
249 #define SOL_TCP IPPROTO_TCP
250 #endif /* ! SOL_TCP */
251     port = 1;
252     setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &port, sizeof(port));
253 #endif /* USE_TCP_NODELAY */
254
255     /* now, bind the socket and set it up for listening */
256     if ((bind(dsi->serversock, (struct sockaddr *) &dsi->server, 
257               sizeof(dsi->server)) < 0) || 
258         (listen(dsi->serversock, DSI_TCPMAXPEND) < 0)) {
259       close(dsi->serversock);
260       return 0;
261     }
262   }
263
264   /* get real address for GetStatus. we'll go through the list of 
265    * interfaces if necessary. */
266   if (!address) {
267     if ((host = gethostbyname(hostname))) /* we can resolve the name */
268       dsi->server.sin_addr.s_addr = ((struct in_addr *) host->h_addr)->s_addr;
269     else {
270       char **start, **list;
271       struct ifreq ifr;
272
273       /* get it from the interface list */
274       start = list = getifacelist();
275       while (list && *list) {
276         strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
277         list++;
278
279 #ifndef IFF_SLAVE
280 #define IFF_SLAVE 0
281 #endif /* ! IFF_SLAVE */
282         if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
283           continue;
284
285         if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
286           continue;
287
288         if ((ifr.ifr_flags & IFF_UP) == 0)
289           continue;
290
291         if (ioctl(dsi->serversock, SIOCGIFADDR, &ifr) < 0)
292           continue;
293         
294         dsi->server.sin_addr.s_addr = 
295           ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
296         syslog(LOG_INFO, "dsi_tcp: Can't resolve hostname (%s).\n"
297                "%s on interface %s will be used instead.", hostname,
298                inet_ntoa(dsi->server.sin_addr), ifr.ifr_name);
299         goto iflist_done;
300
301       }
302       syslog(LOG_INFO, "dsi_tcp (Chooser will not select afp/tcp)\n\
303 Check to make sure %s is in /etc/hosts and the correct domain is in\n\
304 /etc/resolv.conf: %s", hostname, strerror(errno));
305
306 iflist_done:
307       if (start)
308         freeifacelist(start);
309     }
310   }
311
312   /* everything's set up. now point protocol specific functions to 
313    * tcp versions */
314   dsi->proto_open = dsi_tcp_open;
315   dsi->proto_close = dsi_tcp_close;
316   return 1;
317
318 }
319