1 /* $NetBSD: canohost.c,v 1.13 2019/10/12 18:32:22 christos Exp $ */ 2 /* $OpenBSD: canohost.c,v 1.74 2019/06/28 13:35:04 deraadt Exp $ */ 3 /* 4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 6 * All rights reserved 7 * Functions for returning the canonical host name of the remote site. 8 * 9 * As far as I am concerned, the code I have written for this software 10 * can be used freely for any purpose. Any derived versions of this 11 * software must be clearly marked as such, and if the derived work is 12 * incompatible with the protocol description in the RFC file, it must be 13 * called by a name other than "ssh" or "Secure Shell". 14 */ 15 16 #include "includes.h" 17 __RCSID("$NetBSD: canohost.c,v 1.13 2019/10/12 18:32:22 christos Exp $"); 18 #include <sys/types.h> 19 #include <sys/socket.h> 20 #include <sys/un.h> 21 22 #include <netinet/in.h> 23 24 #include <errno.h> 25 #include <netdb.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.h> 29 #include <stdarg.h> 30 #include <time.h> 31 #include <unistd.h> 32 33 #include "xmalloc.h" 34 #include "packet.h" 35 #include "log.h" 36 #include "canohost.h" 37 #include "misc.h" 38 39 /* 40 * Returns the local/remote IP-address/hostname of socket as a string. 41 * The returned string must be freed. 42 */ 43 static char * 44 get_socket_address(int sock, int remote, int flags) 45 { 46 struct sockaddr_storage addr; 47 socklen_t addrlen; 48 char ntop[NI_MAXHOST]; 49 int r; 50 51 /* Get IP address of client. */ 52 addrlen = sizeof(addr); 53 memset(&addr, 0, sizeof(addr)); 54 55 if (remote) { 56 if (getpeername(sock, (struct sockaddr *)&addr, &addrlen) != 0) 57 return NULL; 58 } else { 59 if (getsockname(sock, (struct sockaddr *)&addr, &addrlen) != 0) 60 return NULL; 61 } 62 63 switch (addr.ss_family) { 64 case AF_INET: 65 case AF_INET6: 66 /* Get the address in ascii. */ 67 if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop, 68 sizeof(ntop), NULL, 0, flags)) != 0) { 69 error("%s: getnameinfo %d failed: %s", __func__, 70 flags, ssh_gai_strerror(r)); 71 return NULL; 72 } 73 return xstrdup(ntop); 74 case AF_UNIX: 75 /* Get the Unix domain socket path. */ 76 return xstrdup(((struct sockaddr_un *)&addr)->sun_path); 77 default: 78 /* We can't look up remote Unix domain sockets. */ 79 return NULL; 80 } 81 } 82 83 char * 84 get_peer_ipaddr(int sock) 85 { 86 char *p; 87 88 if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL) 89 return p; 90 return xstrdup("UNKNOWN"); 91 } 92 93 char * 94 get_local_ipaddr(int sock) 95 { 96 char *p; 97 98 if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL) 99 return p; 100 return xstrdup("UNKNOWN"); 101 } 102 103 char * 104 get_local_name(int fd) 105 { 106 char *host, myname[NI_MAXHOST]; 107 108 /* Assume we were passed a socket */ 109 if ((host = get_socket_address(fd, 0, NI_NAMEREQD)) != NULL) 110 return host; 111 112 /* Handle the case where we were passed a pipe */ 113 if (gethostname(myname, sizeof(myname)) == -1) { 114 verbose("%s: gethostname: %s", __func__, strerror(errno)); 115 host = xstrdup("UNKNOWN"); 116 } else { 117 host = xstrdup(myname); 118 } 119 120 return host; 121 } 122 123 /* Returns the local/remote port for the socket. */ 124 125 static int 126 get_sock_port(int sock, int local) 127 { 128 struct sockaddr_storage from; 129 socklen_t fromlen; 130 char strport[NI_MAXSERV]; 131 int r; 132 133 /* Get IP address of client. */ 134 fromlen = sizeof(from); 135 memset(&from, 0, sizeof(from)); 136 if (local) { 137 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) == -1) { 138 error("getsockname failed: %.100s", strerror(errno)); 139 return 0; 140 } 141 } else { 142 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) == -1) { 143 debug("getpeername failed: %.100s", strerror(errno)); 144 return -1; 145 } 146 } 147 148 /* Non-inet sockets don't have a port number. */ 149 if (from.ss_family != AF_INET && from.ss_family != AF_INET6) 150 return 0; 151 152 /* Return port number. */ 153 if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0, 154 strport, sizeof(strport), NI_NUMERICSERV)) != 0) 155 fatal("%s: getnameinfo NI_NUMERICSERV failed: %s", __func__, 156 ssh_gai_strerror(r)); 157 return atoi(strport); 158 } 159 160 int 161 get_peer_port(int sock) 162 { 163 return get_sock_port(sock, 0); 164 } 165 166 int 167 get_local_port(int sock) 168 { 169 return get_sock_port(sock, 1); 170 } 171