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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
/*
* solve.c - Secure RFC-2553-based IP resolution functions
* $Id: solve.c 175 2005-07-01 18:07:39Z rdenisc $
*/
/***********************************************************************
* Copyright (C) 2002-2004 Remi Denis-Courmont. *
* This program is free software; you can redistribute and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation; version 2 of the license. *
* *
* 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. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, you can get it from: *
* http://www.gnu.org/copyleft/gpl.html *
***********************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h> /* stderr */
#include "secstr.h" /* memset(), memcpy(), strcmp() */
#include <stdlib.h> /* malloc(), free() */
#include <sys/types.h> /* needed before sys/socket.h on FreeBSD */
#if HAVE_SYS_SOCKET_H
# include <sys/socket.h> /* getsockname(), getpeername() */
#endif
#if HAVE_SYS_UN_H
# include <sys/un.h>
#endif
#include <errno.h>
#include "solve.h"
/*
* Compares 2 socket addresses. Return 0 if they are identical,
* a positive if they are different, a negative on error. It is assumed
* that they are of the same size (otherwise, YOU know they are
* different anyway, don't you?).
*
* Only nodes are compared, services are not.
*/
static int
sockaddrcmp (const struct sockaddr *a1, size_t a1len,
const struct sockaddr *a2, size_t a2len)
{
char n1[NI_MAXHOST], n2[NI_MAXHOST];
/* Normally, we'd compare addr and res->ai_addr, but there is
no address family independant way to do this (memcmp() won't
work in many case (at least Linux/IPv4).
Instead, we do compare numerical address strings. This requires
yet another (but fortunately non-blocking) call of getnameinfo.
It is moreover assumed that service names cannot be spoofed
(Service Name Service has not been invented, right?).
*/
if ((a1->sa_family != a2->sa_family)
|| getnameinfo (a1, a1len, n1, sizeof (n1), NULL, 0, NI_NUMERICHOST)
|| getnameinfo (a2, a2len, n2, sizeof (n2), NULL, 0, NI_NUMERICHOST))
return -1;
return (strcmp (n1, n2) == 0) ? 0 : 1;
}
/*
* Secure reverse DNS resolution.
* NI_NOFQDN (flags option) will fail unless addr is on the same domain
* as we are (this is absolutely normal). All other flags should work
* correctly.
*
* In case of error, if *servbuf is true, the service name is ok.
*/
static int
secure_getnameinfo(const struct sockaddr *addr, size_t addrlen,
char *namebuf, size_t namelen,
char *servbuf, size_t servlen, int flags)
{
int check;
/* Gets service name once and for all */
check = getnameinfo (addr, addrlen, NULL, 0, servbuf, servlen,
flags);
if (check != 0)
return check;
/* Reverse DNS request */
check = getnameinfo (addr, addrlen, namebuf, namelen, NULL, 0, flags);
if ((check != 0) || (flags & NI_NUMERICHOST))
return check; /* If numeric host name is requested, done. */
else
{
struct addrinfo hints, *res, *info;
/* Hostname DNS request (to prevent malicious users
* from DNS spoofing us). */
memset (&hints, 0, sizeof (hints));
hints.ai_family = addr->sa_family;
check = getaddrinfo (namebuf, NULL, &hints, &res);
if (check == 0)
{
for (info = res; info != NULL; info = info->ai_next)
if (!sockaddrcmp (addr, addrlen, info->ai_addr,
info->ai_addrlen))
{
freeaddrinfo(res);
return 0;
}
/* DNS spoofing detected: use numeric address only */
freeaddrinfo (res);
}
}
return getnameinfo (addr, addrlen, namebuf, namelen, NULL, 0,
flags|NI_NUMERICHOST);
}
/*** Generic struct addrinfo handling ***/
void
freeai (struct addrinfo *res)
{
if (res != NULL)
{
freeai (res->ai_next);
if (res->ai_addr != NULL)
free (res->ai_addr);
free (res);
}
}
struct addrinfo *
makeai (const struct sockaddr *addr, socklen_t addrlen)
{
struct addrinfo *res = (struct addrinfo *)
malloc (sizeof (struct addrinfo));
if (res == NULL)
return NULL;
memset (res, 0, sizeof (struct addrinfo));
if (addr != NULL)
{
struct sockaddr *ad = (struct sockaddr *)
malloc (addrlen);
if (ad == NULL)
{
int errb = errno;
free (res);
errno = errb;
return NULL;
}
memcpy (ad, addr, addrlen);
res->ai_addr = ad;
}
res->ai_addrlen = addrlen;
return res;
}
struct addrinfo *
copyai (const struct addrinfo *src)
{
if (src != NULL)
{
struct addrinfo *res;
res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
if (res == NULL)
return NULL;
memcpy (res, src, sizeof (struct addrinfo));
if (src->ai_next != NULL)
{
res->ai_next = copyai (src->ai_next);
if (res->ai_next == NULL)
{
int errb = errno;
free (res);
errno = errb;
return NULL;
}
}
if (src->ai_addr != NULL)
{
res->ai_addr =
(struct sockaddr *)malloc (src->ai_addrlen);
if (res->ai_addr == NULL)
{
int errb = errno;
freeai (res->ai_next);
free (res);
errno = errb;
return NULL;
}
memcpy (res->ai_addr, src->ai_addr, src->ai_addrlen);
}
return res;
}
return NULL;
}
#if HAVE_SYS_UN_H
/*** Unix (a.k.a. "local") addresses ***/
static int
unix_getaddrinfo (const char *path, const struct addrinfo *hints,
struct addrinfo **res)
{
if (path != NULL)
{
struct sockaddr_un addr;
struct addrinfo *ret;
memset (&addr, 0, sizeof (addr));
addr.sun_family = AF_LOCAL;
# ifdef HAVE_SA_LEN
addr.sun_len = sizeof (addr);
# endif
strncpy (addr.sun_path, path, sizeof (addr.sun_path));
if (addr.sun_path[sizeof (addr.sun_path) - 1])
return EAI_NONAME;
ret = makeai ((struct sockaddr *)&addr, sizeof (addr));
if (ret == NULL)
return EAI_MEMORY;
ret->ai_family = AF_LOCAL;
ret->ai_socktype = hints->ai_socktype ?: SOCK_DGRAM;
*res = ret;
return 0;
}
return EAI_NONAME;
}
#endif
/*** Protocols family-independant addresses resolution ***/
int getnamebyaddr (const struct sockaddr *addr, size_t addrlen,
char *nodename, size_t nlen, char *service,
size_t slen, int flags)
{
switch (addr->sa_family)
{
case AF_INET:
#ifdef AF_INET6
case AF_INET6:
#endif
return secure_getnameinfo (addr, addrlen, nodename,
nlen, service, slen, flags);
#if HAVE_SYS_UN_H
case AF_LOCAL:
*nodename = 0;
secure_strncpy (service,
((struct sockaddr_un *)addr)->sun_path,
slen);
return 0;
#endif
}
return EAI_FAMILY;
}
int
getaddrbyname (const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res)
{
switch ((hints != NULL) ? hints->ai_family : 0)
{
case 0:
case AF_INET:
#ifdef AF_INET6
case AF_INET6:
#endif
{
int check;
struct addrinfo *inet_res, inet_hints;
if (hints != NULL)
memcpy (&inet_hints, hints,
sizeof (struct addrinfo));
else
memset (&inet_hints, 0,
sizeof (struct addrinfo));
inet_hints.ai_flags |= AI_IDN;
// Avoids unknown service error
if ((node == NULL) && (service == NULL))
service = "0";
check = getaddrinfo (node, service, &inet_hints,
&inet_res);
if (check)
return check;
*res = copyai (inet_res);
freeaddrinfo (inet_res);
return (*res == NULL) ? EAI_SYSTEM : 0;
}
#if HAVE_SYS_UN_H
case AF_LOCAL:
return (node != NULL) ? EAI_SERVICE
: unix_getaddrinfo (service, hints, res);
#endif
}
return EAI_FAMILY;
}
|