Update.
[glibc.git] / nscd / nscd_getgr_r.c
blobaa9c3fddcc06acc9304a4aad9233c7b974c49e75
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. */
20 #include <errno.h>
21 #include <grp.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/socket.h>
28 #include <sys/uio.h>
29 #include <sys/un.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;
41 int
42 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
43 size_t buflen)
45 return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
46 buffer, buflen);
50 int
51 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
52 size_t buflen)
54 char buf[12];
55 size_t n;
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. */
64 static int
65 open_socket (void)
67 struct sockaddr_un addr;
68 int sock;
69 int saved_errno = errno;
71 sock = __socket (PF_UNIX, SOCK_STREAM, 0);
72 if (sock < 0)
74 __set_errno (saved_errno);
75 return -1;
78 addr.sun_family = AF_UNIX;
79 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
80 if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
82 __close (sock);
83 __set_errno (saved_errno);
84 return -1;
87 return sock;
91 static int
92 internal_function
93 nscd_getgr_r (const char *key, size_t keylen, request_type type,
94 struct group *resultbuf, char *buffer, size_t buflen)
96 int sock = open_socket ();
97 request_header req;
98 gr_response_header gr_resp;
99 ssize_t nbytes;
100 struct iovec vec[2];
102 if (sock == -1)
104 __nss_not_use_nscd_group = 1;
105 return -1;
108 req.version = NSCD_VERSION;
109 req.type = type;
110 req.key_len = keylen;
112 vec[0].iov_base = &req;
113 vec[0].iov_len = sizeof (request_header);
114 vec[1].iov_base = (void *) key;
115 vec[1].iov_len = keylen;
117 if (__writev (sock, vec, 2) != sizeof (request_header) + keylen)
119 __close (sock);
120 return -1;
123 nbytes = __read (sock, &gr_resp, sizeof (gr_response_header));
124 if (nbytes != sizeof (gr_response_header))
126 __close (sock);
127 return -1;
130 if (gr_resp.found == -1)
132 /* The daemon does not cache this database. */
133 __close (sock);
134 __nss_not_use_nscd_group = 1;
135 return -1;
138 if (gr_resp.found == 1)
140 size_t *len;
141 char *p = buffer;
142 size_t total_len;
143 uintptr_t align;
144 size_t cnt;
146 /* Now allocate the buffer the array for the group members. We must
147 align the pointer. */
148 align = ((__alignof__ (char *) - (p - ((char *) 0)))
149 & (__alignof__ (char *) - 1));
150 total_len = align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
151 + gr_resp.gr_name_len + gr_resp.gr_passwd_len;
152 if (buflen < total_len)
154 no_room:
155 __set_errno (ERANGE);
156 __close (sock);
157 return ERANGE;
159 buflen -= total_len;
161 p += align;
162 resultbuf->gr_mem = (char **) p;
163 p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
165 /* Set pointers for strings. */
166 resultbuf->gr_name = p;
167 p += gr_resp.gr_name_len;
168 resultbuf->gr_passwd = p;
169 p += gr_resp.gr_passwd_len;
171 /* Fill in what we know now. */
172 resultbuf->gr_gid = gr_resp.gr_gid;
174 /* Allocate array to store lengths. */
175 len = alloca (gr_resp.gr_mem_cnt * sizeof (size_t));
177 total_len = gr_resp.gr_mem_cnt * sizeof (size_t);
178 vec[0].iov_base = len;
179 vec[0].iov_len = total_len;
180 vec[1].iov_base = resultbuf->gr_name;
181 vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
182 total_len += gr_resp.gr_name_len + gr_resp.gr_passwd_len;
184 /* Get this data. */
185 if (__readv (sock, vec, 2) != total_len)
187 __close (sock);
188 return -1;
191 /* Clear the terminating entry. */
192 resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
194 /* Prepare reading the group members. */
195 total_len = 0;
196 for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
198 resultbuf->gr_mem[cnt] = p;
199 total_len += len[cnt];
200 p += len[cnt];
203 if (total_len > buflen)
204 goto no_room;
206 if (__read (sock, resultbuf->gr_mem[0], total_len) != total_len)
208 __close (sock);
209 /* The `errno' to some value != ERANGE. */
210 __set_errno (ENOENT);
211 return ENOENT;
214 __close (sock);
215 return 0;
217 else
219 __close (sock);
220 /* The `errno' to some value != ERANGE. */
221 __set_errno (ENOENT);
222 return ENOENT;