From ec136d19b21791c845b1deeff43df137add0639e Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Fri, 2 Sep 2016 13:49:59 +0300 Subject: Move code shared between libpq and backend from backend/libpq/ to common/. When building libpq, ip.c and md5.c were symlinked or copied from src/backend/libpq into src/interfaces/libpq, but now that we have a directory specifically for routines that are shared between the server and client binaries, src/common/, move them there. Some routines in ip.c were only used in the backend. Keep those in src/backend/libpq, but rename to ifaddr.c to avoid confusion with the file that's now in common. Fix the comment in src/common/Makefile to reflect how libpq actually links those files. There are two more files that libpq symlinks directly from src/backend: encnames.c and wchar.c. I don't feel compelled to move those right now, though. Patch by Michael Paquier, with some changes by me. Discussion: <69938195-9c76-8523-0af8-eb718ea5b36e@iki.fi> --- src/backend/commands/user.c | 2 +- src/backend/libpq/Makefile | 2 +- src/backend/libpq/auth.c | 4 +- src/backend/libpq/crypt.c | 2 +- src/backend/libpq/hba.c | 3 +- src/backend/libpq/ifaddr.c | 597 ++++++++++++++++++++++++++ src/backend/libpq/ip.c | 819 ------------------------------------ src/backend/libpq/md5.c | 345 --------------- src/backend/libpq/pqcomm.c | 2 +- src/backend/postmaster/pgstat.c | 2 +- src/backend/postmaster/postmaster.c | 2 +- src/backend/utils/adt/network.c | 2 +- src/backend/utils/adt/pgstatfuncs.c | 2 +- src/backend/utils/adt/varlena.c | 2 +- 14 files changed, 610 insertions(+), 1176 deletions(-) create mode 100644 src/backend/libpq/ifaddr.c delete mode 100644 src/backend/libpq/ip.c delete mode 100644 src/backend/libpq/md5.c (limited to 'src/backend') diff --git a/src/backend/commands/user.c b/src/backend/commands/user.c index b6ea95061d0..821dce3ce7b 100644 --- a/src/backend/commands/user.c +++ b/src/backend/commands/user.c @@ -29,7 +29,7 @@ #include "commands/dbcommands.h" #include "commands/seclabel.h" #include "commands/user.h" -#include "libpq/md5.h" +#include "common/md5.h" #include "miscadmin.h" #include "storage/lmgr.h" #include "utils/acl.h" diff --git a/src/backend/libpq/Makefile b/src/backend/libpq/Makefile index 09410c4bb19..1bdd8adde24 100644 --- a/src/backend/libpq/Makefile +++ b/src/backend/libpq/Makefile @@ -14,7 +14,7 @@ include $(top_builddir)/src/Makefile.global # be-fsstubs is here for historical reasons, probably belongs elsewhere -OBJS = be-fsstubs.o be-secure.o auth.o crypt.o hba.o ip.o md5.o pqcomm.o \ +OBJS = be-fsstubs.o be-secure.o auth.o crypt.o hba.o ifaddr.o pqcomm.o \ pqformat.o pqmq.o pqsignal.o ifeq ($(with_openssl),yes) diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c index fc8b99b444b..d907e6bc919 100644 --- a/src/backend/libpq/auth.c +++ b/src/backend/libpq/auth.c @@ -21,12 +21,12 @@ #include #include +#include "common/ip.h" +#include "common/md5.h" #include "libpq/auth.h" #include "libpq/crypt.h" -#include "libpq/ip.h" #include "libpq/libpq.h" #include "libpq/pqformat.h" -#include "libpq/md5.h" #include "miscadmin.h" #include "replication/walsender.h" #include "storage/ipc.h" diff --git a/src/backend/libpq/crypt.c b/src/backend/libpq/crypt.c index d79f5a24968..d84a1803304 100644 --- a/src/backend/libpq/crypt.c +++ b/src/backend/libpq/crypt.c @@ -21,8 +21,8 @@ #endif #include "catalog/pg_authid.h" +#include "common/md5.h" #include "libpq/crypt.h" -#include "libpq/md5.h" #include "miscadmin.h" #include "utils/builtins.h" #include "utils/syscache.h" diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c index d612c11159e..f1e9a38c92e 100644 --- a/src/backend/libpq/hba.c +++ b/src/backend/libpq/hba.c @@ -26,7 +26,8 @@ #include #include "catalog/pg_collation.h" -#include "libpq/ip.h" +#include "common/ip.h" +#include "libpq/ifaddr.h" #include "libpq/libpq.h" #include "postmaster/postmaster.h" #include "regex/regex.h" diff --git a/src/backend/libpq/ifaddr.c b/src/backend/libpq/ifaddr.c new file mode 100644 index 00000000000..8f7fa2015d4 --- /dev/null +++ b/src/backend/libpq/ifaddr.c @@ -0,0 +1,597 @@ +/*------------------------------------------------------------------------- + * + * ifaddr.c + * IP netmask calculations, and enumerating network interfaces. + * + * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/libpq/ifaddr.c + * + * This file and the IPV6 implementation were initially provided by + * Nigel Kukard , Linux Based Systems Design + * http://www.lbsd.net. + * + *------------------------------------------------------------------------- + */ + +#include "postgres.h" + +#include +#include +#include +#include +#include +#include +#ifdef HAVE_NETINET_TCP_H +#include +#endif +#include +#include + +#include "libpq/ifaddr.h" + +static int range_sockaddr_AF_INET(const struct sockaddr_in * addr, + const struct sockaddr_in * netaddr, + const struct sockaddr_in * netmask); + +#ifdef HAVE_IPV6 +static int range_sockaddr_AF_INET6(const struct sockaddr_in6 * addr, + const struct sockaddr_in6 * netaddr, + const struct sockaddr_in6 * netmask); +#endif + + +/* + * pg_range_sockaddr - is addr within the subnet specified by netaddr/netmask ? + * + * Note: caller must already have verified that all three addresses are + * in the same address family; and AF_UNIX addresses are not supported. + */ +int +pg_range_sockaddr(const struct sockaddr_storage * addr, + const struct sockaddr_storage * netaddr, + const struct sockaddr_storage * netmask) +{ + if (addr->ss_family == AF_INET) + return range_sockaddr_AF_INET((const struct sockaddr_in *) addr, + (const struct sockaddr_in *) netaddr, + (const struct sockaddr_in *) netmask); +#ifdef HAVE_IPV6 + else if (addr->ss_family == AF_INET6) + return range_sockaddr_AF_INET6((const struct sockaddr_in6 *) addr, + (const struct sockaddr_in6 *) netaddr, + (const struct sockaddr_in6 *) netmask); +#endif + else + return 0; +} + +static int +range_sockaddr_AF_INET(const struct sockaddr_in * addr, + const struct sockaddr_in * netaddr, + const struct sockaddr_in * netmask) +{ + if (((addr->sin_addr.s_addr ^ netaddr->sin_addr.s_addr) & + netmask->sin_addr.s_addr) == 0) + return 1; + else + return 0; +} + + +#ifdef HAVE_IPV6 + +static int +range_sockaddr_AF_INET6(const struct sockaddr_in6 * addr, + const struct sockaddr_in6 * netaddr, + const struct sockaddr_in6 * netmask) +{ + int i; + + for (i = 0; i < 16; i++) + { + if (((addr->sin6_addr.s6_addr[i] ^ netaddr->sin6_addr.s6_addr[i]) & + netmask->sin6_addr.s6_addr[i]) != 0) + return 0; + } + + return 1; +} +#endif /* HAVE_IPV6 */ + +/* + * pg_sockaddr_cidr_mask - make a network mask of the appropriate family + * and required number of significant bits + * + * numbits can be null, in which case the mask is fully set. + * + * The resulting mask is placed in *mask, which had better be big enough. + * + * Return value is 0 if okay, -1 if not. + */ +int +pg_sockaddr_cidr_mask(struct sockaddr_storage * mask, char *numbits, int family) +{ + long bits; + char *endptr; + + if (numbits == NULL) + { + bits = (family == AF_INET) ? 32 : 128; + } + else + { + bits = strtol(numbits, &endptr, 10); + if (*numbits == '\0' || *endptr != '\0') + return -1; + } + + switch (family) + { + case AF_INET: + { + struct sockaddr_in mask4; + long maskl; + + if (bits < 0 || bits > 32) + return -1; + memset(&mask4, 0, sizeof(mask4)); + /* avoid "x << 32", which is not portable */ + if (bits > 0) + maskl = (0xffffffffUL << (32 - (int) bits)) + & 0xffffffffUL; + else + maskl = 0; + mask4.sin_addr.s_addr = htonl(maskl); + memcpy(mask, &mask4, sizeof(mask4)); + break; + } + +#ifdef HAVE_IPV6 + case AF_INET6: + { + struct sockaddr_in6 mask6; + int i; + + if (bits < 0 || bits > 128) + return -1; + memset(&mask6, 0, sizeof(mask6)); + for (i = 0; i < 16; i++) + { + if (bits <= 0) + mask6.sin6_addr.s6_addr[i] = 0; + else if (bits >= 8) + mask6.sin6_addr.s6_addr[i] = 0xff; + else + { + mask6.sin6_addr.s6_addr[i] = + (0xff << (8 - (int) bits)) & 0xff; + } + bits -= 8; + } + memcpy(mask, &mask6, sizeof(mask6)); + break; + } +#endif + default: + return -1; + } + + mask->ss_family = family; + return 0; +} + + +/* + * Run the callback function for the addr/mask, after making sure the + * mask is sane for the addr. + */ +static void +run_ifaddr_callback(PgIfAddrCallback callback, void *cb_data, + struct sockaddr * addr, struct sockaddr * mask) +{ + struct sockaddr_storage fullmask; + + if (!addr) + return; + + /* Check that the mask is valid */ + if (mask) + { + if (mask->sa_family != addr->sa_family) + { + mask = NULL; + } + else if (mask->sa_family == AF_INET) + { + if (((struct sockaddr_in *) mask)->sin_addr.s_addr == INADDR_ANY) + mask = NULL; + } +#ifdef HAVE_IPV6 + else if (mask->sa_family == AF_INET6) + { + if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *) mask)->sin6_addr)) + mask = NULL; + } +#endif + } + + /* If mask is invalid, generate our own fully-set mask */ + if (!mask) + { + pg_sockaddr_cidr_mask(&fullmask, NULL, addr->sa_family); + mask = (struct sockaddr *) & fullmask; + } + + (*callback) (addr, mask, cb_data); +} + +#ifdef WIN32 + +#include +#include + +/* + * Enumerate the system's network interface addresses and call the callback + * for each one. Returns 0 if successful, -1 if trouble. + * + * This version is for Win32. Uses the Winsock 2 functions (ie: ws2_32.dll) + */ +int +pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) +{ + INTERFACE_INFO *ptr, + *ii = NULL; + unsigned long length, + i; + unsigned long n_ii = 0; + SOCKET sock; + int error; + + sock = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0); + if (sock == INVALID_SOCKET) + return -1; + + while (n_ii < 1024) + { + n_ii += 64; + ptr = realloc(ii, sizeof(INTERFACE_INFO) * n_ii); + if (!ptr) + { + free(ii); + closesocket(sock); + errno = ENOMEM; + return -1; + } + + ii = ptr; + if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, 0, 0, + ii, n_ii * sizeof(INTERFACE_INFO), + &length, 0, 0) == SOCKET_ERROR) + { + error = WSAGetLastError(); + if (error == WSAEFAULT || error == WSAENOBUFS) + continue; /* need to make the buffer bigger */ + closesocket(sock); + free(ii); + return -1; + } + + break; + } + + for (i = 0; i < length / sizeof(INTERFACE_INFO); ++i) + run_ifaddr_callback(callback, cb_data, + (struct sockaddr *) & ii[i].iiAddress, + (struct sockaddr *) & ii[i].iiNetmask); + + closesocket(sock); + free(ii); + return 0; +} +#elif HAVE_GETIFADDRS /* && !WIN32 */ + +#ifdef HAVE_IFADDRS_H +#include +#endif + +/* + * Enumerate the system's network interface addresses and call the callback + * for each one. Returns 0 if successful, -1 if trouble. + * + * This version uses the getifaddrs() interface, which is available on + * BSDs, AIX, and modern Linux. + */ +int +pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) +{ + struct ifaddrs *ifa, + *l; + + if (getifaddrs(&ifa) < 0) + return -1; + + for (l = ifa; l; l = l->ifa_next) + run_ifaddr_callback(callback, cb_data, + l->ifa_addr, l->ifa_netmask); + + freeifaddrs(ifa); + return 0; +} +#else /* !HAVE_GETIFADDRS && !WIN32 */ + +#ifdef HAVE_SYS_IOCTL_H +#include +#endif + +#ifdef HAVE_NET_IF_H +#include +#endif + +#ifdef HAVE_SYS_SOCKIO_H +#include +#endif + +/* + * SIOCGIFCONF does not return IPv6 addresses on Solaris + * and HP/UX. So we prefer SIOCGLIFCONF if it's available. + * + * On HP/UX, however, it *only* returns IPv6 addresses, + * and the structs are named slightly differently too. + * We'd have to do another call with SIOCGIFCONF to get the + * IPv4 addresses as well. We don't currently bother, just + * fall back to SIOCGIFCONF on HP/UX. + */ + +#if defined(SIOCGLIFCONF) && !defined(__hpux) + +/* + * Enumerate the system's network interface addresses and call the callback + * for each one. Returns 0 if successful, -1 if trouble. + * + * This version uses ioctl(SIOCGLIFCONF). + */ +int +pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) +{ + struct lifconf lifc; + struct lifreq *lifr, + lmask; + struct sockaddr *addr, + *mask; + char *ptr, + *buffer = NULL; + size_t n_buffer = 1024; + pgsocket sock, + fd; + +#ifdef HAVE_IPV6 + pgsocket sock6; +#endif + int i, + total; + + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock == PGINVALID_SOCKET) + return -1; + + while (n_buffer < 1024 * 100) + { + n_buffer += 1024; + ptr = realloc(buffer, n_buffer); + if (!ptr) + { + free(buffer); + close(sock); + errno = ENOMEM; + return -1; + } + + memset(&lifc, 0, sizeof(lifc)); + lifc.lifc_family = AF_UNSPEC; + lifc.lifc_buf = buffer = ptr; + lifc.lifc_len = n_buffer; + + if (ioctl(sock, SIOCGLIFCONF, &lifc) < 0) + { + if (errno == EINVAL) + continue; + free(buffer); + close(sock); + return -1; + } + + /* + * Some Unixes try to return as much data as possible, with no + * indication of whether enough space allocated. Don't believe we have + * it all unless there's lots of slop. + */ + if (lifc.lifc_len < n_buffer - 1024) + break; + } + +#ifdef HAVE_IPV6 + /* We'll need an IPv6 socket too for the SIOCGLIFNETMASK ioctls */ + sock6 = socket(AF_INET6, SOCK_DGRAM, 0); + if (sock6 == PGINVALID_SOCKET) + { + free(buffer); + close(sock); + return -1; + } +#endif + + total = lifc.lifc_len / sizeof(struct lifreq); + lifr = lifc.lifc_req; + for (i = 0; i < total; ++i) + { + addr = (struct sockaddr *) & lifr[i].lifr_addr; + memcpy(&lmask, &lifr[i], sizeof(struct lifreq)); +#ifdef HAVE_IPV6 + fd = (addr->sa_family == AF_INET6) ? sock6 : sock; +#else + fd = sock; +#endif + if (ioctl(fd, SIOCGLIFNETMASK, &lmask) < 0) + mask = NULL; + else + mask = (struct sockaddr *) & lmask.lifr_addr; + run_ifaddr_callback(callback, cb_data, addr, mask); + } + + free(buffer); + close(sock); +#ifdef HAVE_IPV6 + close(sock6); +#endif + return 0; +} +#elif defined(SIOCGIFCONF) + +/* + * Remaining Unixes use SIOCGIFCONF. Some only return IPv4 information + * here, so this is the least preferred method. Note that there is no + * standard way to iterate the struct ifreq returned in the array. + * On some OSs the structures are padded large enough for any address, + * on others you have to calculate the size of the struct ifreq. + */ + +/* Some OSs have _SIZEOF_ADDR_IFREQ, so just use that */ +#ifndef _SIZEOF_ADDR_IFREQ + +/* Calculate based on sockaddr.sa_len */ +#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN +#define _SIZEOF_ADDR_IFREQ(ifr) \ + ((ifr).ifr_addr.sa_len > sizeof(struct sockaddr) ? \ + (sizeof(struct ifreq) - sizeof(struct sockaddr) + \ + (ifr).ifr_addr.sa_len) : sizeof(struct ifreq)) + +/* Padded ifreq structure, simple */ +#else +#define _SIZEOF_ADDR_IFREQ(ifr) \ + sizeof (struct ifreq) +#endif +#endif /* !_SIZEOF_ADDR_IFREQ */ + +/* + * Enumerate the system's network interface addresses and call the callback + * for each one. Returns 0 if successful, -1 if trouble. + * + * This version uses ioctl(SIOCGIFCONF). + */ +int +pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) +{ + struct ifconf ifc; + struct ifreq *ifr, + *end, + addr, + mask; + char *ptr, + *buffer = NULL; + size_t n_buffer = 1024; + pgsocket sock; + + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock == PGINVALID_SOCKET) + return -1; + + while (n_buffer < 1024 * 100) + { + n_buffer += 1024; + ptr = realloc(buffer, n_buffer); + if (!ptr) + { + free(buffer); + close(sock); + errno = ENOMEM; + return -1; + } + + memset(&ifc, 0, sizeof(ifc)); + ifc.ifc_buf = buffer = ptr; + ifc.ifc_len = n_buffer; + + if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) + { + if (errno == EINVAL) + continue; + free(buffer); + close(sock); + return -1; + } + + /* + * Some Unixes try to return as much data as possible, with no + * indication of whether enough space allocated. Don't believe we have + * it all unless there's lots of slop. + */ + if (ifc.ifc_len < n_buffer - 1024) + break; + } + + end = (struct ifreq *) (buffer + ifc.ifc_len); + for (ifr = ifc.ifc_req; ifr < end;) + { + memcpy(&addr, ifr, sizeof(addr)); + memcpy(&mask, ifr, sizeof(mask)); + if (ioctl(sock, SIOCGIFADDR, &addr, sizeof(addr)) == 0 && + ioctl(sock, SIOCGIFNETMASK, &mask, sizeof(mask)) == 0) + run_ifaddr_callback(callback, cb_data, + &addr.ifr_addr, &mask.ifr_addr); + ifr = (struct ifreq *) ((char *) ifr + _SIZEOF_ADDR_IFREQ(*ifr)); + } + + free(buffer); + close(sock); + return 0; +} +#else /* !defined(SIOCGIFCONF) */ + +/* + * Enumerate the system's network interface addresses and call the callback + * for each one. Returns 0 if successful, -1 if trouble. + * + * This version is our fallback if there's no known way to get the + * interface addresses. Just return the standard loopback addresses. + */ +int +pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) +{ + struct sockaddr_in addr; + struct sockaddr_storage mask; + +#ifdef HAVE_IPV6 + struct sockaddr_in6 addr6; +#endif + + /* addr 127.0.0.1/8 */ + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = ntohl(0x7f000001); + memset(&mask, 0, sizeof(mask)); + pg_sockaddr_cidr_mask(&mask, "8", AF_INET); + run_ifaddr_callback(callback, cb_data, + (struct sockaddr *) & addr, + (struct sockaddr *) & mask); + +#ifdef HAVE_IPV6 + /* addr ::1/128 */ + memset(&addr6, 0, sizeof(addr6)); + addr6.sin6_family = AF_INET6; + addr6.sin6_addr.s6_addr[15] = 1; + memset(&mask, 0, sizeof(mask)); + pg_sockaddr_cidr_mask(&mask, "128", AF_INET6); + run_ifaddr_callback(callback, cb_data, + (struct sockaddr *) & addr6, + (struct sockaddr *) & mask); +#endif + + return 0; +} +#endif /* !defined(SIOCGIFCONF) */ + +#endif /* !HAVE_GETIFADDRS */ diff --git a/src/backend/libpq/ip.c b/src/backend/libpq/ip.c deleted file mode 100644 index 9591ed28624..00000000000 --- a/src/backend/libpq/ip.c +++ /dev/null @@ -1,819 +0,0 @@ -/*------------------------------------------------------------------------- - * - * ip.c - * IPv6-aware network access. - * - * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group - * Portions Copyright (c) 1994, Regents of the University of California - * - * - * IDENTIFICATION - * src/backend/libpq/ip.c - * - * This file and the IPV6 implementation were initially provided by - * Nigel Kukard , Linux Based Systems Design - * http://www.lbsd.net. - * - *------------------------------------------------------------------------- - */ - -/* This is intended to be used in both frontend and backend, so use c.h */ -#include "c.h" - -#include -#include -#include -#include -#include -#include -#ifdef HAVE_NETINET_TCP_H -#include -#endif -#include -#include - -#include "libpq/ip.h" - - -static int range_sockaddr_AF_INET(const struct sockaddr_in * addr, - const struct sockaddr_in * netaddr, - const struct sockaddr_in * netmask); - -#ifdef HAVE_IPV6 -static int range_sockaddr_AF_INET6(const struct sockaddr_in6 * addr, - const struct sockaddr_in6 * netaddr, - const struct sockaddr_in6 * netmask); -#endif - -#ifdef HAVE_UNIX_SOCKETS -static int getaddrinfo_unix(const char *path, - const struct addrinfo * hintsp, - struct addrinfo ** result); - -static int getnameinfo_unix(const struct sockaddr_un * sa, int salen, - char *node, int nodelen, - char *service, int servicelen, - int flags); -#endif - - -/* - * pg_getaddrinfo_all - get address info for Unix, IPv4 and IPv6 sockets - */ -int -pg_getaddrinfo_all(const char *hostname, const char *servname, - const struct addrinfo * hintp, struct addrinfo ** result) -{ - int rc; - - /* not all versions of getaddrinfo() zero *result on failure */ - *result = NULL; - -#ifdef HAVE_UNIX_SOCKETS - if (hintp->ai_family == AF_UNIX) - return getaddrinfo_unix(servname, hintp, result); -#endif - - /* NULL has special meaning to getaddrinfo(). */ - rc = getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname, - servname, hintp, result); - - return rc; -} - - -/* - * pg_freeaddrinfo_all - free addrinfo structures for IPv4, IPv6, or Unix - * - * Note: the ai_family field of the original hint structure must be passed - * so that we can tell whether the addrinfo struct was built by the system's - * getaddrinfo() routine or our own getaddrinfo_unix() routine. Some versions - * of getaddrinfo() might be willing to return AF_UNIX addresses, so it's - * not safe to look at ai_family in the addrinfo itself. - */ -void -pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo * ai) -{ -#ifdef HAVE_UNIX_SOCKETS - if (hint_ai_family == AF_UNIX) - { - /* struct was built by getaddrinfo_unix (see pg_getaddrinfo_all) */ - while (ai != NULL) - { - struct addrinfo *p = ai; - - ai = ai->ai_next; - free(p->ai_addr); - free(p); - } - } - else -#endif /* HAVE_UNIX_SOCKETS */ - { - /* struct was built by getaddrinfo() */ - if (ai != NULL) - freeaddrinfo(ai); - } -} - - -/* - * pg_getnameinfo_all - get name info for Unix, IPv4 and IPv6 sockets - * - * The API of this routine differs from the standard getnameinfo() definition - * in two ways: first, the addr parameter is declared as sockaddr_storage - * rather than struct sockaddr, and second, the node and service fields are - * guaranteed to be filled with something even on failure return. - */ -int -pg_getnameinfo_all(const struct sockaddr_storage * addr, int salen, - char *node, int nodelen, - char *service, int servicelen, - int flags) -{ - int rc; - -#ifdef HAVE_UNIX_SOCKETS - if (addr && addr->ss_family == AF_UNIX) - rc = getnameinfo_unix((const struct sockaddr_un *) addr, salen, - node, nodelen, - service, servicelen, - flags); - else -#endif - rc = getnameinfo((const struct sockaddr *) addr, salen, - node, nodelen, - service, servicelen, - flags); - - if (rc != 0) - { - if (node) - strlcpy(node, "???", nodelen); - if (service) - strlcpy(service, "???", servicelen); - } - - return rc; -} - - -#if defined(HAVE_UNIX_SOCKETS) - -/* ------- - * getaddrinfo_unix - get unix socket info using IPv6-compatible API - * - * Bugs: only one addrinfo is set even though hintsp is NULL or - * ai_socktype is 0 - * AI_CANONNAME is not supported. - * ------- - */ -static int -getaddrinfo_unix(const char *path, const struct addrinfo * hintsp, - struct addrinfo ** result) -{ - struct addrinfo hints; - struct addrinfo *aip; - struct sockaddr_un *unp; - - *result = NULL; - - MemSet(&hints, 0, sizeof(hints)); - - if (strlen(path) >= sizeof(unp->sun_path)) - return EAI_FAIL; - - if (hintsp == NULL) - { - hints.ai_family = AF_UNIX; - hints.ai_socktype = SOCK_STREAM; - } - else - memcpy(&hints, hintsp, sizeof(hints)); - - if (hints.ai_socktype == 0) - hints.ai_socktype = SOCK_STREAM; - - if (hints.ai_family != AF_UNIX) - { - /* shouldn't have been called */ - return EAI_FAIL; - } - - aip = calloc(1, sizeof(struct addrinfo)); - if (aip == NULL) - return EAI_MEMORY; - - unp = calloc(1, sizeof(struct sockaddr_un)); - if (unp == NULL) - { - free(aip); - return EAI_MEMORY; - } - - aip->ai_family = AF_UNIX; - aip->ai_socktype = hints.ai_socktype; - aip->ai_protocol = hints.ai_protocol; - aip->ai_next = NULL; - aip->ai_canonname = NULL; - *result = aip; - - unp->sun_family = AF_UNIX; - aip->ai_addr = (struct sockaddr *) unp; - aip->ai_addrlen = sizeof(struct sockaddr_un); - - strcpy(unp->sun_path, path); - -#ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN - unp->sun_len = sizeof(struct sockaddr_un); -#endif - - return 0; -} - -/* - * Convert an address to a hostname. - */ -static int -getnameinfo_unix(const struct sockaddr_un * sa, int salen, - char *node, int nodelen, - char *service, int servicelen, - int flags) -{ - int ret = -1; - - /* Invalid arguments. */ - if (sa == NULL || sa->sun_family != AF_UNIX || - (node == NULL && service == NULL)) - return EAI_FAIL; - - if (node) - { - ret = snprintf(node, nodelen, "%s", "[local]"); - if (ret == -1 || ret > nodelen) - return EAI_MEMORY; - } - - if (service) - { - ret = snprintf(service, servicelen, "%s", sa->sun_path); - if (ret == -1 || ret > servicelen) - return EAI_MEMORY; - } - - return 0; -} -#endif /* HAVE_UNIX_SOCKETS */ - - -/* - * pg_range_sockaddr - is addr within the subnet specified by netaddr/netmask ? - * - * Note: caller must already have verified that all three addresses are - * in the same address family; and AF_UNIX addresses are not supported. - */ -int -pg_range_sockaddr(const struct sockaddr_storage * addr, - const struct sockaddr_storage * netaddr, - const struct sockaddr_storage * netmask) -{ - if (addr->ss_family == AF_INET) - return range_sockaddr_AF_INET((const struct sockaddr_in *) addr, - (const struct sockaddr_in *) netaddr, - (const struct sockaddr_in *) netmask); -#ifdef HAVE_IPV6 - else if (addr->ss_family == AF_INET6) - return range_sockaddr_AF_INET6((const struct sockaddr_in6 *) addr, - (const struct sockaddr_in6 *) netaddr, - (const struct sockaddr_in6 *) netmask); -#endif - else - return 0; -} - -static int -range_sockaddr_AF_INET(const struct sockaddr_in * addr, - const struct sockaddr_in * netaddr, - const struct sockaddr_in * netmask) -{ - if (((addr->sin_addr.s_addr ^ netaddr->sin_addr.s_addr) & - netmask->sin_addr.s_addr) == 0) - return 1; - else - return 0; -} - - -#ifdef HAVE_IPV6 - -static int -range_sockaddr_AF_INET6(const struct sockaddr_in6 * addr, - const struct sockaddr_in6 * netaddr, - const struct sockaddr_in6 * netmask) -{ - int i; - - for (i = 0; i < 16; i++) - { - if (((addr->sin6_addr.s6_addr[i] ^ netaddr->sin6_addr.s6_addr[i]) & - netmask->sin6_addr.s6_addr[i]) != 0) - return 0; - } - - return 1; -} -#endif /* HAVE_IPV6 */ - -/* - * pg_sockaddr_cidr_mask - make a network mask of the appropriate family - * and required number of significant bits - * - * numbits can be null, in which case the mask is fully set. - * - * The resulting mask is placed in *mask, which had better be big enough. - * - * Return value is 0 if okay, -1 if not. - */ -int -pg_sockaddr_cidr_mask(struct sockaddr_storage * mask, char *numbits, int family) -{ - long bits; - char *endptr; - - if (numbits == NULL) - { - bits = (family == AF_INET) ? 32 : 128; - } - else - { - bits = strtol(numbits, &endptr, 10); - if (*numbits == '\0' || *endptr != '\0') - return -1; - } - - switch (family) - { - case AF_INET: - { - struct sockaddr_in mask4; - long maskl; - - if (bits < 0 || bits > 32) - return -1; - memset(&mask4, 0, sizeof(mask4)); - /* avoid "x << 32", which is not portable */ - if (bits > 0) - maskl = (0xffffffffUL << (32 - (int) bits)) - & 0xffffffffUL; - else - maskl = 0; - mask4.sin_addr.s_addr = htonl(maskl); - memcpy(mask, &mask4, sizeof(mask4)); - break; - } - -#ifdef HAVE_IPV6 - case AF_INET6: - { - struct sockaddr_in6 mask6; - int i; - - if (bits < 0 || bits > 128) - return -1; - memset(&mask6, 0, sizeof(mask6)); - for (i = 0; i < 16; i++) - { - if (bits <= 0) - mask6.sin6_addr.s6_addr[i] = 0; - else if (bits >= 8) - mask6.sin6_addr.s6_addr[i] = 0xff; - else - { - mask6.sin6_addr.s6_addr[i] = - (0xff << (8 - (int) bits)) & 0xff; - } - bits -= 8; - } - memcpy(mask, &mask6, sizeof(mask6)); - break; - } -#endif - default: - return -1; - } - - mask->ss_family = family; - return 0; -} - - -/* - * Run the callback function for the addr/mask, after making sure the - * mask is sane for the addr. - */ -static void -run_ifaddr_callback(PgIfAddrCallback callback, void *cb_data, - struct sockaddr * addr, struct sockaddr * mask) -{ - struct sockaddr_storage fullmask; - - if (!addr) - return; - - /* Check that the mask is valid */ - if (mask) - { - if (mask->sa_family != addr->sa_family) - { - mask = NULL; - } - else if (mask->sa_family == AF_INET) - { - if (((struct sockaddr_in *) mask)->sin_addr.s_addr == INADDR_ANY) - mask = NULL; - } -#ifdef HAVE_IPV6 - else if (mask->sa_family == AF_INET6) - { - if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *) mask)->sin6_addr)) - mask = NULL; - } -#endif - } - - /* If mask is invalid, generate our own fully-set mask */ - if (!mask) - { - pg_sockaddr_cidr_mask(&fullmask, NULL, addr->sa_family); - mask = (struct sockaddr *) & fullmask; - } - - (*callback) (addr, mask, cb_data); -} - -#ifdef WIN32 - -#include -#include - -/* - * Enumerate the system's network interface addresses and call the callback - * for each one. Returns 0 if successful, -1 if trouble. - * - * This version is for Win32. Uses the Winsock 2 functions (ie: ws2_32.dll) - */ -int -pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) -{ - INTERFACE_INFO *ptr, - *ii = NULL; - unsigned long length, - i; - unsigned long n_ii = 0; - SOCKET sock; - int error; - - sock = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0); - if (sock == INVALID_SOCKET) - return -1; - - while (n_ii < 1024) - { - n_ii += 64; - ptr = realloc(ii, sizeof(INTERFACE_INFO) * n_ii); - if (!ptr) - { - free(ii); - closesocket(sock); - errno = ENOMEM; - return -1; - } - - ii = ptr; - if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, 0, 0, - ii, n_ii * sizeof(INTERFACE_INFO), - &length, 0, 0) == SOCKET_ERROR) - { - error = WSAGetLastError(); - if (error == WSAEFAULT || error == WSAENOBUFS) - continue; /* need to make the buffer bigger */ - closesocket(sock); - free(ii); - return -1; - } - - break; - } - - for (i = 0; i < length / sizeof(INTERFACE_INFO); ++i) - run_ifaddr_callback(callback, cb_data, - (struct sockaddr *) & ii[i].iiAddress, - (struct sockaddr *) & ii[i].iiNetmask); - - closesocket(sock); - free(ii); - return 0; -} -#elif HAVE_GETIFADDRS /* && !WIN32 */ - -#ifdef HAVE_IFADDRS_H -#include -#endif - -/* - * Enumerate the system's network interface addresses and call the callback - * for each one. Returns 0 if successful, -1 if trouble. - * - * This version uses the getifaddrs() interface, which is available on - * BSDs, AIX, and modern Linux. - */ -int -pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) -{ - struct ifaddrs *ifa, - *l; - - if (getifaddrs(&ifa) < 0) - return -1; - - for (l = ifa; l; l = l->ifa_next) - run_ifaddr_callback(callback, cb_data, - l->ifa_addr, l->ifa_netmask); - - freeifaddrs(ifa); - return 0; -} -#else /* !HAVE_GETIFADDRS && !WIN32 */ - -#ifdef HAVE_SYS_IOCTL_H -#include -#endif - -#ifdef HAVE_NET_IF_H -#include -#endif - -#ifdef HAVE_SYS_SOCKIO_H -#include -#endif - -/* - * SIOCGIFCONF does not return IPv6 addresses on Solaris - * and HP/UX. So we prefer SIOCGLIFCONF if it's available. - * - * On HP/UX, however, it *only* returns IPv6 addresses, - * and the structs are named slightly differently too. - * We'd have to do another call with SIOCGIFCONF to get the - * IPv4 addresses as well. We don't currently bother, just - * fall back to SIOCGIFCONF on HP/UX. - */ - -#if defined(SIOCGLIFCONF) && !defined(__hpux) - -/* - * Enumerate the system's network interface addresses and call the callback - * for each one. Returns 0 if successful, -1 if trouble. - * - * This version uses ioctl(SIOCGLIFCONF). - */ -int -pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) -{ - struct lifconf lifc; - struct lifreq *lifr, - lmask; - struct sockaddr *addr, - *mask; - char *ptr, - *buffer = NULL; - size_t n_buffer = 1024; - pgsocket sock, - fd; - -#ifdef HAVE_IPV6 - pgsocket sock6; -#endif - int i, - total; - - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock == PGINVALID_SOCKET) - return -1; - - while (n_buffer < 1024 * 100) - { - n_buffer += 1024; - ptr = realloc(buffer, n_buffer); - if (!ptr) - { - free(buffer); - close(sock); - errno = ENOMEM; - return -1; - } - - memset(&lifc, 0, sizeof(lifc)); - lifc.lifc_family = AF_UNSPEC; - lifc.lifc_buf = buffer = ptr; - lifc.lifc_len = n_buffer; - - if (ioctl(sock, SIOCGLIFCONF, &lifc) < 0) - { - if (errno == EINVAL) - continue; - free(buffer); - close(sock); - return -1; - } - - /* - * Some Unixes try to return as much data as possible, with no - * indication of whether enough space allocated. Don't believe we have - * it all unless there's lots of slop. - */ - if (lifc.lifc_len < n_buffer - 1024) - break; - } - -#ifdef HAVE_IPV6 - /* We'll need an IPv6 socket too for the SIOCGLIFNETMASK ioctls */ - sock6 = socket(AF_INET6, SOCK_DGRAM, 0); - if (sock6 == PGINVALID_SOCKET) - { - free(buffer); - close(sock); - return -1; - } -#endif - - total = lifc.lifc_len / sizeof(struct lifreq); - lifr = lifc.lifc_req; - for (i = 0; i < total; ++i) - { - addr = (struct sockaddr *) & lifr[i].lifr_addr; - memcpy(&lmask, &lifr[i], sizeof(struct lifreq)); -#ifdef HAVE_IPV6 - fd = (addr->sa_family == AF_INET6) ? sock6 : sock; -#else - fd = sock; -#endif - if (ioctl(fd, SIOCGLIFNETMASK, &lmask) < 0) - mask = NULL; - else - mask = (struct sockaddr *) & lmask.lifr_addr; - run_ifaddr_callback(callback, cb_data, addr, mask); - } - - free(buffer); - close(sock); -#ifdef HAVE_IPV6 - close(sock6); -#endif - return 0; -} -#elif defined(SIOCGIFCONF) - -/* - * Remaining Unixes use SIOCGIFCONF. Some only return IPv4 information - * here, so this is the least preferred method. Note that there is no - * standard way to iterate the struct ifreq returned in the array. - * On some OSs the structures are padded large enough for any address, - * on others you have to calculate the size of the struct ifreq. - */ - -/* Some OSs have _SIZEOF_ADDR_IFREQ, so just use that */ -#ifndef _SIZEOF_ADDR_IFREQ - -/* Calculate based on sockaddr.sa_len */ -#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN -#define _SIZEOF_ADDR_IFREQ(ifr) \ - ((ifr).ifr_addr.sa_len > sizeof(struct sockaddr) ? \ - (sizeof(struct ifreq) - sizeof(struct sockaddr) + \ - (ifr).ifr_addr.sa_len) : sizeof(struct ifreq)) - -/* Padded ifreq structure, simple */ -#else -#define _SIZEOF_ADDR_IFREQ(ifr) \ - sizeof (struct ifreq) -#endif -#endif /* !_SIZEOF_ADDR_IFREQ */ - -/* - * Enumerate the system's network interface addresses and call the callback - * for each one. Returns 0 if successful, -1 if trouble. - * - * This version uses ioctl(SIOCGIFCONF). - */ -int -pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) -{ - struct ifconf ifc; - struct ifreq *ifr, - *end, - addr, - mask; - char *ptr, - *buffer = NULL; - size_t n_buffer = 1024; - pgsocket sock; - - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock == PGINVALID_SOCKET) - return -1; - - while (n_buffer < 1024 * 100) - { - n_buffer += 1024; - ptr = realloc(buffer, n_buffer); - if (!ptr) - { - free(buffer); - close(sock); - errno = ENOMEM; - return -1; - } - - memset(&ifc, 0, sizeof(ifc)); - ifc.ifc_buf = buffer = ptr; - ifc.ifc_len = n_buffer; - - if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) - { - if (errno == EINVAL) - continue; - free(buffer); - close(sock); - return -1; - } - - /* - * Some Unixes try to return as much data as possible, with no - * indication of whether enough space allocated. Don't believe we have - * it all unless there's lots of slop. - */ - if (ifc.ifc_len < n_buffer - 1024) - break; - } - - end = (struct ifreq *) (buffer + ifc.ifc_len); - for (ifr = ifc.ifc_req; ifr < end;) - { - memcpy(&addr, ifr, sizeof(addr)); - memcpy(&mask, ifr, sizeof(mask)); - if (ioctl(sock, SIOCGIFADDR, &addr, sizeof(addr)) == 0 && - ioctl(sock, SIOCGIFNETMASK, &mask, sizeof(mask)) == 0) - run_ifaddr_callback(callback, cb_data, - &addr.ifr_addr, &mask.ifr_addr); - ifr = (struct ifreq *) ((char *) ifr + _SIZEOF_ADDR_IFREQ(*ifr)); - } - - free(buffer); - close(sock); - return 0; -} -#else /* !defined(SIOCGIFCONF) */ - -/* - * Enumerate the system's network interface addresses and call the callback - * for each one. Returns 0 if successful, -1 if trouble. - * - * This version is our fallback if there's no known way to get the - * interface addresses. Just return the standard loopback addresses. - */ -int -pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) -{ - struct sockaddr_in addr; - struct sockaddr_storage mask; - -#ifdef HAVE_IPV6 - struct sockaddr_in6 addr6; -#endif - - /* addr 127.0.0.1/8 */ - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = ntohl(0x7f000001); - memset(&mask, 0, sizeof(mask)); - pg_sockaddr_cidr_mask(&mask, "8", AF_INET); - run_ifaddr_callback(callback, cb_data, - (struct sockaddr *) & addr, - (struct sockaddr *) & mask); - -#ifdef HAVE_IPV6 - /* addr ::1/128 */ - memset(&addr6, 0, sizeof(addr6)); - addr6.sin6_family = AF_INET6; - addr6.sin6_addr.s6_addr[15] = 1; - memset(&mask, 0, sizeof(mask)); - pg_sockaddr_cidr_mask(&mask, "128", AF_INET6); - run_ifaddr_callback(callback, cb_data, - (struct sockaddr *) & addr6, - (struct sockaddr *) & mask); -#endif - - return 0; -} -#endif /* !defined(SIOCGIFCONF) */ - -#endif /* !HAVE_GETIFADDRS */ diff --git a/src/backend/libpq/md5.c b/src/backend/libpq/md5.c deleted file mode 100644 index 5af54e6c561..00000000000 --- a/src/backend/libpq/md5.c +++ /dev/null @@ -1,345 +0,0 @@ -/* - * md5.c - * - * Implements the MD5 Message-Digest Algorithm as specified in - * RFC 1321. This implementation is a simple one, in that it - * needs every input byte to be buffered before doing any - * calculations. I do not expect this file to be used for - * general purpose MD5'ing of large amounts of data, only for - * generating hashed passwords from limited input. - * - * Sverre H. Huseby - * - * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group - * Portions Copyright (c) 1994, Regents of the University of California - * - * IDENTIFICATION - * src/backend/libpq/md5.c - */ - -/* This is intended to be used in both frontend and backend, so use c.h */ -#include "c.h" - -#include "libpq/md5.h" - - -/* - * PRIVATE FUNCTIONS - */ - - -/* - * The returned array is allocated using malloc. the caller should free it - * when it is no longer needed. - */ -static uint8 * -createPaddedCopyWithLength(const uint8 *b, uint32 *l) -{ - uint8 *ret; - uint32 q; - uint32 len, - newLen448; - uint32 len_high, - len_low; /* 64-bit value split into 32-bit sections */ - - len = ((b == NULL) ? 0 : *l); - newLen448 = len + 64 - (len % 64) - 8; - if (newLen448 <= len) - newLen448 += 64; - - *l = newLen448 + 8; - if ((ret = (uint8 *) malloc(sizeof(uint8) * *l)) == NULL) - return NULL; - - if (b != NULL) - memcpy(ret, b, sizeof(uint8) * len); - - /* pad */ - ret[len] = 0x80; - for (q = len + 1; q < newLen448; q++) - ret[q] = 0x00; - - /* append length as a 64 bit bitcount */ - len_low = len; - /* split into two 32-bit values */ - /* we only look at the bottom 32-bits */ - len_high = len >> 29; - len_low <<= 3; - q = newLen448; - ret[q++] = (len_low & 0xff); - len_low >>= 8; - ret[q++] = (len_low & 0xff); - len_low >>= 8; - ret[q++] = (len_low & 0xff); - len_low >>= 8; - ret[q++] = (len_low & 0xff); - ret[q++] = (len_high & 0xff); - len_high >>= 8; - ret[q++] = (len_high & 0xff); - len_high >>= 8; - ret[q++] = (len_high & 0xff); - len_high >>= 8; - ret[q] = (len_high & 0xff); - - return ret; -} - -#define F(x, y, z) (((x) & (y)) | (~(x) & (z))) -#define G(x, y, z) (((x) & (z)) | ((y) & ~(z))) -#define H(x, y, z) ((x) ^ (y) ^ (z)) -#define I(x, y, z) ((y) ^ ((x) | ~(z))) -#define ROT_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) - -static void -doTheRounds(uint32 X[16], uint32 state[4]) -{ - uint32 a, - b, - c, - d; - - a = state[0]; - b = state[1]; - c = state[2]; - d = state[3]; - - /* round 1 */ - a = b + ROT_LEFT((a + F(b, c, d) + X[0] + 0xd76aa478), 7); /* 1 */ - d = a + ROT_LEFT((d + F(a, b, c) + X[1] + 0xe8c7b756), 12); /* 2 */ - c = d + ROT_LEFT((c + F(d, a, b) + X[2] + 0x242070db), 17); /* 3 */ - b = c + ROT_LEFT((b + F(c, d, a) + X[3] + 0xc1bdceee), 22); /* 4 */ - a = b + ROT_LEFT((a + F(b, c, d) + X[4] + 0xf57c0faf), 7); /* 5 */ - d = a + ROT_LEFT((d + F(a, b, c) + X[5] + 0x4787c62a), 12); /* 6 */ - c = d + ROT_LEFT((c + F(d, a, b) + X[6] + 0xa8304613), 17); /* 7 */ - b = c + ROT_LEFT((b + F(c, d, a) + X[7] + 0xfd469501), 22); /* 8 */ - a = b + ROT_LEFT((a + F(b, c, d) + X[8] + 0x698098d8), 7); /* 9 */ - d = a + ROT_LEFT((d + F(a, b, c) + X[9] + 0x8b44f7af), 12); /* 10 */ - c = d + ROT_LEFT((c + F(d, a, b) + X[10] + 0xffff5bb1), 17); /* 11 */ - b = c + ROT_LEFT((b + F(c, d, a) + X[11] + 0x895cd7be), 22); /* 12 */ - a = b + ROT_LEFT((a + F(b, c, d) + X[12] + 0x6b901122), 7); /* 13 */ - d = a + ROT_LEFT((d + F(a, b, c) + X[13] + 0xfd987193), 12); /* 14 */ - c = d + ROT_LEFT((c + F(d, a, b) + X[14] + 0xa679438e), 17); /* 15 */ - b = c + ROT_LEFT((b + F(c, d, a) + X[15] + 0x49b40821), 22); /* 16 */ - - /* round 2 */ - a = b + ROT_LEFT((a + G(b, c, d) + X[1] + 0xf61e2562), 5); /* 17 */ - d = a + ROT_LEFT((d + G(a, b, c) + X[6] + 0xc040b340), 9); /* 18 */ - c = d + ROT_LEFT((c + G(d, a, b) + X[11] + 0x265e5a51), 14); /* 19 */ - b = c + ROT_LEFT((b + G(c, d, a) + X[0] + 0xe9b6c7aa), 20); /* 20 */ - a = b + ROT_LEFT((a + G(b, c, d) + X[5] + 0xd62f105d), 5); /* 21 */ - d = a + ROT_LEFT((d + G(a, b, c) + X[10] + 0x02441453), 9); /* 22 */ - c = d + ROT_LEFT((c + G(d, a, b) + X[15] + 0xd8a1e681), 14); /* 23 */ - b = c + ROT_LEFT((b + G(c, d, a) + X[4] + 0xe7d3fbc8), 20); /* 24 */ - a = b + ROT_LEFT((a + G(b, c, d) + X[9] + 0x21e1cde6), 5); /* 25 */ - d = a + ROT_LEFT((d + G(a, b, c) + X[14] + 0xc33707d6), 9); /* 26 */ - c = d + ROT_LEFT((c + G(d, a, b) + X[3] + 0xf4d50d87), 14); /* 27 */ - b = c + ROT_LEFT((b + G(c, d, a) + X[8] + 0x455a14ed), 20); /* 28 */ - a = b + ROT_LEFT((a + G(b, c, d) + X[13] + 0xa9e3e905), 5); /* 29 */ - d = a + ROT_LEFT((d + G(a, b, c) + X[2] + 0xfcefa3f8), 9); /* 30 */ - c = d + ROT_LEFT((c + G(d, a, b) + X[7] + 0x676f02d9), 14); /* 31 */ - b = c + ROT_LEFT((b + G(c, d, a) + X[12] + 0x8d2a4c8a), 20); /* 32 */ - - /* round 3 */ - a = b + ROT_LEFT((a + H(b, c, d) + X[5] + 0xfffa3942), 4); /* 33 */ - d = a + ROT_LEFT((d + H(a, b, c) + X[8] + 0x8771f681), 11); /* 34 */ - c = d + ROT_LEFT((c + H(d, a, b) + X[11] + 0x6d9d6122), 16); /* 35 */ - b = c + ROT_LEFT((b + H(c, d, a) + X[14] + 0xfde5380c), 23); /* 36 */ - a = b + ROT_LEFT((a + H(b, c, d) + X[1] + 0xa4beea44), 4); /* 37 */ - d = a + ROT_LEFT((d + H(a, b, c) + X[4] + 0x4bdecfa9), 11); /* 38 */ - c = d + ROT_LEFT((c + H(d, a, b) + X[7] + 0xf6bb4b60), 16); /* 39 */ - b = c + ROT_LEFT((b + H(c, d, a) + X[10] + 0xbebfbc70), 23); /* 40 */ - a = b + ROT_LEFT((a + H(b, c, d) + X[13] + 0x289b7ec6), 4); /* 41 */ - d = a + ROT_LEFT((d + H(a, b, c) + X[0] + 0xeaa127fa), 11); /* 42 */ - c = d + ROT_LEFT((c + H(d, a, b) + X[3] + 0xd4ef3085), 16); /* 43 */ - b = c + ROT_LEFT((b + H(c, d, a) + X[6] + 0x04881d05), 23); /* 44 */ - a = b + ROT_LEFT((a + H(b, c, d) + X[9] + 0xd9d4d039), 4); /* 45 */ - d = a + ROT_LEFT((d + H(a, b, c) + X[12] + 0xe6db99e5), 11); /* 46 */ - c = d + ROT_LEFT((c + H(d, a, b) + X[15] + 0x1fa27cf8), 16); /* 47 */ - b = c + ROT_LEFT((b + H(c, d, a) + X[2] + 0xc4ac5665), 23); /* 48 */ - - /* round 4 */ - a = b + ROT_LEFT((a + I(b, c, d) + X[0] + 0xf4292244), 6); /* 49 */ - d = a + ROT_LEFT((d + I(a, b, c) + X[7] + 0x432aff97), 10); /* 50 */ - c = d + ROT_LEFT((c + I(d, a, b) + X[14] + 0xab9423a7), 15); /* 51 */ - b = c + ROT_LEFT((b + I(c, d, a) + X[5] + 0xfc93a039), 21); /* 52 */ - a = b + ROT_LEFT((a + I(b, c, d) + X[12] + 0x655b59c3), 6); /* 53 */ - d = a + ROT_LEFT((d + I(a, b, c) + X[3] + 0x8f0ccc92), 10); /* 54 */ - c = d + ROT_LEFT((c + I(d, a, b) + X[10] + 0xffeff47d), 15); /* 55 */ - b = c + ROT_LEFT((b + I(c, d, a) + X[1] + 0x85845dd1), 21); /* 56 */ - a = b + ROT_LEFT((a + I(b, c, d) + X[8] + 0x6fa87e4f), 6); /* 57 */ - d = a + ROT_LEFT((d + I(a, b, c) + X[15] + 0xfe2ce6e0), 10); /* 58 */ - c = d + ROT_LEFT((c + I(d, a, b) + X[6] + 0xa3014314), 15); /* 59 */ - b = c + ROT_LEFT((b + I(c, d, a) + X[13] + 0x4e0811a1), 21); /* 60 */ - a = b + ROT_LEFT((a + I(b, c, d) + X[4] + 0xf7537e82), 6); /* 61 */ - d = a + ROT_LEFT((d + I(a, b, c) + X[11] + 0xbd3af235), 10); /* 62 */ - c = d + ROT_LEFT((c + I(d, a, b) + X[2] + 0x2ad7d2bb), 15); /* 63 */ - b = c + ROT_LEFT((b + I(c, d, a) + X[9] + 0xeb86d391), 21); /* 64 */ - - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; -} - -static int -calculateDigestFromBuffer(const uint8 *b, uint32 len, uint8 sum[16]) -{ - register uint32 i, - j, - k, - newI; - uint32 l; - uint8 *input; - register uint32 *wbp; - uint32 workBuff[16], - state[4]; - - l = len; - - state[0] = 0x67452301; - state[1] = 0xEFCDAB89; - state[2] = 0x98BADCFE; - state[3] = 0x10325476; - - if ((input = createPaddedCopyWithLength(b, &l)) == NULL) - return 0; - - for (i = 0;;) - { - if ((newI = i + 16 * 4) > l) - break; - k = i + 3; - for (j = 0; j < 16; j++) - { - wbp = (workBuff + j); - *wbp = input[k--]; - *wbp <<= 8; - *wbp |= input[k--]; - *wbp <<= 8; - *wbp |= input[k--]; - *wbp <<= 8; - *wbp |= input[k]; - k += 7; - } - doTheRounds(workBuff, state); - i = newI; - } - free(input); - - j = 0; - for (i = 0; i < 4; i++) - { - k = state[i]; - sum[j++] = (k & 0xff); - k >>= 8; - sum[j++] = (k & 0xff); - k >>= 8; - sum[j++] = (k & 0xff); - k >>= 8; - sum[j++] = (k & 0xff); - } - return 1; -} - -static void -bytesToHex(uint8 b[16], char *s) -{ - static const char *hex = "0123456789abcdef"; - int q, - w; - - for (q = 0, w = 0; q < 16; q++) - { - s[w++] = hex[(b[q] >> 4) & 0x0F]; - s[w++] = hex[b[q] & 0x0F]; - } - s[w] = '\0'; -} - -/* - * PUBLIC FUNCTIONS - */ - -/* - * pg_md5_hash - * - * Calculates the MD5 sum of the bytes in a buffer. - * - * SYNOPSIS #include "md5.h" - * int pg_md5_hash(const void *buff, size_t len, char *hexsum) - * - * INPUT buff the buffer containing the bytes that you want - * the MD5 sum of. - * len number of bytes in the buffer. - * - * OUTPUT hexsum the MD5 sum as a '\0'-terminated string of - * hexadecimal digits. an MD5 sum is 16 bytes long. - * each byte is represented by two heaxadecimal - * characters. you thus need to provide an array - * of 33 characters, including the trailing '\0'. - * - * RETURNS false on failure (out of memory for internal buffers) or - * true on success. - * - * STANDARDS MD5 is described in RFC 1321. - * - * AUTHOR Sverre H. Huseby - * - */ -bool -pg_md5_hash(const void *buff, size_t len, char *hexsum) -{ - uint8 sum[16]; - - if (!calculateDigestFromBuffer(buff, len, sum)) - return false; - - bytesToHex(sum, hexsum); - return true; -} - -bool -pg_md5_binary(const void *buff, size_t len, void *outbuf) -{ - if (!calculateDigestFromBuffer(buff, len, outbuf)) - return false; - return true; -} - - -/* - * Computes MD5 checksum of "passwd" (a null-terminated string) followed - * by "salt" (which need not be null-terminated). - * - * Output format is "md5" followed by a 32-hex-digit MD5 checksum. - * Hence, the output buffer "buf" must be at least 36 bytes long. - * - * Returns TRUE if okay, FALSE on error (out of memory). - */ -bool -pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len, - char *buf) -{ - size_t passwd_len = strlen(passwd); - - /* +1 here is just to avoid risk of unportable malloc(0) */ - char *crypt_buf = malloc(passwd_len + salt_len + 1); - bool ret; - - if (!crypt_buf) - return false; - - /* - * Place salt at the end because it may be known by users trying to crack - * the MD5 output. - */ - memcpy(crypt_buf, passwd, passwd_len); - memcpy(crypt_buf + passwd_len, salt, salt_len); - - strcpy(buf, "md5"); - ret = pg_md5_hash(crypt_buf, passwd_len + salt_len, buf + 3); - - free(crypt_buf); - - return ret; -} diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c index 90b6946b386..bae96bf18fb 100644 --- a/src/backend/libpq/pqcomm.c +++ b/src/backend/libpq/pqcomm.c @@ -89,7 +89,7 @@ #include #endif -#include "libpq/ip.h" +#include "common/ip.h" #include "libpq/libpq.h" #include "miscadmin.h" #include "storage/ipc.h" diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 2f99aea7912..8a2ce913443 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -38,7 +38,7 @@ #include "access/xact.h" #include "catalog/pg_database.h" #include "catalog/pg_proc.h" -#include "libpq/ip.h" +#include "common/ip.h" #include "libpq/libpq.h" #include "libpq/pqsignal.h" #include "mb/pg_wchar.h" diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index a28e215e2d7..eaf3f61ecf4 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -99,9 +99,9 @@ #include "access/xlog.h" #include "bootstrap/bootstrap.h" #include "catalog/pg_control.h" +#include "common/ip.h" #include "lib/ilist.h" #include "libpq/auth.h" -#include "libpq/ip.h" #include "libpq/libpq.h" #include "libpq/pqsignal.h" #include "miscadmin.h" diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c index 3f6987af048..dbc557e5838 100644 --- a/src/backend/utils/adt/network.c +++ b/src/backend/utils/adt/network.c @@ -14,7 +14,7 @@ #include "access/hash.h" #include "catalog/pg_type.h" -#include "libpq/ip.h" +#include "common/ip.h" #include "libpq/libpq-be.h" #include "libpq/pqformat.h" #include "miscadmin.h" diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index 1bba5fa8c81..5d1ccf51d41 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -16,8 +16,8 @@ #include "access/htup_details.h" #include "catalog/pg_type.h" +#include "common/ip.h" #include "funcapi.h" -#include "libpq/ip.h" #include "miscadmin.h" #include "pgstat.h" #include "storage/proc.h" diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index bf7c0cd7356..582d3e460b2 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -21,8 +21,8 @@ #include "access/tuptoaster.h" #include "catalog/pg_collation.h" #include "catalog/pg_type.h" +#include "common/md5.h" #include "lib/hyperloglog.h" -#include "libpq/md5.h" #include "libpq/pqformat.h" #include "miscadmin.h" #include "parser/scansup.h" -- cgit v1.2.3