File: options.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 (445 lines) | stat: -rw-r--r-- 11,835 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
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	<ctype.h>
#include	<errno.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	"cdb.h"
#include	"cdb_make.h"
#include	"hashtbl.h"
#include	"xstrdup.h"
#include	"checkset.h"
#include	"integrit.h"
#include	"rules.h"
#include	"xml.h"
#include	"elcerror.h"
#include	"elcerror_p.h"
#include	"checkset_p.h"
#include	"options_p.h"
#include	"xml_p.h"
#ifdef		ELC_FIND_LEAKS
#include	"leakfind.h"
#endif

/* There is a thread on comp.lang.c.moderated where the gurus explain
 * how the cast on the parameters to ctype routines should be to
 * unsigned char. */
#define	EATSPACE(buf)	do {				\
    while (*(buf)					\
	   && (*(buf) != '\n')				\
	   && (isspace((unsigned char) *(buf))))	\
      ++(buf);						\
} while (0)    

inline static void chomp(char *chp)
{
    /* remove trailing newlines from a one-line string */
    for( ; *chp; ++chp)
      if(*chp == '\n')
	*chp = '\0';
}

char *options_output_str(integrit_t *it)
{
    switch (it->output) {
      case OUTPUT_LINES:
	return "human-readable";
	break;
      case OUTPUT_XML:
	return "xml";
	break;
      default:
	DIE("unknown value for output member in options");
	break;
    };
    return "lompa lompa"; /* not reached */
}

static void options_announce_lines(integrit_t *it, FILE *out)
{
    fprintf(out, PROGNAME ": ---- integrit, version %s -----------------\n",
	    INTEGRIT_VERSION);
    fprintf(out, PROGNAME ": %27s : %s\n", "output", options_output_str(it));
    fprintf(out, PROGNAME ": %27s : %s\n", "conf file", it->conffile);
    fprintf(out, PROGNAME ": %27s : %s\n", "known db", it->knowndbname);
    fprintf(out, PROGNAME ": %27s : %s\n", "current db", it->currdbname);
    fprintf(out, PROGNAME ": %27s : %s\n", "root", it->root);
    fprintf(out, PROGNAME ": %27s : %s\n", "do check",
	    it->do_check ? "yes" : "no");
    fprintf(out, PROGNAME ": %27s : %s\n", "do update",
	    it->do_update ? "yes" : "no");
}

static void options_announce_xml(integrit_t *it, FILE *out)
{
    XML_START_PRINT(out, "options");
    XML_ELEMENT_PRINT(out, "output", options_output_str(it));
    XML_ELEMENT_PRINT(out, "conffile", it->conffile);
    XML_ELEMENT_PRINT(out, "knowndb", it->knowndbname);
    XML_ELEMENT_PRINT(out, "currentdb", it->currdbname);
    XML_ELEMENT_PRINT(out, "root", it->root);
    XML_ELEMENT_PRINT(out, "check", it->do_check ? "yes" : "no");
    XML_ELEMENT_PRINT(out, "update", it->do_update ? "yes" : "no");
    XML_END_PRINT(out, "options");
    putc('\n', out);
}

void options_announce(FILE *out, integrit_t *it)
{
    switch (it->output) {
      case OUTPUT_LINES:
	if (it->verbose > 0)
	  options_announce_lines(it, out);
	break;
      case OUTPUT_XML:
	options_announce_xml(it, out);
	break;
      default:
	DIE("unknown value for output member in options");
	break;
    }
}

void options_init(integrit_t *it)
{
    it->conffile		 = NULL;
    it->knowndbname	 = NULL;
    memset(&it->knowndb, 0, sizeof(it->knowndb));
    it->currdbname	 = NULL;
    memset(&it->currdb, 0, sizeof(it->currdb));
    it->root		 = NULL;
    if (! (it->ruleset = malloc(sizeof(hashtbl_t))) )
      DIE("malloc hashtbl");
    if (hashtbl_init(it->ruleset, 20) == -1)
      DIE("initializing hashtbl");
    it->verbose		 = 1;
    it->do_check		 = 0;
    it->do_update	 = 0;
    it->default_flags	 = RULE_SUM | RULE_INODE | RULE_PERMS
      | RULE_NLINK | RULE_UID | RULE_GID | RULE_MTIME | RULE_CTIME;
    it->output		 = OUTPUT_LINES; /* human-readable output */
}

void *call_checkset_free(void *cset, void *data)
{
    checkset_free((checkset *) cset);
    return NULL;
}

