]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - nis/nss_nis/nis-pwd.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nis / nss_nis / nis-pwd.c
index 7972118c84c042b49e6b487e852876d0b9f98514..ab21054976f65960d50d7070c7d8966d7c40d98f 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996-1998,2001,2002,2003,2006 Free Software Foundation, Inc.
+/* Copyright (C) 1996-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
 
@@ -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
+   <https://www.gnu.org/licenses/>.  */
 
 #include <assert.h>
 #include <ctype.h>
@@ -23,7 +22,7 @@
 #include <nss.h>
 #include <pwd.h>
 #include <string.h>
-#include <bits/libc-lock.h>
+#include <libc-lock.h>
 #include <rpcsvc/yp.h>
 #include <rpcsvc/ypclnt.h>
 
@@ -39,7 +38,7 @@
 /* Protect global state against multiple changers */
 __libc_lock_define_initialized (static, lock)
 
-static bool_t new_start = 1;
+static bool new_start = true;
 static char *oldkey;
 static int oldkeylen;
 static intern_t intern;
@@ -58,7 +57,7 @@ _nis_saveit (int instatus, char *inkey, int inkeylen, char *inval,
     {
       struct response_t *bucket = intern->next;
 
-      if (__builtin_expect (bucket == NULL, 0))
+      if (__glibc_unlikely (bucket == NULL))
        {
 #define MINSIZE 4096 - 4 * sizeof (void *)
          const size_t minsize = MAX (MINSIZE, 2 * (invallen + 1));
@@ -93,7 +92,7 @@ _nis_saveit (int instatus, char *inkey, int inkeylen, char *inval,
        }
 
       char *p = mempcpy (&bucket->mem[intern->offset], inval, invallen);
-      if (__builtin_expect (p[-1] != '\0', 0))
+      if (__glibc_unlikely (p[-1] != '\0'))
        {
          *p = '\0';
          ++invallen;
@@ -108,15 +107,12 @@ _nis_saveit (int instatus, char *inkey, int inkeylen, char *inval,
 static void
 internal_nis_endpwent (void)
 {
-  new_start = 1;
-  if (oldkey != NULL)
-    {
-      free (oldkey);
-      oldkey = NULL;
-      oldkeylen = 0;
-    }
+  new_start = true;
+  free (oldkey);
+  oldkey = NULL;
+  oldkeylen = 0;
 
-  struct response_t *curr = intern.next;
+  struct response_t *curr = intern.start;
 
   while (curr != NULL)
     {
@@ -147,7 +143,7 @@ internal_nis_setpwent (void)
 {
   /* We have to read all the data now.  */
   char *domain;
-  if (__builtin_expect (yp_get_default_domain (&domain), 0))
+  if (__glibc_unlikely (yp_get_default_domain (&domain)))
     return NSS_STATUS_UNAVAIL;
 
   struct ypall_callback ypcb;
@@ -214,7 +210,7 @@ internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen,
        handle_batch_read:
          bucket = intern.next;
 
-         if (__builtin_expect (intern.offset >= bucket->size, 0))
+         if (__glibc_unlikely (intern.offset >= bucket->size))
            {
              if (bucket->next == NULL)
                return NSS_STATUS_NOTFOUND;
@@ -253,7 +249,7 @@ internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen,
            yperr = yp_next (domain, "passwd.byname", oldkey, oldkeylen,
                             &outkey, &keylen, &result, &len);
 
-         if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
+         if (__glibc_unlikely (yperr != YPERR_SUCCESS))
            {
              enum nss_status retval = yperr2nss (yperr);
 
@@ -264,18 +260,21 @@ internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen,
        }
 
       /* Check for adjunct style secret passwords.  They can be
-        recognized by a password starting with "##".  */
+        recognized by a password starting with "##".  We do not use
+        it if the passwd.adjunct.byname table is supposed to be used
+        as a shadow.byname replacement.  */
       char *p = strchr (result, ':');
       size_t namelen;
       char *result2;
       int len2;
-      if (p != NULL    /* This better should be true in all cases.  */
+      if ((_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW) == 0
+         && p != NULL  /* This better should be true in all cases.  */
          && p[1] == '#' && p[2] == '#'
          && (namelen = p - result,
              yp_match (domain, "passwd.adjunct.byname", result, namelen,
                        &result2, &len2)) == YPERR_SUCCESS)
        {
-         /* We found a passwd.adjunct entry.  Merge encrypted
+         /* We found a passwd.adjunct.byname entry.  Merge encrypted
             password therein into original result.  */
          char *encrypted = strchr (result2, ':');
          char *endp;
@@ -313,7 +312,7 @@ internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen,
       else
        {
        non_adjunct:
-         if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
+         if (__glibc_unlikely ((size_t) (len + 1) > buflen))
            {
              free (result);
              *errnop = ERANGE;
@@ -325,15 +324,16 @@ internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen,
        }
 
       while (isspace (*p))
-        ++p;
+       ++p;
       if (!batch_read)
        free (result);
 
       parse_res = _nss_files_parse_pwent (p, pwd, (void *) buffer, buflen,
                                          errnop);
-      if (__builtin_expect (parse_res == -1, 0))
+      if (__glibc_unlikely (parse_res == -1))
        {
-         free (outkey);
+         if (!batch_read)
+           free (outkey);
          *errnop = ERANGE;
          return NSS_STATUS_TRYAGAIN;
        }
@@ -345,7 +345,7 @@ internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen,
          free (oldkey);
          oldkey = outkey;
          oldkeylen = keylen;
-         new_start = 0;
+         new_start = false;
        }
     }
   while (parse_res < 1);
@@ -379,7 +379,7 @@ _nss_nis_getpwnam_r (const char *name, struct passwd *pwd,
     }
 
   char *domain;
-  if (__builtin_expect (yp_get_default_domain (&domain), 0))
+  if (__glibc_unlikely (yp_get_default_domain (&domain)))
     return NSS_STATUS_UNAVAIL;
 
   size_t namelen = strlen (name);
@@ -388,7 +388,7 @@ _nss_nis_getpwnam_r (const char *name, struct passwd *pwd,
   int len;
   int yperr = yp_match (domain, "passwd.byname", name, namelen, &result, &len);
 
-  if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
+  if (__glibc_unlikely (yperr != YPERR_SUCCESS))
     {
       enum nss_status retval = yperr2nss (yperr);
 
@@ -398,16 +398,19 @@ _nss_nis_getpwnam_r (const char *name, struct passwd *pwd,
     }
 
   /* Check for adjunct style secret passwords.  They can be recognized
-     by a password starting with "##".  */
+     by a password starting with "##". We do not use it if the
+     passwd.adjunct.byname table is supposed to be used as a shadow.byname
+     replacement.  */
   char *result2;
   int len2;
   char *p = strchr (result, ':');
-  if (p != NULL        /* This better should be true in all cases.  */
+  if ((_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW) == 0
+      && p != NULL     /* This better should be true in all cases.  */
       && p[1] == '#' && p[2] == '#'
       && yp_match (domain, "passwd.adjunct.byname", name, namelen,
                   &result2, &len2) == YPERR_SUCCESS)
     {
-      /* We found a passwd.adjunct entry.  Merge encrypted password
+      /* We found a passwd.adjunct.byname entry.  Merge encrypted password
         therein into original result.  */
       char *encrypted = strchr (result2, ':');
       char *endp;
@@ -444,7 +447,7 @@ _nss_nis_getpwnam_r (const char *name, struct passwd *pwd,
   else
     {
     non_adjunct:
-      if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
+      if (__glibc_unlikely ((size_t) (len + 1) > buflen))
        {
          free (result);
          *errnop = ERANGE;
@@ -461,10 +464,10 @@ _nss_nis_getpwnam_r (const char *name, struct passwd *pwd,
 
   int parse_res = _nss_files_parse_pwent (p, pwd, (void *) buffer, buflen,
                                          errnop);
-  if (__builtin_expect (parse_res < 1, 0))
+  if (__glibc_unlikely (parse_res < 1))
     {
       if (parse_res == -1)
-        return NSS_STATUS_TRYAGAIN;
+       return NSS_STATUS_TRYAGAIN;
       else
        return NSS_STATUS_NOTFOUND;
     }
@@ -477,7 +480,7 @@ _nss_nis_getpwuid_r (uid_t uid, struct passwd *pwd,
                     char *buffer, size_t buflen, int *errnop)
 {
   char *domain;
-  if (__builtin_expect (yp_get_default_domain (&domain), 0))
+  if (__glibc_unlikely (yp_get_default_domain (&domain)))
     return NSS_STATUS_UNAVAIL;
 
   char buf[32];
@@ -487,7 +490,7 @@ _nss_nis_getpwuid_r (uid_t uid, struct passwd *pwd,
   int len;
   int yperr = yp_match (domain, "passwd.byuid", buf, nlen, &result, &len);
 
-  if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
+  if (__glibc_unlikely (yperr != YPERR_SUCCESS))
     {
       enum nss_status retval = yperr2nss (yperr);
 
@@ -497,18 +500,21 @@ _nss_nis_getpwuid_r (uid_t uid, struct passwd *pwd,
     }
 
   /* Check for adjunct style secret passwords.  They can be recognized
-     by a password starting with "##".  */
+     by a password starting with "##".  We do not use it if the
+     passwd.adjunct.byname table is supposed to be used as a shadow.byname
+     replacement.  */
   char *result2;
   int len2;
   size_t namelen;
   char *p = strchr (result, ':');
-  if (p != NULL        /* This better should be true in all cases.  */
+  if ((_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW) == 0
+      && p != NULL     /* This better should be true in all cases.  */
       && p[1] == '#' && p[2] == '#'
       && (namelen = p - result,
          yp_match (domain, "passwd.adjunct.byname", result, namelen,
                    &result2, &len2)) == YPERR_SUCCESS)
     {
-      /* We found a passwd.adjunct entry.  Merge encrypted password
+      /* We found a passwd.adjunct.byname entry.  Merge encrypted password
         therein into original result.  */
       char *encrypted = strchr (result2, ':');
       char *endp;
@@ -546,7 +552,7 @@ _nss_nis_getpwuid_r (uid_t uid, struct passwd *pwd,
   else
     {
     non_adjunct:
-      if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
+      if (__glibc_unlikely ((size_t) (len + 1) > buflen))
        {
          free (result);
          *errnop = ERANGE;
@@ -563,10 +569,10 @@ _nss_nis_getpwuid_r (uid_t uid, struct passwd *pwd,
 
   int parse_res = _nss_files_parse_pwent (p, pwd, (void *) buffer, buflen,
                                          errnop);
-  if (__builtin_expect (parse_res < 1, 0))
+  if (__glibc_unlikely (parse_res < 1))
     {
       if (parse_res == -1)
-        return NSS_STATUS_TRYAGAIN;
+       return NSS_STATUS_TRYAGAIN;
      else
        return NSS_STATUS_NOTFOUND;
     }