]> sourceware.org Git - glibc.git/blame - nscd/nscd_getserv_r.c
Try harder to re-exec nscd in paranoia mode.
[glibc.git] / nscd / nscd_getserv_r.c
CommitLineData
cfe1fc10 1/* Copyright (C) 2007, 2009 Free Software Foundation, Inc.
b21fa963
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2007.
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 <string.h>
22#include <not-cancel.h>
23#include <stdio-common/_itoa.h>
24
25#include "nscd-client.h"
26#include "nscd_proto.h"
27
28
29int __nss_not_use_nscd_services;
30
31
32static int nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
33 request_type type, struct servent *resultbuf,
34 char *buf, size_t buflen, struct servent **result);
35
36
37int
38__nscd_getservbyname_r (const char *name, const char *proto,
39 struct servent *result_buf, char *buf, size_t buflen,
40 struct servent **result)
41{
42 return nscd_getserv_r (name, strlen (name), proto, GETSERVBYNAME, result_buf,
43 buf, buflen, result);
44}
45
46
47int
48__nscd_getservbyport_r (int port, const char *proto,
49 struct servent *result_buf, char *buf, size_t buflen,
50 struct servent **result)
51{
52 char portstr[3 * sizeof (int) + 2];
53 portstr[sizeof (portstr) - 1] = '\0';
54 char *cp = _itoa_word (port, portstr + sizeof (portstr) - 1, 10, 0);
55
ee78670e 56 return nscd_getserv_r (cp, portstr + sizeof (portstr) - cp, proto,
b21fa963
UD
57 GETSERVBYPORT, result_buf, buf, buflen, result);
58}
59
60
61libc_locked_map_ptr (, __serv_map_handle) attribute_hidden;
62/* Note that we only free the structure if necessary. The memory
63 mapping is not removed since it is not visible to the malloc
64 handling. */
65libc_freeres_fn (serv_map_free)
66{
67 if (__serv_map_handle.mapped != NO_MAPPING)
68 {
69 void *p = __serv_map_handle.mapped;
70 __serv_map_handle.mapped = NO_MAPPING;
71 free (p);
72 }
73}
74
75
76static int
77nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
78 request_type type, struct servent *resultbuf,
79 char *buf, size_t buflen, struct servent **result)
80{
81 int gc_cycle;
82 int nretries = 0;
83
84 /* If the mapping is available, try to search there instead of
85 communicating with the nscd. */
86 struct mapped_database *mapped;
87 mapped = __nscd_get_map_ref (GETFDSERV, "services", &__serv_map_handle,
88 &gc_cycle);
89 size_t protolen = proto == NULL ? 0 : strlen (proto);
90 size_t keylen = critlen + 1 + protolen + 1;
91 char *key = alloca (keylen);
92 memcpy (__mempcpy (__mempcpy (key, crit, critlen),
93 "/", 1), proto ?: "", protolen + 1);
94
95 retry:;
b21fa963
UD
96 const char *s_name = NULL;
97 const char *s_proto = NULL;
98 const uint32_t *aliases_len = NULL;
99 const char *aliases_list = NULL;
100 int retval = -1;
101 const char *recend = (const char *) ~UINTMAX_C (0);
102 int sock = -1;
1a77d37f
JJ
103 serv_response_header serv_resp;
104
b21fa963
UD
105 if (mapped != NO_MAPPING)
106 {
cfe1fc10
JJ
107 struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
108 sizeof serv_resp);
b21fa963
UD
109
110 if (found != NULL)
111 {
1a77d37f
JJ
112 s_name = (char *) (&found->data[0].servdata + 1);
113 serv_resp = found->data[0].servdata;
114 s_proto = s_name + serv_resp.s_name_len;
115 aliases_len = (uint32_t *) (s_proto + serv_resp.s_proto_len);
b21fa963 116 aliases_list = ((char *) aliases_len
1a77d37f
JJ
117 + serv_resp.s_aliases_cnt * sizeof (uint32_t));
118 recend = (const char *) found->data + found->recsize;
119 /* Now check if we can trust serv_resp fields. If GC is
120 in progress, it can contain anything. */
121 if (mapped->head->gc_cycle != gc_cycle)
122 {
123 retval = -2;
124 goto out;
125 }
3687a5a7
JJ
126 if (__builtin_expect ((const char *) aliases_len
127 + serv_resp.s_aliases_cnt * sizeof (uint32_t)
128 > recend, 0))
129 goto out;
b21fa963
UD
130
131#ifndef _STRING_ARCH_unaligned
132 /* The aliases_len array in the mapped database might very
133 well be unaligned. We will access it word-wise so on
134 platforms which do not tolerate unaligned accesses we
135 need to make an aligned copy. */
136 if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
137 != 0)
138 {
1a77d37f 139 uint32_t *tmp = alloca (serv_resp.s_aliases_cnt
b21fa963
UD
140 * sizeof (uint32_t));
141 aliases_len = memcpy (tmp, aliases_len,
1a77d37f 142 serv_resp.s_aliases_cnt
b21fa963
UD
143 * sizeof (uint32_t));
144 }
145#endif
b21fa963
UD
146 }
147 }
148
1a77d37f 149 if (s_name == NULL)
b21fa963 150 {
1a77d37f
JJ
151 sock = __nscd_open_socket (key, keylen, type, &serv_resp,
152 sizeof (serv_resp));
b21fa963
UD
153 if (sock == -1)
154 {
155 __nss_not_use_nscd_services = 1;
156 goto out;
157 }
b21fa963
UD
158 }
159
160 /* No value found so far. */
161 *result = NULL;
162
1a77d37f 163 if (__builtin_expect (serv_resp.found == -1, 0))
b21fa963
UD
164 {
165 /* The daemon does not cache this database. */
166 __nss_not_use_nscd_services = 1;
167 goto out_close;
168 }
169
1a77d37f 170 if (serv_resp.found == 1)
b21fa963
UD
171 {
172 char *cp = buf;
173 uintptr_t align1;
174 uintptr_t align2;
175 size_t total_len;
176 ssize_t cnt;
177 int n;
178
179 /* A first check whether the buffer is sufficiently large is possible. */
180 /* Now allocate the buffer the array for the group members. We must
181 align the pointer and the base of the h_addr_list pointers. */
182 align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
183 & (__alignof__ (char *) - 1));
1a77d37f
JJ
184 align2 = ((__alignof__ (char *) - ((cp + align1 + serv_resp.s_name_len
185 + serv_resp.s_proto_len)
b21fa963
UD
186 - ((char *) 0)))
187 & (__alignof__ (char *) - 1));
1a77d37f 188 if (buflen < (align1 + serv_resp.s_name_len + serv_resp.s_proto_len
b21fa963 189 + align2
1a77d37f 190 + (serv_resp.s_aliases_cnt + 1) * sizeof (char *)))
b21fa963
UD
191 {
192 no_room:
193 __set_errno (ERANGE);
194 retval = ERANGE;
195 goto out_close;
196 }
197 cp += align1;
198
199 /* Prepare the result as far as we can. */
200 resultbuf->s_aliases = (char **) cp;
1a77d37f 201 cp += (serv_resp.s_aliases_cnt + 1) * sizeof (char *);
b21fa963
UD
202
203 resultbuf->s_name = cp;
1a77d37f 204 cp += serv_resp.s_name_len;
b21fa963 205 resultbuf->s_proto = cp;
1a77d37f
JJ
206 cp += serv_resp.s_proto_len + align2;
207 resultbuf->s_port = serv_resp.s_port;
b21fa963
UD
208
209 if (s_name == NULL)
210 {
211 struct iovec vec[2];
212
213 vec[0].iov_base = resultbuf->s_name;
1a77d37f 214 vec[0].iov_len = serv_resp.s_name_len + serv_resp.s_proto_len;
b21fa963
UD
215 total_len = vec[0].iov_len;
216 n = 1;
217
1a77d37f 218 if (serv_resp.s_aliases_cnt > 0)
b21fa963 219 {
1a77d37f 220 aliases_len = alloca (serv_resp.s_aliases_cnt
b21fa963
UD
221 * sizeof (uint32_t));
222 vec[n].iov_base = (void *) aliases_len;
1a77d37f 223 vec[n].iov_len = serv_resp.s_aliases_cnt * sizeof (uint32_t);
b21fa963 224
1a77d37f 225 total_len += serv_resp.s_aliases_cnt * sizeof (uint32_t);
b21fa963
UD
226 ++n;
227 }
228
229 if ((size_t) __readvall (sock, vec, n) != total_len)
230 goto out_close;
231 }
232 else
233 memcpy (resultbuf->s_name, s_name,
1a77d37f 234 serv_resp.s_name_len + serv_resp.s_proto_len);
b21fa963
UD
235
236 /* Now we also can read the aliases. */
237 total_len = 0;
1a77d37f 238 for (cnt = 0; cnt < serv_resp.s_aliases_cnt; ++cnt)
b21fa963
UD
239 {
240 resultbuf->s_aliases[cnt] = cp;
241 cp += aliases_len[cnt];
242 total_len += aliases_len[cnt];
243 }
244 resultbuf->s_aliases[cnt] = NULL;
245
246 if (__builtin_expect ((const char *) aliases_list + total_len > recend,
247 0))
1a77d37f
JJ
248 {
249 /* aliases_len array might contain garbage during nscd GC cycle,
250 retry rather than fail in that case. */
251 if (aliases_list != NULL && mapped->head->gc_cycle != gc_cycle)
252 retval = -2;
253 goto out_close;
254 }
255
b21fa963
UD
256 /* See whether this would exceed the buffer capacity. */
257 if (__builtin_expect (cp > buf + buflen, 0))
1a77d37f
JJ
258 {
259 /* aliases_len array might contain garbage during nscd GC cycle,
260 retry rather than fail in that case. */
261 if (aliases_list != NULL && mapped->head->gc_cycle != gc_cycle)
262 {
263 retval = -2;
264 goto out_close;
265 }
266 goto no_room;
267 }
b21fa963
UD
268
269 /* And finally read the aliases. */
270 if (aliases_list == NULL)
271 {
272 if (total_len == 0
273 || ((size_t) __readall (sock, resultbuf->s_aliases[0], total_len)
274 == total_len))
275 {
276 retval = 0;
277 *result = resultbuf;
278 }
279 }
280 else
281 {
282 memcpy (resultbuf->s_aliases[0], aliases_list, total_len);
283
284 /* Try to detect corrupt databases. */
1a77d37f
JJ
285 if (resultbuf->s_name[serv_resp.s_name_len - 1] != '\0'
286 || resultbuf->s_proto[serv_resp.s_proto_len - 1] != '\0'
287 || ({for (cnt = 0; cnt < serv_resp.s_aliases_cnt; ++cnt)
b21fa963
UD
288 if (resultbuf->s_aliases[cnt][aliases_len[cnt] - 1]
289 != '\0')
290 break;
1a77d37f
JJ
291 cnt < serv_resp.s_aliases_cnt; }))
292 {
293 /* We cannot use the database. */
294 if (mapped->head->gc_cycle != gc_cycle)
295 retval = -2;
296 goto out_close;
297 }
b21fa963
UD
298
299 retval = 0;
300 *result = resultbuf;
301 }
302 }
303 else
304 {
cfca0aa3
UD
305 /* Set errno to 0 to indicate no error, just no found record. */
306 __set_errno (0);
b21fa963
UD
307 /* Even though we have not found anything, the result is zero. */
308 retval = 0;
309 }
310
311 out_close:
312 if (sock != -1)
313 close_not_cancel_no_status (sock);
314 out:
1a77d37f 315 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
b21fa963
UD
316 {
317 /* When we come here this means there has been a GC cycle while we
318 were looking for the data. This means the data might have been
319 inconsistent. Retry if possible. */
1a77d37f 320 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
b21fa963
UD
321 {
322 /* nscd is just running gc now. Disable using the mapping. */
1a77d37f
JJ
323 if (atomic_decrement_val (&mapped->counter) == 0)
324 __nscd_unmap (mapped);
b21fa963
UD
325 mapped = NO_MAPPING;
326 }
327
1a77d37f
JJ
328 if (retval != -1)
329 goto retry;
b21fa963
UD
330 }
331
332 return retval;
333}
This page took 0.109037 seconds and 5 git commands to generate.