1/* Cache handling for host lookup.
2 Copyright (C) 2004-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
10
11 This program 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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>. */
18
19#include <assert.h>
20#include <errno.h>
21#include <grp.h>
22#include <libintl.h>
23#include <string.h>
24#include <time.h>
25#include <unistd.h>
26#include <sys/mman.h>
27#include <scratch_buffer.h>
28#include <config.h>
29
30#include "dbg_log.h"
31#include "nscd.h"
32#ifdef HAVE_SENDFILE
33# include <kernel-features.h>
34#endif
35
36#include "../nss/nsswitch.h"
37
38#ifdef LINK_OBSOLETE_NSL
39# define DEFAULT_CONFIG "compat [NOTFOUND=return] files"
40#else
41# define DEFAULT_CONFIG "files"
42#endif
43
44/* Type of the lookup function. */
45typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
46 long int *, long int *,
47 gid_t **, long int, int *);
48
49
50static const initgr_response_header notfound =
51{
52 .version = NSCD_VERSION,
53 .found = 0,
54 .ngrps = 0
55};
56
57
58#include "../grp/compat-initgroups.c"
59
60
61static time_t
62addinitgroupsX (struct database_dyn *db, int fd, request_header *req,
63 void *key, uid_t uid, struct hashentry *const he,
64 struct datahead *dh)
65{
66 /* Search for the entry matching the key. Please note that we don't
67 look again in the table whether the dataset is now available. We
68 simply insert it. It does not matter if it is in there twice. The
69 pruning function only will look at the timestamp. */
70
71
72 /* We allocate all data in one memory block: the iov vector,
73 the response header and the dataset itself. */
74 struct dataset
75 {
76 struct datahead head;
77 initgr_response_header resp;
78 char strdata[0];
79 } *dataset = NULL;
80
81 if (__glibc_unlikely (debug_level > 0))
82 {
83 if (he == NULL)
84 dbg_log (_("Haven't found \"%s\" in group cache!"), (char *) key);
85 else
86 dbg_log (_("Reloading \"%s\" in group cache!"), (char *) key);
87 }
88
89 static service_user *group_database;
90 service_user *nip;
91 int no_more;
92
93 if (group_database == NULL)
94 no_more = __nss_database_lookup ("group", NULL, DEFAULT_CONFIG,
95 &group_database);
96 else
97 no_more = 0;
98 nip = group_database;
99
100 /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
101 limit can be raised in the kernel configuration without having to
102 recompile libc. */
103 long int limit = __sysconf (_SC_NGROUPS_MAX);
104
105 long int size;
106 if (limit > 0)
107 /* We limit the size of the intially allocated array. */
108 size = MIN (limit, 64);
109 else
110 /* No fixed limit on groups. Pick a starting buffer size. */
111 size = 16;
112
113 long int start = 0;
114 bool all_tryagain = true;
115 bool any_success = false;
116
117 /* This is temporary memory, we need not (and must not) call
118 mempool_alloc. */
119 // XXX This really should use alloca. need to change the backends.
120 gid_t *groups = (gid_t *) malloc (size * sizeof (gid_t));
121 if (__glibc_unlikely (groups == NULL))
122 /* No more memory. */
123 goto out;
124
125 /* Nothing added yet. */
126 while (! no_more)
127 {
128 long int prev_start = start;
129 enum nss_status status;
130 initgroups_dyn_function fct;
131 fct = __nss_lookup_function (nip, "initgroups_dyn");
132
133 if (fct == NULL)
134 {
135 status = compat_call (nip, key, -1, &start, &size, &groups,
136 limit, &errno);
137
138 if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
139 break;
140 }
141 else
142 status = DL_CALL_FCT (fct, (key, -1, &start, &size, &groups,
143 limit, &errno));
144
145 /* Remove duplicates. */
146 long int cnt = prev_start;
147 while (cnt < start)
148 {
149 long int inner;
150 for (inner = 0; inner < prev_start; ++inner)
151 if (groups[inner] == groups[cnt])
152 break;
153
154 if (inner < prev_start)
155 groups[cnt] = groups[--start];
156 else
157 ++cnt;
158 }
159
160 if (status != NSS_STATUS_TRYAGAIN)
161 all_tryagain = false;
162
163 /* This is really only for debugging. */
164 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
165 __libc_fatal ("illegal status in internal_getgrouplist");
166
167 any_success |= status == NSS_STATUS_SUCCESS;
168
169 if (status != NSS_STATUS_SUCCESS
170 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
171 break;
172
173 if (nip->next == NULL)
174 no_more = -1;
175 else
176 nip = nip->next;
177 }
178
179 bool all_written;
180 ssize_t total;
181 time_t timeout;
182 out:
183 all_written = true;
184 timeout = MAX_TIMEOUT_VALUE;
185 if (!any_success)
186 {
187 /* Nothing found. Create a negative result record. */
188 total = sizeof (notfound);
189
190 if (he != NULL && all_tryagain)
191 {
192 /* If we have an old record available but cannot find one now
193 because the service is not available we keep the old record
194 and make sure it does not get removed. */
195 if (reload_count != UINT_MAX && dh->nreloads == reload_count)
196 /* Do not reset the value if we never not reload the record. */
197 dh->nreloads = reload_count - 1;
198
199 /* Reload with the same time-to-live value. */
200 timeout = dh->timeout = time (NULL) + db->postimeout;
201 }
202 else
203 {
204 /* We have no data. This means we send the standard reply for this
205 case. */
206 if (fd != -1
207 && TEMP_FAILURE_RETRY (send (fd, &notfound, total,
208 MSG_NOSIGNAL)) != total)
209 all_written = false;
210
211 /* If we have a transient error or cannot permanently store
212 the result, so be it. */
213 if (all_tryagain || __builtin_expect (db->negtimeout == 0, 0))
214 {
215 /* Mark the old entry as obsolete. */
216 if (dh != NULL)
217 dh->usable = false;
218 }
219 else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
220 + req->key_len), 1)) != NULL)
221 {
222 timeout = datahead_init_neg (&dataset->head,
223 (sizeof (struct dataset)
224 + req->key_len), total,
225 db->negtimeout);
226
227 /* This is the reply. */
228 memcpy (&dataset->resp, &notfound, total);
229
230 /* Copy the key data. */
231 char *key_copy = memcpy (dataset->strdata, key, req->key_len);
232
233 /* If necessary, we also propagate the data to disk. */
234 if (db->persistent)
235 {
236 // XXX async OK?
237 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
238 msync ((void *) pval,
239 ((uintptr_t) dataset & pagesize_m1)
240 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
241 }
242
243 (void) cache_add (req->type, key_copy, req->key_len,
244 &dataset->head, true, db, uid, he == NULL);
245
246 pthread_rwlock_unlock (&db->lock);
247
248 /* Mark the old entry as obsolete. */
249 if (dh != NULL)
250 dh->usable = false;
251 }
252 }
253 }
254 else
255 {
256
257 total = offsetof (struct dataset, strdata) + start * sizeof (int32_t);
258
259 /* If we refill the cache, first assume the reconrd did not
260 change. Allocate memory on the cache since it is likely
261 discarded anyway. If it turns out to be necessary to have a
262 new record we can still allocate real memory. */
263 bool alloca_used = false;
264 dataset = NULL;
265
266 if (he == NULL)
267 dataset = (struct dataset *) mempool_alloc (db, total + req->key_len,
268 1);
269
270 if (dataset == NULL)
271 {
272 /* We cannot permanently add the result in the moment. But
273 we can provide the result as is. Store the data in some
274 temporary memory. */
275 dataset = (struct dataset *) alloca (total + req->key_len);
276
277 /* We cannot add this record to the permanent database. */
278 alloca_used = true;
279 }
280
281 timeout = datahead_init_pos (&dataset->head, total + req->key_len,
282 total - offsetof (struct dataset, resp),
283 he == NULL ? 0 : dh->nreloads + 1,
284 db->postimeout);
285
286 dataset->resp.version = NSCD_VERSION;
287 dataset->resp.found = 1;
288 dataset->resp.ngrps = start;
289
290 char *cp = dataset->strdata;
291
292 /* Copy the GID values. If the size of the types match this is
293 very simple. */
294 if (sizeof (gid_t) == sizeof (int32_t))
295 cp = mempcpy (cp, groups, start * sizeof (gid_t));
296 else
297 {
298 gid_t *gcp = (gid_t *) cp;
299
300 for (int i = 0; i < start; ++i)
301 *gcp++ = groups[i];
302
303 cp = (char *) gcp;
304 }
305
306 /* Finally the user name. */
307 memcpy (cp, key, req->key_len);
308
309 assert (cp == dataset->strdata + total - offsetof (struct dataset,
310 strdata));
311
312 /* Now we can determine whether on refill we have to create a new
313 record or not. */
314 if (he != NULL)
315 {
316 assert (fd == -1);
317
318 if (total + req->key_len == dh->allocsize
319 && total - offsetof (struct dataset, resp) == dh->recsize
320 && memcmp (&dataset->resp, dh->data,
321 dh->allocsize - offsetof (struct dataset, resp)) == 0)
322 {
323 /* The data has not changed. We will just bump the
324 timeout value. Note that the new record has been
325 allocated on the stack and need not be freed. */
326 dh->timeout = dataset->head.timeout;
327 ++dh->nreloads;
328 }
329 else
330 {
331 /* We have to create a new record. Just allocate
332 appropriate memory and copy it. */
333 struct dataset *newp
334 = (struct dataset *) mempool_alloc (db, total + req->key_len,
335 1);
336 if (newp != NULL)
337 {
338 /* Adjust pointer into the memory block. */
339 cp = (char *) newp + (cp - (char *) dataset);
340
341 dataset = memcpy (newp, dataset, total + req->key_len);
342 alloca_used = false;
343 }
344
345 /* Mark the old record as obsolete. */
346 dh->usable = false;
347 }
348 }
349 else
350 {
351 /* We write the dataset before inserting it to the database
352 since while inserting this thread might block and so would
353 unnecessarily let the receiver wait. */
354 assert (fd != -1);
355
356#ifdef HAVE_SENDFILE
357 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
358 {
359 assert (db->wr_fd != -1);
360 assert ((char *) &dataset->resp > (char *) db->data);
361 assert ((char *) dataset - (char *) db->head
362 + total
363 <= (sizeof (struct database_pers_head)
364 + db->head->module * sizeof (ref_t)
365 + db->head->data_size));
366 ssize_t written = sendfileall (fd, db->wr_fd,
367 (char *) &dataset->resp
368 - (char *) db->head,
369 dataset->head.recsize);
370 if (written != dataset->head.recsize)
371 {
372# ifndef __ASSUME_SENDFILE
373 if (written == -1 && errno == ENOSYS)
374 goto use_write;
375# endif
376 all_written = false;
377 }
378 }
379 else
380# ifndef __ASSUME_SENDFILE
381 use_write:
382# endif
383#endif
384 if (writeall (fd, &dataset->resp, dataset->head.recsize)
385 != dataset->head.recsize)
386 all_written = false;
387 }
388
389
390 /* Add the record to the database. But only if it has not been
391 stored on the stack. */
392 if (! alloca_used)
393 {
394 /* If necessary, we also propagate the data to disk. */
395 if (db->persistent)
396 {
397 // XXX async OK?
398 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
399 msync ((void *) pval,
400 ((uintptr_t) dataset & pagesize_m1) + total +
401 req->key_len, MS_ASYNC);
402 }
403
404 (void) cache_add (INITGROUPS, cp, req->key_len, &dataset->head, true,
405 db, uid, he == NULL);
406
407 pthread_rwlock_unlock (&db->lock);
408 }
409 }
410
411 free (groups);
412
413 if (__builtin_expect (!all_written, 0) && debug_level > 0)
414 {
415 char buf[256];
416 dbg_log (_("short write in %s: %s"), __FUNCTION__,
417 strerror_r (errno, buf, sizeof (buf)));
418 }
419
420 return timeout;
421}
422
423
424void
425addinitgroups (struct database_dyn *db, int fd, request_header *req, void *key,
426 uid_t uid)
427{
428 addinitgroupsX (db, fd, req, key, uid, NULL, NULL);
429}
430
431
432time_t
433readdinitgroups (struct database_dyn *db, struct hashentry *he,
434 struct datahead *dh)
435{
436 request_header req =
437 {
438 .type = INITGROUPS,
439 .key_len = he->len
440 };
441
442 return addinitgroupsX (db, -1, &req, db->data + he->key, he->owner, he, dh);
443}
444