/*    $Id: lib.c,v 1.12 2000/04/10 10:38:42 jap Exp $

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

#include "bcnu.h"

extern char progname[];
extern char *id;
extern int s;
extern char buf[], response[];
extern char portmsg[];
extern int dosyslog;

/*
   Print error messages to stderr
 */

bcnu_error (msg)
     char *msg;
{
  int myerr = 1;
  if (errno != 0)
    myerr = errno;

  if (dosyslog)
    {
      openlog (id, LOG_PID, LOG_DAEMON);
      syslog (LOG_NOTICE, "%s - %m", msg);
      closelog ();
    }

  perror (progname);
  fprintf (stderr, "%s: %s\n", progname, msg);
  exit (myerr);
}


bcnu_log (buf)
     char *buf;
{
  FILE *fp;
  long timevar;			/* contains time returned by time() */
  char rettime[80];
  char bcnulogfile[80];

  time (&timevar);
  bcnu_time (timevar, rettime);
  strcpy (bcnulogfile, bcnulog);
  strcat (bcnulogfile, "bcnu.log");
  setlock (bcnulogfile);
  if ((fp = fopen (bcnulogfile, "a")) == NULL)
    {
      bcnu_error ("Can't open log file");
    }
  fprintf (fp, "%s:%s:%s\n", rettime, id, buf);
  fclose (fp);
  rmlock (bcnulogfile);

}


bcnu_init ()
{

  bcnuaname = (char *) getenv ("BCNUAGENT");
  if ((bcnuptr = (char *) getenv ("BCNUHOME")) == NULL)
    strcpy (bcnuhome, BCNUHOME);
  else
    strcpy (bcnuhome, bcnuptr);
  strcpy (bcnulog, bcnuhome);
  strcat (bcnulog, "/logs/");
  strcpy (bcnumsg, bcnuhome);
  strcat (bcnumsg, "/data/");
  strcpy (bcnuagent, bcnuhome);
  strcat (bcnuagent, "/agent/");
  strcpy (bcnuetc, bcnuhome);
  strcat (bcnuetc, "/etc/");
  strcpy (bcnucfg, bcnuetc);
  strcat (bcnucfg, "agents");
  strcpy (bcnuflags, bcnuhome);
  strcat (bcnuflags, "/flags/");

}

void
signal_handler (sig)
     int sig;
{
  switch (sig)
    {
    case SIGTERM:
      fprintf (stderr, "%s: TERM signal received\n", progname);
      siglongjmp (env, sig);
    case SIGPIPE:
      fprintf (stderr, "%s: PIPE signal received\n", progname);
      siglongjmp (env, sig);
    case SIGALRM:
      siglongjmp (env, sig);
    case SIGHUP:
      siglongjmp (env, sig);
    default:
      fprintf (stderr, "%s: unexpected (%d) signal received\n", progname, sig);
      exit (sig);
    }
}
void
chld_handler (sig)
     int sig;
{
  int status;
  /* Wait for any child without blocking */
  if (waitpid (-1, &status, WNOHANG) < 0)
    {
      return;
    }
}

write_pid (pid)
     pid_t pid;
{
  FILE *fp;
  char pidfile[80];

  sprintf (pidfile, "%s/%s.pid", bcnuetc, id);

  if ((fp = fopen (pidfile, "w")) == NULL)
    {
      bcnu_error ("Can't open pid file");
    }

  fprintf (fp, "%d\n", pid);
  fclose (fp);
}

bcnu_time (timevar, rettime)
     long timevar;
     char *rettime;

{

  struct tm *settime;
  int yy;
  settime = localtime (&timevar);
  yy = (settime->tm_year + 1900);
  sprintf (rettime, "%4.4d-%2.2d-%2.2d %2.2d.%2.2d.%2.2d", yy, settime->tm_mon + 1, settime->tm_mday,
	   settime->tm_hour, settime->tm_min, settime->tm_sec);
}

bcnu_hour ()

{
  long timevar;
  struct tm *settime;
  time (&timevar);
  settime = localtime (&timevar);
  hour = settime->tm_hour;
  mins = settime->tm_min;
}


pump_data (len)
     int len;
{
  int i;
  if (send (s, buf, len, 0) != len)
    {
      bcnu_error ("Connection aborted on error");
    }

  i = recv (s, buf, BCNU_MESS, 0);
  if (i == -1)
    {
      bcnu_error ("error on recv-pump");
    }
  buf[i] = 0;
  return (i);
}

ack ()
{
  if (send (s, "OK", 2, 0) != 2)
    bcnu_error ("error from send ack");
}

ckfile (file)
     char *file;
{
  int ckstatus = 0;

  if (strpbrk (file, ";!*<>?"))
    ckstatus = 1;

  if (strstr (file, "./"))
    ckstatus = 1;

  if (ckstatus)
    sprintf (buf, "Invalid characters in file/directory name (%s)", file);

  return (ckstatus);
}

setlock (filename)
     char *filename;
{
  FILE *fp;
  char lockset[80];
  int f;

  sprintf (lockset, "%s.LCK", filename);

  for (f = 0; f < 10; f++)
    {
      if ((fp = fopen (lockset, "r")) == NULL)
	break;
      else
	{
	  sleep (1);
	}
      fclose (fp);

    }

  if ((fp = fopen (lockset, "w")) == NULL)
    bcnu_error ("Error setting lock");
  fclose (fp);
  return;
}

rmlock (filename)
     char *filename;
{
  char lockset[80];
  sprintf (lockset, "%s.LCK", filename);

  unlink (lockset);
}

bcnu_port (aport)
     char *aport;
{
  FILE *fp;

  char inservice[80], inport[80], inprot[12], line[BCNU_MESS + 1];

  bcnu_init ();

  sprintf (buf, "%s/etc/services", bcnuhome);

  if ((fp = fopen (buf, "r")) == NULL)
    {
      return (0);
    }
  while (fgets (line, BCNU_MESS, fp))
    {
      sscanf (line, "%s %[0-9]/%s %[^#\n]\n", inservice, inport, inprot, portmsg);
/*     printf("%s:[%s] [%s] [%s] [%s] %d\n",aport,inservice,inport,inprot,portmsg,strlen(portmsg)); 
 */
      if (strcasecmp (inservice, aport) == 0)
	{
	  return (atoi (inport));
	}
    }
  *portmsg = 0;
  return (0);
}
