1 /* Copyright (C) 1998-2000, 2002-2005, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 #include <sys/socket.h>
33 #include <not-cancel.h>
34 #include <stdio-common/_itoa.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
)
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 gr_response_header
*gr_resp
= NULL
;
101 const char *gr_name
= NULL
;
102 size_t gr_name_len
= 0;
104 const char *recend
= (const char *) ~UINTMAX_C (0);
106 if (mapped
!= NO_MAPPING
)
108 const struct datahead
*found
= __nscd_cache_search (type
, key
, keylen
,
112 gr_resp
= &found
->data
[0].grdata
;
113 len
= (const uint32_t *) (gr_resp
+ 1);
114 /* The alignment is always sufficient. */
115 assert (((uintptr_t) len
& (__alignof__ (*len
) - 1)) == 0);
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
;
123 gr_response_header gr_resp_mem
;
127 sock
= __nscd_open_socket (key
, keylen
, type
, &gr_resp_mem
,
128 sizeof (gr_resp_mem
));
131 __nss_not_use_nscd_group
= 1;
135 gr_resp
= &gr_resp_mem
;
138 /* No value found so far. */
141 if (__builtin_expect (gr_resp
->found
== -1, 0))
143 /* The daemon does not cache this database. */
144 __nss_not_use_nscd_group
= 1;
148 if (gr_resp
->found
== 1)
156 /* Now allocate the buffer the array for the group members. We must
157 align the pointer. */
158 align
= ((__alignof__ (char *) - (p
- ((char *) 0)))
159 & (__alignof__ (char *) - 1));
160 total_len
= (align
+ (1 + gr_resp
->gr_mem_cnt
) * sizeof (char *)
161 + gr_resp
->gr_name_len
+ gr_resp
->gr_passwd_len
);
162 if (__builtin_expect (buflen
< total_len
, 0))
165 __set_errno (ERANGE
);
172 resultbuf
->gr_mem
= (char **) p
;
173 p
+= (1 + gr_resp
->gr_mem_cnt
) * sizeof (char *);
175 /* Set pointers for strings. */
176 resultbuf
->gr_name
= p
;
177 p
+= gr_resp
->gr_name_len
;
178 resultbuf
->gr_passwd
= p
;
179 p
+= gr_resp
->gr_passwd_len
;
181 /* Fill in what we know now. */
182 resultbuf
->gr_gid
= gr_resp
->gr_gid
;
184 /* Read the length information, group name, and password. */
187 /* Allocate array to store lengths. */
190 lensize
= gr_resp
->gr_mem_cnt
* sizeof (uint32_t);
191 len
= (uint32_t *) alloca (lensize
);
193 else if (gr_resp
->gr_mem_cnt
* sizeof (uint32_t) > lensize
)
194 len
= extend_alloca (len
, lensize
,
195 gr_resp
->gr_mem_cnt
* sizeof (uint32_t));
197 vec
[0].iov_base
= (void *) len
;
198 vec
[0].iov_len
= gr_resp
->gr_mem_cnt
* sizeof (uint32_t);
199 vec
[1].iov_base
= resultbuf
->gr_name
;
200 vec
[1].iov_len
= gr_resp
->gr_name_len
+ gr_resp
->gr_passwd_len
;
201 total_len
= vec
[0].iov_len
+ vec
[1].iov_len
;
204 size_t n
= __readvall (sock
, vec
, 2);
205 if (__builtin_expect (n
!= total_len
, 0))
209 /* We already have the data. Just copy the group name and
211 memcpy (resultbuf
->gr_name
, gr_name
,
212 gr_resp
->gr_name_len
+ gr_resp
->gr_passwd_len
);
214 /* Clear the terminating entry. */
215 resultbuf
->gr_mem
[gr_resp
->gr_mem_cnt
] = NULL
;
217 /* Prepare reading the group members. */
219 for (cnt
= 0; cnt
< gr_resp
->gr_mem_cnt
; ++cnt
)
221 resultbuf
->gr_mem
[cnt
] = p
;
222 total_len
+= len
[cnt
];
226 if (__builtin_expect (gr_name
+ gr_name_len
+ total_len
> recend
, 0))
228 if (__builtin_expect (total_len
> buflen
, 0))
234 size_t n
= __readall (sock
, resultbuf
->gr_mem
[0], total_len
);
235 if (__builtin_expect (n
!= total_len
, 0))
237 /* The `errno' to some value != ERANGE. */
238 __set_errno (ENOENT
);
246 /* Copy the group member names. */
247 memcpy (resultbuf
->gr_mem
[0], gr_name
+ gr_name_len
, total_len
);
249 /* Try to detect corrupt databases. */
250 if (resultbuf
->gr_name
[gr_name_len
- 1] != '\0'
251 || resultbuf
->gr_passwd
[gr_resp
->gr_passwd_len
- 1] != '\0'
252 || ({for (cnt
= 0; cnt
< gr_resp
->gr_mem_cnt
; ++cnt
)
253 if (resultbuf
->gr_mem
[cnt
][len
[cnt
] - 1] != '\0')
255 cnt
< gr_resp
->gr_mem_cnt
; }))
257 /* We cannot use the database. */
267 /* The `errno' to some value != ERANGE. */
268 __set_errno (ENOENT
);
269 /* Even though we have not found anything, the result is zero. */
275 close_not_cancel_no_status (sock
);
277 if (__nscd_drop_map_ref (mapped
, &gc_cycle
) != 0 && retval
!= -1)
279 /* When we come here this means there has been a GC cycle while we
280 were looking for the data. This means the data might have been
281 inconsistent. Retry if possible. */
282 if ((gc_cycle
& 1) != 0)
284 /* nscd is just running gc now. Disable using the mapping. */
285 __nscd_unmap (mapped
);