[go: up one dir, main page]

File: utils.c

package info (click to toggle)
fwlogwatch 1.1-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 864 kB
  • ctags: 607
  • sloc: ansic: 5,673; lex: 1,490; php: 706; sh: 445; makefile: 144
file content (446 lines) | stat: -rw-r--r-- 10,411 bytes parent folder | download | duplicates (3)
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
446
/* Copyright (C) 2000-2006 Boris Wesslowski */
/* $Id: utils.c,v 1.46 2006/03/08 19:36:03 bw Exp $ */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include "main.h"

struct input_file *first_file = NULL;

extern struct options opt;
extern struct conn_data *first;
extern struct dns_cache *dns_first;
extern struct known_hosts *first_host;
extern struct whois_entry *whois_first;
extern struct parser_options *excluded_first;


/*
 * xstrncpy() - similar to strncpy(3) but always terminates string
 * with '\0' (if n > 0 and dest != NULL),  doesn't do padding.
 */
char *xstrncpy(char *dest, const char *src, size_t n)
{
  char *r = dest;

  if((n <= 0) || (dest == NULL)) {
    return dest;
  }
  if(src != NULL) {
    while ((--n != 0) && (*src != '\0')) {
      *dest++ = *src++;
    }
  }
  *dest = '\0';
  return r;
}

void *xmalloc(int size)
{
  void *ptr;

  ptr = malloc(size);
  if (ptr == NULL) {
    fprintf(stderr, _("\nMemory allocation error, exiting.\n"));
    exit(EXIT_FAILURE);
  }

  return ptr;
}

void log_exit(unsigned char returncode)
{
  if(opt.pidfile[0] != '\0') {
    if(unlink(opt.pidfile) == -1) {
      if(opt.verbose)
	syslog(LOG_NOTICE, "unlink %s: %s", opt.pidfile, strerror(errno));
    }
  }
  syslog(LOG_NOTICE, _("Exiting"));
  exit(returncode);
}

void run_command(char *buf)
{
  pid_t pid;

  if (strstr(buf, "%") != NULL) {
    syslog(LOG_NOTICE, _("Not executing buffer containing format string"));
    return;
  }

  if(opt.verbose == 2) {
    syslog(LOG_NOTICE, _("Executing '%s'"), buf);
  }

  pid = fork();
  if (pid == -1) {
    syslog(LOG_NOTICE, "fork: %s", strerror(errno));
    log_exit(EXIT_FAILURE);
  }

  if (pid == 0) {
    execl("/bin/sh", "/bin/sh", "-c", buf, NULL);
    syslog(LOG_NOTICE, "execl: %s", strerror(errno));
    log_exit(EXIT_FAILURE);
  }

  wait(NULL);
}

void free_conn_data()
{
  struct conn_data *this;

  this = first;
  while (this != NULL) {
    first = this;
    this = this->next;
    free(first->hostname);
    free(first->chainlabel);
    free(first->branchname);
    free(first->interface);
    free(first);
  }
  first = NULL;
}

void free_dns_cache()
{
  struct dns_cache *dns_this;

  dns_this = dns_first;
  while (dns_this != NULL) {
    dns_first = dns_this;
    dns_this = dns_this->next;
    free(dns_first->fqdn);
    free(dns_first);
  }
  dns_first = NULL;
}

void free_whois()
{
  struct whois_entry *whois_this;

  whois_this = whois_first;
  while (whois_this != NULL) {
    whois_first = whois_this;
    whois_this = whois_this->next;
    free(whois_first->ip_route);
    free(whois_first->ip_descr);
    free(whois_first->as_descr);
    free(whois_first);
  }
  whois_first = NULL;
}

void free_hosts()
{
  struct known_hosts *this_host;

  this_host = first_host;
  while (this_host != NULL) {
    first_host = this_host;
    this_host = this_host->next;
    free(first_host);
  }
  first_host = NULL;
}

void free_exclude_data()
{
  struct parser_options *excluded_this;

  excluded_this = excluded_first;
  while (excluded_this != NULL) {
    excluded_first = excluded_this;
    excluded_this = excluded_this->next;
    if(excluded_first->svalue != NULL)
      free(excluded_first->svalue);
    free(excluded_first);
  }
  excluded_first = NULL;
}

