Update.
[platform/upstream/glibc.git] / nscd / nscd_getgr_r.c
1 /* Copyright (C) 1998, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
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>
30
31 #include "nscd-client.h"
32 #include "nscd_proto.h"
33
34 int __nss_not_use_nscd_group;
35
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;
39
40
41 int
42 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
43                    size_t buflen)
44 {
45   return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
46                        buffer, buflen);
47 }
48
49
50 int
51 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
52                    size_t buflen)
53 {
54   char buf[12];
55   size_t n;
56
57   n = __snprintf (buf, sizeof (buf), "%d", gid) + 1;
58
59   return nscd_getgr_r (buf, n, GETGRBYGID, resultbuf, buffer, buflen);
60 }
61
62
63 static int
64 internal_function
65 nscd_getgr_r (const char *key, size_t keylen, request_type type,
66               struct group *resultbuf, char *buffer, size_t buflen)
67 {
68   int sock = __nscd_open_socket ();
69   request_header req;
70   gr_response_header gr_resp;
71   ssize_t nbytes;
72   struct iovec vec[2];
73   int result = -1;
74
75   if (sock == -1)
76     {
77       __nss_not_use_nscd_group = 1;
78       return -1;
79     }
80
81   req.version = NSCD_VERSION;
82   req.type = type;
83   req.key_len = keylen;
84
85   vec[0].iov_base = &req;
86   vec[0].iov_len = sizeof (request_header);
87   vec[1].iov_base = (void *) key;
88   vec[1].iov_len = keylen;
89
90   nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
91   if (nbytes != (ssize_t) (sizeof (request_header) + keylen))
92     goto out;
93
94   nbytes = TEMP_FAILURE_RETRY (__read (sock, &gr_resp,
95                                        sizeof (gr_response_header)));
96   if (nbytes != (ssize_t) sizeof (gr_response_header))
97     goto out;
98
99   if (gr_resp.found == -1)
100     {
101       /* The daemon does not cache this database.  */
102       __nss_not_use_nscd_group = 1;
103       goto out;
104     }
105
106   if (gr_resp.found == 1)
107     {
108       uint32_t *len;
109       char *p = buffer;
110       size_t total_len;
111       uintptr_t align;
112       nscd_ssize_t cnt;
113
114       /* Now allocate the buffer the array for the group members.  We must
115          align the pointer.  */
116       align = ((__alignof__ (char *) - (p - ((char *) 0)))
117                & (__alignof__ (char *) - 1));
118       total_len = align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
119                   + gr_resp.gr_name_len + gr_resp.gr_passwd_len;
120       if (__builtin_expect (buflen < total_len, 0))
121         {
122         no_room:
123           __set_errno (ERANGE);
124           result = ERANGE;
125           goto out;
126         }
127       buflen -= total_len;
128
129       p += align;
130       resultbuf->gr_mem = (char **) p;
131       p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
132
133       /* Set pointers for strings.  */
134       resultbuf->gr_name = p;
135       p += gr_resp.gr_name_len;
136       resultbuf->gr_passwd = p;
137       p += gr_resp.gr_passwd_len;
138
139       /* Fill in what we know now.  */
140       resultbuf->gr_gid = gr_resp.gr_gid;
141
142       /* Allocate array to store lengths.  */
143       len = (uint32_t *) alloca (gr_resp.gr_mem_cnt * sizeof (uint32_t));
144
145       total_len = gr_resp.gr_mem_cnt * sizeof (uint32_t);
146       vec[0].iov_base = len;
147       vec[0].iov_len = total_len;
148       vec[1].iov_base = resultbuf->gr_name;
149       vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
150       total_len += gr_resp.gr_name_len + gr_resp.gr_passwd_len;
151
152       /* Get this data.  */
153       size_t n = TEMP_FAILURE_RETRY (__readv (sock, vec, 2));
154       if (__builtin_expect (n != total_len, 0))
155         goto out;
156
157       /* Clear the terminating entry.  */
158       resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
159
160       /* Prepare reading the group members.  */
161       total_len = 0;
162       for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
163         {
164           resultbuf->gr_mem[cnt] = p;
165           total_len += len[cnt];
166           p += len[cnt];
167         }
168
169       if (__builtin_expect (total_len > buflen, 0))
170         goto no_room;
171
172       result = 0;
173       n = TEMP_FAILURE_RETRY (__read (sock, resultbuf->gr_mem[0],
174                                              total_len));
175       if (__builtin_expect (n != total_len, 0))
176         {
177           /* The `errno' to some value != ERANGE.  */
178           __set_errno (ENOENT);
179           result = ENOENT;
180         }
181     }
182   else
183     {
184       /* The `errno' to some value != ERANGE.  */
185       __set_errno (ENOENT);
186       result = ENOENT;
187     }
188
189  out:
190   __close (sock);
191
192   return result;
193 }