exploit the possibilities
Home Files News &[SERVICES_TAB]About Contact Add New

sqlsyslogd.c

sqlsyslogd.c
Posted Apr 26, 2000
Authored by Venglin | Site freebsd.lublin.pl

syslogd-to-MySQL wrapper v0.1 prebeta. Stores syslog messages in a MySQL database. Written for FreeBSD.

systems | freebsd
SHA-256 | b44da69a92c3350c4053b05fab764bff3d6940f5eb010c5337cd82acb8578da7

sqlsyslogd.c

Change Mirror Download
/*
* (c) 2000 venglin / buffer0verfl0w security (www.b0f.com)
*
* syslogd-to-MySQL wrapper v0.1 prebeta (FreeBSD)
*
* 1. compile it: cc -O6 -Wall -pipe -m486 -L/usr/local/lib/mysql \
* -I/usr/local/include -o /usr/local/sbin/sqlsyslogd \
* sqlsyslogd.c -lmysqlclient
*
* 2. create table in some database:
* CREATE TABLE logs (
* id int(10) DEFAULT '0' NOT NULL auto_increment,
* timestamp varchar(16),
* host varchar(255),
* prog varchar(255),
* mesg text,
* PRIMARY KEY (id)
* );
*
* 3. put mysql password into /etc/sqlsyslogd.conf
*
* 4. add "*.* |/usr/local/sbin/sqlsyslogd [params]"
* to /etc/syslog.conf and rehash syslogd.
*
* $Log: sqlsyslogd.c,v $
* Revision 1.4 2000/04/19 08:55:13 venglin
* Small changes.
*
* Revision 1.3 2000/04/19 08:50:05 venglin
* Fixes.
*
* Revision 1.2 2000/04/18 19:53:40 venglin
* Fixes
*
* Revision 1.1 2000/04/18 19:51:25 venglin
* Initial revision
*
*/

#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

#define BUFSIZE 1024
#define CONF "/etc/sqlsyslogd.conf"
#define CR 13
#define LF 10

MYSQL db;

static char rcsid[] = "$Id: sqlsyslogd.c,v 1.4 2000/04/19 08:55:13 venglin Exp $";

void usage(av0)
char *av0;
{
fprintf(stderr, "usage: %s [-h hostname] <-u username> [-p]"
" <-t table> [database]\n\n", av0);
exit(0);
}

void cleanup(x)
int x;
{
mysql_close(&db);
exit(0);
}

char *password(void)
{
FILE *fp;
static char passwd[BUFSIZE/16];
char *p;

if ((fp=fopen(CONF, "r")) == NULL)
return NULL;

fgets(passwd, sizeof(passwd), fp);

if (p = index(passwd, CR))
*p = '\0';
if (p = index(passwd, LF))
*p = '\0';

return passwd;
}


int main(argc, argv)
int argc;
char **argv;
{
extern char *optarg;
extern int optind;
int ch;
char buf[BUFSIZE], querybuf[BUFSIZE+100];
char *loghost, *host, *user, *passwd, *av0, *table, *logprog, *logmesg;

av0 = argv[0];
loghost = host = user = passwd = table = logprog = logmesg = NULL;

while ((ch = getopt(argc, argv, "h:u:pt:")) != -1)
switch((char)ch)
{
case 'h':
host = optarg;
break;

case 'u':
user = optarg;
break;

case 'p':
passwd = password();
break;

case 't':
table = optarg;
break;

case '?':
default:
(void)usage(av0);
}

argc -= optind;
argv += optind;

if (!user || !table)
(void)usage(av0);

if (argc < 1)
(void)usage(av0);

mysql_init(&db);

if (!mysql_real_connect(&db, host, user, passwd, *argv, 0, NULL, 0))
{
fprintf(stderr, "failed to connect to database: %s\n",
mysql_error(&db));
exit(1);
}

signal(SIGHUP, cleanup);
signal(SIGINT, cleanup);
signal(SIGQUIT, cleanup);
signal(SIGTERM, cleanup);
signal(SIGSEGV, cleanup);
signal(SIGBUS, cleanup);

while(fgets(buf, sizeof(buf), stdin))
{
if (strlen(buf) > 18)
{
loghost = strtok(buf + 16, " ");
logprog = strtok(NULL, ":");
logmesg = buf + 16 + strlen(loghost) +
strlen(logprog) + 3;

if (loghost && logprog && logmesg)
{
snprintf(querybuf, sizeof(querybuf),
"INSERT INTO %s (timestamp, host, "
"prog, mesg) VALUES ('%.15s', '%s', "
"'%s', '%s')", table, buf,
loghost, logprog, logmesg);

if (mysql_query(&db, querybuf))
fprintf(stderr, "failed to run "
"query: %s\n",
mysql_error(&db));
}
}
}

mysql_close(&db);

exit(0);
}
Login or Register to add favorites

File Archive:

December 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Dec 1st
    0 Files
  • 2
    Dec 2nd
    41 Files
  • 3
    Dec 3rd
    0 Files
  • 4
    Dec 4th
    0 Files
  • 5
    Dec 5th
    0 Files
  • 6
    Dec 6th
    0 Files
  • 7
    Dec 7th
    0 Files
  • 8
    Dec 8th
    0 Files
  • 9
    Dec 9th
    0 Files
  • 10
    Dec 10th
    0 Files
  • 11
    Dec 11th
    0 Files
  • 12
    Dec 12th
    0 Files
  • 13
    Dec 13th
    0 Files
  • 14
    Dec 14th
    0 Files
  • 15
    Dec 15th
    0 Files
  • 16
    Dec 16th
    0 Files
  • 17
    Dec 17th
    0 Files
  • 18
    Dec 18th
    0 Files
  • 19
    Dec 19th
    0 Files
  • 20
    Dec 20th
    0 Files
  • 21
    Dec 21st
    0 Files
  • 22
    Dec 22nd
    0 Files
  • 23
    Dec 23rd
    0 Files
  • 24
    Dec 24th
    0 Files
  • 25
    Dec 25th
    0 Files
  • 26
    Dec 26th
    0 Files
  • 27
    Dec 27th
    0 Files
  • 28
    Dec 28th
    0 Files
  • 29
    Dec 29th
    0 Files
  • 30
    Dec 30th
    0 Files
  • 31
    Dec 31st
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2024 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close