From: =?utf-8?q?Ferenc_W=C3=A1gner?= <wferi@debian.org>
Date: Thu, 5 Nov 2020 08:47:58 +0100
Subject: Embed xgethostname 20020413 for Hurd compatibility

As recommended by
https://www.gnu.org/software/hurd/hurd/porting/guidelines.html.
Source: http://walfield.org/pub/people/neal/xgethostname/
---
 Makefile.am                 |   2 +-
 pam_mysql.c                 |  27 +++++++---
 xgethostname/AUTHOR         |   1 +
 xgethostname/COPYING        |  14 +++++
 xgethostname/ChangeLog      |  64 +++++++++++++++++++++++
 xgethostname/Makefile       |  11 ++++
 xgethostname/README         |   1 +
 xgethostname/VERSION        |   5 ++
 xgethostname/xgethostname.2 |  37 +++++++++++++
 xgethostname/xgethostname.c | 125 ++++++++++++++++++++++++++++++++++++++++++++
 xgethostname/xgethostname.h |  48 +++++++++++++++++
 11 files changed, 327 insertions(+), 8 deletions(-)
 create mode 100644 xgethostname/AUTHOR
 create mode 100644 xgethostname/COPYING
 create mode 100644 xgethostname/ChangeLog
 create mode 100644 xgethostname/Makefile
 create mode 100644 xgethostname/README
 create mode 100644 xgethostname/VERSION
 create mode 100644 xgethostname/xgethostname.2
 create mode 100644 xgethostname/xgethostname.c
 create mode 100644 xgethostname/xgethostname.h

diff --git a/Makefile.am b/Makefile.am
index f2da3cf..b213ee8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2,7 +2,7 @@
 pamexecdir = @PAM_MODS_DIR@
 pamexec_LTLIBRARIES = pam_mysql.la
 
-pam_mysql_la_SOURCES = pam_mysql.c crypto.c crypto-sha1.c crypto-md5.c
+pam_mysql_la_SOURCES = pam_mysql.c crypto.c crypto-sha1.c crypto-md5.c xgethostname/xgethostname.c
 pam_mysql_la_LDFLAGS = -module -avoid-version
 pam_mysql_la_CPPFLAGS = $(openssl_CFLAGS)
 pam_mysql_la_LIBADD   = $(openssl_LIBS) -lpam
diff --git a/pam_mysql.c b/pam_mysql.c
index ef8a427..c2cc42b 100644
--- a/pam_mysql.c
+++ b/pam_mysql.c
@@ -141,6 +141,8 @@
 #include <mysql.h>
 #endif
 
+#include "xgethostname/xgethostname.h"
+
 /*
  * Definitions for the externally accessible functions in this file (these
  * definitions are required for static modules but strongly encouraged
@@ -2678,7 +2680,7 @@ static void pam_mysql_entry_handler_destroy(
 static pam_mysql_err_t pam_mysql_get_host_info(pam_mysql_ctx_t *ctx,
         const char **pretval)
 {
-    char hostname[MAXHOSTNAMELEN + 1];
+    char *hostname;
     char *retval;
 
     if (ctx->my_host_info) {
@@ -2686,8 +2688,9 @@ static pam_mysql_err_t pam_mysql_get_host_info(pam_mysql_ctx_t *ctx,
         return PAM_MYSQL_ERR_SUCCESS;
     }
 
-    if (gethostname(hostname, sizeof(hostname))) {
-        return PAM_MYSQL_ERR_UNKNOWN;
+    hostname = xgethostname();
+    if (!hostname) {
+        return errno == ENOMEM ? PAM_MYSQL_ERR_ALLOC : PAM_MYSQL_ERR_UNKNOWN;
     }
 #ifdef HAVE_GETADDRINFO
     {
@@ -2695,8 +2698,10 @@ static pam_mysql_err_t pam_mysql_get_host_info(pam_mysql_ctx_t *ctx,
         static const struct addrinfo hint = {
             AI_CANONNAME, PF_INET, 0, 0, 0, NULL, NULL, NULL
         };
+        int gai_ret = getaddrinfo(hostname, NULL, &hint, &ainfo);
 
-        switch (getaddrinfo(hostname, NULL, &hint, &ainfo)) {
+        free(hostname);
+        switch (gai_ret) {
             case 0:
                 break;
 
@@ -2747,7 +2752,7 @@ static pam_mysql_err_t pam_mysql_get_host_info(pam_mysql_ctx_t *ctx,
 
         freeaddrinfo(ainfo);
     }
-#else
+#else /* not HAVE_GETADDRINFO */
     {
         struct hostent *hent = NULL;
         struct hostent *tmp;
@@ -2763,6 +2768,7 @@ static pam_mysql_err_t pam_mysql_get_host_info(pam_mysql_ctx_t *ctx,
                     xfree(hent);
                 }
 
+                free(hostname);
                 return PAM_MYSQL_ERR_ALLOC;
             }
 
