Update.
[platform/upstream/glibc.git] / nscd / nscd_gethst_r.c
1 /* Copyright (C) 1998-2002, 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@cygnus.com>, 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 <netdb.h>
22 #include <resolv.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <arpa/nameser.h>
29 #include <sys/socket.h>
30 #include <sys/uio.h>
31 #include <sys/un.h>
32
33 #include "nscd-client.h"
34 #include "nscd_proto.h"
35
36 int __nss_not_use_nscd_hosts;
37
38 static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
39                           struct hostent *resultbuf, char *buffer,
40                           size_t buflen, int *h_errnop) internal_function;
41
42
43 int
44 __nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
45                         char *buffer, size_t buflen, int *h_errnop)
46 {
47   request_type reqtype;
48
49   reqtype = (_res.options & RES_USE_INET6) ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
50
51   return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
52                         buffer, buflen, h_errnop);
53 }
54
55
56 int
57 __nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
58                          char *buffer, size_t buflen, int *h_errnop)
59 {
60   request_type reqtype;
61
62   reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
63
64   return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
65                         buffer, buflen, h_errnop);
66 }
67
68
69 int
70 __nscd_gethostbyaddr_r (const void *addr, socklen_t len, int type,
71                         struct hostent *resultbuf, char *buffer, size_t buflen,
72                         int *h_errnop)
73 {
74   request_type reqtype;
75
76   if (!((len == INADDRSZ && type == AF_INET)
77         || (len == IN6ADDRSZ && type == AF_INET6)))
78     /* LEN and TYPE do not match.  */
79     return -1;
80
81   reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
82
83   return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen,
84                         h_errnop);
85 }
86
87
88 /* Create a socket connected to a name. */
89 int
90 __nscd_open_socket (void)
91 {
92   struct sockaddr_un addr;
93   int sock;
94   int saved_errno = errno;
95
96   sock = __socket (PF_UNIX, SOCK_STREAM, 0);
97   if (sock < 0)
98     {
99       __set_errno (saved_errno);
100       return -1;
101     }
102
103   addr.sun_family = AF_UNIX;
104   strcpy (addr.sun_path, _PATH_NSCDSOCKET);
105   if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
106     {
107       __close (sock);
108       __set_errno (saved_errno);
109       return -1;
110     }
111
112   return sock;
113 }
114
115
116 static int
117 internal_function
118 nscd_gethst_r (const char *key, size_t keylen, request_type type,
119                struct hostent *resultbuf, char *buffer, size_t buflen,
120                int *h_errnop)
121 {
122   int sock = __nscd_open_socket ();
123   hst_response_header hst_resp;
124   request_header req;
125   ssize_t nbytes;
126   struct iovec vec[4];
127   int result = -1;
128
129   if (sock == -1)
130     {
131       __nss_not_use_nscd_group = 1;
132       return -1;
133     }
134
135   req.version = NSCD_VERSION;
136   req.type = type;
137   req.key_len = keylen;
138
139   vec[0].iov_base = &req;
140   vec[0].iov_len = sizeof (request_header);
141   vec[1].iov_base = (void *) key;
142   vec[1].iov_len = req.key_len;
143
144   nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
145   if ((size_t) nbytes != sizeof (request_header) + req.key_len)
146     goto out;
147
148   nbytes = TEMP_FAILURE_RETRY (__read (sock, &hst_resp,
149                                        sizeof (hst_response_header)));
150   if (__builtin_expect (nbytes != sizeof (hst_response_header), 0))
151     goto out;
152
153   if (hst_resp.found == -1)
154     {
155       /* The daemon does not cache this database.  */
156       __nss_not_use_nscd_hosts = 1;
157       goto out;
158     }
159
160   if (hst_resp.found == 1)
161     {
162       uint32_t *aliases_len;
163       char *cp = buffer;
164       uintptr_t align1;
165       uintptr_t align2;
166       size_t total_len;
167       ssize_t cnt;
168       char *ignore;
169       int n;
170
171       /* A first check whether the buffer is sufficiently large is possible.  */
172       /* Now allocate the buffer the array for the group members.  We must
173          align the pointer and the base of the h_addr_list pointers.  */
174       align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
175                 & (__alignof__ (char *) - 1));
176       align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp.h_name_len)
177                                          - ((char *) 0)))
178                 & (__alignof__ (char *) - 1));
179       if (buflen < (align1 + hst_resp.h_name_len + align2
180                     + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt + 2)
181                        * sizeof (char *))
182                     + hst_resp.h_addr_list_cnt * (type == AF_INET
183                                                   ? INADDRSZ : IN6ADDRSZ)))
184         {
185         no_room:
186           __set_errno (ERANGE);
187           result = ERANGE;
188           goto out;
189         }
190       cp += align1;
191
192       /* Prepare the result as far as we can.  */
193       resultbuf->h_aliases = (char **) cp;
194       cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
195       resultbuf->h_addr_list = (char **) cp;
196       cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
197
198       resultbuf->h_name = cp;
199       cp += hst_resp.h_name_len + align2;
200       vec[0].iov_base = resultbuf->h_name;
201       vec[0].iov_len = hst_resp.h_name_len;
202
203       aliases_len = alloca (hst_resp.h_aliases_cnt * sizeof (uint32_t));
204       vec[1].iov_base = aliases_len;
205       vec[1].iov_len = hst_resp.h_aliases_cnt * sizeof (uint32_t);
206
207       total_len = (hst_resp.h_name_len
208                    + hst_resp.h_aliases_cnt * sizeof (uint32_t));
209
210       n = 2;
211       if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
212         {
213           vec[2].iov_base = cp;
214           vec[2].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
215
216           for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
217             {
218               resultbuf->h_addr_list[cnt] = cp;
219               cp += INADDRSZ;
220             }
221
222           resultbuf->h_addrtype = AF_INET;
223           resultbuf->h_length = INADDRSZ;
224
225           total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
226
227           n = 3;
228         }
229       else
230         {
231           if (hst_resp.h_length == INADDRSZ)
232             {
233               ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
234               vec[2].iov_base = ignore;
235               vec[2].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
236
237               total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
238
239               n = 3;
240             }
241
242           vec[n].iov_base = cp;
243           vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
244
245           for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
246             {
247               resultbuf->h_addr_list[cnt] = cp;
248               cp += IN6ADDRSZ;
249             }
250
251           resultbuf->h_addrtype = AF_INET6;
252           resultbuf->h_length = IN6ADDRSZ;
253
254           total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
255
256           ++n;
257         }
258       resultbuf->h_addr_list[cnt] = NULL;
259
260       if ((size_t) TEMP_FAILURE_RETRY (__readv (sock, vec, n)) != total_len)
261         goto out;
262
263       /*  Now we also can read the aliases.  */
264       total_len = 0;
265       for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
266         {
267           resultbuf->h_aliases[cnt] = cp;
268           cp += aliases_len[cnt];
269           total_len += aliases_len[cnt];
270         }
271       resultbuf->h_aliases[cnt] = NULL;
272
273       /* See whether this would exceed the buffer capacity.  */
274       if (cp > buffer + buflen)
275         goto no_room;
276
277       /* And finally read the aliases.  */
278       if ((size_t) TEMP_FAILURE_RETRY (__read (sock, resultbuf->h_aliases[0],
279                                                total_len)) == total_len)
280         result = 0;
281     }
282   else
283     {
284       /* Store the error number.  */
285       *h_errnop = hst_resp.error;
286
287       /* The `errno' to some value != ERANGE.  */
288       __set_errno (ENOENT);
289       result = ENOENT;
290     }
291
292  out:
293   __close (sock);
294
295   return result;
296 }