446cf2
commit bdee910e88006ae33dc83ac3d2c0708adb6627d0
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Wed Jul 15 13:41:31 2020 +0200
446cf2
446cf2
    nss: Add __nss_fgetent_r
446cf2
    
446cf2
    And helper functions __nss_readline, __nss_readline_seek,
446cf2
     __nss_parse_line_result.
446cf2
    
446cf2
    This consolidates common code for handling overlong lines and
446cf2
    parse files.  Use the new functionality in internal_getent
446cf2
    in nss/nss_files/files-XXX.c.
446cf2
    
446cf2
    Tested-by: Carlos O'Donell <carlos@redhat.com>
446cf2
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
446cf2
446cf2
diff -rupN a/include/nss_files.h b/include/nss_files.h
446cf2
--- a/include/nss_files.h	2020-09-14 17:48:49.353699306 -0400
446cf2
+++ b/include/nss_files.h	2020-09-14 17:55:21.856488740 -0400
446cf2
@@ -25,6 +25,28 @@
446cf2
 FILE *__nss_files_fopen (const char *path);
446cf2
 libc_hidden_proto (__nss_files_fopen)
446cf2
 
446cf2
+/* Read a line from FP, storing it BUF.  Strip leading blanks and skip
446cf2
+   comments.  Sets errno and returns error code on failure.  Special
446cf2
+   failure: ERANGE means the buffer is too small.  The function writes
446cf2
+   the original offset to *POFFSET (which can be negative in the case
446cf2
+   of non-seekable input).  */
446cf2
+int __nss_readline (FILE *fp, char *buf, size_t len, off64_t *poffset);
446cf2
+libc_hidden_proto (__nss_readline)
446cf2
+
446cf2
+/* Seek FP to OFFSET.  Sets errno and returns error code on failure.
446cf2
+   On success, sets errno to ERANGE and returns ERANGE (to indicate
446cf2
+   re-reading of the same input line to the caller).  If OFFSET is
446cf2
+   negative, fail with ESPIPE without seeking.  Intended to be used
446cf2
+   after parsing data read by __nss_readline failed with ERANGE.  */
446cf2
+int __nss_readline_seek (FILE *fp, off64_t offset) attribute_hidden;
446cf2
+
446cf2
+/* Handles the result of a parse_line call (as defined by
446cf2
+   nss/nss_files/files-parse.c).  Adjusts the file offset of FP as
446cf2
+   necessary.  Returns 0 on success, and updates errno on failure (and
446cf2
+   returns that error code).  */
446cf2
+int __nss_parse_line_result (FILE *fp, off64_t offset, int parse_line_result);
446cf2
+libc_hidden_proto (__nss_parse_line_result)
446cf2
+
446cf2
 struct parser_data;
446cf2
 
446cf2
 /* Instances of the parse_line function from
446cf2
@@ -52,4 +74,11 @@ libnss_files_hidden_proto (_nss_files_pa
446cf2
 libc_hidden_proto (_nss_files_parse_sgent)
446cf2
 libc_hidden_proto (_nss_files_parse_spent)
446cf2
 
446cf2
+/* Generic implementation of fget*ent_r.  Reads lines from FP until
446cf2
+   EOF or a successful parse into *RESULT using PARSER.  Returns 0 on
446cf2
+   success, ENOENT on EOF, ERANGE on too-small buffer.  */
446cf2
+int __nss_fgetent_r (FILE *fp, void *result,
446cf2
+                     char *buffer, size_t buffer_length,
446cf2
+                     nss_files_parse_line parser) attribute_hidden;
446cf2
+
446cf2
 #endif /* _NSS_FILES_H */
446cf2
diff -rupN a/nss/Makefile b/nss/Makefile
446cf2
--- a/nss/Makefile	2020-09-14 17:48:49.293697045 -0400
446cf2
+++ b/nss/Makefile	2020-09-14 17:55:21.860488891 -0400
446cf2
@@ -28,7 +28,9 @@ headers			:= nss.h
446cf2
 routines		= nsswitch getnssent getnssent_r digits_dots \
446cf2
 			  valid_field valid_list_field rewrite_field \
446cf2
 			  $(addsuffix -lookup,$(databases)) \
446cf2
-			  compat-lookup nss_hash nss_files_fopen
446cf2
+			  compat-lookup nss_hash nss_files_fopen \
446cf2
+			  nss_readline nss_parse_line_result \
446cf2
+			  nss_fgetent_r
446cf2
 
