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