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.
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.
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.
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
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 (map_handle
);
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
)
76 if (map_handle
.mapped
!= NO_MAPPING
)
77 free (map_handle
.mapped
);
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
)
87 const gr_response_header
*gr_resp
= NULL
;
88 const uint32_t *len
= NULL
;
89 const char *gr_name
= NULL
;
90 size_t gr_name_len
= 0;
93 const char *recend
= (const char *) ~UINTMAX_C (0);
95 /* If the mapping is available, try to search there instead of
96 communicating with the nscd. */
97 struct mapped_database
*mapped
= __nscd_get_map_ref (GETFDGR
, "group",
98 &map_handle
, &gc_cycle
);
100 if (mapped
!= NO_MAPPING
)
102 const struct datahead
*found
= __nscd_cache_search (type
, key
, keylen
,
106 gr_resp
= &found
->data
[0].grdata
;
107 len
= (const uint32_t *) (gr_resp
+ 1);
108 /* The alignment is always sufficient. */
109 assert (((uintptr_t) len
& (__alignof__ (*len
) - 1)) == 0);
110 gr_name
= ((const char *) len
111 + gr_resp
->gr_mem_cnt
* sizeof (uint32_t));
112 gr_name_len
= gr_resp
->gr_name_len
+ gr_resp
->gr_passwd_len
;
113 recend
= (const char *) found
->data
+ found
->recsize
;
117 gr_response_header gr_resp_mem
;
121 sock
= __nscd_open_socket (key
, keylen
, type
, &gr_resp_mem
,
122 sizeof (gr_resp_mem
));
125 __nss_not_use_nscd_group
= 1;
129 gr_resp
= &gr_resp_mem
;
132 /* No value found so far. */
135 if (__builtin_expect (gr_resp
->found
== -1, 0))
137 /* The daemon does not cache this database. */
138 __nss_not_use_nscd_group
= 1;
142 if (gr_resp
->found
== 1)
150 /* Now allocate the buffer the array for the group members. We must
151 align the pointer. */
152 align
= ((__alignof__ (char *) - (p
- ((char *) 0)))
153 & (__alignof__ (char *) - 1));
154 total_len
= (align
+ (1 + gr_resp
->gr_mem_cnt
) * sizeof (char *)
155 + gr_resp
->gr_name_len
+ gr_resp
->gr_passwd_len
);
156 if (__builtin_expect (buflen
< total_len
, 0))
159 __set_errno (ERANGE
);
166 resultbuf
->gr_mem
= (char **) p
;
167 p
+= (1 + gr_resp
->gr_mem_cnt
) * sizeof (char *);
169 /* Set pointers for strings. */
170 resultbuf
->gr_name
= p
;
171 p
+= gr_resp
->gr_name_len
;
172 resultbuf
->gr_passwd
= p
;
173 p
+= gr_resp
->gr_passwd_len
;
175 /* Fill in what we know now. */
176 resultbuf
->gr_gid
= gr_resp
->gr_gid
;
178 /* Read the length information, group name, and password. */
181 /* Allocate array to store lengths. */
182 len
= (uint32_t *) alloca (gr_resp
->gr_mem_cnt
* sizeof (uint32_t));
184 vec
[0].iov_base
= (void *) len
;
185 vec
[0].iov_len
= gr_resp
->gr_mem_cnt
* sizeof (uint32_t);
186 vec
[1].iov_base
= resultbuf
->gr_name
;
187 vec
[1].iov_len
= gr_resp
->gr_name_len
+ gr_resp
->gr_passwd_len
;
188 total_len
= vec
[0].iov_len
+ vec
[1].iov_len
;
191 size_t n
= TEMP_FAILURE_RETRY (__readv (sock
, vec
, 2));
192 if (__builtin_expect (n
!= total_len
, 0))
196 /* We already have the data. Just copy the group name and
198 memcpy (resultbuf
->gr_name
, gr_name
, gr_name_len
);
200 /* Clear the terminating entry. */
201 resultbuf
->gr_mem
[gr_resp
->gr_mem_cnt
] = NULL
;
203 /* Prepare reading the group members. */
205 for (cnt
= 0; cnt
< gr_resp
->gr_mem_cnt
; ++cnt
)
207 resultbuf
->gr_mem
[cnt
] = p
;
208 total_len
+= len
[cnt
];
212 if (__builtin_expect (gr_name
+ gr_name_len
+ total_len
> recend
, 0))
214 if (__builtin_expect (total_len
> buflen
, 0))
220 size_t n
= TEMP_FAILURE_RETRY (__read (sock
, resultbuf
->gr_mem
[0],
222 if (__builtin_expect (n
!= total_len
, 0))
224 /* The `errno' to some value != ERANGE. */
225 __set_errno (ENOENT
);
233 /* Copy the group member names. */
234 memcpy (resultbuf
->gr_mem
[0], gr_name
+ gr_name_len
, total_len
);
241 /* The `errno' to some value != ERANGE. */
242 __set_errno (ENOENT
);
243 /* Even though we have not found anything, the result is zero. */
249 close_not_cancel_no_status (sock
);
251 if (__nscd_drop_map_ref (mapped
, gc_cycle
) != 0)
252 /* When we come here this means there has been a GC cycle while we
253 were looking for the data. This means the data might have been
254 inconsistent. Retry. */