]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_tcp.c
merged logging code into main branch. use configure option --without-logfile to...
[netatalk.git] / libatalk / dsi / dsi_tcp.c
1 /*
2  * $Id: dsi_tcp.c,v 1.6 2002-01-04 04:45:48 sibaz 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(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       LOG(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     memset(&newact, 0, sizeof(newact));
135     newact.sa_handler = timeout_handler;
136     if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
137         (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
138         LOG(log_error, logtype_default, "dsi_tcp_open: %s", strerror(errno));
139         exit(1);
140     }
141     
142     /* read in commands. this is similar to dsi_receive except
143      * for the fact that we do some sanity checking to prevent
144      * delinquent connections from causing mischief. */
145     
146     /* read in the first two bytes */
147     dsi_stream_read(dsi, block, 2);
148     if ((block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
149       LOG(log_error, logtype_default, "dsi_tcp_open: invalid header");
150       exit(1);
151     }      
152     
153     /* read in the rest of the header */
154     stored = 2;
155     while (stored < DSI_BLOCKSIZ) {
156       len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
157       if (len > 0)
158         stored += len;
159       else {
160         LOG(log_error, logtype_default, "dsi_tcp_open: stream_read: %s", strerror(errno));
161         exit(1);
162       }
163     }
164     
165     dsi->header.dsi_flags = block[0];
166     dsi->header.dsi_command = block[1];
167     memcpy(&dsi->header.dsi_requestID, block + 2, 
168            sizeof(dsi->header.dsi_requestID));
169     memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
170     memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
171     memcpy(&dsi->header.dsi_reserved, block + 12,
172            sizeof(dsi->header.dsi_reserved));
173     dsi->clientID = ntohs(dsi->header.dsi_requestID);
174     
175     /* make sure we don't over-write our buffers. */
176     dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
177     
178     stored = 0;
179     while (stored < dsi->cmdlen) {
180       len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
181       if (len > 0)
182         stored += len;
183       else {
184         LOG(log_error, logtype_default, "dsi_tcp_open: stream_read: %s", strerror(errno));
185         exit(1);
186       }
187     }
188     
189     /* restore signal */
190     sigaction(SIGALRM, &oldact, NULL);
191
192     LOG(log_info, logtype_default,"ASIP session:%u(%d) from %s:%u(%d)", 
193            ntohs(dsi->server.sin_port), dsi->serversock, 
194            inet_ntoa(dsi->client.sin_addr), ntohs(dsi->client.sin_port),
195            dsi->socket);
196   }
197   
198   /* send back our pid */
199   return pid;
200 }
201
202 /* this needs to accept passed in addresses */
203 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
204                  const u_int16_t ipport, const int proxy)
205 {
206   struct servent     *service;
207   struct hostent     *host;
208   int                port;
209
210   dsi->protocol = DSI_TCPIP;
211
212   /* create a socket */
213   if (proxy)
214     dsi->serversock = -1;
215   else if ((dsi->serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
216     return 0;
217       
218   /* find port */
219   if (ipport)
220     port = htons(ipport);
221   else if ((service = getservbyname("afpovertcp", "tcp")))
222     port = service->s_port;
223   else
224     port = htons(DSI_AFPOVERTCP_PORT);
225
226   /* find address */
227   if (!address) 
228     dsi->server.sin_addr.s_addr = htonl(INADDR_ANY);
229   else if (inet_aton(address, &dsi->server.sin_addr) == 0) {
230     LOG(log_info, logtype_default, "dsi_tcp: invalid address (%s)", address);
231     return 0;
232   }
233
234   dsi->server.sin_family = AF_INET;
235   dsi->server.sin_port = port;
236
237   if (!proxy) {
238     /* this deals w/ quick close/opens */    
239 #ifdef SO_REUSEADDR
240     port = 1;
241     setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &port, sizeof(port));
242 #endif /* SO_REUSEADDR */
243
244 #ifdef USE_TCP_NODELAY 
245 #ifndef SOL_TCP
246 #define SOL_TCP IPPROTO_TCP
247 #endif /* ! SOL_TCP */
248     port = 1;
249     setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &port, sizeof(port));
250 #endif /* USE_TCP_NODELAY */
251
252     /* now, bind the socket and set it up for listening */
253     if ((bind(dsi->serversock, (struct sockaddr *) &dsi->server, 
254               sizeof(dsi->server)) < 0) || 
255         (listen(dsi->serversock, DSI_TCPMAXPEND) < 0)) {
256       close(dsi->serversock);
257       return 0;
258     }
259   }
260
261   /* get real address for GetStatus. we'll go through the list of 
262    * interfaces if necessary. */
263   if (!address) {
264     if ((host = gethostbyname(hostname))) /* we can resolve the name */
265       dsi->server.sin_addr.s_addr = ((struct in_addr *) host->h_addr)->s_addr;
266     else {
267       char **start, **list;
268       struct ifreq ifr;
269
270       /* get it from the interface list */
271       start = list = getifacelist();
272       while (list && *list) {
273         strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
274         list++;
275
276 #ifndef IFF_SLAVE
277 #define IFF_SLAVE 0
278 #endif /* ! IFF_SLAVE */
279         if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
280           continue;
281
282         if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
283           continue;
284
285         if ((ifr.ifr_flags & IFF_UP) == 0)
286           continue;
287
288         if (ioctl(dsi->serversock, SIOCGIFADDR, &ifr) < 0)
289           continue;
290         
291         dsi->server.sin_addr.s_addr = 
292           ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
293         LOG(log_info, logtype_default, "dsi_tcp: Can't resolve hostname (%s).\n"
294                "%s on interface %s will be used instead.", hostname,
295                inet_ntoa(dsi->server.sin_addr), ifr.ifr_name);
296         goto iflist_done;
297
298       }
299       LOG(log_info, logtype_default, "dsi_tcp (Chooser will not select afp/tcp)\n\
300 Check to make sure %s is in /etc/hosts and the correct domain is in\n\
301 /etc/resolv.conf: %s", hostname, strerror(errno));
302
303 iflist_done:
304       if (start)
305         freeifacelist(start);
306     }
307   }
308
309   /* everything's set up. now point protocol specific functions to 
310    * tcp versions */
311   dsi->proto_open = dsi_tcp_open;
312   dsi->proto_close = dsi_tcp_close;
313   return 1;
314
315 }
316