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