Back to index

glibc  2.9
nscd_gethst_r.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998-2005, 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <resolv.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <arpa/nameser.h>
00025 #include <not-cancel.h>
00026 
00027 #include "nscd-client.h"
00028 #include "nscd_proto.h"
00029 
00030 int __nss_not_use_nscd_hosts;
00031 
00032 static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
00033                        struct hostent *resultbuf, char *buffer,
00034                        size_t buflen, struct hostent **result,
00035                        int *h_errnop) internal_function;
00036 
00037 
00038 int
00039 __nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
00040                      char *buffer, size_t buflen, struct hostent **result,
00041                      int *h_errnop)
00042 {
00043   request_type reqtype;
00044 
00045   reqtype = (_res.options & RES_USE_INET6) ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
00046 
00047   return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
00048                      buffer, buflen, result, h_errnop);
00049 }
00050 
00051 
00052 int
00053 __nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
00054                       char *buffer, size_t buflen, struct hostent **result,
00055                       int *h_errnop)
00056 {
00057   request_type reqtype;
00058 
00059   reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
00060 
00061   return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
00062                      buffer, buflen, result, h_errnop);
00063 }
00064 
00065 
00066 int
00067 __nscd_gethostbyaddr_r (const void *addr, socklen_t len, int type,
00068                      struct hostent *resultbuf, char *buffer, size_t buflen,
00069                      struct hostent **result, int *h_errnop)
00070 {
00071   request_type reqtype;
00072 
00073   if (!((len == INADDRSZ && type == AF_INET)
00074        || (len == IN6ADDRSZ && type == AF_INET6)))
00075     /* LEN and TYPE do not match.  */
00076     return -1;
00077 
00078   reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
00079 
00080   return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen, result,
00081                      h_errnop);
00082 }
00083 
00084 
00085 libc_locked_map_ptr (, __hst_map_handle) attribute_hidden;
00086 /* Note that we only free the structure if necessary.  The memory
00087    mapping is not removed since it is not visible to the malloc
00088    handling.  */
00089 libc_freeres_fn (hst_map_free)
00090 {
00091   if (__hst_map_handle.mapped != NO_MAPPING)
00092     {
00093       void *p = __hst_map_handle.mapped;
00094       __hst_map_handle.mapped = NO_MAPPING;
00095       free (p);
00096     }
00097 }
00098 
00099 
00100 static int
00101 internal_function
00102 nscd_gethst_r (const char *key, size_t keylen, request_type type,
00103               struct hostent *resultbuf, char *buffer, size_t buflen,
00104               struct hostent **result, int *h_errnop)
00105 {
00106   int gc_cycle;
00107   int nretries = 0;
00108 
00109   /* If the mapping is available, try to search there instead of
00110      communicating with the nscd.  */
00111   struct mapped_database *mapped;
00112   mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
00113                             &gc_cycle);
00114 
00115  retry:;
00116   const char *h_name = NULL;
00117   const uint32_t *aliases_len = NULL;
00118   const char *addr_list = NULL;
00119   size_t addr_list_len = 0;
00120   int retval = -1;
00121   const char *recend = (const char *) ~UINTMAX_C (0);
00122   int sock = -1;
00123   hst_response_header hst_resp;
00124   if (mapped != NO_MAPPING)
00125     {
00126       /* No const qualifier, as it can change during garbage collection.  */
00127       struct datahead *found = __nscd_cache_search (type, key, keylen, mapped);
00128       if (found != NULL)
00129        {
00130          h_name = (char *) (&found->data[0].hstdata + 1);
00131          hst_resp = found->data[0].hstdata;
00132          aliases_len = (uint32_t *) (h_name + hst_resp.h_name_len);
00133          addr_list = ((char *) aliases_len
00134                      + hst_resp.h_aliases_cnt * sizeof (uint32_t));
00135          addr_list_len = hst_resp.h_addr_list_cnt * INADDRSZ;
00136          recend = (const char *) found->data + found->recsize;
00137          /* Now check if we can trust hst_resp fields.  If GC is
00138             in progress, it can contain anything.  */
00139          if (mapped->head->gc_cycle != gc_cycle)
00140            {
00141              retval = -2;
00142              goto out;
00143            }
00144 
00145 #ifndef _STRING_ARCH_unaligned
00146          /* The aliases_len array in the mapped database might very
00147             well be unaligned.  We will access it word-wise so on
00148             platforms which do not tolerate unaligned accesses we
00149             need to make an aligned copy.  */
00150          if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
00151              != 0)
00152            {
00153              uint32_t *tmp = alloca (hst_resp.h_aliases_cnt
00154                                   * sizeof (uint32_t));
00155              aliases_len = memcpy (tmp, aliases_len,
00156                                 hst_resp.h_aliases_cnt
00157                                 * sizeof (uint32_t));
00158            }
00159 #endif
00160          if (type != GETHOSTBYADDR && type != GETHOSTBYNAME)
00161            {
00162              if (hst_resp.h_length == INADDRSZ)
00163               addr_list += addr_list_len;
00164              addr_list_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
00165            }
00166          if (__builtin_expect ((const char *) addr_list + addr_list_len
00167                             > recend, 0))
00168            goto out;
00169        }
00170     }
00171 
00172   if (h_name == NULL)
00173     {
00174       sock = __nscd_open_socket (key, keylen, type, &hst_resp,
00175                              sizeof (hst_resp));
00176       if (sock == -1)
00177        {
00178          __nss_not_use_nscd_hosts = 1;
00179          goto out;
00180        }
00181     }
00182 
00183   /* No value found so far.  */
00184   *result = NULL;
00185 
00186   if (__builtin_expect (hst_resp.found == -1, 0))
00187     {
00188       /* The daemon does not cache this database.  */
00189       __nss_not_use_nscd_hosts = 1;
00190       goto out_close;
00191     }
00192 
00193   if (hst_resp.found == 1)
00194     {
00195       char *cp = buffer;
00196       uintptr_t align1;
00197       uintptr_t align2;
00198       size_t total_len;
00199       ssize_t cnt;
00200       char *ignore;
00201       int n;
00202 
00203       /* A first check whether the buffer is sufficiently large is possible.  */
00204       /* Now allocate the buffer the array for the group members.  We must
00205         align the pointer and the base of the h_addr_list pointers.  */
00206       align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
00207               & (__alignof__ (char *) - 1));
00208       align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp.h_name_len)
00209                                     - ((char *) 0)))
00210               & (__alignof__ (char *) - 1));
00211       if (buflen < (align1 + hst_resp.h_name_len + align2
00212                   + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt
00213                      + 2)
00214                      * sizeof (char *))
00215                   + hst_resp.h_addr_list_cnt * (type == AF_INET
00216                                             ? INADDRSZ : IN6ADDRSZ)))
00217        {
00218        no_room:
00219          *h_errnop = NETDB_INTERNAL;
00220          __set_errno (ERANGE);
00221          retval = ERANGE;
00222          goto out_close;
00223        }
00224       cp += align1;
00225 
00226       /* Prepare the result as far as we can.  */
00227       resultbuf->h_aliases = (char **) cp;
00228       cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
00229       resultbuf->h_addr_list = (char **) cp;
00230       cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
00231 
00232       resultbuf->h_name = cp;
00233       cp += hst_resp.h_name_len + align2;
00234 
00235       if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
00236        {
00237          resultbuf->h_addrtype = AF_INET;
00238          resultbuf->h_length = INADDRSZ;
00239        }
00240       else
00241        {
00242          resultbuf->h_addrtype = AF_INET6;
00243          resultbuf->h_length = IN6ADDRSZ;
00244        }
00245       for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
00246        {
00247          resultbuf->h_addr_list[cnt] = cp;
00248          cp += resultbuf->h_length;
00249        }
00250       resultbuf->h_addr_list[cnt] = NULL;
00251 
00252       if (h_name == NULL)
00253        {
00254          struct iovec vec[4];
00255 
00256          vec[0].iov_base = resultbuf->h_name;
00257          vec[0].iov_len = hst_resp.h_name_len;
00258          total_len = hst_resp.h_name_len;
00259          n = 1;
00260 
00261          if (hst_resp.h_aliases_cnt > 0)
00262            {
00263              aliases_len = alloca (hst_resp.h_aliases_cnt
00264                                 * sizeof (uint32_t));
00265              vec[n].iov_base = (void *) aliases_len;
00266              vec[n].iov_len = hst_resp.h_aliases_cnt * sizeof (uint32_t);
00267 
00268              total_len += hst_resp.h_aliases_cnt * sizeof (uint32_t);
00269              ++n;
00270            }
00271 
00272          if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
00273            {
00274              vec[n].iov_base = resultbuf->h_addr_list[0];
00275              vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
00276 
00277              total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
00278 
00279              ++n;
00280            }
00281          else
00282            {
00283              if (hst_resp.h_length == INADDRSZ)
00284               {
00285                 ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
00286                 vec[n].iov_base = ignore;
00287                 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
00288 
00289                 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
00290 
00291                 ++n;
00292               }
00293 
00294              vec[n].iov_base = resultbuf->h_addr_list[0];
00295              vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
00296 
00297              total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
00298 
00299              ++n;
00300            }
00301 
00302          if ((size_t) __readvall (sock, vec, n) != total_len)
00303            goto out_close;
00304        }
00305       else
00306        {
00307          memcpy (resultbuf->h_name, h_name, hst_resp.h_name_len);
00308          memcpy (resultbuf->h_addr_list[0], addr_list, addr_list_len);
00309        }
00310 
00311       /*  Now we also can read the aliases.  */
00312       total_len = 0;
00313       for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
00314        {
00315          resultbuf->h_aliases[cnt] = cp;
00316          cp += aliases_len[cnt];
00317          total_len += aliases_len[cnt];
00318        }
00319       resultbuf->h_aliases[cnt] = NULL;
00320 
00321       if (__builtin_expect ((const char *) addr_list + addr_list_len
00322                          + total_len > recend, 0))
00323        {
00324          /* aliases_len array might contain garbage during nscd GC cycle,
00325             retry rather than fail in that case.  */
00326          if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
00327            retval = -2;
00328          goto out_close;
00329        }
00330       /* See whether this would exceed the buffer capacity.  */
00331       if (__builtin_expect (cp > buffer + buflen, 0))
00332        {
00333          /* aliases_len array might contain garbage during nscd GC cycle,
00334             retry rather than fail in that case.  */
00335          if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
00336            {
00337              retval = -2;
00338              goto out_close;
00339            }
00340          goto no_room;
00341        }
00342 
00343       /* And finally read the aliases.  */
00344       if (addr_list == NULL)
00345        {
00346          if (total_len == 0
00347              || ((size_t) __readall (sock, resultbuf->h_aliases[0], total_len)
00348                 == total_len))
00349            {
00350              retval = 0;
00351              *result = resultbuf;
00352            }
00353        }
00354       else
00355        {
00356          memcpy (resultbuf->h_aliases[0],
00357                 (const char *) addr_list + addr_list_len, total_len);
00358 
00359          /* Try to detect corrupt databases.  */
00360          if (resultbuf->h_name[hst_resp.h_name_len - 1] != '\0'
00361              || ({for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
00362                    if (resultbuf->h_aliases[cnt][aliases_len[cnt] - 1]
00363                       != '\0')
00364                      break;
00365                  cnt < hst_resp.h_aliases_cnt; }))
00366            {
00367              /* We cannot use the database.  */
00368              if (mapped->head->gc_cycle != gc_cycle)
00369               retval = -2;
00370              goto out_close;
00371            }
00372 
00373          retval = 0;
00374          *result = resultbuf;
00375        }
00376     }
00377   else
00378     {
00379       /* Store the error number.  */
00380       *h_errnop = hst_resp.error;
00381 
00382       /* Set errno to 0 to indicate no error, just no found record.  */
00383       __set_errno (0);
00384       /* Even though we have not found anything, the result is zero.  */
00385       retval = 0;
00386     }
00387 
00388  out_close:
00389   if (sock != -1)
00390     close_not_cancel_no_status (sock);
00391  out:
00392   if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
00393     {
00394       /* When we come here this means there has been a GC cycle while we
00395         were looking for the data.  This means the data might have been
00396         inconsistent.  Retry if possible.  */
00397       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
00398        {
00399          /* nscd is just running gc now.  Disable using the mapping.  */
00400          if (atomic_decrement_val (&mapped->counter) == 0)
00401            __nscd_unmap (mapped);
00402          mapped = NO_MAPPING;
00403        }
00404 
00405       if (retval != -1)
00406        goto retry;
00407     }
00408 
00409   return retval;
00410 }