Back to index

glibc  2.9
nscd_getserv_r.c
Go to the documentation of this file.
00001 /* Copyright (C) 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2007.
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 <string.h>
00022 #include <not-cancel.h>
00023 #include <stdio-common/_itoa.h>
00024 
00025 #include "nscd-client.h"
00026 #include "nscd_proto.h"
00027 
00028 
00029 int __nss_not_use_nscd_services;
00030 
00031 
00032 static int nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
00033                         request_type type, struct servent *resultbuf,
00034                         char *buf, size_t buflen, struct servent **result);
00035 
00036 
00037 int
00038 __nscd_getservbyname_r (const char *name, const char *proto,
00039                      struct servent *result_buf, char *buf, size_t buflen,
00040                      struct servent **result)
00041 {
00042   return nscd_getserv_r (name, strlen (name), proto, GETSERVBYNAME, result_buf,
00043                       buf, buflen, result);
00044 }
00045 
00046 
00047 int
00048 __nscd_getservbyport_r (int port, const char *proto,
00049                      struct servent *result_buf, char *buf, size_t buflen,
00050                      struct servent **result)
00051 {
00052   char portstr[3 * sizeof (int) + 2];
00053   portstr[sizeof (portstr) - 1] = '\0';
00054   char *cp = _itoa_word (port, portstr + sizeof (portstr) - 1, 10, 0);
00055 
00056   return nscd_getserv_r (cp, portstr + sizeof (portstr) - cp, proto,
00057                       GETSERVBYPORT, result_buf, buf, buflen, result);
00058 }
00059 
00060 
00061 libc_locked_map_ptr (, __serv_map_handle) attribute_hidden;
00062 /* Note that we only free the structure if necessary.  The memory
00063    mapping is not removed since it is not visible to the malloc
00064    handling.  */
00065 libc_freeres_fn (serv_map_free)
00066 {
00067   if (__serv_map_handle.mapped != NO_MAPPING)
00068     {
00069       void *p = __serv_map_handle.mapped;
00070       __serv_map_handle.mapped = NO_MAPPING;
00071       free (p);
00072     }
00073 }
00074 
00075 
00076 static int
00077 nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
00078               request_type type, struct servent *resultbuf,
00079               char *buf, size_t buflen, struct servent **result)
00080 {
00081   int gc_cycle;
00082   int nretries = 0;
00083 
00084   /* If the mapping is available, try to search there instead of
00085      communicating with the nscd.  */
00086   struct mapped_database *mapped;
00087   mapped = __nscd_get_map_ref (GETFDSERV, "services", &__serv_map_handle,
00088                             &gc_cycle);
00089   size_t protolen = proto == NULL ? 0 : strlen (proto);
00090   size_t keylen = critlen + 1 + protolen + 1;
00091   char *key = alloca (keylen);
00092   memcpy (__mempcpy (__mempcpy (key, crit, critlen),
00093                    "/", 1), proto ?: "", protolen + 1);
00094 
00095  retry:;
00096   const char *s_name = NULL;
00097   const char *s_proto = NULL;
00098   const uint32_t *aliases_len = NULL;
00099   const char *aliases_list = NULL;
00100   int retval = -1;
00101   const char *recend = (const char *) ~UINTMAX_C (0);
00102   int sock = -1;
00103   serv_response_header serv_resp;
00104 
00105   if (mapped != NO_MAPPING)
00106     {
00107       struct datahead *found = __nscd_cache_search (type, key, keylen, mapped);
00108 
00109       if (found != NULL)
00110        {
00111          s_name = (char *) (&found->data[0].servdata + 1);
00112          serv_resp = found->data[0].servdata;
00113          s_proto = s_name + serv_resp.s_name_len;
00114          aliases_len = (uint32_t *) (s_proto + serv_resp.s_proto_len);
00115          aliases_list = ((char *) aliases_len
00116                        + serv_resp.s_aliases_cnt * sizeof (uint32_t));
00117          recend = (const char *) found->data + found->recsize;
00118          /* Now check if we can trust serv_resp fields.  If GC is
00119             in progress, it can contain anything.  */
00120          if (mapped->head->gc_cycle != gc_cycle)
00121            {
00122              retval = -2;
00123              goto out;
00124            }
00125          if (__builtin_expect ((const char *) aliases_len
00126                             + serv_resp.s_aliases_cnt * sizeof (uint32_t)
00127                             > recend, 0))
00128            goto out;
00129 
00130 #ifndef _STRING_ARCH_unaligned
00131          /* The aliases_len array in the mapped database might very
00132             well be unaligned.  We will access it word-wise so on
00133             platforms which do not tolerate unaligned accesses we
00134             need to make an aligned copy.  */
00135          if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
00136              != 0)
00137            {
00138              uint32_t *tmp = alloca (serv_resp.s_aliases_cnt
00139                                   * sizeof (uint32_t));
00140              aliases_len = memcpy (tmp, aliases_len,
00141                                 serv_resp.s_aliases_cnt
00142                                 * sizeof (uint32_t));
00143            }
00144 #endif
00145        }
00146     }
00147 
00148   if (s_name == NULL)
00149     {
00150       sock = __nscd_open_socket (key, keylen, type, &serv_resp,
00151                              sizeof (serv_resp));
00152       if (sock == -1)
00153        {
00154          __nss_not_use_nscd_services = 1;
00155          goto out;
00156        }
00157     }
00158 
00159   /* No value found so far.  */
00160   *result = NULL;
00161 
00162   if (__builtin_expect (serv_resp.found == -1, 0))
00163     {
00164       /* The daemon does not cache this database.  */
00165       __nss_not_use_nscd_services = 1;
00166       goto out_close;
00167     }
00168 
00169   if (serv_resp.found == 1)
00170     {
00171       char *cp = buf;
00172       uintptr_t align1;
00173       uintptr_t align2;
00174       size_t total_len;
00175       ssize_t cnt;
00176       int n;
00177 
00178       /* A first check whether the buffer is sufficiently large is possible.  */
00179       /* Now allocate the buffer the array for the group members.  We must
00180         align the pointer and the base of the h_addr_list pointers.  */
00181       align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
00182               & (__alignof__ (char *) - 1));
00183       align2 = ((__alignof__ (char *) - ((cp + align1 + serv_resp.s_name_len
00184                                      + serv_resp.s_proto_len)
00185                                     - ((char *) 0)))
00186               & (__alignof__ (char *) - 1));
00187       if (buflen < (align1 + serv_resp.s_name_len + serv_resp.s_proto_len
00188                   + align2
00189                   + (serv_resp.s_aliases_cnt + 1) * sizeof (char *)))
00190        {
00191        no_room:
00192          __set_errno (ERANGE);
00193          retval = ERANGE;
00194          goto out_close;
00195        }
00196       cp += align1;
00197 
00198       /* Prepare the result as far as we can.  */
00199       resultbuf->s_aliases = (char **) cp;
00200       cp += (serv_resp.s_aliases_cnt + 1) * sizeof (char *);
00201 
00202       resultbuf->s_name = cp;
00203       cp += serv_resp.s_name_len;
00204       resultbuf->s_proto = cp;
00205       cp += serv_resp.s_proto_len + align2;
00206       resultbuf->s_port = serv_resp.s_port;
00207 
00208       if (s_name == NULL)
00209        {
00210          struct iovec vec[2];
00211 
00212          vec[0].iov_base = resultbuf->s_name;
00213          vec[0].iov_len = serv_resp.s_name_len + serv_resp.s_proto_len;
00214          total_len = vec[0].iov_len;
00215          n = 1;
00216 
00217          if (serv_resp.s_aliases_cnt > 0)
00218            {
00219              aliases_len = alloca (serv_resp.s_aliases_cnt
00220                                 * sizeof (uint32_t));
00221              vec[n].iov_base = (void *) aliases_len;
00222              vec[n].iov_len = serv_resp.s_aliases_cnt * sizeof (uint32_t);
00223 
00224              total_len += serv_resp.s_aliases_cnt * sizeof (uint32_t);
00225              ++n;
00226            }
00227 
00228          if ((size_t) __readvall (sock, vec, n) != total_len)
00229            goto out_close;
00230        }
00231       else
00232        memcpy (resultbuf->s_name, s_name,
00233               serv_resp.s_name_len + serv_resp.s_proto_len);
00234 
00235       /*  Now we also can read the aliases.  */
00236       total_len = 0;
00237       for (cnt = 0; cnt < serv_resp.s_aliases_cnt; ++cnt)
00238        {
00239          resultbuf->s_aliases[cnt] = cp;
00240          cp += aliases_len[cnt];
00241          total_len += aliases_len[cnt];
00242        }
00243       resultbuf->s_aliases[cnt] = NULL;
00244 
00245       if (__builtin_expect ((const char *) aliases_list + total_len > recend,
00246                          0))
00247        {
00248          /* aliases_len array might contain garbage during nscd GC cycle,
00249             retry rather than fail in that case.  */
00250          if (aliases_list != NULL && mapped->head->gc_cycle != gc_cycle)
00251            retval = -2;
00252          goto out_close;
00253        }
00254 
00255       /* See whether this would exceed the buffer capacity.  */
00256       if (__builtin_expect (cp > buf + buflen, 0))
00257        {
00258          /* aliases_len array might contain garbage during nscd GC cycle,
00259             retry rather than fail in that case.  */
00260          if (aliases_list != NULL && mapped->head->gc_cycle != gc_cycle)
00261            {
00262              retval = -2;
00263              goto out_close;
00264            }
00265          goto no_room;
00266        }
00267 
00268       /* And finally read the aliases.  */
00269       if (aliases_list == NULL)
00270        {
00271          if (total_len == 0
00272              || ((size_t) __readall (sock, resultbuf->s_aliases[0], total_len)
00273                 == total_len))
00274            {
00275              retval = 0;
00276              *result = resultbuf;
00277            }
00278        }
00279       else
00280        {
00281          memcpy (resultbuf->s_aliases[0], aliases_list, total_len);
00282 
00283          /* Try to detect corrupt databases.  */
00284          if (resultbuf->s_name[serv_resp.s_name_len - 1] != '\0'
00285              || resultbuf->s_proto[serv_resp.s_proto_len - 1] != '\0'
00286              || ({for (cnt = 0; cnt < serv_resp.s_aliases_cnt; ++cnt)
00287                    if (resultbuf->s_aliases[cnt][aliases_len[cnt] - 1]
00288                       != '\0')
00289                      break;
00290                  cnt < serv_resp.s_aliases_cnt; }))
00291            {
00292              /* We cannot use the database.  */
00293              if (mapped->head->gc_cycle != gc_cycle)
00294               retval = -2;
00295              goto out_close;
00296            }
00297 
00298          retval = 0;
00299          *result = resultbuf;
00300        }
00301     }
00302   else
00303     {
00304       /* Set errno to 0 to indicate no error, just no found record.  */
00305       __set_errno (0);
00306       /* Even though we have not found anything, the result is zero.  */
00307       retval = 0;
00308     }
00309 
00310  out_close:
00311   if (sock != -1)
00312     close_not_cancel_no_status (sock);
00313  out:
00314   if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
00315     {
00316       /* When we come here this means there has been a GC cycle while we
00317         were looking for the data.  This means the data might have been
00318         inconsistent.  Retry if possible.  */
00319       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
00320        {
00321          /* nscd is just running gc now.  Disable using the mapping.  */
00322          if (atomic_decrement_val (&mapped->counter) == 0)
00323            __nscd_unmap (mapped);
00324          mapped = NO_MAPPING;
00325        }
00326 
00327       if (retval != -1)
00328        goto retry;
00329     }
00330 
00331   return retval;
00332 }