]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_tcp.c
remove trailing \n char in LOG calls.
[netatalk.git] / libatalk / dsi / dsi_tcp.c
1 /*
2  * $Id: dsi_tcp.c,v 1.9.10.7.2.1 2005-01-11 23:00:42 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()
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     /* install an alarm to deal with non-responsive connections */
139     newact.sa_handler = timeout_handler;
140     sigemptyset(&newact.sa_mask);
141     newact.sa_flags = 0;
142     sigemptyset(&oldact.sa_mask);
143     oldact.sa_flags = 0;
144     setitimer(ITIMER_PROF, &itimer, NULL);
145
146     if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
147         (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
148         LOG(log_error, logtype_default, "dsi_tcp_open: %s", strerror(errno));
149         exit(EXITERR_SYS);
150     }
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_default, "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_default, "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_default, "dsi_tcp_open: stream_read: %s", strerror(errno));
199         exit(EXITERR_CLNT);
200       }
201     }
202     
203     /* stop timer and restore signal handler */
204     memset(&timer, 0, sizeof(timer));
205     setitimer(ITIMER_REAL, &timer, NULL);
206     sigaction(SIGALRM, &oldact, NULL);
207
208     LOG(log_info, logtype_default,"ASIP session:%u(%d) from %s:%u(%d)", 
209            ntohs(dsi->server.sin_port), dsi->serversock, 
210            inet_ntoa(dsi->client.sin_addr), ntohs(dsi->client.sin_port),
211            dsi->socket);
212   }
213   
214   /* send back our pid */
215   return pid;
216 }
217
218 /* this needs to accept passed in addresses */
219 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
220                  const u_int16_t ipport, const int proxy)
221 {
222   struct servent     *service;
223   struct hostent     *host;
224   int                port;
225
226   dsi->protocol = DSI_TCPIP;
227
228   /* create a socket */
229   if (proxy)
230     dsi->serversock = -1;
231   else if ((dsi->serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
232     return 0;
233       
234   /* find port */
235   if (ipport)
236     port = htons(ipport);
237   else if ((service = getservbyname("afpovertcp", "tcp")))
238     port = service->s_port;
239   else
240     port = htons(DSI_AFPOVERTCP_PORT);
241
242   /* find address */
243   if (!address) 
244     dsi->server.sin_addr.s_addr = htonl(INADDR_ANY);
245   else if (inet_aton(address, &dsi->server.sin_addr) == 0) {
246     LOG(log_info, logtype_default, "dsi_tcp: invalid address (%s)", address);
247     return 0;
248   }
249
250   dsi->server.sin_family = AF_INET;
251   dsi->server.sin_port = port;
252
253   if (!proxy) {
254     /* this deals w/ quick close/opens */    
255 #ifdef SO_REUSEADDR
256     port = 1;
257     setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &port, sizeof(port));
258 #endif
259
260 #ifdef USE_TCP_NODELAY 
261
262 #ifndef SOL_TCP
263 #define SOL_TCP IPPROTO_TCP
264 #endif 
265
266     port = 1;
267     setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &port, sizeof(port));
268 #endif /* USE_TCP_NODELAY */
269
270     /* now, bind the socket and set it up for listening */
271     if ((bind(dsi->serversock, (struct sockaddr *) &dsi->server, 
272               sizeof(dsi->server)) < 0) || 
273         (listen(dsi->serversock, DSI_TCPMAXPEND) < 0)) {
274       close(dsi->serversock);
275       return 0;
276     }
277   }
278
279   /* Point protocol specific functions to tcp versions */
280   dsi->proto_open = dsi_tcp_open;
281   dsi->proto_close = dsi_tcp_close;
282
283   /* get real address for GetStatus. we'll go through the list of 
284    * interfaces if necessary. */
285
286   if (address) {
287       /* address is a parameter, use it 'as is' */
288       return 1;
289   }
290   
291   if (!(host = gethostbyname(hostname)) ) { /* we can't resolve the name */
292
293       LOG(log_info, logtype_default, "dsi_tcp: cannot resolve hostname '%s'", hostname);
294       if (proxy) {
295          /* give up we have nothing to advertise */
296          return 0;
297       }
298   }
299   else {
300       if (((struct in_addr *) host->h_addr)->s_addr != 0x100007F) { /* FIXME ugly check */
301           dsi->server.sin_addr.s_addr = ((struct in_addr *) host->h_addr)->s_addr;
302           return 1;
303       }
304       LOG(log_info, logtype_default, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
305   }
306   {
307       char **start, **list;
308       struct ifreq ifr;
309
310       /* get it from the interface list */
311       start = list = getifacelist();
312       while (list && *list) {
313           strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
314           list++;
315
316 #ifndef IFF_SLAVE
317 #define IFF_SLAVE 0
318 #endif
319
320           if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
321             continue;
322
323           if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
324             continue;
325
326           if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
327             continue;
328
329           if (ioctl(dsi->serversock, SIOCGIFADDR, &ifr) < 0)
330             continue;
331         
332           dsi->server.sin_addr.s_addr = 
333             ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
334           LOG(log_info, logtype_default, "dsi_tcp: '%s' on interface '%s' will be used instead.",
335                inet_ntoa(dsi->server.sin_addr), ifr.ifr_name);
336           goto iflist_done;
337       }
338       LOG(log_info, logtype_default, "dsi_tcp (Chooser will not select afp/tcp) \
339 Check to make sure %s is in /etc/hosts and the correct domain is in \
340 /etc/resolv.conf: %s", hostname, strerror(errno));
341
342 iflist_done:
343       if (start)
344           freeifacelist(start);
345   }
346
347   return 1;
348
349 }
350