1 /* 2 * ProFTPD - FTP server daemon 3 * Copyright (c) 2003-2017 The ProFTPD Project team 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. 18 * 19 * As a special exemption, The ProFTPD Project team and other respective 20 * copyright holders give permission to link this program with OpenSSL, and 21 * distribute the resulting executable, without including the source code for 22 * OpenSSL in the source distribution. 23 */ 24 25 /* Network address API */ 26 27 #ifndef PR_NETADDR_H 28 #define PR_NETADDR_H 29 30 #include "os.h" 31 #include "pool.h" 32 33 #ifndef INET_ADDRSTRLEN 34 # define INET_ADDRSTRLEN 16 35 #endif /* INET_ADDRSTRLEN */ 36 37 #ifndef INET6_ADDRSTRLEN 38 # define INET6_ADDRSTRLEN 46 39 #endif /* INET6_ADDRSTRLEN */ 40 41 struct netaddr_struc { 42 int na_family; 43 44 /* Note: this assumes that DNS names have a maximum size of 45 * 256 characters. 46 */ 47 char na_dnsstr[256]; 48 int na_have_dnsstr; 49 50 #ifdef PR_USE_IPV6 51 char na_ipstr[INET6_ADDRSTRLEN]; 52 #else 53 char na_ipstr[INET_ADDRSTRLEN]; 54 #endif /* PR_USE_IPV6 */ 55 int na_have_ipstr; 56 57 /* Note: at some point, this union might/should be replaced with 58 * struct sockaddr_storage. Why? The sockaddr_storage struct is 59 * better defined to be aligned on OS/arch boundaries, for more efficient 60 * allocation/access. 61 */ 62 63 union { 64 struct sockaddr_in v4; 65 #ifdef PR_USE_IPV6 66 struct sockaddr_in6 v6; 67 #endif /* PR_USE_IPV6 */ 68 } na_addr; 69 }; 70 71 typedef struct netaddr_struc pr_netaddr_t; 72 73 #ifndef HAVE_STRUCT_ADDRINFO 74 struct addrinfo { 75 76 /* AI_PASSIVE, AI_CANONNAME */ 77 int ai_flags; 78 79 /* AF/PF_xxx */ 80 int ai_family; 81 82 /* SOCK_xxx */ 83 int ai_socktype; 84 85 /* IPPROTO_xxx for IPv4/v6 */ 86 int ai_protocol; 87 88 /* Length of ai_addr */ 89 int ai_addrlen; 90 91 /* Canonical name for host */ 92 char *ai_canonname; 93 94 /* Binary address */ 95 struct sockaddr *ai_addr; 96 97 /* Next structure in the linked list */ 98 struct addrinfo *ai_next; 99 }; 100 #endif /* HAVE_STRUCT_ADDRINFO */ 101 102 #if defined(HAVE_GETADDRINFO) && !defined(PR_USE_GETADDRINFO) 103 /* Use the system getaddrinfo(2) and freeaddrinfo(2) by redefining the 104 * 'pr_getaddrinfo' and 'pr_freeaddrinfo' symbols to be 'getaddrinfo' and 105 * 'freeaddrinfo', respectively. 106 */ 107 # define pr_getaddrinfo getaddrinfo 108 # define pr_freeaddrinfo freeaddrinfo 109 #else 110 int pr_getaddrinfo(const char *, const char *, const struct addrinfo *, 111 struct addrinfo **); 112 void pr_freeaddrinfo(struct addrinfo *); 113 #endif /* HAVE_GETNAMEINFO and !PR_USE_GETNAMEINFO */ 114 115 /* These AI_ defines are for use by getaddrinfo(3). */ 116 117 /* Indicates that the socket is intended for bind()+listen(). */ 118 #ifndef AI_PASSIVE 119 # define AI_PASSIVE 1 120 #endif /* AI_PASSIVE */ 121 122 /* Return the canonical name. */ 123 #ifndef AI_CANONNAME 124 # define AI_CANONNAME 2 125 #endif /* AI_CANONNAME */ 126 127 /* The following EAI_ defines are for errors. */ 128 129 /* Host address family not supported. */ 130 #ifndef EAI_ADDRFAMILY 131 # define EAI_ADDRFAMILY -1 132 #endif /* EAI_ADDRFAMILY */ 133 134 /* Temporary failure in name resolution. */ 135 #ifndef EAI_AGAIN 136 # define EAI_AGAIN -2 137 #endif /* EAI_AGAIN */ 138 139 /* Invalid value for ai_flags. */ 140 #ifndef EAI_BADFLAGS 141 # define EAI_BADFLAGS -3 142 #endif /* EAI_BADFLAGS */ 143 144 /* Non-recoverable failure in name resolution. */ 145 #ifndef EAI_FAIL 146 # define EAI_FAIL -4 147 #endif /* EAI_FAIL */ 148 149 /* ai_family not supported. */ 150 #ifndef EAI_FAMILY 151 # define EAI_FAMILY -5 152 #endif /* EAI_FAMILY */ 153 154 /* Memory allocation failure. */ 155 #ifndef EAI_MEMORY 156 # define EAI_MEMORY -6 157 #endif /* EAI_MEMORY */ 158 159 /* No address associated with host. */ 160 #ifndef EAI_NODATA 161 # define EAI_NODATA -7 162 #endif /* EAI_NODATA */ 163 164 /* Host nor service not provided, or not known. */ 165 #ifndef EAI_NONAME 166 # define EAI_NONAME -8 167 #endif /* EAI_NONAME */ 168 169 /* Service not supported for ai_socktype. */ 170 #ifndef EAI_SERVICE 171 # define EAI_SERVICE -9 172 #endif /* EAI_SERVICE */ 173 174 /* ai_socktype not supported. */ 175 #ifndef EAI_SOCKTYPE 176 # define EAI_SOCKTYPE -10 177 #endif /* EAI_SOCKTYPE */ 178 179 /* System error contained in errno. */ 180 #ifndef EAI_SYSTEM 181 # define EAI_SYSTEM -11 182 #endif /* EAI_SYSTEM */ 183 184 #if defined(HAVE_GETNAMEINFO) && !defined(PR_USE_GETNAMEINFO) 185 /* Use the system getnameinfo(2) by redefining the 'pr_getnameinfo' symbol 186 * to be simply 'getnameinfo'. 187 */ 188 # define pr_getnameinfo getnameinfo 189 #else 190 int pr_getnameinfo(const struct sockaddr *, socklen_t, char *, size_t, 191 char *, size_t, int); 192 #endif /* HAVE_GETNAMEINFO and !PR_USE_GETNAMEINFO */ 193 194 /* These NI_ defines are for use by getnameinfo(3). */ 195 196 /* Max hostname length returned. */ 197 #ifndef NI_MAXHOST 198 # define NI_MAXHOST 1025 199 #endif /* NI_MAXHOST */ 200 201 /* Max service name length returned. */ 202 #ifndef NI_MAXSERV 203 # define NI_MAXSERV 32 204 #endif /* NI_MAXSERV */ 205 206 /* Do not return FQDNs. */ 207 #ifndef NI_NOFQDN 208 # define NI_NOFQDN 1 209 #endif /* NI_NOFQDN */ 210 211 /* Return the numeric form of the hostname. */ 212 #ifndef NI_NUMERICHOST 213 # define NI_NUMERICHOST 2 214 #endif /* NI_NUMERICHOST */ 215 216 /* Return an error if hostname is not found. */ 217 #ifndef NI_NAMEREQD 218 # define NI_NAMEREQD 4 219 #endif /* NI_NAMEREQD */ 220 221 /* Return the numeric form of the service name. */ 222 #ifndef NI_NUMERICSERV 223 # define NI_NUMERICSERV 8 224 #endif /* NI_NUMERICSERV */ 225 226 /* Datagram service for getservbyname(). */ 227 #ifndef NI_DGRAM 228 # define NI_DGRAM 16 229 #endif /* NI_DGRAM */ 230 231 232 #if defined(HAVE_INET_NTOP) 233 /* Use the system inet_ntop(3) by redefining the 'pr_inet_ntop' symbol to be 234 * 'inet_ntop'. 235 */ 236 # define pr_inet_ntop inet_ntop 237 #else 238 const char *pr_inet_ntop(int, const void *, char *, size_t); 239 #endif 240 241 #if defined(HAVE_INET_PTON) 242 /* Use the system inet_pton(3) by redefining the 'pr_inet_pton' symbol to be 243 * 'inet_pton'. 244 */ 245 # define pr_inet_pton inet_pton 246 #else 247 int pr_inet_pton(int, const char *, void *); 248 #endif 249 250 /* Network Address API 251 */ 252 253 /* Allocate an initialized netaddr from the given pool. */ 254 pr_netaddr_t *pr_netaddr_alloc(pool *); 255 256 /* Duplicate a netaddr using the given pool. */ 257 pr_netaddr_t *pr_netaddr_dup(pool *, const pr_netaddr_t *); 258 259 /* Initialize the given netaddr. */ 260 void pr_netaddr_clear(pr_netaddr_t *); 261 262 /* Given a name (either an IP address string or a DNS name), return a 263 * pr_netaddr_t * for that name. In the case of DNS names, multiple 264 * addresses might be associated with given name; callers that are interested 265 * in these additional addresses should provide a pointer to an array_header *, 266 * which will be filled with an array_header (allocated from the given pool) 267 * that contains a list of additional pr_netaddr_t *'s. 268 * 269 * If there is a failure in resolving the given name to its address(es), 270 * NULL will be return, and an error logged. 271 */ 272 const pr_netaddr_t *pr_netaddr_get_addr(pool *, const char *, array_header **); 273 274 /* Like pr_netaddr_get_addr(), with the ability to specify lookup flags. */ 275 const pr_netaddr_t *pr_netaddr_get_addr2(pool *, const char *, array_header **, 276 unsigned int); 277 #define PR_NETADDR_GET_ADDR_FL_INCL_DEVICE 0x001 278 #define PR_NETADDR_GET_ADDR_FL_EXCL_DNS 0x002 279 280 /* Compare the two given pr_netaddr_ts. In order for the comparison to 281 * be accurate, the pr_netaddr_ts must be of the same family (AF_INET or 282 * AF_INET6). In the case where the pr_netaddr_ts are from different 283 * families, -1 will be returned, with errno set to EINVAL. Otherwise, 284 * the comparison is a fancy memcmp(). 285 */ 286 int pr_netaddr_cmp(const pr_netaddr_t *, const pr_netaddr_t *); 287 288 /* Compare the first N bits of the two given pr_netaddr_ts. In order for 289 * the comparison to be accurate, the pr_netaddr_ts must be of the same family 290 * (AF_INET or AF_INET6). In the case where the pr_netaddr_ts are from 291 * different families, -1 will be returned, with errno set to EINVAL. 292 * Otherwise, the comparison is a fancy memcmp(). 293 */ 294 int pr_netaddr_ncmp(const pr_netaddr_t *, const pr_netaddr_t *, unsigned int); 295 296 /* Compare the given pr_netaddr_t against a glob pattern, as intended for 297 * fnmatch(3). The flags parameter is an OR of the following values: 298 * PR_NETADDR_MATCH_DNS and PR_NETADDR_MATCH_IP. If the PR_NETADDR_MATCH_DNS 299 * flag is used, the given pattern will be matched against the DNS string of 300 * the netaddr, if present. If that doesn't match, and if the 301 * PR_NETADDR_MATCH_IP flag is used, a comparison against the IP address string 302 * will be tried. A return value of -1, with errno set to EINVAL, occurs if 303 * the netaddr or pattern are NULL. Otherwise, TRUE is returned if the address 304 * is matched by the pattern, or FALSE if is not matched. 305 */ 306 int pr_netaddr_fnmatch(const pr_netaddr_t *, const char *, int); 307 #define PR_NETADDR_MATCH_DNS 0x001 308 #define PR_NETADDR_MATCH_IP 0x002 309 310 /* Returns the size of the contained address (or -1, with errno set to EINVAL, 311 * if NULL is used as the argument). If the pr_netaddr_t is of the AF_INET 312 * family, the size of struct sockaddr_in is returned; if of the AF_INET6 313 * family, the size of struct sockaddr_in6 is returned. 314 */ 315 size_t pr_netaddr_get_sockaddr_len(const pr_netaddr_t *); 316 317 /* Returns the size of the contained address (or -1, with errno set to EINVAL, 318 * if NULL is used as the argument). If the pr_netaddr_t is of the AF_INET 319 * family, the size of struct in_addr is returned; if of the AF_INET6 320 * family, the size of struct in6_addr is returned. 321 */ 322 size_t pr_netaddr_get_inaddr_len(const pr_netaddr_t *); 323 324 /* Returns the family of the given pr_netaddr_t, either AF_INET or AF_INET6. 325 * A NULL pr_netaddr_t will result in -1 being returned, and errno set to 326 * EINVAL. 327 */ 328 int pr_netaddr_get_family(const pr_netaddr_t *); 329 330 /* Sets the family on the given pr_netaddr_t. Returns 0 on success, or 331 * -1 on error (as when NULL is used as the argument). 332 */ 333 int pr_netaddr_set_family(pr_netaddr_t *, int); 334 335 /* Returns a void * pointing to either a struct in_addr (if the family of the 336 * given pr_netaddr_t is AF_INET) or a struct in6_addr (if the family of the 337 * given pr_netaddr_t is AF_INET6). Returns NULL on error. 338 */ 339 void *pr_netaddr_get_inaddr(const pr_netaddr_t *); 340 341 /* Returns a struct sockaddr * (pointing to either a struct sockaddr_in or 342 * a struct sockaddr_in6, depending on the family), or NULL if there was an 343 * error. 344 */ 345 struct sockaddr *pr_netaddr_get_sockaddr(const pr_netaddr_t *); 346 347 /* Set the contained sockaddr * in the given pr_netaddr_t to be the 348 * sockaddr given. The family of the pr_netaddr_t must have been set 349 * first. Returns 0 on success, and -1 on error. 350 */ 351 int pr_netaddr_set_sockaddr(pr_netaddr_t *, struct sockaddr *); 352 353 /* Sets the address of the contained sockaddr to be the wildcard address. 354 * Returns 0 on success, and -1 on error. 355 */ 356 int pr_netaddr_set_sockaddr_any(pr_netaddr_t *); 357 358 /* Returns the port of the contained struct sockaddr *. */ 359 unsigned int pr_netaddr_get_port(const pr_netaddr_t *); 360 361 /* Sets the port on the contained struct sockaddr *. Returns 0 on success, 362 * or -1 on error (as when NULL is given as the argument). Note that the 363 * given port number is assumed to be in network byte order already. 364 */ 365 int pr_netaddr_set_port(pr_netaddr_t *, unsigned int); 366 367 /* Sets the port on the contained struct sockaddr *. Returns 0 on success, 368 * or -1 on error (as when NULL is given as the argument). Note that the 369 * given port number is assumed to be in host byte order. 370 */ 371 int pr_netaddr_set_port2(pr_netaddr_t *, unsigned int); 372 373 /* Enables or disable use of reverse DNS lookups. Returns the previous 374 * setting. 375 */ 376 int pr_netaddr_set_reverse_dns(int); 377 378 /* Returns the DNS name associated with the given pr_netaddr_t. If DNS 379 * lookups have been disabled, the returned string will be the IP address. 380 * Returns NULL if there was an error. 381 */ 382 const char *pr_netaddr_get_dnsstr(const pr_netaddr_t *); 383 384 /* Returns the list of DNS names associated with the given pr_netaddr_t. 385 * If DNS lookups have been disabled, an empty list will be returned. 386 * NULL is returned if there is an error. 387 */ 388 array_header *pr_netaddr_get_dnsstr_list(pool *, const pr_netaddr_t *); 389 390 /* Returns the IP address associated with the given pr_netaddr_t. Returns 391 * NULL if there was an error. 392 */ 393 const char *pr_netaddr_get_ipstr(const pr_netaddr_t *); 394 395 /* Returns the name of the local host, as returned by gethostname(2). The 396 * returned string will be dup'd from the given pool, if any. 397 */ 398 const char *pr_netaddr_get_localaddr_str(pool *); 399 400 /* Sets the name of the local host, overriding the name that would have 401 * been returned by gethostname(2). 402 * 403 * This function is used to avoid using DNS lookups on the gethostname(2) 404 * name in order to determine the IP address to use for the default 405 * 'server config' vhost. 406 */ 407 int pr_netaddr_set_localaddr_str(const char *); 408 409 uint32_t pr_netaddr_get_addrno(const pr_netaddr_t *); 410 411 /* Returns TRUE if the given pr_netaddr_t contains a loopback address, 412 * FALSE otherwise. 413 */ 414 int pr_netaddr_is_loopback(const pr_netaddr_t *); 415 416 /* Returns TRUE if the given pr_netaddr_t contains an RFC1918 address, 417 * FALSE otherwise. Note that -1 will be returned if there was an error, 418 * with errno set appropriately. 419 */ 420 int pr_netaddr_is_rfc1918(const pr_netaddr_t *); 421 422 /* Returns TRUE if the given string is an IPv4 address, FALSE if not, and -1 423 * (with errno set appropriately) if there was an error. 424 */ 425 int pr_netaddr_is_v4(const char *); 426 427 /* Returns TRUE if the given string is an IPv6 address, FALSE if not, and -1 428 * (with errno set appropriately) if there was an error. 429 */ 430 int pr_netaddr_is_v6(const char *); 431 432 /* Returns TRUE if the given pr_netaddr_t is of the AF_INET6 family and 433 * contains an IPv4-mapped IPv6 address; otherwise FALSE is returned. A 434 * return value of -1 is used to indicate an error. 435 */ 436 int pr_netaddr_is_v4mappedv6(const pr_netaddr_t *); 437 438 /* Given an IPv4-mapped IPv6 netaddr, returns an IPv4 netaddr allocated from 439 * the given pool. Returns NULL if the given netaddr is not an IPv4-mapped 440 * IPv6 address. 441 */ 442 pr_netaddr_t *pr_netaddr_v6tov4(pool *p, const pr_netaddr_t *addr); 443 444 /* Given an IPv4 netaddr, return an IPv4-mapped IPv6 netaddr allocated from 445 * the given pool. Returns NULL if the given netaddr is not an IPv4 address. 446 */ 447 pr_netaddr_t *pr_netaddr_v4tov6(pool *p, const pr_netaddr_t *addr); 448 449 /* Returns TRUE if IPv6 support is enabled, FALSE otherwise. */ 450 unsigned char pr_netaddr_use_ipv6(void); 451 452 /* Disables runtime use of IPv6 functionality (assuming IPv6 is supported). */ 453 void pr_netaddr_disable_ipv6(void); 454 455 /* Enables runtime use of IPv6 functionality (assuming IPv6 is supported). */ 456 void pr_netaddr_enable_ipv6(void); 457 458 /* Return pointers to static memory which contains the local and remote 459 * netaddr information for the session. DO NOT MODIFY the pointed-to 460 * memory! Returns NULL if no such session information exists. 461 */ 462 const pr_netaddr_t *pr_netaddr_get_sess_local_addr(void); 463 const pr_netaddr_t *pr_netaddr_get_sess_remote_addr(void); 464 const char *pr_netaddr_get_sess_remote_name(void); 465 void pr_netaddr_set_sess_addrs(void); 466 467 /* Clears the cache of ALL netaddr objects. */ 468 void pr_netaddr_clear_cache(void); 469 470 /* Clears the cached DNS names, given an IP address string. */ 471 void pr_netaddr_clear_dnscache(const char *ip_addr); 472 473 /* Clears the cached IP addresses, given a DNS name. */ 474 void pr_netaddr_clear_ipcache(const char *name); 475 476 /* Validates the DNS name returned. */ 477 char *pr_netaddr_validate_dns_str(char *); 478 479 /* Internal use only. */ 480 void init_netaddr(void); 481 482 #endif /* PR_NETADDR_H */ 483