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