cachedumper.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* Copyright (c) 2020-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <https://www.gnu.org/licenses/>. */
  13. /* cachedumper - dump a human-readable representation of a cache file. */
  14. #include <ctype.h>
  15. #include <stdio.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <libintl.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <sys/mman.h>
  22. #include <arpa/inet.h>
  23. #include <getopt.h>
  24. #include <sys/param.h>
  25. #include "nscd.h"
  26. #include "dbg_log.h"
  27. static void *the_cache;
  28. #define NO_REF ((ref_t) -1)
  29. /* Given a chunk of raw data CP of length LEN, print it in a hopefully
  30. user-readable format, including colorizing non-readable characters.
  31. STR prefixes it, if non-NULL. If LEN is -1, CP is
  32. NUL-terminated. */
  33. unsigned char *
  34. data_string (unsigned char *cp, const char *str, int len)
  35. {
  36. int oops = 0;
  37. unsigned char *cpe = cp + len;
  38. printf ("%s", str);
  39. while (len == -1 || cp < cpe)
  40. {
  41. if (isgraph (*cp))
  42. putchar (*cp);
  43. else
  44. printf ("\033[%dm<%02x>\033[0m", *cp % 6 + 31, *cp);
  45. if (len == -1 && *cp == 0)
  46. return cp + 1;
  47. ++cp;
  48. if (++oops > 1000)
  49. break;
  50. }
  51. return cp;
  52. }
  53. void
  54. nscd_print_cache (const char *name)
  55. {
  56. struct stat st;
  57. int fd;
  58. int i;
  59. if (stat (name, &st) < 0)
  60. {
  61. perror (name);
  62. exit (1);
  63. }
  64. fd = open (name, O_RDONLY);
  65. the_cache = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  66. struct database_pers_head *dps = (struct database_pers_head *) the_cache;
  67. /* Shortcut for "print the cache offset (address) of X in the
  68. cache". */
  69. #define A(x) (int) ((char *) &(x) - (char *) the_cache)
  70. /* Common code for "print field DPS->F, it's offset, and contents". */
  71. #define DPS(f) printf("%08x: %24s : %10d %08x\n", A (dps->f), #f, (int) dps->f, (int) dps->f);
  72. if (debug_level > 0)
  73. {
  74. DPS (version);
  75. DPS (header_size);
  76. DPS (gc_cycle);
  77. DPS (nscd_certainly_running);
  78. DPS (timestamp);
  79. DPS (module);
  80. DPS (data_size);
  81. DPS (first_free);
  82. DPS (nentries);
  83. DPS (maxnentries);
  84. DPS (maxnsearched);
  85. DPS (poshit);
  86. DPS (neghit);
  87. DPS (posmiss);
  88. DPS (negmiss);
  89. DPS (rdlockdelayed);
  90. DPS (wrlockdelayed);
  91. DPS (addfailed);
  92. printf ("\n");
  93. }
  94. char *data = (char *) &dps->array[roundup (dps->module,
  95. ALIGN / sizeof (ref_t))];
  96. /* Loop through each entry in the hash table, which is of size
  97. dps->module. Raw data is stored after the hash table in the
  98. cache file. */
  99. for (i = 0; i < dps->module; i++)
  100. {
  101. ref_t r = dps->array[i];
  102. if (r == NO_REF)
  103. continue;
  104. if (debug_level > 2)
  105. printf ("hash[%4d] = 0x%x\n", i, r);
  106. while (r != NO_REF)
  107. {
  108. struct hashentry *here = (struct hashentry *) (data + r);
  109. unsigned char *key = (unsigned char *) data + here->key;
  110. printf ("\n%08x: type %s key %p \"", A (*here),
  111. serv2str[here->type], key);
  112. data_string (key, "", here->len);
  113. struct datahead *dh = (struct datahead *) (data + here->packet);
  114. printf ("\" (len:%ld) Data %08lx\n", (long) here->len,
  115. (long unsigned int) ((char *) dh - (char *) the_cache));
  116. if (debug_level > 0)
  117. {
  118. /* Common code for printing fields in struct DATAHEAD DH. */
  119. #define DH(f) printf ("%08x; %24s : %10d %08x\n", A (dh->f), #f, (int) dh->f, (int) dh->f);
  120. DH (allocsize);
  121. DH (recsize);
  122. DH (timeout);
  123. DH (notfound);
  124. DH (nreloads);
  125. DH (usable);
  126. DH (unused);
  127. DH (ttl);
  128. }
  129. unsigned char *cp = (unsigned char *) (&dh->data[0]);
  130. unsigned char *cpe =
  131. (unsigned char *) (&dh->data[0]) + dh->allocsize;
  132. int i;
  133. uint32_t *grplens;
  134. if (debug_level > 1)
  135. {
  136. data_string (cp, _(" - all data: "), cpe - cp);
  137. printf ("\n");
  138. }
  139. /* These two are common to all responses. */
  140. printf ("V%d F%d",
  141. dh->data[0].pwdata.version, dh->data[0].pwdata.found);
  142. /* Shortcut for the common case where we iterate through
  143. fixed-length strings stored in the data portion of the
  144. cache. CP is updated to point to the next string. */
  145. #define DSTR(str, l) cp = data_string (cp, str, l)
  146. switch (here->type)
  147. {
  148. case GETPWBYNAME:
  149. case GETPWBYUID:
  150. {
  151. pw_response_header *pw = &(dh->data[0].pwdata);
  152. cp += sizeof (*pw);
  153. DSTR (" name ", pw->pw_name_len);
  154. DSTR (" passwd ", pw->pw_passwd_len);
  155. printf (" uid %d gid %d", pw->pw_uid, pw->pw_gid);
  156. DSTR (" gecos ", pw->pw_gecos_len);
  157. DSTR (" dir ", pw->pw_dir_len);
  158. DSTR (" shell ", pw->pw_shell_len);
  159. DSTR (" byuid ", -1);
  160. DSTR (" key ", -1);
  161. printf ("\n");
  162. }
  163. break;
  164. case GETGRBYNAME:
  165. case GETGRBYGID:
  166. {
  167. gr_response_header *gr = &(dh->data[0].grdata);
  168. cp += sizeof (*gr);
  169. grplens = (uint32_t *) cp;
  170. cp += gr->gr_mem_cnt * sizeof (uint32_t);
  171. DSTR (" name ", gr->gr_name_len);
  172. DSTR (" passwd ", gr->gr_passwd_len);
  173. printf (" gid %d members %d [ ", (int) gr->gr_gid,
  174. (int) gr->gr_mem_cnt);
  175. for (i = 0; i < gr->gr_mem_cnt; i++)
  176. DSTR (" ", grplens[i]);
  177. DSTR (" ] bygid ", -1);
  178. DSTR (" key ", -1);
  179. printf ("\n");
  180. }
  181. break;
  182. case GETHOSTBYADDR:
  183. case GETHOSTBYADDRv6:
  184. case GETHOSTBYNAME:
  185. case GETHOSTBYNAMEv6:
  186. {
  187. hst_response_header *hst = &(dh->data[0].hstdata);
  188. printf (" addrtype %d error %d", hst->h_addrtype, hst->error);
  189. cp += sizeof (*hst);
  190. DSTR (" name ", hst->h_name_len);
  191. uint32_t *aliases_len = (uint32_t *) cp;
  192. cp += hst->h_aliases_cnt * sizeof (uint32_t);
  193. uint32_t *addrs = (uint32_t *) cp;
  194. cp += hst->h_length * hst->h_addr_list_cnt;
  195. if (hst->h_aliases_cnt)
  196. {
  197. printf (" aliases [");
  198. for (i = 0; i < hst->h_aliases_cnt; i++)
  199. DSTR (" ", aliases_len[i]);
  200. printf (" ]");
  201. }
  202. if (hst->h_addr_list_cnt)
  203. {
  204. char buf[INET6_ADDRSTRLEN];
  205. printf (" addresses [");
  206. for (i = 0; i < hst->h_addr_list_cnt; i++)
  207. {
  208. inet_ntop (hst->h_addrtype, addrs, buf, sizeof (buf));
  209. printf (" %s", buf);
  210. addrs += hst->h_length;
  211. }
  212. printf (" ]");
  213. }
  214. printf ("\n");
  215. }
  216. break;
  217. case GETAI:
  218. {
  219. ai_response_header *ai = &(dh->data[0].aidata);
  220. printf (" naddrs %ld addrslen %ld canonlen %ld error %d [",
  221. (long) ai->naddrs, (long) ai->addrslen,
  222. (long) ai->canonlen, ai->error);
  223. cp += sizeof (*ai);
  224. unsigned char *addrs = cp;
  225. unsigned char *families = cp + ai->addrslen;
  226. cp = families + ai->naddrs;
  227. char buf[INET6_ADDRSTRLEN];
  228. for (i = 0; i < ai->naddrs; i++)
  229. {
  230. switch (*families)
  231. {
  232. case AF_INET:
  233. inet_ntop (*families, addrs, buf, sizeof (buf));
  234. printf (" %s", buf);
  235. addrs += 4;
  236. break;
  237. case AF_INET6:
  238. inet_ntop (*families, addrs, buf, sizeof (buf));
  239. printf (" %s", buf);
  240. addrs += 16;
  241. break;
  242. }
  243. families++;
  244. }
  245. DSTR (" ] canon ", ai->canonlen);
  246. DSTR (" key ", -1);
  247. printf ("\n");
  248. }
  249. break;
  250. case INITGROUPS:
  251. {
  252. initgr_response_header *ig = &(dh->data[0].initgrdata);
  253. printf (" nresults %d groups [", (int) ig->ngrps);
  254. cp += sizeof (*ig);
  255. grplens = (uint32_t *) cp;
  256. cp += ig->ngrps * sizeof (uint32_t);
  257. for (i = 0; i < ig->ngrps; i++)
  258. printf (" %d", grplens[i]);
  259. DSTR (" ] key ", -1);
  260. printf ("\n");
  261. }
  262. break;
  263. case GETSERVBYNAME:
  264. case GETSERVBYPORT:
  265. {
  266. serv_response_header *serv = &(dh->data[0].servdata);
  267. printf (" alias_cnt %ld port %d (stored as %d)",
  268. (long) serv->s_aliases_cnt,
  269. ((serv->s_port & 0xff00) >> 8) | ((serv->
  270. s_port & 0xff) <<
  271. 8), serv->s_port);
  272. cp += sizeof (*serv);
  273. DSTR (" name ", serv->s_name_len);
  274. DSTR (" proto ", serv->s_proto_len);
  275. if (serv->s_aliases_cnt)
  276. {
  277. uint32_t *alias_len = (uint32_t *) cp;
  278. printf (" aliases [");
  279. cp += sizeof (uint32_t) * serv->s_aliases_cnt;
  280. for (i = 0; i < serv->s_aliases_cnt; i++)
  281. DSTR (" ", alias_len[i]);
  282. printf (" ]");
  283. }
  284. printf ("\n");
  285. }
  286. break;
  287. case GETNETGRENT:
  288. {
  289. netgroup_response_header *ng = &(dh->data[0].netgroupdata);
  290. printf (" nresults %d len %d\n",
  291. (int) ng->nresults, (int) ng->result_len);
  292. cp += sizeof (*ng);
  293. for (i = 0; i < ng->nresults; i++)
  294. {
  295. DSTR (" (", -1);
  296. DSTR (",", -1);
  297. DSTR (",", -1);
  298. printf (")");
  299. }
  300. printf ("\n");
  301. }
  302. break;
  303. case INNETGR:
  304. {
  305. innetgroup_response_header *ing =
  306. &(dh->data[0].innetgroupdata);
  307. printf (" result %d\n", ing->result);
  308. }
  309. break;
  310. default:
  311. break;
  312. }
  313. if (debug_level > 2 && cp && cp < cpe)
  314. {
  315. printf (_(" - remaining data %p: "), cp);
  316. data_string (cp, "", cpe - cp);
  317. printf ("\n");
  318. }
  319. r = here->next;
  320. }
  321. }
  322. munmap (the_cache, st.st_size);
  323. exit (0);
  324. }