File: viewdb.c

package info (click to toggle)
integrit 3.02.00-11
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,480 kB
  • ctags: 559
  • sloc: ansic: 4,204; makefile: 385; sh: 306; perl: 224
file content (131 lines) | stat: -rw-r--r-- 3,350 bytes parent folder | download
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
/* viewdb.c
integrit - file integrity verification system
Copyright (C) 2000, 2001, 2002 Ed L. Cashin

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; either 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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#include	<config.h>
#include	<stdio.h>
#include	<string.h>
#include	<stdlib.h>
#include	<unistd.h>
#include	<fcntl.h>
#include	<limits.h>
#include	<errno.h>
#include	<sys/stat.h>
/* support platforms that don't yet conform to C99 */
#if	HAVE_STDINT_H
#include	<stdint.h>
#elif	HAVE_INTTYPES_H
#include	<inttypes.h>
#else
#error No stdint.h or inttypes.h found.
#endif
#include	"gnupg/sha1.h"
#include	"elcerror_p.h"
#include	"elcerror.h"
#include	"cdb.h"
#include	"cdb_seq.h"
#include	"dbinfo.h"
#include	"show.h"
#ifdef		ELC_FIND_LEAKS
#include	"leakfind.h"
#endif

#undef	PROGNAME
#define	PROGNAME "i-viewdb"

typedef struct options {
  char		*targetname;
  unsigned	checksums: 1;
} options;  

void viewdb(options *opts)
{
    cdb_seq	seq;
    int		err;
    unsigned	ksiz	 = 1024; /* beginning key space allotment */
    char	*key	 = malloc(ksiz);
    dbinfo	val;
    size_t	klen, vlen;

    if (! key)
      DIE("malloc key");

    if ( (seq.fd = open(opts->targetname, O_RDONLY | O_NDELAY)) == -1)
      die(__FUNCTION__, "Error: opening integrit database (%s): %s",
	  opts->targetname, strerror(errno));
      
    if (cdb_seq_start(&seq) == -1)
      DIE("cdb_seq_start");
    while (! cdb_seq_eod(&seq)) { /* while not end of data */
      if ( (err = cdb_seq_sizes(&seq, &klen, &vlen)) == -1)
	DIE("cdb_seq_sizes");
      else if (err == 1)	/* no more in db sequence */
	break;
      while (klen > ksiz) {
	if (ksiz > (UINT_MAX / 2))
	  DIE("key size too big");
	ksiz	 *= 2;
	if (! (key = realloc(key, ksiz)) )
	  die(__FUNCTION__,
	      "Error: realloc key (length %d) with size (%d): %s",
	      klen, ksiz, strerror(errno));
      }
      if (vlen > sizeof(val))
	die(__FUNCTION__, "Error: bad entry (too big value) in DB (%s)",
	    opts->targetname);
      if (cdb_seq_get(&seq, key, klen, &val, vlen) == -1)
	DIE("cdb_seq_getkey");
      show_entry(opts->checksums, key, klen, &val, vlen);
    }

    free(key);
}

void usage(void)
{
    fputs("Usage:\n    " PROGNAME " [-s] {dbfile}\n"
	  "Options:\n    -s    don't show checksums\n",
	  stderr);
}

int main(int argc, char *argv[])
{
    options	opts	 = { NULL, 1 };
    char	*arg	 = argv[1];

    if (argc > 2)
      /* handle the checksums option */
      if (! strcmp(arg, "-s")) {
	opts.checksums	 = 0;
	arg	 = argv[2];
      }
    if (argc > 1)
      opts.targetname	 = arg;
    
    if (opts.targetname) {
      viewdb(&opts);
    } else {
      usage();
      exit(EXIT_FAILURE);
    }

#ifdef		ELC_FIND_LEAKS
    GC_gcollect();		/* find the leaks before exiting */
#endif
    return 0;
}