786673
From 299210c1fa67e2dfb564475986fce11cd33db9ad Mon Sep 17 00:00:00 2001
786673
From: Florian Weimer <fweimer@redhat.com>
786673
Date: Thu, 16 Jul 2020 16:12:46 +0200
786673
Subject: [PATCH 01/11] nss_files: Consolidate file opening in
786673
 __nss_files_fopen
786673
786673
Tested-by: Carlos O'Donell <carlos@redhat.com>
786673
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
786673
---
786673
 include/nss_files.h              | 28 ++++++++++++++++++++++++
786673
 nss/Makefile                     |  2 +-
786673
 nss/Versions                     |  1 +
786673
 nss/nss_files/files-XXX.c        |  3 ++-
786673
 nss/nss_files/files-alias.c      |  5 +++--
786673
 nss/nss_files/files-initgroups.c |  6 ++---
786673
 nss/nss_files/files-netgrp.c     |  5 ++---
786673
 nss/nss_files_fopen.c            | 47 ++++++++++++++++++++++++++++++++++++++++
786673
 8 files changed, 86 insertions(+), 11 deletions(-)
786673
 create mode 100644 include/nss_files.h
786673
 create mode 100644 nss/nss_files_fopen.c
786673
786673
diff -rupN a/include/nss_files.h b/include/nss_files.h
786673
--- a/include/nss_files.h	1969-12-31 19:00:00.000000000 -0500
786673
+++ b/include/nss_files.h	2020-09-11 21:28:42.027034988 -0400
786673
@@ -0,0 +1,28 @@
786673
+/* Internal routines for nss_files.
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
+#ifndef _NSS_FILES_H
786673
+#define _NSS_FILES_H
786673
+
786673
+#include <stdio.h>
786673
+
786673
+/* Open PATH for reading, as a data source for nss_files.  */
786673
+FILE *__nss_files_fopen (const char *path);
786673
+libc_hidden_proto (__nss_files_fopen)
786673
+
786673
+#endif /* _NSS_FILES_H */
786673
diff -rupN a/nss/Makefile b/nss/Makefile
786673
--- a/nss/Makefile	2020-09-11 21:24:05.569544894 -0400
786673
+++ b/nss/Makefile	2020-09-11 21:28:42.035035292 -0400
786673
@@ -28,7 +28,7 @@ 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
786673
+			  compat-lookup nss_hash nss_files_fopen
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-11 21:24:04.852517683 -0400
786673
+++ b/nss/Versions	2020-09-11 21:28:42.041035519 -0400
786673
@@ -21,6 +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
   }
786673
 }
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	2018-08-01 01:10:47.000000000 -0400
786673
+++ b/nss/nss_files/files-XXX.c	2020-09-11 21:28:42.049035823 -0400
786673
@@ -22,6 +22,7 @@
786673
 #include <fcntl.h>
786673
 #include <libc-lock.h>
786673
 #include "nsswitch.h"
786673
+#include <nss_files.h>
786673
 
786673
 #include <kernel-features.h>
786673
 
786673
@@ -74,7 +75,7 @@ internal_setent (FILE **stream)
786673
 
786673
   if (*stream == NULL)
