pwdcache.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* Cache handling for passwd lookup.
  2. Copyright (C) 1998-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <https://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <errno.h>
  16. #include <error.h>
  17. #include <libintl.h>
  18. #include <pwd.h>
  19. #include <stdbool.h>
  20. #include <stddef.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #include <unistd.h>
  26. #include <sys/mman.h>
  27. #include <sys/socket.h>
  28. #include <stackinfo.h>
  29. #include <scratch_buffer.h>
  30. #include "nscd.h"
  31. #include "dbg_log.h"
  32. /* This is the standard reply in case the service is disabled. */
  33. static const pw_response_header disabled =
  34. {
  35. .version = NSCD_VERSION,
  36. .found = -1,
  37. .pw_name_len = 0,
  38. .pw_passwd_len = 0,
  39. .pw_uid = -1,
  40. .pw_gid = -1,
  41. .pw_gecos_len = 0,
  42. .pw_dir_len = 0,
  43. .pw_shell_len = 0
  44. };
  45. /* This is the struct describing how to write this record. */
  46. const struct iovec pwd_iov_disabled =
  47. {
  48. .iov_base = (void *) &disabled,
  49. .iov_len = sizeof (disabled)
  50. };
  51. /* This is the standard reply in case we haven't found the dataset. */
  52. static const pw_response_header notfound =
  53. {
  54. .version = NSCD_VERSION,
  55. .found = 0,
  56. .pw_name_len = 0,
  57. .pw_passwd_len = 0,
  58. .pw_uid = -1,
  59. .pw_gid = -1,
  60. .pw_gecos_len = 0,
  61. .pw_dir_len = 0,
  62. .pw_shell_len = 0
  63. };
  64. static time_t
  65. cache_addpw (struct database_dyn *db, int fd, request_header *req,
  66. const void *key, struct passwd *pwd, uid_t owner,
  67. struct hashentry *const he, struct datahead *dh, int errval)
  68. {
  69. bool all_written = true;
  70. ssize_t total;
  71. time_t t = time (NULL);
  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. pw_response_header resp;
  78. char strdata[0];
  79. } *dataset;
  80. assert (offsetof (struct dataset, resp) == offsetof (struct datahead, data));
  81. time_t timeout = MAX_TIMEOUT_VALUE;
  82. if (pwd == NULL)
  83. {
  84. if (he != NULL && errval == EAGAIN)
  85. {
  86. /* If we have an old record available but cannot find one
  87. now because the service is not available we keep the old
  88. record and make sure it does not get removed. */
  89. if (reload_count != UINT_MAX && dh->nreloads == reload_count)
  90. /* Do not reset the value if we never not reload the record. */
  91. dh->nreloads = reload_count - 1;
  92. /* Reload with the same time-to-live value. */
  93. timeout = dh->timeout = t + db->postimeout;
  94. total = 0;
  95. }
  96. else
  97. {
  98. /* We have no data. This means we send the standard reply for this
  99. case. */
  100. total = sizeof (notfound);
  101. if (fd != -1
  102. && TEMP_FAILURE_RETRY (send (fd, &notfound, total,
  103. MSG_NOSIGNAL)) != total)
  104. all_written = false;
  105. /* If we have a transient error or cannot permanently store
  106. the result, so be it. */
  107. if (errval == EAGAIN || __glibc_unlikely (db->negtimeout == 0))
  108. {
  109. /* Mark the old entry as obsolete. */
  110. if (dh != NULL)
  111. dh->usable = false;
  112. }
  113. else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
  114. + req->key_len), 1)) != NULL)
  115. {
  116. timeout = datahead_init_neg (&dataset->head,
  117. (sizeof (struct dataset)
  118. + req->key_len), total,
  119. db->negtimeout);
  120. /* This is the reply. */
  121. memcpy (&dataset->resp, &notfound, total);
  122. /* Copy the key data. */
  123. char *key_copy = memcpy (dataset->strdata, key, req->key_len);
  124. /* If necessary, we also propagate the data to disk. */
  125. if (db->persistent)
  126. {
  127. // XXX async OK?
  128. uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
  129. msync ((void *) pval,
  130. ((uintptr_t) dataset & pagesize_m1)
  131. + sizeof (struct dataset) + req->key_len, MS_ASYNC);
  132. }
  133. (void) cache_add (req->type, key_copy, req->key_len,
  134. &dataset->head, true, db, owner, he == NULL);
  135. pthread_rwlock_unlock (&db->lock);
  136. /* Mark the old entry as obsolete. */
  137. if (dh != NULL)
  138. dh->usable = false;
  139. }
  140. }
  141. }
  142. else
  143. {
  144. /* Determine the I/O structure. */
  145. size_t pw_name_len = strlen (pwd->pw_name) + 1;
  146. size_t pw_passwd_len = strlen (pwd->pw_passwd) + 1;
  147. size_t pw_gecos_len = strlen (pwd->pw_gecos) + 1;
  148. size_t pw_dir_len = strlen (pwd->pw_dir) + 1;
  149. size_t pw_shell_len = strlen (pwd->pw_shell) + 1;
  150. char *cp;
  151. const size_t key_len = strlen (key);
  152. const size_t buf_len = 3 * sizeof (pwd->pw_uid) + key_len + 1;
  153. char *buf = alloca (buf_len);
  154. ssize_t n;
  155. /* We need this to insert the `byuid' entry. */
  156. int key_offset;
  157. n = snprintf (buf, buf_len, "%d%c%n%s", pwd->pw_uid, '\0',
  158. &key_offset, (char *) key) + 1;
  159. total = (offsetof (struct dataset, strdata)
  160. + pw_name_len + pw_passwd_len
  161. + pw_gecos_len + pw_dir_len + pw_shell_len);
  162. /* If we refill the cache, first assume the reconrd did not
  163. change. Allocate memory on the cache since it is likely
  164. discarded anyway. If it turns out to be necessary to have a
  165. new record we can still allocate real memory. */
  166. bool alloca_used = false;
  167. dataset = NULL;
  168. if (he == NULL)
  169. {
  170. /* Prevent an INVALIDATE request from pruning the data between
  171. the two calls to cache_add. */
  172. if (db->propagate)
  173. pthread_mutex_lock (&db->prune_run_lock);
  174. dataset = (struct dataset *) mempool_alloc (db, total + n, 1);
  175. }
  176. if (dataset == NULL)
  177. {
  178. if (he == NULL && db->propagate)
  179. pthread_mutex_unlock (&db->prune_run_lock);
  180. /* We cannot permanently add the result in the moment. But
  181. we can provide the result as is. Store the data in some
  182. temporary memory. */
  183. dataset = (struct dataset *) alloca (total + n);
  184. /* We cannot add this record to the permanent database. */
  185. alloca_used = true;
  186. }
  187. timeout = datahead_init_pos (&dataset->head, total + n,
  188. total - offsetof (struct dataset, resp),
  189. he == NULL ? 0 : dh->nreloads + 1,
  190. db->postimeout);
  191. dataset->resp.version = NSCD_VERSION;
  192. dataset->resp.found = 1;
  193. dataset->resp.pw_name_len = pw_name_len;
  194. dataset->resp.pw_passwd_len = pw_passwd_len;
  195. dataset->resp.pw_uid = pwd->pw_uid;
  196. dataset->resp.pw_gid = pwd->pw_gid;
  197. dataset->resp.pw_gecos_len = pw_gecos_len;
  198. dataset->resp.pw_dir_len = pw_dir_len;
  199. dataset->resp.pw_shell_len = pw_shell_len;
  200. cp = dataset->strdata;
  201. /* Copy the strings over into the buffer. */
  202. cp = mempcpy (cp, pwd->pw_name, pw_name_len);
  203. cp = mempcpy (cp, pwd->pw_passwd, pw_passwd_len);
  204. cp = mempcpy (cp, pwd->pw_gecos, pw_gecos_len);
  205. cp = mempcpy (cp, pwd->pw_dir, pw_dir_len);
  206. cp = mempcpy (cp, pwd->pw_shell, pw_shell_len);
  207. /* Finally the stringified UID value. */
  208. memcpy (cp, buf, n);
  209. char *key_copy = cp + key_offset;
  210. assert (key_copy == strchr (cp, '\0') + 1);
  211. assert (cp == dataset->strdata + total - offsetof (struct dataset,
  212. strdata));
  213. /* Now we can determine whether on refill we have to create a new
  214. record or not. */
  215. if (he != NULL)
  216. {
  217. assert (fd == -1);
  218. if (dataset->head.allocsize == dh->allocsize
  219. && dataset->head.recsize == dh->recsize
  220. && memcmp (&dataset->resp, dh->data,
  221. dh->allocsize - offsetof (struct dataset, resp)) == 0)
  222. {
  223. /* The data has not changed. We will just bump the
  224. timeout value. Note that the new record has been
  225. allocated on the stack and need not be freed. */
  226. dh->timeout = dataset->head.timeout;
  227. ++dh->nreloads;
  228. }
  229. else
  230. {
  231. /* We have to create a new record. Just allocate
  232. appropriate memory and copy it. */
  233. struct dataset *newp
  234. = (struct dataset *) mempool_alloc (db, total + n, 1);
  235. if (newp != NULL)
  236. {
  237. /* Adjust pointer into the memory block. */
  238. cp = (char *) newp + (cp - (char *) dataset);
  239. key_copy = (char *) newp + (key_copy - (char *) dataset);
  240. dataset = memcpy (newp, dataset, total + n);
  241. alloca_used = false;
  242. }
  243. /* Mark the old record as obsolete. */
  244. dh->usable = false;
  245. }
  246. }
  247. else
  248. {
  249. /* We write the dataset before inserting it to the database
  250. since while inserting this thread might block and so would
  251. unnecessarily let the receiver wait. */
  252. assert (fd != -1);
  253. if (writeall (fd, &dataset->resp, dataset->head.recsize)
  254. != dataset->head.recsize)
  255. all_written = false;
  256. }
  257. /* Add the record to the database. But only if it has not been
  258. stored on the stack. */
  259. if (! alloca_used)
  260. {
  261. /* If necessary, we also propagate the data to disk. */
  262. if (db->persistent)
  263. {
  264. // XXX async OK?
  265. uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
  266. msync ((void *) pval,
  267. ((uintptr_t) dataset & pagesize_m1) + total + n,
  268. MS_ASYNC);
  269. }
  270. /* NB: in the following code we always must add the entry
  271. marked with FIRST first. Otherwise we end up with
  272. dangling "pointers" in case a latter hash entry cannot be
  273. added. */
  274. bool first = true;
  275. /* If the request was by UID, add that entry first. */
  276. if (req->type == GETPWBYUID)
  277. {
  278. if (cache_add (GETPWBYUID, cp, key_offset, &dataset->head, true,
  279. db, owner, he == NULL) < 0)
  280. goto out;
  281. first = false;
  282. }
  283. /* If the key is different from the name add a separate entry. */
  284. else if (strcmp (key_copy, dataset->strdata) != 0)
  285. {
  286. if (cache_add (GETPWBYNAME, key_copy, key_len + 1,
  287. &dataset->head, true, db, owner, he == NULL) < 0)
  288. goto out;
  289. first = false;
  290. }
  291. /* We have to add the value for both, byname and byuid. */
  292. if ((req->type == GETPWBYNAME || db->propagate)
  293. && __builtin_expect (cache_add (GETPWBYNAME, dataset->strdata,
  294. pw_name_len, &dataset->head,
  295. first, db, owner, he == NULL)
  296. == 0, 1))
  297. {
  298. if (req->type == GETPWBYNAME && db->propagate)
  299. (void) cache_add (GETPWBYUID, cp, key_offset, &dataset->head,
  300. false, db, owner, false);
  301. }
  302. out:
  303. pthread_rwlock_unlock (&db->lock);
  304. if (he == NULL && db->propagate)
  305. pthread_mutex_unlock (&db->prune_run_lock);
  306. }
  307. }
  308. if (__builtin_expect (!all_written, 0) && debug_level > 0)
  309. {
  310. char buf[256];
  311. dbg_log (_("short write in %s: %s"), __FUNCTION__,
  312. strerror_r (errno, buf, sizeof (buf)));
  313. }
  314. return timeout;
  315. }
  316. union keytype
  317. {
  318. void *v;
  319. uid_t u;
  320. };
  321. static int
  322. lookup (int type, union keytype key, struct passwd *resultbufp, char *buffer,
  323. size_t buflen, struct passwd **pwd)
  324. {
  325. if (type == GETPWBYNAME)
  326. return __getpwnam_r (key.v, resultbufp, buffer, buflen, pwd);
  327. else
  328. return __getpwuid_r (key.u, resultbufp, buffer, buflen, pwd);
  329. }
  330. static time_t
  331. addpwbyX (struct database_dyn *db, int fd, request_header *req,
  332. union keytype key, const char *keystr, uid_t c_uid,
  333. struct hashentry *he, struct datahead *dh)
  334. {
  335. /* Search for the entry matching the key. Please note that we don't
  336. look again in the table whether the dataset is now available. We
  337. simply insert it. It does not matter if it is in there twice. The
  338. pruning function only will look at the timestamp. */
  339. struct passwd resultbuf;
  340. struct passwd *pwd;
  341. int errval = 0;
  342. struct scratch_buffer tmpbuf;
  343. scratch_buffer_init (&tmpbuf);
  344. if (__glibc_unlikely (debug_level > 0))
  345. {
  346. if (he == NULL)
  347. dbg_log (_("Haven't found \"%s\" in user database cache!"), keystr);
  348. else
  349. dbg_log (_("Reloading \"%s\" in user database cache!"), keystr);
  350. }
  351. while (lookup (req->type, key, &resultbuf,
  352. tmpbuf.data, tmpbuf.length, &pwd) != 0
  353. && (errval = errno) == ERANGE)
  354. if (!scratch_buffer_grow (&tmpbuf))
  355. {
  356. /* We ran out of memory. We cannot do anything but sending a
  357. negative response. In reality this should never
  358. happen. */
  359. pwd = NULL;
  360. /* We set the error to indicate this is (possibly) a temporary
  361. error and that it does not mean the entry is not available
  362. at all. */
  363. errval = EAGAIN;
  364. break;
  365. }
  366. /* Add the entry to the cache. */
  367. time_t timeout = cache_addpw (db, fd, req, keystr, pwd, c_uid, he, dh,
  368. errval);
  369. scratch_buffer_free (&tmpbuf);
  370. return timeout;
  371. }
  372. void
  373. addpwbyname (struct database_dyn *db, int fd, request_header *req,
  374. void *key, uid_t c_uid)
  375. {
  376. union keytype u = { .v = key };
  377. addpwbyX (db, fd, req, u, key, c_uid, NULL, NULL);
  378. }
  379. time_t
  380. readdpwbyname (struct database_dyn *db, struct hashentry *he,
  381. struct datahead *dh)
  382. {
  383. request_header req =
  384. {
  385. .type = GETPWBYNAME,
  386. .key_len = he->len
  387. };
  388. union keytype u = { .v = db->data + he->key };
  389. return addpwbyX (db, -1, &req, u, db->data + he->key, he->owner, he, dh);
  390. }
  391. void
  392. addpwbyuid (struct database_dyn *db, int fd, request_header *req,
  393. void *key, uid_t c_uid)
  394. {
  395. char *ep;
  396. uid_t uid = strtoul ((char *) key, &ep, 10);
  397. if (*(char *) key == '\0' || *ep != '\0') /* invalid numeric uid */
  398. {
  399. if (debug_level > 0)
  400. dbg_log (_("Invalid numeric uid \"%s\"!"), (char *) key);
  401. errno = EINVAL;
  402. return;
  403. }
  404. union keytype u = { .u = uid };
  405. addpwbyX (db, fd, req, u, key, c_uid, NULL, NULL);
  406. }
  407. time_t
  408. readdpwbyuid (struct database_dyn *db, struct hashentry *he,
  409. struct datahead *dh)
  410. {
  411. char *ep;
  412. uid_t uid = strtoul (db->data + he->key, &ep, 10);
  413. /* Since the key has been added before it must be OK. */
  414. assert (*(db->data + he->key) != '\0' && *ep == '\0');
  415. request_header req =
  416. {
  417. .type = GETPWBYUID,
  418. .key_len = he->len
  419. };
  420. union keytype u = { .u = uid };
  421. return addpwbyX (db, -1, &req, u, db->data + he->key, he->owner, he, dh);
  422. }