File: ipchains.ll

package info (click to toggle)
wflogs 0.9.8-5.2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,252 kB
  • ctags: 3,750
  • sloc: ansic: 12,166; sh: 8,059; cpp: 6,412; lex: 1,525; yacc: 1,430; makefile: 731; sed: 16
file content (185 lines) | stat: -rw-r--r-- 5,099 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
/* -*-c-*- */
/*
 * WallFire -- a comprehensive firewall administration tool.
 * 
 * Copyright (C) 2001 Herv Eychenne <rv@wallfire.org>
 * 
 * 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.
 * 
 */

%option prefix="ipchains"
%option outfile="ipchains.cc"
%option noyywrap

%{
using namespace std;

#define YY_NO_UNPUT

#define IPLEN 16

#include <unistd.h>
#include <string.h>
#include <ctype.h>

#include "ipchains.h"
#include "common.h"
#include "defs.h"


static void ipchains_parse_data(char *input);
static void ipchains_parse_rdata(char *input);
static void ipchains_parse_ips(char *input);

static wf_logentry* logentry;
%}

MONTH	"Jan"|"Feb"|"Mar"|"Apr"|"May"|"Jun"|"Jul"|"Aug"|"Sep"|"Oct"|"Nov"|"Dec"
STRING	[a-zA-Z-][a-zA-Z0-9._-]*
LOGHOST	[0-9.a-zA-Z_-]*
DIGIT	[0-9]
NUMBER	{DIGIT}+
BYTE	{DIGIT}{1,3}
IPADDR	{BYTE}\.{BYTE}\.{BYTE}\.{BYTE}
PORT	{DIGIT}{1,5}
HEXDIGIT	[0-9a-fA-F]
HEXNUM	"0x"{HEXDIGIT}+
IPCHAINS	" kernel: Packet log: "

%%

{MONTH}[ ]{1,2}{DIGIT}{1,2}[ ]{DIGIT}{2}:{DIGIT}{2}:{DIGIT}{2}[ ]{LOGHOST}	parse_date(ipchainstext, logentry);
{IPCHAINS}	/* ignore */
{STRING}[ ]{STRING}[ ]{STRING}" PROTO="{NUMBER}	ipchains_parse_data(ipchainstext);
{STRING}" REDIRECT "{NUMBER}" "{STRING}" PROTO="{NUMBER}	ipchains_parse_rdata(ipchainstext);
{IPADDR}":"{PORT}" "{IPADDR}":"{PORT}		ipchains_parse_ips(ipchainstext);
"L="{NUMBER}	logentry->datalen = atoi(ipchainstext+2);
"S="{HEXNUM}	/* ignore */
"I="{NUMBER}	/* ignore */
"F="{HEXNUM}	/* ignore */
"T="{NUMBER}	/* ignore */
"O="{HEXNUM}	/* ignore */
"SYN"		logentry->tcpflags = TCP_SYN;
"(#"{NUMBER}")"	/* ignore */
[ ]+		/* ignore whitespace */
[\n]		/* ignore */
{STRING}	if (verbose) fprintf(stderr, _("Unrecognized token: %s\n"), ipchainstext);
.		if (verbose) fprintf(stderr, _("Unrecognized character: %s\n"), ipchainstext);

%%

static void
ipchains_parse_data(char *input) {
  char chainlabel[32], branchname[32], iface[32];
  int protocol;

  if (sscanf(input, "%30s %30s %30s PROTO=%3d",
	     chainlabel, branchname, iface, &protocol) != 4)
    return;
  logentry->chainlabel = chainlabel;
  logentry->branchname = branchname;

  if (!strcmp(chainlabel, "output") || !strcmp(chainlabel, "forward"))
    logentry->output_iface = iface;
  else /* "input" and default */
    logentry->input_iface = iface;

  logentry->protocol = protocol;
}

static void
ipchains_parse_rdata(char* input) {
  char chainlabel[32], branchname[32], iface[32];
  int port, protocol;
  
  if (sscanf(input, "%30s REDIRECT %5d %30s PROTO=%3d",
	     chainlabel, &port, iface, &protocol) != 4)
    return;

  snprintf(branchname, sizeof(branchname), "RD %d", port);
  logentry->chainlabel = chainlabel;
  logentry->branchname = branchname;

  if (!strcmp(chainlabel, "output") || !strcmp(chainlabel, "forward"))
    logentry->output_iface = iface;
  else /* "input" and default */
    logentry->input_iface = iface;

  logentry->protocol = protocol;
}

static void
ipchains_parse_ips(char *input) {
  int shost1, shost2, shost3, shost4, sport;
  int dhost1, dhost2, dhost3, dhost4, dport;
  char ip[IPLEN];

  if (sscanf(input, "%3d.%3d.%3d.%3d:%5d %3d.%3d.%3d.%3d:%5d",
	     &shost1, &shost2, &shost3, &shost4, &sport,
	     &dhost1, &dhost2, &dhost3, &dhost4, &dport) != 10)
    return;

  snprintf(ip, IPLEN, "%d.%d.%d.%d", shost1, shost2, shost3, shost4);
  if (logentry->sipaddr.set(ip) == false)
    return;

  snprintf(ip, IPLEN, "%d.%d.%d.%d", dhost1, dhost2, dhost3, dhost4);
  if (logentry->dipaddr.set(ip) == false)
    return;

  logentry->sport = sport;
  logentry->dport = dport;
}


wf_inmodule_ipchains::wf_inmodule_ipchains() {
}

bool
wf_inmodule_ipchains::match(const string& line) const {
  return (line.find(" kernel: Packet log: ") != string::npos);
}

enum wf_logentry_parsing_result
wf_inmodule_ipchains::parse(wf_logentry** retlogentry,
  const string& line, int linenum) {
  logentry = new wf_logentry();
  if (logentry == NULL)
    return WF_LOGENTRY_PARSING_ERROR;

  ipchains_scan_string(line.c_str());
  ipchainslex();

  logentry->count = 1;
  logentry->format = "ipchains";

  /*
  if (parser == (IPCHAINS_DATE|IPCHAINS_DATA|IPCHAINS_IPS))
  */
    *retlogentry = logentry;
    return WF_LOGENTRY_PARSING_OK;

  if (verbose)
    fprintf(stderr, _("ipchains parse error in line %d, ignoring.\n"),
	    linenum);

  return WF_LOGENTRY_PARSING_ERROR;
}

extern "C" wf_inmodule*
wf_inmodule_ipchains_init() {
  return new wf_inmodule_ipchains();
}