446cf2
 # These are the databases that go through nss dispatch.
446cf2
 # Caution: if you add a database here, you must add its real name
446cf2
diff -rupN a/nss/Versions b/nss/Versions
446cf2
--- a/nss/Versions	2020-09-14 17:48:49.294697083 -0400
446cf2
+++ b/nss/Versions	2020-09-14 17:55:21.867489155 -0400
446cf2
@@ -21,7 +21,7 @@ libc {
446cf2
     __nss_passwd_lookup2; __nss_group_lookup2; __nss_hosts_lookup2;
446cf2
     __nss_services_lookup2; __nss_next2; __nss_lookup;
446cf2
     __nss_hash; __nss_database_lookup2;
446cf2
-    __nss_files_fopen;
446cf2
+    __nss_files_fopen; __nss_readline; __nss_parse_line_result;
446cf2
   }
446cf2
 }
446cf2
 
446cf2
diff -rupN a/nss/nss_fgetent_r.c b/nss/nss_fgetent_r.c
446cf2
--- a/nss/nss_fgetent_r.c	1969-12-31 19:00:00.000000000 -0500
446cf2
+++ b/nss/nss_fgetent_r.c	2020-09-14 17:55:21.870489268 -0400
446cf2
@@ -0,0 +1,55 @@
446cf2
+/* Generic implementation of fget*ent_r.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <errno.h>
446cf2
+#include <nss_files.h>
446cf2
+
446cf2
+int
446cf2
+__nss_fgetent_r (FILE *fp, void *result, char *buffer, size_t buffer_length,
446cf2
+                 nss_files_parse_line parser)
446cf2
+{
446cf2
+  int ret;
446cf2
+
446cf2
+  _IO_flockfile (fp);
446cf2
+
446cf2
+  while (true)
446cf2
+    {
446cf2
+      off64_t original_offset;
446cf2
+      ret = __nss_readline (fp, buffer, buffer_length, &original_offset);
446cf2
+      if (ret == 0)
446cf2
+        {
446cf2
+          /* Parse the line into *RESULT.  */
446cf2
+          ret = parser (buffer, result,
446cf2
+                        (struct parser_data *) buffer, buffer_length, &errno);
446cf2
+
446cf2
+          /* Translate the result code from the parser into an errno
446cf2
+             value.  Also seeks back to the start of the line if
446cf2
+             necessary.  */
446cf2
+          ret = __nss_parse_line_result (fp, original_offset, ret);
446cf2
+
446cf2
+          if (ret == EINVAL)
446cf2
+            /* Skip over malformed lines.  */
446cf2
+            continue;
446cf2
+        }
446cf2
+      break;
446cf2
+    }
446cf2
+
446cf2
+  _IO_funlockfile (fp);
446cf2
+
446cf2
+  return ret;
446cf2
+}
446cf2
diff -rupN a/nss/nss_files/files-XXX.c b/nss/nss_files/files-XXX.c
446cf2
--- a/nss/nss_files/files-XXX.c	2020-09-14 17:48:49.296697158 -0400
446cf2
+++ b/nss/nss_files/files-XXX.c	2020-09-14 17:55:21.878489569 -0400
446cf2
@@ -135,10 +135,9 @@ internal_getent (FILE *stream, struct ST
446cf2
 		 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO
446cf2
 		 EXTRA_ARGS_DECL)
