1 /* Copyright (C) 1998, 1999, 2000, 2002, 2003 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
27 #include <sys/socket.h>
31 #include "nscd-client.h"
32 #include "nscd_proto.h"
34 int __nss_not_use_nscd_group;
36 static int nscd_getgr_r (const char *key, size_t keylen, request_type type,
37 struct group *resultbuf, char *buffer,
38 size_t buflen) internal_function;
42 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
45 return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
51 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
57 n = __snprintf (buf, sizeof (buf), "%d", gid) + 1;
59 return nscd_getgr_r (buf, n, GETGRBYGID, resultbuf, buffer, buflen);
65 nscd_getgr_r (const char *key, size_t keylen, request_type type,
66 struct group *resultbuf, char *buffer, size_t buflen)
68 int sock = __nscd_open_socket ();
70 gr_response_header gr_resp;
77 __nss_not_use_nscd_group = 1;
81 req.version = NSCD_VERSION;
85 vec[0].iov_base = &req;
86 vec[0].iov_len = sizeof (request_header);
87 vec[1].iov_base = (void *) key;
88 vec[1].iov_len = keylen;
90 nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
91 if (nbytes != (ssize_t) (sizeof (request_header) + keylen))
94 nbytes = TEMP_FAILURE_RETRY (__read (sock, &gr_resp,
95 sizeof (gr_response_header)));
96 if (nbytes != (ssize_t) sizeof (gr_response_header))
99 if (gr_resp.found == -1)
101 /* The daemon does not cache this database. */
102 __nss_not_use_nscd_group = 1;
106 if (gr_resp.found == 1)
114 /* Now allocate the buffer the array for the group members. We must
115 align the pointer. */
116 align = ((__alignof__ (char *) - (p - ((char *) 0)))
117 & (__alignof__ (char *) - 1));
118 total_len = align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
119 + gr_resp.gr_name_len + gr_resp.gr_passwd_len;
120 if (__builtin_expect (buflen < total_len, 0))
123 __set_errno (ERANGE);
130 resultbuf->gr_mem = (char **) p;
131 p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
133 /* Set pointers for strings. */
134 resultbuf->gr_name = p;
135 p += gr_resp.gr_name_len;
136 resultbuf->gr_passwd = p;
137 p += gr_resp.gr_passwd_len;
139 /* Fill in what we know now. */
140 resultbuf->gr_gid = gr_resp.gr_gid;
142 /* Allocate array to store lengths. */
143 len = (uint32_t *) alloca (gr_resp.gr_mem_cnt * sizeof (uint32_t));
145 total_len = gr_resp.gr_mem_cnt * sizeof (uint32_t);
146 vec[0].iov_base = len;
147 vec[0].iov_len = total_len;
148 vec[1].iov_base = resultbuf->gr_name;
149 vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
150 total_len += gr_resp.gr_name_len + gr_resp.gr_passwd_len;
153 size_t n = TEMP_FAILURE_RETRY (__readv (sock, vec, 2));
154 if (__builtin_expect (n != total_len, 0))
157 /* Clear the terminating entry. */
158 resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
160 /* Prepare reading the group members. */
162 for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
164 resultbuf->gr_mem[cnt] = p;
165 total_len += len[cnt];
169 if (__builtin_expect (total_len > buflen, 0))
173 n = TEMP_FAILURE_RETRY (__read (sock, resultbuf->gr_mem[0],
175 if (__builtin_expect (n != total_len, 0))
177 /* The `errno' to some value != ERANGE. */
178 __set_errno (ENOENT);
184 /* The `errno' to some value != ERANGE. */
185 __set_errno (ENOENT);