void init_line()
{
  opt.line->time = 0;
  opt.line->hostname[0] = '\0';
  opt.line->chainlabel[0] = '\0';
  opt.line->branchname[0] = '\0';
  opt.line->interface[0] = '\0';
  opt.line->protocol = 0;
  opt.line->datalen = 0;
  opt.line->shost.s_addr = 0;
  opt.line->sport = 0;
  opt.line->dhost.s_addr = 0;
  opt.line->dport = 0;
  opt.line->flags = 0;
  opt.line->count = 0;
}

void mode_error()
{
  fprintf(stderr, _("fwlogwatch error: mode collision, please check that you didn't specify\n"
		    "   several modes on the command line or a second mode is active in the\n"
		    "   configuration file.\n"
		    "   Please use a separate configuration file for each mode or comment out all\n"
		    "   entries in the default configuration and use command line parameters.\n"));
  exit(EXIT_FAILURE);
}

void build_time(char *smonth, int day, int hour, int minute, int second)
{
  int month = 0, now, then;
  struct tm *t;

  if(opt.mode != REALTIME_RESPONSE) {
    t = localtime(&opt.now);
  } else {
    time_t rr_now;

    rr_now = time(NULL);
    t = localtime(&rr_now);
  }
  now = (int)mktime(t);
  if (strncmp(smonth, "Jan", 3) == 0) { month = 0; }
  else if (strncmp(smonth, "Feb", 3) == 0) { month = 1; }
  else if (strncmp(smonth, "Mar", 3) == 0) { month = 2; }
  else if (strncmp(smonth, "Apr", 3) == 0) { month = 3; }
  else if (strncmp(smonth, "May", 3) == 0) { month = 4; }
  else if (strncmp(smonth, "Jun", 3) == 0) { month = 5; }
  else if (strncmp(smonth, "Jul", 3) == 0) { month = 6; }
  else if (strncmp(smonth, "Aug", 3) == 0) { month = 7; }
  else if (strncmp(smonth, "Sep", 3) == 0) { month = 8; }
  else if (strncmp(smonth, "Oct", 3) == 0) { month = 9; }
  else if (strncmp(smonth, "Nov", 3) == 0) { month = 10; }
  else if (strncmp(smonth, "Dec", 3) == 0) { month = 11; }
  t->tm_mon = month;
  t->tm_mday = day;
  t->tm_hour = hour;
  t->tm_min = minute;
  t->tm_sec = second;
  t->tm_isdst = -1;
  then = (int)mktime(t);
  if (then > now)
    --t->tm_year;

  opt.line->time = mktime(t);
}