446cf2
 {
446cf2
-  char *p;
446cf2
   struct parser_data *data = (void *) buffer;
446cf2
   size_t linebuflen = buffer + buflen - data->linebuffer;
446cf2
-  int parse_result;
446cf2
+  int saved_errno = errno;	/* Do not clobber errno on success.  */
446cf2
 
446cf2
   if (buflen < sizeof *data + 2)
446cf2
     {
446cf2
@@ -149,66 +148,42 @@ internal_getent (FILE *stream, struct ST
446cf2
 
446cf2
   while (true)
446cf2
     {
446cf2
-      ssize_t r = __libc_readline_unlocked
446cf2
-	(stream, data->linebuffer, linebuflen);
446cf2
-      if (r < 0)
446cf2
-	{
446cf2
-	  *errnop = errno;
446cf2
-	  H_ERRNO_SET (NETDB_INTERNAL);
446cf2
-	  if (*errnop == ERANGE)
446cf2
-	    /* Request larger buffer.  */
446cf2
-	    return NSS_STATUS_TRYAGAIN;
446cf2
-	  else
446cf2
-	    /* Other read failure.  */
446cf2
-	    return NSS_STATUS_UNAVAIL;
446cf2
-	}
446cf2
-      else if (r == 0)
446cf2
+      off64_t original_offset;
446cf2
+      int ret = __nss_readline (stream, data->linebuffer, linebuflen,
446cf2
+				&original_offset);
446cf2
+      if (ret == ENOENT)
446cf2
 	{
446cf2
 	  /* End of file.  */
446cf2
 	  H_ERRNO_SET (HOST_NOT_FOUND);
446cf2
+	  __set_errno (saved_errno);
446cf2
 	  return NSS_STATUS_NOTFOUND;
446cf2
 	}
446cf2
-
446cf2
-      /* Everything OK.  Now skip leading blanks.  */
446cf2
-      p = data->linebuffer;
446cf2
-      while (isspace (*p))
446cf2
-	++p;
446cf2
-
446cf2
-      /* Ignore empty and comment lines.  */
446cf2
-      if (*p == '\0' || *p == '#')
446cf2
-	continue;
446cf2
-
446cf2
-      /* Parse the line.   */
446cf2
-      *errnop = EINVAL;
446cf2
-      parse_result = parse_line (p, result, data, buflen, errnop EXTRA_ARGS);
446cf2
-
446cf2
-      if (parse_result == -1)
446cf2
+      else if (ret == 0)
446cf2
 	{
446cf2
-	  if (*errnop == ERANGE)
446cf2
+	  ret = __nss_parse_line_result (stream, original_offset,
446cf2
+					 parse_line (data->linebuffer,
446cf2
+						     result, data, buflen,
446cf2
+						     errnop EXTRA_ARGS));
446cf2
+	  if (ret == 0)
446cf2
 	    {
446cf2
-	      /* Return to the original file position at the beginning
446cf2
-		 of the line, so that the next call can read it again
446cf2
-		 if necessary.  */
446cf2
-	      if (__fseeko64 (stream, -r, SEEK_CUR) != 0)
446cf2
-		{
446cf2
-		  if (errno == ERANGE)
446cf2
-		    *errnop = EINVAL;
446cf2
-		  else
446cf2
-		    *errnop = errno;
446cf2
-		  H_ERRNO_SET (NETDB_INTERNAL);
446cf2
-		  return NSS_STATUS_UNAVAIL;
446cf2
-		}
446cf2
+	      /* Line has been parsed successfully.  */
446cf2
+	      __set_errno (saved_errno);
446cf2
+	      return NSS_STATUS_SUCCESS;
446cf2
 	    }
446cf2
-	  H_ERRNO_SET (NETDB_INTERNAL);
446cf2
-	  return NSS_STATUS_TRYAGAIN;
446cf2
+	  else if (ret == EINVAL)
446cf2
+	    /* If it is invalid, loop to get the next line of the file
446cf2
+	       to parse.  */
446cf2
+	    continue;
446cf2
 	}
446cf2
 
446cf2
-      /* Return the data if parsed successfully.  */
446cf2
-      if (parse_result != 0)
446cf2
-	return NSS_STATUS_SUCCESS;
446cf2
-
446cf2
-      /* If it is invalid, loop to get the next line of the file to
446cf2
-	 parse.  */
446cf2
+      *errnop = ret;
446cf2
+      H_ERRNO_SET (NETDB_INTERNAL);
446cf2
+      if (ret == ERANGE)
446cf2
+	/* Request larger buffer.  */
446cf2
+	return NSS_STATUS_TRYAGAIN;
446cf2
+      else
446cf2
+	/* Other read failure.  */
446cf2
+	return NSS_STATUS_UNAVAIL;
446cf2
     }
446cf2
 }
446cf2
 
446cf2
diff -rupN a/nss/nss_parse_line_result.c b/nss/nss_parse_line_result.c
446cf2
--- a/nss/nss_parse_line_result.c	1969-12-31 19:00:00.000000000 -0500
446cf2
+++ b/nss/nss_parse_line_result.c	2020-09-14 17:55:21.880489645 -0400
446cf2
@@ -0,0 +1,46 @@
446cf2
+/* Implementation of __nss_parse_line_result.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <nss_files.h>
446cf2
+
446cf2
+#include <assert.h>
446cf2
+#include <errno.h>
446cf2
+
446cf2
+int
446cf2
+__nss_parse_line_result (FILE *fp, off64_t offset, int parse_line_result)
446cf2
+{
446cf2
+  assert (parse_line_result >= -1 && parse_line_result <= 1);
446cf2
+
446cf2
+  switch (__builtin_expect (parse_line_result, 1))
446cf2
+    {
446cf2
+    case 1:
446cf2
+      /* Sucess.  */
446cf2
+      return 0;
446cf2
+    case 0:
446cf2
+      /* Parse error.  */
446cf2
+      __set_errno (EINVAL);
446cf2
+      return EINVAL;
446cf2
+    case -1:
446cf2
+      /* Out of buffer space.  */
446cf2
+      return __nss_readline_seek (fp, offset);
446cf2
+
446cf2
+      default:
446cf2
+        __builtin_unreachable ();
446cf2
+    }
446cf2
+}
446cf2
+libc_hidden_def (__nss_parse_line_result)
446cf2
diff -rupN a/nss/nss_readline.c b/nss/nss_readline.c
446cf2
--- a/nss/nss_readline.c	1969-12-31 19:00:00.000000000 -0500
446cf2
+++ b/nss/nss_readline.c	2020-09-14 17:55:21.883489758 -0400
446cf2
@@ -0,0 +1,99 @@
446cf2
+/* Read a line from an nss_files database file.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <nss_files.h>
446cf2
+
446cf2
+#include <ctype.h>
446cf2
+#include <errno.h>
446cf2
+#include <string.h>
446cf2
+
446cf2
+int
446cf2
+__nss_readline (FILE *fp, char *buf, size_t len, off64_t *poffset)
446cf2
+{
446cf2
+  /* We need space for at least one character, the line terminator,
446cf2
+     and the NUL byte.  */
446cf2
+  if (len < 3)
446cf2
+    {
446cf2
+      *poffset = -1;
446cf2
+      __set_errno (ERANGE);
446cf2
+      return ERANGE;
446cf2
+    }
446cf2
+
446cf2
+  while (true)
446cf2
+    {
446cf2
+      /* Keep original offset for retries.  */
446cf2
+      *poffset = __ftello64 (fp);
446cf2
+
446cf2
+      buf[len - 1] = '\xff';        /* Marker to recognize truncation.  */
446cf2
+      if (fgets_unlocked (buf, len, fp) == NULL)
446cf2
+        {
446cf2
+          if (feof_unlocked (fp))
446cf2
+            {
446cf2
+              __set_errno (ENOENT);
446cf2
+              return ENOENT;
446cf2
+            }
446cf2
+          else
446cf2
+            {
446cf2
+              /* Any other error.  Do not return ERANGE in this case
446cf2
+                 because the caller would retry.  */
446cf2
+              if (errno == ERANGE)
446cf2
+                __set_errno (EINVAL);
446cf2
+              return errno;
446cf2
+            }
446cf2
+        }
446cf2
+      else if (buf[len - 1] != '\xff')
446cf2
+        /* The buffer is too small.  Arrange for re-reading the same
446cf2
+           line on the next call.  */
446cf2
+        return __nss_readline_seek (fp, *poffset);
446cf2
+
446cf2
+      /* fgets_unlocked succeeded.  */
446cf2
+
446cf2
+      /* Remove leading whitespace.  */
446cf2
+      char *p = buf;
446cf2
+      while (isspace (*p))
446cf2
+        ++p;
446cf2
+      if (*p == '\0' || *p == '#')
446cf2
+        /* Skip empty lines and comments.  */
446cf2
+        continue;
446cf2
+      if (p != buf)
446cf2
+        memmove (buf, p, strlen (p));
446cf2
+
446cf2
+      /* Return line to the caller.  */
446cf2
+      return 0;
446cf2
+    }
446cf2
+}
446cf2
+libc_hidden_def (__nss_readline)
446cf2
+
446cf2
+int
446cf2
+__nss_readline_seek (FILE *fp, off64_t offset)
446cf2
+{
446cf2
+  if (offset < 0 /* __ftello64 failed.  */
446cf2
+      || __fseeko64 (fp, offset, SEEK_SET) < 0)
446cf2
+    {
446cf2
+      /* Without seeking support, it is not possible to
446cf2
+         re-read the same line, so this is a hard failure.  */
446cf2
+      fseterr_unlocked (fp);
446cf2
+      __set_errno (ESPIPE);
446cf2
+      return ESPIPE;
446cf2
+    }
446cf2
+  else
446cf2
+    {
446cf2
+      __set_errno (ERANGE);
446cf2
+      return ERANGE;
446cf2
+    }
446cf2
+}