-/* Copyright (C) 1997-2002, 2003, 2005, 2006 Free Software Foundation, Inc.
+/* Copyright (C) 1997-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
- License along with the GNU C Library; if not, write to the Free
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA. */
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+#include <assert.h>
#include <atomic.h>
#include <ctype.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <rpcsvc/nis.h>
-#include <bits/libc-lock.h>
+#include <libc-lock.h>
#include "nss-nisplus.h"
static u_long tablename_len;
#define NISENTRYVAL(idx, col, res) \
- (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
+ (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
#define NISENTRYLEN(idx, col, res) \
- (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
-
-/* Get implementation for some internal functions. */
-#include <resolv/mapv4v6addr.h>
-
+ (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
static int
_nss_nisplus_parse_hostent (nis_result *result, int af, struct hostent *host,
- char *buffer, size_t buflen, int *errnop,
- int flags)
+ char *buffer, size_t buflen, int *errnop)
{
unsigned int i;
char *first_unused = buffer;
if (result == NULL)
return 0;
- if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
- __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ ||
- strcmp(NIS_RES_OBJECT (result)[0].EN_data.en_type, "hosts_tbl") != 0 ||
- NIS_RES_OBJECT (result)[0].EN_data.en_cols.en_cols_len < 4)
+ if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
+ || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
+ || strcmp (NIS_RES_OBJECT (result)[0].EN_data.en_type, "hosts_tbl") != 0
+ || NIS_RES_OBJECT (result)[0].EN_data.en_cols.en_cols_len < 4)
return 0;
char *data = first_unused;
- if (room_left < (af == AF_INET6 || (flags & AI_V4MAPPED) != 0
- ? IN6ADDRSZ : INADDRSZ))
+ if (room_left < (af != AF_INET ? IN6ADDRSZ : INADDRSZ))
{
no_more_room:
*errnop = ERANGE;
}
/* Parse address. */
- if (af == AF_INET && inet_pton (af, NISENTRYVAL (0, 2, result), data) > 0)
+ if (af != AF_INET6
+ && inet_pton (AF_INET, NISENTRYVAL (0, 2, result), data) > 0)
{
- if (flags & AI_V4MAPPED)
- {
- map_v4v6_address (data, data);
- host->h_addrtype = AF_INET6;
- host->h_length = IN6ADDRSZ;
- }
- else
- {
- host->h_addrtype = AF_INET;
- host->h_length = INADDRSZ;
- }
+ host->h_addrtype = AF_INET;
+ host->h_length = INADDRSZ;
}
- else if (af == AF_INET6
+ else if (af != AF_INET
&& inet_pton (AF_INET6, NISENTRYVAL (0, 2, result), data) > 0)
{
host->h_addrtype = AF_INET6;
first_unused = __stpncpy (first_unused, NISENTRYVAL (0, 0, result),
NISENTRYLEN (0, 0, result));
*first_unused++ = '\0';
- room_left -= NISENTRYLEN (0, 0, result) + 1;
- /* XXX Rewrite at some point to allocate the array first and then
- copy the strings. It wasteful to first concatenate the strings
- to just split them again later. */
+ room_left -= NISENTRYLEN (0, 0, result) + 1;
char *line = first_unused;
- for (i = 0; i < NIS_RES_NUMOBJ (result); ++i)
+
+ /* When this is a call to gethostbyname4_r we do not need the aliases. */
+ if (af != AF_UNSPEC)
{
- if (strcmp (NISENTRYVAL (i, 1, result), host->h_name) != 0)
+ /* XXX Rewrite at some point to allocate the array first and then
+ copy the strings. It is wasteful to first concatenate the strings
+ to just split them again later. */
+ for (i = 0; i < NIS_RES_NUMOBJ (result); ++i)
{
- if (NISENTRYLEN (i, 1, result) + 2 > room_left)
- goto no_more_room;
-
- *first_unused++ = ' ';
- first_unused = __stpncpy (first_unused, NISENTRYVAL (i, 1, result),
- NISENTRYLEN (i, 1, result));
- *first_unused = '\0';
- room_left -= NISENTRYLEN (i, 1, result) + 1;
+ if (strcmp (NISENTRYVAL (i, 1, result), host->h_name) != 0)
+ {
+ if (NISENTRYLEN (i, 1, result) + 2 > room_left)
+ goto no_more_room;
+
+ *first_unused++ = ' ';
+ first_unused = __stpncpy (first_unused,
+ NISENTRYVAL (i, 1, result),
+ NISENTRYLEN (i, 1, result));
+ *first_unused = '\0';
+ room_left -= NISENTRYLEN (i, 1, result) + 1;
+ }
}
+ *first_unused++ = '\0';
}
- *first_unused++ = '\0';
/* Adjust the pointer so it is aligned for
storing pointers. */
host->h_addr_list[1] = NULL;
host->h_aliases = &host->h_addr_list[2];
- i = 0;
- while (*line != '\0')
+ /* When this is a call to gethostbyname4_r we do not need the aliases. */
+ if (af != AF_UNSPEC)
{
- /* Skip leading blanks. */
- while (isspace (*line))
- ++line;
+ i = 0;
+ while (*line != '\0')
+ {
+ /* Skip leading blanks. */
+ while (isspace (*line))
+ ++line;
- if (*line == '\0')
- break;
+ if (*line == '\0')
+ break;
- if (room_left < sizeof (char *))
- goto no_more_room;
+ if (room_left < sizeof (char *))
+ goto no_more_room;
- room_left -= sizeof (char *);
- host->h_aliases[i++] = line;
+ room_left -= sizeof (char *);
+ host->h_aliases[i++] = line;
- while (*line != '\0' && *line != ' ')
- ++line;
+ while (*line != '\0' && *line != ' ')
+ ++line;
- if (*line == ' ')
- *line++ = '\0';
- }
+ if (*line == ' ')
+ *line++ = '\0';
+ }
- host->h_aliases[i] = NULL;
+ host->h_aliases[i] = NULL;
+ }
return 1;
}
return NSS_STATUS_SUCCESS;
}
+
enum nss_status
_nss_nisplus_sethostent (int stayopen)
{
return status;
}
+
enum nss_status
_nss_nisplus_endhostent (void)
{
return NSS_STATUS_SUCCESS;
}
+
static enum nss_status
internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
size_t buflen, int *errnop, int *herrnop)
return NSS_STATUS_TRYAGAIN;
}
if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
- {
- enum nss_status retval = niserr2nss (result->status);
- if (retval == NSS_STATUS_TRYAGAIN)
- {
- *herrnop = NETDB_INTERNAL;
- *errnop = errno;
- }
- return retval;
- }
+ {
+ enum nss_status retval = niserr2nss (result->status);
+ if (retval == NSS_STATUS_TRYAGAIN)
+ {
+ *herrnop = NETDB_INTERNAL;
+ *errnop = errno;
+ }
+ return retval;
+ }
}
else
return NSS_STATUS_TRYAGAIN;
}
if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
- {
- enum nss_status retval= niserr2nss (result->status);
+ {
+ enum nss_status retval= niserr2nss (result->status);
nis_freeresult (result);
result = saved_res;
- if (retval == NSS_STATUS_TRYAGAIN)
- {
- *herrnop = NETDB_INTERNAL;
+ if (retval == NSS_STATUS_TRYAGAIN)
+ {
+ *herrnop = NETDB_INTERNAL;
*errnop = errno;
- }
- return retval;
- }
+ }
+ return retval;
+ }
}
- if (_res.options & RES_USE_INET6)
- parse_res = _nss_nisplus_parse_hostent (result, AF_INET6, host, buffer,
- buflen, errnop, AI_V4MAPPED);
- else
- parse_res = _nss_nisplus_parse_hostent (result, AF_INET, host, buffer,
- buflen, errnop, 0);
+ parse_res = _nss_nisplus_parse_hostent (result, AF_INET, host, buffer,
+ buflen, errnop);
if (parse_res == -1)
- {
+ {
nis_freeresult (result);
result = saved_res;
- *herrnop = NETDB_INTERNAL;
+ *herrnop = NETDB_INTERNAL;
*errnop = ERANGE;
- return NSS_STATUS_TRYAGAIN;
- }
+ return NSS_STATUS_TRYAGAIN;
+ }
if (saved_res != NULL)
nis_freeresult (saved_res);
return NSS_STATUS_SUCCESS;
}
+
enum nss_status
_nss_nisplus_gethostent_r (struct hostent *result, char *buffer,
size_t buflen, int *errnop, int *herrnop)
return status;
}
+
static enum nss_status
-internal_gethostbyname2_r (const char *name, int af, struct hostent *host,
- char *buffer, size_t buflen, int *errnop,
- int *herrnop, int flags)
+get_tablename (int *herrnop)
{
- int parse_res, retval;
+ __libc_lock_lock (lock);
- if (tablename_val == NULL)
- {
- __libc_lock_lock (lock);
+ enum nss_status status = _nss_create_tablename (herrnop);
+
+ __libc_lock_unlock (lock);
- enum nss_status status = _nss_create_tablename (errnop);
+ if (status != NSS_STATUS_SUCCESS)
+ *herrnop = NETDB_INTERNAL;
+
+ return status;
+}
- __libc_lock_unlock (lock);
+static enum nss_status
+internal_gethostbyname2_r (const char *name, int af, struct hostent *host,
+ char *buffer, size_t buflen, int *errnop,
+ int *herrnop)
+{
+ if (tablename_val == NULL)
+ {
+ enum nss_status status = get_tablename (herrnop);
if (status != NSS_STATUS_SUCCESS)
- {
- *herrnop = NETDB_INTERNAL;
- return NSS_STATUS_UNAVAIL;
- }
+ return status;
}
if (name == NULL)
return NSS_STATUS_NOTFOUND;
}
- nis_result *result;
char buf[strlen (name) + 10 + tablename_len];
int olderr = errno;
/* Search at first in the alias list, and use the correct name
for the next search. */
snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
- result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
+ nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
if (result != NULL)
{
- char *bufptr = buf;
-
/* If we did not find it, try it as original name. But if the
database is correct, we should find it in the first case, too. */
- if ((result->status != NIS_SUCCESS
- && result->status != NIS_S_SUCCESS)
- || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
- || strcmp (result->objects.objects_val->EN_data.en_type,
- "hosts_tbl") != 0
- || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
- snprintf (buf, sizeof (buf), "[cname=%s],%s", name, tablename_val);
- else
+ char *bufptr = buf;
+ size_t buflen = sizeof (buf);
+
+ if ((result->status == NIS_SUCCESS || result->status == NIS_S_SUCCESS)
+ && __type_of (result->objects.objects_val) == NIS_ENTRY_OBJ
+ && strcmp (result->objects.objects_val->EN_data.en_type,
+ "hosts_tbl") == 0
+ && result->objects.objects_val->EN_data.en_cols.en_cols_len >= 3)
{
/* We need to allocate a new buffer since there is no
- guarantee the returned name has a length limit. */
- const char *entryval = NISENTRYVAL(0, 0, result);
- size_t buflen = strlen (entryval) + 10 + tablename_len;
+ guarantee the returned alias name has a length limit. */
+ name = NISENTRYVAL(0, 0, result);
+ size_t buflen = strlen (name) + 10 + tablename_len;
bufptr = alloca (buflen);
- snprintf (bufptr, buflen, "[cname=%s],%s",
- entryval, tablename_val);
}
+ snprintf (bufptr, buflen, "[cname=%s],%s", name, tablename_val);
+
nis_freeresult (result);
result = nis_list (bufptr, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
}
if (result == NULL)
{
*errnop = ENOMEM;
+ *herrnop = NETDB_INTERNAL;
return NSS_STATUS_TRYAGAIN;
}
- retval = niserr2nss (result->status);
- if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
+ int retval = niserr2nss (result->status);
+ if (__glibc_unlikely (retval != NSS_STATUS_SUCCESS))
{
if (retval == NSS_STATUS_TRYAGAIN)
{
*errnop = errno;
- *herrnop = NETDB_INTERNAL;
+ *herrnop = TRY_AGAIN;
}
else
- __set_errno (olderr);
+ {
+ __set_errno (olderr);
+ *herrnop = NETDB_INTERNAL;
+ }
nis_freeresult (result);
return retval;
}
- parse_res = _nss_nisplus_parse_hostent (result, af, host, buffer,
- buflen, errnop, flags);
+ int parse_res = _nss_nisplus_parse_hostent (result, af, host, buffer,
+ buflen, errnop);
nis_freeresult (result);
return NSS_STATUS_NOTFOUND;
}
+
enum nss_status
_nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
char *buffer, size_t buflen, int *errnop,
int *herrnop)
{
+ if (af != AF_INET && af != AF_INET6)
+ {
+ *herrnop = HOST_NOT_FOUND;
+ return NSS_STATUS_NOTFOUND;
+ }
+
return internal_gethostbyname2_r (name, af, host, buffer, buflen, errnop,
- herrnop,
- ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0));
+ herrnop);
}
+
enum nss_status
_nss_nisplus_gethostbyname_r (const char *name, struct hostent *host,
char *buffer, size_t buflen, int *errnop,
int *h_errnop)
{
- if (_res.options & RES_USE_INET6)
- {
- enum nss_status status;
-
- status = internal_gethostbyname2_r (name, AF_INET6, host, buffer,
- buflen, errnop, h_errnop,
- AI_V4MAPPED);
- if (status == NSS_STATUS_SUCCESS)
- return status;
- }
-
return internal_gethostbyname2_r (name, AF_INET, host, buffer,
- buflen, errnop, h_errnop, 0);
+ buflen, errnop, h_errnop);
}
+
enum nss_status
_nss_nisplus_gethostbyaddr_r (const void *addr, socklen_t addrlen, int af,
struct hostent *host, char *buffer,
{
if (tablename_val == NULL)
{
- __libc_lock_lock (lock);
-
- enum nss_status status = _nss_create_tablename (errnop);
-
- __libc_lock_unlock (lock);
-
+ enum nss_status status = get_tablename (herrnop);
if (status != NSS_STATUS_SUCCESS)
return status;
}
}
retval = niserr2nss (result->status);
- if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
+ if (__glibc_unlikely (retval != NSS_STATUS_SUCCESS))
{
if (retval == NSS_STATUS_TRYAGAIN)
{
}
parse_res = _nss_nisplus_parse_hostent (result, af, host,
- buffer, buflen, errnop,
- ((_res.options & RES_USE_INET6)
- ? AI_V4MAPPED : 0));
+ buffer, buflen, errnop);
nis_freeresult (result);
if (parse_res > 0)
__set_errno (olderr);
return NSS_STATUS_NOTFOUND;
}
+
+
+enum nss_status
+_nss_nisplus_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
+ char *buffer, size_t buflen, int *errnop,
+ int *herrnop, int32_t *ttlp)
+{
+ struct hostent host;
+
+ enum nss_status status = internal_gethostbyname2_r (name, AF_UNSPEC, &host,
+ buffer, buflen,
+ errnop, herrnop);
+ if (__glibc_likely (status == NSS_STATUS_SUCCESS))
+ {
+ if (*pat == NULL)
+ {
+ uintptr_t pad = (-(uintptr_t) buffer
+ % __alignof__ (struct gaih_addrtuple));
+ buffer += pad;
+ buflen = buflen > pad ? buflen - pad : 0;
+
+ if (__glibc_unlikely (buflen < sizeof (struct gaih_addrtuple)))
+ {
+ free (result);
+ *errnop = ERANGE;
+ *herrnop = NETDB_INTERNAL;
+ return NSS_STATUS_TRYAGAIN;
+ }
+ }
+
+ (*pat)->next = NULL;
+ (*pat)->name = host.h_name;
+ (*pat)->family = host.h_addrtype;
+
+ memcpy ((*pat)->addr, host.h_addr_list[0], host.h_length);
+ (*pat)->scopeid = 0;
+ assert (host.h_addr_list[1] == NULL);
+ }
+
+ return status;
+}