initgrcache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* Cache handling for host lookup.
  2. Copyright (C) 2004-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 <grp.h>
  17. #include <libintl.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #include <sys/mman.h>
  22. #include <scratch_buffer.h>
  23. #include <config.h>
  24. #include "dbg_log.h"
  25. #include "nscd.h"
  26. #include "../nss/nsswitch.h"
  27. /* Type of the lookup function. */
  28. typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
  29. long int *, long int *,
  30. gid_t **, long int, int *);
  31. static const initgr_response_header notfound =
  32. {
  33. .version = NSCD_VERSION,
  34. .found = 0,
  35. .ngrps = 0
  36. };
  37. #include "../nss/initgroups-fallback.c"
  38. static time_t
  39. addinitgroupsX (struct database_dyn *db, int fd, request_header *req,
  40. void *key, uid_t uid, struct hashentry *const he,
  41. struct datahead *dh)
  42. {
  43. /* Search for the entry matching the key. Please note that we don't
  44. look again in the table whether the dataset is now available. We
  45. simply insert it. It does not matter if it is in there twice. The
  46. pruning function only will look at the timestamp. */
  47. /* We allocate all data in one memory block: the iov vector,
  48. the response header and the dataset itself. */
  49. struct dataset
  50. {
  51. struct datahead head;
  52. initgr_response_header resp;
  53. char strdata[0];
  54. } *dataset = NULL;
  55. if (__glibc_unlikely (debug_level > 0))
  56. {
  57. if (he == NULL)
  58. dbg_log (_("Haven't found \"%s\" in group cache!"), (char *) key);
  59. else
  60. dbg_log (_("Reloading \"%s\" in group cache!"), (char *) key);
  61. }
  62. static nss_action_list group_database;
  63. nss_action_list nip;
  64. int no_more;
  65. if (group_database == NULL)
  66. no_more = !__nss_database_get (nss_database_group, &group_database);
  67. else
  68. no_more = 0;
  69. nip = group_database;
  70. /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
  71. limit can be raised in the kernel configuration without having to
  72. recompile libc. */
  73. long int limit = __sysconf (_SC_NGROUPS_MAX);
  74. long int size;
  75. if (limit > 0)
  76. /* We limit the size of the initially allocated array. */
  77. size = MIN (limit, 64);
  78. else
  79. /* No fixed limit on groups. Pick a starting buffer size. */
  80. size = 16;
  81. long int start = 0;
  82. bool all_tryagain = true;
  83. bool any_success = false;
  84. /* This is temporary memory, we need not (and must not) call
  85. mempool_alloc. */
  86. // XXX This really should use alloca. need to change the backends.
  87. gid_t *groups = (gid_t *) malloc (size * sizeof (gid_t));
  88. if (__glibc_unlikely (groups == NULL))
  89. /* No more memory. */
  90. goto out;
  91. /* Nothing added yet. */
  92. while (! no_more)
  93. {
  94. long int prev_start = start;
  95. enum nss_status status;
  96. initgroups_dyn_function fct;
  97. fct = __nss_lookup_function (nip, "initgroups_dyn");
  98. if (fct == NULL)
  99. {
  100. status = compat_call (nip, key, -1, &start, &size, &groups,
  101. limit, &errno);
  102. if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
  103. break;
  104. }
  105. else
  106. status = DL_CALL_FCT (fct, (key, -1, &start, &size, &groups,
  107. limit, &errno));
  108. /* Remove duplicates. */
  109. long int cnt = prev_start;
  110. while (cnt < start)
  111. {
  112. long int inner;
  113. for (inner = 0; inner < prev_start; ++inner)
  114. if (groups[inner] == groups[cnt])
  115. break;
  116. if (inner < prev_start)
  117. groups[cnt] = groups[--start];
  118. else
  119. ++cnt;
  120. }
  121. if (status != NSS_STATUS_TRYAGAIN)
  122. all_tryagain = false;
  123. /* This is really only for debugging. */
  124. if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
  125. __libc_fatal ("Illegal status in internal_getgrouplist.\n");
  126. any_success |= status == NSS_STATUS_SUCCESS;
  127. if (status != NSS_STATUS_SUCCESS
  128. && nss_next_action (nip, status) == NSS_ACTION_RETURN)
  129. break;
  130. if (nip[1].module == NULL)
  131. no_more = -1;
  132. else
  133. ++nip;
  134. }
  135. bool all_written;
  136. ssize_t total;
  137. time_t timeout;
  138. out:
  139. all_written = true;
  140. timeout = MAX_TIMEOUT_VALUE;
  141. if (!any_success)
  142. {
  143. /* Nothing found. Create a negative result record. */
  144. total = sizeof (notfound);
  145. if (he != NULL && all_tryagain)
  146. {
  147. /* If we have an old record available but cannot find one now
  148. because the service is not available we keep the old record
  149. and make sure it does not get removed. */
  150. if (reload_count != UINT_MAX && dh->nreloads == reload_count)
  151. /* Do not reset the value if we never not reload the record. */
  152. dh->nreloads = reload_count - 1;
  153. /* Reload with the same time-to-live value. */
  154. timeout = dh->timeout = time (NULL) + db->postimeout;
  155. }
  156. else
  157. {
  158. /* We have no data. This means we send the standard reply for this
  159. case. */
  160. if (fd != -1
  161. && TEMP_FAILURE_RETRY (send (fd, &notfound, total,
  162. MSG_NOSIGNAL)) != total)
  163. all_written = false;
  164. /* If we have a transient error or cannot permanently store
  165. the result, so be it. */
  166. if (all_tryagain || __builtin_expect (db->negtimeout == 0, 0))
  167. {
  168. /* Mark the old entry as obsolete. */
  169. if (dh != NULL)
  170. dh->usable = false;
  171. }
  172. else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
  173. + req->key_len), 1)) != NULL)
  174. {
  175. timeout = datahead_init_neg (&dataset->head,
  176. (sizeof (struct dataset)
  177. + req->key_len), total,
  178. db->negtimeout);
  179. /* This is the reply. */
  180. memcpy (&dataset->resp, &notfound, total);
  181. /* Copy the key data. */
  182. char *key_copy = memcpy (dataset->strdata, key, req->key_len);
  183. /* If necessary, we also propagate the data to disk. */
  184. if (db->persistent)
  185. {
  186. // XXX async OK?
  187. uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
  188. msync ((void *) pval,
  189. ((uintptr_t) dataset & pagesize_m1)
  190. + sizeof (struct dataset) + req->key_len, MS_ASYNC);
  191. }
  192. (void) cache_add (req->type, key_copy, req->key_len,
  193. &dataset->head, true, db, uid, he == NULL);
  194. pthread_rwlock_unlock (&db->lock);
  195. /* Mark the old entry as obsolete. */
  196. if (dh != NULL)
  197. dh->usable = false;
  198. }
  199. }
  200. }
  201. else
  202. {
  203. total = offsetof (struct dataset, strdata) + start * sizeof (int32_t);
  204. /* If we refill the cache, first assume the reconrd did not
  205. change. Allocate memory on the cache since it is likely
  206. discarded anyway. If it turns out to be necessary to have a
  207. new record we can still allocate real memory. */
  208. bool alloca_used = false;
  209. dataset = NULL;
  210. if (he == NULL)
  211. dataset = (struct dataset *) mempool_alloc (db, total + req->key_len,
  212. 1);
  213. if (dataset == NULL)
  214. {
  215. /* We cannot permanently add the result in the moment. But
  216. we can provide the result as is. Store the data in some
  217. temporary memory. */
  218. dataset = (struct dataset *) alloca (total + req->key_len);
  219. /* We cannot add this record to the permanent database. */
  220. alloca_used = true;
  221. }
  222. timeout = datahead_init_pos (&dataset->head, total + req->key_len,
  223. total - offsetof (struct dataset, resp),
  224. he == NULL ? 0 : dh->nreloads + 1,
  225. db->postimeout);
  226. dataset->resp.version = NSCD_VERSION;
  227. dataset->resp.found = 1;
  228. dataset->resp.ngrps = start;
  229. char *cp = dataset->strdata;
  230. /* Copy the GID values. If the size of the types match this is
  231. very simple. */
  232. if (sizeof (gid_t) == sizeof (int32_t))
  233. cp = mempcpy (cp, groups, start * sizeof (gid_t));
  234. else
  235. {
  236. gid_t *gcp = (gid_t *) cp;
  237. for (int i = 0; i < start; ++i)
  238. *gcp++ = groups[i];
  239. cp = (char *) gcp;
  240. }
  241. /* Finally the user name. */
  242. memcpy (cp, key, req->key_len);
  243. assert (cp == dataset->strdata + total - offsetof (struct dataset,
  244. strdata));
  245. /* Now we can determine whether on refill we have to create a new
  246. record or not. */
  247. if (he != NULL)
  248. {
  249. assert (fd == -1);
  250. if (total + req->key_len == dh->allocsize
  251. && total - offsetof (struct dataset, resp) == dh->recsize
  252. && memcmp (&dataset->resp, dh->data,
  253. dh->allocsize - offsetof (struct dataset, resp)) == 0)
  254. {
  255. /* The data has not changed. We will just bump the
  256. timeout value. Note that the new record has been
  257. allocated on the stack and need not be freed. */
  258. dh->timeout = dataset->head.timeout;
  259. ++dh->nreloads;
  260. }
  261. else
  262. {
  263. /* We have to create a new record. Just allocate
  264. appropriate memory and copy it. */
  265. struct dataset *newp
  266. = (struct dataset *) mempool_alloc (db, total + req->key_len,
  267. 1);
  268. if (newp != NULL)
  269. {
  270. /* Adjust pointer into the memory block. */
  271. cp = (char *) newp + (cp - (char *) dataset);
  272. dataset = memcpy (newp, dataset, total + req->key_len);
  273. alloca_used = false;
  274. }
  275. /* Mark the old record as obsolete. */
  276. dh->usable = false;
  277. }
  278. }
  279. else
  280. {
  281. /* We write the dataset before inserting it to the database
  282. since while inserting this thread might block and so would
  283. unnecessarily let the receiver wait. */
  284. assert (fd != -1);
  285. if (writeall (fd, &dataset->resp, dataset->head.recsize)
  286. != dataset->head.recsize)
  287. all_written = false;
  288. }
  289. /* Add the record to the database. But only if it has not been
  290. stored on the stack. */
  291. if (! alloca_used)
  292. {
  293. /* If necessary, we also propagate the data to disk. */
  294. if (db->persistent)
  295. {
  296. // XXX async OK?
  297. uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
  298. msync ((void *) pval,
  299. ((uintptr_t) dataset & pagesize_m1) + total
  300. + req->key_len, MS_ASYNC);
  301. }
  302. (void) cache_add (INITGROUPS, cp, req->key_len, &dataset->head, true,
  303. db, uid, he == NULL);
  304. pthread_rwlock_unlock (&db->lock);
  305. }
  306. }
  307. free (groups);
  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. void
  317. addinitgroups (struct database_dyn *db, int fd, request_header *req, void *key,
  318. uid_t uid)
  319. {
  320. addinitgroupsX (db, fd, req, key, uid, NULL, NULL);
  321. }
  322. time_t
  323. readdinitgroups (struct database_dyn *db, struct hashentry *he,
  324. struct datahead *dh)
  325. {
  326. request_header req =
  327. {
  328. .type = INITGROUPS,
  329. .key_len = he->len
  330. };
  331. return addinitgroupsX (db, -1, &req, db->data + he->key, he->owner, he, dh);
  332. }