]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - nis/nss_nisplus/nisplus-network.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-network.c
index 960c0558e90c7b7b65a8c2efd6fbe2358bd209a7..f13880d2ded1a109b72a9b0d14654ee73231bb25 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1997,1998,2000-2003,2005,2006 Free Software Foundation, Inc.
+/* Copyright (C) 1997-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
 
@@ -13,9 +13,8 @@
    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
+   <http://www.gnu.org/licenses/>.  */
 
 #include <atomic.h>
 #include <ctype.h>
@@ -26,7 +25,7 @@
 #include <string.h>
 #include <arpa/inet.h>
 #include <rpcsvc/nis.h>
-#include <bits/libc-lock.h>
+#include <libc-lock.h>
 
 #include "nss-nisplus.h"
 
@@ -36,11 +35,11 @@ static nis_result *result;
 static nis_name tablename_val;
 static u_long tablename_len;
 
-#define NISENTRYVAL(idx,col,res) \
-        ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
+#define NISENTRYVAL(idx, col, res) \
+        (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
 
-#define NISENTRYLEN(idx,col,res) \
-        ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
+#define NISENTRYLEN(idx, col, res) \
+        (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
 
 
 static int
@@ -54,10 +53,10 @@ _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
     return 0;
 
   if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
-      || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
-      || strcmp (result->objects.objects_val[0].EN_data.en_type,
+      || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
+      || strcmp (NIS_RES_OBJECT (result)[0].EN_data.en_type,
                 "networks_tbl") != 0
-      || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
+      || NIS_RES_OBJECT (result)[0].EN_data.en_cols.en_cols_len < 3)
     return 0;
 
   if (NISENTRYLEN (0, 0, result) >= room_left)
@@ -72,39 +71,45 @@ _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
            NISENTRYLEN (0, 0, result));
   first_unused[NISENTRYLEN (0, 0, result)] = '\0';
   network->n_name = first_unused;
-  room_left -= (strlen (first_unused) +1);
-  first_unused += strlen (first_unused) +1;
+  size_t len = strlen (first_unused) + 1;
+  room_left -= len;
+  first_unused += len;
+
   network->n_addrtype = 0;
   network->n_net = inet_network (NISENTRYVAL (0, 2, result));
-  char *p = first_unused;
 
-  char *line = p;
-  for (unsigned int i = 0; i < result->objects.objects_len; ++i)
+  /* XXX Rewrite at some point to allocate the array first and then
+     copy the strings.  It wasteful to first concatenate the strings
+     to just split them again later.  */
+  char *line = first_unused;
+  for (unsigned int i = 0; i < NIS_RES_NUMOBJ (result); ++i)
     {
       if (strcmp (NISENTRYVAL (i, 1, result), network->n_name) != 0)
         {
           if (NISENTRYLEN (i, 1, result) + 2 > room_left)
            goto no_more_room;
 
-         *p++ = ' ';
-          p = __stpncpy (p, NISENTRYVAL (i, 1, result),
-                        NISENTRYLEN (i, 1, result));
-          *p = '\0';
+         *first_unused++ = ' ';
+          first_unused = __stpncpy (first_unused, NISENTRYVAL (i, 1, result),
+                                   NISENTRYLEN (i, 1, result));
           room_left -= (NISENTRYLEN (i, 1, result) + 1);
         }
     }
-  *p++ = '\0';
-  first_unused = p;
+  *first_unused++ = '\0';
 
   /* Adjust the pointer so it is aligned for
      storing pointers.  */
-  first_unused += __alignof__ (char *) - 1;
-  first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
-  network->n_aliases = (char **) first_unused;
-  if (room_left < 2 * sizeof (char *))
+  size_t adjust = ((__alignof__ (char *)
+                   - (first_unused - (char *) 0) % __alignof__ (char *))
+                  % __alignof__ (char *));
+  if (room_left < adjust + sizeof (char *))
     goto no_more_room;
-  room_left -= (2 * sizeof (char *));
-  network->n_aliases[0] = NULL;
+  first_unused += adjust;
+  room_left -= adjust;
+  network->n_aliases = (char **) first_unused;
+
+  /* For the terminating NULL pointer.  */
+  room_left -= sizeof (char *);
 
   unsigned int i = 0;
   while (*line != '\0')
@@ -120,24 +125,20 @@ _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
        goto no_more_room;
 
       room_left -= sizeof (char *);
-      network->n_aliases[i] = line;
+      network->n_aliases[i++] = line;
 
       while (*line != '\0' && *line != ' ')
         ++line;
 
       if (*line == ' ')
-       {
-         *line = '\0';
-         ++line;
-          ++i;
-        }
-      else
-        network->n_aliases[i + 1] = NULL;
+       *line++ = '\0';
     }
