1 /* Copyright (C) 1998, 1999 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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,
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);
63 /* Create a socket connected to a name. */
67 struct sockaddr_un addr;
69 int saved_errno = errno;
71 sock = __socket (PF_UNIX, SOCK_STREAM, 0);
74 __set_errno (saved_errno);
78 addr.sun_family = AF_UNIX;
79 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
80 if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
83 __set_errno (saved_errno);
92 nscd_getgr_r (const char *key, size_t keylen, request_type type,
93 struct group *resultbuf, char *buffer, size_t buflen)
95 int sock = open_socket ();
97 gr_response_header gr_resp;
103 __nss_not_use_nscd_group = 1;
107 req.version = NSCD_VERSION;
109 req.key_len = keylen;
111 vec[0].iov_base = &req;
112 vec[0].iov_len = sizeof (request_header);
113 vec[1].iov_base = (void *) key;
114 vec[1].iov_len = keylen;
116 if (__writev (sock, vec, 2) != sizeof (request_header) + keylen)
122 nbytes = __read (sock, &gr_resp, sizeof (gr_response_header));
123 if (nbytes != sizeof (gr_response_header))
129 if (gr_resp.found == -1)
131 /* The daemon does not cache this database. */
133 __nss_not_use_nscd_group = 1;
137 if (gr_resp.found == 1)
145 /* Now allocate the buffer the array for the group members. We must
146 align the pointer. */
147 align = ((__alignof__ (char *) - (p - ((char *) 0)))
148 & (__alignof__ (char *) - 1));
149 if (buflen < (align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
150 + gr_resp.gr_name_len + gr_resp.gr_passwd_len))
153 __set_errno (ERANGE);
159 resultbuf->gr_mem = (char **) p;
160 p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
161 buflen -= align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
163 /* Set pointers for strings. */
164 resultbuf->gr_name = p;
165 p += gr_resp.gr_name_len;
166 resultbuf->gr_passwd = p;
167 p += gr_resp.gr_passwd_len;
169 /* Fill in what we know now. */
170 resultbuf->gr_gid = gr_resp.gr_gid;
172 /* Allocate array to store lengths. */
173 len = alloca (gr_resp.gr_mem_cnt * sizeof (size_t));
175 total_len = gr_resp.gr_mem_cnt * sizeof (size_t);
176 vec[0].iov_base = len;
177 vec[0].iov_len = total_len;
178 vec[1].iov_base = resultbuf->gr_name;
179 vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
180 total_len += gr_resp.gr_name_len + gr_resp.gr_passwd_len;
185 if (__readv (sock, vec, 2) != total_len)
191 /* Clear the terminating entry. */
192 resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
194 /* Prepare reading the group members. */
196 for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
198 resultbuf->gr_mem[cnt] = p;
199 total_len += len[cnt];
203 if (total_len > buflen)
206 if (__read (sock, resultbuf->gr_mem[0], total_len) != total_len)