]> git.ipfire.org Git - thirdparty/glibc.git/blob - nscd/nscd_getgr_r.c
Update.
[thirdparty/glibc.git] / nscd / nscd_getgr_r.c
1 /* Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004
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.
5
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.
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
14 Lesser General Public License for more details.
15
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. */
20
21 #include <alloca.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <grp.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <sys/socket.h>
32 #include <sys/uio.h>
33 #include <sys/un.h>
34 #include <not-cancel.h>
35 #include <stdio-common/_itoa.h>
36
37 #include "nscd-client.h"
38 #include "nscd_proto.h"
39
40 int __nss_not_use_nscd_group;
41
42 static int nscd_getgr_r (const char *key, size_t keylen, request_type type,
43 struct group *resultbuf, char *buffer,
44 size_t buflen, struct group **result)
45 internal_function;
46
47
48 int
49 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
50 size_t buflen, struct group **result)
51 {
52 return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
53 buffer, buflen, result);
54 }
55
56
57 int
58 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
59 size_t buflen, struct group **result)
60 {
61 char buf[3 * sizeof (gid_t)];
62 buf[sizeof (buf) - 1] = '\0';
63 char *cp = _itoa_word (gid, buf + sizeof (buf) - 1, 10, 0);
64
65 return nscd_getgr_r (cp, buf + sizeof (buf) - cp, GETGRBYGID, resultbuf,
66 buffer, buflen, result);
67 }
68
69
70 libc_locked_map_ptr (map_handle);
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. */
74 libc_freeres_fn (gr_map_free)
75 {
76 if (map_handle.mapped != NO_MAPPING)
77 free (map_handle.mapped);
78 }
79
80
81 static int
82 internal_function
83 nscd_getgr_r (const char *key, size_t keylen, request_type type,
84 struct group *resultbuf, char *buffer, size_t buflen,
85 struct group **result)
86 {
87 int gc_cycle;
88 const uint32_t *len = NULL;
89 size_t lensize = 0;
90
91 /* If the mapping is available, try to search there instead of
92 communicating with the nscd. */
93 struct mapped_database *mapped = __nscd_get_map_ref (GETFDGR, "group",
94 &map_handle, &gc_cycle);
95 retry:;
96 const gr_response_header *gr_resp = NULL;
97 const char *gr_name = NULL;
98 size_t gr_name_len = 0;
99 int retval = -1;
100 const char *recend = (const char *) ~UINTMAX_C (0);
101
102 if (mapped != NO_MAPPING)
103 {
104 const struct datahead *found = __nscd_cache_search (type, key, keylen,
105 mapped);
106 if (found != NULL)
107 {
108 gr_resp = &found->data[0].grdata;
109 len = (const uint32_t *) (gr_resp + 1);
110 /* The alignment is always sufficient. */
111 assert (((uintptr_t) len & (__alignof__ (*len) - 1)) == 0);
112 gr_name = ((const char *) len
113 + gr_resp->gr_mem_cnt * sizeof (uint32_t));
114 gr_name_len = gr_resp->gr_name_len + gr_resp->gr_passwd_len;
115 recend = (const char *) found->data + found->recsize;
116 }
117 }
118
119 gr_response_header gr_resp_mem;
120 int sock = -1;
121 if (gr_resp == NULL)
122 {
123 sock = __nscd_open_socket (key, keylen, type, &gr_resp_mem,
124 sizeof (gr_resp_mem));
125 if (sock == -1)
126 {
127 __nss_not_use_nscd_group = 1;
128 goto out;
129 }
130
131 gr_resp = &gr_resp_mem;
132 }
133
134 /* No value found so far. */
135 *result = NULL;
136
137 if (__builtin_expect (gr_resp->found == -1, 0))
138 {
139 /* The daemon does not cache this database. */
140 __nss_not_use_nscd_group = 1;
141 goto out_close;
142 }
143
144 if (gr_resp->found == 1)
145 {
146 struct iovec vec[2];
147 char *p = buffer;
148 size_t total_len;
149 uintptr_t align;
150 nscd_ssize_t cnt;
151
152 /* Now allocate the buffer the array for the group members. We must
153 align the pointer. */
154 align = ((__alignof__ (char *) - (p - ((char *) 0)))
155 & (__alignof__ (char *) - 1));
156 total_len = (align + (1 + gr_resp->gr_mem_cnt) * sizeof (char *)
157 + gr_resp->gr_name_len + gr_resp->gr_passwd_len);
158 if (__builtin_expect (buflen < total_len, 0))
159 {
160 no_room:
161 __set_errno (ERANGE);
162 retval = ERANGE;
163 goto out_close;
164 }
165 buflen -= total_len;
166
167 p += align;
168 resultbuf->gr_mem = (char **) p;
169 p += (1 + gr_resp->gr_mem_cnt) * sizeof (char *);
170
171 /* Set pointers for strings. */
172 resultbuf->gr_name = p;
173 p += gr_resp->gr_name_len;
174 resultbuf->gr_passwd = p;
175 p += gr_resp->gr_passwd_len;
176
177 /* Fill in what we know now. */
178 resultbuf->gr_gid = gr_resp->gr_gid;
179
180 /* Read the length information, group name, and password. */
181 if (gr_name == NULL)
182 {
183 /* Allocate array to store lengths. */
184 if (lensize == 0)
185 {
186 lensize = gr_resp->gr_mem_cnt * sizeof (uint32_t);
187 len = (uint32_t *) alloca (lensize);
188 }
189 else if (gr_resp->gr_mem_cnt * sizeof (uint32_t) > lensize)
190 len = extend_alloca (len, lensize,
191 gr_resp->gr_mem_cnt * sizeof (uint32_t));
192
193 vec[0].iov_base = (void *) len;
194 vec[0].iov_len = gr_resp->gr_mem_cnt * sizeof (uint32_t);
195 vec[1].iov_base = resultbuf->gr_name;
196 vec[1].iov_len = gr_resp->gr_name_len + gr_resp->gr_passwd_len;
197 total_len = vec[0].iov_len + vec[1].iov_len;
198
199 /* Get this data. */
200 size_t n = TEMP_FAILURE_RETRY (__readv (sock, vec, 2));
201 if (__builtin_expect (n != total_len, 0))
202 goto out_close;
203 }
204 else
205 /* We already have the data. Just copy the group name and
206 password. */
207 memcpy (resultbuf->gr_name, gr_name, gr_name_len);
208
209 /* Clear the terminating entry. */
210 resultbuf->gr_mem[gr_resp->gr_mem_cnt] = NULL;
211
212 /* Prepare reading the group members. */
213 total_len = 0;
214 for (cnt = 0; cnt < gr_resp->gr_mem_cnt; ++cnt)
215 {
216 resultbuf->gr_mem[cnt] = p;
217 total_len += len[cnt];
218 p += len[cnt];
219 }
220
221 if (__builtin_expect (gr_name + gr_name_len + total_len > recend, 0))
222 goto out_close;
223 if (__builtin_expect (total_len > buflen, 0))
224 goto no_room;
225
226 retval = 0;
227 if (gr_name == NULL)
228 {
229 size_t n = TEMP_FAILURE_RETRY (__read (sock, resultbuf->gr_mem[0],
230 total_len));
231 if (__builtin_expect (n != total_len, 0))
232 {
233 /* The `errno' to some value != ERANGE. */
234 __set_errno (ENOENT);
235 retval = ENOENT;
236 }
237 else
238 *result = resultbuf;
239 }
240 else
241 {
242 /* Copy the group member names. */
243 memcpy (resultbuf->gr_mem[0], gr_name + gr_name_len, total_len);
244
245 *result = resultbuf;
246 }
247 }
248 else
249 {
250 /* The `errno' to some value != ERANGE. */
251 __set_errno (ENOENT);
252 /* Even though we have not found anything, the result is zero. */
253 retval = 0;
254 }
255
256 out_close:
257 if (sock != -1)
258 close_not_cancel_no_status (sock);
259 out:
260 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0 && retval != -1)
261 {
262 /* When we come here this means there has been a GC cycle while we
263 were looking for the data. This means the data might have been
264 inconsistent. Retry if possible. */
265 if ((gc_cycle & 1) != 0)
266 {
267 /* nscd is just running gc now. Disable using the mapping. */
268 __nscd_unmap (mapped);
269 mapped = NO_MAPPING;
270 }
271
272 goto retry;
273 }
274
275 return retval;
276 }