]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_tcp.c
implemented config.h
[netatalk.git] / libatalk / dsi / dsi_tcp.c
1 /*
2  * Copyright (c) 1997, 1998 Adrian Sun (asun@zoology.washington.edu)
3  * All rights reserved. See COPYRIGHT.
4  *
5  * this provides both proto_open() and proto_close() to account for
6  * protocol specific initialization and shutdown procedures. all the
7  * read/write stuff is done in dsi_stream.c.  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #define USE_TCP_NODELAY
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <netdb.h>
21 #include <sys/types.h>
22 #include <sys/time.h>
23 #include <sys/socket.h>
24
25 #include <sys/ioctl.h>
26 #include <net/if.h>
27 #include <netinet/tcp.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30
31 #include <signal.h>
32 #include <syslog.h>
33
34 #ifdef __svr4__
35 #include <sys/sockio.h>
36 #endif
37
38 #ifdef TCPWRAP
39 #include <tcpd.h>
40 int allow_severity = LOG_INFO;
41 int deny_severity = LOG_WARNING;
42 #endif
43
44 #include <atalk/dsi.h>
45 #include <atalk/compat.h>
46 #include <atalk/util.h>
47 #include <netatalk/endian.h>
48 #include "dsi_private.h"
49
50 #define min(a,b)  ((a) < (b) ? (a) : (b))
51
52 #ifndef DSI_TCPMAXPEND
53 #define DSI_TCPMAXPEND      20       /* max # of pending connections */
54 #endif
55
56 #ifndef DSI_TCPTIMEOUT
57 #define DSI_TCPTIMEOUT      120     /* timeout in seconds for connections */
58 #endif
59
60
61 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
62 #ifndef SOCKLEN_T
63 #define SOCKLEN_T unsigned int
64 #endif
65
66 static void dsi_tcp_close(DSI *dsi)
67 {
68   if (dsi->socket == -1)
69     return;
70
71   close(dsi->socket);
72   dsi->socket = -1;
73 }
74
75 /* alarm handler for tcp_open */
76 static void timeout_handler()
77 {
78   syslog(LOG_ERR, "dsi_tcp_open: connection timed out");
79   exit(1);
80 }
81
82 /* accept the socket and do a little sanity checking */
83 static int dsi_tcp_open(DSI *dsi)
84 {
85   pid_t pid;
86   SOCKLEN_T len;
87
88   len = sizeof(dsi->client);
89   dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client,
90                        &len);
91
92 #ifdef TCPWRAP
93   {
94     struct request_info req;
95     request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL);
96     fromhost(&req);
97     if (!hosts_access(&req)) {
98       syslog(deny_severity, "refused connect from %s", eval_client(&req));
99       close(dsi->socket);
100       errno = ECONNREFUSED;
101       dsi->socket = -1;
102     }
103   }
104 #endif
105
106   if (dsi->socket < 0)
107     return -1;
108
109   if ((pid = fork()) == 0) { /* child */
110     static const struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
111     struct sigaction newact, oldact;
112     u_int8_t block[DSI_BLOCKSIZ];
113     size_t stored;
114     
115     /* reset a couple signals */
116     signal(SIGTERM, SIG_DFL); 
117     signal(SIGHUP, SIG_DFL);
118
119     /* install an alarm to deal with non-responsive connections */
120     memset(&newact, 0, sizeof(newact));
121     newact.sa_handler = timeout_handler;
122     if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
123         (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
124         syslog(LOG_ERR, "dsi_tcp_open: %m");
125         exit(1);
126     }
127     
128     /* read in commands. this is similar to dsi_receive except
129      * for the fact that we do some sanity checking to prevent
130      * delinquent connections from causing mischief. */
131     
132     /* read in the first two bytes */
133     dsi_stream_read(dsi, block, 2);
134     if ((block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
135       syslog(LOG_ERR, "dsi_tcp_open: invalid header");
136       exit(1);
137     }      
138     
139     /* read in the rest of the header */
140     stored = 2;
141     while (stored < DSI_BLOCKSIZ) {
142       len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
143       if (len > 0)
144         stored += len;
145       else {
146         syslog(LOG_ERR, "dsi_tcp_open: stream_read: %m");
147         exit(1);
148       }
149     }
150     
151     dsi->header.dsi_flags = block[0];
152     dsi->header.dsi_command = block[1];
153     memcpy(&dsi->header.dsi_requestID, block + 2, 
154            sizeof(dsi->header.dsi_requestID));
155     memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
156     memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
157     memcpy(&dsi->header.dsi_reserved, block + 12,
158            sizeof(dsi->header.dsi_reserved));
159     dsi->clientID = ntohs(dsi->header.dsi_requestID);
160     
161     /* make sure we don't over-write our buffers. */
162     dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
163     
164     stored = 0;
165     while (stored < dsi->cmdlen) {
166       len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
167       if (len > 0)
168         stored += len;
169       else {
170         syslog(LOG_ERR, "dsi_tcp_open: stream_read: %m");
171         exit(1);
172       }
173     }
174     
175     /* restore signal */
176     sigaction(SIGALRM, &oldact, NULL);
177
178     syslog(LOG_INFO,"ASIP session:%u(%d) from %s:%u(%d)", 
179            ntohs(dsi->server.sin_port), dsi->serversock, 
180            inet_ntoa(dsi->client.sin_addr), ntohs(dsi->client.sin_port),
181            dsi->socket);
182   }
183   
184   /* send back our pid */
185   return pid;
186 }
187
188 /* this needs to accept passed in addresses */
189 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
190                  const u_int16_t ipport, const int proxy)
191 {
192   struct servent     *service;
193   struct hostent     *host;
194   int                port;
195
196   dsi->protocol = DSI_TCPIP;
197
198   /* create a socket */
199   if (proxy)
200     dsi->serversock = -1;
201   else if ((dsi->serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
202     return 0;
203       
204   /* find port */
205   if (ipport)
206     port = htons(ipport);
207   else if ((service = getservbyname("afpovertcp", "tcp")))
208     port = service->s_port;
209   else
210     port = htons(DSI_AFPOVERTCP_PORT);
211
212   /* find address */
213   if (!address) 
214     dsi->server.sin_addr.s_addr = htonl(INADDR_ANY);
215   else if (inet_aton(address, &dsi->server.sin_addr) == 0) {
216     syslog(LOG_INFO, "dsi_tcp: invalid address (%s)", address);
217     return 0;
218   }
219
220   dsi->server.sin_family = AF_INET;
221   dsi->server.sin_port = port;
222
223   if (!proxy) {
224     /* this deals w/ quick close/opens */    
225 #ifdef SO_REUSEADDR
226     port = 1;
227     setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &port, sizeof(port));
228 #endif
229
230 #ifdef USE_TCP_NODELAY 
231 #ifndef SOL_TCP
232 #define SOL_TCP IPPROTO_TCP
233 #endif
234     port = 1;
235     setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &port, sizeof(port));
236 #endif
237
238     /* now, bind the socket and set it up for listening */
239     if ((bind(dsi->serversock, (struct sockaddr *) &dsi->server, 
240               sizeof(dsi->server)) < 0) || 
241         (listen(dsi->serversock, DSI_TCPMAXPEND) < 0)) {
242       close(dsi->serversock);
243       return 0;
244     }
245   }
246
247   /* get real address for GetStatus. we'll go through the list of 
248    * interfaces if necessary. */
249   if (!address) {
250     if ((host = gethostbyname(hostname))) /* we can resolve the name */
251       dsi->server.sin_addr.s_addr = ((struct in_addr *) host->h_addr)->s_addr;
252     else {
253       char **start, **list;
254       struct ifreq ifr;
255
256       /* get it from the interface list */
257       start = list = getifacelist();
258       while (list && *list) {
259         strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
260         list++;
261
262 #ifndef IFF_SLAVE
263 #define IFF_SLAVE 0
264 #endif
265         if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
266           continue;
267
268         if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
269           continue;
270
271         if ((ifr.ifr_flags & IFF_UP) == 0)
272           continue;
273
274         if (ioctl(dsi->serversock, SIOCGIFADDR, &ifr) < 0)
275           continue;
276         
277         dsi->server.sin_addr.s_addr = 
278           ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
279         syslog(LOG_INFO, "dsi_tcp: Can't resolve hostname (%s).\n"
280                "%s on interface %s will be used instead.", hostname,
281                inet_ntoa(dsi->server.sin_addr), ifr.ifr_name);
282         goto iflist_done;
283
284       }
285       syslog(LOG_INFO, "dsi_tcp (Chooser will not select afp/tcp)\n\
286 Check to make sure %s is in /etc/hosts and the correct domain is in\n\
287 /etc/resolv.conf: %m", hostname);
288
289 iflist_done:
290       if (start)
291         freeifacelist(start);
292     }
293   }
294
295   /* everything's set up. now point protocol specific functions to 
296    * tcp versions */
297   dsi->proto_open = dsi_tcp_open;
298   dsi->proto_close = dsi_tcp_close;
299   return 1;
300
301 }
302