2 * Copyright (c) 1997, 1998 Adrian Sun (asun@zoology.washington.edu)
3 * All rights reserved. See COPYRIGHT.
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. */
11 #endif /* HAVE_CONFIG_H */
20 #endif /* HAVE_NETDB_H */
21 #include <sys/types.h>
23 #include <sys/socket.h>
26 #include <sys/ioctl.h>
29 #include <net/route.h>
32 #include <netinet/tcp.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
37 #include <atalk/logger.h>
40 #include <sys/sockio.h>
45 int allow_severity = log_info;
46 int deny_severity = log_warning;
49 #include <atalk/dsi.h>
50 #include <atalk/compat.h>
51 #include <atalk/util.h>
52 #include <atalk/errchk.h>
54 #define min(a,b) ((a) < (b) ? (a) : (b))
56 #ifndef DSI_TCPMAXPEND
57 #define DSI_TCPMAXPEND 20 /* max # of pending connections */
58 #endif /* DSI_TCPMAXPEND */
60 #ifndef DSI_TCPTIMEOUT
61 #define DSI_TCPTIMEOUT 120 /* timeout in seconds for connections */
62 #endif /* ! DSI_TCPTIMEOUT */
65 /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
67 #define SOCKLEN_T unsigned int
68 #endif /* ! SOCKLEN_T */
70 static void dsi_tcp_close(DSI *dsi)
72 if (dsi->socket == -1)
79 /* alarm handler for tcp_open */
80 static void timeout_handler(int sig _U_)
82 LOG(log_error, logtype_dsi, "dsi_tcp_open: connection timed out");
87 * Allocate DSI read buffer and read-ahead buffer
89 static void dsi_init_buffer(DSI *dsi)
91 if ((dsi->commands = malloc(dsi->server_quantum)) == NULL) {
92 LOG(log_error, logtype_dsi, "dsi_init_buffer: OOM");
93 AFP_PANIC("OOM in dsi_init_buffer");
96 /* dsi_peek() read ahead buffer, default is 12 * 300k = 3,6 MB (Apr 2011) */
97 if ((dsi->buffer = malloc(dsi->dsireadbuf * dsi->server_quantum)) == NULL) {
98 LOG(log_error, logtype_dsi, "dsi_init_buffer: OOM");
99 AFP_PANIC("OOM in dsi_init_buffer");
101 dsi->start = dsi->buffer;
102 dsi->eof = dsi->buffer;
103 dsi->end = dsi->buffer + (dsi->dsireadbuf * dsi->server_quantum);
106 static struct itimerval itimer;
107 /* accept the socket and do a little sanity checking */
108 static int dsi_tcp_open(DSI *dsi)
113 len = sizeof(dsi->client);
114 dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client, &len);
118 struct request_info req;
119 request_init(&req, RQ_DAEMON, "afpd", RQ_FILE, dsi->socket, NULL);
121 if (!hosts_access(&req)) {
122 LOG(deny_severity, logtype_dsi, "refused connect from %s", eval_client(&req));
124 errno = ECONNREFUSED;
133 getitimer(ITIMER_PROF, &itimer);
134 if (0 == (pid = fork()) ) { /* child */
135 static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
136 struct sigaction newact, oldact;
137 uint8_t block[DSI_BLOCKSIZ];
141 server_reset_signal();
144 /* install an alarm to deal with non-responsive connections */
145 newact.sa_handler = timeout_handler;
146 sigemptyset(&newact.sa_mask);
148 sigemptyset(&oldact.sa_mask);
150 setitimer(ITIMER_PROF, &itimer, NULL);
152 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
153 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
154 LOG(log_error, logtype_dsi, "dsi_tcp_open: %s", strerror(errno));
159 dsi_init_buffer(dsi);
161 /* read in commands. this is similar to dsi_receive except
162 * for the fact that we do some sanity checking to prevent
163 * delinquent connections from causing mischief. */
165 /* read in the first two bytes */
166 len = dsi_stream_read(dsi, block, 2);
168 /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
171 if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
172 LOG(log_error, logtype_dsi, "dsi_tcp_open: invalid header");
176 /* read in the rest of the header */
178 while (stored < DSI_BLOCKSIZ) {
179 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
183 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
188 dsi->header.dsi_flags = block[0];
189 dsi->header.dsi_command = block[1];
190 memcpy(&dsi->header.dsi_requestID, block + 2,
191 sizeof(dsi->header.dsi_requestID));
192 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
193 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
194 memcpy(&dsi->header.dsi_reserved, block + 12,
195 sizeof(dsi->header.dsi_reserved));
196 dsi->clientID = ntohs(dsi->header.dsi_requestID);
198 /* make sure we don't over-write our buffers. */
199 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), dsi->server_quantum);
202 while (stored < dsi->cmdlen) {
203 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
207 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
212 /* stop timer and restore signal handler */
214 memset(&timer, 0, sizeof(timer));
215 setitimer(ITIMER_REAL, &timer, NULL);
216 sigaction(SIGALRM, &oldact, NULL);
219 LOG(log_info, logtype_dsi, "AFP/TCP session from %s:%u",
220 getip_string((struct sockaddr *)&dsi->client),
221 getip_port((struct sockaddr *)&dsi->client));
224 /* send back our pid */
228 /* get it from the interface list */
233 static void guess_interface(DSI *dsi, const char *hostname, const char *port)
236 char **start, **list;
238 struct sockaddr_in *sa = (struct sockaddr_in *)&dsi->server;
240 start = list = getifacelist();
244 fd = socket(PF_INET, SOCK_STREAM, 0);
246 while (list && *list) {
247 strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
251 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
254 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
257 if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
260 if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
263 memset(&dsi->server, 0, sizeof(struct sockaddr_storage));
264 sa->sin_family = AF_INET;
265 sa->sin_port = htons(atoi(port));
266 sa->sin_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
268 LOG(log_info, logtype_dsi, "dsi_tcp: '%s:%s' on interface '%s' will be used instead.",
269 getip_string((struct sockaddr *)&dsi->server), port, ifr.ifr_name);
273 LOG(log_note, logtype_dsi, "dsi_tcp: couldn't find network interface with IP address to advertice, "
274 "check to make sure \"%s\" is in /etc/hosts or can be resolved with DNS, or "
275 "add a netinterface that is not a loopback or point-2-point type", hostname);
279 freeifacelist(start);
283 #ifndef AI_NUMERICSERV
284 #define AI_NUMERICSERV 0
287 /* this needs to accept passed in addresses */
288 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *inaddress, const char *inport)
295 struct addrinfo hints, *servinfo, *p;
297 /* Check whether address is of the from IP:PORT and split */
300 if (address && strchr(address, ':')) {
301 EC_NULL_LOG( address = a = strdup(address) );
307 /* Prepare hint for getaddrinfo */
308 memset(&hints, 0, sizeof hints);
309 #if !defined(FREEBSD)
310 hints.ai_family = AF_UNSPEC;
312 hints.ai_socktype = SOCK_STREAM;
313 hints.ai_flags = AI_NUMERICSERV;
316 hints.ai_flags |= AI_PASSIVE;
318 hints.ai_family = AF_INET6;
321 hints.ai_flags |= AI_NUMERICHOST;
323 hints.ai_family = AF_UNSPEC;
326 if ((ret = getaddrinfo(address ? address : NULL, port, &hints, &servinfo)) != 0) {
327 LOG(log_error, logtype_dsi, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret));
331 /* loop through all the results and bind to the first we can */
332 for (p = servinfo; p != NULL; p = p->ai_next) {
333 if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
334 LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno));
339 * Set some socket options:
340 * SO_REUSEADDR deals w/ quick close/opens
341 * TCP_NODELAY diables Nagle
345 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
347 #if defined(FREEBSD) && defined(IPV6_BINDV6ONLY)
349 setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on));
353 #define SOL_TCP IPPROTO_TCP
356 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
358 if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) {
359 close(dsi->serversock);
360 LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno));
364 if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) {
365 close(dsi->serversock);
366 LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno));
374 LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket");
375 freeaddrinfo(servinfo);
379 /* Copy struct sockaddr to struct sockaddr_storage */
380 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
381 freeaddrinfo(servinfo);
383 /* Point protocol specific functions to tcp versions */
384 dsi->proto_open = dsi_tcp_open;
385 dsi->proto_close = dsi_tcp_close;
387 /* get real address for GetStatus. */
390 /* address is a parameter, use it 'as is' */
394 /* Prepare hint for getaddrinfo */
395 memset(&hints, 0, sizeof hints);
396 hints.ai_family = AF_UNSPEC;
397 hints.ai_socktype = SOCK_STREAM;
399 if ((err = getaddrinfo(hostname, port, &hints, &servinfo)) != 0) {
400 LOG(log_info, logtype_dsi, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(err));
404 for (p = servinfo; p != NULL; p = p->ai_next) {
405 if (p->ai_family == AF_INET) { // IPv4
406 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
407 if ( (ipv4->sin_addr.s_addr & htonl(0x7f000000)) != htonl(0x7f000000) )
410 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
411 unsigned char ipv6loopb[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
412 if ((memcmp(ipv6->sin6_addr.s6_addr, ipv6loopb, 16)) != 0)
418 /* Store found address in dsi->server */
419 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
420 freeaddrinfo(servinfo);
423 LOG(log_info, logtype_dsi, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
424 freeaddrinfo(servinfo);
427 guess_interface(dsi, hostname, port ? port : "548");