|
|
e354a5 |
From 299210c1fa67e2dfb564475986fce11cd33db9ad Mon Sep 17 00:00:00 2001
|
|
|
e354a5 |
From: Florian Weimer <fweimer@redhat.com>
|
|
|
e354a5 |
Date: Thu, 16 Jul 2020 16:12:46 +0200
|
|
|
e354a5 |
Subject: [PATCH 01/11] nss_files: Consolidate file opening in
|
|
|
e354a5 |
__nss_files_fopen
|
|
|
e354a5 |
|
|
|
e354a5 |
Tested-by: Carlos O'Donell <carlos@redhat.com>
|
|
|
e354a5 |
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
|
|
|
e354a5 |
---
|
|
|
e354a5 |
include/nss_files.h | 28 ++++++++++++++++++++++++
|
|
|
e354a5 |
nss/Makefile | 2 +-
|
|
|
e354a5 |
nss/Versions | 1 +
|
|
|
e354a5 |
nss/nss_files/files-XXX.c | 3 ++-
|
|
|
e354a5 |
nss/nss_files/files-alias.c | 5 +++--
|
|
|
e354a5 |
nss/nss_files/files-initgroups.c | 6 ++---
|
|
|
e354a5 |
nss/nss_files/files-netgrp.c | 5 ++---
|
|
|
e354a5 |
nss/nss_files_fopen.c | 47 ++++++++++++++++++++++++++++++++++++++++
|
|
|
e354a5 |
8 files changed, 86 insertions(+), 11 deletions(-)
|
|
|
e354a5 |
create mode 100644 include/nss_files.h
|
|
|
e354a5 |
create mode 100644 nss/nss_files_fopen.c
|
|
|
e354a5 |
|
|
|
e354a5 |
diff -rupN a/include/nss_files.h b/include/nss_files.h
|
|
|
e354a5 |
--- a/include/nss_files.h 1969-12-31 19:00:00.000000000 -0500
|
|
|
e354a5 |
+++ b/include/nss_files.h 2020-09-11 21:28:42.027034988 -0400
|
|
|
e354a5 |
@@ -0,0 +1,28 @@
|
|
|
e354a5 |
+/* Internal routines for nss_files.
|
|
|
e354a5 |
+ Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
e354a5 |
+ This file is part of the GNU C Library.
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
e354a5 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
e354a5 |
+ License as published by the Free Software Foundation; either
|
|
|
e354a5 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
e354a5 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
e354a5 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
e354a5 |
+ Lesser General Public License for more details.
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
e354a5 |
+ License along with the GNU C Library; if not, see
|
|
|
e354a5 |
+ <https://www.gnu.org/licenses/>. */
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+#ifndef _NSS_FILES_H
|
|
|
e354a5 |
+#define _NSS_FILES_H
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+#include <stdio.h>
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+/* Open PATH for reading, as a data source for nss_files. */
|
|
|
e354a5 |
+FILE *__nss_files_fopen (const char *path);
|
|
|
e354a5 |
+libc_hidden_proto (__nss_files_fopen)
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+#endif /* _NSS_FILES_H */
|
|
|
e354a5 |
diff -rupN a/nss/Makefile b/nss/Makefile
|
|
|
e354a5 |
--- a/nss/Makefile 2020-09-11 21:24:05.569544894 -0400
|
|
|
e354a5 |
+++ b/nss/Makefile 2020-09-11 21:28:42.035035292 -0400
|
|
|
e354a5 |
@@ -28,7 +28,7 @@ headers := nss.h
|
|
|
e354a5 |
routines = nsswitch getnssent getnssent_r digits_dots \
|
|
|
e354a5 |
valid_field valid_list_field rewrite_field \
|
|
|
e354a5 |
$(addsuffix -lookup,$(databases)) \
|
|
|
e354a5 |
- compat-lookup nss_hash
|
|
|
e354a5 |
+ compat-lookup nss_hash nss_files_fopen
|
|
|
e354a5 |
|
|
|
e354a5 |
# These are the databases that go through nss dispatch.
|
|
|
e354a5 |
# Caution: if you add a database here, you must add its real name
|
|
|
e354a5 |
diff -rupN a/nss/Versions b/nss/Versions
|
|
|
e354a5 |
--- a/nss/Versions 2020-09-11 21:24:04.852517683 -0400
|
|
|
e354a5 |
+++ b/nss/Versions 2020-09-11 21:28:42.041035519 -0400
|
|
|
e354a5 |
@@ -21,6 +21,7 @@ libc {
|
|
|
e354a5 |
__nss_passwd_lookup2; __nss_group_lookup2; __nss_hosts_lookup2;
|
|
|
e354a5 |
__nss_services_lookup2; __nss_next2; __nss_lookup;
|
|
|
e354a5 |
__nss_hash; __nss_database_lookup2;
|
|
|
e354a5 |
+ __nss_files_fopen;
|
|
|
e354a5 |
}
|
|
|
e354a5 |
}
|
|
|
e354a5 |
|
|
|
e354a5 |
diff -rupN a/nss/nss_files/files-XXX.c b/nss/nss_files/files-XXX.c
|
|
|
e354a5 |
--- a/nss/nss_files/files-XXX.c 2018-08-01 01:10:47.000000000 -0400
|
|
|
e354a5 |
+++ b/nss/nss_files/files-XXX.c 2020-09-11 21:28:42.049035823 -0400
|
|
|
e354a5 |
@@ -22,6 +22,7 @@
|
|
|
e354a5 |
#include <fcntl.h>
|
|
|
e354a5 |
#include <libc-lock.h>
|
|
|
e354a5 |
#include "nsswitch.h"
|
|
|
e354a5 |
+#include <nss_files.h>
|
|
|
e354a5 |
|
|
|
e354a5 |
#include <kernel-features.h>
|
|
|
e354a5 |
|
|
|
e354a5 |
@@ -74,7 +75,7 @@ internal_setent (FILE **stream)
|
|
|
e354a5 |
|
|
|
e354a5 |
if (*stream == NULL)
|
|
|
e354a5 |
{
|
|
|
e354a5 |
- *stream = fopen (DATAFILE, "rce");
|
|
|
e354a5 |
+ *stream = __nss_files_fopen (DATAFILE);
|
|
|
e354a5 |
|
|
|
e354a5 |
if (*stream == NULL)
|
|
|
e354a5 |
status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
|
|
|
e354a5 |
diff -rupN a/nss/nss_files/files-alias.c b/nss/nss_files/files-alias.c
|
|
|
e354a5 |
--- a/nss/nss_files/files-alias.c 2020-09-11 21:24:02.004409596 -0400
|
|
|
e354a5 |
+++ b/nss/nss_files/files-alias.c 2020-09-11 21:28:42.055036051 -0400
|
|
|
e354a5 |
@@ -29,6 +29,7 @@
|
|
|
e354a5 |
#include <kernel-features.h>
|
|
|
e354a5 |
|
|
|
e354a5 |
#include "nsswitch.h"
|
|
|
e354a5 |
+#include <nss_files.h>
|
|
|
e354a5 |
|
|
|
e354a5 |
/* Locks the static variables in this file. */
|
|
|
e354a5 |
__libc_lock_define_initialized (static, lock)
|
|
|
e354a5 |
@@ -47,7 +48,7 @@ internal_setent (FILE **stream)
|
|
|
e354a5 |
|
|
|
e354a5 |
if (*stream == NULL)
|
|
|
e354a5 |
{
|
|
|
e354a5 |
- *stream = fopen ("/etc/aliases", "rce");
|
|
|
e354a5 |
+ *stream = __nss_files_fopen ("/etc/aliases");
|
|
|
e354a5 |
|
|
|
e354a5 |
if (*stream == NULL)
|
|
|
e354a5 |
status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
|
|
|
e354a5 |
@@ -213,7 +214,7 @@ get_next_alias (FILE *stream, const char
|
|
|
e354a5 |
|
|
|
e354a5 |
first_unused = cp;
|
|
|
e354a5 |
|
|
|
e354a5 |
- listfile = fopen (&cp[9], "rce");
|
|
|
e354a5 |
+ listfile = __nss_files_fopen (&cp[9]);
|
|
|
e354a5 |
/* If the file does not exist we simply ignore
|
|
|
e354a5 |
the statement. */
|
|
|
e354a5 |
if (listfile != NULL
|
|
|
e354a5 |
diff -rupN a/nss/nss_files/files-initgroups.c b/nss/nss_files/files-initgroups.c
|
|
|
e354a5 |
--- a/nss/nss_files/files-initgroups.c 2018-08-01 01:10:47.000000000 -0400
|
|
|
e354a5 |
+++ b/nss/nss_files/files-initgroups.c 2020-09-11 22:08:25.130740010 -0400
|
|
|
e354a5 |
@@ -25,22 +25,20 @@
|
|
|
e354a5 |
#include <stdbool.h>
|
|
|
e354a5 |
#include <stdlib.h>
|
|
|
e354a5 |
#include <scratch_buffer.h>
|
|
|
e354a5 |
+#include <nss_files.h>
|
|
|
e354a5 |
|
|
|
e354a5 |
enum nss_status
|
|
|
e354a5 |
_nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
|
|
|
e354a5 |
long int *size, gid_t **groupsp, long int limit,
|
|
|
e354a5 |
int *errnop)
|
|
|
e354a5 |
{
|
|
|
e354a5 |
- FILE *stream = fopen ("/etc/group", "rce");
|
|
|
e354a5 |
+ FILE *stream = __nss_files_fopen ("/etc/group");
|
|
|
e354a5 |
if (stream == NULL)
|
|
|
e354a5 |
{
|
|
|
e354a5 |
*errnop = errno;
|
|
|
e354a5 |
return *errnop == ENOMEM ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
|
|
|
e354a5 |
}
|
|
|
e354a5 |
|
|
|
e354a5 |
- /* No other thread using this stream. */
|
|
|
e354a5 |
- __fsetlocking (stream, FSETLOCKING_BYCALLER);
|
|
|
e354a5 |
-
|
|
|
e354a5 |
char *line = NULL;
|
|
|
e354a5 |
size_t linelen = 0;
|
|
|
e354a5 |
enum nss_status status = NSS_STATUS_SUCCESS;
|
|
|
e354a5 |
diff -rupN a/nss/nss_files/files-netgrp.c b/nss/nss_files/files-netgrp.c
|
|
|
e354a5 |
--- a/nss/nss_files/files-netgrp.c 2018-08-01 01:10:47.000000000 -0400
|
|
|
e354a5 |
+++ b/nss/nss_files/files-netgrp.c 2020-09-11 21:28:42.068036544 -0400
|
|
|
e354a5 |
@@ -26,6 +26,7 @@
|
|
|
e354a5 |
#include <string.h>
|
|
|
e354a5 |
#include "nsswitch.h"
|
|
|
e354a5 |
#include "netgroup.h"
|
|
|
e354a5 |
+#include <nss_files.h>
|
|
|
e354a5 |
|
|
|
e354a5 |
#define DATAFILE "/etc/netgroup"
|
|
|
e354a5 |
|
|
|
e354a5 |
@@ -62,7 +63,7 @@ _nss_files_setnetgrent (const char *grou
|
|
|
e354a5 |
return NSS_STATUS_UNAVAIL;
|
|
|
e354a5 |
|
|
|
e354a5 |
/* Find the netgroups file and open it. */
|
|
|
e354a5 |
- fp = fopen (DATAFILE, "rce");
|
|
|
e354a5 |
+ fp = __nss_files_fopen (DATAFILE);
|
|
|
e354a5 |
if (fp == NULL)
|
|
|
e354a5 |
status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
|
|
|
e354a5 |
else
|
|
|
e354a5 |
@@ -76,8 +77,6 @@ _nss_files_setnetgrent (const char *grou
|
|
|
e354a5 |
status = NSS_STATUS_NOTFOUND;
|
|
|
e354a5 |
result->cursor = result->data;
|
|
|
e354a5 |
|
|
|
e354a5 |
- __fsetlocking (fp, FSETLOCKING_BYCALLER);
|
|
|
e354a5 |
-
|
|
|
e354a5 |
while (!feof_unlocked (fp))
|
|
|
e354a5 |
{
|
|
|
e354a5 |
ssize_t curlen = getline (&line, &line_len, fp);
|
|
|
e354a5 |
diff -rupN a/nss/nss_files_fopen.c b/nss/nss_files_fopen.c
|
|
|
e354a5 |
--- a/nss/nss_files_fopen.c 1969-12-31 19:00:00.000000000 -0500
|
|
|
e354a5 |
+++ b/nss/nss_files_fopen.c 2020-09-11 21:28:42.074036771 -0400
|
|
|
e354a5 |
@@ -0,0 +1,47 @@
|
|
|
e354a5 |
+/* Open an nss_files database file.
|
|
|
e354a5 |
+ Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
e354a5 |
+ This file is part of the GNU C Library.
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
e354a5 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
e354a5 |
+ License as published by the Free Software Foundation; either
|
|
|
e354a5 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
e354a5 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
e354a5 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
e354a5 |
+ Lesser General Public License for more details.
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
e354a5 |
+ License along with the GNU C Library; if not, see
|
|
|
e354a5 |
+ <https://www.gnu.org/licenses/>. */
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+#include <nss_files.h>
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+#include <errno.h>
|
|
|
e354a5 |
+#include <stdio_ext.h>
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+FILE *
|
|
|
e354a5 |
+__nss_files_fopen (const char *path)
|
|
|
e354a5 |
+{
|
|
|
e354a5 |
+ FILE *fp = fopen (path, "rce");
|
|
|
e354a5 |
+ if (fp == NULL)
|
|
|
e354a5 |
+ return NULL;
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+ /* The stream is not shared across threads. */
|
|
|
e354a5 |
+ __fsetlocking (fp, FSETLOCKING_BYCALLER);
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+ /* This tells libio that the file is seekable, and that fp->_offset
|
|
|
e354a5 |
+ is correct, ensuring that __ftello64 is efficient (bug 26257). */
|
|
|
e354a5 |
+ if (__fseeko64 (fp, 0, SEEK_SET) < 0)
|
|
|
e354a5 |
+ {
|
|
|
e354a5 |
+ /* nss_files requires seekable files, to deal with repeated
|
|
|
e354a5 |
+ reads of the same line after reporting ERANGE. */
|
|
|
e354a5 |
+ fclose (fp);
|
|
|
e354a5 |
+ __set_errno (ESPIPE);
|
|
|
e354a5 |
+ return NULL;
|
|
|
e354a5 |
+ }
|
|
|
e354a5 |
+
|
|
|
e354a5 |
+ return fp;
|
|
|
e354a5 |
+}
|
|
|
e354a5 |
+libc_hidden_def (__nss_files_fopen)
|