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