/*    $Id: log.c,v 1.6 1999/09/23 12:03:54 jap Exp $

 *    Copyright (c) 1998 John Phillips
 *
 *    You may distribute under the terms of the Artistic License
 *
 */

#include "bcnu.h"

extern struct bcnu_agent_def bcnud_agent[];
extern struct bcnu_msg_def bcnud_in;
extern char progname[];
extern char *id, *subver;
extern char starttime[];
extern char buf[], response[];
extern int s, masterok, errorlevel;

int err, msg, agt;
char time_logged[30], bcnud_time_logged[30];


#if MYSQLDATA

#include "mysql.h"

bcnu_write_data ()
{
  int count, num;
  MYSQL *sock, mysql;
  char bcnulogfile[180];

  if (!(sock = mysql_connect (&mysql, NULL, 0, 0)))
    {
      sprintf (workbuf, "Couldn't connect to engine!\n%s\n", mysql_error (&mysql));
      bcnu_error (workbuf);
    }

  strcpy (bcnulogfile, "msg");

  if (mysql_select_db (sock, "bcnu") < 0)
    {
      sprintf (workbuf, "Couldn't select database bcnu!\n%s\n",
	       mysql_error (sock));
      bcnu_error (workbuf);
    }

  bcnu_time (bcnud_in.time_logged, time_logged);
  bcnu_time (bcnud_in.bcnud_time_logged, bcnud_time_logged);

  sprintf (workbuf, "insert into %s values (NULL,'%s',%d,%d,'%s','%s','%s','%s','%s','%s','%s',%d,'%s')", bcnulogfile,
	   bcnud_in.msg_type, bcnud_in.severity, bcnud_in.host_type,bcnud_in.host_for, bcnud_in.host_from, bcnud_in.agent_from, time_logged,
	   bcnud_in.log_info, bcnud_in.bcnud_host_from, bcnud_time_logged,
	   bcnud_in.bcnud_file, bcnud_in.bcnud_filename);

  if (mysql_query (sock, workbuf) < 0)
    {
      sprintf (workbuf, "insert failed (%s)\n", mysql_error (sock));
      bcnu_error (workbuf);
    }
  mysql_close (sock);
  if (bcnud_in.severity != BCNU_NOLOG)
    bcnu_write_status ();
}

#else

bcnu_write_data ()
{

  FILE *fp;
  char bcnulogfile[180];
  long timevar;
  struct tm *settime;
  int yy;

  time (&timevar);
  settime = localtime (&timevar);
  yy=(settime->tm_year + 1900);
  sprintf (bcnulogfile, "%s%4.4d%2.2d%2.2d.log", bcnumsg, yy, settime->tm_mon + 1, settime->tm_mday);

  if (debug)
    printf ("logging to %s\n", bcnulogfile);

  setlock (bcnulogfile);
  if ((fp = fopen (bcnulogfile, "a")) == NULL)
    {
      bcnu_error ("Can't open data log file");
    }

  bcnu_time (bcnud_in.time_logged, time_logged);
  bcnu_time (bcnud_in.bcnud_time_logged, bcnud_time_logged);

  fprintf (fp, "%s:%d:%s:%s:%s:%s:%s:%s:%s:%d:%s:%d\n",
	   bcnud_in.msg_type, bcnud_in.severity, bcnud_in.host_for, bcnud_in.host_from, bcnud_in.agent_from, time_logged,
	   bcnud_in.log_info, bcnud_in.bcnud_host_from, bcnud_time_logged,
	   bcnud_in.bcnud_file, bcnud_in.bcnud_filename, bcnud_in.host_type);
  fclose (fp);
  rmlock (bcnulogfile);
  if (bcnud_in.severity != BCNU_NOLOG)
    bcnu_write_status ();
}
#endif

