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