about summary refs log tree commit diff
path: root/hesiod
diff options
context:
space:
mode:
Diffstat (limited to 'hesiod')
-rw-r--r--hesiod/Makefile7
-rw-r--r--hesiod/README.hesiod15
-rw-r--r--hesiod/Versions3
-rw-r--r--hesiod/hesiod.c830
-rw-r--r--hesiod/hesiod.h77
-rw-r--r--hesiod/hesiod_p.h33
-rw-r--r--hesiod/nss_hesiod/hesiod-grp.c79
-rw-r--r--hesiod/nss_hesiod/hesiod-init.c39
-rw-r--r--hesiod/nss_hesiod/hesiod-proto.c137
-rw-r--r--hesiod/nss_hesiod/hesiod-pwd.c78
-rw-r--r--hesiod/nss_hesiod/hesiod-service.c95
-rw-r--r--hesiod/nss_hesiod/nss_hesiod.h21
12 files changed, 747 insertions, 667 deletions
diff --git a/hesiod/Makefile b/hesiod/Makefile
index f1356bbfbd..0dea8fa362 100644
--- a/hesiod/Makefile
+++ b/hesiod/Makefile
@@ -1,4 +1,4 @@
-# Copyright (C) 1997, 1998 Free Software Foundation, Inc.
+# Copyright (C) 1997, 1998, 2000 Free Software Foundation, Inc.
 # This file is part of the GNU C Library.
 
 # The GNU C Library is free software; you can redistribute it and/or
@@ -29,14 +29,13 @@ extra-libs-others = $(extra-libs)
 subdir-dirs = nss_hesiod
 vpath %.c nss_hesiod
 
-libnss_hesiod-routines	:= hesiod hesiod-grp hesiod-pwd hesiod-service
+libnss_hesiod-routines	:= hesiod hesiod-grp hesiod-init hesiod-proto \
+                           hesiod-pwd hesiod-service
 # Build only shared library
 libnss_hesiod-inhibit-o	= $(filter-out .os,$(object-suffixes))
 
 include ../Rules
 
-CFLAGS-hesiod.c = -DSYSCONFDIR='"$(sysconfdir)"'
-
 # Depend on libc.so so a DT_NEEDED is generated in the shared objects.
 # This ensures they will load libc.so for needed symbols if loaded by
 # a statically-linked program that hasn't already loaded it.
diff --git a/hesiod/README.hesiod b/hesiod/README.hesiod
index 335a73e603..18cd5d45dd 100644
--- a/hesiod/README.hesiod
+++ b/hesiod/README.hesiod
@@ -65,18 +65,15 @@ Configuring Hesiod
 Next, you will have to configure Hesiod.  If you are already running
 Hesiod in your network, you probably already have a file named
 `hesiod.conf' on your machines (probably as `/etc/hesiod.conf' or
-`/usr/local/etc/hesiod.conf').  The Hesiod NSS module expects this
-file to be found in the sysconfdir (`/usr/local/etc/hesiod.conf' by
-default, see the installation notes on how to change this) or in the
-location specified by the environment variable `HESIOD_CONFIG'.  If
-there is no configuration file you will want to create your own.  It
-should look something like:
+`/usr/local/etc/hesiod.conf').  The Hesiod NSS module looks for
+`/etc/hesiod.conf' by default.  If there is no configuration file you
+will want to create your own.  It should look something like:
 
   rhs=.your.domain
   lhs=.ns
 
 The value of rhs can be overridden by the environment variable
-HES_DOMAIN.
+`HES_DOMAIN'.
 
 Configuring your name servers
 -----------------------------