unsigned char convert_ip(char *ip, struct in_addr *addr)
{
#ifndef SOLARIS
  int retval;

  retval = inet_aton(ip, addr);
  if (retval == 0) {
#else
#ifndef INADDR_NONE
#define INADDR_NONE -1
#endif
  addr->s_addr = inet_addr(ip);
  if (addr->s_addr == INADDR_NONE) {
#endif
    if (opt.verbose)
      fprintf(stderr, _("IP address error: %s\n"), ip);
    return IN_ADDR_ERROR;
  }
  return IN_ADDR_OK;
}

unsigned long int parse_cidr(char *input)
{
  char *pnt;
  int n;
  unsigned long int netmask[33] = {
    0x0,
    0x80000000, 0xC0000000, 0xE0000000, 0xF0000000,
    0xF8000000, 0xFC000000, 0xFE000000, 0xFF000000,
    0xFF800000, 0xFFC00000, 0xFFE00000, 0xFFF00000,
    0xFFF80000, 0xFFFC0000, 0xFFFE0000, 0xFFFF0000,
    0xFFFF8000, 0xFFFFC000, 0xFFFFE000, 0xFFFFF000,
    0xFFFFF800, 0xFFFFFC00, 0xFFFFFE00, 0xFFFFFF00,
    0xFFFFFF80, 0xFFFFFFC0, 0xFFFFFFE0, 0xFFFFFFF0,
    0xFFFFFFF8, 0xFFFFFFFC, 0xFFFFFFFE, 0xFFFFFFFF
  };

  pnt = strstr(input, "/");
  if (pnt != NULL) {
    n = atoi(pnt+1);
    if ((n < 0) || (n > 32)) {
      fprintf(stderr, _("Error in CIDR format: %s\n"), input);
      exit(EXIT_FAILURE);
    }
    *pnt = '\0';
  } else {
    n = 32;
  }

  return ntohl(netmask[n]);
}

void add_known_host(char *ip)
{
  struct known_hosts *host, *test_host;

  host = xmalloc(sizeof(struct known_hosts));
  host->netmask.s_addr = parse_cidr(ip);
  if(convert_ip(ip, &host->shost) == IN_ADDR_ERROR) {
    fprintf(stderr, _("(known host)\n"));
    free(host);
    exit(EXIT_FAILURE);
  }

  host->shost.s_addr = host->shost.s_addr & host->netmask.s_addr;

  test_host = first_host;
  while (test_host != NULL) {
    if (test_host->shost.s_addr == host->shost.s_addr) {
      free(host);
      return;
    }
    test_host = test_host->next;
  }

  host->time = 0;
  host->count = 0;
  host->protocol = 0;
  host->dhost.s_addr = 0;
  host->sport = 0;
  host->dport = 0;
  host->id = opt.global_id++;
  host->next = first_host;
  first_host = host;
}

void add_exclude_hpb(char *input, unsigned char mode)
{
  struct parser_options *excluded_this;
  struct in_addr ip;

  excluded_this = xmalloc(sizeof(struct parser_options));
  excluded_this->mode = mode;
  excluded_this->svalue = NULL;
  if(mode & PARSER_MODE_HOST) {
    struct parser_options *excluded_test;
    excluded_this->netmask.s_addr = parse_cidr(input);
    if (convert_ip(input, &ip) == IN_ADDR_ERROR) {
      fprintf(stderr, _("(excluded host)\n"));
      free(excluded_this);
      exit(EXIT_FAILURE);
    }
    excluded_this->value = ip.s_addr & excluded_this->netmask.s_addr;
    excluded_test = excluded_first;
    while (excluded_test != NULL) {
      if (excluded_test->value == excluded_this->value) {
	free(excluded_this);
	return;
      }
      excluded_test = excluded_test->next;
    }
  } else if(mode & PARSER_MODE_PORT) {
    excluded_this->value = atoi(input);
  } else if(mode & (PARSER_MODE_CHAIN | PARSER_MODE_BRANCH)) {
    excluded_this->svalue = xmalloc(strlen(input)+1);
    xstrncpy(excluded_this->svalue, input, strlen(input)+1);
  }
  excluded_this->next = excluded_first;
  excluded_first = excluded_this;
}

void add_input_file(char *name)
{
  struct input_file *file, *ptr;

  if(!strncmp(name, "-", FILESIZE))
    opt.std_in = 1;

  if (opt.std_in) {
    opt.filecount = 0;
    return;
  }

  file = xmalloc(sizeof(struct input_file));
  file->name = xmalloc(strlen(name)+1);
  file->next = NULL;

  xstrncpy(file->name, name, strlen(name)+1);

  ptr = first_file;
  if (ptr == NULL) {
    first_file = file;
  } else {
    while (ptr->next != NULL) {
      ptr = ptr->next;
    }
    ptr->next = file;
  }
  opt.filecount++;
}

void free_input_file()
{
  struct input_file *file;

  file = first_file;
  while (file != NULL) {
    free(file->name);
    first_file = file;
    file = file->next;
    free(first_file);
  }
  first_file = NULL;
}

void generate_email_header(FILE *fd)
{
  time_t now;
  char stime[TIMESIZE];

  now = time(NULL);
  strftime(stime, TIMESIZE, "%Y%m%d-%H%M%S", localtime(&now));

  fprintf(fd, "From: %s\n", opt.sender);
  fprintf(fd, "To: %s\n", opt.recipient);
  if(opt.cc[0] != '\0')
    fprintf(fd, "Cc: %s\n", opt.cc);
  fprintf(fd, "Subject: %s\n", opt.title);
  fprintf(fd, "X-Generator: %s %s (C) %s\n", PACKAGE, VERSION, COPYRIGHT);
  if(opt.html) {
    fprintf(fd, "Mime-Version: 1.0\n");
    fprintf(fd, "Content-Type: text/html; charset=iso-8859-1\n");
    fprintf(fd, "Content-Disposition: inline; filename=\"fwlogwatch_summary-%s.html\"\n", stime);
  }
  fprintf(fd, "\n");
}

void fdprintf(int fd, char *format, ...)
{
  if(opt.status != FD_ERROR) {
    char buf[BUFSIZE];
    va_list argv;
    ssize_t retval;

    va_start(argv, format);
    vsnprintf(buf, BUFSIZE, format, argv);
    retval = write(fd, buf, strlen(buf));
    va_end(argv);
    if(retval == -1) {
      syslog(LOG_NOTICE, "write: %s", strerror(errno));
      opt.status = FD_ERROR;
      return;
    }
    fflush(NULL);
  }
}