void options_destroy(integrit_t *it)
{
    free(it->knowndbname);
    free(it->currdbname);
    free(it->root);
    hashtbl_free(it->ruleset, call_checkset_free, NULL);
    hashtbl_destroy(it->ruleset);
    free(it->ruleset);
}

/* call EATSPACE before calling this */
inline static int blank_or_comment(const char *buf)
{
    switch (*buf) {
      case '\0':		/* empty string counts as blank */
	return 1;
	break;
      case '\n':		/* blank line */
	return 1;
	break;
      case '#':			/* comment */
	return 1;
	break;
      default:
	return 0;
	break;
    }
}

inline static void die_noprop(const char *prop, const char *func)
{
    fprintf(stderr, PROGNAME " (%s) Error: no value for property: %s\n",
	    func, prop);
    exit(INTEGRIT_EXIT_FAILURE);
}

inline static void do_assignment(integrit_t *it, char *buf, char *eq)
{
    char	*property;
    char	*val	 = eq + 1;

    *eq	 = '\0';
    if ( (property = strstr(buf, "known")) ) {
      /* the commandline overrides the config file
       * (really the first one to set it wins and the rest are ignored)
       */
      if (it->knowndbname)
	return;
      EATSPACE(val);
      if (! *val)
	die_noprop(property, __FUNCTION__);
      chomp(val);
      it->knowndbname	 = xstrdup(val);
    } else if ( (property = strstr(buf, "current")) ) {
      /* the commandline overrides the config file
       * (really the first one to set it wins and the rest are ignored)
       */
      if (it->currdbname)
	return;
      EATSPACE(val);
      if (! *val)
	die_noprop(property, __FUNCTION__);
      chomp(val);
      it->currdbname	 = xstrdup(val);
    } else if ( (property = strstr(buf, "root")) ) {
      EATSPACE(val);
      if (! *val)
	die_noprop(property, __FUNCTION__);
      chomp(val);
      it->root	 = xstrdup(val);
    } else {
      die(__FUNCTION__, "Error: unknown property: %s\n", property);
    }
}

static void options_add_checkset(integrit_t *it, char *namebuf,
				 size_t namebuf_chars, checkset *cset)
{
    hashtbl_t	*h	 = it->ruleset;
    checkset	*old;
    void *replaced;

    if ( (old = hashtbl_lookup(h, namebuf, namebuf_chars)) ) {
      /* checkset merge frees the old checkset */
      cset	 = checkset_merge(cset, old, namebuf);
    }

    if (hashtbl_store(h, namebuf, namebuf_chars, cset, &replaced) == -1)
      DIE("storing checkset in table");
}

/* this function is made more complex by the fact that is supports both
 * non-inheriting and (regular) cascading checksets.  The cset pointer
 * is overloaded to point to a non-inheriting checkset (ni_checkset)
 * when necessary.
 */
inline static void do_rule(integrit_t *it, char *buf)
{
    checkset	*cset;
    char	namebuf[BUFSIZ];
    int		namebuf_chars	 = 0;
    int		n_switches;
    char	ignore		 = 0;
    char	nochildren	 = 0;
    char	noinherit	 = 0;

    EATSPACE(buf);
    switch (*buf) {
      case '!':
	ignore		 = 1;
	++buf;
	break;
      case '=':
	nochildren	 = 1;
	++buf;
	break;
      case '$':
	noinherit	 = 1;
	++buf;
	break;
      default:
	break;	
    }

    EATSPACE(buf);
    for ( ; *buf && (*buf != '\n'); ++buf) {
      if (namebuf_chars == (BUFSIZ - 3)) /* space for two new chars,
					  * plus null char */
	die(__FUNCTION__, "Error: filename too long in config");

      if (*buf == '\\') {
	if (isspace((unsigned char) *(buf + 1)))
	  namebuf[namebuf_chars++]	 = *(++buf);
	else
	  namebuf[namebuf_chars++]	 = '\\';
      } else if (isspace((unsigned char) *buf)) {
	break;			/* end of the name */
      } else {
	namebuf[namebuf_chars++]	 = *buf;
      }
    }
    namebuf[namebuf_chars]	 = '\0';

    EATSPACE(buf);
    n_switches	 = strlen(buf);
    /* trim trailing whitespace */
    while (n_switches && isspace((unsigned char) buf[n_switches - 1])) {
      buf[n_switches - 1]	 = '\0';
      --n_switches;
    }
    if (strspn(buf, "SsIiPpLlUuGgZzAaMmCcRr") != n_switches)
      die(__FUNCTION__,
	  "Error: unrecognized check switch in conf file rule for %s",
	  namebuf);

    if (noinherit)
      cset	 = (checkset *) ni_checkset_new();
    else
      cset	 = checkset_new();

    CHECKSET_SETBOOLEAN(cset, RULE_IGNORE, ignore);
    CHECKSET_SETBOOLEAN(cset, RULE_NOCHILDREN, nochildren);
    CHECKSET_SETBOOLEAN(cset, RULE_NOINHERIT, noinherit);

    if (noinherit)
      ((ni_checkset *) cset)->ni_switches	 = xstrdup(buf);
    else
      cset->switches	 = xstrdup(buf);
    
    chomp(namebuf);
#ifdef	DEBUG
    fprintf(stderr, "debug: path (%s) checkset (", namebuf);
    checkset_show(stderr, cset);
    fputs(")\n", stderr);
#endif
    
    options_add_checkset(it, namebuf, namebuf_chars, cset);
}

