1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/*
* Copyright (c) 1997, 1998 Motoyuki Kasahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/*
* This program requires the following Autoconf macros:
* AC_C_CONST
* AC_TYPE_SIZE_T
* AC_HEADER_STDC
* AC_CHECK_HEADERS(string.h, memory.h, unistd.h)
* AC_CHECK_FUNCS(strchr, memcpy)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <syslog.h>
#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h>
#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#include <memory.h>
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include <strings.h>
#endif /* not STDC_HEADERS and not HAVE_STRING_H */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "hostname.h"
#ifdef USE_FAKELOG
#include "fakelog.h"
#endif
#ifndef HAVE_STRCHR
#define strchr index
#define strrchr rindex
#endif /* HAVE_STRCHR */
#ifndef HAVE_MEMCPY
#define memcpy(d, s, n) bcopy((s), (d), (n))
#ifdef __STDC__
void *memchr(const void *, int, size_t);
int memcmp(const void *, const void *, size_t);
void *memmove(void *, const void *, size_t);
void *memset(void *, int, size_t);
#else /* not __STDC__ */
char *memchr();
int memcmp();
char *memmove();
char *memset();
#endif /* not __STDC__ */
#endif /* not HAVE_MEMCPY */
/*
* Get my canonical hostname and its address.
*
* The hostname and address are put into `hostname' and `address'.
* If unknown, UNKNOWN_HOST is put into it.
* If succeeded, 0 is returned. Otherwise -1 is returned.
*/
int
identify_my_host(hostname, address)
char *hostname;
char *address;
{
struct hostent *byname;
struct in_addr addr;
char *strp;
char *ntoa;
/*
* Set hostname and address to UNKNOWN_HOST.
*/
strncpy(hostname, UNKNOWN_HOST, MAXLEN_HOSTNAME);
*(hostname + MAXLEN_HOSTNAME) = '\0';
strncpy(address, UNKNOWN_HOST, MAXLEN_HOSTNAME);
*(address + MAXLEN_HOSTNAME) = '\0';
/*
* Get my hostname by gethostname() and gethostbyname().
*/
if (gethostname(hostname, MAXLEN_HOSTNAME + 1) < 0)
return -1;
byname = gethostbyname(hostname);
if (byname == NULL)
return -1;
if (MAXLEN_HOSTNAME < strlen(byname->h_name)) {
strncpy(hostname, byname->h_name, MAXLEN_HOSTNAME);
*(hostname + MAXLEN_HOSTNAME) = '\0';
syslog(LOG_ERR, "too long hostname: %s...", hostname);
return -1;
}
strcpy(hostname, byname->h_name);
/*
* Get an address of the hostname.
*/
memcpy(&addr, byname->h_addr, sizeof(addr));
ntoa = inet_ntoa(addr);
if (MAXLEN_HOSTNAME < strlen(ntoa)) {
strncpy(address, ntoa, MAXLEN_HOSTNAME);
*(address + MAXLEN_HOSTNAME) = '\0';
syslog(LOG_ERR, "too long address: %s...", address);
return -1;
}
strcpy(address, ntoa);
/*
* Change the hostname to lower cases.
*/
for (strp = hostname; *strp != '\0'; strp++) {
if (isupper(*strp))
*strp = tolower(*strp);
}
return 0;
}
/*
* Get remote hostname and address of the socket `fd'.
*
* The hostname and address are put into `hostname' and `address'.
* If unknown, UNKNOWN_HOST is put into it.
* If succeed, 0 is returned. Otherwise -1 is returned.
*/
int
identify_remote_host(fd, hostname, address)
int fd;
char *hostname;
char *address;
{
struct sockaddr_in peer;
struct hostent *byname, *byaddr;
char **addrp;
char *strp;
char *ntoa;
int peerlen;
/*
* Set hostname and address to UNKNOWN_HOST.
*/
strncpy(hostname, UNKNOWN_HOST, MAXLEN_HOSTNAME);
*(hostname + MAXLEN_HOSTNAME) = '\0';
strncpy(address, UNKNOWN_HOST, MAXLEN_HOSTNAME);
*(address + MAXLEN_HOSTNAME) = '\0';
/*
* Get an IP address of the remote host by getpeername().
*/
peerlen = sizeof(peer);
if (getpeername(fd, (struct sockaddr *)&peer, &peerlen) < 0) {
syslog(LOG_ERR, "getpeername() failed, %m");
return -1;
}
ntoa = inet_ntoa(peer.sin_addr);
if (MAXLEN_HOSTNAME < strlen(ntoa)) {
strncpy(address, ntoa, MAXLEN_HOSTNAME);
*(address + MAXLEN_HOSTNAME) = '\0';
syslog(LOG_ERR, "too long address: %s...", address);
return -1;
}
strcpy(address, ntoa);
/*
* Get a hostname of the remote host by gethostbyaddr().
*/
byaddr = gethostbyaddr((char *)&peer.sin_addr, sizeof(peer.sin_addr),
AF_INET);
if (byaddr == NULL) {
syslog(LOG_ERR, "cannot get a hostname: %s", address);
return 0;
}
if (MAXLEN_HOSTNAME < strlen(byaddr->h_name)) {
strncpy(hostname, byaddr->h_name, MAXLEN_HOSTNAME);
*(hostname + MAXLEN_HOSTNAME) = '\0';
syslog(LOG_ERR, "too long hostname: %s...", hostname);
return -1;
}
strcpy(hostname, byaddr->h_name);
/*
* Verify the hostname by reversed lookup.
*/
byname = gethostbyname(hostname);
if (byname == NULL) {
syslog(LOG_ERR, "cannot get a hostname: %s", address);
return 0;
}
for (addrp = byname->h_addr_list; *addrp != NULL; addrp++) {
if (memcmp(&peer.sin_addr, *addrp, byname->h_length) == 0)
break;
}
if (*addrp == NULL) {
syslog(LOG_ERR, "reversed lookup mismatch: %s -> %s",
address, hostname);
strncpy(hostname, UNKNOWN_HOST, MAXLEN_HOSTNAME);
*(hostname + MAXLEN_HOSTNAME) = '\0';
return 0;
}
/*
* Change the hostname to lower cases.
*/
for (strp = hostname; *strp != '\0'; strp++) {
if (isupper(*strp))
*strp = tolower(*strp);
}
return 0;
}
|