File: main.c

package info (click to toggle)
integrit 2.03.02-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,328 kB
  • ctags: 555
  • sloc: ansic: 4,190; sh: 295; makefile: 257; perl: 224
file content (224 lines) | stat: -rw-r--r-- 6,164 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
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
/*
integrit - file integrity verification system
Copyright (C) 2000, 2001 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.

*/
/* $Header: /cvsroot/integrit/integrit/main.c,v 1.20 2001/07/31 02:40:33 ecashin Exp $ */
#include	<config.h>
#include	<stdio.h>
#include	<unistd.h>
#include	<string.h>
#include	<stdlib.h>
#include	<errno.h>
#include	<fcntl.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/md5.h"
#include	"cdb.h"
#include	"cdb_make.h"
#include	"hashtbl/hashtbl.h"
#include	"elcwft.h"
#include	"checkset.h"
#include	"options.h"
#include	"rules.h"
#include	"eachfile.h"
#include	"missing.h"
#include	"utilities.h"
#include	"xml.h"
#include	"elcerror.h"
#include	"elcerror_p.h"
#include	"utilities_p.h"
#include	"options_p.h"
#include	"elcwft_p.h"
#include	"eachfile_p.h"
#include	"missing_p.h"
#include	"xml_p.h"
#ifdef		ELC_FIND_LEAKS
#include	"leakfind.h"
#endif

static void open_known_cdb(options *opts)
{
    int		fd	 = open(opts->knowndbname,
				O_RDONLY | O_NDELAY);

    if (fd == -1)
      die(__FUNCTION__, "Error: opening known-state database (%s): %s",
	  opts->knowndbname, strerror(errno));
    cdb_init(&opts->knowndb, fd);
}

static void open_current_cdb(options *opts)
{
    FILE	*newdb	 = NULL; /* get rid of superfluous compiler warning */
    int		fd;

    fd	 = open(opts->currdbname,
		O_TRUNC | O_CREAT | O_WRONLY | O_NDELAY, 0640);
    if ((fd == -1) || ! (newdb = fdopen(fd, "wb")) )
      DIE("opening current-state database");
    memset(&opts->currdb, 0, sizeof(opts->currdb));
    if (cdb_make_start(&opts->currdb, newdb) == -1)
      DIE("start cdb_make");
}

static void close_current_cdb(options *opts)
{
    if (cdb_make_finish(&opts->currdb) == -1)
      DIE("finishing current-state database");
    (void) fclose(opts->currdb.fout);
}    

static void close_known_cdb(options *opts)
{
    (void) close(opts->knowndb.fd);
    cdb_free(&opts->knowndb);
}

static void get_currdb_checksum(options *opts, char sumbuf[])
{
    MD5_CONTEXT	context;
    char	buf[BUFSIZ];
    char	*fname	 = opts->currdbname;
    long	n;
    int		fd	 = open(fname, O_RDONLY);

    if (fd == -1)
      die(__FUNCTION__, "Error: opening file (%s): %s",
	  fname, strerror(errno));
    md5_init(&context);
    while ( (n = read(fd, buf, BUFSIZ)) ) {
      if (n == -1)
	die(__FUNCTION__, "Error: reading file (%s): %s",
	    fname, strerror(errno));
      md5_write(&context, buf, (unsigned long) n);
    }
    (void) close(fd);
    md5_final(&context);

    /* It would be faster to use context.buf directly, but we only
     * do an md5 once.
     */
    memcpy(sumbuf, md5_read(&context), MD5_DIGEST_LENGTH);
    /* memcpy(sumbuf, context.buf, MD5_DIGEST_LENGTH); */
}

static void show_currdb_checksum_abbrev(options *opts, char *sumbuf, size_t n)
{
    char	*fname	 = opts->currdbname;

    fputs(PROGNAME ": current-state db md5sum -------------- \n"
	  PROGNAME ": ", stdout);
    hexprint(stdout, sumbuf, n);
    fputs("  ", stdout);
    puts(fname);
}

static void show_currdb_checksum_xml(options *opts, const char *checksum_type,
				     char *sumbuf, size_t n)
{
    char	*fname	 = opts->currdbname;

    fprintf(stdout, "<checksum type=\"%s\" file=\"%s\">",
	    checksum_type, fname);
    hexprint(stdout, sumbuf, n);
    XML_END_PRINT(stdout, "checksum");
    putc('\n', stdout);
}

int main(int argc, char *argv[])
{
    options	opts;
    char	newdb_checksum[MD5_DIGEST_LENGTH];
    wft_context_t wftctx	 = { 0, };
    int		ret;

#if	defined(ELC_FIND_LEAKS) && 0 /* disabled */
    GC_find_leak	 = 1;		/* turn on boehm leak detection
					 * in newer versions of gc.
					 * comment this out for old
					 * versions of gc */
#endif

    options_init(&opts);
    options_set(&opts, argc, argv);
    if (opts.output == OUTPUT_XML) {
      xml_declaration(stdout);
      /* xml_dtd(stdout); */
      xml_start_report(stdout, &opts);
    }
    options_announce(stdout, &opts);

    /* todo: use shared file locking with fcntl */
    if (opts.do_check)
      open_known_cdb(&opts);
    if (opts.do_update)
      open_current_cdb(&opts);
    
    if (opts.do_check || opts.do_update) {
      wftctx.rootname	 = opts.root;
      wftctx.callback	 = process_file;
      wftctx.cb_data	 = &opts;
      wftctx.options	 = opts.verbose > 0 ? WFT_VERBOSE : 0;
      if ( (ret = walk_file_tree(&wftctx)) == WFT_ERROR
	   || ret == WFT_OPENDIR_FAIL)
	DIE("walk_file_tree");
    }
    
    if (opts.do_update) {
      close_current_cdb(&opts);
      get_currdb_checksum(&opts, newdb_checksum);
    }

    if (opts.do_check) {
      if (opts.do_update)
	check_for_missing(&opts);	/* only do this after new current cdb
					   is closed (i.e. cdb_make is done) */
      else if (opts.verbose > 0)
	fputs(PROGNAME ": not doing update, so no check for missing files\n",
	      stderr);
      
      close_known_cdb(&opts);
    }
    if (opts.do_update) {
      if (opts.output == OUTPUT_XML)
	show_currdb_checksum_xml(&opts, "MD5",
				 newdb_checksum, sizeof(newdb_checksum));
      else
	show_currdb_checksum_abbrev(&opts,
				    newdb_checksum, sizeof(newdb_checksum));
    }
    
    if (opts.output == OUTPUT_XML) {
      XML_END_PRINT(stdout, "report");
      putc('\n', stdout);
    }

    options_destroy(&opts);
#ifdef		ELC_FIND_LEAKS
    GC_gcollect();		/* find the leaks before exiting */
#endif

    return 0;
}