@@ -115,8 +112,8 @@ file `named.hesiod' containing data that looks something like:
   123.gid         CNAME   libc.group
   gnu.passwd      TXT     "gnu:*:4567:123:GNU:/home/gnu:/bin/bash"
   456.uid         CNAME   mark.passwd
-  nss.service     TXT     "nss;tcp;789;switch sw "
-  nss.service     TXT     "nss;udp;789;switch sw"
+  nss.service     TXT     "nss tcp 789 switch sw "
+  nss.service     TXT     "nss udp 789 switch sw"
 
 where `libc' is an example of a group, `gnu' an example of an user,
 and `nss' an example of a service.  Note that the format used to
diff --git a/hesiod/Versions b/hesiod/Versions
index 2634b4006b..c51dee9c50 100644
--- a/hesiod/Versions
+++ b/hesiod/Versions
@@ -9,5 +9,8 @@ libnss_hesiod {
   }
   GLIBC_2.2 {
     _nss_hesiod_initgroups;
+    _nss_hesiod_getservbyport_r;
+    _nss_hesiod_setprotoent; _nss_hesiod_endprotoent;
+    _nss_hesiod_getprotobyname_r; _nss_hesiod_getprotobynumber_r;
   }
 }
diff --git a/hesiod/hesiod.c b/hesiod/hesiod.c
index f89a29ac95..3a01fd076b 100644
--- a/hesiod/hesiod.c
+++ b/hesiod/hesiod.c
@@ -1,4 +1,9 @@
-/* Copyright (c) 1996 by Internet Software Consortium.
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char rcsid[] = "$BINDId: hesiod.c,v 1.21 2000/02/28 14:51:08 vixie Exp $";
+#endif
+
+/*
+ * Copyright (c) 1996,1999 by Internet Software Consortium.
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -14,469 +19,488 @@
  * SOFTWARE.
  */
 
-/* Copyright 1996 by the Massachusetts Institute of Technology.
- *
- * Permission to use, copy, modify, and distribute this
- * software and its documentation for any purpose and without
- * fee is hereby granted, provided that the above copyright
- * notice appear in all copies and that both that copyright
- * notice and this permission notice appear in supporting
- * documentation, and that the name of M.I.T. not be used in
- * advertising or publicity pertaining to distribution of the
- * software without specific, written prior permission.
- * M.I.T. makes no representations about the suitability of
- * this software for any purpose.  It is provided "as is"
- * without express or implied warranty.
+/*
+ * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>.
  */
 
-/* This file is part of the hesiod library.  It implements the core
- * portion of the hesiod resolver.
- *
- * This file is loosely based on an interim version of hesiod.c from
- * the BIND IRS library, which was in turn based on an earlier version
- * of this file.  Extensive changes have been made on each step of the
- * path.
+/*
+ * hesiod.c --- the core portion of the hesiod resolver.
  *
- * This implementation is not truly thread-safe at the moment because
- * it uses res_send() and accesses _res.
+ * This file is derived from the hesiod library from Project Athena;
+ * It has been extensively rewritten by Theodore Ts'o to have a more
+ * thread-safe interface.
  */
 
-#if defined(LIBC_SCCS) && !defined(lint)
-static const char rcsid[] = "$Id$";
-#endif
+/* Imports */
 
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <arpa/nameser.h>
+
 #include <errno.h>
 #include <netdb.h>
 #include <resolv.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
+
 #include "hesiod.h"
 #include "hesiod_p.h"
+#undef DEF_RHS
 
-/* A few operating systems don't define these. */
-#ifndef C_HS
-# define C_HS	4
-#endif
-#ifndef T_TXT
-# define T_TXT	16
-#endif
+#define _PATH_HESIOD_CONF "/etc/hesiod.conf"
 
-static int read_config_file (struct hesiod_p *ctx, const char *filename);
-static char **get_txt_records (struct hesiod_p *ctx, int class,
-			       const char *name);
-#ifdef _LIBC
-# define cistrcmp(s1, s2) strcasecmp (s1, s2)
-#else
-static int cistrcmp (const char *s1, const char *s2);
-#endif
+/* Forward */
+
+int		hesiod_init(void **context);
+void		hesiod_end(void *context);
+char *		hesiod_to_bind(void *context, const char *name,
+			       const char *type);
+char **		hesiod_resolve(void *context, const char *name,
+			       const char *type);
+void		hesiod_free_list(void *context, char **list);
 
-/* This function is called to initialize a hesiod_p. */
+static int	parse_config_file(struct hesiod_p *ctx, const char *filename);
+static char **	get_txt_records(struct hesiod_p *ctx, int class,
+				const char *name);
+static int	init(struct hesiod_p *ctx);
+
+/* Public */
+
+/*
+ * This function is called to initialize a hesiod_p.
+ */
 int
-hesiod_init (void **context)
-{
-  struct hesiod_p *ctx;
-  const char *p, *configname;
-
-  ctx = malloc (sizeof (struct hesiod_p));
-  if (ctx)
-    {
-      *context = ctx;
-      configname = __secure_getenv ("HESIOD_CONFIG");
-      if (!configname)
-	configname = SYSCONFDIR "/hesiod.conf";
-      if (read_config_file (ctx, configname) >= 0)
-	{
-	  /* The default rhs can be overridden by an environment variable. */
-	  p = __secure_getenv ("HES_DOMAIN");
-	  if (p)
-	    {
-	      if (ctx->rhs)
-		free (ctx->rhs);
-	      ctx->rhs = malloc (strlen (p) + 2);
-	      if (ctx->rhs)
-		{
-		  *ctx->rhs = '.';
-		  strcpy (ctx->rhs + 1, (*p == '.') ? p + 1 : p);
-		  return 0;
+hesiod_init(void **context) {
+	struct hesiod_p *ctx;
+	const char *configname;
+	char *cp;
+
+	ctx = malloc(sizeof(struct hesiod_p));
+	if (ctx == 0) {
+		__set_errno(ENOMEM);
+		return (-1);
+	}
+
+	ctx->LHS = NULL;
+	ctx->RHS = NULL;
+	ctx->res = NULL;
+
+	configname = __secure_getenv("HESIOD_CONFIG");
+	if (!configname)
+	  configname = _PATH_HESIOD_CONF;
+	if (parse_config_file(ctx, configname) < 0) {
+#ifdef DEF_RHS
+		/*
+		 * Use compiled in defaults.
+		 */
+		ctx->LHS = malloc(strlen(DEF_LHS)+1);
+		ctx->RHS = malloc(strlen(DEF_RHS)+1);
+		if (ctx->LHS == 0 || ctx->RHS == 0) {
+			__set_errno(ENOMEM);
+			goto cleanup;
 		}
-	      else
-		__set_errno (ENOMEM);
-	    }
-	  else
-	    return 0;
+		strcpy(ctx->LHS, DEF_LHS);
+		strcpy(ctx->RHS, DEF_RHS);
+#else
+		goto cleanup;
+#endif
 	}
-    }
-  else
-    __set_errno (ENOMEM);
-
-  if (ctx->lhs)
-    free (ctx->lhs);
-  if (ctx->rhs)
-    free (ctx->rhs);
-  if (ctx)
-    free (ctx);
-  return -1;
+	/*
+	 * The default RHS can be overridden by an environment
+	 * variable.
+	 */
+	if ((cp = __secure_getenv("HES_DOMAIN")) != NULL) {
+		if (ctx->RHS)
+			free(ctx->RHS);
+		ctx->RHS = malloc(strlen(cp)+2);
+		if (!ctx->RHS) {
+			__set_errno(ENOMEM);
+			goto cleanup;
+		}
+		if (cp[0] == '.')
+			strcpy(ctx->RHS, cp);
+		else {
+			strcpy(ctx->RHS, ".");
+			strcat(ctx->RHS, cp);
+		}
+	}
+
+	/*
+	 * If there is no default hesiod realm set, we return an
+	 * error.
+	 */
+	if (!ctx->RHS) {
+		__set_errno(ENOEXEC);
+		goto cleanup;
+	}
+	
+#if 0
+	if (res_ninit(ctx->res) < 0)
+		goto cleanup;
+#endif
+
+	*context = ctx;
+	return (0);
+
+ cleanup:
+	hesiod_end(ctx);
+	return (-1);
 }
 
-/* This function deallocates the hesiod_p. */
+/*
+ * This function deallocates the hesiod_p
+ */
 void
-hesiod_end (void *context)
-{
-  struct hesiod_p *ctx = (struct hesiod_p *) context;
-
-  free (ctx->rhs);
-  if (ctx->lhs)
-    free (ctx->lhs);
-  free (ctx);
+hesiod_end(void *context) {
+	struct hesiod_p *ctx = (struct hesiod_p *) context;
+	int save_errno = errno;
+
+	if (ctx->res)
+		res_nclose(ctx->res);
+	if (ctx->RHS)
+		free(ctx->RHS);
+	if (ctx->LHS)
+		free(ctx->LHS);
+	if (ctx->res && ctx->free_res)
+		(*ctx->free_res)(ctx->res);
+	free(ctx);
+	__set_errno(save_errno);
 }
 
-/* This function takes a hesiod (name, type) and returns a DNS
+/*
+ * This function takes a hesiod (name, type) and returns a DNS
  * name which is to be resolved.
  */
 char *
-hesiod_to_bind (void *context, const char *name, const char *type)
-{
-  struct hesiod_p *ctx = (struct hesiod_p *) context;
-  char bindname[MAXDNAME], *p, *endp, *ret, **rhs_list = NULL;
-  const char *rhs;
-  size_t len;
-
-  endp = stpcpy (bindname, name);
-
-  /* Find the right right hand side to use, possibly truncating bindname. */
-  p = strchr (bindname, '@');
-  if (p)
-    {
-      *p++ = 0;
-      if (strchr (p, '.'))
-	rhs = name + (p - bindname);
-      else
-	{
-	  rhs_list = hesiod_resolve (context, p, "rhs-extension");
-	  if (rhs_list)
-	    rhs = *rhs_list;
-	  else
-	    {
-	      __set_errno (ENOENT);
-	      return NULL;
-	    }
+hesiod_to_bind(void *context, const char *name, const char *type) {
+	struct hesiod_p *ctx = (struct hesiod_p *) context;
+	char *bindname;
+	char **rhs_list = NULL;
+	const char *RHS, *cp;
+
+	/* Decide what our RHS is, and set cp to the end of the actual name. */
+	if ((cp = strchr(name, '@')) != NULL) {
+		if (strchr(cp + 1, '.'))
+			RHS = cp + 1;
+		else if ((rhs_list = hesiod_resolve(context, cp + 1,
+		    "rhs-extension")) != NULL)
+			RHS = *rhs_list;
+		else {
+			__set_errno(ENOENT);
+			return (NULL);
+		}
+	} else {
+		RHS = ctx->RHS;
+		cp = name + strlen(name);
+	}
+
+	/*
+	 * Allocate the space we need, including up to three periods and
+	 * the terminating NUL.
+	 */
+	if ((bindname = malloc((cp - name) + strlen(type) + strlen(RHS) +
+	    (ctx->LHS ? strlen(ctx->LHS) : 0) + 4)) == NULL) {
+		__set_errno(ENOMEM);
+		if (rhs_list)
+			hesiod_free_list(context, rhs_list);
+		return NULL;
+	}
+
+	/* Now put together the DNS name. */
+	memcpy(bindname, name, cp - name);
+	bindname[cp - name] = '\0';
+	strcat(bindname, ".");
+	strcat(bindname, type);
+	if (ctx->LHS) {
+		if (ctx->LHS[0] != '.')
+			strcat(bindname, ".");
+		strcat(bindname, ctx->LHS);
 	}
-    }
-  else
-    rhs = ctx->rhs;
-
-  /* See if we have enough room. */
-  len = (endp - bindname) + 1 + strlen (type);
-  if (ctx->lhs)
-    len += strlen (ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0);
-  len += strlen (rhs) + ((rhs[0] != '.') ? 1 : 0);
-  if (len > sizeof (bindname) - 1)
-    {
-      if (rhs_list)
-	hesiod_free_list (context, rhs_list);
-      __set_errno (EMSGSIZE);
-      return NULL;
-    }
-
-  /* Put together the rest of the domain. */
-  endp = stpcpy (stpcpy (endp, "."), type);
-  if (ctx->lhs)
-    {
-      if (ctx->lhs[0] != '.')
-	endp = stpcpy (endp, ".");
-      endp = stpcpy (endp, ctx->lhs);
-    }
-  if (rhs[0] != '.')
-    endp = stpcpy (endp, ".");
-  endp = stpcpy (endp, rhs);
-
-  /* rhs_list is no longer needed, since we're done with rhs. */
-  if (rhs_list)
-    hesiod_free_list (context, rhs_list);
-
-  /* Make a copy of the result and return it to the caller. */
-  ret = malloc ((endp - bindname) + 1);
-  if (!ret)
-    {
-      __set_errno (ENOMEM);
-      return NULL;
-    }
-  return strcpy (ret, bindname);
+	if (RHS[0] != '.')
+		strcat(bindname, ".");
+	strcat(bindname, RHS);
+
+	if (rhs_list)
+		hesiod_free_list(context, rhs_list);
+
+	return (bindname);
 }
 
-/* This is the core function.  Given a hesiod name and type, it
+/*
+ * This is the core function.  Given a hesiod (name, type), it
  * returns an array of strings returned by the resolver.
  */
 char **
-hesiod_resolve (void *context, const char *name, const char *type)
-{
-  struct hesiod_p *ctx = (struct hesiod_p *) context;
-  char *bindname, **retvec;
-
-  bindname = hesiod_to_bind (context, name, type);
-  if (bindname == NULL)
-    return NULL;
+hesiod_resolve(void *context, const char *name, const char *type) {
+	struct hesiod_p *ctx = (struct hesiod_p *) context;
+	char *bindname = hesiod_to_bind(context, name, type);
+	char **retvec;
+
+	if (bindname == NULL)
+		return (NULL);
+	if (init(ctx) == -1) {
+		free(bindname);
+		return (NULL);
+	}
 
-  retvec = get_txt_records(ctx, ctx->classes[0], bindname);
-  if (retvec == NULL && errno == ENOENT && ctx->classes[1])
-    retvec = get_txt_records (ctx, ctx->classes[1], bindname);
+	if ((retvec = get_txt_records(ctx, C_IN, bindname))) {
+		free(bindname);
+		return (retvec);
+	}
+	
+	if (errno != ENOENT)
+		return (NULL);
 
-  free (bindname);
-  return retvec;
+	retvec = get_txt_records(ctx, C_HS, bindname);
+	free(bindname);
+	return (retvec);
 }
 
 void
-hesiod_free_list (void *context, char **list)
-{
-  char **p;
+hesiod_free_list(void *context, char **list) {
+	char **p;
 
-  for (p = list; *p; p++)
-    free (*p);
-  free (list);
+	for (p = list; *p; p++)
+		free(*p);
+	free(list);
 }
 
-/* This function parses the /etc/hesiod.conf file.  Returns 0 on success,
- * -1 on failure.  On failure, it might leave values in ctx->lhs or
- * ctx->rhs which need to be freed by the caller. */
+/*
+ * This function parses the /etc/hesiod.conf file
+ */
 static int
-read_config_file (struct hesiod_p *ctx, const char *filename)
-{
-  char *key, *data, *p, **which;
-  char buf[MAXDNAME + 7];
-  int n;
-  FILE *fp;
-
-  /* Set default query classes. */
-  ctx->classes[0] = C_IN;
-  ctx->classes[1] = C_HS;
-
-  /* Try to open the configuration file. */
-  fp = fopen (filename, "r");
-  if (fp == NULL)
-    {
-      /* Use compiled in default domain names. */
-      ctx->lhs = malloc (strlen (DEF_LHS) + 1);
-      ctx->rhs = malloc (strlen (DEF_RHS) + 1);
-      if (ctx->lhs && ctx->rhs)
-	{
-	  strcpy (ctx->lhs, DEF_LHS);
-	  strcpy (ctx->rhs, DEF_RHS);
-	  return 0;
-	}
-      else
-	{
-	  __set_errno (ENOMEM);
-	  return -1;
-	}
-    }
-
-  ctx->lhs = NULL;
-  ctx->rhs = NULL;
-  while (fgets (buf, sizeof (buf), fp) != NULL)
-    {
-      p = buf;
-      if (*p == '#' || *p == '\n' || *p == '\r')
-	continue;
-      while (*p == ' ' || *p == '\t')
-	++p;
-      key = p;
-      while(*p != ' ' && *p != '\t' && *p != '=')
-	++p;
-      *p++ = 0;
-
-      while (isspace (*p) || *p == '=')
-	++p;
-      data = p;
-      while (!isspace (*p))
-	++p;
-      *p = 0;
-
-      if (cistrcmp (key, "lhs") == 0 || cistrcmp (key, "rhs") == 0)
-	{
-	  which = (strcmp (key, "lhs") == 0) ? &ctx->lhs : &ctx->rhs;
-	  *which = strdup (data);
-	  if (!*which)
-	    {
-	      __set_errno (ENOMEM);
-	      return -1;
-	    }
-	}
-      else if (cistrcmp (key, "classes") == 0)
-	{
-	  n = 0;
-	  while (*data && n < 2)
-	    {
-	      p = data;
-	      while (*p && *p != ',')
-		++p;
-	      if (*p)
-		*p++ = 0;
-	      if (cistrcmp (data, "IN") == 0)
-		ctx->classes[n++] = C_IN;
-	      else if (cistrcmp (data, "HS") == 0)
-		ctx->classes[n++] = C_HS;
-	      data = p;
-	    }
-	  while (n < 2)
-	    ctx->classes[n++] = 0;
+parse_config_file(struct hesiod_p *ctx, const char *filename) {
+	char *key, *data, *cp, **cpp;
+	char buf[MAXDNAME+7];
+	FILE *fp;
+
+	/*
+	 * Clear the existing configuration variable, just in case
+	 * they're set.
+	 */
+	if (ctx->RHS)
+		free(ctx->RHS);
+	if (ctx->LHS)
+		free(ctx->LHS);
+	ctx->RHS = ctx->LHS = 0;
+
+	/*
+	 * Now open and parse the file...
+	 */
+	if (!(fp = fopen(filename, "r")))
+		return (-1);
+	
+	while (fgets(buf, sizeof(buf), fp) != NULL) {
+		cp = buf;
+		if (*cp == '#' || *cp == '\n' || *cp == '\r')
+			continue;
+		while(*cp == ' ' || *cp == '\t')
+			cp++;
+		key = cp;
+		while(*cp != ' ' && *cp != '\t' && *cp != '=')
+			cp++;
+		*cp++ = '\0';
+		
+		while(*cp == ' ' || *cp == '\t' || *cp == '=')
+			cp++;
+		data = cp;
+		while(*cp != ' ' && *cp != '\n' && *cp != '\r')
+			cp++;
+		*cp++ = '\0';
+
+		if (strcmp(key, "lhs") == 0)
+			cpp = &ctx->LHS;
+		else if (strcmp(key, "rhs") == 0)
+			cpp = &ctx->RHS;
+		else
+			continue;
+
+		*cpp = malloc(strlen(data) + 1);
+		if (!*cpp) {
+			__set_errno(ENOMEM);
+			goto cleanup;
+		}
+		strcpy(*cpp, data);
 	}
-    }
-  fclose (fp);
-
-  if (!ctx->rhs || ctx->classes[0] == 0 || ctx->classes[0] == ctx->classes[1])
-    {
-      __set_errno (ENOEXEC);
-      return -1;
-    }
-
-  return 0;
+	fclose(fp);
+	return (0);
+	
+ cleanup:
+	fclose(fp);
+	if (ctx->RHS)
+		free(ctx->RHS);
+	if (ctx->LHS)
+		free(ctx->LHS);
+	ctx->RHS = ctx->LHS = 0;
+	return (-1);
 }
 
-/* Given a DNS class and a DNS name, do a lookup for TXT records, and
+/*
+ * Given a DNS class and a DNS name, do a lookup for TXT records, and
  * return a list of them.
  */
 static char **
-get_txt_records (struct hesiod_p *ctx, int qclass, const char *name)
-{
-  HEADER *hp;
-  unsigned char qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor;
-  char *dst, **list;
-  int ancount, qdcount, i, j, n, skip, type, class, len;
-
-  /* Make sure the resolver is initialized. */
-  if ((_res.options & RES_INIT) == 0 && res_init () == -1)
-    return NULL;
-
-  /* Construct the query. */
-  n = res_mkquery (QUERY, name, qclass, T_TXT, NULL, 0,
-		   NULL, qbuf, PACKETSZ);
-  if (n < 0)
-      return NULL;
-
-  /* Send the query. */
-  n = res_send (qbuf, n, abuf, MAX_HESRESP);
-  if (n < 0)
-    {
-      __set_errno (ECONNREFUSED);
-      return NULL;
-    }
-
-  /* Parse the header of the result. */
-  hp = (HEADER *) abuf;
-  ancount = ntohs (hp->ancount);
-  qdcount = ntohs (hp->qdcount);
-  p = abuf + sizeof (HEADER);
-  eom = abuf + n;
-
-  /* Skip questions, trying to get to the answer section which follows. */
-  for (i = 0; i < qdcount; ++i)
-    {
-      skip = dn_skipname (p, eom);
-      if (skip < 0 || p + skip + QFIXEDSZ > eom)
-	{
-	  __set_errno (EMSGSIZE);
-	  return NULL;
+get_txt_records(struct hesiod_p *ctx, int class, const char *name) {
+	struct {
+		int type;		/* RR type */
+		int class;		/* RR class */
+		int dlen;		/* len of data section */
+		u_char *data;		/* pointer to data */
+	} rr;
+	HEADER *hp;
+	u_char qbuf[MAX_HESRESP], abuf[MAX_HESRESP];
+	u_char *cp, *erdata, *eom;
+	char *dst, *edst, **list;
+	int ancount, qdcount;
+	int i, j, n, skip;
+
+	/*
+	 * Construct the query and send it.
+	 */
+	n = res_nmkquery(ctx->res, QUERY, name, class, T_TXT, NULL, 0,
+			 NULL, qbuf, MAX_HESRESP);
+	if (n < 0) {
+		__set_errno(EMSGSIZE);
+		return (NULL);
 	}
-      p += skip + QFIXEDSZ;
-    }
-
-  /* Allocate space for the text record answers. */
-  list = malloc ((ancount + 1) * sizeof(char *));
-  if (list == NULL)
-    {
-      __set_errno (ENOMEM);
-      return NULL;
-    }
-
-  /* Parse the answers. */
-  j = 0;
-  for (i = 0; i < ancount; i++)
-    {
-      /* Parse the header of this answer. */
-      skip = dn_skipname (p, eom);
-      if (skip < 0 || p + skip + 10 > eom)
-	break;
-      type = p[skip + 0] << 8 | p[skip + 1];
-      class = p[skip + 2] << 8 | p[skip + 3];
-      len = p[skip + 8] << 8 | p[skip + 9];
-      p += skip + 10;
-      if (p + len > eom)
-	{
-	  __set_errno (EMSGSIZE);
-	  break;
+	n = res_nsend(ctx->res, qbuf, n, abuf, MAX_HESRESP);
+	if (n < 0) {
+		__set_errno(ECONNREFUSED);
+		return (NULL);
+	}
+	if (n < HFIXEDSZ) {
+		__set_errno(EMSGSIZE);
+		return (NULL);
 	}
 
-      /* Skip entries of the wrong class and type. */
-      if (class != qclass || type != T_TXT)
-	{
-	  p += len;
-	  continue;
+	/*
+	 * OK, parse the result.
+	 */
+	hp = (HEADER *) abuf;
+	ancount = ntohs(hp->ancount);
+	qdcount = ntohs(hp->qdcount);
+	cp = abuf + sizeof(HEADER);
+	eom = abuf + n;
+
+	/* Skip query, trying to get to the answer section which follows. */
+	for (i = 0; i < qdcount; i++) {
+		skip = dn_skipname(cp, eom);
+		if (skip < 0 || cp + skip + QFIXEDSZ > eom) {
+			__set_errno(EMSGSIZE);
+			return (NULL);
+		}
+		cp += skip + QFIXEDSZ;
 	}
 
-      /* Allocate space for this answer. */
-      list[j] = malloc (len);
-      if (!list[j])
-	{
-	  __set_errno (ENOMEM);
-	  break;
+	list = malloc((ancount + 1) * sizeof(char *));
+	if (!list) {
+		__set_errno(ENOMEM);
+		return (NULL);
 	}
-      dst = list[j++];
-
-      /* Copy answer data into the allocated area. */
-      eor = p + len;
-      while (p < eor)
-	{
-	  n = (unsigned char) *p++;
-	  if (p + n > eor)
-	    {
-	      __set_errno (EMSGSIZE);
-	      break;
-	    }
-	  dst = mempcpy (dst, p, n);
-	  p += n;
+	j = 0;
+	for (i = 0; i < ancount; i++) {
+		skip = dn_skipname(cp, eom);
+		if (skip < 0) {
+			__set_errno(EMSGSIZE);
+			goto cleanup;
+		}
+		cp += skip;
+		if (cp + 3 * INT16SZ + INT32SZ > eom) {
+			__set_errno(EMSGSIZE);
+			goto cleanup;
+		}
+		rr.type = ns_get16(cp);
+		cp += INT16SZ;
+		rr.class = ns_get16(cp);
+		cp += INT16SZ + INT32SZ;	/* skip the ttl, too */
+		rr.dlen = ns_get16(cp);
+		cp += INT16SZ;
+		if (cp + rr.dlen > eom) {
+			__set_errno(EMSGSIZE);
+			goto cleanup;
+		}
+		rr.data = cp;
+		cp += rr.dlen;
+		if (rr.class != class || rr.type != T_TXT)
+			continue;
+		if (!(list[j] = malloc(rr.dlen)))
+			goto cleanup;
+		dst = list[j++];
+		edst = dst + rr.dlen;
+		erdata = rr.data + rr.dlen;
+		cp = rr.data;
+		while (cp < erdata) {
+			n = (unsigned char) *cp++;
+			if (cp + n > eom || dst + n > edst) {
+				__set_errno(EMSGSIZE);
+				goto cleanup;
+			}
+			memcpy(dst, cp, n);
+			cp += n;
+			dst += n;
+		}
+		if (cp != erdata) {
+			__set_errno(EMSGSIZE);
+			goto cleanup;
+		}
+		*dst = '\0';
 	}
-      if (p < eor)
-	{
-	  __set_errno (EMSGSIZE);
-	  break;
+	list[j] = NULL;
+	if (j == 0) {
+		__set_errno(ENOENT);
+		goto cleanup;
 	}
-      *dst = 0;
-    }
-
-  /* If we didn't terminate the loop normally, something went wrong. */
-  if (i < ancount)
-    {
-      for (i = 0; i < j; i++)
-	free (list[i]);
-      free (list);
-      return NULL;
-    }
-
-  if (j == 0)
-    {
-      __set_errno (ENOENT);
-      free (list);
-      return NULL;
-    }
-
-  list[j] = NULL;
-  return list;
+	return (list);
+
+ cleanup:
+	for (i = 0; i < j; i++)
+		free(list[i]);
+	free(list);
+	return (NULL);
+}
+
+struct __res_state *
+__hesiod_res_get(void *context) {
+	struct hesiod_p *ctx = context;
+
+	if (!ctx->res) {
+		struct __res_state *res;
+		res = (struct __res_state *)malloc(sizeof *res);
+		if (res == NULL) {
+			__set_errno(ENOMEM);
+			return (NULL);
+		}
+		memset(res, 0, sizeof *res);
+		__hesiod_res_set(ctx, res, free);
+	}
+
+	return (ctx->res);
+}
+
+void
+__hesiod_res_set(void *context, struct __res_state *res,
+	         void (*free_res)(void *)) {
+	struct hesiod_p *ctx = context;
+
+	if (ctx->res && ctx->free_res) {
+		res_nclose(ctx->res);
+		(*ctx->free_res)(ctx->res);
+	}
+
+	ctx->res = res;
+	ctx->free_res = free_res;
 }
 
-#ifndef _LIBC
 static int
-cistrcmp (const char *s1, const char *s2)
-{
-  while (*s1 && tolower(*s1) == tolower(*s2))
-    {
-      s1++;
-      s2++;
-    }
-  return tolower(*s1) - tolower(*s2);
+init(struct hesiod_p *ctx) {
+	
+	if (!ctx->res && !__hesiod_res_get(ctx))
+		return (-1);
+
+	if (((ctx->res->options & RES_INIT) == 0) &&
+	    (res_ninit(ctx->res) == -1))
+		return (-1);
+
+	return (0);
 }
-#endif
diff --git a/hesiod/hesiod.h b/hesiod/hesiod.h
index 9c3d323811..c4711349c1 100644
--- a/hesiod/hesiod.h
+++ b/hesiod/hesiod.h
@@ -1,7 +1,5 @@
-/* $Id$ */
-
 /*
- * Copyright (c) 1996 by Internet Software Consortium.
+ * Copyright (c) 1996,1999 by Internet Software Consortium.
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -17,57 +15,26 @@
  * SOFTWARE.
  */
 
-#ifndef HESIOD__INCLUDED
-#define HESIOD__INCLUDED
-
-#include <sys/types.h>
-#include <pwd.h>
-#include <netdb.h>
-
-/* Application-visible define to signal that we have the new interfaces. */
-#define HESIOD_INTERFACES
-
-struct hesiod_postoffice {
-  char *hesiod_po_type;
-  char *hesiod_po_host;
-  char *hesiod_po_name;
-};
-
-int hesiod_init(void **context);
-void hesiod_end(void *context);
-char *hesiod_to_bind(void *context, const char *name, const char *type);
-char **hesiod_resolve(void *context, const char *name, const char *type);
-void hesiod_free_list(void *context, char **list);
-struct passwd *hesiod_getpwnam(void *context, const char *name);
-struct passwd *hesiod_getpwuid(void *context, uid_t uid);
-void hesiod_free_passwd(void *context, struct passwd *pw);
-struct servent *hesiod_getservbyname(void *context, const char *name,
-				     const char *proto);
-void hesiod_free_servent(void *context, struct servent *serv);
-struct hesiod_postoffice *hesiod_getmailhost(void *context, const char *user);
-void hesiod_free_postoffice(void *context, struct hesiod_postoffice *po);
-
-/* Compatibility stuff. */
-
-#define HES_ER_UNINIT	-1	/* uninitialized */
-#define HES_ER_OK	0	/* no error */
-#define HES_ER_NOTFOUND	1	/* Hesiod name not found by server */
-#define HES_ER_CONFIG	2	/* local problem (no config file?) */
-#define HES_ER_NET	3	/* network problem */
-
-struct hes_postoffice {
-  char *po_type;
-  char *po_host;
-  char *po_name;
-};
+/*
+ * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>.
+ */
 
-int hes_init(void);
-char *hes_to_bind(const char *name, const char *type);
-char **hes_resolve(const char *name, const char *type);
-int hes_error(void);
-struct passwd *hes_getpwnam(const char *name);
-struct passwd *hes_getpwuid(uid_t uid);
-struct servent *hes_getservbyname(const char *name, const char *proto);
-struct hes_postoffice *hes_getmailhost(const char *name);
+/*
+ * $BINDId: hesiod.h,v 1.7 1999/01/08 19:22:45 vixie Exp $
+ */
 
-#endif
+#ifndef _HESIOD_H_INCLUDED
+#define _HESIOD_H_INCLUDED
+
+int		hesiod_init __P((void **context));
+void		hesiod_end __P((void *context));
+char *		hesiod_to_bind __P((void *context, const char *name,
+				    const char *type));
+char **		hesiod_resolve __P((void *context, const char *name,
+				    const char *type));
+void		hesiod_free_list __P((void *context, char **list));
+struct __res_state * __hesiod_res_get __P((void *context));
+void		__hesiod_res_set __P((void *context, struct __res_state *,
+				      void (*)(void *)));
+
+#endif /*_HESIOD_H_INCLUDED*/
diff --git a/hesiod/hesiod_p.h b/hesiod/hesiod_p.h
index 809916ac2b..71aca0976d 100644
--- a/hesiod/hesiod_p.h
+++ b/hesiod/hesiod_p.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996 by Internet Software Consortium.
+ * Copyright (c) 1996,1999 by Internet Software Consortium.
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -16,26 +16,33 @@
  */
 
 /*
- * $Id$
+ * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>.
  */
 
 /*
- * hesiod_p.h -- private definitions for the hesiod library
+ * $BINDId: hesiod_p.h,v 1.9 1999/01/08 19:24:39 vixie Exp $
  */
 
-#ifndef HESIOD_P_H_INCLUDED
-#define HESIOD_P_H_INCLUDED
+/*
+ * hesiod_p.h -- private definitions for the hesiod library
+ */
 
-/* Defaults if the configuration file is not present. */
-#define DEF_RHS ".athena.mit.edu"
-#define DEF_LHS ".ns"
+#ifndef _HESIOD_P_H_INCLUDED
+#define _HESIOD_P_H_INCLUDED
 
+#define DEF_RHS		".Athena.MIT.EDU"	/* Defaults if HESIOD_CONF */
+#define DEF_LHS		".ns"			/*    file is not */
+						/*    present. */
 struct hesiod_p {
-  char *lhs;			/* normally ".ns" */
-  char *rhs;			/* AKA the default hesiod domain */
-  int classes[2];		/* The class search order. */
+	char *		LHS;		/* normally ".ns" */
+	char *		RHS;		/* AKA the default hesiod domain */
+	struct __res_state * res;	/* resolver context */
+	void		(*free_res)(void *);
+	void		(*res_set)(struct hesiod_p *, struct __res_state *,
+				   void (*)(void *));
+	struct __res_state * (*res_get)(struct hesiod_p *);
 };
 
-#define MAX_HESRESP 1024
+#define MAX_HESRESP	1024
 
-#endif /*HESIOD_P_H_INCLUDED*/
+#endif /*_HESIOD_P_H_INCLUDED*/
diff --git a/hesiod/nss_hesiod/hesiod-grp.c b/hesiod/nss_hesiod/hesiod-grp.c
index a973fab80f..2d5b96b7f9 100644
--- a/hesiod/nss_hesiod/hesiod-grp.c
+++ b/hesiod/nss_hesiod/hesiod-grp.c
@@ -21,11 +21,12 @@
 #include <errno.h>
 #include <grp.h>
 #include <hesiod.h>
+#include <nss.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <nss.h>
-#include <bits/libc-lock.h>
+
+#include "nss_hesiod.h"
 
 /* Get the declaration of the parser function.  */
 #define ENTNAME grent
@@ -33,50 +34,15 @@
 #define EXTERN_PARSER
 #include <nss/nss_files/files-parse.c>
 
-/* Locks the static variables in this file.  */
-__libc_lock_define_initialized (static, lock);
-
-static void *context = NULL;
-
-static enum nss_status
-internal_setgrent (void)
-{
-  if (!context)
-    {
-      if (hesiod_init (&context) == -1)
-	return NSS_STATUS_UNAVAIL;
-    }
-
-  return NSS_STATUS_SUCCESS;
-}
-
 enum nss_status
 _nss_hesiod_setgrent (void)
 {
-  enum nss_status status;
-
-  __libc_lock_lock (lock);
-
-  status = internal_setgrent ();
-
-  __libc_lock_unlock (lock);
-
-  return status;
+  return NSS_STATUS_SUCCESS;
 }
 
 enum nss_status
 _nss_hesiod_endgrent (void)
 {
-  __libc_lock_lock (lock);
-
-  if (context)
-    {
-      hesiod_end (context);
-      context = NULL;
-    }
-
-  __libc_lock_unlock (lock);
-
   return NSS_STATUS_SUCCESS;
 }
 
@@ -84,32 +50,37 @@ static enum nss_status
 lookup (const char *name, const char *type, struct group *grp,
 	char *buffer, size_t buflen, int *errnop)
 {
-  enum nss_status status;
   struct parser_data *data = (void *) buffer;
   size_t linebuflen;
+  void *context;
   char **list;
   int parse_res;
   size_t len;
 
-  status = internal_setgrent ();
-  if (status != NSS_STATUS_SUCCESS)
-    return status;
+  context = _nss_hesiod_init ();
+  if (context == NULL)
+    return NSS_STATUS_UNAVAIL;
 
   list = hesiod_resolve (context, name, type);
   if (list == NULL)
-    return errno == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
+    {
+      hesiod_end (context);
+      return errno == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
+    }
 
   linebuflen = buffer + buflen - data->linebuffer;
   len = strlen (*list) + 1;
   if (linebuflen < len)
     {
       hesiod_free_list (context, list);
+      hesiod_end (context);
       *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
   memcpy (data->linebuffer, *list, len);
   hesiod_free_list (context, list);
+  hesiod_end (context);
 
   parse_res = _nss_files_parse_grent (buffer, grp, data, buflen, errnop);
   if (parse_res < 1)
@@ -122,34 +93,19 @@ enum nss_status
 _nss_hesiod_getgrnam_r (const char *name, struct group *grp,
 			char *buffer, size_t buflen, int *errnop)
 {
-  enum nss_status status;
-
-  __libc_lock_lock (lock);
-
-  status = lookup (name, "group", grp, buffer, buflen, errnop);
-
-  __libc_lock_unlock (lock);
-
-  return status;
+  return lookup (name, "group", grp, buffer, buflen, errnop);
 }
 
 enum nss_status
 _nss_hesiod_getgrgid_r (gid_t gid, struct group *grp,
 			char *buffer, size_t buflen, int *errnop)
 {
-  enum nss_status status = NSS_STATUS_UNAVAIL;
   char gidstr[21];	/* We will probably never have a gid_t with more
 			   than 64 bits.  */
 
   snprintf (gidstr, sizeof gidstr, "%d", gid);
 
-  __libc_lock_lock (lock);
-
-  status = lookup (gidstr, "gid", grp, buffer, buflen, errnop);
-
-  __libc_lock_unlock (lock);
-
-  return status;
+  return lookup (gidstr, "gid", grp, buffer, buflen, errnop);
 }
 
 static int
@@ -217,7 +173,8 @@ _nss_hesiod_initgroups (const char *user, gid_t group, long int *start,
   char *p;
   void *context;
 
-  if (hesiod_init (&context) == -1)
+  context = _nss_hesiod_init ();
+  if (context == NULL)
     return NSS_STATUS_UNAVAIL;
 
   list = hesiod_resolve (context, user, "grplist");
diff --git a/hesiod/nss_hesiod/hesiod-init.c b/hesiod/nss_hesiod/hesiod-init.c
new file mode 100644
index 0000000000..ab85ff3cbc
--- /dev/null
+++ b/hesiod/nss_hesiod/hesiod-init.c
@@ -0,0 +1,39 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 2000.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <sys/cdefs.h>		/* Needs to come before <hesiod.h>.  */
+#include <hesiod.h>
+#include <resolv.h>
+#include <stddef.h>
+
+#include "nss_hesiod.h"
+
+void *
+_nss_hesiod_init (void)
+{
+  void *context;
+
+  if (hesiod_init (&context) == -1)
+    return NULL;
+
+  /* Use the default (per-thread) resolver state.  */
+  __hesiod_res_set (context, &_res, NULL);
+
+  return context;
+}
diff --git a/hesiod/nss_hesiod/hesiod-proto.c b/hesiod/nss_hesiod/hesiod-proto.c
new file mode 100644
index 0000000000..d77e8d9134
--- /dev/null
+++ b/hesiod/nss_hesiod/hesiod-proto.c
@@ -0,0 +1,137 @@
+/* Copyright (C) 1997, 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <hesiod.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <nss.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "nss_hesiod.h"
+
+/* Declare a parser for Hesiod protocol entries.  Although the format
+   of the entries is identical to those in /etc/protocols, here is no
+   predefined parser for us to use.  */
+
+#define ENTNAME protoent
+
+struct protoent_data {};
+
+#define TRAILING_LIST_MEMBER		p_aliases
+#define TRAILING_LIST_SEPARATOR_P	isspace
+#include <nss/nss_files/files-parse.c>
+LINE_PARSER
+("#",
+ STRING_FIELD (result->p_name, isspace, 1);
+ INT_FIELD (result->p_proto, isspace, 1, 10,);
+ )
+
+enum nss_status
+_nss_hesiod_setprotoent (void)
+{
+  return NSS_STATUS_SUCCESS;
+}
+
+enum nss_status
+_nss_hesiod_endprotoent (void)
+{
+  return NSS_STATUS_SUCCESS;
+}
+
+static enum nss_status
+lookup (const char *name, const char *type, struct protoent *proto,
+	char *buffer, size_t buflen, int *errnop)
+{
+  struct parser_data *data = (void *) buffer;
+  size_t linebuflen;
+  void *context;
+  char **list, **item;
+  int parse_res;
+  int found;
+
+  context = _nss_hesiod_init ();
+  if (context == NULL)
+    return NSS_STATUS_UNAVAIL;
+
+  list = hesiod_resolve (context, name, type);
+  if (list == NULL)
+    {
+      hesiod_end (context);
+      return errno == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
+    }
+
+  linebuflen = buffer + buflen - data->linebuffer;
+
+  item = list;
+  found = 0;
+  do
+    {
+      size_t len = strlen (*item) + 1;
+
+      if (linebuflen < len)
+	{
+	  hesiod_free_list (context, list);
+	  hesiod_end (context);
+	  *errnop = ERANGE;
+	  return NSS_STATUS_TRYAGAIN;
+	}
+
+      memcpy (data->linebuffer, *item, len);
+
+      parse_res = parse_line (buffer, proto, data, buflen, errnop);
+      if (parse_res == -1)
+	{
+	  hesiod_free_list (context, list);
+	  hesiod_end (context);
+	  return NSS_STATUS_TRYAGAIN;
+	}
+
+      if (parse_res > 0)
+	found = 1;
+
+      ++item;
+    }
+  while (*item != NULL && !found);
+
+  hesiod_free_list (context, list);
+  hesiod_end (context);
+
+  return found ? NSS_STATUS_SUCCESS : NSS_STATUS_NOTFOUND;
+}
+
+enum nss_status
+_nss_hesiod_getprotobyname_r (const char *name, struct protoent *proto,
+			      char *buffer, size_t buflen, int *errnop)
+{
+  return lookup (name, "protocol", proto, buffer, buflen, errnop);
+}
+
+enum nss_status
+_nss_hesiod_getprotobynumber_r (const int protocol, struct protoent *proto,
+				char *buffer, size_t buflen, int *errnop)
+{
+  char protostr[21];
+
+  snprintf (protostr, sizeof protostr, "%d", protocol);
+
+  return lookup (protostr, "protonum", proto, buffer, buflen, errnop);
+}
diff --git a/hesiod/nss_hesiod/hesiod-pwd.c b/hesiod/nss_hesiod/hesiod-pwd.c
index ce97443164..b7d2dd5c09 100644
--- a/hesiod/nss_hesiod/hesiod-pwd.c
+++ b/hesiod/nss_hesiod/hesiod-pwd.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1997, 2000 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
 
@@ -17,65 +17,31 @@
    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.  */
 
-#include <bits/libc-lock.h>
 #include <errno.h>
 #include <hesiod.h>
-#include <nss.h>
 #include <pwd.h>
+#include <nss.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+#include "nss_hesiod.h"
+
 /* Get the declaration of the parser function.  */
 #define ENTNAME pwent
 #define STRUCTURE passwd
 #define EXTERN_PARSER
 #include <nss/nss_files/files-parse.c>
 
-/* Locks the static variables in this file.  */
-__libc_lock_define_initialized (static, lock);
-
-static void *context = NULL;
-
-static enum nss_status
-internal_setpwent (void)
-{
-  if (!context)
-    {
-      if (hesiod_init (&context) == -1)
-	return NSS_STATUS_UNAVAIL;
-    }
-
-  return NSS_STATUS_SUCCESS;
-}
-
 enum nss_status
 _nss_hesiod_setpwent (void)
 {
-  enum nss_status status;
-
-  __libc_lock_lock (lock);
-
-  status = internal_setpwent ();
-
-  __libc_lock_unlock (lock);
-
-  return status;
+  return NSS_STATUS_SUCCESS;
 }
 
 enum nss_status
 _nss_hesiod_endpwent (void)
 {
-  __libc_lock_lock (lock);
-
-  if (context)
-    {
-      hesiod_end (context);
-      context = NULL;
-    }
-
-  __libc_lock_unlock (lock);
-
   return NSS_STATUS_SUCCESS;
 }
 
@@ -83,32 +49,37 @@ static enum nss_status
 lookup (const char *name, const char *type, struct passwd *pwd,
 	char *buffer, size_t buflen, int *errnop)
 {
-  enum nss_status status;
   struct parser_data *data = (void *) buffer;
   size_t linebuflen;
+  void *context;
   char **list;
   int parse_res;
   size_t len;
 
-  status = internal_setpwent ();
-  if (status != NSS_STATUS_SUCCESS)
-    return status;
+  context = _nss_hesiod_init ();
+  if (context == NULL)
+    return NSS_STATUS_UNAVAIL;
 
   list = hesiod_resolve (context, name, type);
   if (list == NULL)
-    return errno == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
+    {
+      hesiod_end (context);
+      return errno == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
+    }
 
   linebuflen = buffer + buflen - data->linebuffer;
   len = strlen (*list) + 1;
   if (linebuflen < len)
     {
       hesiod_free_list (context, list);
+      hesiod_end (context);
       *errnop = ERANGE;
       return NSS_STATUS_TRYAGAIN;
     }
 
   memcpy (data->linebuffer, *list, len);
   hesiod_free_list (context, list);
+  hesiod_end (context);
 
   parse_res = _nss_files_parse_pwent (buffer, pwd, data, buflen, errnop);
   if (parse_res < 1)
@@ -121,32 +92,17 @@ enum nss_status
 _nss_hesiod_getpwnam_r (const char *name, struct passwd *pwd,
 			char *buffer, size_t buflen, int *errnop)
 {
-  enum nss_status status;
-
-  __libc_lock_lock (lock);
-
-  status = lookup (name, "passwd", pwd, buffer, buflen, errnop);
-
-  __libc_lock_unlock (lock);
-
-  return status;
+  return lookup (name, "passwd", pwd, buffer, buflen, errnop);
 }
 
 enum nss_status
 _nss_hesiod_getpwuid_r (uid_t uid, struct passwd *pwd,
 			char *buffer, size_t buflen, int *errnop)
 {
-  enum nss_status status = NSS_STATUS_UNAVAIL;
   char uidstr[21];	/* We will probably never have a gid_t with more
 			   than 64 bits.  */
 
   snprintf (uidstr, sizeof uidstr, "%d", uid);
 
-  __libc_lock_lock (lock);
-
-  status = lookup (uidstr, "uid", pwd, buffer, buflen, errnop);
-
-  __libc_lock_unlock (lock);
-
-  return status;
+  return lookup (uidstr, "uid", pwd, buffer, buflen, errnop);
 }
diff --git a/hesiod/nss_hesiod/hesiod-service.c b/hesiod/nss_hesiod/hesiod-service.c
index e0ca965310..8faea3aa5d 100644
--- a/hesiod/nss_hesiod/hesiod-service.c
+++ b/hesiod/nss_hesiod/hesiod-service.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1997, 2000 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
 
@@ -17,102 +17,68 @@
    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.  */
 
-#include <bits/libc-lock.h>
 #include <errno.h>
 #include <hesiod.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <nss.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+#include "nss_hesiod.h"
 
 /* Hesiod uses a format for service entries that differs from the
    traditional format.  We therefore declare our own parser.  */
 
 #define ENTNAME servent
 
-#define ENTDATA servent_data
 struct servent_data {};
 
 #define TRAILING_LIST_MEMBER		s_aliases
 #define TRAILING_LIST_SEPARATOR_P	isspace
 #include <nss/nss_files/files-parse.c>
-#define ISSEMICOLON(c)	((c) ==  ';')
+#define ISSC_OR_SPACE(c)	((c) ==  ';' || isspace (c))
 LINE_PARSER
-("",
- (void) entdata;
- STRING_FIELD (result->s_name, ISSEMICOLON, 1);
- STRING_FIELD (result->s_proto, ISSEMICOLON, 1);
- INT_FIELD (result->s_port, ISSEMICOLON, 10, 0, htons);
+("#",
+ STRING_FIELD (result->s_name, ISSC_OR_SPACE, 1);
+ STRING_FIELD (result->s_proto, ISSC_OR_SPACE, 1);
+ INT_FIELD (result->s_port, ISSC_OR_SPACE, 10, 0, htons);
  )
 
-
-/* Locks the static variables in this file.  */
-__libc_lock_define_initialized (static, lock);
-
-static void *context = NULL;
-
-static enum nss_status
-internal_setservent (void)
-{
-  if (!context)
-    {
-      if (hesiod_init (&context) == -1)
-	return NSS_STATUS_UNAVAIL;
-    }
-
-  return NSS_STATUS_SUCCESS;
-}
-
 enum nss_status
 _nss_hesiod_setservent (void)
 {
-  enum nss_status status;
-
-  __libc_lock_lock (lock);
-
-  status = internal_setservent ();
-
-  __libc_lock_unlock (lock);
-
-  return status;
+  return NSS_STATUS_SUCCESS;
 }
 
 enum nss_status
 _nss_hesiod_endservent (void)
 {
-  __libc_lock_lock (lock);
-
-  if (context)
-    {
-      hesiod_end (context);
-      context = NULL;
-    }
-
-  __libc_lock_unlock (lock);
-
   return NSS_STATUS_SUCCESS;
 }
 
 static enum nss_status
-lookup (const char *name, const char *protocol, struct servent *serv,
-	char *buffer, size_t buflen, int *errnop)
+lookup (const char *name, const char *type, const char *protocol,
+	struct servent *serv, char *buffer, size_t buflen, int *errnop)
 {
-  enum nss_status status;
   struct parser_data *data = (void *) buffer;
   size_t linebuflen;
+  void *context;
   char **list, **item;
   int parse_res;
   int found;
 
-  status = internal_setservent ();
-  if (status != NSS_STATUS_SUCCESS)
-    return status;
+  context = _nss_hesiod_init ();
+  if (context == NULL)
+    return NSS_STATUS_UNAVAIL;
 
-  list = hesiod_resolve (context, name, "service");
+  list = hesiod_resolve (context, name, type);
   if (list == NULL)
-    return errno == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
+    {
+      hesiod_end (context);
+      return errno == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
+    }
 
   linebuflen = buffer + buflen - data->linebuffer;
 
@@ -125,6 +91,7 @@ lookup (const char *name, const char *protocol, struct servent *serv,
       if (linebuflen < len)
 	{
 	  hesiod_free_list (context, list);
+	  hesiod_end (context);
 	  *errnop = ERANGE;
 	  return NSS_STATUS_TRYAGAIN;
 	}
@@ -135,17 +102,19 @@ lookup (const char *name, const char *protocol, struct servent *serv,
       if (parse_res == -1)
 	{
 	  hesiod_free_list (context, list);
+	  hesiod_end (context);
 	  return NSS_STATUS_TRYAGAIN;
 	}
 
       if (parse_res > 0)
-	found = protocol == NULL || strcmp (serv->s_proto, protocol) == 0;
+	found = protocol == NULL || strcasecmp (serv->s_proto, protocol) == 0;
 
       ++item;
     }
   while (*item != NULL && !found);
 
   hesiod_free_list (context, list);
+  hesiod_end (context);
 
   return found ? NSS_STATUS_SUCCESS : NSS_STATUS_NOTFOUND;
 }
@@ -155,13 +124,17 @@ _nss_hesiod_getservbyname_r (const char *name, const char *protocol,
 			     struct servent *serv,
 			     char *buffer, size_t buflen, int *errnop)
 {
-  enum nss_status status;
-
-  __libc_lock_lock (lock);
+  return lookup (name, "service", protocol, serv, buffer, buflen, errnop);
+}
 
-  status = lookup (name, protocol, serv, buffer, buflen, errnop);
+enum nss_status
+_nss_hesiod_getservbyport_r (const int port, const char *protocol,
+			     struct servent *serv,
+			     char *buffer, size_t buflen, int *errnop)
+{
+  char portstr[6];	    /* Port numbers are restricted to 16 bits. */
 
-  __libc_lock_unlock (lock);
+  snprintf (portstr, sizeof portstr, "%d", ntohs (port));
 
-  return status;
+  return lookup (portstr, "port", protocol, serv, buffer, buflen, errnop);
 }
diff --git a/hesiod/nss_hesiod/nss_hesiod.h b/hesiod/nss_hesiod/nss_hesiod.h
new file mode 100644
index 0000000000..78039d8352
--- /dev/null
+++ b/hesiod/nss_hesiod/nss_hesiod.h
@@ -0,0 +1,21 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 2000.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+/* Initialize a Hesiod context.  */
+extern void *_nss_hesiod_init (void);