Commit | Line | Data |
---|---|---|
bc2e93eb AT |
1 | /* |
2 | Copyright (C) Andrew Tridgell 1998 | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | */ | |
18 | ||
19 | /* | |
20 | socket functions used in rsync | |
21 | ||
22 | */ | |
23 | ||
f0fca04e AT |
24 | #include "rsync.h" |
25 | ||
4c3b4b25 AT |
26 | |
27 | /* establish a proxy connection on an open socket to a web roxy by using the CONNECT | |
28 | method */ | |
29 | static int establish_proxy_connection(int fd, char *host, int port) | |
30 | { | |
31 | char buffer[1024]; | |
32 | char *cp; | |
33 | ||
34 | slprintf(buffer, sizeof(buffer), "CONNECT %s:%d HTTP/1.0\r\n\r\n", host, port); | |
35 | if (write(fd, buffer, strlen(buffer)) != strlen(buffer)) { | |
36 | rprintf(FERROR, "failed to write to proxy - %s\n", | |
37 | strerror(errno)); | |
38 | return -1; | |
39 | } | |
40 | ||
41 | for (cp = buffer; cp < &buffer[sizeof(buffer) - 1]; cp++) { | |
42 | if (read(fd, cp, 1) != 1) { | |
43 | rprintf(FERROR, "failed to read from proxy\n"); | |
44 | return -1; | |
45 | } | |
46 | if (*cp == '\n') | |
47 | break; | |
48 | } | |
49 | ||
50 | if (*cp != '\n') | |
51 | cp++; | |
52 | *cp-- = '\0'; | |
53 | if (*cp == '\r') | |
54 | *cp = '\0'; | |
55 | if (strncmp(buffer, "HTTP/", 5) != 0) { | |
56 | rprintf(FERROR, "bad response from proxy - %s\n", | |
57 | buffer); | |
58 | return -1; | |
59 | } | |
60 | for (cp = &buffer[5]; isdigit(*cp) || (*cp == '.'); cp++) | |
61 | ; | |
62 | while (*cp == ' ') | |
63 | cp++; | |
64 | if (*cp != '2') { | |
65 | rprintf(FERROR, "bad response from proxy - %s\n", | |
66 | buffer); | |
67 | return -1; | |
68 | } | |
69 | /* throw away the rest of the HTTP header */ | |
70 | while (1) { | |
71 | for (cp = buffer; cp < &buffer[sizeof(buffer) - 1]; | |
72 | cp++) { | |
73 | if (read(fd, cp, 1) != 1) { | |
74 | rprintf(FERROR, "failed to read from proxy\n"); | |
75 | return -1; | |
76 | } | |
77 | if (*cp == '\n') | |
78 | break; | |
79 | } | |
80 | if ((cp > buffer) && (*cp == '\n')) | |
81 | cp--; | |
82 | if ((cp == buffer) && ((*cp == '\n') || (*cp == '\r'))) | |
83 | break; | |
84 | } | |
85 | return 0; | |
86 | } | |
87 | ||
88 | ||
f0fca04e | 89 | /* open a socket to a tcp remote host with the specified port |
4c3b4b25 AT |
90 | based on code from Warren |
91 | proxy support by Stephen Rothwell */ | |
bc2e93eb AT |
92 | int open_socket_out(char *host, int port) |
93 | { | |
f0fca04e AT |
94 | int type = SOCK_STREAM; |
95 | struct sockaddr_in sock_out; | |
96 | int res; | |
97 | struct hostent *hp; | |
4c3b4b25 AT |
98 | char *h; |
99 | unsigned p; | |
100 | int proxied = 0; | |
101 | char buffer[1024]; | |
102 | char *cp; | |
103 | ||
104 | /* if we have a RSYNC_PROXY env variable then redirect our connetcion via a web proxy | |
105 | at the given address. The format is hostname:port */ | |
106 | h = getenv("RSYNC_PROXY"); | |
107 | proxied = (h != NULL) && (*h != '\0'); | |
108 | ||
109 | if (proxied) { | |
110 | strlcpy(buffer, h, sizeof(buffer)); | |
111 | cp = strchr(buffer, ':'); | |
112 | if (cp == NULL) { | |
113 | rprintf(FERROR, "invalid proxy specification\n"); | |
114 | return -1; | |
115 | } | |
116 | *cp++ = '\0'; | |
117 | p = atoi(cp); | |
118 | h = buffer; | |
119 | } else { | |
120 | h = host; | |
121 | p = port; | |
122 | } | |
f0fca04e AT |
123 | |
124 | res = socket(PF_INET, type, 0); | |
125 | if (res == -1) { | |
126 | return -1; | |
127 | } | |
128 | ||
4c3b4b25 | 129 | hp = gethostbyname(h); |
f0fca04e | 130 | if (!hp) { |
4c3b4b25 AT |
131 | rprintf(FERROR,"unknown host: %s\n", h); |
132 | close(res); | |
f0fca04e AT |
133 | return -1; |
134 | } | |
135 | ||
136 | memcpy(&sock_out.sin_addr, hp->h_addr, hp->h_length); | |
4c3b4b25 | 137 | sock_out.sin_port = htons(p); |
f0fca04e AT |
138 | sock_out.sin_family = PF_INET; |
139 | ||
140 | if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) { | |
4c3b4b25 AT |
141 | rprintf(FERROR,"failed to connect to %s - %s\n", h, strerror(errno)); |
142 | close(res); | |
143 | return -1; | |
144 | } | |
145 | ||
146 | if (proxied && establish_proxy_connection(res, host, port) != 0) { | |
f0fca04e | 147 | close(res); |
f0fca04e AT |
148 | return -1; |
149 | } | |
150 | ||
151 | return res; | |
152 | } | |
153 | ||
154 | ||
155 | /**************************************************************************** | |
156 | open a socket of the specified type, port and address for incoming data | |
157 | ****************************************************************************/ | |
5c9730a4 | 158 | static int open_socket_in(int type, int port, struct in_addr *address) |
f0fca04e AT |
159 | { |
160 | struct hostent *hp; | |
161 | struct sockaddr_in sock; | |
d41c7d02 | 162 | char host_name[MAXHOSTNAMELEN]; |
f0fca04e AT |
163 | int res; |
164 | int one=1; | |
165 | ||
166 | /* get my host name */ | |
167 | if (gethostname(host_name, sizeof(host_name)) == -1) { | |
168 | rprintf(FERROR,"gethostname failed\n"); | |
169 | return -1; | |
170 | } | |
171 | ||
172 | /* get host info */ | |
173 | if ((hp = gethostbyname(host_name)) == 0) { | |
174 | rprintf(FERROR,"gethostbyname: Unknown host %s\n",host_name); | |
175 | return -1; | |
176 | } | |
177 | ||
f5780433 | 178 | memset((char *)&sock,0,sizeof(sock)); |
f0fca04e AT |
179 | memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length); |
180 | sock.sin_port = htons(port); | |
181 | sock.sin_family = hp->h_addrtype; | |
5c9730a4 AT |
182 | if (address) { |
183 | sock.sin_addr = *address; | |
184 | } else { | |
185 | sock.sin_addr.s_addr = INADDR_ANY; | |
186 | } | |
f0fca04e AT |
187 | res = socket(hp->h_addrtype, type, 0); |
188 | if (res == -1) { | |
189 | rprintf(FERROR,"socket failed\n"); | |
190 | return -1; | |
191 | } | |
192 | ||
193 | setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one)); | |
194 | ||
195 | /* now we've got a socket - we need to bind it */ | |
196 | if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) == -1) { | |
197 | rprintf(FERROR,"bind failed on port %d\n", port); | |
198 | close(res); | |
199 | return -1; | |
200 | } | |
201 | ||
202 | return res; | |
203 | } | |
204 | ||
205 | ||
206 | /**************************************************************************** | |
207 | determine if a file descriptor is in fact a socket | |
208 | ****************************************************************************/ | |
209 | int is_a_socket(int fd) | |
210 | { | |
3eb38818 AT |
211 | int v,l; |
212 | l = sizeof(int); | |
213 | return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0); | |
f0fca04e AT |
214 | } |
215 | ||
216 | ||
8ef4ffd6 | 217 | void start_accept_loop(int port, int (*fn)(int )) |
f0fca04e AT |
218 | { |
219 | int s; | |
5c9730a4 | 220 | extern struct in_addr socket_address; |
f0fca04e | 221 | |
f0fca04e | 222 | /* open an incoming socket */ |
5c9730a4 | 223 | s = open_socket_in(SOCK_STREAM, port, &socket_address); |
f0fca04e | 224 | if (s == -1) |
65417579 | 225 | exit_cleanup(RERR_SOCKETIO); |
f0fca04e AT |
226 | |
227 | /* ready to listen */ | |
228 | if (listen(s, 5) == -1) { | |
229 | close(s); | |
65417579 | 230 | exit_cleanup(RERR_SOCKETIO); |
f0fca04e AT |
231 | } |
232 | ||
233 | ||
234 | /* now accept incoming connections - forking a new process | |
235 | for each incoming connection */ | |
236 | while (1) { | |
237 | fd_set fds; | |
238 | int fd; | |
239 | struct sockaddr addr; | |
240 | int in_addrlen = sizeof(addr); | |
241 | ||
242 | FD_ZERO(&fds); | |
243 | FD_SET(s, &fds); | |
244 | ||
245 | if (select(s+1, &fds, NULL, NULL, NULL) != 1) { | |
246 | continue; | |
247 | } | |
248 | ||
249 | if(!FD_ISSET(s, &fds)) continue; | |
250 | ||
251 | fd = accept(s,&addr,&in_addrlen); | |
252 | ||
253 | if (fd == -1) continue; | |
254 | ||
31f440e6 AT |
255 | signal(SIGCHLD, SIG_IGN); |
256 | ||
257 | /* we shouldn't have any children left hanging around | |
258 | but I have had reports that on Digital Unix zombies | |
259 | are produced, so this ensures that they are reaped */ | |
260 | #ifdef WNOHANG | |
0503f060 | 261 | while (waitpid(-1, NULL, WNOHANG) > 0); |
31f440e6 AT |
262 | #endif |
263 | ||
f0fca04e AT |
264 | if (fork()==0) { |
265 | close(s); | |
266 | ||
267 | _exit(fn(fd)); | |
268 | } | |
269 | ||
270 | close(fd); | |
271 | } | |
f0fca04e AT |
272 | } |
273 | ||
274 | ||
275 | enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON}; | |
276 | ||
277 | struct | |
278 | { | |
279 | char *name; | |
280 | int level; | |
281 | int option; | |
282 | int value; | |
283 | int opttype; | |
284 | } socket_options[] = { | |
285 | {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL}, | |
286 | {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL}, | |
287 | {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL}, | |
288 | #ifdef TCP_NODELAY | |
289 | {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL}, | |
290 | #endif | |
291 | #ifdef IPTOS_LOWDELAY | |
292 | {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON}, | |
293 | #endif | |
294 | #ifdef IPTOS_THROUGHPUT | |
295 | {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON}, | |
296 | #endif | |
297 | #ifdef SO_SNDBUF | |
298 | {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT}, | |
299 | #endif | |
300 | #ifdef SO_RCVBUF | |
301 | {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT}, | |
302 | #endif | |
303 | #ifdef SO_SNDLOWAT | |
304 | {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT}, | |
305 | #endif | |
306 | #ifdef SO_RCVLOWAT | |
307 | {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT}, | |
308 | #endif | |
309 | #ifdef SO_SNDTIMEO | |
310 | {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT}, | |
311 | #endif | |
312 | #ifdef SO_RCVTIMEO | |
313 | {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT}, | |
314 | #endif | |
315 | {NULL,0,0,0,0}}; | |
316 | ||
317 | ||
318 | ||
319 | /**************************************************************************** | |
320 | set user socket options | |
321 | ****************************************************************************/ | |
322 | void set_socket_options(int fd, char *options) | |
323 | { | |
324 | char *tok; | |
a6801c39 AT |
325 | if (!options || !*options) return; |
326 | ||
f0fca04e AT |
327 | options = strdup(options); |
328 | ||
329 | if (!options) out_of_memory("set_socket_options"); | |
330 | ||
331 | for (tok=strtok(options, " \t,"); tok; tok=strtok(NULL," \t,")) { | |
332 | int ret=0,i; | |
333 | int value = 1; | |
334 | char *p; | |
335 | int got_value = 0; | |
336 | ||
337 | if ((p = strchr(tok,'='))) { | |
338 | *p = 0; | |
339 | value = atoi(p+1); | |
340 | got_value = 1; | |
341 | } | |
342 | ||
343 | for (i=0;socket_options[i].name;i++) | |
344 | if (strcmp(socket_options[i].name,tok)==0) | |
345 | break; | |
346 | ||
347 | if (!socket_options[i].name) { | |
348 | rprintf(FERROR,"Unknown socket option %s\n",tok); | |
349 | continue; | |
350 | } | |
351 | ||
352 | switch (socket_options[i].opttype) { | |
353 | case OPT_BOOL: | |
354 | case OPT_INT: | |
355 | ret = setsockopt(fd,socket_options[i].level, | |
356 | socket_options[i].option,(char *)&value,sizeof(int)); | |
357 | break; | |
358 | ||
359 | case OPT_ON: | |
360 | if (got_value) | |
361 | rprintf(FERROR,"syntax error - %s does not take a value\n",tok); | |
362 | ||
363 | { | |
364 | int on = socket_options[i].value; | |
365 | ret = setsockopt(fd,socket_options[i].level, | |
366 | socket_options[i].option,(char *)&on,sizeof(int)); | |
367 | } | |
368 | break; | |
369 | } | |
370 | ||
371 | if (ret != 0) | |
372 | rprintf(FERROR,"Failed to set socket option %s\n",tok); | |
373 | } | |
374 | ||
375 | free(options); | |
376 | } | |
377 | ||
378 | /**************************************************************************** | |
379 | become a daemon, discarding the controlling terminal | |
380 | ****************************************************************************/ | |
381 | void become_daemon(void) | |
382 | { | |
b11ed3b1 AT |
383 | int i; |
384 | ||
c46ded46 | 385 | if (fork()) { |
f0fca04e | 386 | _exit(0); |
c46ded46 | 387 | } |
f0fca04e AT |
388 | |
389 | /* detach from the terminal */ | |
390 | #ifdef HAVE_SETSID | |
391 | setsid(); | |
392 | #else | |
393 | #ifdef TIOCNOTTY | |
c46ded46 AT |
394 | i = open("/dev/tty", O_RDWR); |
395 | if (i >= 0) { | |
396 | ioctl(i, (int) TIOCNOTTY, (char *)0); | |
397 | close(i); | |
f0fca04e AT |
398 | } |
399 | #endif /* TIOCNOTTY */ | |
400 | #endif | |
b11ed3b1 AT |
401 | /* make sure that stdin, stdout an stderr don't stuff things |
402 | up (library functions, for example) */ | |
403 | for (i=0;i<3;i++) { | |
404 | close(i); | |
405 | open("/dev/null", O_RDWR); | |
406 | } | |
bc2e93eb | 407 | } |
ff8b29b8 AT |
408 | |
409 | /******************************************************************* | |
410 | return the IP addr of the client as a string | |
411 | ******************************************************************/ | |
412 | char *client_addr(int fd) | |
413 | { | |
414 | struct sockaddr sa; | |
415 | struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa); | |
416 | int length = sizeof(sa); | |
417 | static char addr_buf[100]; | |
11a5a3c7 AT |
418 | static int initialised; |
419 | ||
420 | if (initialised) return addr_buf; | |
421 | ||
422 | initialised = 1; | |
ff8b29b8 AT |
423 | |
424 | if (getpeername(fd, &sa, &length)) { | |
65417579 | 425 | exit_cleanup(RERR_SOCKETIO); |
ff8b29b8 | 426 | } |
11a5a3c7 | 427 | |
37f9805d | 428 | strlcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr), sizeof(addr_buf)); |
ff8b29b8 AT |
429 | return addr_buf; |
430 | } | |
431 | ||
432 | ||
433 | /******************************************************************* | |
434 | return the DNS name of the client | |
435 | ******************************************************************/ | |
436 | char *client_name(int fd) | |
437 | { | |
438 | struct sockaddr sa; | |
439 | struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa); | |
440 | int length = sizeof(sa); | |
441 | static char name_buf[100]; | |
442 | struct hostent *hp; | |
de5fb374 AT |
443 | char **p; |
444 | char *def = "UNKNOWN"; | |
11a5a3c7 AT |
445 | static int initialised; |
446 | ||
447 | if (initialised) return name_buf; | |
448 | ||
449 | initialised = 1; | |
ff8b29b8 | 450 | |
de5fb374 | 451 | strcpy(name_buf,def); |
ff8b29b8 AT |
452 | |
453 | if (getpeername(fd, &sa, &length)) { | |
65417579 | 454 | exit_cleanup(RERR_SOCKETIO); |
ff8b29b8 AT |
455 | } |
456 | ||
457 | /* Look up the remote host name. */ | |
458 | if ((hp = gethostbyaddr((char *) &sockin->sin_addr, | |
459 | sizeof(sockin->sin_addr), | |
460 | AF_INET))) { | |
37f9805d | 461 | strlcpy(name_buf,(char *)hp->h_name,sizeof(name_buf)); |
ff8b29b8 AT |
462 | } |
463 | ||
de5fb374 AT |
464 | |
465 | /* do a forward lookup as well to prevent spoofing */ | |
466 | hp = gethostbyname(name_buf); | |
467 | if (!hp) { | |
468 | strcpy(name_buf,def); | |
469 | rprintf(FERROR,"reverse name lookup failed\n"); | |
470 | } else { | |
471 | for (p=hp->h_addr_list;*p;p++) { | |
472 | if (memcmp(*p, &sockin->sin_addr, hp->h_length) == 0) { | |
473 | break; | |
474 | } | |
475 | } | |
476 | if (!*p) { | |
477 | strcpy(name_buf,def); | |
478 | rprintf(FERROR,"reverse name lookup mismatch - spoofed address?\n"); | |
479 | } | |
480 | } | |
481 | ||
ff8b29b8 AT |
482 | return name_buf; |
483 | } | |
5c9730a4 AT |
484 | |
485 | /******************************************************************* | |
486 | convert a string to an IP address. The string can be a name or | |
487 | dotted decimal number | |
488 | ******************************************************************/ | |
489 | struct in_addr *ip_address(const char *str) | |
490 | { | |
491 | static struct in_addr ret; | |
492 | struct hostent *hp; | |
493 | ||
494 | /* try as an IP address */ | |
495 | if (inet_aton(str, &ret) != 0) { | |
496 | return &ret; | |
497 | } | |
498 | ||
499 | /* otherwise assume it's a network name of some sort and use | |
500 | gethostbyname */ | |
501 | if ((hp = gethostbyname(str)) == 0) { | |
502 | rprintf(FERROR, "gethostbyname: Unknown host. %s\n",str); | |
503 | return NULL; | |
504 | } | |
505 | ||
506 | if (hp->h_addr == NULL) { | |
507 | rprintf(FERROR, "gethostbyname: host address is invalid for host %s\n",str); | |
508 | return NULL; | |
509 | } | |
510 | ||
511 | if (hp->h_length > sizeof(ret)) { | |
512 | rprintf(FERROR, "gethostbyname: host address is too large\n"); | |
513 | return NULL; | |
514 | } | |
515 | ||
516 | memcpy(&ret.s_addr, hp->h_addr, hp->h_length); | |
517 | ||
518 | return(&ret); | |
519 | } |