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