1 /* Copyright (C) 1998-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
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.
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.
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/>. */
29 #include <sys/socket.h>
32 #include <not-cancel.h>
35 #include "nscd-client.h"
36 #include "nscd_proto.h"
38 int __nss_not_use_nscd_group
;
40 static int nscd_getgr_r (const char *key
, size_t keylen
, request_type type
,
41 struct group
*resultbuf
, char *buffer
,
42 size_t buflen
, struct group
**result
)
47 __nscd_getgrnam_r (const char *name
, struct group
*resultbuf
, char *buffer
,
48 size_t buflen
, struct group
**result
)
50 return nscd_getgr_r (name
, strlen (name
) + 1, GETGRBYNAME
, resultbuf
,
51 buffer
, buflen
, result
);
56 __nscd_getgrgid_r (gid_t gid
, struct group
*resultbuf
, char *buffer
,
57 size_t buflen
, struct group
**result
)
59 char buf
[3 * sizeof (gid_t
)];
60 buf
[sizeof (buf
) - 1] = '\0';
61 char *cp
= _itoa_word (gid
, buf
+ sizeof (buf
) - 1, 10, 0);
63 return nscd_getgr_r (cp
, buf
+ sizeof (buf
) - cp
, GETGRBYGID
, resultbuf
,
64 buffer
, buflen
, result
);
68 libc_locked_map_ptr (,__gr_map_handle
) attribute_hidden
;
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
72 libc_freeres_fn (gr_map_free
)
74 if (__gr_map_handle
.mapped
!= NO_MAPPING
)
76 void *p
= __gr_map_handle
.mapped
;
77 __gr_map_handle
.mapped
= NO_MAPPING
;
85 nscd_getgr_r (const char *key
, size_t keylen
, request_type type
,
86 struct group
*resultbuf
, char *buffer
, size_t buflen
,
87 struct group
**result
)
91 const uint32_t *len
= NULL
;
94 /* If the mapping is available, try to search there instead of
95 communicating with the nscd. */
96 struct mapped_database
*mapped
= __nscd_get_map_ref (GETFDGR
, "group",
100 const char *gr_name
= NULL
;
101 size_t gr_name_len
= 0;
103 const char *recend
= (const char *) ~UINTMAX_C (0);
104 gr_response_header gr_resp
;
106 if (mapped
!= NO_MAPPING
)
108 struct datahead
*found
= __nscd_cache_search (type
, key
, keylen
, mapped
,
112 len
= (const uint32_t *) (&found
->data
[0].grdata
+ 1);
113 gr_resp
= found
->data
[0].grdata
;
114 gr_name
= ((const char *) len
115 + gr_resp
.gr_mem_cnt
* sizeof (uint32_t));
116 gr_name_len
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
117 recend
= (const char *) found
->data
+ found
->recsize
;
118 /* Now check if we can trust gr_resp fields. If GC is
119 in progress, it can contain anything. */
120 if (mapped
->head
->gc_cycle
!= gc_cycle
)
126 /* The alignment is always sufficient, unless GC is in progress. */
127 assert (((uintptr_t) len
& (__alignof__ (*len
) - 1)) == 0);
134 sock
= __nscd_open_socket (key
, keylen
, type
, &gr_resp
,
138 __nss_not_use_nscd_group
= 1;
143 /* No value found so far. */
146 if (__glibc_unlikely (gr_resp
.found
== -1))
148 /* The daemon does not cache this database. */
149 __nss_not_use_nscd_group
= 1;
153 if (gr_resp
.found
== 1)
161 /* Now allocate the buffer the array for the group members. We must
162 align the pointer. */
163 align
= ((__alignof__ (char *) - (p
- ((char *) 0)))
164 & (__alignof__ (char *) - 1));
165 total_len
= (align
+ (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *)
166 + gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
);
167 if (__glibc_unlikely (buflen
< total_len
))
170 __set_errno (ERANGE
);
177 resultbuf
->gr_mem
= (char **) p
;
178 p
+= (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *);
180 /* Set pointers for strings. */
181 resultbuf
->gr_name
= p
;
182 p
+= gr_resp
.gr_name_len
;
183 resultbuf
->gr_passwd
= p
;
184 p
+= gr_resp
.gr_passwd_len
;
186 /* Fill in what we know now. */
187 resultbuf
->gr_gid
= gr_resp
.gr_gid
;
189 /* Read the length information, group name, and password. */
192 /* Handle a simple, usual case: no group members. */
193 if (__glibc_likely (gr_resp
.gr_mem_cnt
== 0))
195 size_t n
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
196 if (__builtin_expect (__readall (sock
, resultbuf
->gr_name
, n
)
202 /* Allocate array to store lengths. */
205 lensize
= gr_resp
.gr_mem_cnt
* sizeof (uint32_t);
206 len
= (uint32_t *) alloca (lensize
);
208 else if (gr_resp
.gr_mem_cnt
* sizeof (uint32_t) > lensize
)
209 len
= extend_alloca (len
, lensize
,
210 gr_resp
.gr_mem_cnt
* sizeof (uint32_t));
212 vec
[0].iov_base
= (void *) len
;
213 vec
[0].iov_len
= gr_resp
.gr_mem_cnt
* sizeof (uint32_t);
214 vec
[1].iov_base
= resultbuf
->gr_name
;
215 vec
[1].iov_len
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
216 total_len
= vec
[0].iov_len
+ vec
[1].iov_len
;
219 size_t n
= __readvall (sock
, vec
, 2);
220 if (__glibc_unlikely (n
!= total_len
))
225 /* We already have the data. Just copy the group name and
227 memcpy (resultbuf
->gr_name
, gr_name
,
228 gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
);
230 /* Clear the terminating entry. */
231 resultbuf
->gr_mem
[gr_resp
.gr_mem_cnt
] = NULL
;
233 /* Prepare reading the group members. */
235 for (cnt
= 0; cnt
< gr_resp
.gr_mem_cnt
; ++cnt
)
237 resultbuf
->gr_mem
[cnt
] = p
;
238 total_len
+= len
[cnt
];
242 if (__glibc_unlikely (gr_name
+ gr_name_len
+ total_len
> recend
))
244 /* len array might contain garbage during nscd GC cycle,
245 retry rather than fail in that case. */
246 if (gr_name
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
250 if (__glibc_unlikely (total_len
> buflen
))
252 /* len array might contain garbage during nscd GC cycle,
253 retry rather than fail in that case. */
254 if (gr_name
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
265 /* If there are no group members TOTAL_LEN is zero. */
269 && __builtin_expect (__readall (sock
, resultbuf
->gr_mem
[0],
270 total_len
) != total_len
, 0))
272 /* The `errno' to some value != ERANGE. */
273 __set_errno (ENOENT
);
281 /* Copy the group member names. */
282 memcpy (resultbuf
->gr_mem
[0], gr_name
+ gr_name_len
, total_len
);
284 /* Try to detect corrupt databases. */
285 if (resultbuf
->gr_name
[gr_name_len
- 1] != '\0'
286 || resultbuf
->gr_passwd
[gr_resp
.gr_passwd_len
- 1] != '\0'
287 || ({for (cnt
= 0; cnt
< gr_resp
.gr_mem_cnt
; ++cnt
)
288 if (resultbuf
->gr_mem
[cnt
][len
[cnt
] - 1] != '\0')
290 cnt
< gr_resp
.gr_mem_cnt
; }))
292 /* We cannot use the database. */
293 retval
= mapped
->head
->gc_cycle
!= gc_cycle
? -2 : -1;
302 /* Set errno to 0 to indicate no error, just no found record. */
304 /* Even though we have not found anything, the result is zero. */
310 close_not_cancel_no_status (sock
);
312 if (__nscd_drop_map_ref (mapped
, &gc_cycle
) != 0)
314 /* When we come here this means there has been a GC cycle while we
315 were looking for the data. This means the data might have been
316 inconsistent. Retry if possible. */
317 if ((gc_cycle
& 1) != 0 || ++nretries
== 5 || retval
== -1)
319 /* nscd is just running gc now. Disable using the mapping. */
320 if (atomic_decrement_val (&mapped
->counter
) == 0)
321 __nscd_unmap (mapped
);