1 /* Copyright (C) 1998-2017 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>
34 #include <scratch_buffer.h>
36 #include "nscd-client.h"
37 #include "nscd_proto.h"
39 int __nss_not_use_nscd_group
;
41 static int nscd_getgr_r (const char *key
, size_t keylen
, request_type type
,
42 struct group
*resultbuf
, char *buffer
,
43 size_t buflen
, struct group
**result
)
48 __nscd_getgrnam_r (const char *name
, struct group
*resultbuf
, char *buffer
,
49 size_t buflen
, struct group
**result
)
51 return nscd_getgr_r (name
, strlen (name
) + 1, GETGRBYNAME
, resultbuf
,
52 buffer
, buflen
, result
);
57 __nscd_getgrgid_r (gid_t gid
, struct group
*resultbuf
, char *buffer
,
58 size_t buflen
, struct group
**result
)
60 char buf
[3 * sizeof (gid_t
)];
61 buf
[sizeof (buf
) - 1] = '\0';
62 char *cp
= _itoa_word (gid
, buf
+ sizeof (buf
) - 1, 10, 0);
64 return nscd_getgr_r (cp
, buf
+ sizeof (buf
) - cp
, GETGRBYGID
, resultbuf
,
65 buffer
, buflen
, result
);
69 libc_locked_map_ptr (,__gr_map_handle
) attribute_hidden
;
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
73 libc_freeres_fn (gr_map_free
)
75 if (__gr_map_handle
.mapped
!= NO_MAPPING
)
77 void *p
= __gr_map_handle
.mapped
;
78 __gr_map_handle
.mapped
= NO_MAPPING
;
86 nscd_getgr_r (const char *key
, size_t keylen
, request_type type
,
87 struct group
*resultbuf
, char *buffer
, size_t buflen
,
88 struct group
**result
)
92 const uint32_t *len
= NULL
;
93 struct scratch_buffer lenbuf
;
94 scratch_buffer_init (&lenbuf
);
96 /* If the mapping is available, try to search there instead of
97 communicating with the nscd. */
98 struct mapped_database
*mapped
= __nscd_get_map_ref (GETFDGR
, "group",
102 const char *gr_name
= NULL
;
103 size_t gr_name_len
= 0;
105 const char *recend
= (const char *) ~UINTMAX_C (0);
106 gr_response_header gr_resp
;
108 if (mapped
!= NO_MAPPING
)
110 struct datahead
*found
= __nscd_cache_search (type
, key
, keylen
, mapped
,
114 len
= (const uint32_t *) (&found
->data
[0].grdata
+ 1);
115 gr_resp
= found
->data
[0].grdata
;
116 gr_name
= ((const char *) len
117 + gr_resp
.gr_mem_cnt
* sizeof (uint32_t));
118 gr_name_len
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
119 recend
= (const char *) found
->data
+ found
->recsize
;
120 /* Now check if we can trust gr_resp fields. If GC is
121 in progress, it can contain anything. */
122 if (mapped
->head
->gc_cycle
!= gc_cycle
)
128 /* The alignment is always sufficient, unless GC is in progress. */
129 assert (((uintptr_t) len
& (__alignof__ (*len
) - 1)) == 0);
136 sock
= __nscd_open_socket (key
, keylen
, type
, &gr_resp
,
140 __nss_not_use_nscd_group
= 1;
145 /* No value found so far. */
148 if (__glibc_unlikely (gr_resp
.found
== -1))
150 /* The daemon does not cache this database. */
151 __nss_not_use_nscd_group
= 1;
155 if (gr_resp
.found
== 1)
163 /* Now allocate the buffer the array for the group members. We must
164 align the pointer. */
165 align
= ((__alignof__ (char *) - (p
- ((char *) 0)))
166 & (__alignof__ (char *) - 1));
167 total_len
= (align
+ (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *)
168 + gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
);
169 if (__glibc_unlikely (buflen
< total_len
))
172 __set_errno (ERANGE
);
179 resultbuf
->gr_mem
= (char **) p
;
180 p
+= (1 + gr_resp
.gr_mem_cnt
) * sizeof (char *);
182 /* Set pointers for strings. */
183 resultbuf
->gr_name
= p
;
184 p
+= gr_resp
.gr_name_len
;
185 resultbuf
->gr_passwd
= p
;
186 p
+= gr_resp
.gr_passwd_len
;
188 /* Fill in what we know now. */
189 resultbuf
->gr_gid
= gr_resp
.gr_gid
;
191 /* Read the length information, group name, and password. */
194 /* Handle a simple, usual case: no group members. */
195 if (__glibc_likely (gr_resp
.gr_mem_cnt
== 0))
197 size_t n
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
198 if (__builtin_expect (__readall (sock
, resultbuf
->gr_name
, n
)
204 /* Allocate array to store lengths. */
205 if (!scratch_buffer_set_array_size
206 (&lenbuf
, gr_resp
.gr_mem_cnt
, sizeof (uint32_t)))
210 vec
[0].iov_base
= (void *) len
;
211 vec
[0].iov_len
= gr_resp
.gr_mem_cnt
* sizeof (uint32_t);
212 vec
[1].iov_base
= resultbuf
->gr_name
;
213 vec
[1].iov_len
= gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
;
214 total_len
= vec
[0].iov_len
+ vec
[1].iov_len
;
217 size_t n
= __readvall (sock
, vec
, 2);
218 if (__glibc_unlikely (n
!= total_len
))
223 /* We already have the data. Just copy the group name and
225 memcpy (resultbuf
->gr_name
, gr_name
,
226 gr_resp
.gr_name_len
+ gr_resp
.gr_passwd_len
);
228 /* Clear the terminating entry. */
229 resultbuf
->gr_mem
[gr_resp
.gr_mem_cnt
] = NULL
;
231 /* Prepare reading the group members. */
233 for (cnt
= 0; cnt
< gr_resp
.gr_mem_cnt
; ++cnt
)
235 resultbuf
->gr_mem
[cnt
] = p
;
236 total_len
+= len
[cnt
];
240 if (__glibc_unlikely (gr_name
+ gr_name_len
+ total_len
> recend
))
242 /* len array might contain garbage during nscd GC cycle,
243 retry rather than fail in that case. */
244 if (gr_name
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
248 if (__glibc_unlikely (total_len
> buflen
))
250 /* len array might contain garbage during nscd GC cycle,
251 retry rather than fail in that case. */
252 if (gr_name
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
263 /* If there are no group members TOTAL_LEN is zero. */
267 && __builtin_expect (__readall (sock
, resultbuf
->gr_mem
[0],
268 total_len
) != total_len
, 0))
270 /* The `errno' to some value != ERANGE. */
271 __set_errno (ENOENT
);
279 /* Copy the group member names. */
280 memcpy (resultbuf
->gr_mem
[0], gr_name
+ gr_name_len
, total_len
);
282 /* Try to detect corrupt databases. */
283 if (resultbuf
->gr_name
[gr_name_len
- 1] != '\0'
284 || resultbuf
->gr_passwd
[gr_resp
.gr_passwd_len
- 1] != '\0'
285 || ({for (cnt
= 0; cnt
< gr_resp
.gr_mem_cnt
; ++cnt
)
286 if (resultbuf
->gr_mem
[cnt
][len
[cnt
] - 1] != '\0')
288 cnt
< gr_resp
.gr_mem_cnt
; }))
290 /* We cannot use the database. */
291 retval
= mapped
->head
->gc_cycle
!= gc_cycle
? -2 : -1;
300 /* Set errno to 0 to indicate no error, just no found record. */
302 /* Even though we have not found anything, the result is zero. */
308 __close_nocancel_nostatus (sock
);
310 if (__nscd_drop_map_ref (mapped
, &gc_cycle
) != 0)
312 /* When we come here this means there has been a GC cycle while we
313 were looking for the data. This means the data might have been
314 inconsistent. Retry if possible. */
315 if ((gc_cycle
& 1) != 0 || ++nretries
== 5 || retval
== -1)
317 /* nscd is just running gc now. Disable using the mapping. */
318 if (atomic_decrement_val (&mapped
->counter
) == 0)
319 __nscd_unmap (mapped
);
327 scratch_buffer_free (&lenbuf
);