@@ -2776,12 +2782,14 @@ static pam_mysql_err_t pam_mysql_get_host_info(pam_mysql_ctx_t *ctx,
 
             if (errno != ERANGE) {
                 xfree(hent);
+                free(hostname);
                 return PAM_MYSQL_ERR_UNKNOWN;
             }
 
             if (hent_size + 32 < hent_size) {
                 syslog(LOG_AUTHPRIV | LOG_CRIT, PAM_MYSQL_LOG_PREFIX "allocation failure at " __FILE__ ":%d", __LINE__);
                 xfree(hent);
+                free(hostname);
                 return PAM_MYSQL_ERR_ALLOC;
             }
 
@@ -2796,6 +2804,7 @@ static pam_mysql_err_t pam_mysql_get_host_info(pam_mysql_ctx_t *ctx,
                     xfree(hent);
                 }
 
+                free(hostname);
                 return PAM_MYSQL_ERR_ALLOC;
             }
 
@@ -2809,20 +2818,24 @@ static pam_mysql_err_t pam_mysql_get_host_info(pam_mysql_ctx_t *ctx,
 
             if (errno != ERANGE) {
                 xfree(hent);
+                free(hostname);
                 return PAM_MYSQL_ERR_UNKNOWN;
             }
 
             if (hent_size + 32 < hent_size) {
                 syslog(LOG_AUTHPRIV | LOG_CRIT, PAM_MYSQL_LOG_PREFIX "allocation failure at " __FILE__ ":%d", __LINE__);
                 xfree(hent);
+                free(hostname);
                 return PAM_MYSQL_ERR_ALLOC;
             }
 
             hent_size += 32;
         }
-#else
+#else /* not (HAVE_SUNOS_GETHOSTBYNAME_R or HAVE_GNU_GETHOSTBYNAME_R) */
+        free(hostname);
         return PAM_MYSQL_ERR_NOTIMPL;