static void usage(void)
{
    fputs("Usage:\n"
	  "\n"
	  "      integrit -C conffile [options]\n"
	  "      integrit -V\n"
	  "      integrit -h\n"
	  "\n"
	  "Options:\n"
	  "\n"
	  "      -C	specify configuration file\n"
	  "      -x	use XML output instead of abbreviated output\n"
	  "      -u	do update: create current state database\n"
	  "      -c	do check: verify current state against known db\n"
	  "      -q	lower verbosity\n"
	  "      -v	raise verbosity\n"
	  "      -N	specify the current (New) database, overriding conf file\n"
	  "      -O	specify the known (Old) database, overriding conf file\n"
	  "      -V	show integrit version info and exit\n"
	  "      -h	show this help      \n\n", stderr);
}

void parse_args(integrit_t *it, int argc, char *argv[])
{
    int		c;
    opterr	 = 0;

    while ( (c = getopt(argc, argv, "hVvqC:cuxN:O:")) != -1) {
      switch (c) {
	case 'h':
	  usage();
	  exit(EXIT_SUCCESS);
	  break;
	case 'V':
	  puts(PROGNAME " version " INTEGRIT_VERSION);
	  exit(EXIT_SUCCESS);
	  break;
	case 'v':
	  ++it->verbose;
	  break;
	case 'q':
	  --(it->verbose);
	  break;
	case 'C':
	  it->conffile	 = optarg;
	  break;
	case 'c':
	  it->do_check	 = 1;
	  break;
	case 'N':
	  it->currdbname = xstrdup(optarg);
	  break;
	case 'O':
	  it->knowndbname = xstrdup(optarg);
	  break;
	case 'u':
	  it->do_update	 = 1;
	  break;
	case 'x':
	  it->output	 = OUTPUT_XML;
	  break;
	case '?':
	  if (isprint(optopt))
	    warn(__FUNCTION__, "Error: unknown option `-%c'.\n", optopt);
	  else
	    warn(__FUNCTION__,
		"Error: unknown option character `\\x%x'.\n", optopt);
	  usage();
	  exit(INTEGRIT_EXIT_FAILURE);
	  break;
	default:
	  abort();		/* this shouldn't happen */
	  break;
      }	/* end switch */
    } /* end while getopt */

    if (! it->conffile)
      die(__FUNCTION__, "Error: no conffile on command line");
}

void options_set(integrit_t *it, int argc, char *argv[])
{
    char	buf[BUFSIZ];
    char	*cp;
    char	*equalsign;
    FILE	*conf;

    parse_args(it, argc, argv);
    if (! (conf = fopen(it->conffile, "r")) )
      DIE("opening conf file");

    while (fgets(buf, BUFSIZ, conf)) {
      cp	 = buf;
      EATSPACE(cp);
      if (blank_or_comment(cp))
	continue;
      else if (*cp == '=')
	do_rule(it, cp);
      else if ( (equalsign = strchr(cp, '=')) )
	do_assignment(it, cp, equalsign);
      else
	do_rule(it, cp);
    }
    if (! it->knowndbname)
      die(__FUNCTION__, "Error: known database unspecified\n");
    if (! it->currdbname)
      die(__FUNCTION__, "Error: current database unspecified\n");
    if (! it->root)
      die(__FUNCTION__, "Error: root search directory unspecified\n");
}