Project

General

Profile

Task #166 » snortsam.patch

Lukáš Macura, 07/21/2011 03:04 PM

View differences:

feeds/packages/net/snort/patches/020-snortsam.patch (revision 0)
1
diff -ruN snort-2.8.4.1.orig/autojunk.sh snort-2.8.4.1/autojunk.sh
2
--- snort-2.8.4.1.orig/autojunk.sh	1970-01-01 03:30:00.000000000 +0330
3
+++ snort-2.8.4.1/autojunk.sh	2009-06-23 16:40:44.000000000 +0430
4
@@ -0,0 +1,7 @@
5
+#!/bin/sh
6
+# the list of commands that need to run before we do a compile
7
+libtoolize --automake --copy
8
+aclocal -I m4
9
+autoheader
10
+automake --add-missing --copy
11
+autoconf
12
diff -ruN snort-2.8.4.1.orig/src/Makefile.am snort-2.8.4.1/src/Makefile.am
13
--- snort-2.8.4.1.orig/src/Makefile.am	2009-06-23 16:40:16.000000000 +0430
14
+++ snort-2.8.4.1/src/Makefile.am	2009-06-23 16:40:44.000000000 +0430
15
@@ -50,7 +50,8 @@
16
 pcap_pkthdr32.h \
17
 cpuclock.h \
18
 sf_types.h \
19
-log_text.c log_text.h
20
+log_text.c log_text.h \
21
+twofish.c twofish.h
22
 
23
 snort_LDADD = output-plugins/libspo.a \
24
 detection-plugins/libspd.a            \
25
diff -ruN snort-2.8.4.1.orig/src/fatal.h snort-2.8.4.1/src/fatal.h
26
--- snort-2.8.4.1.orig/src/fatal.h	1970-01-01 03:30:00.000000000 +0330
27
+++ snort-2.8.4.1/src/fatal.h	2009-06-23 16:40:44.000000000 +0430
28
@@ -0,0 +1,40 @@
29
+/* $Id$ */
30
+/*
31
+** Copyright (C) 2002-2008 Sourcefire, Inc.
32
+** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
33
+**
34
+** This program is free software; you can redistribute it and/or modify
35
+** it under the terms of the GNU General Public License Version 2 as
36
+** published by the Free Software Foundation.  You may not use, modify or
37
+** distribute this program under any other version of the GNU General
38
+** Public License.
39
+**
40
+** This program is distributed in the hope that it will be useful,
41
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
42
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43
+** GNU General Public License for more details.
44
+**
45
+** You should have received a copy of the GNU General Public License
46
+** along with this program; if not, write to the Free Software
47
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
48
+*/
49
+
50
+#ifndef __FATAL_H__
51
+#define __FATAL_H__
52
+
53
+
54
+/*
55
+ * in debugging mode print out the filename and the line number where the
56
+ * failure have occured
57
+ */
58
+
59
+
60
+#ifdef DEBUG
61
+	#define	FATAL(msg) 	{ printf("%s:%d: ", __FILE__, __LINE__); FatalError( (char *) msg); }
62
+#else
63
+	#define	FATAL(msg)	FatalError( (char *) msg)
64
+#endif
65
+
66
+
67
+
68
+#endif	/* __FATAL_H__ */
69
diff -ruN snort-2.8.4.1.orig/src/output-plugins/Makefile.am snort-2.8.4.1/src/output-plugins/Makefile.am
70
--- snort-2.8.4.1.orig/src/output-plugins/Makefile.am	2009-06-23 16:40:16.000000000 +0430
71
+++ snort-2.8.4.1/src/output-plugins/Makefile.am	2009-06-23 16:40:44.000000000 +0430
72
@@ -11,6 +11,7 @@
73
 spo_log_tcpdump.h spo_unified.c spo_unified2.c spo_unified.h spo_unified2.h \
74
 spo_log_ascii.c spo_log_ascii.h spo_alert_sf_socket.h spo_alert_sf_socket.c \
75
 spo_alert_prelude.c spo_alert_prelude.h spo_alert_arubaaction.c spo_alert_arubaaction.h \
76
+spo_alert_fwsam.c spo_alert_fwsam.h \
77
 spo_alert_test.c spo_alert_test.h
78
 
79
 INCLUDES = @INCLUDES@
