about summary refs log tree commit diff
path: root/nscd/cachedumper.c
blob: 9b47d5e670b51b7e533a9c1f861b3b47e21e9ffb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* Copyright (c) 2020 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation; version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <https://www.gnu.org/licenses/>.  */

/* cachedumper - dump a human-readable representation of a cache file.  */

#include <ctype.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <libintl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include <getopt.h>
#include <sys/param.h>

#include "nscd.h"
#include "dbg_log.h"

static void *the_cache;

#define NO_REF ((ref_t) -1)

/* Given a chunk of raw data CP of length LEN, print it in a hopefully
   user-readable format, including colorizing non-readable characters.
   STR prefixes it, if non-NULL.  If LEN is -1, CP is
   NUL-terminated.  */
unsigned char *
data_string (unsigned char *cp, const char *str, int len)
{
  int oops = 0;
  unsigned char *cpe = cp + len;
  printf ("%s", str);
  while (len == -1 || cp < cpe)
    {
      if (isgraph (*cp))
	putchar (*cp);
      else
	printf ("\033[%dm<%02x>\033[0m", *cp % 6 + 31, *cp);
      if (len == -1 && *cp == 0)
	return cp + 1;

      ++cp;
      if (++oops > 1000)
	break;
    }
  return cp;
}

void
nscd_print_cache (const char *name)
{
  struct stat st;
  int fd;
  int i;

  if (stat (name, &st) < 0)
    {
      perror (name);
      exit (1);
    }

  fd = open (name, O_RDONLY);

  the_cache = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);

  struct database_pers_head *dps = (struct database_pers_head *) the_cache;

  /* Shortcut for "print the cache offset (address) of X in the
     cache".  */
#define A(x) (int) ((char *) &(x) - (char *) the_cache)

  /* Common code for "print field DPS->F, it's offset, and contents".  */
