]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - nss/nss_files/files-alias.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nss / nss_files / files-alias.c
index 8e9cd60bb9c6c5942a8d02fbfe1b3493dc35d1ff..2dcbe8f428d126e7ccef593982bfa7de934eee21 100644 (file)
@@ -1,5 +1,5 @@
 /* Mail alias file parser in nss_files module.
-   Copyright (C) 1996-2013 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.
 
 
    You should have received a copy of the GNU Lesser General Public
    License along with the GNU C Library; if not, see
-   <http://www.gnu.org/licenses/>.  */
+   <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>
 /* 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", "rce");
+      *stream = fopen ("/etc/aliases", "rce");
 
-      if (stream == NULL)
+      if (*stream == NULL)
        status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
-      else
-       {
-#if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
-# ifdef O_CLOEXEC
-         if (__have_o_cloexec <= 0)
-# endif
-           {
-             /* We have to make sure the file is  `closed on exec'.  */
-             int result;
-             int flags;
-
-             result = flags = fcntl (fileno (stream), F_GETFD, 0);
-             if (result >= 0)
-               {
-# ifdef O_CLOEXEC
-                 if (__have_o_cloexec == 0)
-                   __have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1;
-                 if (__have_o_cloexec < 0)
-# endif
-                   {
-                     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;
-               }
-           }
-#endif
-       }
     }
   else
-    rewind (stream);
+    rewind (*stream);
 
   return status;
 }
@@ -102,16 +67,7 @@ _nss_files_setaliasent (void)
 
   __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);
 
@@ -121,12 +77,12 @@ _nss_files_setaliasent (void)
 
 /* 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;
     }
 }
 
@@ -137,7 +93,7 @@ _nss_files_endaliasent (void)
 {
   __libc_lock_lock (lock);
 
-  internal_endent ();
+  internal_endent (&stream);
 
   __libc_lock_unlock (lock);
 
@@ -146,7 +102,7 @@ _nss_files_endaliasent (void)
 \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;
@@ -265,6 +221,13 @@ get_next_alias (const char *match, struct aliasent *result,
                        {
                          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);
@@ -273,6 +236,7 @@ get_next_alias (const char *match, struct aliasent *result,
                              if (first_unused[room_left - 1] != '\xff')
                                {
                                  free (old_line);
+                                 fclose (listfile);
                                  goto no_more_room;
                                }
 
@@ -300,6 +264,7 @@ get_next_alias (const char *match, struct aliasent *result,
                                                       + __alignof__ (char *)))
                                        {
                                          free (old_line);
+                                         fclose (listfile);
                                          goto no_more_room;
                                        }
                                      room_left -= ((first_unused - cp)
@@ -368,6 +333,16 @@ get_next_alias (const char *match, struct aliasent *result,
                     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");
@@ -397,35 +372,16 @@ _nss_files_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen,
 
   /* 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);
@@ -440,6 +396,7 @@ _nss_files_getaliasbyname_r (const char *name, struct aliasent *result,
 {
   /* Return next entry in host file.  */
   enum nss_status status = NSS_STATUS_SUCCESS;
+  FILE *stream = NULL;
 
   if (name == NULL)
     {
@@ -447,11 +404,8 @@ _nss_files_getaliasbyname_r (const char *name, struct aliasent *result,
       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)
     {
@@ -459,13 +413,11 @@ _nss_files_getaliasbyname_r (const char *name, struct aliasent *result,
 
       /* 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;
 }