Back to index

glibc  2.9
nscd_getpw_r.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998, 1999, 2003, 2004, 2005, 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 <assert.h>
00022 #include <errno.h>
00023 #include <pwd.h>
00024 #include <stdint.h>
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <unistd.h>
00029 #include <sys/mman.h>
00030 #include <sys/socket.h>
00031 #include <sys/uio.h>
00032 #include <sys/un.h>
00033 #include <not-cancel.h>
00034 #include <stdio-common/_itoa.h>
00035 
00036 #include "nscd-client.h"
00037 #include "nscd_proto.h"
00038 
00039 int __nss_not_use_nscd_passwd;
00040 
00041 static int nscd_getpw_r (const char *key, size_t keylen, request_type type,
00042                       struct passwd *resultbuf, char *buffer,
00043                       size_t buflen, struct passwd **result)
00044      internal_function;
00045 
00046 int
00047 __nscd_getpwnam_r (const char *name, struct passwd *resultbuf, char *buffer,
00048                  size_t buflen, struct passwd **result)
00049 {
00050   if (name == NULL)
00051     return -1;
00052 
00053   return nscd_getpw_r (name, strlen (name) + 1, GETPWBYNAME, resultbuf,
00054                      buffer, buflen, result);
00055 }
00056 
00057 int
00058 __nscd_getpwuid_r (uid_t uid, struct passwd *resultbuf, char *buffer,
00059                  size_t buflen, struct passwd **result)
00060 {
00061   char buf[3 * sizeof (uid_t)];
00062   buf[sizeof (buf) - 1] = '\0';
00063   char *cp = _itoa_word (uid, buf + sizeof (buf) - 1, 10, 0);
00064 
00065   return nscd_getpw_r (cp, buf + sizeof (buf) - cp, GETPWBYUID, resultbuf,
00066                      buffer, buflen, result);
00067 }
00068 
00069 
00070 libc_locked_map_ptr (static, map_handle);
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 (pw_map_free)
00075 {
00076   if (map_handle.mapped != NO_MAPPING)
00077     {
00078       void *p = map_handle.mapped;
00079       map_handle.mapped = NO_MAPPING;
00080       free (p);
00081     }
00082 }
00083 
00084 
00085 static int
00086 internal_function
00087 nscd_getpw_r (const char *key, size_t keylen, request_type type,
00088              struct passwd *resultbuf, char *buffer, size_t buflen,
00089              struct passwd **result)
00090 {
00091   int gc_cycle;
00092   int nretries = 0;
00093 
00094   /* If the mapping is available, try to search there instead of
00095      communicating with the nscd.  */
00096   struct mapped_database *mapped;
00097   mapped = __nscd_get_map_ref (GETFDPW, "passwd", &map_handle, &gc_cycle);
00098 
00099  retry:;
00100   const char *pw_name = NULL;
00101   int retval = -1;
00102   const char *recend = (const char *) ~UINTMAX_C (0);
00103   pw_response_header pw_resp;
00104 
00105   if (mapped != NO_MAPPING)
00106     {
00107       struct datahead *found = __nscd_cache_search (type, key, keylen, mapped);
00108       if (found != NULL)
00109        {
00110          pw_name = (const char *) (&found->data[0].pwdata + 1);
00111          pw_resp = found->data[0].pwdata;
00112          recend = (const char *) found->data + found->recsize;
00113          /* Now check if we can trust pw_resp fields.  If GC is
00114             in progress, it can contain anything.  */
00115          if (mapped->head->gc_cycle != gc_cycle)
00116            {
00117              retval = -2;
00118              goto out;
00119            }
00120        }
00121     }
00122 
00123   int sock = -1;
00124   if (pw_name == NULL)
00125     {
00126       sock = __nscd_open_socket (key, keylen, type, &pw_resp,
00127                              sizeof (pw_resp));
00128       if (sock == -1)
00129        {
00130          __nss_not_use_nscd_passwd = 1;
00131          goto out;
00132        }
00133     }
00134 
00135   /* No value found so far.  */
00136   *result = NULL;
00137 
00138   if (__builtin_expect (pw_resp.found == -1, 0))
00139     {
00140       /* The daemon does not cache this database.  */
00141       __nss_not_use_nscd_passwd = 1;
00142       goto out_close;
00143     }
00144 
00145   if (pw_resp.found == 1)
00146     {
00147       /* Set the information we already have.  */
00148       resultbuf->pw_uid = pw_resp.pw_uid;
00149       resultbuf->pw_gid = pw_resp.pw_gid;
00150 
00151       char *p = buffer;
00152       /* get pw_name */
00153       resultbuf->pw_name = p;
00154       p += pw_resp.pw_name_len;
00155       /* get pw_passwd */
00156       resultbuf->pw_passwd = p;
00157       p += pw_resp.pw_passwd_len;
00158       /* get pw_gecos */
00159       resultbuf->pw_gecos = p;
00160       p += pw_resp.pw_gecos_len;
00161       /* get pw_dir */
00162       resultbuf->pw_dir = p;
00163       p += pw_resp.pw_dir_len;
00164       /* get pw_pshell */
00165       resultbuf->pw_shell = p;
00166       p += pw_resp.pw_shell_len;
00167 
00168       ssize_t total = p - buffer;
00169       if (__builtin_expect (pw_name + total > recend, 0))
00170        goto out_close;
00171       if (__builtin_expect (buflen < total, 0))
00172        {
00173          __set_errno (ERANGE);
00174          retval = ERANGE;
00175          goto out_close;
00176        }
00177 
00178       retval = 0;
00179       if (pw_name == NULL)
00180        {
00181          ssize_t nbytes = __readall (sock, buffer, total);
00182 
00183          if (__builtin_expect (nbytes != total, 0))
00184            {
00185              /* The `errno' to some value != ERANGE.  */
00186              __set_errno (ENOENT);
00187              retval = ENOENT;
00188            }
00189          else
00190            *result = resultbuf;
00191        }
00192       else
00193        {
00194          /* Copy the various strings.  */
00195          memcpy (resultbuf->pw_name, pw_name, total);
00196 
00197          /* Try to detect corrupt databases.  */
00198          if (resultbuf->pw_name[pw_resp.pw_name_len - 1] != '\0'
00199              || resultbuf->pw_passwd[pw_resp.pw_passwd_len - 1] != '\0'
00200              || resultbuf->pw_gecos[pw_resp.pw_gecos_len - 1] != '\0'
00201              || resultbuf->pw_dir[pw_resp.pw_dir_len - 1] != '\0'
00202              || resultbuf->pw_shell[pw_resp.pw_shell_len - 1] != '\0')
00203            {
00204              /* We cannot use the database.  */
00205              retval = mapped->head->gc_cycle != gc_cycle ? -2 : -1;
00206              goto out_close;
00207            }
00208 
00209          *result = resultbuf;
00210        }
00211     }
00212   else
00213     {
00214       /* Set errno to 0 to indicate no error, just no found record.  */
00215       __set_errno (0);
00216       /* Even though we have not found anything, the result is zero.  */
00217       retval = 0;
00218     }
00219 
00220  out_close:
00221   if (sock != -1)
00222     close_not_cancel_no_status (sock);
00223  out:
00224   if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
00225     {
00226       /* When we come here this means there has been a GC cycle while we
00227         were looking for the data.  This means the data might have been
00228         inconsistent.  Retry if possible.  */
00229       if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
00230        {
00231          /* nscd is just running gc now.  Disable using the mapping.  */
00232          if (atomic_decrement_val (&mapped->counter) == 0)
00233            __nscd_unmap (mapped);
00234          mapped = NO_MAPPING;
00235        }
00236 
00237       if (retval != -1)
00238        goto retry;
00239     }
00240 
00241   return retval;
00242 }