Remove _dl_initial_dtv
[glibc.git] / nscd / nscd_getpw_r.c
blobe405057d1627aff82d085951c3d42ad5f399db05
1 /* Copyright (C) 1998, 1999, 2003, 2004, 2005, 2007, 2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <pwd.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/mman.h>
29 #include <sys/socket.h>
30 #include <sys/uio.h>
31 #include <sys/un.h>
32 #include <not-cancel.h>
33 #include <_itoa.h>
35 #include "nscd-client.h"
36 #include "nscd_proto.h"
38 int __nss_not_use_nscd_passwd;
40 static int nscd_getpw_r (const char *key, size_t keylen, request_type type,
41 struct passwd *resultbuf, char *buffer,
42 size_t buflen, struct passwd **result)
43 internal_function;
45 int
46 __nscd_getpwnam_r (const char *name, struct passwd *resultbuf, char *buffer,
47 size_t buflen, struct passwd **result)
49 if (name == NULL)
50 return -1;
52 return nscd_getpw_r (name, strlen (name) + 1, GETPWBYNAME, resultbuf,
53 buffer, buflen, result);
56 int
57 __nscd_getpwuid_r (uid_t uid, struct passwd *resultbuf, char *buffer,
58 size_t buflen, struct passwd **result)
60 char buf[3 * sizeof (uid_t)];
61 buf[sizeof (buf) - 1] = '\0';
62 char *cp = _itoa_word (uid, buf + sizeof (buf) - 1, 10, 0);
64 return nscd_getpw_r (cp, buf + sizeof (buf) - cp, GETPWBYUID, resultbuf,
65 buffer, buflen, result);
69 libc_locked_map_ptr (static, map_handle);
70 /* Note that we only free the structure if necessary. The memory
71 mapping is not removed since it is not visible to the malloc
72 handling. */
73 libc_freeres_fn (pw_map_free)
75 if (map_handle.mapped != NO_MAPPING)
77 void *p = map_handle.mapped;
78 map_handle.mapped = NO_MAPPING;
79 free (p);
84 static int
85 internal_function
86 nscd_getpw_r (const char *key, size_t keylen, request_type type,
87 struct passwd *resultbuf, char *buffer, size_t buflen,
88 struct passwd **result)
90 int gc_cycle;
91 int nretries = 0;
93 /* If the mapping is available, try to search there instead of
94 communicating with the nscd. */
95 struct mapped_database *mapped;
96 mapped = __nscd_get_map_ref (GETFDPW, "passwd", &map_handle, &gc_cycle);
98 retry:;
99 const char *pw_name = NULL;
100 int retval = -1;
101 const char *recend = (const char *) ~UINTMAX_C (0);
102 pw_response_header pw_resp;
104 if (mapped != NO_MAPPING)
106 struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
107 sizeof pw_resp);
108 if (found != NULL)
110 pw_name = (const char *) (&found->data[0].pwdata + 1);
111 pw_resp = found->data[0].pwdata;
112 recend = (const char *) found->data + found->recsize;
113 /* Now check if we can trust pw_resp fields. If GC is
114 in progress, it can contain anything. */
115 if (mapped->head->gc_cycle != gc_cycle)
117 retval = -2;
118 goto out;
123 int sock = -1;
124 if (pw_name == NULL)
126 sock = __nscd_open_socket (key, keylen, type, &pw_resp,
127 sizeof (pw_resp));
128 if (sock == -1)
130 __nss_not_use_nscd_passwd = 1;
131 goto out;
135 /* No value found so far. */
136 *result = NULL;
138 if (__builtin_expect (pw_resp.found == -1, 0))
140 /* The daemon does not cache this database. */
141 __nss_not_use_nscd_passwd = 1;
142 goto out_close;
145 if (pw_resp.found == 1)
147 /* Set the information we already have. */
148 resultbuf->pw_uid = pw_resp.pw_uid;
149 resultbuf->pw_gid = pw_resp.pw_gid;
151 char *p = buffer;
152 /* get pw_name */
153 resultbuf->pw_name = p;
154 p += pw_resp.pw_name_len;
155 /* get pw_passwd */
156 resultbuf->pw_passwd = p;
157 p += pw_resp.pw_passwd_len;
158 /* get pw_gecos */
159 resultbuf->pw_gecos = p;
160 p += pw_resp.pw_gecos_len;
161 /* get pw_dir */
162 resultbuf->pw_dir = p;
163 p += pw_resp.pw_dir_len;
164 /* get pw_pshell */
165 resultbuf->pw_shell = p;
166 p += pw_resp.pw_shell_len;
168 ssize_t total = p - buffer;
169 if (__builtin_expect (pw_name + total > recend, 0))
170 goto out_close;
171 if (__builtin_expect (buflen < total, 0))
173 __set_errno (ERANGE);
174 retval = ERANGE;
175 goto out_close;
178 retval = 0;
179 if (pw_name == NULL)
181 ssize_t nbytes = __readall (sock, buffer, total);
183 if (__builtin_expect (nbytes != total, 0))
185 /* The `errno' to some value != ERANGE. */
186 __set_errno (ENOENT);
187 retval = ENOENT;
189 else
190 *result = resultbuf;
192 else
194 /* Copy the various strings. */
195 memcpy (resultbuf->pw_name, pw_name, total);
197 /* Try to detect corrupt databases. */
198 if (resultbuf->pw_name[pw_resp.pw_name_len - 1] != '\0'
199 || resultbuf->pw_passwd[pw_resp.pw_passwd_len - 1] != '\0'
200 || resultbuf->pw_gecos[pw_resp.pw_gecos_len - 1] != '\0'
201 || resultbuf->pw_dir[pw_resp.pw_dir_len - 1] != '\0'
202 || resultbuf->pw_shell[pw_resp.pw_shell_len - 1] != '\0')
204 /* We cannot use the database. */
205 retval = mapped->head->gc_cycle != gc_cycle ? -2 : -1;
206 goto out_close;
209 *result = resultbuf;
212 else
214 /* Set errno to 0 to indicate no error, just no found record. */
215 __set_errno (0);
216 /* Even though we have not found anything, the result is zero. */
217 retval = 0;
220 out_close:
221 if (sock != -1)
222 close_not_cancel_no_status (sock);
223 out:
224 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
226 /* When we come here this means there has been a GC cycle while we
227 were looking for the data. This means the data might have been
228 inconsistent. Retry if possible. */
229 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
231 /* nscd is just running gc now. Disable using the mapping. */
232 if (atomic_decrement_val (&mapped->counter) == 0)
233 __nscd_unmap (mapped);
234 mapped = NO_MAPPING;
237 if (retval != -1)
238 goto retry;
241 return retval;