log.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  secwatch - Copyright (C) 2006 Nic Stevens -- See COPYING for license details
00003 ------------------------------------------------------------------------------
00004  log.c - functions required for logging and e-mail alerts as well as the basic
00005  functons for parsing and returning configuration items related logging
00006 -----------------------------------------------------------------------------*/
00007 #ifdef HAVE_CONFIG_H
00008 #include "config.h"
00009 #endif
00010 #include <stdio.h>
00011 #include <stdarg.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <unistd.h>
00015 #include <sys/types.h>
00016 #include <sys/stat.h>
00017 #include <errno.h>
00018 #define SYSLOG_NAMES
00019 #include <syslog.h>
00020 #include <strutil.h>
00021 #include <configfile.h>
00022 
00026 int writeLog(const char *fmt, ...) {
00027      va_list ap;
00028      static int fac=-1, pri, e;
00029      if(fac == -1) {
00030           fac = confVarSyslog("logfac",&e);
00031           pri = confVarSyslog("logpri",&e);
00032      }
00033      openlog("secwatch",LOG_PID,fac);
00034      va_start(ap,fmt);
00035      vsyslog(pri,fmt,ap);
00036      va_end(ap);
00037      return 1;
00038 }
00042 int  inform(char *fmt, ...) {
00043      char cmdbuf[32767];
00044      char obuf[1024];
00045      int x;
00046      char * mailfrom; 
00047      char * mailto; 
00048      char * sendmail; 
00049      FILE *p;
00050      va_list ap;
00051 
00052      sendmail = confVarPath("sendmail",&x);
00053      mailfrom = confVarText("mailfrom",&x);
00054      mailto = confVarText("mailto",&x);
00055 
00056      va_start(ap,fmt);
00057      x = vsnprintf(obuf,sizeof(obuf),fmt,ap);
00058      va_end(ap);
00059 
00060      writeLog(obuf);
00061      sprintf(cmdbuf, "%s -F%s %s",sendmail,mailfrom,mailto);
00062      if((p = popen(cmdbuf,"w")) == NULL) {
00063           writeLog("Could not send mail [%s] %m",cmdbuf);
00064           return;
00065      }
00066      fprintf(p,"Subject: %s\n",obuf);
00067      fprintf(p,"subject: %s\n\n",obuf);
00068      fprintf(p,"%s\n",obuf);
00069      fclose(p);
00070      return x;
00071 }
00076 char * sysLogPriId(int pri) {
00077      int i;
00078      for(i = 0; prioritynames[i].c_name != NULL; i++) 
00079           if(prioritynames[i].c_val == pri)
00080                return prioritynames[i].c_name;
00081      return NULL;
00082 }
00087 int sysLogPri(char *name) {
00088      int i;
00089      for(i = 0; prioritynames[i].c_name != NULL; i++) 
00090           if(strcmp(name,prioritynames[i].c_name) == 0)
00091                return prioritynames[i].c_val;
00092      return -1;
00093 }
00098 char * sysLogFacId(int fac) {
00099      int i;
00100      for(i = 0; facilitynames[i].c_name != NULL; i++) 
00101           if(facilitynames[i].c_val == fac)
00102                return facilitynames[i].c_name;
00103      return NULL;
00104 }
00109 int sysLogFac(char *name) {
00110      int i;
00111      for(i = 0; facilitynames[i].c_name != NULL; i++) 
00112           if(strcmp(name,facilitynames[i].c_name) == 0)
00113                return facilitynames[i].c_val;
00114      return -1;
00115 }

Generated on Tue Oct 31 10:17:23 2006 for secwatch by  doxygen 1.4.6