Update.
[glibc.git] / nscd / nscd_getgr_r.c
blobeb9d280eee3145ce34f29aa7f7b571357bb33849
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);
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 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 ();
96 request_header req;
97 gr_response_header gr_resp;
98 ssize_t nbytes;
99 struct iovec vec[2];
101 if (sock == -1)
103 __nss_not_use_nscd_group = 1;
104 return 1;
107 req.version = NSCD_VERSION;
108 req.type = type;
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)
118 __close (sock);
119 return 1;
122 nbytes = __read (sock, &gr_resp, sizeof (gr_response_header));
123 if (nbytes != sizeof (gr_response_header))
125 __close (sock);
126 return 1;
129 if (gr_resp.found == -1)
131 /* The daemon does not cache this database. */
132 __close (sock);
133 __nss_not_use_nscd_group = 1;
134 return 1;
137 if (gr_resp.found == 1)
139 size_t *len;
140 char *p = buffer;
141 size_t total_len;
142 uintptr_t align;
143 size_t cnt;
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))
152 no_room:
153 __set_errno (ERANGE);
154 __close (sock);
155 return -1;
158 p += align;
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;
182 buflen -= total_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 return -1;
212 __close (sock);
213 return 0;
215 else
217 __close (sock);
218 return -1;