-#endif
+#endif /* HAVE_SUNOS_GETHOSTBYNAME_R or HAVE_GNU_GETHOSTBYNAME_R */
+        free(hostname);
 
         switch (hent->h_addrtype) {
             case AF_INET:
diff --git a/xgethostname/AUTHOR b/xgethostname/AUTHOR
new file mode 100644
index 0000000..e4baae7
--- /dev/null
+++ b/xgethostname/AUTHOR
@@ -0,0 +1 @@
+Neal H Walfield <neal@cs.uml.edu>
diff --git a/xgethostname/COPYING b/xgethostname/COPYING
new file mode 100644
index 0000000..cd9368e
--- /dev/null
+++ b/xgethostname/COPYING
@@ -0,0 +1,14 @@
+This program is placed into the public domain.  Its distribution
+is unlimited.
+
+THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/xgethostname/ChangeLog b/xgethostname/ChangeLog
new file mode 100644
index 0000000..9261dca
--- /dev/null
+++ b/xgethostname/ChangeLog
@@ -0,0 +1,64 @@
+2002-04-13  Neal H Walfield  <neal@walfield.org>
+
+	* VERSION: Update to 20010413.
+	* Release version 20010413.
+	
+2002-04-13  Neal H Walfield  <neal@walfield.org>
+
+	* xgethostname.c (xgethostname): gethostname returns -1 on error,
+	not the error code.  There is no need to use realloc as it copies
+	the unused buffer.
+
+2001-08-03  Neal H Walfield  <neal@cs.uml.edu>
+
+	* VERSION: Update to 20010803.
+	* Release version 20010803.
+
+2001-08-03  Neal H Walfield  <neal@cs.uml.edu>
+
+	* Makefile: Make no assumptions about the make that the user
+	will be using.
+	* xgethostname.c (xgethostname): Be explicitly sure that the
+	returned buffer is NULL terminated.
+	Always compile in the while (err = ENAMETOOLONG) loop; this
+	may help to catch a very exotic implementation and does not
+	hurt anyone.
+
+2001-08-02  Neal H Walfield  <neal@cs.uml.edu>
+
+	* Release version 20010802.
+
+2001-08-01  Neal H Walfield  <neal@cs.uml.edu>
+
+	* Makefile: New file.
+	* README: New file.
+	* xgethostname.c (xgethostname) [!_SC_HOST_NAME_MAX]: Should
+	not be subject to this condition.
+	(xgethostname): If the system defines a limit < 256 and > 0,
+	do not artificially inflate it, rather, respect it.
+
+2001-07-30  Neal H Walfield  <neal@cs.uml.edu>
+
+	* REAMME: Move from here . . .
+	* xgethostname.2: to here.
+	Format it for man.
+
+2001-07-30  Neal H Walfield  <neal@cs.uml.edu>
+
+	* README: Fix grammer error.
+	* xgethostname.c: Likewise.
+	* xgethostname.h: Likewise.
+
+	* Makefile: New file.
+
+2001-07-29  Neal H Walfield  <neal@cs.uml.edu>
+
+	* Release version 20010729.
+
+	* AUTHOR: New file.
+	* ChangeLog: New file.
+	* COPYING: New file.
+	* README: New file.
+	* VERSION: New file.
+	* xgethostname.c: New file.
+	* xgethostname.h: New file.
diff --git a/xgethostname/Makefile b/xgethostname/Makefile
new file mode 100644
index 0000000..7ad8002
--- /dev/null
+++ b/xgethostname/Makefile
@@ -0,0 +1,11 @@
+targets=xgethostname
+
+.PHONY: all
+all: $(targets)
+
+xgethostname: xgethostname.c
+	$(CC) -DWANT_TO_TEST_XGETHOSTNAME $(CFLAGS) -o xgethostname xgethostname.c
+
+.PHONY: clean
+clean:
+	rm -rf $(targets)
diff --git a/xgethostname/README b/xgethostname/README
new file mode 100644
index 0000000..85d9779
--- /dev/null
+++ b/xgethostname/README
@@ -0,0 +1 @@
+A wrapper for gethostname that automatically checks system limitations.
diff --git a/xgethostname/VERSION b/xgethostname/VERSION
new file mode 100644
index 0000000..89ff365
--- /dev/null
+++ b/xgethostname/VERSION
@@ -0,0 +1,5 @@
+Version:
+April 13, 2002
+
+Latest version can always be found at:
+ftp://walfield.org/pub/people/neal/xgethostname/xgethostname-latest.tar.gz
diff --git a/xgethostname/xgethostname.2 b/xgethostname/xgethostname.2
new file mode 100644
index 0000000..3f26212
--- /dev/null
+++ b/xgethostname/xgethostname.2
@@ -0,0 +1,37 @@
+.TH XGETHOSTNAME 2 "30 July 2001" "xgethostname"
+.SH NAME
+xgethostname \- get the host name.
+.SH
+SYNOPSIS
+.BI "char *xgethostname (void);"
+.SH DESCRIPTION
+The xhostname function is intended to replace
+.BR gethostname(2),
+a function used to access the host name.  The old interface is
+inflexable given that it assumes the existance of the
+.BR MAXHOSTNAMELEN
+macro, which neither
+.BR POSIX
+nor the proposed
+.BR "Single Unix Specification version 3"
+guarantee to be defined.
+.SH RETURN VALUE
+On success, a malloced, null terminated (possibly truncated)
+string containing the host name is returned.  On failure,
+.I NULL
+is returned and errno is set.
+.SH ERRORS
+.TP
+.BR ENOMEM
+Failed to allocate memory.
+.LP
+As 
+.BR xgethostname
+is really just a wrapper around your systems
+.BR gethostname (2),
+see that man page for additional details.
+.SH "SEE ALSO"
+.BR gethostname (2)
+.SH AUTHOR
+.BR xgethostname
+was written by Neal H Walfield  <neal@cs.uml.edu>.
diff --git a/xgethostname/xgethostname.c b/xgethostname/xgethostname.c
new file mode 100644
index 0000000..b7ed5ec
--- /dev/null
+++ b/xgethostname/xgethostname.c
@@ -0,0 +1,125 @@
+/* Copyright (c) 2001 Neal H Walfield <neal@cs.uml.edu>.
+   
+   This file is placed into the public domain.  Its distribution
+   is unlimited.
+
+   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
+   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+   IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
+   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* NAME
+
+	xgethostname - get the host name.
+
+   SYNOPSIS
+
+   	char *xgethostname (void);
+
+   DESCRIPTION
+
+	The xhostname function is intended to replace gethostname(2), a
+	function used to access the host name.  The old interface is
+	inflexable given that it assumes the existance of the
+	MAXHOSTNAMELEN macro, which neither POSIX nor the proposed
+	Single Unix Specification version 3 guarantee to be defined.
+
+   RETURN VALUE
+
+	On success, a malloced, null terminated (possibly truncated)
+	string containing the host name is returned.  On failure,
+	NULL is returned and errno is set.
+ */
+
+#include <sys/param.h>	/* For MAXHOSTNAMELEN */
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+
+char *
+xgethostname (void)
+{
+  int size = 0;
+  int addnull = 0;
+  char *buf;
+  int err;
+
+#ifdef MAXHOSTNAMELEN
+  size = MAXHOSTNAMELEN;
+  addnull = 1;
+#else /* MAXHOSTNAMELEN */
+#ifdef _SC_HOST_NAME_MAX
+  size = sysconf (_SC_HOST_NAME_MAX);
+  addnull = 1;
+#endif /* _SC_HOST_NAME_MAX */
+  if (size <= 0)
+    size = 256;
+#endif /* MAXHOSTNAMELEN */
+
+  buf = malloc (size + addnull);
+  if (! buf)
+    {
+      errno = ENOMEM;
+      return NULL;
+    }
+
+  err = gethostname (buf, size);
+  while (err == -1 && errno == ENAMETOOLONG)
+    {
+      free (buf);
+
+      size *= 2;
+      buf = malloc (size + addnull);
+      if (! buf)
+	{
+	  errno = ENOMEM;
+	  return NULL;
+	}
+      
+      err = gethostname (buf, size);
+    }
+
+  if (err)
+    {
+      if (buf)
+        free (buf);
+      errno = err;
+      return NULL;
+    }
+
+  if (addnull)
+    buf[size] = '\0';
+
+  return buf;
+}
+
+#ifdef WANT_TO_TEST_XGETHOSTNAME
+#include <stdio.h>
+#include <string.h>
+
+int
+main (int argc, char *argv[])
+{
+  char *hostname;
+
+  hostname = xgethostname ();
+  if (! hostname)
+    {
+      perror ("xgethostname");
+      return 1;
+    }
+
+  printf ("%s\n", hostname);
+  free (hostname);
+
+  return 0;
+}
+#endif /* WANT_TO_TEST_XGETHOSTNAME */
diff --git a/xgethostname/xgethostname.h b/xgethostname/xgethostname.h
new file mode 100644
index 0000000..1734746
--- /dev/null
+++ b/xgethostname/xgethostname.h
@@ -0,0 +1,48 @@
+/* Copyright (c) 2001 Neal H Walfield <neal@cs.uml.edu>.
+   
+   This file is placed into the public domain.  Its distribution
+   is unlimited.
+
+   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
+   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+   IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
+   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* NAME
+
+	xgethostname - get the host name.
+
+   SYNOPSIS
+
+   	char *xgethostname (void);
+
+   DESCRIPTION
+
+	The xhostname function is intended to replace gethostname(2), a
+	function used to access the host name.  The old interface is
+	inflexable given that it assumes the existance of the
+	MAXHOSTNAMELEN macro, which neither POSIX nor the proposed
+	Single Unix Specification version 3 guarantee to be defined.
+
+   RETURN VALUE
+
+	On success, a malloced, null terminated (possibly truncated)
+	string containing the host name is returned.  On failure,
+	NULL is returned and errno is set.
+ */
+
+#ifndef XGETHOSTNAME
+#define XGETHOSTNAME
+
+char * xgethostname (void);
+
+#endif /* XGETHOSTNAME */
+
