X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/a6801c397732a55d2f9f477a360db6fc09bb1c3d..ea2111d10a10a164334eff3b621ed7e630404345:/socket.c diff --git a/socket.c b/socket.c index 3c876ed0..acf7a1be 100644 --- a/socket.c +++ b/socket.c @@ -125,12 +125,12 @@ void start_accept_loop(int port, int (*fn)(int )) /* open an incoming socket */ s = open_socket_in(SOCK_STREAM, port); if (s == -1) - exit(1); + exit_cleanup(1); /* ready to listen */ if (listen(s, 5) == -1) { close(s); - exit(1); + exit_cleanup(1); } @@ -308,7 +308,7 @@ char *client_addr(int fd) static char addr_buf[100]; if (getpeername(fd, &sa, &length)) { - exit(1); + exit_cleanup(1); } strlcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr), sizeof(addr_buf)-1); @@ -327,11 +327,13 @@ char *client_name(int fd) int length = sizeof(sa); static char name_buf[100]; struct hostent *hp; + char **p; + char *def = "UNKNOWN"; - strcpy(name_buf,"UNKNOWN"); + strcpy(name_buf,def); if (getpeername(fd, &sa, &length)) { - exit(1); + exit_cleanup(1); } /* Look up the remote host name. */ @@ -341,5 +343,23 @@ char *client_name(int fd) strlcpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1); } + + /* do a forward lookup as well to prevent spoofing */ + hp = gethostbyname(name_buf); + if (!hp) { + strcpy(name_buf,def); + rprintf(FERROR,"reverse name lookup failed\n"); + } else { + for (p=hp->h_addr_list;*p;p++) { + if (memcmp(*p, &sockin->sin_addr, hp->h_length) == 0) { + break; + } + } + if (!*p) { + strcpy(name_buf,def); + rprintf(FERROR,"reverse name lookup mismatch - spoofed address?\n"); + } + } + return name_buf; }