/* pdumpq - Pcap Dump for Linux/Netfilter QUEUE * * Copyright (C) 2001 Ian Jones * * 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 /* init for mail buffer */ void mail_init (void) { char *decoder; if (access (decoder = strndup (MAILCMD, strcspn (MAILCMD, " ")), X_OK) != 0) crash (decoder); if ((queue = malloc (sizeof (Queue))) == NULL) crash ("malloc:mail_init"); memset (queue, 0, sizeof (Queue)); queue->stream = open_memstream (&queue->queue, &queue->size); if (write_file_header (queue->stream) != 1) crash ("mail_init, write_file_header"); } /* save pkt to buffer, set ALRM if it's the first */ void mail_enqueue (ipq_packet_msg_t * m) { if (dump_pkt (m, queue->stream)) queue->num++; else crash ("enqueueing mail buffer:pdumpq"); if (queue->num == 1) alarm (v.timer); /* set the timer, first alert in queue */ if (queue->num == v.maxq) queue->flush = 1; /* flush to mail if capacity is reached */ } /* flush to mail and reset buffer */ void mail (void) { int nbytes; FILE *mailst; char email[strlen (MAILCMD) + strlen (v.email) + 1]; stpcpy (stpcpy (email, MAILCMD), v.email); alarm (0); if ((mailst = popen (email, "w")) == NULL) crash (email); for (nbytes = 0; nbytes < queue->size; nbytes += TEMP_FAILURE_RETRY (write (fileno (mailst), queue->queue, queue->size))); fflush (mailst); syslog (LOG_INFO, "Flushed %d packets to mail", queue->num); if (pclose (mailst) == -1) crash ("closing mail()"); rewind (queue->stream); if (write_file_header (queue->stream) != 1) crash ("mail(), write_file_header"); queue->num = queue->flush = 0; }