]> git.ipfire.org Git - thirdparty/glibc.git/blob - nscd/nscd_getpw_r.c
Update.
[thirdparty/glibc.git] / nscd / nscd_getpw_r.c
1 /* Copyright (C) 1998, 1999, 2003, 2004 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
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 <stdio-common/_itoa.h>
34
35 #include "nscd-client.h"
36 #include "nscd_proto.h"
37
38 int __nss_not_use_nscd_passwd;
39
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;
44
45 int
46 __nscd_getpwnam_r (const char *name, struct passwd *resultbuf, char *buffer,
47 size_t buflen, struct passwd **result)
48 {
49 if (name == NULL)
50 return -1;
51
52 return nscd_getpw_r (name, strlen (name) + 1, GETPWBYNAME, resultbuf,
53 buffer, buflen, result);
54 }
55
56 int
57 __nscd_getpwuid_r (uid_t uid, struct passwd *resultbuf, char *buffer,
58 size_t buflen, struct passwd **result)
59 {
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);
63
64 return nscd_getpw_r (cp, buf + sizeof (buf) - cp, GETPWBYUID, resultbuf,
65 buffer, buflen, result);
66 }
67
68
69 libc_locked_map_ptr (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 (gr_map_free)
74 {
75 if (map_handle.mapped != NO_MAPPING)
76 free (map_handle.mapped);
77 }
78
79
80 static int
81 internal_function
82 nscd_getpw_r (const char *key, size_t keylen, request_type type,
83 struct passwd *resultbuf, char *buffer, size_t buflen,
84 struct passwd **result)
85 {
86 int gc_cycle;
87 /* If the mapping is available, try to search there instead of
88 communicating with the nscd. */
89 struct mapped_database *mapped;
90 mapped = __nscd_get_map_ref (GETFDPW, "passwd", &map_handle, &gc_cycle);
91
92 retry:;
93 const pw_response_header *pw_resp = NULL;
94 const char *pw_name = NULL;
95 int retval = -1;
96 const char *recend = (const char *) ~UINTMAX_C (0);
97
98 if (mapped != NO_MAPPING)
99 {
100 const struct datahead *found = __nscd_cache_search (type, key, keylen,
101 mapped);
102 if (found != NULL)
103 {
104 pw_resp = &found->data[0].pwdata;
105 pw_name = (const char *) (pw_resp + 1);
106 recend = (const char *) found->data + found->recsize;
107 }
108 }
109
110 pw_response_header pw_resp_mem;
111 int sock = -1;
112 if (pw_resp == NULL)
113 {
114 sock = __nscd_open_socket (key, keylen, type, &pw_resp_mem,
115 sizeof (pw_resp_mem));
116 if (sock == -1)
117 {
118 __nss_not_use_nscd_passwd = 1;
119 goto out;
120 }
121
122 pw_resp = &pw_resp_mem;
123 }
124
125 /* No value found so far. */
126 *result = NULL;
127
128 if (__builtin_expect (pw_resp->found == -1, 0))
129 {
130 /* The daemon does not cache this database. */
131 __nss_not_use_nscd_passwd = 1;
132 goto out_close;
133 }
134
135 if (pw_resp->found == 1)
136 {
137 /* Set the information we already have. */
138 resultbuf->pw_uid = pw_resp->pw_uid;
139 resultbuf->pw_gid = pw_resp->pw_gid;
140
141 char *p = buffer;
142 /* get pw_name */
143 resultbuf->pw_name = p;
144 p += pw_resp->pw_name_len;
145 /* get pw_passwd */
146 resultbuf->pw_passwd = p;
147 p += pw_resp->pw_passwd_len;
148 /* get pw_gecos */
149 resultbuf->pw_gecos = p;
150 p += pw_resp->pw_gecos_len;
151 /* get pw_dir */
152 resultbuf->pw_dir = p;
153 p += pw_resp->pw_dir_len;
154 /* get pw_pshell */
155 resultbuf->pw_shell = p;
156 p += pw_resp->pw_shell_len;
157
158 ssize_t total = p - buffer;
159 if (__builtin_expect (pw_name + total > recend, 0))
160 goto out_close;
161 if (__builtin_expect (buflen < total, 0))
162 {
163 __set_errno (ERANGE);
164 retval = ERANGE;
165 goto out_close;
166 }
167
168 retval = 0;
169 if (pw_name == NULL)
170 {
171 ssize_t nbytes = TEMP_FAILURE_RETRY (__read (sock, buffer, total));
172
173 if (__builtin_expect (nbytes != total, 0))
174 {
175 /* The `errno' to some value != ERANGE. */
176 __set_errno (ENOENT);
177 retval = ENOENT;
178 }
179 else
180 *result = resultbuf;
181 }
182 else
183 {
184 /* Copy the various strings. */
185 memcpy (resultbuf->pw_name, pw_name, total);
186
187 *result = resultbuf;
188 }
189 }
190 else
191 {
192 /* The `errno' to some value != ERANGE. */
193 __set_errno (ENOENT);
194 /* Even though we have not found anything, the result is zero. */
195 retval = 0;
196 }
197
198 out_close:
199 if (sock != -1)
200 close_not_cancel_no_status (sock);
201 out:
202 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0 && retval != -1)
203 {
204 /* When we come here this means there has been a GC cycle while we
205 were looking for the data. This means the data might have been
206 inconsistent. Retry if possible. */
207 if ((gc_cycle & 1) != 0)
208 {
209 /* nscd is just running gc now. Disable using the mapping. */
210 __nscd_unmap (mapped);
211 mapped = NO_MAPPING;
212 }
213
214 free (resultbuf);
215
216 goto retry;
217 }
218
219 return retval;
220 }