about summary refs log tree commit diff
path: root/nis/nis_subr.c
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2007-07-12 18:26:36 +0000
committerJakub Jelinek <jakub@redhat.com>2007-07-12 18:26:36 +0000
commit0ecb606cb6cf65de1d9fc8a919bceb4be476c602 (patch)
tree2ea1f8305970753e4a657acb2ccc15ca3eec8e2c /nis/nis_subr.c
parent7d58530341304d403a6626d7f7a1913165fe2f32 (diff)
downloadglibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.tar.gz
glibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.tar.xz
glibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.zip
2.5-18.1
Diffstat (limited to 'nis/nis_subr.c')
-rw-r--r--nis/nis_subr.c105
1 files changed, 70 insertions, 35 deletions
diff --git a/nis/nis_subr.c b/nis/nis_subr.c
index 4c4ef8b5b4..c68189e541 100644
--- a/nis/nis_subr.c
+++ b/nis/nis_subr.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 1997, 1999, 2000, 2004 Free Software Foundation, Inc.
+/* Copyright (c) 1997,1999,2000,2004,2005,2006 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
 
@@ -39,22 +39,13 @@ nis_leaf_of_r (const_nis_name name, char *buffer, size_t buflen)
   while (name[i] != '.' && name[i] != '\0')
     i++;
 
-  if (i > buflen - 1)
+  if (__builtin_expect (i >= buflen, 0))
     {
       __set_errno (ERANGE);
       return NULL;
     }
 
-  if (i > 0)
-    {
-      if ((size_t)i >= buflen)
-	{
-	  __set_errno (ERANGE);
-	  return NULL;
-	}
-
-      *((char *) __mempcpy (buffer, name, i)) = '\0';
-    }
+  *((char *) __mempcpy (buffer, name, i)) = '\0';
 
   return buffer;
 }
@@ -98,13 +89,12 @@ nis_name_of_r (const_nis_name name, char *buffer, size_t buflen)
 }
 libnsl_hidden_def (nis_name_of_r)
 
-static int
+static int __always_inline
 count_dots (const_nis_name str)
 {
   int count = 0;
-  size_t l = strlen (str);
 
-  for (size_t i = 0; i < l; ++i)
+  for (size_t i = 0; str[i] != '\0'; ++i)
     if (str[i] == '.')
       ++count;
 
@@ -117,25 +107,23 @@ count_dots (const_nis_name str)
 nis_name *
 nis_getnames (const_nis_name name)
 {
-  nis_name *getnames = NULL;
-  char local_domain[NIS_MAXNAMELEN + 1];
+  const char *local_domain = nis_local_directory ();
+  size_t local_domain_len = strlen (local_domain);
+  size_t name_len = strlen (name);
   char *path;
-  char *cp;
-  int count;
   int pos = 0;
-  int have_point;
   char *saveptr;
+  int have_point;
+  const char *cp;
+  const char *cp2;
 
-  strncpy (local_domain, nis_local_directory (), NIS_MAXNAMELEN);
-  local_domain[NIS_MAXNAMELEN] = '\0';
-
-  count = 1;
-  getnames = malloc ((count + 1) * sizeof (char *));
+  int count = 2;
+  nis_name *getnames = malloc ((count + 1) * sizeof (char *));
   if (__builtin_expect (getnames == NULL, 0))
       return NULL;
 
   /* Do we have a fully qualified NIS+ name ? If yes, give it back */
-  if (name[strlen (name) - 1] == '.')
+  if (name[name_len - 1] == '.')
     {
       if ((getnames[0] = strdup (name)) == NULL)
 	{
@@ -151,6 +139,44 @@ nis_getnames (const_nis_name name)
       return getnames;
     }
 
+  /* If the passed NAME is shared a suffix (the latter of course with
+     a final dot) with each other we pass back NAME with a final
+     dot.  */
+  if (local_domain_len > 2)
+    {
+      have_point = 0;
+      cp = &local_domain[local_domain_len - 2];
+      cp2 = &name[name_len - 1];
+
+      while (*cp == *cp2)
+	{
+	  if (*cp == '.')
+	    have_point = 1;
+	  --cp;
+	  --cp2;
+	  if (cp < local_domain)
+	    {
+	      have_point = cp2 < name || *cp2 == '.';
+	      break;
+	    }
+	  if (cp2 < name)
+	    {
+	      have_point = *cp == '.';
+	      break;
+	    }
+	}
+
+      if (have_point)
+	{
+	  getnames[0] = malloc (name_len + 2);
+	  if (getnames[0] == NULL)
+	    goto free_null;
+
+	  strcpy (stpcpy (getnames[0], name), ".");
+	  ++pos;
+	}
+    }
+
   /* Get the search path, where we have to search "name" */
   path = getenv ("NIS_PATH");
   if (path == NULL)
@@ -158,17 +184,17 @@ nis_getnames (const_nis_name name)
   else
     path = strdupa (path);
 
-  have_point = (strchr (name, '.') != NULL);
+  have_point = strchr (name, '.') != NULL;
 
   cp = __strtok_r (path, ":", &saveptr);
   while (cp)
     {
       if (strcmp (cp, "$") == 0)
 	{
-	  char *cptr = local_domain;
+	  const char *cptr = local_domain;
 	  char *tmp;
 
-	  while ((have_point && *cptr != '\0') || (count_dots (cptr) >= 2))
+	  while (*cptr != '\0' && count_dots (cptr) >= 2)
 	    {
 	      if (pos >= count)
 		{
@@ -179,8 +205,7 @@ nis_getnames (const_nis_name name)
 		    goto free_null;
 		  getnames = newp;
 		}
-	      tmp = malloc (strlen (cptr) + strlen (local_domain) +
-			    strlen (name) + 2);
+	      tmp = malloc (strlen (cptr) + local_domain_len + name_len + 2);
 	      if (__builtin_expect (tmp == NULL, 0))
 		goto free_null;
 
@@ -210,7 +235,7 @@ nis_getnames (const_nis_name name)
 	    {
 	      char *p;
 
-	      tmp = malloc (cplen + strlen (local_domain) + strlen (name) + 2);
+	      tmp = malloc (cplen + local_domain_len + name_len + 2);
 	      if (__builtin_expect (tmp == NULL, 0))
 		goto free_null;
 
@@ -226,13 +251,16 @@ nis_getnames (const_nis_name name)
 	    {
 	      char *p;
 
-	      tmp = malloc (cplen + strlen (name) + 2);
+	      tmp = malloc (cplen + name_len + 3);
 	      if (__builtin_expect (tmp == NULL, 0))
 		goto free_null;
 
-	      p = __stpcpy (tmp, name);
+	      p = __mempcpy (tmp, name, name_len);
 	      *p++ = '.';
-	      memcpy (p, cp, cplen + 1);
+	      p = __mempcpy (p, cp, cplen);
+	      if (p[-1] != '.')
+		*p++ = '.';
+	      *p = '\0';
 	    }
 
 	  if (pos >= count)
@@ -250,6 +278,13 @@ nis_getnames (const_nis_name name)
       cp = __strtok_r (NULL, ":", &saveptr);
     }
 
+  if (pos == 0
+      && __asprintf (&getnames[pos++], "%s%s%s%s",
+		     name, name[name_len - 1] == '.' ? "" : ".",
+		     local_domain,
+		     local_domain[local_domain_len - 1] == '.' ? "" : ".") < 0)
+    goto free_null;
+
   getnames[pos] = NULL;
 
   return getnames;