]> git.ipfire.org Git - thirdparty/glibc.git/blob - nscd/nscd_getgr_r.c
Update.
[thirdparty/glibc.git] / nscd / nscd_getgr_r.c
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.
5
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.
10
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.
15
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. */
20
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>
33
34 #include "nscd-client.h"
35 #include "nscd_proto.h"
36
37 int __nss_not_use_nscd_group;
38
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;
43
44
45 int
46 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
47 size_t buflen, struct group **result)
48 {
49 return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
50 buffer, buflen, result);
51 }
52
53
54 int
55 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
56 size_t buflen, struct group **result)
57 {
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);
61
62 return nscd_getgr_r (cp, buf + sizeof (buf) - cp, GETGRBYGID, resultbuf,
63 buffer, buflen, result);
64 }
65
66
67 static int
68 internal_function
69 nscd_getgr_r (const char *key, size_t keylen, request_type type,
70 struct group *resbuf, char *buffer, size_t buflen,
71 struct group **result)
72 {
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)
77 {
78 __nss_not_use_nscd_group = 1;
79 return -1;
80 }
81
82 /* No value found so far. */
83 int retval = -1;
84 *result = NULL;
85
86 if (gr_resp.found == -1)
87 {
88 /* The daemon does not cache this database. */
89 __nss_not_use_nscd_group = 1;
90 goto out;
91 }
92
93 if (gr_resp.found == 1)
94 {
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;
101
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))
109 {
110 no_room:
111 __set_errno (ERANGE);
112 retval = ERANGE;
113 goto out;
114 }
115 buflen -= total_len;
116
117 p += align;
118 resbuf->gr_mem = (char **) p;
119 p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
120
121 /* Set pointers for strings. */
122 resbuf->gr_name = p;
123 p += gr_resp.gr_name_len;
124 resbuf->gr_passwd = p;
125 p += gr_resp.gr_passwd_len;
126
127 /* Fill in what we know now. */
128 resbuf->gr_gid = gr_resp.gr_gid;
129
130 /* Allocate array to store lengths. */
131 len = (uint32_t *) alloca (gr_resp.gr_mem_cnt * sizeof (uint32_t));
132
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 = resbuf->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;
139
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;
144
145 /* Clear the terminating entry. */
146 resbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
147
148 /* Prepare reading the group members. */
149 total_len = 0;
150 for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
151 {
152 resbuf->gr_mem[cnt] = p;
153 total_len += len[cnt];
154 p += len[cnt];
155 }
156
157 if (__builtin_expect (total_len > buflen, 0))
158 goto no_room;
159
160 if (gr_resp.gr_mem_cnt > 0
161 && __builtin_expect (TEMP_FAILURE_RETRY (__read (sock,
162 resbuf->gr_mem[0],
163 total_len))
164 != total_len, 0))
165 {
166 /* The `errno' to some value != ERANGE. */
167 __set_errno (ENOENT);
168 retval = ENOENT;
169 }
170 else
171 {
172 retval = 0;
173 *result = resbuf;
174 }
175 }
176 else
177 {
178 /* The `errno' to some value != ERANGE. */
179 __set_errno (ENOENT);
180 /* Even though we have not found anything, the result is zero. */
181 retval = 0;
182 }
183
184 out:
185 close_not_cancel_no_status (sock);
186
187 return retval;
188 }