Update.
[glibc.git] / nscd / nscd_getgr_r.c
blobf27da0256930f0088a50b544bd852462bccb4c17
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, size_t keylen, request_type type,
36 struct group *resultbuf, char *buffer,
37 size_t buflen);
40 int
41 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
42 size_t buflen)
44 return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
45 buffer, buflen);
49 int
50 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
51 size_t buflen)
53 char buf[12];
54 size_t n;
56 n = __snprintf (buf, sizeof (buf), "%d", gid) + 1;
58 return nscd_getgr_r (buf, n, GETGRBYGID, resultbuf, buffer, buflen);
62 /* Create a socket connected to a name. */
63 static int
64 open_socket (void)
66 struct sockaddr_un addr;
67 int sock;
68 int saved_errno = errno;
70 sock = __socket (PF_UNIX, SOCK_STREAM, 0);
71 if (sock < 0)
73 __set_errno (saved_errno);
74 return -1;
77 addr.sun_family = AF_UNIX;
78 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
79 if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
81 __close (sock);
82 __set_errno (saved_errno);
83 return -1;
86 return sock;
90 static int
91 nscd_getgr_r (const char *key, size_t keylen, request_type type,
92 struct group *resultbuf, char *buffer, size_t buflen)
94 int sock = open_socket ();
95 request_header req;
96 gr_response_header gr_resp;
97 ssize_t nbytes;
98 struct iovec vec[2];
100 if (sock == -1)
102 __nss_not_use_nscd_group = 1;
103 return 1;
106 req.version = NSCD_VERSION;
107 req.type = type;
108 req.key_len = keylen;
110 vec[0].iov_base = &req;
111 vec[0].iov_len = sizeof (request_header);
112 vec[1].iov_base = (void *) key;
113 vec[1].iov_len = keylen;
115 if (__writev (sock, vec, 2) != sizeof (request_header) + keylen)
117 __close (sock);
118 return 1;
121 nbytes = __read (sock, &gr_resp, sizeof (gr_response_header));
122 if (nbytes != sizeof (gr_response_header))
124 __close (sock);
125 return 1;
128 if (gr_resp.found == -1)
130 /* The daemon does not cache this database. */
131 __close (sock);
132 __nss_not_use_nscd_group = 1;
133 return 1;
136 if (gr_resp.found == 1)
138 size_t *len;
139 char *p = buffer;
140 size_t total_len;
141 uintptr_t align;
142 size_t cnt;
144 /* Now allocate the buffer the array for the group members. We must
145 align the pointer. */
146 align = ((__alignof__ (char *) - (p - ((char *) 0)))
147 & (__alignof__ (char *) - 1));
148 if (buflen < (align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
149 + gr_resp.gr_name_len + gr_resp.gr_passwd_len))
151 no_room:
152 __set_errno (ERANGE);
153 __close (sock);
154 return -1;
157 p += align;
158 resultbuf->gr_mem = (char **) p;
159 p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
160 buflen -= align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
162 /* Set pointers for strings. */
163 resultbuf->gr_name = p;
164 p += gr_resp.gr_name_len;
165 resultbuf->gr_passwd = p;
166 p += gr_resp.gr_passwd_len;
168 /* Fill in what we know now. */
169 resultbuf->gr_gid = gr_resp.gr_gid;
171 /* Allocate array to store lengths. */
172 len = alloca (gr_resp.gr_mem_cnt * sizeof (size_t));
174 total_len = gr_resp.gr_mem_cnt * sizeof (size_t);
175 vec[0].iov_base = len;
176 vec[0].iov_len = total_len;
177 vec[1].iov_base = resultbuf->gr_name;
178 vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
179 total_len += gr_resp.gr_name_len + gr_resp.gr_passwd_len;
181 buflen -= total_len;
183 /* Get this data. */
184 if (__readv (sock, vec, 2) != total_len)
186 __close (sock);
187 return 1;
190 /* Clear the terminating entry. */
191 resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
193 /* Prepare reading the group members. */
194 total_len = 0;
195 for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
197 resultbuf->gr_mem[cnt] = p;
198 total_len += len[cnt];
199 p += len[cnt];
202 if (total_len > buflen)
203 goto no_room;
205 if (__read (sock, resultbuf->gr_mem[0], total_len) != total_len)
207 __close (sock);
208 return -1;
211 __close (sock);
212 return 0;
214 else
216 __close (sock);
217 return -1;