PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/netatalk-2.2.3/libatalk/dsi/dsi_tcp.c

#
C | 413 lines | 303 code | 70 blank | 40 comment | 53 complexity | dcd4267521a9e7fd56a5fec49ca400ad MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * $Id: dsi_tcp.c,v 1.25 2009-12-08 22:34:37 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. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif /* HAVE_CONFIG_H */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #ifdef HAVE_UNISTD_H
  17. #include <unistd.h>
  18. #endif /* HAVE_UNISTD_H */
  19. #include <errno.h>
  20. #ifdef HAVE_NETDB_H
  21. #include <netdb.h>
  22. #endif /* HAVE_NETDB_H */
  23. #include <sys/types.h>
  24. #include <sys/time.h>
  25. #include <sys/socket.h>
  26. #ifdef HAVE_STDINT_H
  27. #include <stdint.h>
  28. #endif /* HAVE_STDINT_H */
  29. #include <sys/ioctl.h>
  30. #ifdef TRU64
  31. #include <sys/mbuf.h>
  32. #include <net/route.h>
  33. #endif /* TRU64 */
  34. #include <net/if.h>
  35. #include <netinet/tcp.h>
  36. #include <netinet/in.h>
  37. #include <arpa/inet.h>
  38. #include <signal.h>
  39. #include <atalk/logger.h>
  40. #ifdef __svr4__
  41. #include <sys/sockio.h>
  42. #endif /* __svr4__ */
  43. #ifdef TCPWRAP
  44. #include <tcpd.h>
  45. int allow_severity = log_info;
  46. int deny_severity = log_warning;
  47. #endif /* TCPWRAP */
  48. #include <atalk/dsi.h>
  49. #include <atalk/compat.h>
  50. #include <atalk/util.h>
  51. #include <netatalk/endian.h>
  52. #include "dsi_private.h"
  53. #define min(a,b) ((a) < (b) ? (a) : (b))
  54. #ifndef DSI_TCPMAXPEND
  55. #define DSI_TCPMAXPEND 20 /* max # of pending connections */
  56. #endif /* DSI_TCPMAXPEND */
  57. #ifndef DSI_TCPTIMEOUT
  58. #define DSI_TCPTIMEOUT 120 /* timeout in seconds for connections */
  59. #endif /* ! DSI_TCPTIMEOUT */
  60. /* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
  61. #ifndef SOCKLEN_T
  62. #define SOCKLEN_T unsigned int
  63. #endif /* ! SOCKLEN_T */
  64. static void dsi_tcp_close(DSI *dsi)
  65. {
  66. if (dsi->socket == -1)
  67. return;
  68. close(dsi->socket);
  69. dsi->socket = -1;
  70. }
  71. /* alarm handler for tcp_open */
  72. static void timeout_handler(int sig _U_)
  73. {
  74. LOG(log_error, logtype_dsi, "dsi_tcp_open: connection timed out");
  75. exit(EXITERR_CLNT);
  76. }
  77. static struct itimerval itimer;
  78. /* accept the socket and do a little sanity checking */
  79. static int dsi_tcp_open(DSI *dsi)
  80. {
  81. pid_t pid;
  82. SOCKLEN_T len;
  83. len = sizeof(dsi->client);
  84. dsi->socket = accept(dsi->serversock, (struct sockaddr *) &dsi->client, &len);
  85. #ifdef TCPWRAP
  86. {
  87. struct request_info req;
  88. request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL);
  89. fromhost(&req);
  90. if (!hosts_access(&req)) {
  91. LOG(deny_severity, logtype_dsi, "refused connect from %s", eval_client(&req));
  92. close(dsi->socket);
  93. errno = ECONNREFUSED;
  94. dsi->socket = -1;
  95. }
  96. }
  97. #endif /* TCPWRAP */
  98. if (dsi->socket < 0)
  99. return -1;
  100. getitimer(ITIMER_PROF, &itimer);
  101. if (0 == (pid = fork()) ) { /* child */
  102. static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}};
  103. struct sigaction newact, oldact;
  104. u_int8_t block[DSI_BLOCKSIZ];
  105. size_t stored;
  106. /* Immediateyl mark globally that we're a child now */
  107. parent_or_child = 1;
  108. /* reset signals */
  109. server_reset_signal();
  110. #ifndef DEBUGGING
  111. /* install an alarm to deal with non-responsive connections */
  112. newact.sa_handler = timeout_handler;
  113. sigemptyset(&newact.sa_mask);
  114. newact.sa_flags = 0;
  115. sigemptyset(&oldact.sa_mask);
  116. oldact.sa_flags = 0;
  117. setitimer(ITIMER_PROF, &itimer, NULL);
  118. if ((sigaction(SIGALRM, &newact, &oldact) < 0) ||
  119. (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
  120. LOG(log_error, logtype_dsi, "dsi_tcp_open: %s", strerror(errno));
  121. exit(EXITERR_SYS);
  122. }
  123. #endif
  124. /* read in commands. this is similar to dsi_receive except
  125. * for the fact that we do some sanity checking to prevent
  126. * delinquent connections from causing mischief. */
  127. /* read in the first two bytes */
  128. len = dsi_stream_read(dsi, block, 2);
  129. if (!len ) {
  130. /* connection already closed, don't log it (normal OSX 10.3 behaviour) */
  131. exit(EXITERR_CLNT);
  132. }
  133. if (len < 2 || (block[0] > DSIFL_MAX) || (block[1] > DSIFUNC_MAX)) {
  134. LOG(log_error, logtype_dsi, "dsi_tcp_open: invalid header");
  135. exit(EXITERR_CLNT);
  136. }
  137. /* read in the rest of the header */
  138. stored = 2;
  139. while (stored < DSI_BLOCKSIZ) {
  140. len = dsi_stream_read(dsi, block + stored, sizeof(block) - stored);
  141. if (len > 0)
  142. stored += len;
  143. else {
  144. LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
  145. exit(EXITERR_CLNT);
  146. }
  147. }
  148. dsi->header.dsi_flags = block[0];
  149. dsi->header.dsi_command = block[1];
  150. memcpy(&dsi->header.dsi_requestID, block + 2,
  151. sizeof(dsi->header.dsi_requestID));
  152. memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
  153. memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
  154. memcpy(&dsi->header.dsi_reserved, block + 12,
  155. sizeof(dsi->header.dsi_reserved));
  156. dsi->clientID = ntohs(dsi->header.dsi_requestID);
  157. /* make sure we don't over-write our buffers. */
  158. dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
  159. stored = 0;
  160. while (stored < dsi->cmdlen) {
  161. len = dsi_stream_read(dsi, dsi->commands + stored, dsi->cmdlen - stored);
  162. if (len > 0)
  163. stored += len;
  164. else {
  165. LOG(log_error, logtype_dsi, "dsi_tcp_open: stream_read: %s", strerror(errno));
  166. exit(EXITERR_CLNT);
  167. }
  168. }
  169. /* stop timer and restore signal handler */
  170. #ifndef DEBUGGING
  171. memset(&timer, 0, sizeof(timer));
  172. setitimer(ITIMER_REAL, &timer, NULL);
  173. sigaction(SIGALRM, &oldact, NULL);
  174. #endif
  175. LOG(log_info, logtype_dsi, "AFP/TCP session from %s:%u",
  176. getip_string((struct sockaddr *)&dsi->client),
  177. getip_port((struct sockaddr *)&dsi->client));
  178. }
  179. /* send back our pid */
  180. return pid;
  181. }
  182. /* get it from the interface list */
  183. #ifndef IFF_SLAVE
  184. #define IFF_SLAVE 0
  185. #endif
  186. static void guess_interface(DSI *dsi, const char *hostname, const char *port)
  187. {
  188. int fd;
  189. char **start, **list;
  190. struct ifreq ifr;
  191. struct sockaddr_in *sa = (struct sockaddr_in *)&dsi->server;
  192. start = list = getifacelist();
  193. if (!start)
  194. return;
  195. fd = socket(PF_INET, SOCK_STREAM, 0);
  196. while (list && *list) {
  197. strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
  198. list++;
  199. if (ioctl(dsi->serversock, SIOCGIFFLAGS, &ifr) < 0)
  200. continue;
  201. if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
  202. continue;
  203. if (!(ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) )
  204. continue;
  205. if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
  206. continue;
  207. memset(&dsi->server, 0, sizeof(struct sockaddr_storage));
  208. sa->sin_family = AF_INET;
  209. sa->sin_port = htons(atoi(port));
  210. sa->sin_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
  211. LOG(log_info, logtype_dsi, "dsi_tcp: '%s:%s' on interface '%s' will be used instead.",
  212. getip_string((struct sockaddr *)&dsi->server), port, ifr.ifr_name);
  213. goto iflist_done;
  214. }
  215. LOG(log_info, logtype_dsi, "dsi_tcp (Chooser will not select afp/tcp) "
  216. "Check to make sure %s is in /etc/hosts and the correct domain is in "
  217. "/etc/resolv.conf: %s", hostname, strerror(errno));
  218. iflist_done:
  219. close(fd);
  220. freeifacelist(start);
  221. }
  222. #ifndef AI_NUMERICSERV
  223. #define AI_NUMERICSERV 0
  224. #endif
  225. /* this needs to accept passed in addresses */
  226. int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address,
  227. const char *port, const int proxy)
  228. {
  229. int ret;
  230. int flag;
  231. struct addrinfo hints, *servinfo, *p;
  232. dsi->protocol = DSI_TCPIP;
  233. /* Prepare hint for getaddrinfo */
  234. memset(&hints, 0, sizeof hints);
  235. #if !defined(FREEBSD)
  236. hints.ai_family = AF_UNSPEC;
  237. #endif
  238. hints.ai_socktype = SOCK_STREAM;
  239. hints.ai_flags = AI_NUMERICSERV;
  240. if ( ! address) {
  241. hints.ai_flags |= AI_PASSIVE;
  242. #if defined(FREEBSD)
  243. hints.ai_family = AF_INET6;
  244. #endif
  245. } else {
  246. hints.ai_flags |= AI_NUMERICHOST;
  247. #if defined(FREEBSD)
  248. hints.ai_family = AF_UNSPEC;
  249. #endif
  250. }
  251. if ((ret = getaddrinfo(address ? address : NULL, port ? port : "548", &hints, &servinfo)) != 0) {
  252. LOG(log_error, logtype_dsi, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret));
  253. return 0;
  254. }
  255. /* create a socket */
  256. if (proxy)
  257. dsi->serversock = -1;
  258. else {
  259. /* loop through all the results and bind to the first we can */
  260. for (p = servinfo; p != NULL; p = p->ai_next) {
  261. if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
  262. LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno));
  263. continue;
  264. }
  265. /*
  266. * Set some socket options:
  267. * SO_REUSEADDR deals w/ quick close/opens
  268. * TCP_NODELAY diables Nagle
  269. */
  270. #ifdef SO_REUSEADDR
  271. flag = 1;
  272. setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
  273. #endif
  274. #if defined(FREEBSD) && defined(IPV6_BINDV6ONLY)
  275. int on = 0;
  276. setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on));
  277. #endif
  278. #ifndef SOL_TCP
  279. #define SOL_TCP IPPROTO_TCP
  280. #endif
  281. flag = 1;
  282. setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
  283. if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) {
  284. close(dsi->serversock);
  285. LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno));
  286. continue;
  287. }
  288. if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) {
  289. close(dsi->serversock);
  290. LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno));
  291. continue;
  292. }
  293. break;
  294. }
  295. if (p == NULL) {
  296. LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket");
  297. freeaddrinfo(servinfo);
  298. return 0;
  299. }
  300. /* Copy struct sockaddr to struct sockaddr_storage */
  301. memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
  302. freeaddrinfo(servinfo);
  303. } /* if (proxy) */
  304. /* Point protocol specific functions to tcp versions */
  305. dsi->proto_open = dsi_tcp_open;
  306. dsi->proto_close = dsi_tcp_close;
  307. /* get real address for GetStatus. */
  308. if (address) {
  309. /* address is a parameter, use it 'as is' */
  310. return 1;
  311. }
  312. /* Prepare hint for getaddrinfo */
  313. memset(&hints, 0, sizeof hints);
  314. hints.ai_family = AF_UNSPEC;
  315. hints.ai_socktype = SOCK_STREAM;
  316. if ((ret = getaddrinfo(hostname, port ? port : "548", &hints, &servinfo)) != 0) {
  317. LOG(log_info, logtype_dsi, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(ret));
  318. goto interfaces;
  319. }
  320. for (p = servinfo; p != NULL; p = p->ai_next) {
  321. if (p->ai_family == AF_INET) { // IPv4
  322. struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
  323. if ( (ipv4->sin_addr.s_addr & htonl(0x7f000000)) != htonl(0x7f000000) )
  324. break;
  325. } else { // IPv6
  326. struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
  327. unsigned char ipv6loopb[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
  328. if ((memcmp(ipv6->sin6_addr.s6_addr, ipv6loopb, 16)) != 0)
  329. break;
  330. }
  331. }
  332. if (p) {
  333. /* Store found address in dsi->server */
  334. memcpy(&dsi->server, p->ai_addr, p->ai_addrlen);
  335. freeaddrinfo(servinfo);
  336. return 1;
  337. }
  338. LOG(log_info, logtype_dsi, "dsi_tcp: hostname '%s' resolves to loopback address", hostname);
  339. freeaddrinfo(servinfo);
  340. interfaces:
  341. guess_interface(dsi, hostname, port ? port : "548");
  342. return 1;
  343. }