]> git.ipfire.org Git - thirdparty/glibc.git/blame - nscd/initgrcache.c
Rename sys/ucontext.h to bits/ucontext.h.
[thirdparty/glibc.git] / nscd / initgrcache.c
CommitLineData
f7e7a396 1/* Cache handling for host lookup.
d614a753 2 Copyright (C) 2004-2020 Free Software Foundation, Inc.
f7e7a396
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
5
43bc8ac6 6 This program is free software; you can redistribute it and/or modify
2e2efe65
RM
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.
f7e7a396 10
43bc8ac6 11 This program is distributed in the hope that it will be useful,
f7e7a396 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
43bc8ac6
UD
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
f7e7a396 15
43bc8ac6 16 You should have received a copy of the GNU General Public License
5a82c748 17 along with this program; if not, see <https://www.gnu.org/licenses/>. */
f7e7a396
UD
18
19#include <assert.h>
20#include <errno.h>
21#include <grp.h>
22#include <libintl.h>
0e8caaf0 23#include <stdio.h>
f7e7a396
UD
24#include <string.h>
25#include <time.h>
26#include <unistd.h>
27#include <sys/mman.h>
0e8caaf0 28#include <sys/param.h>
94ab7b6b 29#include <sys/socket.h>
866ba63b 30#include <scratch_buffer.h>
a1a78204 31#include <config.h>
eac10791
UD
32
33#include "dbg_log.h"
34#include "nscd.h"
f7e7a396
UD
35
36#include "../nss/nsswitch.h"
37
a1a78204
SE
38#ifdef LINK_OBSOLETE_NSL
39# define DEFAULT_CONFIG "compat [NOTFOUND=return] files"
40#else
41# define DEFAULT_CONFIG "files"
42#endif
f7e7a396
UD
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
a4c7ea7b 61static time_t
f7e7a396 62addinitgroupsX (struct database_dyn *db, int fd, request_header *req,
20e498bd 63 void *key, uid_t uid, struct hashentry *const he,
f7e7a396
UD
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
a1ffb40e 81 if (__glibc_unlikely (debug_level > 0))
f7e7a396
UD
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;
b2179107 90 service_user *nip;
f7e7a396
UD
91 int no_more;
92
b2179107 93 if (group_database == NULL)
a9368c34
FW
94 no_more = __nss_database_lookup2 ("group", NULL, DEFAULT_CONFIG,
95 &group_database);
b2179107
AS
96 else
97 no_more = 0;
98 nip = group_database;
f7e7a396
UD
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;
cd248c3f 115 bool any_success = false;
f7e7a396 116
e8667ddc 117 /* This is temporary memory, we need not (and must not) call
f7e7a396
UD
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));
a1ffb40e 121 if (__glibc_unlikely (groups == NULL))
f7e7a396
UD
122 /* No more memory. */
123 goto out;
124
125 /* Nothing added yet. */
126 while (! no_more)
127 {
695c4370 128 long int prev_start = start;
f7e7a396
UD
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
695c4370
UD
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
f7e7a396
UD
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)
a6e8926f 165 __libc_fatal ("Illegal status in internal_getgrouplist.\n");
f7e7a396 166
fbbc73b3
UD
167 any_success |= status == NSS_STATUS_SUCCESS;
168
f7e7a396
UD
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
306dfba9 179 bool all_written;
f7e7a396 180 ssize_t total;
a4c7ea7b 181 time_t timeout;
f7e7a396 182 out:
306dfba9 183 all_written = true;
a4c7ea7b 184 timeout = MAX_TIMEOUT_VALUE;
fbbc73b3 185 if (!any_success)
f7e7a396
UD
186 {
187 /* Nothing found. Create a negative result record. */
306dfba9 188 total = sizeof (notfound);
f7e7a396
UD
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;
a4c7ea7b
UD
198
199 /* Reload with the same time-to-live value. */
200 timeout = dh->timeout = time (NULL) + db->postimeout;
f7e7a396
UD
201 }
202 else
203 {
204 /* We have no data. This means we send the standard reply for this
205 case. */
306dfba9
AS
206 if (fd != -1
207 && TEMP_FAILURE_RETRY (send (fd, &notfound, total,
208 MSG_NOSIGNAL)) != total)
209 all_written = false;
f7e7a396 210
3e1aa84e
UD
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))
445b4a53
TK
214 {
215 /* Mark the old entry as obsolete. */
216 if (dh != NULL)
217 dh->usable = false;
218 }
99231d9a
UD
219 else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
220 + req->key_len), 1)) != NULL)
f7e7a396 221 {
1cdeb237
SP
222 timeout = datahead_init_neg (&dataset->head,
223 (sizeof (struct dataset)
224 + req->key_len), total,
225 db->negtimeout);
f7e7a396
UD
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
7e71e55f 243 (void) cache_add (req->type, key_copy, req->key_len,
528741cb 244 &dataset->head, true, db, uid, he == NULL);
f7e7a396 245
00ebd7ed
UD
246 pthread_rwlock_unlock (&db->lock);
247
f7e7a396
UD
248 /* Mark the old entry as obsolete. */
249 if (dh != NULL)
250 dh->usable = false;
251 }
f7e7a396
UD
252 }
253 }
254 else
255 {
256
306dfba9 257 total = offsetof (struct dataset, strdata) + start * sizeof (int32_t);
f7e7a396
UD
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)
20e498bd
UD
267 dataset = (struct dataset *) mempool_alloc (db, total + req->key_len,
268 1);
f7e7a396
UD
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
1cdeb237
SP
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);
f7e7a396
UD
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
5a337776
UD
309 assert (cp == dataset->strdata + total - offsetof (struct dataset,
310 strdata));
311
f7e7a396
UD
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
c52137d3 334 = (struct dataset *) mempool_alloc (db, total + req->key_len,
20e498bd 335 1);
f7e7a396
UD
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
8c78faa9
AZ
356 if (writeall (fd, &dataset->resp, dataset->head.recsize)
357 != dataset->head.recsize)
358 all_written = false;
f7e7a396
UD
359 }
360
361
362 /* Add the record to the database. But only if it has not been
363 stored on the stack. */
364 if (! alloca_used)
365 {
366 /* If necessary, we also propagate the data to disk. */
367 if (db->persistent)
368 {
369 // XXX async OK?
370 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
371 msync ((void *) pval,
34a5a146
JM
372 ((uintptr_t) dataset & pagesize_m1) + total
373 + req->key_len, MS_ASYNC);
f7e7a396
UD
374 }
375
7e71e55f 376 (void) cache_add (INITGROUPS, cp, req->key_len, &dataset->head, true,
528741cb 377 db, uid, he == NULL);
00ebd7ed
UD
378
379 pthread_rwlock_unlock (&db->lock);
f7e7a396
UD
380 }
381 }
382
383 free (groups);
384
306dfba9 385 if (__builtin_expect (!all_written, 0) && debug_level > 0)
f7e7a396
UD
386 {
387 char buf[256];
388 dbg_log (_("short write in %s: %s"), __FUNCTION__,
389 strerror_r (errno, buf, sizeof (buf)));
390 }
a4c7ea7b
UD
391
392 return timeout;
f7e7a396
UD
393}
394
395
396void
397addinitgroups (struct database_dyn *db, int fd, request_header *req, void *key,
398 uid_t uid)
399{
400 addinitgroupsX (db, fd, req, key, uid, NULL, NULL);
401}
402
403
a4c7ea7b 404time_t
f7e7a396
UD
405readdinitgroups (struct database_dyn *db, struct hashentry *he,
406 struct datahead *dh)
407{
408 request_header req =
409 {
410 .type = INITGROUPS,
411 .key_len = he->len
412 };
413
a4c7ea7b 414 return addinitgroupsX (db, -1, &req, db->data + he->key, he->owner, he, dh);
f7e7a396 415}