bcnu_write_status ()
{

  FILE *fp;
  char bcnulogfile[180];
  char bcnulogdir[140];
  struct stat statbuf;

  sprintf (bcnulogfile, "%s/agents/%s", bcnumsg, bcnud_in.agent_from);
  if (fp=fopen (bcnulogfile, "w"))
	  fclose(fp);

  sprintf (bcnulogdir, "%s/hosts/%s", bcnumsg, bcnud_in.host_for);

  if (stat (bcnulogdir, &statbuf) != 0)
    mkdir (bcnulogdir, 0755);

  sprintf (bcnulogfile, "%s/%s", bcnulogdir, bcnud_in.agent_from);

  if (debug)
    printf ("logging to file [%s] [%s] [%s]\n", bcnulogfile,bcnulogdir,bcnud_in.agent_from);
  if ((fp = fopen (bcnulogfile, "w")) == NULL)
    {
      bcnu_error ("Can't open data log file");
    }

  fprintf (fp, "%s:%d:%s:%s:%s:%s:%s:%s:%s:%d:%s:%d\n",
	   bcnud_in.msg_type, bcnud_in.severity, bcnud_in.host_for, bcnud_in.host_from, bcnud_in.agent_from, time_logged,
	   bcnud_in.log_info, bcnud_in.bcnud_host_from, bcnud_time_logged,
	   bcnud_in.bcnud_file, bcnud_in.bcnud_filename,bcnud_in.host_type);
  fclose (fp);

  if (errorlevel >= bcnud_in.severity)
    return;

/* Alert handling - host level*/
  sprintf (bcnulogfile, "%s/flags/%s.ignore", bcnuhome, bcnud_in.host_for);

/* if the ignore file is there, don't write an error */
  if (stat (bcnulogfile, &statbuf) == 0)
    {
      sprintf (buf, "Ignoring: ALERT for %s,from host %s by agent %s @ %s - %s",
	       bcnud_in.host_for, bcnud_in.host_from, 
	       bcnud_in.agent_from, time_logged, bcnud_in.log_info);
      bcnu_log (buf);
      return;
    }

/* Alert handling - agent level*/
  sprintf (bcnulogfile, "%s/flags/%s-%s.ignore", bcnuhome, bcnud_in.host_for, bcnud_in.agent_from);

/* if the ignore file is there, don't write an error */
  if (stat (bcnulogfile, &statbuf) == 0)
    {
      sprintf (buf, "Ignoring: ALERT for %s-%s,from host %s by agent %s @ %s - %s",
	       bcnud_in.host_for, bcnud_in.agent_from, bcnud_in.host_from, 
	       bcnud_in.agent_from, time_logged, bcnud_in.log_info);
      bcnu_log (buf);
      return;
    }

  sprintf (bcnulogfile, "%s/flags/bcnud.alert", bcnuhome);

  if ((fp = fopen (bcnulogfile, "a")) == NULL)
    {
      bcnu_error ("Can't open error/warning file");
    }

  fprintf (fp, "%s %s %s %s\n",
    bcnud_in.host_for, bcnud_in.agent_from, time_logged,bcnud_in.log_info);
  fclose (fp);

}

bcnu_register (hostname)
char * hostname;
{

  FILE *fp;
  char bcnulogfile[180];
  char bcnulogdir[140];
  struct stat statbuf;

  sprintf (bcnulogdir, "%s/win32alert", bcnumsg);

  if (stat (bcnulogdir, &statbuf) != 0)
    mkdir (bcnulogdir, 0755);

  sprintf (bcnulogfile, "%s/%s", bcnulogdir, bcnud_in.bcnud_filename);

  if (debug)
    printf ("logging to file [%s] [%s] [%s]\n", bcnulogfile,bcnulogdir,bcnud_in.bcnud_filename);
  if ((fp = fopen (bcnulogfile, "w")) == NULL)
    {
      bcnu_error ("Can't open win32alert file");
    }

  fprintf (fp, "%s\n",hostname);
  fclose (fp);

}
