]> 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 0a910aa0fe79b3adefe532e528bcdc895de21b57..2dcbe8f428d126e7ccef593982bfa7de934eee21 100644 (file)
@@ -1,78 +1,59 @@
 /* Mail alias file parser in nss_files module.
-   Copyright (C) 1996, 1997 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.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   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 { none, 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");
-
-      if (stream == NULL)
-       status = NSS_STATUS_UNAVAIL;
-      else
-       {
-         /* We have to make sure the file is  `closed on exec'.  */
-         int result, flags;
+      *stream = fopen ("/etc/aliases", "rce");
 
-         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;
-           }
-       }
+      if (*stream == NULL)
+       status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : 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,8 +102,8 @@ _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,
-               char *buffer, size_t buflen)
+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;
   int ignore = 0;
@@ -148,18 +120,23 @@ get_next_alias (const char *match, struct aliasent *result,
       size_t room_left = buflen - (buflen % __alignof__ (char *));
       char *line;
 
+      /* Check whether the buffer is large enough for even trying to
+        read something.  */
+      if (room_left < 2)
+       goto no_more_room;
+
       /* Read the first line.  It must contain the alias name and
         possibly some alias names.  */
-      first_unused[room_left - 1] = '\0';
-      line = fgets (first_unused, room_left, stream);
+      first_unused[room_left - 1] = '\xff';
+      line = fgets_unlocked (first_unused, room_left, stream);
       if (line == NULL)
        /* Nothing to read.  */
        break;
-      else if (first_unused[room_left - 1] != '\0')
+      else if (first_unused[room_left - 1] != '\xff')
        {
          /* The line is too long for our buffer.  */
        no_more_room:
-         __set_errno (ERANGE);
+         *errnop = ERANGE;
          status = NSS_STATUS_TRYAGAIN;
          break;
        }
@@ -200,7 +177,8 @@ get_next_alias (const char *match, struct aliasent *result,
             looking for.  If it does not match we simply ignore all
             lines until the next line containing the start of a new
             alias is found.  */
-         ignore = match != NULL && strcmp (result->alias_name, match) != 0;
+         ignore = (match != NULL
+                   && __strcasecmp (result->alias_name, match) != 0);
 
          while (! ignore)
            {
@@ -235,21 +213,30 @@ 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))
                            {
-                             first_unused[room_left - 1] = '\0';
-                             line = fgets (first_unused, room_left, 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 (line == NULL)
                                break;
-                             if (first_unused[room_left - 1] != '\0')
+                             if (first_unused[room_left - 1] != '\xff')
                                {
                                  free (old_line);
+                                 fclose (listfile);
                                  goto no_more_room;
                                }
 
@@ -277,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)
@@ -291,8 +279,8 @@ get_next_alias (const char *match, struct aliasent *result,
                          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;
@@ -311,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;
@@ -343,9 +331,19 @@ get_next_alias (const char *match, struct aliasent *result,
 
                  /* The just read character is a white space and so
                     can be ignored.  */
-                 first_unused[room_left - 1] = '\0';
-                 line = fgets (first_unused, room_left, stream);
-                 if (first_unused[room_left - 1] != '\0')
+                 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");
                  if (cp != NULL)
@@ -364,7 +362,8 @@ get_next_alias (const char *match, struct aliasent *result,
 
 
 enum nss_status
-_nss_files_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen)
+_nss_files_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen,
+                         int *errnop)
 {
   /* Return next entry in host file.  */
   enum nss_status status = NSS_STATUS_SUCCESS;
@@ -373,33 +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;
-
-      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);
-         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 = none;
-       }
+      result->alias_local = 1;
+
+      /* 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);
@@ -410,10 +392,11 @@ _nss_files_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen)
 
 enum nss_status
 _nss_files_getaliasbyname_r (const char *name, struct aliasent *result,
-                            char *buffer, size_t buflen)
+                            char *buffer, size_t buflen, int *errnop)
 {
   /* Return next entry in host file.  */
   enum nss_status status = NSS_STATUS_SUCCESS;
+  FILE *stream = NULL;
 
   if (name == NULL)
     {
@@ -421,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)
     {
@@ -433,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);
+       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;
 }