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);
107 * Free any allocated ressources of the master afpd DSI objects and close server socket
109 void dsi_free(DSI *dsi)
111 close(dsi->serversock);
112 dsi->serversock = -1;
115 dsi->commands = NULL;
121 free(dsi->bonjourname);
122 dsi->bonjourname = NULL;
126 static struct itimerval itimer;
127 /* accept the socket and do a little sanity checking */
128 static pid_t dsi_tcp_open(DSI *dsi)
133 len = sizeof(dsi->client);
134 dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client, &len);
138 struct request_info req;
139 request_init(&req, RQ_DAEMON, "afpd", RQ_FILE, dsi->socket, NULL);
141 if (!hosts_access(&req)) {
142 LOG(deny_severity, logtype_dsi, "refused connect from %s", eval_client(&req));
144 errno = ECONNREFUSED;
153 getitimer(ITIMER_PROF, &itimer);
154 if (0 == (pid = fork()) ) { /* child */
155 static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
156 struct sigaction newact, oldact;
157 uint8_t block[DSI_BLOCKSIZ];
161 server_reset_signal();
164 /* install an alarm to deal with non-responsive connections */
165 newact.sa_handler = timeout_handler;
166 sigemptyset(&newact.sa_mask);
168 sigemptyset(&oldact.sa_mask);
170 setitimer(ITIMER_PROF, &itimer, NULL);
172 if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
173 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
174 LOG(log_error, logtype_dsi, "dsi_tcp_open: %s", strerror(errno));
179 dsi_init_buffer(dsi);
181 /* read in commands. this is similar to dsi_receive except
182 * for the fact that we do some sanity checking to prevent
183 * delinquent connections from causing mischief. */
185 /* read in the first two bytes */
186 len = dsi_stream_read(dsi, block, 2);
188 /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
191 if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
192 LOG(log_error, logtype_dsi, "dsi_tcp_open: invalid header");
196 /* read in the rest of the header */
198 while (stored < DSI_BLOCKSIZ) {
199 len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
203 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
208 dsi->header.dsi_flags = block[0];
209 dsi->header.dsi_command = block[1];
210 memcpy(&dsi->header.dsi_requestID, block + 2,
211 sizeof(dsi->header.dsi_requestID));
212 memcpy(&dsi->header.dsi_data.dsi_code, block + 4, sizeof(dsi->header.dsi_data.dsi_code));
213 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
214 memcpy(&dsi->header.dsi_reserved, block + 12,
215 sizeof(dsi->header.dsi_reserved));
216 dsi->clientID = ntohs(dsi->header.dsi_requestID);
218 /* make sure we don't over-write our buffers. */
219 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), dsi->server_quantum);
222 while (stored < dsi->cmdlen) {
223 len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
227 LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
232 /* stop timer and restore signal handler */
234 memset(&timer, 0, sizeof(timer));
235 setitimer(ITIMER_REAL, &timer, NULL);
236 sigaction(SIGALRM, &oldact, NULL);
239 LOG(log_info, logtype_dsi, "AFP/TCP session from %s:%u",
240 getip_string((struct sockaddr *)&dsi->client),
241 getip_port((struct sockaddr *)&dsi->client));
244 /* send back our pid */
248 /* get it from the interface list */
253 static void guess_interface(DSI *dsi, const char *hostname, const char *port)
256 char **start, **list;
258 struct sockaddr_in *sa = (struct sockaddr_in *)&dsi->server;
260 start = list = getifacelist();
264 fd = socket(PF_INET, SOCK_STREAM, 0);
266 while (list && *list) {
267 strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
271 if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
274 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
277 if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
280 if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
283 memset(&dsi->server, 0, sizeof(struct sockaddr_storage));
284 sa->sin_family = AF_INET;
285 sa->sin_port = htons(atoi(port));
286 sa->sin_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
288 LOG(log_info, logtype_dsi, "dsi_tcp: '%s:%s' on interface '%s' will be used instead.",
289 getip_string((struct sockaddr *)&dsi->server), port, ifr.ifr_name);
293 LOG(log_note, logtype_dsi, "dsi_tcp: couldn't find network interface with IP address to advertice, "
294 "check to make sure \"%s\" is in /etc/hosts or can be resolved with DNS, or "
295 "add a netinterface that is not a loopback or point-2-point type", hostname);
299 freeifacelist(start);
303 #ifndef AI_NUMERICSERV
304 #define AI_NUMERICSERV 0
307 /* this needs to accept passed in addresses */
308 int dsi_tcp_init(DSI *dsi, const char *hostname, const char *inaddress, const char *inport)
315 struct addrinfo hints, *servinfo, *p;
317 /* Check whether address is of the from IP:PORT and split */
320 if (address && strchr(address, ':')) {
321 EC_NULL_LOG( address = a = strdup(address) );
327 /* Prepare hint for getaddrinfo */
328 memset(&hints, 0, sizeof hints);
329 #if !defined(FREEBSD)
330 hints.ai_family = AF_UNSPEC;
332 hints.ai_socktype = SOCK_STREAM;
333 hints.ai_flags = AI_NUMERICSERV;
336 hints.ai_flags |= AI_PASSIVE;
338 hints.ai_family = AF_INET6;
341 hints.ai_flags |= AI_NUMERICHOST;
343 hints.ai_family = AF_UNSPEC;
346 if ((ret = getaddrinfo(address ? address : NULL, port, &hints, &servinfo)) != 0) {
347 LOG(log_error, logtype_dsi, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret));
351 /* loop through all the results and bind to the first we can */
352 for (p = servinfo; p != NULL; p = p->ai_next) {
353 if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
354 LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno));
359 * Set some socket options:
360 * SO_REUSEADDR deals w/ quick close/opens
361 * TCP_NODELAY diables Nagle
365 setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
367 #if defined(FREEBSD) && defined(IPV6_BINDV6ONLY)
369 setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on));
373 #define SOL_TCP IPPROTO_TCP
376 setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
378 if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) {
379 close(dsi->serversock);
380 LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno));
384 if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) {
385 close(dsi->serversock);
386 LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno));
394 LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket");
395 freeaddrinfo(servinfo);
399 /* Copy struct sockaddr to struct sockaddr_storage */
400 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
401 freeaddrinfo(servinfo);
403 /* Point protocol specific functions to tcp versions */
404 dsi->proto_open = dsi_tcp_open;
405 dsi->proto_close = dsi_tcp_close;
407 /* get real address for GetStatus. */
410 /* address is a parameter, use it 'as is' */
414 /* Prepare hint for getaddrinfo */
415 memset(&hints, 0, sizeof hints);
416 hints.ai_family = AF_UNSPEC;
417 hints.ai_socktype = SOCK_STREAM;
419 if ((err = getaddrinfo(hostname, port, &hints, &servinfo)) != 0) {
420 LOG(log_info, logtype_dsi, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(err));
424 for (p = servinfo; p != NULL; p = p->ai_next) {
425 if (p->ai_family == AF_INET) { // IPv4
426 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
427 if ( (ipv4->sin_addr.s_addr & htonl(0x7f000000)) != htonl(0x7f000000) )
430 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
431 unsigned char ipv6loopb[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
432 if ((memcmp(ipv6->sin6_addr.s6_addr, ipv6loopb, 16)) != 0)
438 /* Store found address in dsi->server */
439 memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
440 freeaddrinfo(servinfo);
443 LOG(log_info, logtype_dsi, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
444 freeaddrinfo(servinfo);
447 guess_interface(dsi, hostname, port ? port : "548");