+  network->n_aliases[i] = NULL;
 
   return 1;
 }
 
+
 static enum nss_status
 _nss_create_tablename (int *errnop)
 {
@@ -230,6 +231,11 @@ internal_nisplus_getnetent_r (struct netent *network, char *buffer,
            }
 
          result = nis_first_entry (tablename_val);
+         if (result == NULL)
+           {
+             *errnop = errno;
+             return NSS_STATUS_TRYAGAIN;
+           }
          if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
            {
              int retval = niserr2nss (result->status);
@@ -247,9 +253,13 @@ internal_nisplus_getnetent_r (struct netent *network, char *buffer,
        }
       else
        {
-         nis_result *res = nis_next_entry (tablename_val, &result->cookie);
          saved_res = result;
-         result = res;
+         result = nis_next_entry (tablename_val, &result->cookie);
+         if (result == NULL)
+           {
+             *errnop = errno;
+             return NSS_STATUS_TRYAGAIN;
+           }
          if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
            {
              int retval = niserr2nss (result->status);
@@ -327,7 +337,7 @@ _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
   /* Search at first in the alias list, and use the correct name
      for the next search */
   snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
-  result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
+  result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH | USE_DGRAM, NULL, NULL);
 
   if (result != NULL)
     {
@@ -355,7 +365,8 @@ _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
        }
 
       nis_freeresult (result);
-      result = nis_list (bufptr, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
+      result = nis_list (bufptr, FOLLOW_LINKS | FOLLOW_PATH | USE_DGRAM,
+                        NULL, NULL);
     }
 
   if (result == NULL)
@@ -365,7 +376,7 @@ _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
     }
 
   retval = niserr2nss (result->status);
-  if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
+  if (__glibc_unlikely (retval != NSS_STATUS_SUCCESS))
     {
       if (retval == NSS_STATUS_TRYAGAIN)
        {
@@ -420,14 +431,15 @@ _nss_nisplus_getnetbyaddr_r (uint32_t addr, const int type,
     char buf2[18];
     int olderr = errno;
 
-    struct in_addr in = inet_makeaddr (addr, 0);
+    struct in_addr in = { .s_addr = htonl (addr) };
     strcpy (buf2, inet_ntoa (in));
     size_t b2len = strlen (buf2);
 
     while (1)
       {
        snprintf (buf, sizeof (buf), "[addr=%s],%s", buf2, tablename_val);
-       nis_result *result = nis_list (buf, EXPAND_NAME, NULL, NULL);
+       nis_result *result = nis_list (buf, EXPAND_NAME | USE_DGRAM,
+                                      NULL, NULL);
 
        if (result == NULL)
          {
@@ -435,7 +447,7 @@ _nss_nisplus_getnetbyaddr_r (uint32_t addr, const int type,
            return NSS_STATUS_TRYAGAIN;
          }
        enum nss_status retval = niserr2nss (result->status);
-       if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
+       if (__glibc_unlikely (retval != NSS_STATUS_SUCCESS))
          {
            if (b2len > 2 && buf2[b2len - 2] == '.' && buf2[b2len - 1] == '0')
              {
@@ -443,6 +455,7 @@ _nss_nisplus_getnetbyaddr_r (uint32_t addr, const int type,
                   removed (one by one) */
                buf2[b2len - 2] = '\0';
                b2len -= 2;
+               nis_freeresult (result);
                continue;
              }