]> 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 c4717e124224cd023ad8d9fbfc2c418587d11949..2dcbe8f428d126e7ccef593982bfa7de934eee21 100644 (file)
@@ -1,5 +1,5 @@
 /* Mail alias file parser in nss_files module.
-   Copyright (C) 1996,97,98,99,2002,2006 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;
 }
@@ -86,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);
 
@@ -105,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;
     }
 }
 
@@ -121,7 +93,7 @@ _nss_files_endaliasent (void)
 {
   __libc_lock_lock (lock);
 
-  internal_endent ();
+  internal_endent (&stream);
 
   __libc_lock_unlock (lock);
 
@@ -130,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;
@@ -149,7 +121,7 @@ get_next_alias (const char *match, struct aliasent *result,
       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;
 
@@ -241,14 +213,21 @@ get_next_alias (const char *match, struct aliasent *result,
 
                      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);
@@ -257,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;
                                }
 
@@ -284,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)
@@ -318,7 +299,7 @@ get_next_alias (const char *match, struct aliasent *result,
                     just read character.  */
                  int ch;
 
-                 ch = fgetc (stream);
+                 ch = fgetc_unlocked (stream);
                  if (ch == EOF || ch == '\n' || !isspace (ch))
                    {
                      size_t cnt;
@@ -352,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");
@@ -381,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);
@@ -424,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)
     {
@@ -431,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)
     {
@@ -443,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;
 }