786673
     {
786673
-      *stream = fopen (DATAFILE, "rce");
786673
+      *stream = __nss_files_fopen (DATAFILE);
786673
 
786673
       if (*stream == NULL)
786673
 	status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
786673
diff -rupN a/nss/nss_files/files-alias.c b/nss/nss_files/files-alias.c
786673
--- a/nss/nss_files/files-alias.c	2020-09-11 21:24:02.004409596 -0400
786673
+++ b/nss/nss_files/files-alias.c	2020-09-11 21:28:42.055036051 -0400
786673
@@ -29,6 +29,7 @@
786673
 #include <kernel-features.h>
786673
 
786673
 #include "nsswitch.h"
786673
+#include <nss_files.h>
786673
 
786673
 /* Locks the static variables in this file.  */
786673
 __libc_lock_define_initialized (static, lock)
786673
@@ -47,7 +48,7 @@ internal_setent (FILE **stream)
786673
 
786673
   if (*stream == NULL)
786673
     {
786673
-      *stream = fopen ("/etc/aliases", "rce");
786673
+      *stream = __nss_files_fopen ("/etc/aliases");
786673
 
786673
       if (*stream == NULL)
786673
 	status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
786673
@@ -213,7 +214,7 @@ get_next_alias (FILE *stream, const char
786673
 
786673
 		      first_unused = cp;
786673
 
786673
-		      listfile = fopen (&cp[9], "rce");
786673
+		      listfile = __nss_files_fopen (&cp[9]);
786673
 		      /* If the file does not exist we simply ignore
786673
 			 the statement.  */
786673
 		      if (listfile != NULL
786673
diff -rupN a/nss/nss_files/files-initgroups.c b/nss/nss_files/files-initgroups.c
786673
--- a/nss/nss_files/files-initgroups.c	2018-08-01 01:10:47.000000000 -0400
786673
+++ b/nss/nss_files/files-initgroups.c	2020-09-11 22:08:25.130740010 -0400
786673
@@ -25,22 +25,20 @@
786673
 #include <stdbool.h>
786673
 #include <stdlib.h>
786673
 #include <scratch_buffer.h>
786673
+#include <nss_files.h>
786673
 
786673
 enum nss_status
786673
 _nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
786673
 			   long int *size, gid_t **groupsp, long int limit,
786673
 			   int *errnop)
786673
 {
786673
-  FILE *stream = fopen ("/etc/group", "rce");
786673
+  FILE *stream = __nss_files_fopen ("/etc/group");
786673
   if (stream == NULL)
786673
     {
786673
       *errnop = errno;
786673
       return *errnop == ENOMEM ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
786673
     }
786673
 
786673
-  /* No other thread using this stream.  */
786673
-  __fsetlocking (stream, FSETLOCKING_BYCALLER);
786673
-
786673
   char *line = NULL;
786673
   size_t linelen = 0;
786673
   enum nss_status status = NSS_STATUS_SUCCESS;
786673
diff -rupN a/nss/nss_files/files-netgrp.c b/nss/nss_files/files-netgrp.c
786673
--- a/nss/nss_files/files-netgrp.c	2018-08-01 01:10:47.000000000 -0400
786673
+++ b/nss/nss_files/files-netgrp.c	2020-09-11 21:28:42.068036544 -0400
786673
@@ -26,6 +26,7 @@
786673
 #include <string.h>
786673
 #include "nsswitch.h"
786673
 #include "netgroup.h"
786673
+#include <nss_files.h>
786673
 
786673
 #define DATAFILE	"/etc/netgroup"
786673
 
786673
@@ -62,7 +63,7 @@ _nss_files_setnetgrent (const char *grou
786673
     return NSS_STATUS_UNAVAIL;
786673
 
786673
   /* Find the netgroups file and open it.  */
786673
-  fp = fopen (DATAFILE, "rce");
786673
+  fp = __nss_files_fopen (DATAFILE);
786673
   if (fp == NULL)
786673
     status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
786673
   else
786673
@@ -76,8 +77,6 @@ _nss_files_setnetgrent (const char *grou
786673
       status = NSS_STATUS_NOTFOUND;
786673
       result->cursor = result->data;
786673
 
786673
-      __fsetlocking (fp, FSETLOCKING_BYCALLER);
786673
-
786673
       while (!feof_unlocked (fp))
786673
 	{
786673
 	  ssize_t curlen = getline (&line, &line_len, fp);
786673
diff -rupN a/nss/nss_files_fopen.c b/nss/nss_files_fopen.c
786673
--- a/nss/nss_files_fopen.c	1969-12-31 19:00:00.000000000 -0500
786673
+++ b/nss/nss_files_fopen.c	2020-09-11 21:28:42.074036771 -0400
786673
@@ -0,0 +1,47 @@
786673
+/* Open 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 <errno.h>
786673
+#include <stdio_ext.h>
786673
+
786673
+FILE *
786673
+__nss_files_fopen (const char *path)
786673
+{
786673
+  FILE *fp = fopen (path, "rce");
786673
+  if (fp == NULL)
786673
+    return NULL;
786673
+
786673
+  /* The stream is not shared across threads.  */
786673
+  __fsetlocking (fp, FSETLOCKING_BYCALLER);
786673
+
786673
+  /* This tells libio that the file is seekable, and that fp->_offset
786673
+     is correct, ensuring that __ftello64 is efficient (bug 26257).  */
786673
+  if (__fseeko64 (fp, 0, SEEK_SET) < 0)
786673
+    {
786673
+      /* nss_files requires seekable files, to deal with repeated
786673
+         reads of the same line after reporting ERANGE.  */
786673
+      fclose (fp);
786673
+      __set_errno (ESPIPE);
786673
+      return NULL;
786673
+    }
786673
+
786673
+  return fp;
786673
+}
786673
+libc_hidden_def (__nss_files_fopen)