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