/* Mail alias file parser in nss_files module.
- Copyright (C) 1996,97,98,99,2002 Free Software Foundation, Inc.
+ Copyright (C) 1996-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
- License along with the GNU C Library; if not, write to the Free
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA. */
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
#include <aliases.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
-#include <bits/libc-lock.h>
+#include <libc-lock.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <kernel-features.h>
+
#include "nsswitch.h"
/* Locks the static variables in this file. */
__libc_lock_define_initialized (static, lock)
\f
-/* Maintenance of the shared stream open on the database file. */
+/* Maintenance of the stream open on the database file. For getXXent
+ operations the stream needs to be held open across calls, the other
+ getXXbyYY operations all use their own stream. */
static FILE *stream;
-static fpos_t position;
-static enum { nouse, getent, getby } last_use;
static enum nss_status
-internal_setent (void)
+internal_setent (FILE **stream)
{
enum nss_status status = NSS_STATUS_SUCCESS;
- if (stream == NULL)
+ if (*stream == NULL)
{
- stream = fopen ("/etc/aliases", "r");
+ *stream = fopen ("/etc/aliases", "rce");
- if (stream == NULL)
+ if (*stream == NULL)
status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
- else
- {
- /* We have to make sure the file is `closed on exec'. */
- int result, flags;
-
- result = flags = fcntl (fileno (stream), F_GETFD, 0);
- if (result >= 0)
- {
- flags |= FD_CLOEXEC;
- result = fcntl (fileno (stream), F_SETFD, flags);
- }
- if (result < 0)
- {
- /* Something went wrong. Close the stream and return a
- failure. */
- fclose (stream);
- stream = NULL;
- status = NSS_STATUS_UNAVAIL;
- }
- }
}
else
- rewind (stream);
+ rewind (*stream);
return status;
}
__libc_lock_lock (lock);
- status = internal_setent ();
-
- if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
- {
- fclose (stream);
- stream = NULL;
- status = NSS_STATUS_UNAVAIL;
- }
-
- last_use = getent;
+ status = internal_setent (&stream);
__libc_lock_unlock (lock);
/* Close the database file. */
static void
-internal_endent (void)
+internal_endent (FILE **stream)
{
- if (stream != NULL)
+ if (*stream != NULL)
{
- fclose (stream);
- stream = NULL;
+ fclose (*stream);
+ *stream = NULL;
}
}
{
__libc_lock_lock (lock);
- internal_endent ();
+ internal_endent (&stream);
__libc_lock_unlock (lock);
\f
/* Parsing the database file into `struct aliasent' data structures. */
static enum nss_status
-get_next_alias (const char *match, struct aliasent *result,
+get_next_alias (FILE *stream, const char *match, struct aliasent *result,
char *buffer, size_t buflen, int *errnop)
{
enum nss_status status = NSS_STATUS_NOTFOUND;
char *line;
/* Check whether the buffer is large enough for even trying to
- read something. */
+ read something. */
if (room_left < 2)
goto no_more_room;
first_unused = cp;
- listfile = fopen (&cp[9], "r");
+ listfile = fopen (&cp[9], "rce");
/* If the file does not exist we simply ignore
the statement. */
if (listfile != NULL
&& (old_line = strdup (line)) != NULL)
{
- while (! feof (listfile))
+ while (! feof_unlocked (listfile))
{
+ if (room_left < 2)
+ {
+ free (old_line);
+ fclose (listfile);
+ goto no_more_room;
+ }
+
first_unused[room_left - 1] = '\xff';
line = fgets_unlocked (first_unused, room_left,
listfile);
if (first_unused[room_left - 1] != '\xff')
{
free (old_line);
+ fclose (listfile);
goto no_more_room;
}
+ __alignof__ (char *)))
{
free (old_line);
+ fclose (listfile);
goto no_more_room;
}
room_left -= ((first_unused - cp)
first_unused[room_left - 1] = '\0';
strncpy (first_unused, old_line, room_left);
- if (old_line != NULL)
- free (old_line);
+ free (old_line);
+ line = first_unused;
if (first_unused[room_left - 1] != '\0')
goto no_more_room;
just read character. */
int ch;
- ch = fgetc (stream);
+ ch = fgetc_unlocked (stream);
if (ch == EOF || ch == '\n' || !isspace (ch))
{
size_t cnt;
can be ignored. */
first_unused[room_left - 1] = '\xff';
line = fgets_unlocked (first_unused, room_left, stream);
+ if (line == NULL)
+ {
+ /* Continuation line without any data and
+ without a newline at the end. Treat it as an
+ empty line and retry, reaching EOF once
+ more. */
+ line = first_unused;
+ *line = '\0';
+ continue;
+ }
if (first_unused[room_left - 1] != '\xff')
goto no_more_room;
cp = strpbrk (line, "#\n");
/* Be prepared that the set*ent function was not called before. */
if (stream == NULL)
- status = internal_setent ();
+ status = internal_setent (&stream);
if (status == NSS_STATUS_SUCCESS)
{
- /* If the last use was not by the getent function we need the
- position the stream. */
- if (last_use != getent)
- {
- if (fsetpos (stream, &position) < 0)
- status = NSS_STATUS_UNAVAIL;
- else
- last_use = getent;
- }
+ result->alias_local = 1;
- if (status == NSS_STATUS_SUCCESS)
- {
- result->alias_local = 1;
-
- /* Read lines until we get a definite result. */
- do
- status = get_next_alias (NULL, result, buffer, buflen, errnop);
- while (status == NSS_STATUS_RETURN);
-
- /* If we successfully read an entry remember this position. */
- if (status == NSS_STATUS_SUCCESS)
- fgetpos (stream, &position);
- else
- last_use = nouse;
- }
+ /* Read lines until we get a definite result. */
+ do
+ status = get_next_alias (stream, NULL, result, buffer, buflen, errnop);
+ while (status == NSS_STATUS_RETURN);
}
__libc_lock_unlock (lock);
{
/* Return next entry in host file. */
enum nss_status status = NSS_STATUS_SUCCESS;
+ FILE *stream = NULL;
if (name == NULL)
{
return NSS_STATUS_UNAVAIL;
}
- __libc_lock_lock (lock);
-
- /* Open the stream or rest it. */
- status = internal_setent ();
- last_use = getby;
+ /* Open the stream. */
+ status = internal_setent (&stream);
if (status == NSS_STATUS_SUCCESS)
{
/* Read lines until we get a definite result. */
do
- status = get_next_alias (name, result, buffer, buflen, errnop);
+ status = get_next_alias (stream, name, result, buffer, buflen, errnop);
while (status == NSS_STATUS_RETURN);
}
- internal_endent ();
-
- __libc_lock_unlock (lock);
+ internal_endent (&stream);
return status;
}