80
diff -ruN snort-2.8.4.1.orig/src/output-plugins/spo_alert_fwsam.c snort-2.8.4.1/src/output-plugins/spo_alert_fwsam.c
81
--- snort-2.8.4.1.orig/src/output-plugins/spo_alert_fwsam.c	1970-01-01 03:30:00.000000000 +0330
82
+++ snort-2.8.4.1/src/output-plugins/spo_alert_fwsam.c	2009-06-23 16:40:44.000000000 +0430
83
@@ -0,0 +1,1380 @@
84
+/* $id: snortpatchb,v 1.2 2002/10/26 03:32:35 fknobbe Exp $
85
+**
86
+** spo_alert_fwsam.c
87
+**
88
+** Copyright (c) 2001-2004 Frank Knobbe <frank@knobbe.us>
89
+**
90
+** This program is free software; you can redistribute it and/or modify
91
+** it under the terms of the GNU General Public License as published by
92
+** the Free Software Foundation; either version 2 of the License, or
93
+** (at your option) any later version.
94
+**
95
+** This program is distributed in the hope that it will be useful,
96
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
97
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
98
+** GNU General Public License for more details.
99
+**
100
+** You should have received a copy of the GNU General Public License
101
+** along with this program; if not, write to the Free Software
102
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
103
+*/
104
+
105
+/* 
106
+ * Purpose:
107
+ *
108
+ * This module sends alerts to a remote service on a host running SnortSam
109
+ * (the agent) which will block the intruding IP address on a variety of
110
+ * host and network firewalls.
111
+ *
112
+ * SnortSam also performs checks against a white-list of never-to-be-blocked IP addresses,
113
+ * can override block durations (for example for known proxies), and can detect attack conditions
114
+ * where too many blocks are received within a defined interval. If an attack is detected
115
+ * it will unblock the last x blocks and wait for the attack to end.
116
+ *
117
+ * See the SnortSam documentation for more information.
118
+ *
119
+ *
120
+ * Output Plugin Parameters:
121
+ ***************************
122
+ *   
123
+ * output alert_fwsam: <SnortSam Station>:<port>/<key> 
124
+ *
125
+ *	<FW Mgmt Station>:	The IP address or host name of the host running SnortSam.
126
+ *	<port>:			The port the remote SnortSam service listens on (default 898).
127
+ *	<key>:			The key used for authentication (encryption really)
128
+ *				of the communication to the remote service.
129
+ *
130
+ * Examples:
131
+ *
132
+ * output alert_fwsam: snortsambox/idspassword
133
+ * output alert_fwsam: fw1.domain.tld:898/mykey
134
+ * output alert_fwsam: 192.168.0.1/borderfw  192.168.1.254/wanfw
135
+ *
136
+ *
137
+ * Rule Options:
138
+ ***************
139
+ *
140
+ * fwsam:	who[how],time;
141
+ *
142
+ *  who: src, source, dst, dest, destination
143
+ *			IP address to be blocked according to snort rule (some rules
144
+ *			are reversed, i.e. homenet -> any [and you want to block any]).
145
+ *          src denotes IP to the left of -> and dst denotes IP to the right
146
+ *
147
+ *  how: Optional. In, out, src, dest, either, both, this, conn, connection
148
+ *			Tells FW-1 to block packets INcoming from host, OUTgoing to host,
149
+ *			EITHERway, or only THIS connection (IP/Service pair).
150
+ *			See 'fw sam' for more information. May be ignored by other plugins.
151
+ *
152
+ * time: Duration of block in seconds. (Accepts 'days', 'months', 'weeks',
153
+ *		 'years', 'minutes', 'seconds', 'hours'. Alternatively, a value of
154
+ *		 0, or the keyword PERManent, INFinite, or ALWAYS, will block the 
155
+ *		 host permanently. Be careful with this!
156
+ *			Tells FW-1 (and others) how long to inhibit packets from the host.
157
+ *
158
+ * Examples:
159
+ *
160
+ * fwsam:  src[either],15min;
161
+ *     or  dst[in], 2 days 4 hours
162
+ *     or  src, 1 hour
163
+ *
164
+ *         (default: src[either],5min)
165
+ *
166
+ *
167
+ * Effect:
168
+ *
169
+ * Alerts are sent to the remote SnortSam services on Firewall-1 Management Stations
170
+ * or other hosts running SnortSam (as required for Cisco Routers and PIX).
171
+ * The remote services will invoke the SAM configuration via the fw sam
172
+ * command line, or by sending a packet to the SAM port 18183, or by using the official
173
+ * OPSEC API calls, or by telnetting into Cisco routers or PIX firewalls.
174
+ * The communication over the network is encrypted using two-fish.
175
+ * (Implementation ripped from CryptCat by Farm9 with permission.)
176
+ *
177
+ * Future Plans:
178
+ *
179
+ * - Custom alert trigger per rule (x alerts in y secs) --> Seems to exist in Snort 1.9 now.
180
+ * - Enable/Allow tagged fwsam: arguments to provide different values to
181
+ *        different stations.  --> Seems to be accomplished with custom rule-types
182
+ *
183
+ *
184
+ * Comments:
185
+ * 
186
+ * It seem that above wishes can be implemented with todays setup. Feedback concerning
187
+ * these is greatly appreciated.
188
+ *
189
+*/
190
+
191
+
192
+#include "spo_alert_fwsam.h"
193
+#include "twofish.h"
194
+/* external globals from rules.c  */
195
+extern char *file_name;
196
+extern int file_line;
197
+extern OptTreeNode *otn_tmp;
198
+extern PV pv;
199
+
200
+
201
+/* my globals  */
202
+
203
+FWsamList *FWsamStationList=NULL;			/* Global (for all alert-types) list of snortsam stations */
204
+FWsamOptions *FWsamOptionField=NULL;
205
+unsigned long FWsamMaxOptions=0;
206
+
207
+
208
+/*
209
+ * Function: AlertFWsamSetup()
210
+ *
211
+ * Purpose: Registers the output plugin keyword and initialization 
212
+ *          function into the output plugin list.  This is the function that
213
+ *          gets called from InitOutputPlugins() in plugbase.c.
214
+ *			It also registers itself as a plugin in order to parse every rule 
215
+ *			and to set the appropiate flags from fwsam: option.
216
+ *
217
+ * Arguments: None.
218
+ *
219
+ * Returns: void function
220
+ *
221
+*/
222
+void AlertFWsamSetup(void)
223
+{
224
+    /* link the preprocessor keyword to the init function in 
225
+       the preproc list */
226
+    RegisterOutputPlugin("alert_fwsam", NT_OUTPUT_ALERT, AlertFWsamInit);
227
+    RegisterPlugin("fwsam", AlertFWsamOptionInit, NULL, OPT_TYPE_ACTION);
228
+	
229
+#ifdef FWSAMDEBUG   /* This allows debugging of fwsam only */
230
+    LogMessage("DEBUG => [Alert_FWsam](AlertFWsamSetup) Output plugin is plugged in...\n");
231
+#endif
232
+}
233
+
234
+
235
+/*	This function checks if a given snortsam station is already in
236
+ *	a given list.
237
+*/
238
+int FWsamStationExists(FWsamStation *who,FWsamList *list)
239
+{
240
+    while(list)
241
+    {
242
+        if(list->station) {
243
+//			if(	who->stationip.s_addr==list->station->stationip.s_addr && 
244
+            if(IP_EQUALITY(&who->stationip, &list->station->stationip) &&
245
+            who->stationport==list->station->stationport)
246
+            return TRUE;
247
+        }
248
+        list=list->next;
249
+    }
250
+    return FALSE;
251
+}
252
+
253
+/*
254
+ * Function: AlertFWsamInit(char *args)
255
+ *
256
+ * Purpose: Calls the argument parsing function, performs final setup on data
257
+ *          structs, links the preproc function into the function list.
258
+ *
259
+ * Arguments: args => ptr to argument string
260
+ *
261
+ * Returns: void function
262
+ *
263
+*/
264
+void AlertFWsamInit(char *args)
265
+{	char *ap;
266
+	unsigned long statip,cnt,again,i;
267
+	char *stathost,*statport,*statpass;
268
+	FWsamStation *station;
269
+	FWsamList *fwsamlist=NULL;	/* alert-type dependent list of snortsam stations  */
270
+	FWsamList *listp,*newlistp;
271
+	struct hostent *hoste;
272
+	char buf[1024]="";
273
+	FILE *fp;
274
+	FWsamOptions tempopt;
275
+
276
+#ifdef FWSAMDEBUG
277
+	unsigned long hostcnt=0;
278
+	
279
+
280
+
281
+    LogMessage("DEBUG => [Alert_FWsam](AlertFWsamInit) Output plugin initializing...\n");
282
+#endif
283
+
284
+    pv.alert_plugin_active = 1;
285
+
286
+    /* parse the argument list from the rules file */
287
+
288
+	if(args == NULL)
289
+		FatalError("ERROR %s (%d) => [Alert_FWsam](AlertFWsamInit) No arguments to alert_fwsam preprocessor!\n", file_name, file_line);
290
+
291
+	if(!FWsamOptionField && !FWsamMaxOptions)
292
+	{	strncpy(buf,pv.config_dir,sizeof(buf)-1);
293
+		strncpy(buf+strlen(buf),SID_MAPFILE,sizeof(buf)-strlen(buf)-1);
294
+#ifdef FWSAMDEBUG
295
+		LogMessage("DEBUG => [Alert_FWsam](AlertFWsamSetup) Using file: %s\n",buf);
296
+#endif
297
+		fp=fopen(buf,"rt");
298
+		if(!fp)
299
+		{	strncpy(buf,pv.config_dir,sizeof(buf)-1);
300
+			strncpy(buf+strlen(buf),SID_ALT_MAPFILE,sizeof(buf)-strlen(buf)-1);
301
+			fp=fopen(buf,"rt");
302
+		}
303
+		if(fp)  /* Check for presence of map file and read those in, sorted. */
304
+		{   LogMessage("INFO => [Alert_FWsam](AlertFWsamSetup) Using sid-map file: %s\n",buf);
305
+
306
+			while(FWsamReadLine(buf,sizeof(buf),fp))
307
+				if(*buf)
308
+					FWsamMaxOptions++;
309
+			if(FWsamMaxOptions)
310
+			{   if((FWsamOptionField=(FWsamOptions *)malloc(sizeof(FWsamOptions)*FWsamMaxOptions))==NULL)
311
+					FatalError("ERROR => [Alert_FWsam](AlertFWsamSetup) malloc failed for OptionField!\n");
312
+				fseek(fp,0,SEEK_SET);
313
+				for(cnt=0;cnt<FWsamMaxOptions;)
314
+				{	FWsamReadLine(buf,sizeof(buf),fp);
315
+					if(*buf)
316
+						FWsamParseLine(&(FWsamOptionField[cnt++]),buf);
317
+				}
318
+				if(FWsamMaxOptions>1)
319
+				{	for(again=TRUE,cnt=FWsamMaxOptions-1;cnt>=1 && again;cnt--)
320
+					{	for(again=FALSE,i=0;i<cnt;i++)
321
+						{	if(FWsamOptionField[i].sid>FWsamOptionField[i+1].sid)
322
+							{	memcpy(&tempopt,&(FWsamOptionField[i]),sizeof(FWsamOptions));
323
+								memcpy(&(FWsamOptionField[i]),&(FWsamOptionField[i+1]),sizeof(FWsamOptions));
324
+								memcpy(&(FWsamOptionField[i+1]),&tempopt,sizeof(FWsamOptions));
325
+								again=TRUE;
326
+							}
327
+						}
328
+					}
329
+				}
330
+			}
331
+			else
332
+				FWsamMaxOptions=1;
333
+			fclose(fp);
334
+		}
335
+		else
336
+			FWsamMaxOptions=1;
337
+	}
338
+
339
+
340
+	ap=args; /* start at the beginning of the argument */
341
+	while(*ap && isspace(*ap)) ap++;
342
+	while(*ap)
343
+	{	stathost=ap; /* first argument should be host */
344
+		statport=NULL; 
345
+		statpass=NULL;
346
+		while(*ap && *ap!=':' && *ap!='/' && !isspace(*ap)) ap++; /* find token */
347
+		switch(*ap)
348
+		{	case ':':	*ap++=0; /* grab the port */
349
+						statport=ap;
350
+						while(*ap && *ap!='/' && !isspace(*ap)) ap++;
351
+						if(*ap!='/')
352
+							break;
353
+			case '/':	*ap++=0; /* grab the key */
354
+						statpass=ap;
355
+						while(*ap && !isspace(*ap)) ap++;
356
+			default:	break;
357
+		}	
358
+		if(*ap)
359
+		{	*ap++=0;
360
+			while(isspace(*ap)) ap++;
361
+		}
362
+		/* now we have the first host with port and password (key) */
363
+		/* next we check for valid/blank password/port */
364
+		if(statpass!=NULL)
365
+			if(!*statpass)
366
+				statpass=NULL;
367
+		if(statport!=NULL)
368
+			if(!*statport)
369
+				statport=NULL;
370
+		statip=0;
371
+		/* now we check if a valid host was specified */
372
+		if(inet_addr(stathost)==INADDR_NONE)
373
+		{	hoste=gethostbyname(stathost);
374
+			if (!hoste) 
375
+				LogMessage("WARNING %s (%d) => [Alert_FWsam](AlertFWsamInit) Unable to resolve host '%s'!\n",file_name,file_line,stathost);
376
+			else
377
+				statip=*(unsigned long *)hoste->h_addr;
378
+		} 
379
+		else
380
+		{	statip=inet_addr(stathost);
381
+			if(!statip)
382
+				LogMessage("WARNING %s (%d) => [Alert_FWsam](AlertFWsamInit) Invalid host address '%s'!\n",file_name,file_line,stathost);
383
+		}
384
+		if(statip)
385
+		{	/* groovie, a valid host. Let's alloc and assemble the structure for it. */
386
+			if((station=(FWsamStation *)malloc(sizeof(FWsamStation)))==NULL)
387
+				FatalError("ERROR => [Alert_FWsam](AlertFWsamInit) malloc failed for station!\n");
388
+
389
+//			station->stationip.s_addr=statip; /* the IP address */
390
+			station->stationip.ip32[0] = statip; /* the IP address */
391
+			if(statport!=NULL && atoi(statport)>0) /* if the user specified one */
392
+				station->stationport=atoi(statport); /* use users setting */
393
+			else
394
+				station->stationport=FWSAM_DEFAULTPORT; /* set the default port */
395
+
396
+			if(statpass!=NULL) /* if specified by user */
397
+				strncpy(station->stationkey,statpass,TwoFish_KEY_LENGTH); /* use defined key */
398
+			else
399
+				station->stationkey[0]=0;
400
+			station->stationkey[TwoFish_KEY_LENGTH]=0; /* make sure it's terminated. (damn strncpy...) */
401
+
402
+			strcpy(station->initialkey,station->stationkey);
403
+			station->stationfish=TwoFishInit(station->stationkey);
404
+
405
+			station->localsocketaddr.sin_port=htons(0); /* let's use dynamic ports for now */
406
+			station->localsocketaddr.sin_addr.s_addr=0;
407
+			station->localsocketaddr.sin_family=AF_INET;
408
+			station->stationsocketaddr.sin_port=htons(station->stationport);
409
+			//station->stationsocketaddr.sin_addr=station->stationip;
410
+			station->stationsocketaddr.sin_addr.s_addr=station->stationip.ip32[0];
411
+			station->stationsocketaddr.sin_family=AF_INET; /* load all socket crap and keep for later */
412
+
413
+			do
414
+				station->myseqno=rand(); /* the seqno this host will use */
415
+			while(station->myseqno<20 || station->myseqno>65500);
416
+			station->mykeymod[0]=rand();
417
+			station->mykeymod[1]=rand();
418
+			station->mykeymod[2]=rand();
419
+			station->mykeymod[3]=rand();
420
+			station->stationseqno=0;				/* peer hasn't answered yet. */
421
+			
422
+			
423
+			if(!FWsamStationExists(station,FWsamStationList))	/* If we don't have the station already in global list....*/
424
+			{	if(FWsamCheckIn(station))			/* ...and we can talk to the agent...  */
425
+				{	if((newlistp=(FWsamList *)malloc(sizeof(FWsamList)))==NULL)
426
+						FatalError("ERROR => [Alert_FWsam](AlertFWsamInit) malloc failed for global newlistp!\n");
427
+					newlistp->station=station;
428
+					newlistp->next=NULL;
429
+					
430
+					if(!FWsamStationList)				/* ... add it to the global list/ */
431
+						FWsamStationList=newlistp;
432
+					else
433
+					{	listp=FWsamStationList;
434
+						while(listp->next)
435
+							listp=listp->next;
436
+						listp->next=newlistp;
437
+					}
438
+				}
439
+				else
440
+				{	TwoFishDestroy(station->stationfish); /* if not, we trash it. */
441
+					free(station);
442
+					station=NULL;
443
+				}
444
+			}
445
+#ifdef FWSAMDEBUG
446
+			else
447
+				LogMessage("DEBUG => [Alert_FWsam](AlertFWsamInit) Host %s:%i already in global list, skipping CheckIn.\n", sfip_ntoa(&station->stationip),station->stationport);
448
+#endif
449
+				
450
+			if(station)
451
+			{	if(!FWsamStationExists(station,fwsamlist))	/* If we don't have the station already in local list....*/
452
+				{	if((newlistp=(FWsamList *)malloc(sizeof(FWsamList)))==NULL)
453
+						FatalError("ERROR => [Alert_FWsam](AlertFWsamInit) malloc failed for local newlistp!\n");
454
+					newlistp->station=station;
455
+					newlistp->next=NULL;
456
+					
457
+					if(!fwsamlist)				/* ... add it to the local list/ */
458
+						fwsamlist=newlistp;
459
+					else
460
+					{	listp=fwsamlist;
461
+						while(listp->next)
462
+							listp=listp->next;
463
+						listp->next=newlistp;
464
+					}
465
+				}
466
+
467
+#ifdef FWSAMDEBUG
468
+				else
469
+					LogMessage("DEBUG => [Alert_FWsam](AlertFWsamInit) Host %s:%i already in local list, skipping.\n",sfip_ntoa(&station->stationip),station->stationport);
470
+				LogMessage("DEBUG => [Alert_FWsam](AlertFWsamInit) #%i: Host %s [%s] port %i password %s\n",++hostcnt,stathost,sfip_ntoa(&station->stationip),station->stationport,station->stationkey);
471
+#endif
472
+			}
473
+
474
+		}
475
+	}	/* next one */
476
+
477
+#ifdef FWSAMDEBUG
478
+    LogMessage("DEBUG => [Alert_FWsam](AlertFWsamInit) Linking fwsam alert function to call list...\n");
479
+#endif
480
+
481
+    /* Set the preprocessor function into the function list */
482
+    AddFuncToOutputList(AlertFWsam, NT_OUTPUT_ALERT, fwsamlist);
483
+    AddFuncToCleanExitList(AlertFWsamCleanExitFunc, fwsamlist);
484
+    AddFuncToRestartList(AlertFWsamRestartFunc, fwsamlist);
485
+}
486
+
487
+
488
+/*	This routine reads in a str from a file, snips white-spaces
489
+ *	off the front and back, removes comments, and pretties the
490
+ *	string. Returns true or false if a line was read or not.
491
+*/
492
+int FWsamReadLine(char *buf,unsigned long bufsize,FILE *fp)
493
+{	char *p;
494
+
495
+	if(fgets(buf,bufsize-1,fp))
496
+	{	buf[bufsize-1]=0;
497
+
498
+#ifdef FWSAMDEBUG_off
499
+		LogMessage("DEBUG => [Alert_FWsam](AlertFWsamReadLine) Line: %s\n",buf);
500
+#endif
501
+
502
+		p=buf;
503
+		while(isspace(*p))
504
+		  p++;
505
+		if(p>buf);
506
+			strcpy(buf,p);			
507
+		if(*buf)
508
+		{	p=buf+strlen(buf)-1;	/* remove leading and trailing spaces */
509
+			while(isspace(*p))
510
+				*p-- =0;
511
+		}
512
+		p=buf;
513
+		if(*p=='#' || *p==';')
514
+			*p=0;
515
+		else
516
+			p++;
517
+		while(*p)					/* remove inline comments (except escaped #'s and ;'s) */
518
+		{	if(*p=='#' || *p==';')
519
+			{	if(*(p-1)=='\\')
520
+					strcpy(p-1,p);
521
+				else
522
+					*p=0;
523
+			}
524
+			else
525
+				p++;
526
+		}
527
+		return TRUE;
528
+	}
529
+	return FALSE;
530
+}
531
+
532
+
533
+/* Parses the duration of the argument, recognizing minutes, hours, etc.. 
534
+*/
535
+unsigned long FWsamParseDuration(char *p)
536
+{	unsigned long dur=0,tdu;
537
+	char *tok,c1,c2;
538
+
539
+	while(*p)
540
+	{	tok=p;
541
+		while(*p && isdigit(*p))
542
+			p++;
543
+		if(*p)
544
+		{	c1=tolower(*p);
545
+			*p=0;
546
+			p++;
547
+			if(*p && !isdigit(*p))
548
+			{	c2=tolower(*p++);
549
+				while(*p && !isdigit(*p))
550
+					p++;
551
+			}
552
+			else
553
+				c2=0;
554
+			tdu=atol(tok);
555
+			switch(c1)
556
+			{	case 'm':	if(c2=='o')				/* month */
557
+								tdu*=(60*60*24*30);	/* use 30 days */
558
+							else
559
+								tdu*=60;			/* minutes */
560
+				case 's':	break;					/* seconds */
561
+				case 'h':	tdu*=(60*60);			/* hours */
562
+							break;
563
+				case 'd':	tdu*=(60*60*24);		/* days */
564
+							break;
565
+				case 'w':	tdu*=(60*60*24*7);		/* week */
566
+							break;
567
+				case 'y':	tdu*=(60*60*24*365);	/* year */
568
+							break;
569
+			}
570
+			dur+=tdu;
571
+		}
572
+		else
573
+			dur+=atol(tok);
574
+	}
575
+
576
+	return dur;
577
+}
578
+
579
+
580
+/*  This routine parses an option line. It is called by FWsamParseLine,
581
+ *  which parses the sid-block.map file, and also by AlertFWsamOptionInit,
582
+ *  which is called by Snort when processing fwsam: options in rules.
583
+ *  It returns TRUE it there is a possible option problem, otherwise FALSE.
584
+*/
585
+int FWsamParseOption(FWsamOptions *optp,char *ap)
586
+{   int possprob=FALSE;
587
+
588
+	/* set defaults */
589
+
590
+	optp->duration=300;					/* default of 5 minute block */
591
+	optp->how=FWSAM_HOW_INOUT;			/* inbound and outbound block */
592
+	optp->who=FWSAM_WHO_SRC;			/* the source  */
593
+    optp->loglevel=FWSAM_LOG_LONGALERT; /* the log level default */
594
+	/* parse the fwsam keywords */
595
+
596
+#ifdef FWSAMDEBUG
597
+	LogMessage("DEBUG => [Alert_FWsam](AlertFWamOptionInit) Parse Options Args: %s\n",ap);
598
+#endif
599
+
600
+	if(*ap)		/* should be dst/src (the WHO) or duration */
601
+	{	if(isdigit(*ap))
602
+			optp->duration=FWsamParseDuration(ap);
603
+		else 
604
+		{	switch(*ap)			/* yeah, we're lazy and check only the first character */
605
+			{	case 'p':	;								/* permanent, perm */
606
+				case 'f':	;								/* forever */
607
+				case 'i':	optp->duration=0;				/* infinite, inf */
608
+							break;
609
+				case 'd':	optp->who=FWSAM_WHO_DST;		/* destination, dest, dst */
610
+							break;
611
+				case 's':	optp->who=FWSAM_WHO_SRC;		/* source, src */
612
+							break;
613
+				default:	possprob=TRUE;
614
+			}
615
+			while(*ap && *ap!=',' && *ap!='[')
616
+				ap++;
617
+			if(*ap=='[')  
618
+			{	ap++;		/* now we have the HOW */
619
+				switch(*ap)
620
+				{	case 'i':	;							/* in */
621
+					case 's':	optp->how=FWSAM_HOW_IN;		/* source, src */
622
+								break;
623
+					case 'o':	;							/* out */
624
+					case 'd':	optp->how=FWSAM_HOW_OUT;	/* destination, dest, dst */
625
+								break;
626
+					case 'b':	;							/* both */
627
+					case 'e':	optp->how=FWSAM_HOW_INOUT;	/* either */
628
+								break;
629
+					case 't':	;							/* this */
630
+					case 'c':	optp->how=FWSAM_HOW_THIS;	/* connection, conn */
631
+								break;
632
+					default:	possprob=TRUE;
633
+				}
634
+				while(*ap && *ap!=',')
635
+					ap++;
636
+			}
637
+			if(*ap==',')
638
+			{	ap++;  
639
+				if(isdigit(*ap))  /* and figure out how long to block */
640
+					optp->duration=FWsamParseDuration(ap);
641
+				else if(*ap=='p' || *ap=='f' || *ap=='i')
642
+					optp->duration=0;
643
+				else
644
+					possprob=TRUE;
645
+			}
646
+			else if(!*ap)
647
+				possprob=TRUE;
648
+		}
649
+	}
650
+	else
651
+		possprob=TRUE;
652
+
653
+	return possprob;
654
+}
655
+
656
+
657
+/*	This goes through the lines of sid-block.map and sets the 
658
+ *	options for fwsam if the file is being used.
659
+*/
660
+void FWsamParseLine(FWsamOptions *optp,char *buf)
661
+{   char *ap;
662
+
663
+	ap=buf; /* start at the beginning of the argument */
664
+
665
+	while(*ap)
666
+	{	if(isspace(*ap))		/* normalize spaces (tabs into space, etc) */
667
+			*ap=' ';
668
+		if(isupper(*ap))		/* and set to lower case */
669
+			*ap=tolower(*ap);
670
+		ap++;
671
+	}
672
+	while((ap=strrchr(buf,' '))!=NULL)	/* remove spaces */
673
+		strcpy(ap,ap+1);
674
+
675
+	ap=buf;
676
+	if(*ap)
677
+	{	while(*ap && *ap!=':' && *ap!='|')
678
+			ap++;
679
+		*ap++ =0;
680
+		while(*ap && (*ap==':' || *ap=='|'))
681
+			ap++;
682
+
683
+		optp->sid=(unsigned long)atol(buf);
684
+
685
+		if(FWsamParseOption(optp,ap))
686
+			LogMessage("WARNING %s (%d) => [Alert_FWsam](AlertFWamOptionInit) Possible option problem. Using %s[%s],%lu.\n",file_name,file_line,(optp->who==FWSAM_WHO_SRC)?"src":"dst",(optp->how==FWSAM_HOW_IN)?"in":((optp->how==FWSAM_HOW_OUT)?"out":"either"),optp->duration);
687
+	}
688
+	else
689
+		optp->sid=0;
690
+}
691
+
692
+
693
+
694
+/*
695
+ * Function: AlertFWsamOptionInit(char *data, OptTreeNode *otn, int protocol)
696
+ *
697
+ * Purpose: Parses each rule and sets the option flags in the tree.
698
+ *
699
+ * Arguments: args => ptr to argument string
700
+ *
701
+ * Returns: void function
702
+ *
703
+*/
704
+void AlertFWsamOptionInit(char *args,OptTreeNode *otn,int protocol)
705
+{
706
+    FWsamOptions *optp;
707
+	char *ap;
708
+
709
+
710
+#ifdef FWSAMDEBUG
711
+    LogMessage("DEBUG => [Alert_FWsam](AlertFWamOptionInit) FWsamOptionInit is parsing...\n");
712
+#endif
713
+
714
+    if((optp=(FWsamOptions *)malloc(sizeof(FWsamOptions)))==NULL)
715
+		FatalError("ERROR => [Alert_FWsam](AlertFWamOptionInit) malloc failed for opt!\n");
716
+
717
+
718
+	ap=args; /* start at the beginning of the argument */
719
+
720
+	while(*ap)
721
+	{	if(isspace(*ap))		/* normalize spaces (tabs into space, etc) */
722
+			*ap=' ';
723
+		if(isupper(*ap))		/* and set to lower case */
724
+			*ap=tolower(*ap);
725
+		ap++;
726
+	}
727
+	while((ap=strrchr(args,' '))!=NULL)	/* remove spaces */
728
+		strcpy(ap,ap+1);
729
+
730
+	
731
+	if(FWsamParseOption(optp,args))
732
+		LogMessage("WARNING %s (%d) => [Alert_FWsam](AlertFWamOptionInit) Possible option problem. Using %s[%s],%lu.\n",file_name,file_line,(optp->who==FWSAM_WHO_SRC)?"src":"dst",(optp->how==FWSAM_HOW_IN)?"in":((optp->how==FWSAM_HOW_OUT)?"out":"either"),optp->duration);
733
+
734
+	otn->ds_list[PLUGIN_FWSAM]=(FWsamOptions *)optp;
735
+}
736
+
737
+
738
+/* Generates a new encryption key for TwoFish based on seq numbers and a random that
739
+ * the SnortSam agents send on checkin (in protocol)
740
+*/
741
+void FWsamNewStationKey(FWsamStation *station,FWsamPacket *packet)
742
+{
743
+    //unsigned char newkey[TwoFish_KEY_LENGTH+2];
744
+    char newkey[TwoFish_KEY_LENGTH+2];
745
+	int i;
746
+
747
+	newkey[0]=packet->snortseqno[0];		/* current snort seq # (which both know) */
748
+	newkey[1]=packet->snortseqno[1];			
749
+	newkey[2]=packet->fwseqno[0];			/* current SnortSam seq # (which both know) */
750
+	newkey[3]=packet->fwseqno[1];
751
+	newkey[4]=packet->protocol[0];		/* the random SnortSam chose */
752
+	newkey[5]=packet->protocol[1];
753
+
754
+	strncpy(newkey+6,station->stationkey,TwoFish_KEY_LENGTH-6); /* append old key */
755
+	newkey[TwoFish_KEY_LENGTH]=0;
756
+
757
+	newkey[0]^=station->mykeymod[0];		/* modify key with key modifiers which were */
758
+	newkey[1]^=station->mykeymod[1];		/* exchanged during the check-in handshake. */
759
+	newkey[2]^=station->mykeymod[2];
760
+	newkey[3]^=station->mykeymod[3];
761
+	newkey[4]^=station->fwkeymod[0];
762
+	newkey[5]^=station->fwkeymod[1];
763
+	newkey[6]^=station->fwkeymod[2];
764
+	newkey[7]^=station->fwkeymod[3];
765
+
766
+	for(i=0;i<=7;i++)
767
+		if(newkey[i]==0)
768
+			newkey[i]++;
769
+
770
+	strcpy(station->stationkey,newkey);
771
+	TwoFishDestroy(station->stationfish);
772
+	station->stationfish=TwoFishInit(newkey);
773
+}
774
+
775
+
776
+/*	This routine will search the option list as defined
777
+ *	by the sid-block.map file and return a pointer
778
+ *	to the matching record.
779
+*/
780
+FWsamOptions *FWsamGetOption(unsigned long sid)
781
+{	signed long i,step,diff,o,o2;
782
+
783
+#ifdef FWSAM_FANCYFETCH       /* Fancy-fetch jumps in decreasing n/2 steps and takes much less lookups */
784
+	o=o2= -1;
785
+	i=step=FWsamMaxOptions>>1;
786
+	while(i>=0 && i<FWsamMaxOptions && i!=o2)
787
+	{	diff=sid-FWsamOptionField[i].sid;
788
+		if(!diff)
789
+			return &(FWsamOptionField[i]);
790
+		if(step>1)
791
+			step=step>>1;
792
+		o2=o;
793
+		o=i;
794
+		if(diff>0)
795
+			i+=step;
796
+		else
797
+			i-=step;
798
+	}
799
+#else						/* This is just a sequential list lookup */
800
+	for(i=0;i<FWsamMaxOptions;i++)
801
+		if(FWsamOptionField[i].sid==sid)
802
+			return &(FWsamOptionField[i]);
803
+#endif
804
+	return NULL;
805
+}
806
+
807
+
808
+/****************************************************************************
809
+ *
810
+ * Function: AlertFWsam(Packet *, char *)
811
+ *
812
+ * Purpose: Send the current alert to a remote module on a FW-1 mgmt station
813
+ *
814
+ * Arguments: p => pointer to the packet data struct
815
+ *            msg => the message to print in the alert
816
+ *
817
+ * Returns: void function
818
+ *
819
+ ***************************************************************************/
820
+void AlertFWsam(Packet *p, char *msg, void *arg, Event *event)
821
+{	FWsamOptions *optp;
822
+	FWsamPacket sampacket;
823
+	FWsamStation *station=NULL;
824
+	FWsamList *fwsamlist;
825
+	SOCKET stationsocket;
826
+	int i,len,deletestation,stationtry=0;
827
+	//unsigned char *encbuf,*decbuf;
828
+    char *encbuf,*decbuf;
829
+	static unsigned long lastbsip[FWSAM_REPET_BLOCKS],lastbdip[FWSAM_REPET_BLOCKS],
830
+						 lastbduration[FWSAM_REPET_BLOCKS],lastbtime[FWSAM_REPET_BLOCKS];
831
+	static unsigned short lastbsp[FWSAM_REPET_BLOCKS],lastbdp[FWSAM_REPET_BLOCKS],
832
+						  lastbproto[FWSAM_REPET_BLOCKS],lastbpointer;
833
+	static unsigned char lastbmode[FWSAM_REPET_BLOCKS];
834
+	static unsigned long btime=0;
835
+
836
+
837
+	if(otn_tmp==NULL)
838
+    {
839
+#ifdef FWSAMDEBUG
840
+        LogMessage("DEBUG => [Alert_FWsam] NULL otn_tmp!\n");
841
+#endif
842
+        return;
843
+    }
844
+    if(p == NULL)
845
+    {
846
+#ifdef FWSAMDEBUG
847
+        LogMessage("DEBUG => [Alert_FWsam] NULL packet!\n");
848
+#endif
849
+        return;
850
+    }
851
+    if(arg == NULL)
852
+    {
853
+#ifdef FWSAMDEBUG
854
+        LogMessage("DEBUG => [Alert_FWsam] NULL arg!\n");
855
+#endif
856
+        return;
857
+    }
858
+
859
+    /* SnortSam does no IPv6 */
860
+    if (!IS_IP4(p)) {
861
+#ifdef FWSAMDEBUG
862
+        LogMessage("DEBUG => [Alert_FWsam] not acting on non-IP4 packet!\n");
863
+#endif
864
+        return;
865
+    }
866
+
867
+	optp=NULL;
868
+
869
+	if(FWsamOptionField)            /* If using the file (field present), let's use that */
870
+		optp=FWsamGetOption(event->sig_id);
871
+
872
+	if(!optp)                       /* If file not present, check if an fwsam option was defined on the triggering rule */
873
+		optp=otn_tmp->ds_list[PLUGIN_FWSAM];
874
+
875
+	if(optp)	/* if options specified for this rule */
876
+	{	if(!btime)			/* if this is the first time this function is */
877
+		{	for(i=0;i<FWSAM_REPET_BLOCKS;i++)	/*  called, reset the time and protocol to 0. */
878
+			{	lastbproto[i]=0;
879
+				lastbtime[i]=0;
880
+			}
881
+		}	
882
+	
883
+		fwsamlist=(FWsamList *)arg;
884
+
885
+#ifdef FWSAMDEBUG
886
+		LogMessage("DEBUG => [Alert_FWsam] Alert -> Msg=\"%s\"\n",msg);
887
+
888
+		LogMessage("DEBUG => [Alert_FWsam] Alert -> Option: %s[%s],%lu.\n",(optp->who==FWSAM_WHO_SRC)?"src":"dst",(optp->how==FWSAM_HOW_IN)?"in":((optp->how==FWSAM_HOW_OUT)?"out":"either"),optp->duration);
889
+#endif 
890
+
891
+		len=TRUE;
892
+		btime=(unsigned long)time(NULL);	/* get current time */
893
+		/* This is a cheap check to see if the blocking request matches any of the previous requests. */
894
+		for(i=0;i<FWSAM_REPET_BLOCKS && len;i++) 
895
+		{	if( ((optp->how==FWSAM_HOW_THIS)?	/* if blocking mode SERVICE, check for src and dst	  */
896
+				( lastbsip[i]==p->iph->ip_src.s_addr && lastbdip[i]==p->iph->ip_dst.s_addr &&lastbproto[i]==p->iph->ip_proto &&
897
+				  ((p->iph->ip_proto==IPPROTO_TCP || p->iph->ip_proto==IPPROTO_UDP)? /* check port only of TCP or UDP */
898
+/*					((optp->who==FWSAM_WHO_SRC)?(lastbsp[i]==p->sp):(lastbdp[i]==p->dp)):TRUE) ): */
899
+					lastbdp[i]==p->dp:TRUE) ):
900
+				((optp->who==FWSAM_WHO_SRC)?(lastbsip[i]==p->iph->ip_src.s_addr):(lastbdip[i]==p->iph->ip_dst.s_addr))) && /* otherwise if we block source, only compare source. Same for dest. */
901
+				lastbduration[i]==optp->duration &&
902
+				(lastbmode[i]&(FWSAM_HOW|FWSAM_WHO))==(optp->how|optp->who) &&
903
+				(btime-lastbtime[i]<((optp->duration>FWSAM_REPET_TIME)?FWSAM_REPET_TIME:optp->duration)))
904
+			{	len=FALSE;		/* If so, we don't need to block again. */
905
+			}
906
+		}
907
+		if(len)
908
+		{	if(++lastbpointer>=FWSAM_REPET_BLOCKS)		/* increase repetitive check pointer */
909
+				lastbpointer=0;
910
+			lastbsip[lastbpointer]=p->iph->ip_src.s_addr;		/* and note packet details */
911
+			lastbdip[lastbpointer]=p->iph->ip_dst.s_addr;
912
+			lastbduration[lastbpointer]=optp->duration;	
913
+			lastbmode[lastbpointer]=optp->how|optp->who|optp->loglevel; 
914
+			lastbproto[lastbpointer]=p->iph->ip_proto;			
915
+			if(p->iph->ip_proto==IPPROTO_TCP || p->iph->ip_proto==IPPROTO_UDP)
916
+			{	lastbsp[lastbpointer]=p->sp;					/* set ports if TCP or UDP */
917
+				lastbdp[lastbpointer]=p->dp;
918
+			}
919
+			lastbtime[lastbpointer]=btime;
920
+
921
+
922
+			while(fwsamlist!=NULL)
923
+			{	station=fwsamlist->station;
924
+				//if(station->stationip.s_addr)
925
+				if(station->stationip.ip32[0])
926
+				{	deletestation=FALSE;
927
+					stationtry++;				/* first try */
928
+					/* create a socket for the station */
929
+					stationsocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); 
930
+					if(stationsocket==INVALID_SOCKET)
931
+						FatalError("ERROR => [Alert_FWsam] Funky socket error (socket)!\n");
932
+					if(bind(stationsocket,(struct sockaddr *)&(station->localsocketaddr),sizeof(struct sockaddr)))
933
+						FatalError("ERROR => [Alert_FWsam] Could not bind socket!\n");
934
+	
935
+					/* let's connect to the agent */
936
+					if(connect(stationsocket,(struct sockaddr *)&station->stationsocketaddr,sizeof(struct sockaddr)))
937
+					{
938
+						LogMessage("WARNING => [Alert_FWsam] Could not send block to host %s. Will try later.\n",sfip_ntoa(&station->stationip));
939
+#ifdef WIN32
940
+						closesocket(stationsocket);
941
+#else
942
+						close(stationsocket);
943
+#endif
944
+						stationtry=0;
945
+					}
946
+					else
947
+					{	
948
+#ifdef FWSAMDEBUG
949
+						LogMessage("DEBUG => [Alert_FWsam] Connected to host %s.\n",sfip_ntoa(&station->stationip));
950
+#endif
951
+						/* now build the packet */
952
+						station->myseqno+=station->stationseqno; /* increase my seqno by adding agent seq no */
953
+						sampacket.endiancheck=1;						/* This is an endian indicator for Snortsam */
954
+						sampacket.snortseqno[0]=(char)station->myseqno;
955
+						sampacket.snortseqno[1]=(char)(station->myseqno>>8);
956
+						sampacket.fwseqno[0]=(char)station->stationseqno;/* fill station seqno */
957
+						sampacket.fwseqno[1]=(char)(station->stationseqno>>8);	
958
+						sampacket.status=FWSAM_STATUS_BLOCK;			/* set block mode */
959
+						sampacket.version=FWSAM_PACKETVERSION;			/* set packet version */
960
+						sampacket.duration[0]=(char)optp->duration;		/* set duration */
961
+						sampacket.duration[1]=(char)(optp->duration>>8);
962
+						sampacket.duration[2]=(char)(optp->duration>>16);
963
+						sampacket.duration[3]=(char)(optp->duration>>24);
964
+						sampacket.fwmode=optp->how|optp->who|optp->loglevel; /* set the mode */
965
+						sampacket.dstip[0]=(char)p->iph->ip_dst.s_addr; /* destination IP */
966
+						sampacket.dstip[1]=(char)(p->iph->ip_dst.s_addr>>8);
967
+						sampacket.dstip[2]=(char)(p->iph->ip_dst.s_addr>>16);
968
+						sampacket.dstip[3]=(char)(p->iph->ip_dst.s_addr>>24);
969
+						sampacket.srcip[0]=(char)p->iph->ip_src.s_addr;	/* source IP */
970
+						sampacket.srcip[1]=(char)(p->iph->ip_src.s_addr>>8);
971
+						sampacket.srcip[2]=(char)(p->iph->ip_src.s_addr>>16);
972
+						sampacket.srcip[3]=(char)(p->iph->ip_src.s_addr>>24);
973
+						sampacket.protocol[0]=(char)p->iph->ip_proto;	/* protocol */
974
+						sampacket.protocol[1]=(char)(p->iph->ip_proto>>8);/* protocol */
975
+
976
+						if(p->iph->ip_proto==IPPROTO_TCP || p->iph->ip_proto==IPPROTO_UDP)
977
+						{	sampacket.srcport[0]=(char)p->sp;	/* set ports */
978
+							sampacket.srcport[1]=(char)(p->sp>>8);
979
+							sampacket.dstport[0]=(char)p->dp;
980
+							sampacket.dstport[1]=(char)(p->dp>>8);
981
+						} 
982
+						else
983
+							sampacket.srcport[0]=sampacket.srcport[1]=sampacket.dstport[0]=sampacket.dstport[1]=0;
984
+
985
+						sampacket.sig_id[0]=(char)event->sig_id;		/* set signature ID */
986
+						sampacket.sig_id[1]=(char)(event->sig_id>>8);
987
+						sampacket.sig_id[2]=(char)(event->sig_id>>16);
988
+						sampacket.sig_id[3]=(char)(event->sig_id>>24);
989
+
990
+#ifdef FWSAMDEBUG
991
+						LogMessage("DEBUG => [Alert_FWsam] Sending BLOCK\n");
992
+						LogMessage("DEBUG => [Alert_FWsam] Snort SeqNo:  %x\n",station->myseqno);
993
+						LogMessage("DEBUG => [Alert_FWsam] Mgmt SeqNo :  %x\n",station->stationseqno);
994
+						LogMessage("DEBUG => [Alert_FWsam] Status     :  %i\n",FWSAM_STATUS_BLOCK);
995
+						LogMessage("DEBUG => [Alert_FWsam] Mode       :  %i\n",optp->how|optp->who|optp->loglevel);
996
+						LogMessage("DEBUG => [Alert_FWsam] Duration   :  %li\n",optp->duration);
997
+						LogMessage("DEBUG => [Alert_FWsam] Protocol   :  %i\n",GET_IPH_PROTO(p));
998
+#ifdef SUP_IP6
999
+						LogMessage("DEBUG => [Alert_FWsam] Src IP     :  %s\n",sfip_ntoa(GET_SRC_IP(p)));
1000
+						LogMessage("DEBUG => [Alert_FWsam] Dest IP    :  %s\n",sfip_ntoa(GET_DST_IP(p)));
1001
+#else
1002
+						LogMessage("DEBUG => [Alert_FWsam] Src IP     :  %s\n",inet_ntoa(p->iph->ip_src));
1003
+						LogMessage("DEBUG => [Alert_FWsam] Dest IP    :  %s\n",inet_ntoa(p->iph->ip_dst));
1004
+#endif
1005
+						LogMessage("DEBUG => [Alert_FWsam] Src Port   :  %i\n",p->sp);
1006
+						LogMessage("DEBUG => [Alert_FWsam] Dest Port  :  %i\n",p->dp);
1007
+						LogMessage("DEBUG => [Alert_FWsam] Sig_ID     :  %lu\n",event->sig_id);
1008
+
1009
+#endif
1010
+
1011
+						encbuf=TwoFishAlloc(sizeof(FWsamPacket),FALSE,FALSE,station->stationfish); /* get the encryption buffer */
1012
+						len=TwoFishEncrypt((char *)&sampacket,&encbuf,sizeof(FWsamPacket),FALSE,station->stationfish); /* encrypt the packet with current key */
1013
+
1014
+						if(send(stationsocket,encbuf,len,0)!=len) /* weird...could not send */
1015
+						{	LogMessage("WARNING => [Alert_FWsam] Could not send to host %s. Will try again later.\n",sfip_ntoa(&station->stationip));
1016
+#ifdef WIN32
1017
+							closesocket(stationsocket);
1018
+#else
1019
+							close(stationsocket);
1020
+#endif
1021
+							stationtry=0;
1022
+						}
1023
+						else
1024
+						{	i=FWSAM_NETWAIT;
1025
+#ifdef WIN32
1026
+							ioctlsocket(stationsocket,FIONBIO,&i);	/* set non blocking and wait for  */
1027
+#else
1028
+							ioctl(stationsocket,FIONBIO,&i);		/* set non blocking and wait for  */
1029
+#endif
1030
+							while(i-- >1)							/* the response packet	 */
1031
+							{	waitms(10); /* wait for response (default maximum 3 secs */
1032
+								if(recv(stationsocket,encbuf,len,0)==len)
1033
+									i=0; /* if we received packet we set the counter to 0. */
1034
+										 /* by the time we check with if, it's already dec'ed to -1 */
1035
+							}
1036
+							if(!i) /* id we timed out (i was one, then dec'ed)... */
1037
+							{	LogMessage("WARNING => [Alert_FWsam] Did not receive response from host %s. Will try again later.\n",sfip_ntoa(&station->stationip));
1038
+#ifdef WIN32
1039
+								closesocket(stationsocket);
1040
+#else
1041
+								close(stationsocket);
1042
+#endif
1043
+								stationtry=0;
1044
+							}
1045
+							else /* got a packet */
1046
+							{	decbuf=(char *)&sampacket; /* get the pointer to the packet struct */
1047
+								len=TwoFishDecrypt(encbuf,&decbuf,sizeof(FWsamPacket)+TwoFish_BLOCK_SIZE,FALSE,station->stationfish); /* try to decrypt the packet with current key */
1048
+
1049
+								if(len!=sizeof(FWsamPacket)) /* invalid decryption */
1050
+								{	strcpy(station->stationkey,station->initialkey); /* try the intial key */
1051
+									TwoFishDestroy(station->stationfish);
1052
+									station->stationfish=TwoFishInit(station->stationkey); /* re-initialize the TwoFish with the intial key */
1053
+									len=TwoFishDecrypt(encbuf,&decbuf,sizeof(FWsamPacket)+TwoFish_BLOCK_SIZE,FALSE,station->stationfish); /* try again to decrypt */
1054
+									LogMessage("INFO => [Alert_FWsam] Had to use initial key!\n");
1055
+								}
1056
+								if(len==sizeof(FWsamPacket)) /* valid decryption */
1057
+								{	if(sampacket.version==FWSAM_PACKETVERSION)/* master speaks my language */
1058
+									{	if(sampacket.status==FWSAM_STATUS_OK || sampacket.status==FWSAM_STATUS_NEWKEY 
1059
+										|| sampacket.status==FWSAM_STATUS_RESYNC || sampacket.status==FWSAM_STATUS_HOLD) 
1060
+										{	station->stationseqno=sampacket.fwseqno[0] | (sampacket.fwseqno[1]<<8); /* get stations seqno */
1061
+											station->lastcontact=(unsigned long)time(NULL); /* set the last contact time (not used yet) */
1062
+#ifdef FWSAMDEBUG
1063
+									LogMessage("DEBUG => [Alert_FWsam] Received %s\n",sampacket.status==FWSAM_STATUS_OK?"OK":
1064
+																		   sampacket.status==FWSAM_STATUS_NEWKEY?"NEWKEY":
1065
+																		   sampacket.status==FWSAM_STATUS_RESYNC?"RESYNC":
1066
+																		   sampacket.status==FWSAM_STATUS_HOLD?"HOLD":"ERROR");
1067
+									LogMessage("DEBUG => [Alert_FWsam] Snort SeqNo:  %x\n",sampacket.snortseqno[0]|(sampacket.snortseqno[1]<<8));
1068
+									LogMessage("DEBUG => [Alert_FWsam] Mgmt SeqNo :  %x\n",station->stationseqno);
1069
+									LogMessage("DEBUG => [Alert_FWsam] Status     :  %i\n",sampacket.status);
1070
+									LogMessage("DEBUG => [Alert_FWsam] Version    :  %i\n",sampacket.version);
1071
+#endif
1072
+											if(sampacket.status==FWSAM_STATUS_HOLD)
1073
+											{	i=FWSAM_NETHOLD;			/* Stay on hold for a maximum of 60 secs (default) */
1074
+												while(i-- >1)							/* the response packet	 */
1075
+												{	waitms(10); /* wait for response  */
1076
+													if(recv(stationsocket,encbuf,sizeof(FWsamPacket)+TwoFish_BLOCK_SIZE,0)==sizeof(FWsamPacket)+TwoFish_BLOCK_SIZE)
1077
+													  i=0; /* if we received packet we set the counter to 0. */
1078
+										 		}
1079
+												if(!i) /* id we timed out (i was one, then dec'ed)... */
1080
+												{	LogMessage("WARNING => [Alert_FWsam] Did not receive response from host %s. Will try again later.\n",sfip_ntoa(&station->stationip));
1081
+													stationtry=0;
1082
+													sampacket.status=FWSAM_STATUS_ERROR;
1083
+												}
1084
+												else /* got a packet */
1085
+												{	decbuf=(char *)&sampacket; /* get the pointer to the packet struct */
1086
+													len=TwoFishDecrypt(encbuf,&decbuf,sizeof(FWsamPacket)+TwoFish_BLOCK_SIZE,FALSE,station->stationfish); /* try to decrypt the packet with current key */
1087
+
1088
+													if(len!=sizeof(FWsamPacket)) /* invalid decryption */
1089
+													{	strcpy(station->stationkey,station->initialkey); /* try the intial key */
1090
+														TwoFishDestroy(station->stationfish);
1091
+														station->stationfish=TwoFishInit(station->stationkey); /* re-initialize the TwoFish with the intial key */
1092
+														len=TwoFishDecrypt(encbuf,&decbuf,sizeof(FWsamPacket)+TwoFish_BLOCK_SIZE,FALSE,station->stationfish); /* try again to decrypt */
1093
+														LogMessage("INFO => [Alert_FWsam] Had to use initial key again!\n");
1094
+													}
1095
+#ifdef FWSAMDEBUG
1096
+									LogMessage("DEBUG => [Alert_FWsam] Received %s\n",sampacket.status==FWSAM_STATUS_OK?"OK":
1097
+																		   sampacket.status==FWSAM_STATUS_NEWKEY?"NEWKEY":
1098
+																		   sampacket.status==FWSAM_STATUS_RESYNC?"RESYNC":
1099
+																		   sampacket.status==FWSAM_STATUS_HOLD?"HOLD":"ERROR");
1100
+									LogMessage("DEBUG => [Alert_FWsam] Snort SeqNo:  %x\n",sampacket.snortseqno[0]|(sampacket.snortseqno[1]<<8));
1101
+									LogMessage("DEBUG => [Alert_FWsam] Mgmt SeqNo :  %x\n",station->stationseqno);
1102
+									LogMessage("DEBUG => [Alert_FWsam] Status     :  %i\n",sampacket.status);
1103
+									LogMessage("DEBUG => [Alert_FWsam] Version    :  %i\n",sampacket.version);
1104
+#endif
1105
+													if(len!=sizeof(FWsamPacket)) /* invalid decryption */
1106
+													{	ErrorMessage("ERROR => [Alert_FWsam] Password mismatch! Ignoring host %s.\n",sfip_ntoa(&station->stationip));
1107
+														deletestation=TRUE;
1108
+														sampacket.status=FWSAM_STATUS_ERROR;
1109
+													}
1110
+													else if(sampacket.version!=FWSAM_PACKETVERSION) /* invalid protocol version */
1111
+													{	ErrorMessage("ERROR => [Alert_FWsam] Protocol version error! Ignoring host %s.\n",sfip_ntoa(&station->stationip));
1112
+														deletestation=TRUE;
1113
+														sampacket.status=FWSAM_STATUS_ERROR;
1114
+													}
1115
+													else if(sampacket.status!=FWSAM_STATUS_OK && sampacket.status!=FWSAM_STATUS_NEWKEY && sampacket.status!=FWSAM_STATUS_RESYNC) 
1116
+													{	ErrorMessage("ERROR => [Alert_FWsam] Funky handshake error! Ignoring host %s.\n",sfip_ntoa(&station->stationip));
1117
+														deletestation=TRUE;
1118
+														sampacket.status=FWSAM_STATUS_ERROR;
1119
+													}
1120
+												}
1121
+											}
1122
+											if(sampacket.status==FWSAM_STATUS_RESYNC)  /* if station want's to resync... */
1123
+											{	strcpy(station->stationkey,station->initialkey); /* ...we use the intial key... */
1124
+												memcpy(station->fwkeymod,sampacket.duration,4);	 /* and note the random key modifier */
1125
+											}
1126
+											if(sampacket.status==FWSAM_STATUS_NEWKEY || sampacket.status==FWSAM_STATUS_RESYNC)	
1127
+											{	
1128
+												FWsamNewStationKey(station,&sampacket); /* generate new TwoFish keys */
1129
+#ifdef FWSAMDEBUG
1130
+													LogMessage("DEBUG => [Alert_FWsam] Generated new encryption key...\n");
1131
+#endif
1132
+											}
1133
+#ifdef WIN32
1134
+											closesocket(stationsocket);
1135
+#else
1136
+											close(stationsocket);
1137
+#endif
1138
+											stationtry=0;
1139
+										}
1140
+										else if(sampacket.status==FWSAM_STATUS_ERROR) /* if SnortSam reports an error on second try, */
1141
+										{	
1142
+#ifdef WIN32
1143
+											closesocket(stationsocket);				  /* something is messed up and ... */
1144
+#else
1145
+											close(stationsocket);
1146
+#endif
1147
+											if(stationtry>1)						  /* we ignore that station. */
1148
+											{	deletestation=TRUE;					  /* flag for deletion */
1149
+												ErrorMessage("ERROR => [Alert_FWsam] Could not renegotiate key! Ignoring host %s.\n",sfip_ntoa(&station->stationip));
1150
+											}
1151
+											else							/* if we get an error on the first try, */
1152
+											{	if(!FWsamCheckIn(station))	/* we first try to check in again. */
1153
+												{	deletestation=TRUE;
1154
+													ErrorMessage("ERROR => [Alert_FWsam] Password mismatch! Ignoring host %s.\n",sfip_ntoa(&station->stationip));
1155
+												}
1156
+											}
1157
+										}
1158
+										else /* an unknown status means trouble... */
1159
+										{	ErrorMessage("ERROR => [Alert_FWsam] Funky handshake error! Ignoring host %s.\n",sfip_ntoa(&station->stationip));
1160
+#ifdef WIN32
1161
+											closesocket(stationsocket);
1162
+#else
1163
+											close(stationsocket);
1164
+#endif
1165
+											deletestation=TRUE;
1166
+										}
1167
+									}
1168
+									else   /* if the SnortSam agent uses a different packet version, we have no choice but to ignore it. */
1169
+									{	ErrorMessage("ERROR => [Alert_FWsam] Protocol version error! Ignoring host %s.\n",sfip_ntoa(&station->stationip));
1170
+#ifdef WIN32
1171
+										closesocket(stationsocket);
1172
+#else
1173
+										close(stationsocket);
1174
+#endif
1175
+										deletestation=TRUE;
1176
+									}
1177
+								}
1178
+								else /* if the intial key failed to decrypt as well, the keys are not configured the same, and we ignore that SnortSam station. */
1179
+								{	ErrorMessage("ERROR => [Alert_FWsam] Password mismatch! Ignoring host %s.\n",sfip_ntoa(&station->stationip));
1180
+#ifdef WIN32
1181
+									closesocket(stationsocket);
1182
+#else
1183
+									close(stationsocket);
1184
+#endif
1185
+									deletestation=TRUE;
1186
+								}
1187
+							}
1188
+						}
1189
+						free(encbuf); /* release of the TwoFishAlloc'ed encryption buffer */
1190
+					}
1191
+					if(stationtry==0 || deletestation)		/* if everything went real well, or real bad... */
1192
+					{	if(deletestation){					/* If it went bad, we remove the station from the list by marking the IP */
1193
+//							station->stationip.s_addr=0;
1194
+							station->stationip.ip32[0]=0;
1195
+                                                }
1196
+						fwsamlist=fwsamlist->next;
1197
+					}
1198
+				}
1199
+				else
1200
+					fwsamlist=fwsamlist->next;
1201
+			}
1202
+		}
1203
+		else
1204
+		{
1205
+#ifdef FWSAMDEBUG
1206
+			LogMessage("DEBUG => [Alert_FWsam] Skipping repetitive block.\n");
1207
+#endif
1208
+		}
1209
+	}
1210
+}
1211
+
1212
+/*  FWsamCheckOut will be called when Snort exists. It de-registeres this snort sensor 
1213
+ *  from the list of sensor that the SnortSam agent keeps. 
1214
+ */
1215
+void FWsamCheckOut(FWsamStation *station)
1216
+{	FWsamPacket sampacket;
1217
+	SOCKET stationsocket;
1218
+	int i,len;
1219
+    char *encbuf,*decbuf;
1220
+	//unsigned char *encbuf,*decbuf;
1221
+
1222
+
1223
+	stationsocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); 
1224
+	if(stationsocket==INVALID_SOCKET)
1225
+		FatalError("ERROR => [Alert_FWsam](FWsamCheckOut) Funky socket error (socket)!\n");
1226
+	if(bind(stationsocket,(struct sockaddr *)&(station->localsocketaddr),sizeof(struct sockaddr)))
1227
+		FatalError("ERROR => [Alert_FWsam](FWsamCheckOut) Could not bind socket!\n");
1228
+
1229
+	/* let's connect to the agent */
1230
+	if(!connect(stationsocket,(struct sockaddr *)&station->stationsocketaddr,sizeof(struct sockaddr)))
1231
+	{	LogMessage("INFO => [Alert_FWsam](FWsamCheckOut) Disconnecting from host %s.\n",sfip_ntoa(&station->stationip));
1232
+		/* now build the packet */
1233
+		station->myseqno+=station->stationseqno; /* increase my seqno */
1234
+		sampacket.endiancheck=1;
1235
+		sampacket.snortseqno[0]=(char)station->myseqno;
1236
+		sampacket.snortseqno[1]=(char)(station->myseqno>>8);
1237
+		sampacket.fwseqno[0]=(char)station->stationseqno; /* fill station seqno */
1238
+		sampacket.fwseqno[1]=(char)(station->stationseqno>>8);
1239
+		sampacket.status=FWSAM_STATUS_CHECKOUT;  /* checking out... */
1240
+		sampacket.version=FWSAM_PACKETVERSION;
1241
+
1242
+#ifdef FWSAMDEBUG
1243
+			LogMessage("DEBUG => [Alert_FWsam](FWsamCheckOut) Sending CHECKOUT\n");
1244
+			LogMessage("DEBUG => [Alert_FWsam](FWsamCheckOut) Snort SeqNo:  %x\n",station->myseqno);
1245
+			LogMessage("DEBUG => [Alert_FWsam](FWsamCheckOut) Mgmt SeqNo :  %x\n",station->stationseqno);
1246
+			LogMessage("DEBUG => [Alert_FWsam](FWsamCheckOut) Status     :  %i\n",sampacket.status);
1247
+
1248
+#endif
1249
+
1250
+		encbuf=TwoFishAlloc(sizeof(FWsamPacket),FALSE,FALSE,station->stationfish); /* get encryption buffer */
1251
+		len=TwoFishEncrypt((char *)&sampacket,&encbuf,sizeof(FWsamPacket),FALSE,station->stationfish); /* encrypt packet with current key */
1252
+
1253
+		if(send(stationsocket,encbuf,len,0)==len)
1254
+		{	i=FWSAM_NETWAIT;
1255
+#ifdef WIN32
1256
+			ioctlsocket(stationsocket,FIONBIO,&i);	/* set non blocking and wait for  */
1257
+#else
1258
+			ioctl(stationsocket,FIONBIO,&i);		/* set non blocking and wait for  */
1259
+#endif
1260
+			while(i-- >1)
1261
+			{	waitms(10);					/* ...wait a maximum of 3 secs for response... */
1262
+				if(recv(stationsocket,encbuf,len,0)==len) /* ... for the status packet */
1263
+					i=0;
1264
+			}
1265
+			if(i) /* if we got the packet */
1266
+			{	decbuf=(char *)&sampacket;
1267
+				len=TwoFishDecrypt(encbuf,&decbuf,sizeof(FWsamPacket)+TwoFish_BLOCK_SIZE,FALSE,station->stationfish);
1268
+
1269
+				if(len!=sizeof(FWsamPacket)) /* invalid decryption */
1270
+				{	strcpy(station->stationkey,station->initialkey); /* try initial key */
1271
+					TwoFishDestroy(station->stationfish);			 /* toss this fish */
1272
+					station->stationfish=TwoFishInit(station->stationkey); /* re-initialze TwoFish with initial key */
1273
+					len=TwoFishDecrypt(encbuf,&decbuf,sizeof(FWsamPacket)+TwoFish_BLOCK_SIZE,FALSE,station->stationfish); /* and try to decrypt again */
1274
+					LogMessage("INFO => [Alert_FWsam](FWsamCheckOut) Had to use initial key!\n");
1275
+				}
1276
+				if(len==sizeof(FWsamPacket)) /* valid decryption */
1277
+				{	if(sampacket.version!=FWSAM_PACKETVERSION) /* but don't really care since we are on the way out */
1278
+						ErrorMessage("WARNING => [Alert_FWsam](FWsamCheckOut) Protocol version error! What the hell, we're quitting anyway! :)\n");
1279
+				}
1280
+				else
1281
+					ErrorMessage("WARNING => [Alert_FWsam](FWsamCheckOut) Password mismatch! What the hell, we're quitting anyway! :)\n");
1282
+			}
1283
+		}
1284
+		free(encbuf); /* release TwoFishAlloc'ed buffer */
1285
+	}
1286
+	else
1287
+		LogMessage("WARNING => [Alert_FWsam] Could not connect to host %s for CheckOut. What the hell, we're quitting anyway! :)\n",sfip_ntoa(&station->stationip));
1288
+#ifdef WIN32
1289
+	closesocket(stationsocket);
1290
+#else
1291
+	close(stationsocket);
1292
+#endif
1293
+}
1294
+
1295
+
1296
+/*   FWSamFree: Disconnects all FW-1 management stations,
1297
+ *   closes sockets, and frees the structures.
1298
+ */
1299
+void FWsamFree(FWsamList *list)	
1300
+{
1301
+    FWsamList *next;
1302
+
1303
+    while(list)	/* Free pointer list for rule type */
1304
+    {
1305
+        next=list->next;
1306
+        free(list);
1307
+        list=next;
1308
+    }
1309
+    list=FWsamStationList;
1310
+
1311
+    while(list) /* Free global pointer list and stations */
1312
+    {
1313
+        next=list->next;
1314
+        if (list->station)
1315
+        {
1316
+            if(list->station->stationip.ip32[0])
1317
+            //if(list->station->stationip.s_addr)
1318
+                FWsamCheckOut(list->station); /* Send a Check-Out to SnortSam, */
1319
+
1320
+            TwoFishDestroy(list->station->stationfish);	/* toss the fish, */
1321
+            free(list->station); /* free station, */
1322
+        } 
1323
+        free(list); /* free pointer, */
1324
+        list=next; /* and move to next. */
1325
+    }
1326
+    FWsamStationList=NULL;
1327
+    if(FWsamOptionField)
1328
+        free(FWsamOptionField);
1329
+}
1330
+
1331
+void AlertFWsamCleanExitFunc(int signal, void *arg)
1332
+{	FWsamList *fwsamlist;
1333
+
1334
+#ifdef FWSAMDEBUG
1335
+    LogMessage("DEBUG => [Alert_FWsam](AlertFWsamCleanExitFunc) Exiting...\n");
1336
+#endif
1337
+
1338
+	fwsamlist=(FWsamList *)arg;
1339
+	FWsamFree(fwsamlist); /* Free all elements */
1340
+}
1341
+
1342
+void AlertFWsamRestartFunc(int signal, void *arg)
1343
+{	FWsamList *fwsamlist;
1344
+
1345
+#ifdef FWSAMDEBUG
1346
+    LogMessage("DEBUG => [Alert_FWsam](AlertFWsamRestartFunc) Restarting...\n");
1347
+#endif
1348
+
1349
+	fwsamlist=(FWsamList *)arg;
1350
+	FWsamFree(fwsamlist); /* Free all elements */
1351
+}
1352
+
1353
+/*  This routine registers this Snort sensor with SnortSam.
1354
+ *  It will also change the encryption key based on some variables.
1355
+ */
1356
+int FWsamCheckIn(FWsamStation *station)
1357
+{	int i,len,stationok=TRUE;
1358
+	FWsamPacket sampacket;
1359
+    char *encbuf,*decbuf;
1360
+	//unsigned char *encbuf,*decbuf;
1361
+	SOCKET stationsocket;
1362
+
1363
+
1364
+	/* create a socket for the station */
1365
+	stationsocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); 
1366
+	if(stationsocket==INVALID_SOCKET)
1367
+		FatalError("ERROR => [Alert_FWsam](FWsamCheckIn) Funky socket error (socket)!\n");
1368
+	if(bind(stationsocket,(struct sockaddr *)&(station->localsocketaddr),sizeof(struct sockaddr)))
1369
+		FatalError("ERROR => [Alert_FWsam](FWsamCheckIn) Could not bind socket!\n");
1370
+
1371
+	i=TRUE;
1372
+	/* let's connect to the agent */
1373
+	if(connect(stationsocket,(struct sockaddr *)&station->stationsocketaddr,sizeof(struct sockaddr)))
1374
+		LogMessage("WARNING => [Alert_FWsam](FWsamCheckIn) Could not connect to host %s. Will try later.\n",sfip_ntoa(&station->stationip));
1375
+	else
1376
+	{	LogMessage("INFO => [Alert_FWsam](FWsamCheckIn) Connected to host %s.\n",sfip_ntoa(&station->stationip));
1377
+		/* now build the packet */
1378
+		sampacket.endiancheck=1;
1379
+		sampacket.snortseqno[0]=(char)station->myseqno; /* fill my sequence number number */
1380
+		sampacket.snortseqno[1]=(char)(station->myseqno>>8); /* fill my sequence number number */
1381
+		sampacket.status=FWSAM_STATUS_CHECKIN; /* let's check in */
1382
+		sampacket.version=FWSAM_PACKETVERSION; /* set the packet version */
1383
+		memcpy(sampacket.duration,station->mykeymod,4);  /* we'll send SnortSam our key modifier in the duration slot */
1384
+											   /* (the checkin packet is just the plain initial key) */
1385
+#ifdef FWSAMDEBUG
1386
+			LogMessage("DEBUG => [Alert_FWsam](FWsamCheckIn) Sending CheckIn\n");
1387
+			LogMessage("DEBUG => [Alert_FWsam](FWsamCheckIn) Snort SeqNo:  %x\n",station->myseqno);
1388
+			LogMessage("DEBUG => [Alert_FWsam](FWsamCheckIn) Mode       :  %i\n",sampacket.status);
1389
+			LogMessage("DEBUG => [Alert_FWsam](FWsamCheckIn) Version    :  %i\n",sampacket.version);
1390
+#endif
1391
+		encbuf=TwoFishAlloc(sizeof(FWsamPacket),FALSE,FALSE,station->stationfish); /* get buffer for encryption */
1392
+		len=TwoFishEncrypt((char *)&sampacket,&encbuf,sizeof(FWsamPacket),FALSE,station->stationfish); /* encrypt with initial key */
1393
+		if(send(stationsocket,encbuf,len,0)!=len) /* weird...could not send */
1394
+			LogMessage("WARNING => [Alert_FWsam](FWsamCheckIn) Could not send to host %s. Will try again later.\n",sfip_ntoa(&station->stationip));
1395
+		else
1396
+		{	i=FWSAM_NETWAIT;
1397
+#ifdef WIN32
1398
+			ioctlsocket(stationsocket,FIONBIO,&i);	/* set non blocking and wait for  */
1399
+#else
1400
+			ioctl(stationsocket,FIONBIO,&i);		/* set non blocking and wait for  */
1401
+#endif
1402
+			while(i-- >1)
1403
+			{	waitms(10); /* wait a maximum of 3 secs for response */
1404
+				if(recv(stationsocket,encbuf,len,0)==len)
1405
+					i=0;
1406
+			}
1407
+			if(!i) /* time up? */
1408
+				LogMessage("WARNING => [Alert_FWsam](FWsamCheckIn) Did not receive response from host %s. Will try again later.\n",sfip_ntoa(&station->stationip));
1409
+			else
1410
+			{	decbuf=(char *)&sampacket; /* got status packet */
1411
+				len=TwoFishDecrypt(encbuf,&decbuf,sizeof(FWsamPacket)+TwoFish_BLOCK_SIZE,FALSE,station->stationfish); /* try to decrypt with initial key */
1412
+				if(len==sizeof(FWsamPacket)) /* valid decryption */
1413
+				{	
1414
+#ifdef FWSAMDEBUG
1415
+					LogMessage("DEBUG => [Alert_FWsam](FWsamCheckIn) Received %s\n",sampacket.status==FWSAM_STATUS_OK?"OK":
1416
+															   sampacket.status==FWSAM_STATUS_NEWKEY?"NEWKEY":
1417
+															   sampacket.status==FWSAM_STATUS_RESYNC?"RESYNC":
1418
+															   sampacket.status==FWSAM_STATUS_HOLD?"HOLD":"ERROR");
1419
+					LogMessage("DEBUG => [Alert_FWsam](FWsamCheckIn) Snort SeqNo:  %x\n",sampacket.snortseqno[0]|(sampacket.snortseqno[1]<<8));
1420
+					LogMessage("DEBUG => [Alert_FWsam](FWsamCheckIn) Mgmt SeqNo :  %x\n",sampacket.fwseqno[0]|(sampacket.fwseqno[1]<<8));
1421
+					LogMessage("DEBUG => [Alert_FWsam](FWsamCheckIn) Status     :  %i\n",sampacket.status);
1422
+					LogMessage("DEBUG => [Alert_FWsam](FWsamCheckIn) Version    :  %i\n",sampacket.version);
1423
+#endif
1424
+					if(sampacket.version==FWSAM_PACKETVERSION) /* master speaks my language */
1425
+					{	if(sampacket.status==FWSAM_STATUS_OK || sampacket.status==FWSAM_STATUS_NEWKEY || sampacket.status==FWSAM_STATUS_RESYNC) 
1426
+						{	station->stationseqno=sampacket.fwseqno[0]|(sampacket.fwseqno[1]<<8); /* get stations seqno */
1427
+							station->lastcontact=(unsigned long)time(NULL);
1428
+							
1429
+							if(sampacket.status==FWSAM_STATUS_NEWKEY || sampacket.status==FWSAM_STATUS_RESYNC)	/* generate new keys */
1430
+							{	memcpy(station->fwkeymod,sampacket.duration,4); /* note the key modifier */
1431
+								FWsamNewStationKey(station,&sampacket); /* and generate new TwoFish keys (with key modifiers) */
1432
+#ifdef FWSAMDEBUG
1433
+				LogMessage("DEBUG => [Alert_FWsam](FWsamCheckIn) Generated new encryption key...\n");
1434
+#endif
1435
+							}
1436
+						}
1437
+						else /* weird, got a strange status back */
1438
+						{	ErrorMessage("ERROR => [Alert_FWsam](FWsamCheckIn) Funky handshake error! Ignoring host %s.\n",sfip_ntoa(&station->stationip));
1439
+							stationok=FALSE;
1440
+						}
1441
+					}
1442
+					else /* packet version does not match */
1443
+					{	ErrorMessage("ERROR =>[Alert_FWsam](FWsamCheckIn) Protocol version error! Ignoring host %s.\n",sfip_ntoa(&station->stationip));
1444
+						stationok=FALSE;
1445
+					}
1446
+				}
1447
+				else /* key does not match */
1448
+				{	ErrorMessage("ERROR => [Alert_FWsam](FWsamCheckIn) Password mismatch! Ignoring host %s.\n",sfip_ntoa(&station->stationip));
1449
+					stationok=FALSE;
1450
+				}
1451
+			}
1452
+		}
1453
+		free(encbuf); /* release TwoFishAlloc'ed buffer */
1454
+	}
1455
+#ifdef WIN32
1456
+	closesocket(stationsocket);
1457
+#else
1458
+	close(stationsocket);
1459
+#endif
1460
+	return stationok;
1461
+}
1462
+#undef FWSAMDEBUG
1463
+
1464
diff -ruN snort-2.8.4.1.orig/src/output-plugins/spo_alert_fwsam.h snort-2.8.4.1/src/output-plugins/spo_alert_fwsam.h
1465
--- snort-2.8.4.1.orig/src/output-plugins/spo_alert_fwsam.h	1970-01-01 03:30:00.000000000 +0330
1466
+++ snort-2.8.4.1/src/output-plugins/spo_alert_fwsam.h	2009-06-23 16:40:44.000000000 +0430
1467
@@ -0,0 +1,216 @@
1468
+/* $Id: snortpatchb,v 1.5 2005/10/06 08:50:39 fknobbe Exp $
1469
+**
1470
+** spo_alert_fwsam.h
1471
+**
1472
+** Copyright (c) 2001-2004 Frank Knobbe <frank@knobbe.us>
1473
+**
1474
+** This program is free software; you can redistribute it and/or modify
1475
+** it under the terms of the GNU General Public License as published by
1476
+** the Free Software Foundation; either version 2 of the License, or
1477
+** (at your option) any later version.
1478
+**
1479
+** This program is distributed in the hope that it will be useful,
1480
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
1481
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1482
+** GNU General Public License for more details.
1483
+**
1484
+** You should have received a copy of the GNU General Public License
1485
+** along with this program; if not, write to the Free Software
1486
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1487
+*/
1488
+
1489
+/* This file gets included in plugbase.c when it is integrated into the rest 
1490
+ * of the program.
1491
+ *
1492
+ * For more info, see the beginning of spo_alert_fwsam.c
1493
+ *
1494
+ */
1495
+
1496
+#ifndef __SPO_FWSAM_H__
... This diff was truncated because it exceeds the maximum size that can be displayed.
(2-2/2)