dumpfile_alert.cc 3.25 KB
Newer Older
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
/*
    This file is part of Kismet

    Kismet 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.

    Kismet 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 Kismet; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "config.h"

#include "dumpfile_alert.h"

int dumpfilealert_chain_hook(CHAINCALL_PARMS) {
	Dumpfile_Alert *auxptr = (Dumpfile_Alert *) auxdata;
	return auxptr->chain_handler(in_pack);
}

Dumpfile_Alert::Dumpfile_Alert() {
	fprintf(stderr, "FATAL OOPS: Dumpfile_Alert called with no globalreg\n");
	exit(1);
}

Dumpfile_Alert::Dumpfile_Alert(GlobalRegistry *in_globalreg) : 
	Dumpfile(in_globalreg) {

	char errstr[STATUS_MAX];
	globalreg = in_globalreg;

	alertfile = NULL;

	type = "alert";

	if (globalreg->packetchain == NULL) {
		fprintf(stderr, "FATAL OOPS:  Packetchain missing before "
				"Dumpfile_Alert\n");
		exit(1);
	}

	if (globalreg->alertracker == NULL) {
		fprintf(stderr, "FATAL OOPS:  Alertracker missing before "
				"Dumpfile_Alert\n");
		exit(1);
	}

	// Find the file name
	if ((fname = ProcessConfigOpt("alert")) == "" || 
		globalreg->fatal_condition) {
		return;
	}

	alertfile = fopen(fname.c_str(), "w");
	if (alertfile == NULL) {
		snprintf(errstr, STATUS_MAX, "Failed to open alert dump file '%s': %s",
				 fname.c_str(), strerror(errno));
		_MSG(errstr, MSGFLAG_FATAL);
		globalreg->fatal_condition = 1;
		return;
	}

	_MSG("Opened alert log file '" + fname + "'", MSGFLAG_INFO);

	globalreg->packetchain->RegisterHandler(&dumpfilealert_chain_hook, this,
											CHAINPOS_LOGGING, -100);

	globalreg->RegisterDumpFile(this);
}

Dumpfile_Alert::~Dumpfile_Alert() {
	globalreg->packetchain->RemoveHandler(&dumpfilealert_chain_hook, 
										  CHAINPOS_LOGGING);
	
	// Close files
	if (alertfile != NULL) {
		Flush();
		fclose(alertfile);
	}

	alertfile = NULL;
}

int Dumpfile_Alert::Flush() {
	if (alertfile == NULL)
		return 0;

	fflush(alertfile);

	return 1;
}

int Dumpfile_Alert::chain_handler(kis_packet *in_pack) {
	if (alertfile == NULL)
		return 0;

	kis_alert_component *alrtinfo = NULL;

	if (in_pack->error)
		return 0;

	// Grab the alerts
	alrtinfo = (kis_alert_component *) in_pack->fetch(_PCM(PACK_COMP_ALERT));

	if (alrtinfo == NULL)
		return 0;

	for (unsigned int x = 0; x < alrtinfo->alert_vec.size(); x++) {
		fprintf(alertfile, "%.24s %s %d %s %s %s %s %s\n", 
				ctime((const time_t *) &(alrtinfo->alert_vec[x]->tm.tv_sec)),
				alrtinfo->alert_vec[x]->header.c_str(),
				alrtinfo->alert_vec[x]->channel,
				alrtinfo->alert_vec[x]->bssid.Mac2String().c_str(),
				alrtinfo->alert_vec[x]->source.Mac2String().c_str(),
				alrtinfo->alert_vec[x]->dest.Mac2String().c_str(),
				alrtinfo->alert_vec[x]->other.Mac2String().c_str(),
				alrtinfo->alert_vec[x]->text.c_str());
	}

	dumped_frames++;

	return 1;
}