#define DPS(f) printf("%08x: %24s : %10d %08x\n", A (dps->f), #f, (int) dps->f, (int) dps->f);

  if (debug_level > 0)
    {
      DPS (version);
      DPS (header_size);
      DPS (gc_cycle);
      DPS (nscd_certainly_running);
      DPS (timestamp);
      DPS (module);
      DPS (data_size);
      DPS (first_free);
      DPS (nentries);
      DPS (maxnentries);
      DPS (maxnsearched);
      DPS (poshit);
      DPS (neghit);
      DPS (posmiss);
      DPS (negmiss);
      DPS (rdlockdelayed);
      DPS (wrlockdelayed);
      DPS (addfailed);
      printf ("\n");
    }


  char *data = (char *) &dps->array[roundup (dps->module,
					     ALIGN / sizeof (ref_t))];

  /* Loop through each entry in the hash table, which is of size
     dps->module.  Raw data is stored after the hash table in the
     cache file.  */
  for (i = 0; i < dps->module; i++)
    {
      ref_t r = dps->array[i];
      if (r == NO_REF)
	continue;

      if (debug_level > 2)
	printf ("hash[%4d] = 0x%x\n", i, r);

      while (r != NO_REF)
	{
	  struct hashentry *here = (struct hashentry *) (data + r);

	  unsigned char *key = (unsigned char *) data + here->key;

	  printf ("\n%08x: type %s key %p \"", A (*here),
		  serv2str[here->type], key);

	  data_string (key, "", here->len);

	  struct datahead *dh = (struct datahead *) (data + here->packet);
	  printf ("\" (len:%ld)  Data %08lx\n", (long) here->len,
		  (long unsigned int) ((char *) dh - (char *) the_cache));

	  if (debug_level > 0)
	    {
/* Common code for printing fields in struct DATAHEAD DH.  */
#define DH(f) printf ("%08x; %24s : %10d %08x\n", A (dh->f), #f, (int) dh->f, (int) dh->f);
	      DH (allocsize);
	      DH (recsize);
	      DH (timeout);
	      DH (notfound);
	      DH (nreloads);
	      DH (usable);
	      DH (unused);
	      DH (ttl);
	    }

	  unsigned char *cp = (unsigned char *) (&dh->data[0]);
	  unsigned char *cpe =
	    (unsigned char *) (&dh->data[0]) + dh->allocsize;


	  int i;
	  uint32_t *grplens;

	  if (debug_level > 1)
	    {
	      data_string (cp, _(" - all data: "), cpe - cp);
	      printf ("\n");
	    }

	  /* These two are common to all responses.  */
	  printf ("V%d F%d",
		  dh->data[0].pwdata.version, dh->data[0].pwdata.found);

/* Shortcut for the common case where we iterate through
   fixed-length strings stored in the data portion of the
   cache.  CP is updated to point to the next string.  */
#define DSTR(str, l) cp = data_string (cp, str, l)

	  switch (here->type)
	    {
	    case GETPWBYNAME:
	    case GETPWBYUID:
	      {
		pw_response_header *pw = &(dh->data[0].pwdata);
		cp += sizeof (*pw);
		DSTR (" name ", pw->pw_name_len);
		DSTR (" passwd ", pw->pw_passwd_len);
		printf (" uid %d gid %d", pw->pw_uid, pw->pw_gid);
		DSTR (" gecos ", pw->pw_gecos_len);
		DSTR (" dir ", pw->pw_dir_len);
		DSTR (" shell ", pw->pw_shell_len);
		DSTR (" byuid ", -1);
		DSTR (" key ", -1);
		printf ("\n");
	      }
	      break;

	    case GETGRBYNAME:
	    case GETGRBYGID:
	      {
		gr_response_header *gr = &(dh->data[0].grdata);
		cp += sizeof (*gr);
		grplens = (uint32_t *) cp;
		cp += gr->gr_mem_cnt * sizeof (uint32_t);
		DSTR (" name ", gr->gr_name_len);
		DSTR (" passwd ", gr->gr_passwd_len);
		printf (" gid %d members %d [ ", (int) gr->gr_gid,
			(int) gr->gr_mem_cnt);
		for (i = 0; i < gr->gr_mem_cnt; i++)
		  DSTR (" ", grplens[i]);
		DSTR (" ] bygid ", -1);
		DSTR (" key ", -1);
		printf ("\n");
	      }
	      break;

	    case GETHOSTBYADDR:
	    case GETHOSTBYADDRv6:
	    case GETHOSTBYNAME:
	    case GETHOSTBYNAMEv6:
	      {
		hst_response_header *hst = &(dh->data[0].hstdata);
		printf (" addrtype %d error %d", hst->h_addrtype, hst->error);
		cp += sizeof (*hst);
		DSTR (" name ", hst->h_name_len);
		uint32_t *aliases_len = (uint32_t *) cp;
		cp += hst->h_aliases_cnt * sizeof (uint32_t);
		uint32_t *addrs = (uint32_t *) cp;
		cp += hst->h_length * hst->h_addr_list_cnt;

		if (hst->h_aliases_cnt)
		  {
		    printf (" aliases [");
		    for (i = 0; i < hst->h_aliases_cnt; i++)
		      DSTR (" ", aliases_len[i]);
		    printf (" ]");
		  }
		if (hst->h_addr_list_cnt)
		  {
		    char buf[INET6_ADDRSTRLEN];
		    printf (" addresses [");
		    for (i = 0; i < hst->h_addr_list_cnt; i++)
		      {
			inet_ntop (hst->h_addrtype, addrs, buf, sizeof (buf));
			printf (" %s", buf);
			addrs += hst->h_length;
		      }
		    printf (" ]");
		  }

		printf ("\n");
	      }
	      break;

	    case GETAI:
	      {
		ai_response_header *ai = &(dh->data[0].aidata);
		printf (" naddrs %ld addrslen %ld canonlen %ld error %d [",
			(long) ai->naddrs, (long) ai->addrslen,
			(long) ai->canonlen, ai->error);
		cp += sizeof (*ai);
		unsigned char *addrs = cp;
		unsigned char *families = cp + ai->addrslen;
		cp = families + ai->naddrs;
		char buf[INET6_ADDRSTRLEN];

		for (i = 0; i < ai->naddrs; i++)
		  {
		    switch (*families)
		      {
		      case AF_INET:
			inet_ntop (*families, addrs, buf, sizeof (buf));
			printf (" %s", buf);
			addrs += 4;
			break;
		      case AF_INET6:
			inet_ntop (*families, addrs, buf, sizeof (buf));
			printf (" %s", buf);
			addrs += 16;
			break;
		      }
		    families++;
		  }
		DSTR (" ] canon ", ai->canonlen);
		DSTR (" key ", -1);
		printf ("\n");
	      }
	      break;

	    case INITGROUPS:
	      {
		initgr_response_header *ig = &(dh->data[0].initgrdata);
		printf (" nresults %d groups [", (int) ig->ngrps);
		cp += sizeof (*ig);
		grplens = (uint32_t *) cp;
		cp += ig->ngrps * sizeof (uint32_t);
		for (i = 0; i < ig->ngrps; i++)
		  printf (" %d", grplens[i]);
		DSTR (" ] key ", -1);
		printf ("\n");
	      }
	      break;

	    case GETSERVBYNAME:
	    case GETSERVBYPORT:
	      {
		serv_response_header *serv = &(dh->data[0].servdata);
		printf (" alias_cnt %ld port %d (stored as %d)",
			(long) serv->s_aliases_cnt,
			((serv->s_port & 0xff00) >> 8) | ((serv->
							   s_port & 0xff) <<
							  8), serv->s_port);
		cp += sizeof (*serv);
		DSTR (" name ", serv->s_name_len);
		DSTR (" proto ", serv->s_proto_len);
		if (serv->s_aliases_cnt)
		  {
		    uint32_t *alias_len = (uint32_t *) cp;
		    printf (" aliases [");
		    cp += sizeof (uint32_t) * serv->s_aliases_cnt;
		    for (i = 0; i < serv->s_aliases_cnt; i++)
		      DSTR (" ", alias_len[i]);
		    printf (" ]");
		  }
		printf ("\n");
	      }
	      break;

	    case GETNETGRENT:
	      {
		netgroup_response_header *ng = &(dh->data[0].netgroupdata);
		printf (" nresults %d len %d\n",
			(int) ng->nresults, (int) ng->result_len);
		cp += sizeof (*ng);
		for (i = 0; i < ng->nresults; i++)
		  {
		    DSTR (" (", -1);
		    DSTR (",", -1);
		    DSTR (",", -1);
		    printf (")");
		  }
		printf ("\n");
	      }
	      break;

	    case INNETGR:
	      {
		innetgroup_response_header *ing =
		  &(dh->data[0].innetgroupdata);
		printf (" result %d\n", ing->result);
	      }
	      break;

	    default:
	      break;
	    }

	  if (debug_level > 2 && cp && cp < cpe)
	    {
	      printf (_(" - remaining data %p: "), cp);
	      data_string (cp, "", cpe - cp);
	      printf ("\n");
	    }


	  r = here->next;
	}
    }

  munmap (the_cache, st.st_size);

  exit (0);
}