Back to index

glibc  2.9
nscd_getgr_r.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998-2000, 2002-2005, 2006, 2007
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <alloca.h>
00022 #include <assert.h>
00023 #include <errno.h>
00024 #include <grp.h>
00025 #include <stdint.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <unistd.h>
00030 #include <sys/mman.h>
00031 #include <sys/socket.h>
00032 #include <sys/uio.h>
00033 #include <sys/un.h>
00034 #include <not-cancel.h>
00035 #include <stdio-common/_itoa.h>
00036 
00037 #include "nscd-client.h"
00038 #include "nscd_proto.h"
00039 
00040 int __nss_not_use_nscd_group;
00041 
00042 static int nscd_getgr_r (const char *key, size_t keylen, request_type type,
00043                       struct group *resultbuf, char *buffer,
00044                       size_t buflen, struct group **result)
00045      internal_function;
00046 
00047 
00048 int
00049 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
00050                  size_t buflen, struct group **result)
00051 {
00052   return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
00053                      buffer, buflen, result);
00054 }
00055 
00056 
00057 int
00058 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
00059                  size_t buflen, struct group **result)
00060 {
00061   char buf[3 * sizeof (gid_t)];
00062   buf[sizeof (buf) - 1] = '\0';
00063   char *cp = _itoa_word (gid, buf + sizeof (buf) - 1, 10, 0);
00064 
00065   return nscd_getgr_r (cp, buf + sizeof (buf) - cp, GETGRBYGID, resultbuf,
00066                      buffer, buflen, result);
00067 }
00068 
00069 
00070 libc_locked_map_ptr (,__gr_map_handle) attribute_hidden;
00071 /* Note that we only free the structure if necessary.  The memory
00072    mapping is not removed since it is not visible to the malloc
00073    handling.  */
00074 libc_freeres_fn (gr_map_free)
00075 {
00076   if (__gr_map_handle.mapped != NO_MAPPING)
00077     {
00078       void *p = __gr_map_handle.mapped;
00079       __gr_map_handle.mapped = NO_MAPPING;
00080       free (p);
00081     }
00082 }
00083 
00084 
00085 static int
00086 internal_function
00087 nscd_getgr_r (const char *key, size_t keylen, request_type type,
00088              struct group *resultbuf, char *buffer, size_t buflen,
00089              struct group **result)
00090 {
00091   int gc_cycle;
00092   int nretries = 0;
00093   const uint32_t *len = NULL;
00094   size_t lensize = 0;
00095 
00096   /* If the mapping is available, try to search there instead of
00097      communicating with the nscd.  */
00098   struct mapped_database *mapped = __nscd_get_map_ref (GETFDGR, "group",
00099                                                  &__gr_map_handle,
00100                                                  &gc_cycle);
00101  retry:;
00102   const char *gr_name = NULL;
00103   size_t gr_name_len = 0;
00104   int retval = -1;
00105   const char *recend = (const char *) ~UINTMAX_C (0);
00106   gr_response_header gr_resp;
00107 
00108   if (mapped != NO_MAPPING)
00109     {
00110       struct datahead *found = __nscd_cache_search (type, key, keylen, mapped);
00111       if (found != NULL)
00112        {
00113          len = (const uint32_t *) (&found->data[0].grdata + 1);
00114          gr_resp = found->data[0].grdata;
00115          gr_name = ((const char *) len
00116                    + gr_resp.gr_mem_cnt * sizeof (uint32_t));
00117          gr_name_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
00118          recend = (const char *) found->data + found->recsize;
00119          /* Now check if we can trust gr_resp fields.  If GC is
00120             in progress, it can contain anything.  */
00121          if (mapped->head->gc_cycle != gc_cycle)
00122            {
00123              retval = -2;
00124              goto out;
00125            }
00126 
00127          /* The alignment is always sufficient, unless GC is in progress.  */
00128          assert (((uintptr_t) len & (__alignof__ (*len) - 1)) == 0);
00129        }
00130     }
00131 
00132   int sock = -1;
00133   if (gr_name == NULL)
00134     {
00135       sock = __nscd_open_socket (key, keylen, type, &gr_resp,
00136                              sizeof (gr_resp));
00137       if (sock == -1)
00138        {
00139          __nss_not_use_nscd_group = 1;
00140          goto out;
00141        }
00142     }
00143 
00144   /* No value found so far.  */
00145   *result = NULL;
00146 
00147   if (__builtin_expect (gr_resp.found == -1, 0))
00148     {
00149       /* The daemon does not cache this database.  */
00150       __nss_not_use_nscd_group = 1;
00151       goto out_close;
00152     }
00153 
00154   if (gr_resp.found == 1)
00155     {
00156       struct iovec vec[2];
00157       char *p = buffer;
00158       size_t total_len;
00159       uintptr_t align;
00160       nscd_ssize_t cnt;
00161 
00162       /* Now allocate the buffer the array for the group members.  We must
00163         align the pointer.  */
00164       align = ((__alignof__ (char *) - (p - ((char *) 0)))
00165               & (__alignof__ (char *) - 1));
00166       total_len = (align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
00167                  + gr_resp.gr_name_len + gr_resp.gr_passwd_len);
00168       if (__builtin_expect (buflen < total_len, 0))
00169        {
00170        no_room:
00171          __set_errno (ERANGE);
00172          retval = ERANGE;
00173          goto out_close;
00174        }
00175       buflen -= total_len;
00176 
00177       p += align;
00178       resultbuf->gr_mem = (char **) p;
00179       p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
00180 
00181       /* Set pointers for strings.  */
00182       resultbuf->gr_name = p;
00183       p += gr_resp.gr_name_len;
00184       resultbuf->gr_passwd = p;
00185       p += gr_resp.gr_passwd_len;
00186 
00187       /* Fill in what we know now.  */
00188       resultbuf->gr_gid = gr_resp.gr_gid;
00189 
00190       /* Read the length information, group name, and password.  */
00191       if (gr_name == NULL)
00192        {
00193          /* Handle a simple, usual case: no group members.  */
00194          if (__builtin_expect (gr_resp.gr_mem_cnt == 0, 1))
00195            {
00196              size_t n = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
00197              if (__builtin_expect (__readall (sock, resultbuf->gr_name, n)
00198                                 != (ssize_t) n, 0))
00199               goto out_close;
00200            }
00201          else
00202            {
00203              /* Allocate array to store lengths.  */
00204              if (lensize == 0)
00205               {
00206                 lensize = gr_resp.gr_mem_cnt * sizeof (uint32_t);
00207                 len = (uint32_t *) alloca (lensize);
00208               }
00209              else if (gr_resp.gr_mem_cnt * sizeof (uint32_t) > lensize)
00210               len = extend_alloca (len, lensize,
00211                                  gr_resp.gr_mem_cnt * sizeof (uint32_t));
00212 
00213              vec[0].iov_base = (void *) len;
00214              vec[0].iov_len = gr_resp.gr_mem_cnt * sizeof (uint32_t);
00215              vec[1].iov_base = resultbuf->gr_name;
00216              vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
00217              total_len = vec[0].iov_len + vec[1].iov_len;
00218 
00219              /* Get this data.  */
00220              size_t n = __readvall (sock, vec, 2);
00221              if (__builtin_expect (n != total_len, 0))
00222               goto out_close;
00223            }
00224        }
00225       else
00226        /* We already have the data.  Just copy the group name and
00227           password.  */
00228        memcpy (resultbuf->gr_name, gr_name,
00229               gr_resp.gr_name_len + gr_resp.gr_passwd_len);
00230 
00231       /* Clear the terminating entry.  */
00232       resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
00233 
00234       /* Prepare reading the group members.  */
00235       total_len = 0;
00236       for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
00237        {
00238          resultbuf->gr_mem[cnt] = p;
00239          total_len += len[cnt];
00240          p += len[cnt];
00241        }
00242 
00243       if (__builtin_expect (gr_name + gr_name_len + total_len > recend, 0))
00244        {
00245          /* len array might contain garbage during nscd GC cycle,
00246             retry rather than fail in that case.  */
00247          if (gr_name != NULL && mapped->head->gc_cycle != gc_cycle)
00248            retval = -2;
00249          goto out_close;
00250        }
00251       if (__builtin_expect (total_len > buflen, 0))
00252        {
00253          /* len array might contain garbage during nscd GC cycle,
00254             retry rather than fail in that case.  */
00255          if (gr_name != NULL && mapped->head->gc_cycle != gc_cycle)
00256            {
00257              retval = -2;
00258              goto out_close;
00259            }
00260          else
00261            goto no_room;
00262        }
00263 
00264       retval = 0;
00265 
00266       /* If there are no group members TOTAL_LEN is zero.  */
00267       if (gr_name == NULL)
00268        {
00269          if (total_len > 0
00270              && __builtin_expect (__readall (sock, resultbuf->gr_mem[0],
00271                                          total_len) != total_len, 0))
00272            {
00273              /* The `errno' to some value != ERANGE.  */
00274              __set_errno (ENOENT);
00275              retval = ENOENT;
00276            }
00277          else
00278            *result = resultbuf;
00279        }
00280       else
00281        {
00282          /* Copy the group member names.  */
00283          memcpy (resultbuf->gr_mem[0], gr_name + gr_name_len, total_len);
00284 
00285          /* Try to detect corrupt databases.  */
00286          if (resultbuf->gr_name[gr_name_len - 1] != '\0'
00287              || resultbuf->gr_passwd[gr_resp.gr_passwd_len - 1] != '\0'
00288              || ({for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
00289                   if (resultbuf->gr_mem[cnt][len[cnt] - 1] != '\0')
00290                     break;
00291                 cnt < gr_resp.gr_mem_cnt; }))
00292            {
00293              /* We cannot use the database.  */
00294              retval = mapped->head->gc_cycle != gc_cycle ? -2 : -1;
00295              goto out_close;
00296            }
00297 
00298          *result = resultbuf;
00299        }
00300     }
00301   else
00302     {
00303       /* Set errno to 0 to indicate no error, just no found record.  */
00304       __set_errno (0);
00305       /* Even though we have not found anything, the result is zero.  */
00306       retval = 0;
00307     }
00308 
00309  out_close:
00310   if (sock != -1)
00311     close_not_cancel_no_status (sock);
00312  out:
00313   if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
00314     {
00315       /* When we come here this means there has been a GC cycle while we
00316         were looking for the data.  This means the data might have been
00317         inconsistent.  Retry if possible.  */
00318       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
00319        {
00320          /* nscd is just running gc now.  Disable using the mapping.  */
00321          if (atomic_decrement_val (&mapped->counter) == 0)
00322            __nscd_unmap (mapped);
00323          mapped = NO_MAPPING;
00324        }
00325 
00326       if (retval != -1)
00327        goto retry;
00328     }
00329 
00330   return retval;
00331 }