Update.
[glibc.git] / nscd / nscd_getgr_r.c
blobec4f5a12973c2095a5f56c92ae99fa8ed0a1ce58
1 /* Copyright (C) 1998 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. */
20 #include <errno.h>
21 #include <grp.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
27 #include <sys/uio.h>
28 #include <sys/un.h>
30 #include "nscd.h"
31 #include "nscd_proto.h"
33 int __nss_not_use_nscd_group;
35 static int __nscd_getgr_r (const char *key, request_type type,
36 struct group *resultbuf, char *buffer,
37 size_t buflen);
39 int
40 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
41 size_t buflen)
43 if (name == NULL)
44 return 1;
46 return __nscd_getgr_r (name, GETGRBYNAME, resultbuf, buffer, buflen);
49 int
50 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
51 size_t buflen)
53 char *p = buffer;
54 int plen;
56 plen = __snprintf (buffer, buflen, "%d", gid);
57 if (plen == -1)
59 __set_errno (ERANGE);
60 return -1;
62 p = buffer + plen + 1;
64 return __nscd_getgr_r (buffer, GETGRBYGID, resultbuf, p, buflen - plen -1);
67 /* Create a socket connected to a name. */
68 static int
69 nscd_open_socket (void)
71 struct sockaddr_un addr;
72 int sock;
73 int saved_errno = errno;
75 sock = __socket (PF_UNIX, SOCK_STREAM, 0);
76 if (sock < 0)
78 __set_errno (saved_errno);
79 return -1;
82 addr.sun_family = AF_UNIX;
83 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
84 if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
86 __close (sock);
87 __set_errno (saved_errno);
88 return -1;
91 return sock;
94 static int
95 __nscd_getgr_r (const char *key, request_type type, struct group *resultbuf,
96 char *buffer, size_t buflen)
98 int sock = nscd_open_socket ();
99 request_header req;
100 gr_response_header gr_resp;
101 ssize_t nbytes;
102 size_t maxiov;
103 size_t sum;
105 if (sock == -1)
107 __nss_not_use_nscd_group = 1;
108 return 1;
111 req.version = NSCD_VERSION;
112 req.type = type;
113 req.key_len = strlen (key);
114 nbytes = __write (sock, &req, sizeof (request_header));
115 if (nbytes != sizeof (request_header))
117 __close (sock);
118 return 1;
121 nbytes = __write (sock, key, req.key_len);
122 if (nbytes != req.key_len)
124 __close (sock);
125 return 1;
128 nbytes = __read (sock, &gr_resp, sizeof (gr_response_header));
129 if (nbytes != sizeof (gr_response_header))
131 __close (sock);
132 return 1;
135 if (gr_resp.found == -1)
137 /* The daemon does not cache this database. */
138 __close (sock);
139 __nss_not_use_nscd_group = 1;
140 return 1;
143 if (gr_resp.found == 1)
145 struct iovec *vec;
146 size_t *len;
147 char *p = buffer;
148 int nblocks;
149 size_t total_len;
150 uintptr_t align;
152 /* A first check whether the buffer is sufficently large is possible. */
153 if (buflen < gr_resp.gr_name_len + 1 + gr_resp.gr_passwd_len + 1)
155 __set_errno (ERANGE);
156 __close (sock);
157 return -1;
160 /* Allocate the IOVEC. */
161 vec = alloca ((2 + gr_resp.gr_mem_len) * sizeof (struct iovec));
162 len = alloca (gr_resp.gr_mem_len * sizeof (size_t));
164 vec[0].iov_base = resultbuf->gr_name = p;
165 vec[0].iov_len = gr_resp.gr_name_len;
166 total_len = gr_resp.gr_name_len;
167 p += gr_resp.gr_name_len + 1;
169 vec[1].iov_base = resultbuf->gr_passwd = p;
170 vec[1].iov_len = gr_resp.gr_passwd_len;
171 total_len += gr_resp.gr_passwd_len;
172 p += gr_resp.gr_passwd_len + 1;
173 buflen -= total_len;
174 nblocks = 2;
176 if (gr_resp.gr_mem_len > 0)
178 vec[2].iov_base = len;
179 vec[2].iov_len = gr_resp.gr_mem_len * sizeof (size_t);
180 total_len += gr_resp.gr_mem_len * sizeof (size_t);
181 nblocks = 3;
184 /* Get this data. */
185 if (__readv (sock, vec, nblocks) != total_len)
187 __close (sock);
188 return 1;
191 /* Now we know the sizes. First terminate the strings we just read. */
192 resultbuf->gr_name[gr_resp.gr_name_len] = '\0';
193 resultbuf->gr_passwd[gr_resp.gr_passwd_len] = '\0';
195 resultbuf->gr_gid = gr_resp.gr_gid;
197 /* Now allocate the buffer the array for the group members. We must
198 align the pointer. */
199 align = ((__alignof__ (char *) - (p - ((char *) 0)))
200 & (__alignof__ (char *) - 1));
201 if (align + (1 + gr_resp.gr_mem_len) * sizeof (char *) > buflen)
203 __set_errno (ERANGE);
204 __close (sock);
205 return -1;
207 p += align;
208 resultbuf->gr_mem = (char **) p;
209 p += (1 + gr_resp.gr_mem_len) * sizeof (char *);
210 buflen -= align + (1 + gr_resp.gr_mem_len) * sizeof (char *);
212 resultbuf->gr_mem[gr_resp.gr_mem_len] = NULL;
214 if (gr_resp.gr_mem_len > 0)
216 /* Prepare reading the group members. */
217 size_t i;
219 total_len = 0;
220 for (i = 0; i < gr_resp.gr_mem_len; ++i)
222 if (len[i] >= buflen)
224 __set_errno (ERANGE);
225 __close (sock);
226 return -1;
229 vec[i].iov_base = resultbuf->gr_mem[i] = p;
230 vec[i].iov_len = len[i];
231 total_len += len[i];
232 buflen -= len[i];
233 p += len[i];
234 *p++ = '\0';
237 #ifdef UIO_MAXIOV
238 maxiov = UIO_MAXIOV;
239 #else
240 maxiov = sysconf (_SC_UIO_MAXIOV);
241 #endif
243 sum = 0;
244 while (i > maxiov)
246 sum += __readv (sock, vec, maxiov);
247 vec += maxiov;
248 i -= maxiov;
251 if (sum + __readv (sock, vec, i) != total_len)
253 __close (sock);
254 return -1;
257 __close (sock);
258 return 0;
260 else
262 __close (sock);
263 return -1;