1 /* Cache handling for passwd lookup.
2 Copyright (C) 1998-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
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.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
31 #include <sys/socket.h>
32 #include <stackinfo.h>
33 #include <scratch_buffer.h>
38 /* This is the standard reply in case the service is disabled. */
39 static const pw_response_header disabled
=
41 .version
= NSCD_VERSION
,
52 /* This is the struct describing how to write this record. */
53 const struct iovec pwd_iov_disabled
=
55 .iov_base
= (void *) &disabled
,
56 .iov_len
= sizeof (disabled
)
60 /* This is the standard reply in case we haven't found the dataset. */
61 static const pw_response_header notfound
=
63 .version
= NSCD_VERSION
,
76 cache_addpw (struct database_dyn
*db
, int fd
, request_header
*req
,
77 const void *key
, struct passwd
*pwd
, uid_t owner
,
78 struct hashentry
*const he
, struct datahead
*dh
, int errval
)
80 bool all_written
= true;
82 time_t t
= time (NULL
);
84 /* We allocate all data in one memory block: the iov vector,
85 the response header and the dataset itself. */
89 pw_response_header resp
;
93 assert (offsetof (struct dataset
, resp
) == offsetof (struct datahead
, data
));
95 time_t timeout
= MAX_TIMEOUT_VALUE
;
98 if (he
!= NULL
&& errval
== EAGAIN
)
100 /* If we have an old record available but cannot find one
101 now because the service is not available we keep the old
102 record and make sure it does not get removed. */
103 if (reload_count
!= UINT_MAX
&& dh
->nreloads
== reload_count
)
104 /* Do not reset the value if we never not reload the record. */
105 dh
->nreloads
= reload_count
- 1;
107 /* Reload with the same time-to-live value. */
108 timeout
= dh
->timeout
= t
+ db
->postimeout
;
114 /* We have no data. This means we send the standard reply for this
116 total
= sizeof (notfound
);
119 && TEMP_FAILURE_RETRY (send (fd
, ¬found
, total
,
120 MSG_NOSIGNAL
)) != total
)
123 /* If we have a transient error or cannot permanently store
124 the result, so be it. */
125 if (errval
== EAGAIN
|| __glibc_unlikely (db
->negtimeout
== 0))
127 /* Mark the old entry as obsolete. */
131 else if ((dataset
= mempool_alloc (db
, (sizeof (struct dataset
)
132 + req
->key_len
), 1)) != NULL
)
134 timeout
= datahead_init_neg (&dataset
->head
,
135 (sizeof (struct dataset
)
136 + req
->key_len
), total
,
139 /* This is the reply. */
140 memcpy (&dataset
->resp
, ¬found
, total
);
142 /* Copy the key data. */
143 char *key_copy
= memcpy (dataset
->strdata
, key
, req
->key_len
);
145 /* If necessary, we also propagate the data to disk. */
149 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
150 msync ((void *) pval
,
151 ((uintptr_t) dataset
& pagesize_m1
)
152 + sizeof (struct dataset
) + req
->key_len
, MS_ASYNC
);
155 (void) cache_add (req
->type
, key_copy
, req
->key_len
,
156 &dataset
->head
, true, db
, owner
, he
== NULL
);
158 pthread_rwlock_unlock (&db
->lock
);
160 /* Mark the old entry as obsolete. */
168 /* Determine the I/O structure. */
169 size_t pw_name_len
= strlen (pwd
->pw_name
) + 1;
170 size_t pw_passwd_len
= strlen (pwd
->pw_passwd
) + 1;
171 size_t pw_gecos_len
= strlen (pwd
->pw_gecos
) + 1;
172 size_t pw_dir_len
= strlen (pwd
->pw_dir
) + 1;
173 size_t pw_shell_len
= strlen (pwd
->pw_shell
) + 1;
175 const size_t key_len
= strlen (key
);
176 const size_t buf_len
= 3 * sizeof (pwd
->pw_uid
) + key_len
+ 1;
177 char *buf
= alloca (buf_len
);
180 /* We need this to insert the `byuid' entry. */
182 n
= snprintf (buf
, buf_len
, "%d%c%n%s", pwd
->pw_uid
, '\0',
183 &key_offset
, (char *) key
) + 1;
185 total
= (offsetof (struct dataset
, strdata
)
186 + pw_name_len
+ pw_passwd_len
187 + pw_gecos_len
+ pw_dir_len
+ pw_shell_len
);
189 /* If we refill the cache, first assume the reconrd did not
190 change. Allocate memory on the cache since it is likely
191 discarded anyway. If it turns out to be necessary to have a
192 new record we can still allocate real memory. */
193 bool alloca_used
= false;
198 /* Prevent an INVALIDATE request from pruning the data between
199 the two calls to cache_add. */
201 pthread_mutex_lock (&db
->prune_run_lock
);
202 dataset
= (struct dataset
*) mempool_alloc (db
, total
+ n
, 1);
207 if (he
== NULL
&& db
->propagate
)
208 pthread_mutex_unlock (&db
->prune_run_lock
);
210 /* We cannot permanently add the result in the moment. But
211 we can provide the result as is. Store the data in some
213 dataset
= (struct dataset
*) alloca (total
+ n
);
215 /* We cannot add this record to the permanent database. */
219 timeout
= datahead_init_pos (&dataset
->head
, total
+ n
,
220 total
- offsetof (struct dataset
, resp
),
221 he
== NULL
? 0 : dh
->nreloads
+ 1,
224 dataset
->resp
.version
= NSCD_VERSION
;
225 dataset
->resp
.found
= 1;
226 dataset
->resp
.pw_name_len
= pw_name_len
;
227 dataset
->resp
.pw_passwd_len
= pw_passwd_len
;
228 dataset
->resp
.pw_uid
= pwd
->pw_uid
;
229 dataset
->resp
.pw_gid
= pwd
->pw_gid
;
230 dataset
->resp
.pw_gecos_len
= pw_gecos_len
;
231 dataset
->resp
.pw_dir_len
= pw_dir_len
;
232 dataset
->resp
.pw_shell_len
= pw_shell_len
;
234 cp
= dataset
->strdata
;
236 /* Copy the strings over into the buffer. */
237 cp
= mempcpy (cp
, pwd
->pw_name
, pw_name_len
);
238 cp
= mempcpy (cp
, pwd
->pw_passwd
, pw_passwd_len
);
239 cp
= mempcpy (cp
, pwd
->pw_gecos
, pw_gecos_len
);
240 cp
= mempcpy (cp
, pwd
->pw_dir
, pw_dir_len
);
241 cp
= mempcpy (cp
, pwd
->pw_shell
, pw_shell_len
);
243 /* Finally the stringified UID value. */
245 char *key_copy
= cp
+ key_offset
;
246 assert (key_copy
== strchr (cp
, '\0') + 1);
248 assert (cp
== dataset
->strdata
+ total
- offsetof (struct dataset
,
251 /* Now we can determine whether on refill we have to create a new
257 if (dataset
->head
.allocsize
== dh
->allocsize
258 && dataset
->head
.recsize
== dh
->recsize
259 && memcmp (&dataset
->resp
, dh
->data
,
260 dh
->allocsize
- offsetof (struct dataset
, resp
)) == 0)
262 /* The data has not changed. We will just bump the
263 timeout value. Note that the new record has been
264 allocated on the stack and need not be freed. */
265 dh
->timeout
= dataset
->head
.timeout
;
270 /* We have to create a new record. Just allocate
271 appropriate memory and copy it. */
273 = (struct dataset
*) mempool_alloc (db
, total
+ n
, 1);
276 /* Adjust pointer into the memory block. */
277 cp
= (char *) newp
+ (cp
- (char *) dataset
);
278 key_copy
= (char *) newp
+ (key_copy
- (char *) dataset
);
280 dataset
= memcpy (newp
, dataset
, total
+ n
);
284 /* Mark the old record as obsolete. */
290 /* We write the dataset before inserting it to the database
291 since while inserting this thread might block and so would
292 unnecessarily let the receiver wait. */
295 if (writeall (fd
, &dataset
->resp
, dataset
->head
.recsize
)
296 != dataset
->head
.recsize
)
301 /* Add the record to the database. But only if it has not been
302 stored on the stack. */
305 /* If necessary, we also propagate the data to disk. */
309 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
310 msync ((void *) pval
,
311 ((uintptr_t) dataset
& pagesize_m1
) + total
+ n
,
315 /* NB: in the following code we always must add the entry
316 marked with FIRST first. Otherwise we end up with
317 dangling "pointers" in case a latter hash entry cannot be
321 /* If the request was by UID, add that entry first. */
322 if (req
->type
== GETPWBYUID
)
324 if (cache_add (GETPWBYUID
, cp
, key_offset
, &dataset
->head
, true,
325 db
, owner
, he
== NULL
) < 0)
330 /* If the key is different from the name add a separate entry. */
331 else if (strcmp (key_copy
, dataset
->strdata
) != 0)
333 if (cache_add (GETPWBYNAME
, key_copy
, key_len
+ 1,
334 &dataset
->head
, true, db
, owner
, he
== NULL
) < 0)
340 /* We have to add the value for both, byname and byuid. */
341 if ((req
->type
== GETPWBYNAME
|| db
->propagate
)
342 && __builtin_expect (cache_add (GETPWBYNAME
, dataset
->strdata
,
343 pw_name_len
, &dataset
->head
,
344 first
, db
, owner
, he
== NULL
)
347 if (req
->type
== GETPWBYNAME
&& db
->propagate
)
348 (void) cache_add (GETPWBYUID
, cp
, key_offset
, &dataset
->head
,
349 false, db
, owner
, false);
353 pthread_rwlock_unlock (&db
->lock
);
354 if (he
== NULL
&& db
->propagate
)
355 pthread_mutex_unlock (&db
->prune_run_lock
);
359 if (__builtin_expect (!all_written
, 0) && debug_level
> 0)
362 dbg_log (_("short write in %s: %s"), __FUNCTION__
,
363 strerror_r (errno
, buf
, sizeof (buf
)));
378 lookup (int type
, union keytype key
, struct passwd
*resultbufp
, char *buffer
,
379 size_t buflen
, struct passwd
**pwd
)
381 if (type
== GETPWBYNAME
)
382 return __getpwnam_r (key
.v
, resultbufp
, buffer
, buflen
, pwd
);
384 return __getpwuid_r (key
.u
, resultbufp
, buffer
, buflen
, pwd
);
389 addpwbyX (struct database_dyn
*db
, int fd
, request_header
*req
,
390 union keytype key
, const char *keystr
, uid_t c_uid
,
391 struct hashentry
*he
, struct datahead
*dh
)
393 /* Search for the entry matching the key. Please note that we don't
394 look again in the table whether the dataset is now available. We
395 simply insert it. It does not matter if it is in there twice. The
396 pruning function only will look at the timestamp. */
397 struct passwd resultbuf
;
400 struct scratch_buffer tmpbuf
;
401 scratch_buffer_init (&tmpbuf
);
403 if (__glibc_unlikely (debug_level
> 0))
406 dbg_log (_("Haven't found \"%s\" in user database cache!"), keystr
);
408 dbg_log (_("Reloading \"%s\" in user database cache!"), keystr
);
411 while (lookup (req
->type
, key
, &resultbuf
,
412 tmpbuf
.data
, tmpbuf
.length
, &pwd
) != 0
413 && (errval
= errno
) == ERANGE
)
414 if (!scratch_buffer_grow (&tmpbuf
))
416 /* We ran out of memory. We cannot do anything but sending a
417 negative response. In reality this should never
420 /* We set the error to indicate this is (possibly) a temporary
421 error and that it does not mean the entry is not available
427 /* Add the entry to the cache. */
428 time_t timeout
= cache_addpw (db
, fd
, req
, keystr
, pwd
, c_uid
, he
, dh
,
430 scratch_buffer_free (&tmpbuf
);
436 addpwbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
437 void *key
, uid_t c_uid
)
439 union keytype u
= { .v
= key
};
441 addpwbyX (db
, fd
, req
, u
, key
, c_uid
, NULL
, NULL
);
446 readdpwbyname (struct database_dyn
*db
, struct hashentry
*he
,
454 union keytype u
= { .v
= db
->data
+ he
->key
};
456 return addpwbyX (db
, -1, &req
, u
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
461 addpwbyuid (struct database_dyn
*db
, int fd
, request_header
*req
,
462 void *key
, uid_t c_uid
)
465 uid_t uid
= strtoul ((char *) key
, &ep
, 10);
467 if (*(char *) key
== '\0' || *ep
!= '\0') /* invalid numeric uid */
470 dbg_log (_("Invalid numeric uid \"%s\"!"), (char *) key
);
476 union keytype u
= { .u
= uid
};
478 addpwbyX (db
, fd
, req
, u
, key
, c_uid
, NULL
, NULL
);
483 readdpwbyuid (struct database_dyn
*db
, struct hashentry
*he
,
487 uid_t uid
= strtoul (db
->data
+ he
->key
, &ep
, 10);
489 /* Since the key has been added before it must be OK. */
490 assert (*(db
->data
+ he
->key
) != '\0' && *ep
== '\0');
497 union keytype u
= { .u
= uid
};
499 return addpwbyX (db
, -1, &req
, u
, db
->data
+ he
->key
, he
->owner
, he
, dh
);