Update.
[glibc.git] / nscd / nscd_getgr_r.c
blobb9cde7304427e3fc7a9b7a4055f33a9bffa296a1
1 /* Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <grp.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/socket.h>
29 #include <sys/uio.h>
30 #include <sys/un.h>
31 #include <not-cancel.h>
32 #include <stdio-common/_itoa.h>
34 #include "nscd-client.h"
35 #include "nscd_proto.h"
37 int __nss_not_use_nscd_group;
39 static int nscd_getgr_r (const char *key, size_t keylen, request_type type,
40 struct group *resultbuf, char *buffer,
41 size_t buflen, struct group **result)
42 internal_function;
45 int
46 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
47 size_t buflen, struct group **result)
49 return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
50 buffer, buflen, result);
54 int
55 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
56 size_t buflen, struct group **result)
58 char buf[3 * sizeof (gid_t)];
59 buf[sizeof (buf) - 1] = '\0';
60 char *cp = _itoa_word (gid, buf + sizeof (buf) - 1, 10, 0);
62 return nscd_getgr_r (cp, buf + sizeof (buf) - cp, GETGRBYGID, resultbuf,
63 buffer, buflen, result);
67 static int
68 internal_function
69 nscd_getgr_r (const char *key, size_t keylen, request_type type,
70 struct group *resultbuf, char *buffer, size_t buflen,
71 struct group **result)
73 gr_response_header gr_resp;
74 int sock = __nscd_open_socket (key, keylen, type, &gr_resp,
75 sizeof (gr_resp));
76 if (sock == -1)
78 __nss_not_use_nscd_group = 1;
79 return -1;
82 /* No value found so far. */
83 int retval = -1;
84 *result = NULL;
86 if (gr_resp.found == -1)
88 /* The daemon does not cache this database. */
89 __nss_not_use_nscd_group = 1;
90 goto out;
93 if (gr_resp.found == 1)
95 struct iovec vec[2];
96 uint32_t *len;
97 char *p = buffer;
98 size_t total_len;
99 uintptr_t align;
100 nscd_ssize_t cnt;
102 /* Now allocate the buffer the array for the group members. We must
103 align the pointer. */
104 align = ((__alignof__ (char *) - (p - ((char *) 0)))
105 & (__alignof__ (char *) - 1));
106 total_len = align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
107 + gr_resp.gr_name_len + gr_resp.gr_passwd_len;
108 if (__builtin_expect (buflen < total_len, 0))
110 no_room:
111 __set_errno (ERANGE);
112 retval = ERANGE;
113 goto out;
115 buflen -= total_len;
117 p += align;
118 resultbuf->gr_mem = (char **) p;
119 p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
121 /* Set pointers for strings. */
122 resultbuf->gr_name = p;
123 p += gr_resp.gr_name_len;
124 resultbuf->gr_passwd = p;
125 p += gr_resp.gr_passwd_len;
127 /* Fill in what we know now. */
128 resultbuf->gr_gid = gr_resp.gr_gid;
130 /* Allocate array to store lengths. */
131 len = (uint32_t *) alloca (gr_resp.gr_mem_cnt * sizeof (uint32_t));
133 total_len = gr_resp.gr_mem_cnt * sizeof (uint32_t);
134 vec[0].iov_base = len;
135 vec[0].iov_len = total_len;
136 vec[1].iov_base = resultbuf->gr_name;
137 vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
138 total_len += gr_resp.gr_name_len + gr_resp.gr_passwd_len;
140 /* Get this data. */
141 size_t n = TEMP_FAILURE_RETRY (__readv (sock, vec, 2));
142 if (__builtin_expect (n != total_len, 0))
143 goto out;
145 /* Clear the terminating entry. */
146 resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
148 /* Prepare reading the group members. */
149 total_len = 0;
150 for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
152 resultbuf->gr_mem[cnt] = p;
153 total_len += len[cnt];
154 p += len[cnt];
157 if (__builtin_expect (total_len > buflen, 0))
158 goto no_room;
160 retval = 0;
161 n = TEMP_FAILURE_RETRY (__read (sock, resultbuf->gr_mem[0],
162 total_len));
163 if (__builtin_expect (n != total_len, 0))
165 /* The `errno' to some value != ERANGE. */
166 __set_errno (ENOENT);
167 retval = ENOENT;
169 else
170 *result = resultbuf;
172 else
174 /* The `errno' to some value != ERANGE. */
175 __set_errno (ENOENT);
176 /* Even though we have not found anything, the result is zero. */
177 retval = 0;
180 out:
181 close_not_cancel_no_status (sock);
183 return retval;