1 /* Copyright (C) 1998-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
28 #include <sys/socket.h>
31 #include <not-cancel.h>
33 #include <scratch_buffer.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
);
46 __nscd_getgrnam_r (const char *name
, struct group
*resultbuf
, char *buffer
,
47 size_t buflen
, struct group
**result
)
49 return nscd_getgr_r (name
, strlen (name
) + 1, GETGRBYNAME
, resultbuf
,
50 buffer
, buflen
, result
);
55 __nscd_getgrgid_r (gid_t gid
, struct group
*resultbuf
, char *buffer
,
56 size_t buflen
, struct group
**result
)
58 char buf
[3 * sizeof (gid_t
)];
59 buf
[sizeof (buf
) - 1] = '\0';
60 char *cp
= _itoa_word (gid
, buf
+ sizeof (buf
) - 1, 10, 0);
62 return nscd_getgr_r (cp
, buf
+ sizeof (buf
) - cp
, GETGRBYGID
, resultbuf
,
63 buffer
, buflen
, result
);
67 libc_locked_map_ptr (,__gr_map_handle
) attribute_hidden
;
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
71 libc_freeres_fn (gr_map_free
)
73 if (__gr_map_handle
.mapped
!= NO_MAPPING
)
75 void *p
= __gr_map_handle
.mapped
;
76 __gr_map_handle
.mapped
= NO_MAPPING
;
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
)
89 const uint32_t *len
= NULL
;
90 struct scratch_buffer lenbuf
;
91 scratch_buffer_init (&lenbuf
);
93 /* If the mapping is available, try to search there instead of
94 communicating with the nscd. */
95 struct mapped_database
*mapped
= __nscd_get_map_ref (GETFDGR
, "group",
99 const char *gr_name
= NULL
;
100 size_t gr_name_len
= 0;
102 const char *recend
= (const char *) ~UINTMAX_C (0);
103 gr_response_header gr_resp
;
105 if (mapped
!= NO_MAPPING
)
107 struct datahead
*found
= __nscd_cache_search (type
, key
, keylen
, mapped
,
111 len
= (const uint32_t *) (&found
->data
[0].grdata
+ 1);
112 gr_resp
= found
->data
[0].grdata
;
113 gr_name
= ((const char *) len
114 + gr_resp
.gr_mem_cnt
* sizeof (uint32_t));
115 gr_name_len
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
116 recend
= (const char *) found
->data
+ found
->recsize
;
117 /* Now check if we can trust gr_resp fields. If GC is
118 in progress, it can contain anything. */
119 if (mapped
->head
->gc_cycle
!= gc_cycle
)
125 /* The alignment is always sufficient, unless GC is in progress. */
126 assert (((uintptr_t) len
& (__alignof__ (*len
) - 1)) == 0);
133 sock
= __nscd_open_socket (key
, keylen
, type
, &gr_resp
,
137 __nss_not_use_nscd_group
= 1;
142 /* No value found so far. */
145 if (__glibc_unlikely (gr_resp
.found
== -1))
147 /* The daemon does not cache this database. */
148 __nss_not_use_nscd_group
= 1;
152 if (gr_resp
.found
== 1)
160 /* Now allocate the buffer the array for the group members. We must
161 align the pointer. */
162 align
= ((__alignof__ (char *) - (p
- ((char *) 0)))
163 & (__alignof__ (char *) - 1));
164 total_len
= (align
+ (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *)
165 + gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
);
166 if (__glibc_unlikely (buflen
< total_len
))
169 __set_errno (ERANGE
);
176 resultbuf
->gr_mem
= (char **) p
;
177 p
+= (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *);
179 /* Set pointers for strings. */
180 resultbuf
->gr_name
= p
;
181 p
+= gr_resp
.gr_name_len
;
182 resultbuf
->gr_passwd
= p
;
183 p
+= gr_resp
.gr_passwd_len
;
185 /* Fill in what we know now. */
186 resultbuf
->gr_gid
= gr_resp
.gr_gid
;
188 /* Read the length information, group name, and password. */
191 /* Handle a simple, usual case: no group members. */
192 if (__glibc_likely (gr_resp
.gr_mem_cnt
== 0))
194 size_t n
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
195 if (__builtin_expect (__readall (sock
, resultbuf
->gr_name
, n
)
201 /* Allocate array to store lengths. */
202 if (!scratch_buffer_set_array_size
203 (&lenbuf
, gr_resp
.gr_mem_cnt
, sizeof (uint32_t)))
207 vec
[0].iov_base
= (void *) len
;
208 vec
[0].iov_len
= gr_resp
.gr_mem_cnt
* sizeof (uint32_t);
209 vec
[1].iov_base
= resultbuf
->gr_name
;
210 vec
[1].iov_len
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
211 total_len
= vec
[0].iov_len
+ vec
[1].iov_len
;
214 size_t n
= __readvall (sock
, vec
, 2);
215 if (__glibc_unlikely (n
!= total_len
))
220 /* We already have the data. Just copy the group name and
222 memcpy (resultbuf
->gr_name
, gr_name
,
223 gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
);
225 /* Clear the terminating entry. */
226 resultbuf
->gr_mem
[gr_resp
.gr_mem_cnt
] = NULL
;
228 /* Prepare reading the group members. */
230 for (cnt
= 0; cnt
< gr_resp
.gr_mem_cnt
; ++cnt
)
232 resultbuf
->gr_mem
[cnt
] = p
;
233 total_len
+= len
[cnt
];
237 if (__glibc_unlikely (gr_name
+ gr_name_len
+ total_len
> recend
))
239 /* len array might contain garbage during nscd GC cycle,
240 retry rather than fail in that case. */
241 if (gr_name
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
245 if (__glibc_unlikely (total_len
> buflen
))
247 /* len array might contain garbage during nscd GC cycle,
248 retry rather than fail in that case. */
249 if (gr_name
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
260 /* If there are no group members TOTAL_LEN is zero. */
264 && __builtin_expect (__readall (sock
, resultbuf
->gr_mem
[0],
265 total_len
) != total_len
, 0))
267 /* The `errno' to some value != ERANGE. */
268 __set_errno (ENOENT
);
276 /* Copy the group member names. */
277 memcpy (resultbuf
->gr_mem
[0], gr_name
+ gr_name_len
, total_len
);
279 /* Try to detect corrupt databases. */
280 if (resultbuf
->gr_name
[gr_name_len
- 1] != '\0'
281 || resultbuf
->gr_passwd
[gr_resp
.gr_passwd_len
- 1] != '\0'
282 || ({for (cnt
= 0; cnt
< gr_resp
.gr_mem_cnt
; ++cnt
)
283 if (resultbuf
->gr_mem
[cnt
][len
[cnt
] - 1] != '\0')
285 cnt
< gr_resp
.gr_mem_cnt
; }))
287 /* We cannot use the database. */
288 retval
= mapped
->head
->gc_cycle
!= gc_cycle
? -2 : -1;
297 /* Set errno to 0 to indicate no error, just no found record. */
299 /* Even though we have not found anything, the result is zero. */
305 __close_nocancel_nostatus (sock
);
307 if (__nscd_drop_map_ref (mapped
, &gc_cycle
) != 0)
309 /* When we come here this means there has been a GC cycle while we
310 were looking for the data. This means the data might have been
311 inconsistent. Retry if possible. */
312 if ((gc_cycle
& 1) != 0 || ++nretries
== 5 || retval
== -1)
314 /* nscd is just running gc now. Disable using the mapping. */
315 if (atomic_fetch_add_relaxed (&mapped
->counter
, -1) == 1)
316 __nscd_unmap (mapped
);
324 scratch_buffer_free (&lenbuf
);