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